Run util/openssl-format-source -v -c .
[openssl.git] / apps / speed.c
1 /* apps/speed.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  *
61  * Portions of the attached software ("Contribution") are developed by
62  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63  *
64  * The Contribution is licensed pursuant to the OpenSSL open source
65  * license provided above.
66  *
67  * The ECDH and ECDSA speed test software is originally written by
68  * Sumit Gupta of Sun Microsystems Laboratories.
69  *
70  */
71
72 /* most of this code has been pilfered from my libdes speed.c program */
73
74 #ifndef OPENSSL_NO_SPEED
75
76 # undef SECONDS
77 # define SECONDS                 3
78 # define PRIME_SECONDS   10
79 # define RSA_SECONDS             10
80 # define DSA_SECONDS             10
81 # define ECDSA_SECONDS   10
82 # define ECDH_SECONDS    10
83
84 /* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
85 /* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
86
87 # undef PROG
88 # define PROG speed_main
89
90 # include <stdio.h>
91 # include <stdlib.h>
92
93 # include <string.h>
94 # include <math.h>
95 # include "apps.h"
96 # include <openssl/crypto.h>
97 # include <openssl/rand.h>
98 # include <openssl/err.h>
99 # include <openssl/evp.h>
100 # include <openssl/objects.h>
101 # if !defined(OPENSSL_SYS_MSDOS)
102 #  include OPENSSL_UNISTD
103 # endif
104
105 # ifndef OPENSSL_SYS_NETWARE
106 #  include <signal.h>
107 # endif
108
109 # if defined(_WIN32) || defined(__CYGWIN__)
110 #  include <windows.h>
111 #  if defined(__CYGWIN__) && !defined(_WIN32)
112   /*
113    * <windows.h> should define _WIN32, which normally is mutually exclusive
114    * with __CYGWIN__, but if it didn't...
115    */
116 #   define _WIN32
117   /* this is done because Cygwin alarm() fails sometimes. */
118 #  endif
119 # endif
120
121 # include <openssl/bn.h>
122 # ifndef OPENSSL_NO_DES
123 #  include <openssl/des.h>
124 # endif
125 # ifndef OPENSSL_NO_AES
126 #  include <openssl/aes.h>
127 # endif
128 # ifndef OPENSSL_NO_CAMELLIA
129 #  include <openssl/camellia.h>
130 # endif
131 # ifndef OPENSSL_NO_MD2
132 #  include <openssl/md2.h>
133 # endif
134 # ifndef OPENSSL_NO_MDC2
135 #  include <openssl/mdc2.h>
136 # endif
137 # ifndef OPENSSL_NO_MD4
138 #  include <openssl/md4.h>
139 # endif
140 # ifndef OPENSSL_NO_MD5
141 #  include <openssl/md5.h>
142 # endif
143 # ifndef OPENSSL_NO_HMAC
144 #  include <openssl/hmac.h>
145 # endif
146 # include <openssl/evp.h>
147 # ifndef OPENSSL_NO_SHA
148 #  include <openssl/sha.h>
149 # endif
150 # ifndef OPENSSL_NO_RMD160
151 #  include <openssl/ripemd.h>
152 # endif
153 # ifndef OPENSSL_NO_WHIRLPOOL
154 #  include <openssl/whrlpool.h>
155 # endif
156 # ifndef OPENSSL_NO_RC4
157 #  include <openssl/rc4.h>
158 # endif
159 # ifndef OPENSSL_NO_RC5
160 #  include <openssl/rc5.h>
161 # endif
162 # ifndef OPENSSL_NO_RC2
163 #  include <openssl/rc2.h>
164 # endif
165 # ifndef OPENSSL_NO_IDEA
166 #  include <openssl/idea.h>
167 # endif
168 # ifndef OPENSSL_NO_SEED
169 #  include <openssl/seed.h>
170 # endif
171 # ifndef OPENSSL_NO_BF
172 #  include <openssl/blowfish.h>
173 # endif
174 # ifndef OPENSSL_NO_CAST
175 #  include <openssl/cast.h>
176 # endif
177 # ifndef OPENSSL_NO_RSA
178 #  include <openssl/rsa.h>
179 #  include "./testrsa.h"
180 # endif
181 # include <openssl/x509.h>
182 # ifndef OPENSSL_NO_DSA
183 #  include <openssl/dsa.h>
184 #  include "./testdsa.h"
185 # endif
186 # ifndef OPENSSL_NO_ECDSA
187 #  include <openssl/ecdsa.h>
188 # endif
189 # ifndef OPENSSL_NO_ECDH
190 #  include <openssl/ecdh.h>
191 # endif
192 # include <openssl/modes.h>
193
194 # include <openssl/bn.h>
195
196 # ifndef HAVE_FORK
197 #  if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_NETWARE)
198 #   define HAVE_FORK 0
199 #  else
200 #   define HAVE_FORK 1
201 #  endif
202 # endif
203
204 # if HAVE_FORK
205 #  undef NO_FORK
206 # else
207 #  define NO_FORK
208 # endif
209
210 # undef BUFSIZE
211 # define BUFSIZE (1024*8+1)
212 # define MAX_MISALIGNMENT 63
213
214 static volatile int run = 0;
215
216 static int mr = 0;
217 static int usertime = 1;
218
219 static double Time_F(int s);
220 static void print_message(const char *s, long num, int length);
221 static void pkey_print_message(const char *str, const char *str2,
222                                long num, int bits, int sec);
223 static void print_result(int alg, int run_no, int count, double time_used);
224 # ifndef NO_FORK
225 static int do_multi(int multi);
226 # endif
227
228 # define ALGOR_NUM       30
229 # define SIZE_NUM        5
230 # define PRIME_NUM       3
231 # define RSA_NUM         7
232 # define DSA_NUM         3
233
234 # define EC_NUM       16
235 # define MAX_ECDH_SIZE 256
236
237 static const char *names[ALGOR_NUM] = {
238     "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
239     "des cbc", "des ede3", "idea cbc", "seed cbc",
240     "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
241     "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
242     "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
243     "evp", "sha256", "sha512", "whirlpool",
244     "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash"
245 };
246
247 static double results[ALGOR_NUM][SIZE_NUM];
248 static int lengths[SIZE_NUM] = { 16, 64, 256, 1024, 8 * 1024 };
249
250 # ifndef OPENSSL_NO_RSA
251 static double rsa_results[RSA_NUM][2];
252 # endif
253 # ifndef OPENSSL_NO_DSA
254 static double dsa_results[DSA_NUM][2];
255 # endif
256 # ifndef OPENSSL_NO_ECDSA
257 static double ecdsa_results[EC_NUM][2];
258 # endif
259 # ifndef OPENSSL_NO_ECDH
260 static double ecdh_results[EC_NUM][1];
261 # endif
262
263 # if defined(OPENSSL_NO_DSA) && !(defined(OPENSSL_NO_ECDSA) && defined(OPENSSL_NO_ECDH))
264 static const char rnd_seed[] =
265     "string to make the random number generator think it has entropy";
266 static int rnd_fake = 0;
267 # endif
268
269 # ifdef SIGALRM
270 #  if defined(__STDC__) || defined(sgi) || defined(_AIX)
271 #   define SIGRETTYPE void
272 #  else
273 #   define SIGRETTYPE int
274 #  endif
275
276 static SIGRETTYPE sig_done(int sig);
277 static SIGRETTYPE sig_done(int sig)
278 {
279     signal(SIGALRM, sig_done);
280     run = 0;
281 }
282 # endif
283
284 # define START   0
285 # define STOP    1
286
287 # if defined(_WIN32)
288
289 #  if !defined(SIGALRM)
290 #   define SIGALRM
291 #  endif
292 static unsigned int lapse, schlock;
293 static void alarm_win32(unsigned int secs)
294 {
295     lapse = secs * 1000;
296 }
297
298 #  define alarm alarm_win32
299
300 static DWORD WINAPI sleepy(VOID * arg)
301 {
302     schlock = 1;
303     Sleep(lapse);
304     run = 0;
305     return 0;
306 }
307
308 static double Time_F(int s)
309 {
310     double ret;
311     static HANDLE thr;
312
313     if (s == START) {
314         schlock = 0;
315         thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
316         if (thr == NULL) {
317             DWORD ret = GetLastError();
318             BIO_printf(bio_err, "unable to CreateThread (%d)", ret);
319             ExitProcess(ret);
320         }
321         while (!schlock)
322             Sleep(0);           /* scheduler spinlock */
323         ret = app_tminterval(s, usertime);
324     } else {
325         ret = app_tminterval(s, usertime);
326         if (run)
327             TerminateThread(thr, 0);
328         CloseHandle(thr);
329     }
330
331     return ret;
332 }
333 # else
334
335 static double Time_F(int s)
336 {
337     double ret = app_tminterval(s, usertime);
338     if (s == STOP)
339         alarm(0);
340     return ret;
341 }
342 # endif
343
344 # ifndef OPENSSL_NO_ECDH
345 static const int KDF1_SHA1_len = 20;
346 static void *KDF1_SHA1(const void *in, size_t inlen, void *out,
347                        size_t *outlen)
348 {
349 #  ifndef OPENSSL_NO_SHA
350     if (*outlen < SHA_DIGEST_LENGTH)
351         return NULL;
352     else
353         *outlen = SHA_DIGEST_LENGTH;
354     return SHA1(in, inlen, out);
355 #  else
356     return NULL;
357 #  endif                        /* OPENSSL_NO_SHA */
358 }
359 # endif                         /* OPENSSL_NO_ECDH */
360
361 static void multiblock_speed(const EVP_CIPHER *evp_cipher);
362
363 int MAIN(int, char **);
364
365 int MAIN(int argc, char **argv)
366 {
367     unsigned char *buf_malloc = NULL, *buf2_malloc = NULL;
368     unsigned char *buf = NULL, *buf2 = NULL;
369     int mret = 1;
370     long count = 0, save_count = 0;
371     int i, j, k;
372 # if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
373     long rsa_count;
374 # endif
375 # ifndef OPENSSL_NO_RSA
376     unsigned rsa_num;
377 # endif
378     unsigned char md[EVP_MAX_MD_SIZE];
379 # ifndef OPENSSL_NO_MD2
380     unsigned char md2[MD2_DIGEST_LENGTH];
381 # endif
382 # ifndef OPENSSL_NO_MDC2
383     unsigned char mdc2[MDC2_DIGEST_LENGTH];
384 # endif
385 # ifndef OPENSSL_NO_MD4
386     unsigned char md4[MD4_DIGEST_LENGTH];
387 # endif
388 # ifndef OPENSSL_NO_MD5
389     unsigned char md5[MD5_DIGEST_LENGTH];
390     unsigned char hmac[MD5_DIGEST_LENGTH];
391 # endif
392 # ifndef OPENSSL_NO_SHA
393     unsigned char sha[SHA_DIGEST_LENGTH];
394 #  ifndef OPENSSL_NO_SHA256
395     unsigned char sha256[SHA256_DIGEST_LENGTH];
396 #  endif
397 #  ifndef OPENSSL_NO_SHA512
398     unsigned char sha512[SHA512_DIGEST_LENGTH];
399 #  endif
400 # endif
401 # ifndef OPENSSL_NO_WHIRLPOOL
402     unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
403 # endif
404 # ifndef OPENSSL_NO_RMD160
405     unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
406 # endif
407 # ifndef OPENSSL_NO_RC4
408     RC4_KEY rc4_ks;
409 # endif
410 # ifndef OPENSSL_NO_RC5
411     RC5_32_KEY rc5_ks;
412 # endif
413 # ifndef OPENSSL_NO_RC2
414     RC2_KEY rc2_ks;
415 # endif
416 # ifndef OPENSSL_NO_IDEA
417     IDEA_KEY_SCHEDULE idea_ks;
418 # endif
419 # ifndef OPENSSL_NO_SEED
420     SEED_KEY_SCHEDULE seed_ks;
421 # endif
422 # ifndef OPENSSL_NO_BF
423     BF_KEY bf_ks;
424 # endif
425 # ifndef OPENSSL_NO_CAST
426     CAST_KEY cast_ks;
427 # endif
428     static const unsigned char key16[16] = {
429         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
430         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
431     };
432 # ifndef OPENSSL_NO_AES
433     static const unsigned char key24[24] = {
434         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
435         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
436         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
437     };
438     static const unsigned char key32[32] = {
439         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
440         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
441         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
442         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
443     };
444 # endif
445 # ifndef OPENSSL_NO_CAMELLIA
446     static const unsigned char ckey24[24] = {
447         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
448         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
449         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
450     };
451     static const unsigned char ckey32[32] = {
452         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
453         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
454         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
455         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
456     };
457 # endif
458 # ifndef OPENSSL_NO_AES
459 #  define MAX_BLOCK_SIZE 128
460 # else
461 #  define MAX_BLOCK_SIZE 64
462 # endif
463     unsigned char DES_iv[8];
464     unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
465 # ifndef OPENSSL_NO_DES
466     static DES_cblock key =
467         { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
468     static DES_cblock key2 =
469         { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 };
470     static DES_cblock key3 =
471         { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 };
472     DES_key_schedule sch;
473     DES_key_schedule sch2;
474     DES_key_schedule sch3;
475 # endif
476 # ifndef OPENSSL_NO_AES
477     AES_KEY aes_ks1, aes_ks2, aes_ks3;
478 # endif
479 # ifndef OPENSSL_NO_CAMELLIA
480     CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
481 # endif
482 # define D_MD2           0
483 # define D_MDC2          1
484 # define D_MD4           2
485 # define D_MD5           3
486 # define D_HMAC          4
487 # define D_SHA1          5
488 # define D_RMD160        6
489 # define D_RC4           7
490 # define D_CBC_DES       8
491 # define D_EDE3_DES      9
492 # define D_CBC_IDEA      10
493 # define D_CBC_SEED      11
494 # define D_CBC_RC2       12
495 # define D_CBC_RC5       13
496 # define D_CBC_BF        14
497 # define D_CBC_CAST      15
498 # define D_CBC_128_AES   16
499 # define D_CBC_192_AES   17
500 # define D_CBC_256_AES   18
501 # define D_CBC_128_CML   19
502 # define D_CBC_192_CML   20
503 # define D_CBC_256_CML   21
504 # define D_EVP           22
505 # define D_SHA256        23
506 # define D_SHA512        24
507 # define D_WHIRLPOOL     25
508 # define D_IGE_128_AES   26
509 # define D_IGE_192_AES   27
510 # define D_IGE_256_AES   28
511 # define D_GHASH         29
512     double d = 0.0;
513     long c[ALGOR_NUM][SIZE_NUM];
514
515 # ifndef OPENSSL_SYS_WIN32
516 # endif
517 # define R_DSA_512       0
518 # define R_DSA_1024      1
519 # define R_DSA_2048      2
520 # define R_RSA_512       0
521 # define R_RSA_1024      1
522 # define R_RSA_2048      2
523 # define R_RSA_3072      3
524 # define R_RSA_4096      4
525 # define R_RSA_7680      5
526 # define R_RSA_15360     6
527
528 # define R_EC_P160    0
529 # define R_EC_P192    1
530 # define R_EC_P224    2
531 # define R_EC_P256    3
532 # define R_EC_P384    4
533 # define R_EC_P521    5
534 # define R_EC_K163    6
535 # define R_EC_K233    7
536 # define R_EC_K283    8
537 # define R_EC_K409    9
538 # define R_EC_K571    10
539 # define R_EC_B163    11
540 # define R_EC_B233    12
541 # define R_EC_B283    13
542 # define R_EC_B409    14
543 # define R_EC_B571    15
544
545 # ifndef OPENSSL_NO_RSA
546     RSA *rsa_key[RSA_NUM];
547     long rsa_c[RSA_NUM][2];
548     static unsigned int rsa_bits[RSA_NUM] = {
549         512, 1024, 2048, 3072, 4096, 7680, 15360
550     };
551     static unsigned char *rsa_data[RSA_NUM] = {
552         test512, test1024, test2048, test3072, test4096, test7680, test15360
553     };
554     static int rsa_data_length[RSA_NUM] = {
555         sizeof(test512), sizeof(test1024),
556         sizeof(test2048), sizeof(test3072),
557         sizeof(test4096), sizeof(test7680),
558         sizeof(test15360)
559     };
560 # endif
561 # ifndef OPENSSL_NO_DSA
562     DSA *dsa_key[DSA_NUM];
563     long dsa_c[DSA_NUM][2];
564     static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
565 # endif
566 # ifndef OPENSSL_NO_EC
567     /*
568      * We only test over the following curves as they are representative, To
569      * add tests over more curves, simply add the curve NID and curve name to
570      * the following arrays and increase the EC_NUM value accordingly.
571      */
572     static unsigned int test_curves[EC_NUM] = {
573         /* Prime Curves */
574         NID_secp160r1,
575         NID_X9_62_prime192v1,
576         NID_secp224r1,
577         NID_X9_62_prime256v1,
578         NID_secp384r1,
579         NID_secp521r1,
580         /* Binary Curves */
581         NID_sect163k1,
582         NID_sect233k1,
583         NID_sect283k1,
584         NID_sect409k1,
585         NID_sect571k1,
586         NID_sect163r2,
587         NID_sect233r1,
588         NID_sect283r1,
589         NID_sect409r1,
590         NID_sect571r1
591     };
592     static const char *test_curves_names[EC_NUM] = {
593         /* Prime Curves */
594         "secp160r1",
595         "nistp192",
596         "nistp224",
597         "nistp256",
598         "nistp384",
599         "nistp521",
600         /* Binary Curves */
601         "nistk163",
602         "nistk233",
603         "nistk283",
604         "nistk409",
605         "nistk571",
606         "nistb163",
607         "nistb233",
608         "nistb283",
609         "nistb409",
610         "nistb571"
611     };
612     static int test_curves_bits[EC_NUM] = {
613         160, 192, 224, 256, 384, 521,
614         163, 233, 283, 409, 571,
615         163, 233, 283, 409, 571
616     };
617
618 # endif
619
620 # ifndef OPENSSL_NO_ECDSA
621     unsigned char ecdsasig[256];
622     unsigned int ecdsasiglen;
623     EC_KEY *ecdsa[EC_NUM];
624     long ecdsa_c[EC_NUM][2];
625 # endif
626
627 # ifndef OPENSSL_NO_ECDH
628     EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM];
629     unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE];
630     int secret_size_a, secret_size_b;
631     int ecdh_checks = 0;
632     int secret_idx = 0;
633     long ecdh_c[EC_NUM][2];
634 # endif
635
636     int rsa_doit[RSA_NUM];
637     int dsa_doit[DSA_NUM];
638 # ifndef OPENSSL_NO_ECDSA
639     int ecdsa_doit[EC_NUM];
640 # endif
641 # ifndef OPENSSL_NO_ECDH
642     int ecdh_doit[EC_NUM];
643 # endif
644     int doit[ALGOR_NUM];
645     int pr_header = 0;
646     const EVP_CIPHER *evp_cipher = NULL;
647     const EVP_MD *evp_md = NULL;
648     int decrypt = 0;
649 # ifndef NO_FORK
650     int multi = 0;
651 # endif
652     int multiblock = 0;
653     int misalign = MAX_MISALIGNMENT + 1;
654
655 # ifndef TIMES
656     usertime = -1;
657 # endif
658
659     apps_startup();
660     memset(results, 0, sizeof(results));
661 # ifndef OPENSSL_NO_DSA
662     memset(dsa_key, 0, sizeof(dsa_key));
663 # endif
664 # ifndef OPENSSL_NO_ECDSA
665     for (i = 0; i < EC_NUM; i++)
666         ecdsa[i] = NULL;
667 # endif
668 # ifndef OPENSSL_NO_ECDH
669     for (i = 0; i < EC_NUM; i++) {
670         ecdh_a[i] = NULL;
671         ecdh_b[i] = NULL;
672     }
673 # endif
674
675     if (bio_err == NULL)
676         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
677             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
678
679     if (!load_config(bio_err, NULL))
680         goto end;
681
682 # ifndef OPENSSL_NO_RSA
683     memset(rsa_key, 0, sizeof(rsa_key));
684     for (i = 0; i < RSA_NUM; i++)
685         rsa_key[i] = NULL;
686 # endif
687
688     if ((buf_malloc =
689          (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {
690         BIO_printf(bio_err, "out of memory\n");
691         goto end;
692     }
693     if ((buf2_malloc =
694          (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {
695         BIO_printf(bio_err, "out of memory\n");
696         goto end;
697     }
698
699     misalign = 0;               /* set later and buf/buf2 are adjusted
700                                  * accordingly */
701     buf = buf_malloc;
702     buf2 = buf2_malloc;
703
704     memset(c, 0, sizeof(c));
705     memset(DES_iv, 0, sizeof(DES_iv));
706     memset(iv, 0, sizeof(iv));
707
708     for (i = 0; i < ALGOR_NUM; i++)
709         doit[i] = 0;
710     for (i = 0; i < RSA_NUM; i++)
711         rsa_doit[i] = 0;
712     for (i = 0; i < DSA_NUM; i++)
713         dsa_doit[i] = 0;
714 # ifndef OPENSSL_NO_ECDSA
715     for (i = 0; i < EC_NUM; i++)
716         ecdsa_doit[i] = 0;
717 # endif
718 # ifndef OPENSSL_NO_ECDH
719     for (i = 0; i < EC_NUM; i++)
720         ecdh_doit[i] = 0;
721 # endif
722
723     j = 0;
724     argc--;
725     argv++;
726     while (argc) {
727         if ((argc > 0) && (strcmp(*argv, "-elapsed") == 0)) {
728             usertime = 0;
729             j--;                /* Otherwise, -elapsed gets confused with an
730                                  * algorithm. */
731         } else if ((argc > 0) && (strcmp(*argv, "-evp") == 0)) {
732             argc--;
733             argv++;
734             if (argc == 0) {
735                 BIO_printf(bio_err, "no EVP given\n");
736                 goto end;
737             }
738             evp_cipher = EVP_get_cipherbyname(*argv);
739             if (!evp_cipher) {
740                 evp_md = EVP_get_digestbyname(*argv);
741             }
742             if (!evp_cipher && !evp_md) {
743                 BIO_printf(bio_err, "%s is an unknown cipher or digest\n",
744                            *argv);
745                 goto end;
746             }
747             doit[D_EVP] = 1;
748         } else if (argc > 0 && !strcmp(*argv, "-decrypt")) {
749             decrypt = 1;
750             j--;                /* Otherwise, -elapsed gets confused with an
751                                  * algorithm. */
752         }
753 # ifndef OPENSSL_NO_ENGINE
754         else if ((argc > 0) && (strcmp(*argv, "-engine") == 0)) {
755             argc--;
756             argv++;
757             if (argc == 0) {
758                 BIO_printf(bio_err, "no engine given\n");
759                 goto end;
760             }
761             setup_engine(bio_err, *argv, 0);
762             /*
763              * j will be increased again further down.  We just don't want
764              * speed to confuse an engine with an algorithm, especially when
765              * none is given (which means all of them should be run)
766              */
767             j--;
768         }
769 # endif
770 # ifndef NO_FORK
771         else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) {
772             argc--;
773             argv++;
774             if (argc == 0) {
775                 BIO_printf(bio_err, "no multi count given\n");
776                 goto end;
777             }
778             multi = atoi(argv[0]);
779             if (multi <= 0) {
780                 BIO_printf(bio_err, "bad multi count\n");
781                 goto end;
782             }
783             j--;                /* Otherwise, -mr gets confused with an
784                                  * algorithm. */
785         }
786 # endif
787         else if (argc > 0 && !strcmp(*argv, "-mr")) {
788             mr = 1;
789             j--;                /* Otherwise, -mr gets confused with an
790                                  * algorithm. */
791         } else if (argc > 0 && !strcmp(*argv, "-mb")) {
792             multiblock = 1;
793             j--;
794         } else if (argc > 0 && !strcmp(*argv, "-misalign")) {
795             argc--;
796             argv++;
797             if (argc == 0) {
798                 BIO_printf(bio_err, "no misalignment given\n");
799                 goto end;
800             }
801             misalign = atoi(argv[0]);
802             if (misalign < 0 || misalign > MAX_MISALIGNMENT) {
803                 BIO_printf(bio_err,
804                            "misalignment is outsize permitted range 0-%d\n",
805                            MAX_MISALIGNMENT);
806                 goto end;
807             }
808             buf = buf_malloc + misalign;
809             buf2 = buf2_malloc + misalign;
810             j--;
811         } else
812 # ifndef OPENSSL_NO_MD2
813         if (strcmp(*argv, "md2") == 0)
814             doit[D_MD2] = 1;
815         else
816 # endif
817 # ifndef OPENSSL_NO_MDC2
818         if (strcmp(*argv, "mdc2") == 0)
819             doit[D_MDC2] = 1;
820         else
821 # endif
822 # ifndef OPENSSL_NO_MD4
823         if (strcmp(*argv, "md4") == 0)
824             doit[D_MD4] = 1;
825         else
826 # endif
827 # ifndef OPENSSL_NO_MD5
828         if (strcmp(*argv, "md5") == 0)
829             doit[D_MD5] = 1;
830         else
831 # endif
832 # ifndef OPENSSL_NO_MD5
833         if (strcmp(*argv, "hmac") == 0)
834             doit[D_HMAC] = 1;
835         else
836 # endif
837 # ifndef OPENSSL_NO_SHA
838         if (strcmp(*argv, "sha1") == 0)
839             doit[D_SHA1] = 1;
840         else if (strcmp(*argv, "sha") == 0)
841             doit[D_SHA1] = 1, doit[D_SHA256] = 1, doit[D_SHA512] = 1;
842         else
843 #  ifndef OPENSSL_NO_SHA256
844         if (strcmp(*argv, "sha256") == 0)
845             doit[D_SHA256] = 1;
846         else
847 #  endif
848 #  ifndef OPENSSL_NO_SHA512
849         if (strcmp(*argv, "sha512") == 0)
850             doit[D_SHA512] = 1;
851         else
852 #  endif
853 # endif
854 # ifndef OPENSSL_NO_WHIRLPOOL
855         if (strcmp(*argv, "whirlpool") == 0)
856             doit[D_WHIRLPOOL] = 1;
857         else
858 # endif
859 # ifndef OPENSSL_NO_RMD160
860         if (strcmp(*argv, "ripemd") == 0)
861             doit[D_RMD160] = 1;
862         else if (strcmp(*argv, "rmd160") == 0)
863             doit[D_RMD160] = 1;
864         else if (strcmp(*argv, "ripemd160") == 0)
865             doit[D_RMD160] = 1;
866         else
867 # endif
868 # ifndef OPENSSL_NO_RC4
869         if (strcmp(*argv, "rc4") == 0)
870             doit[D_RC4] = 1;
871         else
872 # endif
873 # ifndef OPENSSL_NO_DES
874         if (strcmp(*argv, "des-cbc") == 0)
875             doit[D_CBC_DES] = 1;
876         else if (strcmp(*argv, "des-ede3") == 0)
877             doit[D_EDE3_DES] = 1;
878         else
879 # endif
880 # ifndef OPENSSL_NO_AES
881         if (strcmp(*argv, "aes-128-cbc") == 0)
882             doit[D_CBC_128_AES] = 1;
883         else if (strcmp(*argv, "aes-192-cbc") == 0)
884             doit[D_CBC_192_AES] = 1;
885         else if (strcmp(*argv, "aes-256-cbc") == 0)
886             doit[D_CBC_256_AES] = 1;
887         else if (strcmp(*argv, "aes-128-ige") == 0)
888             doit[D_IGE_128_AES] = 1;
889         else if (strcmp(*argv, "aes-192-ige") == 0)
890             doit[D_IGE_192_AES] = 1;
891         else if (strcmp(*argv, "aes-256-ige") == 0)
892             doit[D_IGE_256_AES] = 1;
893         else
894 # endif
895 # ifndef OPENSSL_NO_CAMELLIA
896         if (strcmp(*argv, "camellia-128-cbc") == 0)
897             doit[D_CBC_128_CML] = 1;
898         else if (strcmp(*argv, "camellia-192-cbc") == 0)
899             doit[D_CBC_192_CML] = 1;
900         else if (strcmp(*argv, "camellia-256-cbc") == 0)
901             doit[D_CBC_256_CML] = 1;
902         else
903 # endif
904 # ifndef OPENSSL_NO_RSA
905 #  if 0                         /* was: #ifdef RSAref */
906         if (strcmp(*argv, "rsaref") == 0) {
907             RSA_set_default_openssl_method(RSA_PKCS1_RSAref());
908             j--;
909         } else
910 #  endif
911 #  ifndef RSA_NULL
912         if (strcmp(*argv, "openssl") == 0) {
913             RSA_set_default_method(RSA_PKCS1_SSLeay());
914             j--;
915         } else
916 #  endif
917 # endif                         /* !OPENSSL_NO_RSA */
918         if (strcmp(*argv, "dsa512") == 0)
919             dsa_doit[R_DSA_512] = 2;
920         else if (strcmp(*argv, "dsa1024") == 0)
921             dsa_doit[R_DSA_1024] = 2;
922         else if (strcmp(*argv, "dsa2048") == 0)
923             dsa_doit[R_DSA_2048] = 2;
924         else if (strcmp(*argv, "rsa512") == 0)
925             rsa_doit[R_RSA_512] = 2;
926         else if (strcmp(*argv, "rsa1024") == 0)
927             rsa_doit[R_RSA_1024] = 2;
928         else if (strcmp(*argv, "rsa2048") == 0)
929             rsa_doit[R_RSA_2048] = 2;
930         else if (strcmp(*argv, "rsa3072") == 0)
931             rsa_doit[R_RSA_3072] = 2;
932         else if (strcmp(*argv, "rsa4096") == 0)
933             rsa_doit[R_RSA_4096] = 2;
934         else if (strcmp(*argv, "rsa7680") == 0)
935             rsa_doit[R_RSA_7680] = 2;
936         else if (strcmp(*argv, "rsa15360") == 0)
937             rsa_doit[R_RSA_15360] = 2;
938         else
939 # ifndef OPENSSL_NO_RC2
940         if (strcmp(*argv, "rc2-cbc") == 0)
941             doit[D_CBC_RC2] = 1;
942         else if (strcmp(*argv, "rc2") == 0)
943             doit[D_CBC_RC2] = 1;
944         else
945 # endif
946 # ifndef OPENSSL_NO_RC5
947         if (strcmp(*argv, "rc5-cbc") == 0)
948             doit[D_CBC_RC5] = 1;
949         else if (strcmp(*argv, "rc5") == 0)
950             doit[D_CBC_RC5] = 1;
951         else
952 # endif
953 # ifndef OPENSSL_NO_IDEA
954         if (strcmp(*argv, "idea-cbc") == 0)
955             doit[D_CBC_IDEA] = 1;
956         else if (strcmp(*argv, "idea") == 0)
957             doit[D_CBC_IDEA] = 1;
958         else
959 # endif
960 # ifndef OPENSSL_NO_SEED
961         if (strcmp(*argv, "seed-cbc") == 0)
962             doit[D_CBC_SEED] = 1;
963         else if (strcmp(*argv, "seed") == 0)
964             doit[D_CBC_SEED] = 1;
965         else
966 # endif
967 # ifndef OPENSSL_NO_BF
968         if (strcmp(*argv, "bf-cbc") == 0)
969             doit[D_CBC_BF] = 1;
970         else if (strcmp(*argv, "blowfish") == 0)
971             doit[D_CBC_BF] = 1;
972         else if (strcmp(*argv, "bf") == 0)
973             doit[D_CBC_BF] = 1;
974         else
975 # endif
976 # ifndef OPENSSL_NO_CAST
977         if (strcmp(*argv, "cast-cbc") == 0)
978             doit[D_CBC_CAST] = 1;
979         else if (strcmp(*argv, "cast") == 0)
980             doit[D_CBC_CAST] = 1;
981         else if (strcmp(*argv, "cast5") == 0)
982             doit[D_CBC_CAST] = 1;
983         else
984 # endif
985 # ifndef OPENSSL_NO_DES
986         if (strcmp(*argv, "des") == 0) {
987             doit[D_CBC_DES] = 1;
988             doit[D_EDE3_DES] = 1;
989         } else
990 # endif
991 # ifndef OPENSSL_NO_AES
992         if (strcmp(*argv, "aes") == 0) {
993             doit[D_CBC_128_AES] = 1;
994             doit[D_CBC_192_AES] = 1;
995             doit[D_CBC_256_AES] = 1;
996         } else if (strcmp(*argv, "ghash") == 0) {
997             doit[D_GHASH] = 1;
998         } else
999 # endif
1000 # ifndef OPENSSL_NO_CAMELLIA
1001         if (strcmp(*argv, "camellia") == 0) {
1002             doit[D_CBC_128_CML] = 1;
1003             doit[D_CBC_192_CML] = 1;
1004             doit[D_CBC_256_CML] = 1;
1005         } else
1006 # endif
1007 # ifndef OPENSSL_NO_RSA
1008         if (strcmp(*argv, "rsa") == 0) {
1009             rsa_doit[R_RSA_512] = 1;
1010             rsa_doit[R_RSA_1024] = 1;
1011             rsa_doit[R_RSA_2048] = 1;
1012             rsa_doit[R_RSA_3072] = 1;
1013             rsa_doit[R_RSA_4096] = 1;
1014             rsa_doit[R_RSA_7680] = 1;
1015             rsa_doit[R_RSA_15360] = 1;
1016         } else
1017 # endif
1018 # ifndef OPENSSL_NO_DSA
1019         if (strcmp(*argv, "dsa") == 0) {
1020             dsa_doit[R_DSA_512] = 1;
1021             dsa_doit[R_DSA_1024] = 1;
1022             dsa_doit[R_DSA_2048] = 1;
1023         } else
1024 # endif
1025 # ifndef OPENSSL_NO_ECDSA
1026         if (strcmp(*argv, "ecdsap160") == 0)
1027             ecdsa_doit[R_EC_P160] = 2;
1028         else if (strcmp(*argv, "ecdsap192") == 0)
1029             ecdsa_doit[R_EC_P192] = 2;
1030         else if (strcmp(*argv, "ecdsap224") == 0)
1031             ecdsa_doit[R_EC_P224] = 2;
1032         else if (strcmp(*argv, "ecdsap256") == 0)
1033             ecdsa_doit[R_EC_P256] = 2;
1034         else if (strcmp(*argv, "ecdsap384") == 0)
1035             ecdsa_doit[R_EC_P384] = 2;
1036         else if (strcmp(*argv, "ecdsap521") == 0)
1037             ecdsa_doit[R_EC_P521] = 2;
1038         else if (strcmp(*argv, "ecdsak163") == 0)
1039             ecdsa_doit[R_EC_K163] = 2;
1040         else if (strcmp(*argv, "ecdsak233") == 0)
1041             ecdsa_doit[R_EC_K233] = 2;
1042         else if (strcmp(*argv, "ecdsak283") == 0)
1043             ecdsa_doit[R_EC_K283] = 2;
1044         else if (strcmp(*argv, "ecdsak409") == 0)
1045             ecdsa_doit[R_EC_K409] = 2;
1046         else if (strcmp(*argv, "ecdsak571") == 0)
1047             ecdsa_doit[R_EC_K571] = 2;
1048         else if (strcmp(*argv, "ecdsab163") == 0)
1049             ecdsa_doit[R_EC_B163] = 2;
1050         else if (strcmp(*argv, "ecdsab233") == 0)
1051             ecdsa_doit[R_EC_B233] = 2;
1052         else if (strcmp(*argv, "ecdsab283") == 0)
1053             ecdsa_doit[R_EC_B283] = 2;
1054         else if (strcmp(*argv, "ecdsab409") == 0)
1055             ecdsa_doit[R_EC_B409] = 2;
1056         else if (strcmp(*argv, "ecdsab571") == 0)
1057             ecdsa_doit[R_EC_B571] = 2;
1058         else if (strcmp(*argv, "ecdsa") == 0) {
1059             for (i = 0; i < EC_NUM; i++)
1060                 ecdsa_doit[i] = 1;
1061         } else
1062 # endif
1063 # ifndef OPENSSL_NO_ECDH
1064         if (strcmp(*argv, "ecdhp160") == 0)
1065             ecdh_doit[R_EC_P160] = 2;
1066         else if (strcmp(*argv, "ecdhp192") == 0)
1067             ecdh_doit[R_EC_P192] = 2;
1068         else if (strcmp(*argv, "ecdhp224") == 0)
1069             ecdh_doit[R_EC_P224] = 2;
1070         else if (strcmp(*argv, "ecdhp256") == 0)
1071             ecdh_doit[R_EC_P256] = 2;
1072         else if (strcmp(*argv, "ecdhp384") == 0)
1073             ecdh_doit[R_EC_P384] = 2;
1074         else if (strcmp(*argv, "ecdhp521") == 0)
1075             ecdh_doit[R_EC_P521] = 2;
1076         else if (strcmp(*argv, "ecdhk163") == 0)
1077             ecdh_doit[R_EC_K163] = 2;
1078         else if (strcmp(*argv, "ecdhk233") == 0)
1079             ecdh_doit[R_EC_K233] = 2;
1080         else if (strcmp(*argv, "ecdhk283") == 0)
1081             ecdh_doit[R_EC_K283] = 2;
1082         else if (strcmp(*argv, "ecdhk409") == 0)
1083             ecdh_doit[R_EC_K409] = 2;
1084         else if (strcmp(*argv, "ecdhk571") == 0)
1085             ecdh_doit[R_EC_K571] = 2;
1086         else if (strcmp(*argv, "ecdhb163") == 0)
1087             ecdh_doit[R_EC_B163] = 2;
1088         else if (strcmp(*argv, "ecdhb233") == 0)
1089             ecdh_doit[R_EC_B233] = 2;
1090         else if (strcmp(*argv, "ecdhb283") == 0)
1091             ecdh_doit[R_EC_B283] = 2;
1092         else if (strcmp(*argv, "ecdhb409") == 0)
1093             ecdh_doit[R_EC_B409] = 2;
1094         else if (strcmp(*argv, "ecdhb571") == 0)
1095             ecdh_doit[R_EC_B571] = 2;
1096         else if (strcmp(*argv, "ecdh") == 0) {
1097             for (i = 0; i < EC_NUM; i++)
1098                 ecdh_doit[i] = 1;
1099         } else
1100 # endif
1101         {
1102             BIO_printf(bio_err, "Error: bad option or value\n");
1103             BIO_printf(bio_err, "\n");
1104             BIO_printf(bio_err, "Available values:\n");
1105 # ifndef OPENSSL_NO_MD2
1106             BIO_printf(bio_err, "md2      ");
1107 # endif
1108 # ifndef OPENSSL_NO_MDC2
1109             BIO_printf(bio_err, "mdc2     ");
1110 # endif
1111 # ifndef OPENSSL_NO_MD4
1112             BIO_printf(bio_err, "md4      ");
1113 # endif
1114 # ifndef OPENSSL_NO_MD5
1115             BIO_printf(bio_err, "md5      ");
1116 #  ifndef OPENSSL_NO_HMAC
1117             BIO_printf(bio_err, "hmac     ");
1118 #  endif
1119 # endif
1120 # ifndef OPENSSL_NO_SHA1
1121             BIO_printf(bio_err, "sha1     ");
1122 # endif
1123 # ifndef OPENSSL_NO_SHA256
1124             BIO_printf(bio_err, "sha256   ");
1125 # endif
1126 # ifndef OPENSSL_NO_SHA512
1127             BIO_printf(bio_err, "sha512   ");
1128 # endif
1129 # ifndef OPENSSL_NO_WHIRLPOOL
1130             BIO_printf(bio_err, "whirlpool");
1131 # endif
1132 # ifndef OPENSSL_NO_RMD160
1133             BIO_printf(bio_err, "rmd160");
1134 # endif
1135 # if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \
1136     !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \
1137     !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RMD160) || \
1138     !defined(OPENSSL_NO_WHIRLPOOL)
1139             BIO_printf(bio_err, "\n");
1140 # endif
1141
1142 # ifndef OPENSSL_NO_IDEA
1143             BIO_printf(bio_err, "idea-cbc ");
1144 # endif
1145 # ifndef OPENSSL_NO_SEED
1146             BIO_printf(bio_err, "seed-cbc ");
1147 # endif
1148 # ifndef OPENSSL_NO_RC2
1149             BIO_printf(bio_err, "rc2-cbc  ");
1150 # endif
1151 # ifndef OPENSSL_NO_RC5
1152             BIO_printf(bio_err, "rc5-cbc  ");
1153 # endif
1154 # ifndef OPENSSL_NO_BF
1155             BIO_printf(bio_err, "bf-cbc");
1156 # endif
1157 # if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || !defined(OPENSSL_NO_RC2) || \
1158     !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)
1159             BIO_printf(bio_err, "\n");
1160 # endif
1161 # ifndef OPENSSL_NO_DES
1162             BIO_printf(bio_err, "des-cbc  des-ede3 ");
1163 # endif
1164 # ifndef OPENSSL_NO_AES
1165             BIO_printf(bio_err, "aes-128-cbc aes-192-cbc aes-256-cbc ");
1166             BIO_printf(bio_err, "aes-128-ige aes-192-ige aes-256-ige ");
1167 # endif
1168 # ifndef OPENSSL_NO_CAMELLIA
1169             BIO_printf(bio_err, "\n");
1170             BIO_printf(bio_err,
1171                        "camellia-128-cbc camellia-192-cbc camellia-256-cbc ");
1172 # endif
1173 # ifndef OPENSSL_NO_RC4
1174             BIO_printf(bio_err, "rc4");
1175 # endif
1176             BIO_printf(bio_err, "\n");
1177
1178 # ifndef OPENSSL_NO_RSA
1179             BIO_printf(bio_err,
1180                        "rsa512   rsa1024  rsa2048  rsa3072  rsa4096\n");
1181             BIO_printf(bio_err, "rsa7680  rsa15360\n");
1182 # endif
1183
1184 # ifndef OPENSSL_NO_DSA
1185             BIO_printf(bio_err, "dsa512   dsa1024  dsa2048\n");
1186 # endif
1187 # ifndef OPENSSL_NO_ECDSA
1188             BIO_printf(bio_err, "ecdsap160 ecdsap192 ecdsap224 "
1189                        "ecdsap256 ecdsap384 ecdsap521\n");
1190             BIO_printf(bio_err,
1191                        "ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\n");
1192             BIO_printf(bio_err,
1193                        "ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571\n");
1194             BIO_printf(bio_err, "ecdsa\n");
1195 # endif
1196 # ifndef OPENSSL_NO_ECDH
1197             BIO_printf(bio_err, "ecdhp160  ecdhp192  ecdhp224 "
1198                        "ecdhp256  ecdhp384  ecdhp521\n");
1199             BIO_printf(bio_err,
1200                        "ecdhk163  ecdhk233  ecdhk283  ecdhk409  ecdhk571\n");
1201             BIO_printf(bio_err,
1202                        "ecdhb163  ecdhb233  ecdhb283  ecdhb409  ecdhb571\n");
1203             BIO_printf(bio_err, "ecdh\n");
1204 # endif
1205
1206 # ifndef OPENSSL_NO_IDEA
1207             BIO_printf(bio_err, "idea     ");
1208 # endif
1209 # ifndef OPENSSL_NO_SEED
1210             BIO_printf(bio_err, "seed     ");
1211 # endif
1212 # ifndef OPENSSL_NO_RC2
1213             BIO_printf(bio_err, "rc2      ");
1214 # endif
1215 # ifndef OPENSSL_NO_DES
1216             BIO_printf(bio_err, "des      ");
1217 # endif
1218 # ifndef OPENSSL_NO_AES
1219             BIO_printf(bio_err, "aes      ");
1220 # endif
1221 # ifndef OPENSSL_NO_CAMELLIA
1222             BIO_printf(bio_err, "camellia ");
1223 # endif
1224 # ifndef OPENSSL_NO_RSA
1225             BIO_printf(bio_err, "rsa      ");
1226 # endif
1227 # ifndef OPENSSL_NO_BF
1228             BIO_printf(bio_err, "blowfish");
1229 # endif
1230 # if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \
1231     !defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \
1232     !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \
1233     !defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA)
1234             BIO_printf(bio_err, "\n");
1235 # endif
1236
1237             BIO_printf(bio_err, "\n");
1238             BIO_printf(bio_err, "Available options:\n");
1239 # if defined(TIMES) || defined(USE_TOD)
1240             BIO_printf(bio_err, "-elapsed        "
1241                        "measure time in real time instead of CPU user time.\n");
1242 # endif
1243 # ifndef OPENSSL_NO_ENGINE
1244             BIO_printf(bio_err,
1245                        "-engine e       "
1246                        "use engine e, possibly a hardware device.\n");
1247 # endif
1248             BIO_printf(bio_err, "-evp e          " "use EVP e.\n");
1249             BIO_printf(bio_err,
1250                        "-decrypt        "
1251                        "time decryption instead of encryption (only EVP).\n");
1252             BIO_printf(bio_err,
1253                        "-mr             "
1254                        "produce machine readable output.\n");
1255             BIO_printf(bio_err,
1256                        "-mb             "
1257                        "perform multi-block benchmark (for specific ciphers)\n");
1258             BIO_printf(bio_err,
1259                        "-misalign n     "
1260                        "perform benchmark with misaligned data\n");
1261 # ifndef NO_FORK
1262             BIO_printf(bio_err,
1263                        "-multi n        " "run n benchmarks in parallel.\n");
1264 # endif
1265             goto end;
1266         }
1267         argc--;
1268         argv++;
1269         j++;
1270     }
1271
1272 # ifndef NO_FORK
1273     if (multi && do_multi(multi))
1274         goto show_res;
1275 # endif
1276
1277     if (j == 0) {
1278         for (i = 0; i < ALGOR_NUM; i++) {
1279             if (i != D_EVP)
1280                 doit[i] = 1;
1281         }
1282         for (i = 0; i < RSA_NUM; i++)
1283             rsa_doit[i] = 1;
1284         for (i = 0; i < DSA_NUM; i++)
1285             dsa_doit[i] = 1;
1286 # ifndef OPENSSL_NO_ECDSA
1287         for (i = 0; i < EC_NUM; i++)
1288             ecdsa_doit[i] = 1;
1289 # endif
1290 # ifndef OPENSSL_NO_ECDH
1291         for (i = 0; i < EC_NUM; i++)
1292             ecdh_doit[i] = 1;
1293 # endif
1294     }
1295     for (i = 0; i < ALGOR_NUM; i++)
1296         if (doit[i])
1297             pr_header++;
1298
1299     if (usertime == 0 && !mr)
1300         BIO_printf(bio_err,
1301                    "You have chosen to measure elapsed time "
1302                    "instead of user CPU time.\n");
1303
1304 # ifndef OPENSSL_NO_RSA
1305     for (i = 0; i < RSA_NUM; i++) {
1306         const unsigned char *p;
1307
1308         p = rsa_data[i];
1309         rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]);
1310         if (rsa_key[i] == NULL) {
1311             BIO_printf(bio_err, "internal error loading RSA key number %d\n",
1312                        i);
1313             goto end;
1314         }
1315 #  if 0
1316         else {
1317             BIO_printf(bio_err,
1318                        mr ? "+RK:%d:"
1319                        : "Loaded RSA key, %d bit modulus and e= 0x",
1320                        BN_num_bits(rsa_key[i]->n));
1321             BN_print(bio_err, rsa_key[i]->e);
1322             BIO_printf(bio_err, "\n");
1323         }
1324 #  endif
1325     }
1326 # endif
1327
1328 # ifndef OPENSSL_NO_DSA
1329     dsa_key[0] = get_dsa512();
1330     dsa_key[1] = get_dsa1024();
1331     dsa_key[2] = get_dsa2048();
1332 # endif
1333
1334 # ifndef OPENSSL_NO_DES
1335     DES_set_key_unchecked(&key, &sch);
1336     DES_set_key_unchecked(&key2, &sch2);
1337     DES_set_key_unchecked(&key3, &sch3);
1338 # endif
1339 # ifndef OPENSSL_NO_AES
1340     AES_set_encrypt_key(key16, 128, &aes_ks1);
1341     AES_set_encrypt_key(key24, 192, &aes_ks2);
1342     AES_set_encrypt_key(key32, 256, &aes_ks3);
1343 # endif
1344 # ifndef OPENSSL_NO_CAMELLIA
1345     Camellia_set_key(key16, 128, &camellia_ks1);
1346     Camellia_set_key(ckey24, 192, &camellia_ks2);
1347     Camellia_set_key(ckey32, 256, &camellia_ks3);
1348 # endif
1349 # ifndef OPENSSL_NO_IDEA
1350     idea_set_encrypt_key(key16, &idea_ks);
1351 # endif
1352 # ifndef OPENSSL_NO_SEED
1353     SEED_set_key(key16, &seed_ks);
1354 # endif
1355 # ifndef OPENSSL_NO_RC4
1356     RC4_set_key(&rc4_ks, 16, key16);
1357 # endif
1358 # ifndef OPENSSL_NO_RC2
1359     RC2_set_key(&rc2_ks, 16, key16, 128);
1360 # endif
1361 # ifndef OPENSSL_NO_RC5
1362     RC5_32_set_key(&rc5_ks, 16, key16, 12);
1363 # endif
1364 # ifndef OPENSSL_NO_BF
1365     BF_set_key(&bf_ks, 16, key16);
1366 # endif
1367 # ifndef OPENSSL_NO_CAST
1368     CAST_set_key(&cast_ks, 16, key16);
1369 # endif
1370 # ifndef OPENSSL_NO_RSA
1371     memset(rsa_c, 0, sizeof(rsa_c));
1372 # endif
1373 # ifndef SIGALRM
1374 #  ifndef OPENSSL_NO_DES
1375     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1376     count = 10;
1377     do {
1378         long it;
1379         count *= 2;
1380         Time_F(START);
1381         for (it = count; it; it--)
1382             DES_ecb_encrypt((DES_cblock *)buf,
1383                             (DES_cblock *)buf, &sch, DES_ENCRYPT);
1384         d = Time_F(STOP);
1385     } while (d < 3);
1386     save_count = count;
1387     c[D_MD2][0] = count / 10;
1388     c[D_MDC2][0] = count / 10;
1389     c[D_MD4][0] = count;
1390     c[D_MD5][0] = count;
1391     c[D_HMAC][0] = count;
1392     c[D_SHA1][0] = count;
1393     c[D_RMD160][0] = count;
1394     c[D_RC4][0] = count * 5;
1395     c[D_CBC_DES][0] = count;
1396     c[D_EDE3_DES][0] = count / 3;
1397     c[D_CBC_IDEA][0] = count;
1398     c[D_CBC_SEED][0] = count;
1399     c[D_CBC_RC2][0] = count;
1400     c[D_CBC_RC5][0] = count;
1401     c[D_CBC_BF][0] = count;
1402     c[D_CBC_CAST][0] = count;
1403     c[D_CBC_128_AES][0] = count;
1404     c[D_CBC_192_AES][0] = count;
1405     c[D_CBC_256_AES][0] = count;
1406     c[D_CBC_128_CML][0] = count;
1407     c[D_CBC_192_CML][0] = count;
1408     c[D_CBC_256_CML][0] = count;
1409     c[D_SHA256][0] = count;
1410     c[D_SHA512][0] = count;
1411     c[D_WHIRLPOOL][0] = count;
1412     c[D_IGE_128_AES][0] = count;
1413     c[D_IGE_192_AES][0] = count;
1414     c[D_IGE_256_AES][0] = count;
1415     c[D_GHASH][0] = count;
1416
1417     for (i = 1; i < SIZE_NUM; i++) {
1418         long l0, l1;
1419
1420         l0 = (long)lengths[0];
1421         l1 = (long)lengths[i];
1422
1423         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1424         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1425         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1426         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1427         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1428         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1429         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1430         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1431         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1432         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1433
1434         l0 = (long)lengths[i - 1];
1435
1436         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1437         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1438         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1439         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1440         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1441         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1442         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1443         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1444         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1445         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1446         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1447         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1448         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1449         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1450         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1451         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1452         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1453         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1454     }
1455
1456 #   ifndef OPENSSL_NO_RSA
1457     rsa_c[R_RSA_512][0] = count / 2000;
1458     rsa_c[R_RSA_512][1] = count / 400;
1459     for (i = 1; i < RSA_NUM; i++) {
1460         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1461         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1462         if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
1463             rsa_doit[i] = 0;
1464         else {
1465             if (rsa_c[i][0] == 0) {
1466                 rsa_c[i][0] = 1;
1467                 rsa_c[i][1] = 20;
1468             }
1469         }
1470     }
1471 #   endif
1472
1473 #   ifndef OPENSSL_NO_DSA
1474     dsa_c[R_DSA_512][0] = count / 1000;
1475     dsa_c[R_DSA_512][1] = count / 1000 / 2;
1476     for (i = 1; i < DSA_NUM; i++) {
1477         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1478         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1479         if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
1480             dsa_doit[i] = 0;
1481         else {
1482             if (dsa_c[i] == 0) {
1483                 dsa_c[i][0] = 1;
1484                 dsa_c[i][1] = 1;
1485             }
1486         }
1487     }
1488 #   endif
1489
1490 #   ifndef OPENSSL_NO_ECDSA
1491     ecdsa_c[R_EC_P160][0] = count / 1000;
1492     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1493     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1494         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1495         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1496         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1497             ecdsa_doit[i] = 0;
1498         else {
1499             if (ecdsa_c[i] == 0) {
1500                 ecdsa_c[i][0] = 1;
1501                 ecdsa_c[i][1] = 1;
1502             }
1503         }
1504     }
1505     ecdsa_c[R_EC_K163][0] = count / 1000;
1506     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1507     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1508         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1509         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1510         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1511             ecdsa_doit[i] = 0;
1512         else {
1513             if (ecdsa_c[i] == 0) {
1514                 ecdsa_c[i][0] = 1;
1515                 ecdsa_c[i][1] = 1;
1516             }
1517         }
1518     }
1519     ecdsa_c[R_EC_B163][0] = count / 1000;
1520     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1521     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1522         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1523         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1524         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1525             ecdsa_doit[i] = 0;
1526         else {
1527             if (ecdsa_c[i] == 0) {
1528                 ecdsa_c[i][0] = 1;
1529                 ecdsa_c[i][1] = 1;
1530             }
1531         }
1532     }
1533 #   endif
1534
1535 #   ifndef OPENSSL_NO_ECDH
1536     ecdh_c[R_EC_P160][0] = count / 1000;
1537     ecdh_c[R_EC_P160][1] = count / 1000;
1538     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1539         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1540         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1541         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1542             ecdh_doit[i] = 0;
1543         else {
1544             if (ecdh_c[i] == 0) {
1545                 ecdh_c[i][0] = 1;
1546                 ecdh_c[i][1] = 1;
1547             }
1548         }
1549     }
1550     ecdh_c[R_EC_K163][0] = count / 1000;
1551     ecdh_c[R_EC_K163][1] = count / 1000;
1552     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1553         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1554         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1555         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1556             ecdh_doit[i] = 0;
1557         else {
1558             if (ecdh_c[i] == 0) {
1559                 ecdh_c[i][0] = 1;
1560                 ecdh_c[i][1] = 1;
1561             }
1562         }
1563     }
1564     ecdh_c[R_EC_B163][0] = count / 1000;
1565     ecdh_c[R_EC_B163][1] = count / 1000;
1566     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1567         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1568         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1569         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1570             ecdh_doit[i] = 0;
1571         else {
1572             if (ecdh_c[i] == 0) {
1573                 ecdh_c[i][0] = 1;
1574                 ecdh_c[i][1] = 1;
1575             }
1576         }
1577     }
1578 #   endif
1579
1580 #   define COND(d) (count < (d))
1581 #   define COUNT(d) (d)
1582 #  else
1583 /* not worth fixing */
1584 #   error "You cannot disable DES on systems without SIGALRM."
1585 #  endif                        /* OPENSSL_NO_DES */
1586 # else
1587 #  define COND(c) (run && count<0x7fffffff)
1588 #  define COUNT(d) (count)
1589 #  ifndef _WIN32
1590     signal(SIGALRM, sig_done);
1591 #  endif
1592 # endif                         /* SIGALRM */
1593
1594 # ifndef OPENSSL_NO_MD2
1595     if (doit[D_MD2]) {
1596         for (j = 0; j < SIZE_NUM; j++) {
1597             print_message(names[D_MD2], c[D_MD2][j], lengths[j]);
1598             Time_F(START);
1599             for (count = 0, run = 1; COND(c[D_MD2][j]); count++)
1600                 EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL,
1601                            EVP_md2(), NULL);
1602             d = Time_F(STOP);
1603             print_result(D_MD2, j, count, d);
1604         }
1605     }
1606 # endif
1607 # ifndef OPENSSL_NO_MDC2
1608     if (doit[D_MDC2]) {
1609         for (j = 0; j < SIZE_NUM; j++) {
1610             print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);
1611             Time_F(START);
1612             for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)
1613                 EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL,
1614                            EVP_mdc2(), NULL);
1615             d = Time_F(STOP);
1616             print_result(D_MDC2, j, count, d);
1617         }
1618     }
1619 # endif
1620
1621 # ifndef OPENSSL_NO_MD4
1622     if (doit[D_MD4]) {
1623         for (j = 0; j < SIZE_NUM; j++) {
1624             print_message(names[D_MD4], c[D_MD4][j], lengths[j]);
1625             Time_F(START);
1626             for (count = 0, run = 1; COND(c[D_MD4][j]); count++)
1627                 EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]),
1628                            NULL, EVP_md4(), NULL);
1629             d = Time_F(STOP);
1630             print_result(D_MD4, j, count, d);
1631         }
1632     }
1633 # endif
1634
1635 # ifndef OPENSSL_NO_MD5
1636     if (doit[D_MD5]) {
1637         for (j = 0; j < SIZE_NUM; j++) {
1638             print_message(names[D_MD5], c[D_MD5][j], lengths[j]);
1639             Time_F(START);
1640             for (count = 0, run = 1; COND(c[D_MD5][j]); count++)
1641                 MD5(buf, lengths[j], md5);
1642             d = Time_F(STOP);
1643             print_result(D_MD5, j, count, d);
1644         }
1645     }
1646 # endif
1647
1648 # if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)
1649     if (doit[D_HMAC]) {
1650         HMAC_CTX hctx;
1651
1652         HMAC_CTX_init(&hctx);
1653         HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...",
1654                      16, EVP_md5(), NULL);
1655
1656         for (j = 0; j < SIZE_NUM; j++) {
1657             print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);
1658             Time_F(START);
1659             for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {
1660                 HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);
1661                 HMAC_Update(&hctx, buf, lengths[j]);
1662                 HMAC_Final(&hctx, &(hmac[0]), NULL);
1663             }
1664             d = Time_F(STOP);
1665             print_result(D_HMAC, j, count, d);
1666         }
1667         HMAC_CTX_cleanup(&hctx);
1668     }
1669 # endif
1670 # ifndef OPENSSL_NO_SHA
1671     if (doit[D_SHA1]) {
1672         for (j = 0; j < SIZE_NUM; j++) {
1673             print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);
1674             Time_F(START);
1675             for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)
1676 #  if 0
1677                 EVP_Digest(buf, (unsigned long)lengths[j], &(sha[0]), NULL,
1678                            EVP_sha1(), NULL);
1679 #  else
1680                 SHA1(buf, lengths[j], sha);
1681 #  endif
1682             d = Time_F(STOP);
1683             print_result(D_SHA1, j, count, d);
1684         }
1685     }
1686 #  ifndef OPENSSL_NO_SHA256
1687     if (doit[D_SHA256]) {
1688         for (j = 0; j < SIZE_NUM; j++) {
1689             print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]);
1690             Time_F(START);
1691             for (count = 0, run = 1; COND(c[D_SHA256][j]); count++)
1692                 SHA256(buf, lengths[j], sha256);
1693             d = Time_F(STOP);
1694             print_result(D_SHA256, j, count, d);
1695         }
1696     }
1697 #  endif
1698
1699 #  ifndef OPENSSL_NO_SHA512
1700     if (doit[D_SHA512]) {
1701         for (j = 0; j < SIZE_NUM; j++) {
1702             print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]);
1703             Time_F(START);
1704             for (count = 0, run = 1; COND(c[D_SHA512][j]); count++)
1705                 SHA512(buf, lengths[j], sha512);
1706             d = Time_F(STOP);
1707             print_result(D_SHA512, j, count, d);
1708         }
1709     }
1710 #  endif
1711 # endif
1712
1713 # ifndef OPENSSL_NO_WHIRLPOOL
1714     if (doit[D_WHIRLPOOL]) {
1715         for (j = 0; j < SIZE_NUM; j++) {
1716             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]);
1717             Time_F(START);
1718             for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++)
1719                 WHIRLPOOL(buf, lengths[j], whirlpool);
1720             d = Time_F(STOP);
1721             print_result(D_WHIRLPOOL, j, count, d);
1722         }
1723     }
1724 # endif
1725
1726 # ifndef OPENSSL_NO_RMD160
1727     if (doit[D_RMD160]) {
1728         for (j = 0; j < SIZE_NUM; j++) {
1729             print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]);
1730             Time_F(START);
1731             for (count = 0, run = 1; COND(c[D_RMD160][j]); count++)
1732                 EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL,
1733                            EVP_ripemd160(), NULL);
1734             d = Time_F(STOP);
1735             print_result(D_RMD160, j, count, d);
1736         }
1737     }
1738 # endif
1739 # ifndef OPENSSL_NO_RC4
1740     if (doit[D_RC4]) {
1741         for (j = 0; j < SIZE_NUM; j++) {
1742             print_message(names[D_RC4], c[D_RC4][j], lengths[j]);
1743             Time_F(START);
1744             for (count = 0, run = 1; COND(c[D_RC4][j]); count++)
1745                 RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf);
1746             d = Time_F(STOP);
1747             print_result(D_RC4, j, count, d);
1748         }
1749     }
1750 # endif
1751 # ifndef OPENSSL_NO_DES
1752     if (doit[D_CBC_DES]) {
1753         for (j = 0; j < SIZE_NUM; j++) {
1754             print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]);
1755             Time_F(START);
1756             for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++)
1757                 DES_ncbc_encrypt(buf, buf, lengths[j], &sch,
1758                                  &DES_iv, DES_ENCRYPT);
1759             d = Time_F(STOP);
1760             print_result(D_CBC_DES, j, count, d);
1761         }
1762     }
1763
1764     if (doit[D_EDE3_DES]) {
1765         for (j = 0; j < SIZE_NUM; j++) {
1766             print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]);
1767             Time_F(START);
1768             for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++)
1769                 DES_ede3_cbc_encrypt(buf, buf, lengths[j],
1770                                      &sch, &sch2, &sch3,
1771                                      &DES_iv, DES_ENCRYPT);
1772             d = Time_F(STOP);
1773             print_result(D_EDE3_DES, j, count, d);
1774         }
1775     }
1776 # endif
1777 # ifndef OPENSSL_NO_AES
1778     if (doit[D_CBC_128_AES]) {
1779         for (j = 0; j < SIZE_NUM; j++) {
1780             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j],
1781                           lengths[j]);
1782             Time_F(START);
1783             for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++)
1784                 AES_cbc_encrypt(buf, buf,
1785                                 (unsigned long)lengths[j], &aes_ks1,
1786                                 iv, AES_ENCRYPT);
1787             d = Time_F(STOP);
1788             print_result(D_CBC_128_AES, j, count, d);
1789         }
1790     }
1791     if (doit[D_CBC_192_AES]) {
1792         for (j = 0; j < SIZE_NUM; j++) {
1793             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j],
1794                           lengths[j]);
1795             Time_F(START);
1796             for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++)
1797                 AES_cbc_encrypt(buf, buf,
1798                                 (unsigned long)lengths[j], &aes_ks2,
1799                                 iv, AES_ENCRYPT);
1800             d = Time_F(STOP);
1801             print_result(D_CBC_192_AES, j, count, d);
1802         }
1803     }
1804     if (doit[D_CBC_256_AES]) {
1805         for (j = 0; j < SIZE_NUM; j++) {
1806             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j],
1807                           lengths[j]);
1808             Time_F(START);
1809             for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++)
1810                 AES_cbc_encrypt(buf, buf,
1811                                 (unsigned long)lengths[j], &aes_ks3,
1812                                 iv, AES_ENCRYPT);
1813             d = Time_F(STOP);
1814             print_result(D_CBC_256_AES, j, count, d);
1815         }
1816     }
1817
1818     if (doit[D_IGE_128_AES]) {
1819         for (j = 0; j < SIZE_NUM; j++) {
1820             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j],
1821                           lengths[j]);
1822             Time_F(START);
1823             for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++)
1824                 AES_ige_encrypt(buf, buf2,
1825                                 (unsigned long)lengths[j], &aes_ks1,
1826                                 iv, AES_ENCRYPT);
1827             d = Time_F(STOP);
1828             print_result(D_IGE_128_AES, j, count, d);
1829         }
1830     }
1831     if (doit[D_IGE_192_AES]) {
1832         for (j = 0; j < SIZE_NUM; j++) {
1833             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j],
1834                           lengths[j]);
1835             Time_F(START);
1836             for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++)
1837                 AES_ige_encrypt(buf, buf2,
1838                                 (unsigned long)lengths[j], &aes_ks2,
1839                                 iv, AES_ENCRYPT);
1840             d = Time_F(STOP);
1841             print_result(D_IGE_192_AES, j, count, d);
1842         }
1843     }
1844     if (doit[D_IGE_256_AES]) {
1845         for (j = 0; j < SIZE_NUM; j++) {
1846             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j],
1847                           lengths[j]);
1848             Time_F(START);
1849             for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++)
1850                 AES_ige_encrypt(buf, buf2,
1851                                 (unsigned long)lengths[j], &aes_ks3,
1852                                 iv, AES_ENCRYPT);
1853             d = Time_F(STOP);
1854             print_result(D_IGE_256_AES, j, count, d);
1855         }
1856     }
1857     if (doit[D_GHASH]) {
1858         GCM128_CONTEXT *ctx =
1859             CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
1860         CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12);
1861
1862         for (j = 0; j < SIZE_NUM; j++) {
1863             print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]);
1864             Time_F(START);
1865             for (count = 0, run = 1; COND(c[D_GHASH][j]); count++)
1866                 CRYPTO_gcm128_aad(ctx, buf, lengths[j]);
1867             d = Time_F(STOP);
1868             print_result(D_GHASH, j, count, d);
1869         }
1870         CRYPTO_gcm128_release(ctx);
1871     }
1872 # endif
1873 # ifndef OPENSSL_NO_CAMELLIA
1874     if (doit[D_CBC_128_CML]) {
1875         for (j = 0; j < SIZE_NUM; j++) {
1876             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j],
1877                           lengths[j]);
1878             Time_F(START);
1879             for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++)
1880                 Camellia_cbc_encrypt(buf, buf,
1881                                      (unsigned long)lengths[j], &camellia_ks1,
1882                                      iv, CAMELLIA_ENCRYPT);
1883             d = Time_F(STOP);
1884             print_result(D_CBC_128_CML, j, count, d);
1885         }
1886     }
1887     if (doit[D_CBC_192_CML]) {
1888         for (j = 0; j < SIZE_NUM; j++) {
1889             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j],
1890                           lengths[j]);
1891             Time_F(START);
1892             for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++)
1893                 Camellia_cbc_encrypt(buf, buf,
1894                                      (unsigned long)lengths[j], &camellia_ks2,
1895                                      iv, CAMELLIA_ENCRYPT);
1896             d = Time_F(STOP);
1897             print_result(D_CBC_192_CML, j, count, d);
1898         }
1899     }
1900     if (doit[D_CBC_256_CML]) {
1901         for (j = 0; j < SIZE_NUM; j++) {
1902             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j],
1903                           lengths[j]);
1904             Time_F(START);
1905             for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++)
1906                 Camellia_cbc_encrypt(buf, buf,
1907                                      (unsigned long)lengths[j], &camellia_ks3,
1908                                      iv, CAMELLIA_ENCRYPT);
1909             d = Time_F(STOP);
1910             print_result(D_CBC_256_CML, j, count, d);
1911         }
1912     }
1913 # endif
1914 # ifndef OPENSSL_NO_IDEA
1915     if (doit[D_CBC_IDEA]) {
1916         for (j = 0; j < SIZE_NUM; j++) {
1917             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]);
1918             Time_F(START);
1919             for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++)
1920                 idea_cbc_encrypt(buf, buf,
1921                                  (unsigned long)lengths[j], &idea_ks,
1922                                  iv, IDEA_ENCRYPT);
1923             d = Time_F(STOP);
1924             print_result(D_CBC_IDEA, j, count, d);
1925         }
1926     }
1927 # endif
1928 # ifndef OPENSSL_NO_SEED
1929     if (doit[D_CBC_SEED]) {
1930         for (j = 0; j < SIZE_NUM; j++) {
1931             print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]);
1932             Time_F(START);
1933             for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++)
1934                 SEED_cbc_encrypt(buf, buf,
1935                                  (unsigned long)lengths[j], &seed_ks, iv, 1);
1936             d = Time_F(STOP);
1937             print_result(D_CBC_SEED, j, count, d);
1938         }
1939     }
1940 # endif
1941 # ifndef OPENSSL_NO_RC2
1942     if (doit[D_CBC_RC2]) {
1943         for (j = 0; j < SIZE_NUM; j++) {
1944             print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]);
1945             Time_F(START);
1946             for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++)
1947                 RC2_cbc_encrypt(buf, buf,
1948                                 (unsigned long)lengths[j], &rc2_ks,
1949                                 iv, RC2_ENCRYPT);
1950             d = Time_F(STOP);
1951             print_result(D_CBC_RC2, j, count, d);
1952         }
1953     }
1954 # endif
1955 # ifndef OPENSSL_NO_RC5
1956     if (doit[D_CBC_RC5]) {
1957         for (j = 0; j < SIZE_NUM; j++) {
1958             print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]);
1959             Time_F(START);
1960             for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++)
1961                 RC5_32_cbc_encrypt(buf, buf,
1962                                    (unsigned long)lengths[j], &rc5_ks,
1963                                    iv, RC5_ENCRYPT);
1964             d = Time_F(STOP);
1965             print_result(D_CBC_RC5, j, count, d);
1966         }
1967     }
1968 # endif
1969 # ifndef OPENSSL_NO_BF
1970     if (doit[D_CBC_BF]) {
1971         for (j = 0; j < SIZE_NUM; j++) {
1972             print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]);
1973             Time_F(START);
1974             for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++)
1975                 BF_cbc_encrypt(buf, buf,
1976                                (unsigned long)lengths[j], &bf_ks,
1977                                iv, BF_ENCRYPT);
1978             d = Time_F(STOP);
1979             print_result(D_CBC_BF, j, count, d);
1980         }
1981     }
1982 # endif
1983 # ifndef OPENSSL_NO_CAST
1984     if (doit[D_CBC_CAST]) {
1985         for (j = 0; j < SIZE_NUM; j++) {
1986             print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]);
1987             Time_F(START);
1988             for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++)
1989                 CAST_cbc_encrypt(buf, buf,
1990                                  (unsigned long)lengths[j], &cast_ks,
1991                                  iv, CAST_ENCRYPT);
1992             d = Time_F(STOP);
1993             print_result(D_CBC_CAST, j, count, d);
1994         }
1995     }
1996 # endif
1997
1998     if (doit[D_EVP]) {
1999 # ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
2000         if (multiblock && evp_cipher) {
2001             if (!
2002                 (EVP_CIPHER_flags(evp_cipher) &
2003                  EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2004                 fprintf(stderr, "%s is not multi-block capable\n",
2005                         OBJ_nid2ln(evp_cipher->nid));
2006                 goto end;
2007             }
2008             multiblock_speed(evp_cipher);
2009             mret = 0;
2010             goto end;
2011         }
2012 # endif
2013         for (j = 0; j < SIZE_NUM; j++) {
2014             if (evp_cipher) {
2015                 EVP_CIPHER_CTX ctx;
2016                 int outl;
2017
2018                 names[D_EVP] = OBJ_nid2ln(evp_cipher->nid);
2019                 /*
2020                  * -O3 -fschedule-insns messes up an optimization here!
2021                  * names[D_EVP] somehow becomes NULL
2022                  */
2023                 print_message(names[D_EVP], save_count, lengths[j]);
2024
2025                 EVP_CIPHER_CTX_init(&ctx);
2026                 if (decrypt)
2027                     EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
2028                 else
2029                     EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
2030                 EVP_CIPHER_CTX_set_padding(&ctx, 0);
2031
2032                 Time_F(START);
2033                 if (decrypt)
2034                     for (count = 0, run = 1;
2035                          COND(save_count * 4 * lengths[0] / lengths[j]);
2036                          count++)
2037                         EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
2038                 else
2039                     for (count = 0, run = 1;
2040                          COND(save_count * 4 * lengths[0] / lengths[j]);
2041                          count++)
2042                         EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
2043                 if (decrypt)
2044                     EVP_DecryptFinal_ex(&ctx, buf, &outl);
2045                 else
2046                     EVP_EncryptFinal_ex(&ctx, buf, &outl);
2047                 d = Time_F(STOP);
2048                 EVP_CIPHER_CTX_cleanup(&ctx);
2049             }
2050             if (evp_md) {
2051                 names[D_EVP] = OBJ_nid2ln(evp_md->type);
2052                 print_message(names[D_EVP], save_count, lengths[j]);
2053
2054                 Time_F(START);
2055                 for (count = 0, run = 1;
2056                      COND(save_count * 4 * lengths[0] / lengths[j]); count++)
2057                     EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL);
2058
2059                 d = Time_F(STOP);
2060             }
2061             print_result(D_EVP, j, count, d);
2062         }
2063     }
2064 # ifndef OPENSSL_SYS_WIN32
2065 # endif
2066     RAND_pseudo_bytes(buf, 36);
2067 # ifndef OPENSSL_NO_RSA
2068     for (j = 0; j < RSA_NUM; j++) {
2069         int ret;
2070         if (!rsa_doit[j])
2071             continue;
2072         ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]);
2073         if (ret == 0) {
2074             BIO_printf(bio_err,
2075                        "RSA sign failure.  No RSA sign will be done.\n");
2076             ERR_print_errors(bio_err);
2077             rsa_count = 1;
2078         } else {
2079             pkey_print_message("private", "rsa",
2080                                rsa_c[j][0], rsa_bits[j], RSA_SECONDS);
2081             /* RSA_blinding_on(rsa_key[j],NULL); */
2082             Time_F(START);
2083             for (count = 0, run = 1; COND(rsa_c[j][0]); count++) {
2084                 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2,
2085                                &rsa_num, rsa_key[j]);
2086                 if (ret == 0) {
2087                     BIO_printf(bio_err, "RSA sign failure\n");
2088                     ERR_print_errors(bio_err);
2089                     count = 1;
2090                     break;
2091                 }
2092             }
2093             d = Time_F(STOP);
2094             BIO_printf(bio_err,
2095                        mr ? "+R1:%ld:%d:%.2f\n"
2096                        : "%ld %d bit private RSA's in %.2fs\n",
2097                        count, rsa_bits[j], d);
2098             rsa_results[j][0] = d / (double)count;
2099             rsa_count = count;
2100         }
2101
2102 #  if 1
2103         ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);
2104         if (ret <= 0) {
2105             BIO_printf(bio_err,
2106                        "RSA verify failure.  No RSA verify will be done.\n");
2107             ERR_print_errors(bio_err);
2108             rsa_doit[j] = 0;
2109         } else {
2110             pkey_print_message("public", "rsa",
2111                                rsa_c[j][1], rsa_bits[j], RSA_SECONDS);
2112             Time_F(START);
2113             for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {
2114                 ret = RSA_verify(NID_md5_sha1, buf, 36, buf2,
2115                                  rsa_num, rsa_key[j]);
2116                 if (ret <= 0) {
2117                     BIO_printf(bio_err, "RSA verify failure\n");
2118                     ERR_print_errors(bio_err);
2119                     count = 1;
2120                     break;
2121                 }
2122             }
2123             d = Time_F(STOP);
2124             BIO_printf(bio_err,
2125                        mr ? "+R2:%ld:%d:%.2f\n"
2126                        : "%ld %d bit public RSA's in %.2fs\n",
2127                        count, rsa_bits[j], d);
2128             rsa_results[j][1] = d / (double)count;
2129         }
2130 #  endif
2131
2132         if (rsa_count <= 1) {
2133             /* if longer than 10s, don't do any more */
2134             for (j++; j < RSA_NUM; j++)
2135                 rsa_doit[j] = 0;
2136         }
2137     }
2138 # endif
2139
2140     RAND_pseudo_bytes(buf, 20);
2141 # ifndef OPENSSL_NO_DSA
2142     if (RAND_status() != 1) {
2143         RAND_seed(rnd_seed, sizeof rnd_seed);
2144         rnd_fake = 1;
2145     }
2146     for (j = 0; j < DSA_NUM; j++) {
2147         unsigned int kk;
2148         int ret;
2149
2150         if (!dsa_doit[j])
2151             continue;
2152
2153         /* DSA_generate_key(dsa_key[j]); */
2154         /* DSA_sign_setup(dsa_key[j],NULL); */
2155         ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
2156         if (ret == 0) {
2157             BIO_printf(bio_err,
2158                        "DSA sign failure.  No DSA sign will be done.\n");
2159             ERR_print_errors(bio_err);
2160             rsa_count = 1;
2161         } else {
2162             pkey_print_message("sign", "dsa",
2163                                dsa_c[j][0], dsa_bits[j], DSA_SECONDS);
2164             Time_F(START);
2165             for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {
2166                 ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
2167                 if (ret == 0) {
2168                     BIO_printf(bio_err, "DSA sign failure\n");
2169                     ERR_print_errors(bio_err);
2170                     count = 1;
2171                     break;
2172                 }
2173             }
2174             d = Time_F(STOP);
2175             BIO_printf(bio_err,
2176                        mr ? "+R3:%ld:%d:%.2f\n"
2177                        : "%ld %d bit DSA signs in %.2fs\n",
2178                        count, dsa_bits[j], d);
2179             dsa_results[j][0] = d / (double)count;
2180             rsa_count = count;
2181         }
2182
2183         ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
2184         if (ret <= 0) {
2185             BIO_printf(bio_err,
2186                        "DSA verify failure.  No DSA verify will be done.\n");
2187             ERR_print_errors(bio_err);
2188             dsa_doit[j] = 0;
2189         } else {
2190             pkey_print_message("verify", "dsa",
2191                                dsa_c[j][1], dsa_bits[j], DSA_SECONDS);
2192             Time_F(START);
2193             for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {
2194                 ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
2195                 if (ret <= 0) {
2196                     BIO_printf(bio_err, "DSA verify failure\n");
2197                     ERR_print_errors(bio_err);
2198                     count = 1;
2199                     break;
2200                 }
2201             }
2202             d = Time_F(STOP);
2203             BIO_printf(bio_err,
2204                        mr ? "+R4:%ld:%d:%.2f\n"
2205                        : "%ld %d bit DSA verify in %.2fs\n",
2206                        count, dsa_bits[j], d);
2207             dsa_results[j][1] = d / (double)count;
2208         }
2209
2210         if (rsa_count <= 1) {
2211             /* if longer than 10s, don't do any more */
2212             for (j++; j < DSA_NUM; j++)
2213                 dsa_doit[j] = 0;
2214         }
2215     }
2216     if (rnd_fake)
2217         RAND_cleanup();
2218 # endif
2219
2220 # ifndef OPENSSL_NO_ECDSA
2221     if (RAND_status() != 1) {
2222         RAND_seed(rnd_seed, sizeof rnd_seed);
2223         rnd_fake = 1;
2224     }
2225     for (j = 0; j < EC_NUM; j++) {
2226         int ret;
2227
2228         if (!ecdsa_doit[j])
2229             continue;           /* Ignore Curve */
2230         ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);
2231         if (ecdsa[j] == NULL) {
2232             BIO_printf(bio_err, "ECDSA failure.\n");
2233             ERR_print_errors(bio_err);
2234             rsa_count = 1;
2235         } else {
2236 #  if 1
2237             EC_KEY_precompute_mult(ecdsa[j], NULL);
2238 #  endif
2239             /* Perform ECDSA signature test */
2240             EC_KEY_generate_key(ecdsa[j]);
2241             ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);
2242             if (ret == 0) {
2243                 BIO_printf(bio_err,
2244                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
2245                 ERR_print_errors(bio_err);
2246                 rsa_count = 1;
2247             } else {
2248                 pkey_print_message("sign", "ecdsa",
2249                                    ecdsa_c[j][0],
2250                                    test_curves_bits[j], ECDSA_SECONDS);
2251
2252                 Time_F(START);
2253                 for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {
2254                     ret = ECDSA_sign(0, buf, 20,
2255                                      ecdsasig, &ecdsasiglen, ecdsa[j]);
2256                     if (ret == 0) {
2257                         BIO_printf(bio_err, "ECDSA sign failure\n");
2258                         ERR_print_errors(bio_err);
2259                         count = 1;
2260                         break;
2261                     }
2262                 }
2263                 d = Time_F(STOP);
2264
2265                 BIO_printf(bio_err,
2266                            mr ? "+R5:%ld:%d:%.2f\n" :
2267                            "%ld %d bit ECDSA signs in %.2fs \n",
2268                            count, test_curves_bits[j], d);
2269                 ecdsa_results[j][0] = d / (double)count;
2270                 rsa_count = count;
2271             }
2272
2273             /* Perform ECDSA verification test */
2274             ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
2275             if (ret != 1) {
2276                 BIO_printf(bio_err,
2277                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
2278                 ERR_print_errors(bio_err);
2279                 ecdsa_doit[j] = 0;
2280             } else {
2281                 pkey_print_message("verify", "ecdsa",
2282                                    ecdsa_c[j][1],
2283                                    test_curves_bits[j], ECDSA_SECONDS);
2284                 Time_F(START);
2285                 for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {
2286                     ret =
2287                         ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,
2288                                      ecdsa[j]);
2289                     if (ret != 1) {
2290                         BIO_printf(bio_err, "ECDSA verify failure\n");
2291                         ERR_print_errors(bio_err);
2292                         count = 1;
2293                         break;
2294                     }
2295                 }
2296                 d = Time_F(STOP);
2297                 BIO_printf(bio_err,
2298                            mr ? "+R6:%ld:%d:%.2f\n"
2299                            : "%ld %d bit ECDSA verify in %.2fs\n",
2300                            count, test_curves_bits[j], d);
2301                 ecdsa_results[j][1] = d / (double)count;
2302             }
2303
2304             if (rsa_count <= 1) {
2305                 /* if longer than 10s, don't do any more */
2306                 for (j++; j < EC_NUM; j++)
2307                     ecdsa_doit[j] = 0;
2308             }
2309         }
2310     }
2311     if (rnd_fake)
2312         RAND_cleanup();
2313 # endif
2314
2315 # ifndef OPENSSL_NO_ECDH
2316     if (RAND_status() != 1) {
2317         RAND_seed(rnd_seed, sizeof rnd_seed);
2318         rnd_fake = 1;
2319     }
2320     for (j = 0; j < EC_NUM; j++) {
2321         if (!ecdh_doit[j])
2322             continue;
2323         ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);
2324         ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);
2325         if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {
2326             BIO_printf(bio_err, "ECDH failure.\n");
2327             ERR_print_errors(bio_err);
2328             rsa_count = 1;
2329         } else {
2330             /* generate two ECDH key pairs */
2331             if (!EC_KEY_generate_key(ecdh_a[j]) ||
2332                 !EC_KEY_generate_key(ecdh_b[j])) {
2333                 BIO_printf(bio_err, "ECDH key generation failure.\n");
2334                 ERR_print_errors(bio_err);
2335                 rsa_count = 1;
2336             } else {
2337                 /*
2338                  * If field size is not more than 24 octets, then use SHA-1
2339                  * hash of result; otherwise, use result (see section 4.8 of
2340                  * draft-ietf-tls-ecc-03.txt).
2341                  */
2342                 int field_size, outlen;
2343                 void *(*kdf) (const void *in, size_t inlen, void *out,
2344                               size_t *xoutlen);
2345                 field_size =
2346                     EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));
2347                 if (field_size <= 24 * 8) {
2348                     outlen = KDF1_SHA1_len;
2349                     kdf = KDF1_SHA1;
2350                 } else {
2351                     outlen = (field_size + 7) / 8;
2352                     kdf = NULL;
2353                 }
2354                 secret_size_a =
2355                     ECDH_compute_key(secret_a, outlen,
2356                                      EC_KEY_get0_public_key(ecdh_b[j]),
2357                                      ecdh_a[j], kdf);
2358                 secret_size_b =
2359                     ECDH_compute_key(secret_b, outlen,
2360                                      EC_KEY_get0_public_key(ecdh_a[j]),
2361                                      ecdh_b[j], kdf);
2362                 if (secret_size_a != secret_size_b)
2363                     ecdh_checks = 0;
2364                 else
2365                     ecdh_checks = 1;
2366
2367                 for (secret_idx = 0; (secret_idx < secret_size_a)
2368                      && (ecdh_checks == 1); secret_idx++) {
2369                     if (secret_a[secret_idx] != secret_b[secret_idx])
2370                         ecdh_checks = 0;
2371                 }
2372
2373                 if (ecdh_checks == 0) {
2374                     BIO_printf(bio_err, "ECDH computations don't match.\n");
2375                     ERR_print_errors(bio_err);
2376                     rsa_count = 1;
2377                 }
2378
2379                 pkey_print_message("", "ecdh",
2380                                    ecdh_c[j][0],
2381                                    test_curves_bits[j], ECDH_SECONDS);
2382                 Time_F(START);
2383                 for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {
2384                     ECDH_compute_key(secret_a, outlen,
2385                                      EC_KEY_get0_public_key(ecdh_b[j]),
2386                                      ecdh_a[j], kdf);
2387                 }
2388                 d = Time_F(STOP);
2389                 BIO_printf(bio_err,
2390                            mr ? "+R7:%ld:%d:%.2f\n" :
2391                            "%ld %d-bit ECDH ops in %.2fs\n", count,
2392                            test_curves_bits[j], d);
2393                 ecdh_results[j][0] = d / (double)count;
2394                 rsa_count = count;
2395             }
2396         }
2397
2398         if (rsa_count <= 1) {
2399             /* if longer than 10s, don't do any more */
2400             for (j++; j < EC_NUM; j++)
2401                 ecdh_doit[j] = 0;
2402         }
2403     }
2404     if (rnd_fake)
2405         RAND_cleanup();
2406 # endif
2407 # ifndef NO_FORK
2408  show_res:
2409 # endif
2410     if (!mr) {
2411         fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_VERSION));
2412         fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_BUILT_ON));
2413         printf("options:");
2414         printf("%s ", BN_options());
2415 # ifndef OPENSSL_NO_MD2
2416         printf("%s ", MD2_options());
2417 # endif
2418 # ifndef OPENSSL_NO_RC4
2419         printf("%s ", RC4_options());
2420 # endif
2421 # ifndef OPENSSL_NO_DES
2422         printf("%s ", DES_options());
2423 # endif
2424 # ifndef OPENSSL_NO_AES
2425         printf("%s ", AES_options());
2426 # endif
2427 # ifndef OPENSSL_NO_IDEA
2428         printf("%s ", idea_options());
2429 # endif
2430 # ifndef OPENSSL_NO_BF
2431         printf("%s ", BF_options());
2432 # endif
2433         fprintf(stdout, "\n%s\n", SSLeay_version(SSLEAY_CFLAGS));
2434     }
2435
2436     if (pr_header) {
2437         if (mr)
2438             fprintf(stdout, "+H");
2439         else {
2440             fprintf(stdout,
2441                     "The 'numbers' are in 1000s of bytes per second processed.\n");
2442             fprintf(stdout, "type        ");
2443         }
2444         for (j = 0; j < SIZE_NUM; j++)
2445             fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]);
2446         fprintf(stdout, "\n");
2447     }
2448
2449     for (k = 0; k < ALGOR_NUM; k++) {
2450         if (!doit[k])
2451             continue;
2452         if (mr)
2453             fprintf(stdout, "+F:%d:%s", k, names[k]);
2454         else
2455             fprintf(stdout, "%-13s", names[k]);
2456         for (j = 0; j < SIZE_NUM; j++) {
2457             if (results[k][j] > 10000 && !mr)
2458                 fprintf(stdout, " %11.2fk", results[k][j] / 1e3);
2459             else
2460                 fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]);
2461         }
2462         fprintf(stdout, "\n");
2463     }
2464 # ifndef OPENSSL_NO_RSA
2465     j = 1;
2466     for (k = 0; k < RSA_NUM; k++) {
2467         if (!rsa_doit[k])
2468             continue;
2469         if (j && !mr) {
2470             printf("%18ssign    verify    sign/s verify/s\n", " ");
2471             j = 0;
2472         }
2473         if (mr)
2474             fprintf(stdout, "+F2:%u:%u:%f:%f\n",
2475                     k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2476         else
2477             fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2478                     rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2479                     1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
2480     }
2481 # endif
2482 # ifndef OPENSSL_NO_DSA
2483     j = 1;
2484     for (k = 0; k < DSA_NUM; k++) {
2485         if (!dsa_doit[k])
2486             continue;
2487         if (j && !mr) {
2488             printf("%18ssign    verify    sign/s verify/s\n", " ");
2489             j = 0;
2490         }
2491         if (mr)
2492             fprintf(stdout, "+F3:%u:%u:%f:%f\n",
2493                     k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2494         else
2495             fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2496                     dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2497                     1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
2498     }
2499 # endif
2500 # ifndef OPENSSL_NO_ECDSA
2501     j = 1;
2502     for (k = 0; k < EC_NUM; k++) {
2503         if (!ecdsa_doit[k])
2504             continue;
2505         if (j && !mr) {
2506             printf("%30ssign    verify    sign/s verify/s\n", " ");
2507             j = 0;
2508         }
2509
2510         if (mr)
2511             fprintf(stdout, "+F4:%u:%u:%f:%f\n",
2512                     k, test_curves_bits[k],
2513                     ecdsa_results[k][0], ecdsa_results[k][1]);
2514         else
2515             fprintf(stdout,
2516                     "%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2517                     test_curves_bits[k],
2518                     test_curves_names[k],
2519                     ecdsa_results[k][0], ecdsa_results[k][1],
2520                     1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
2521     }
2522 # endif
2523
2524 # ifndef OPENSSL_NO_ECDH
2525     j = 1;
2526     for (k = 0; k < EC_NUM; k++) {
2527         if (!ecdh_doit[k])
2528             continue;
2529         if (j && !mr) {
2530             printf("%30sop      op/s\n", " ");
2531             j = 0;
2532         }
2533         if (mr)
2534             fprintf(stdout, "+F5:%u:%u:%f:%f\n",
2535                     k, test_curves_bits[k],
2536                     ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2537
2538         else
2539             fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\n",
2540                     test_curves_bits[k],
2541                     test_curves_names[k],
2542                     ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2543     }
2544 # endif
2545
2546     mret = 0;
2547
2548  end:
2549     ERR_print_errors(bio_err);
2550     if (buf_malloc != NULL)
2551         OPENSSL_free(buf_malloc);
2552     if (buf2_malloc != NULL)
2553         OPENSSL_free(buf2_malloc);
2554 # ifndef OPENSSL_NO_RSA
2555     for (i = 0; i < RSA_NUM; i++)
2556         if (rsa_key[i] != NULL)
2557             RSA_free(rsa_key[i]);
2558 # endif
2559 # ifndef OPENSSL_NO_DSA
2560     for (i = 0; i < DSA_NUM; i++)
2561         if (dsa_key[i] != NULL)
2562             DSA_free(dsa_key[i]);
2563 # endif
2564
2565 # ifndef OPENSSL_NO_ECDSA
2566     for (i = 0; i < EC_NUM; i++)
2567         if (ecdsa[i] != NULL)
2568             EC_KEY_free(ecdsa[i]);
2569 # endif
2570 # ifndef OPENSSL_NO_ECDH
2571     for (i = 0; i < EC_NUM; i++) {
2572         if (ecdh_a[i] != NULL)
2573             EC_KEY_free(ecdh_a[i]);
2574         if (ecdh_b[i] != NULL)
2575             EC_KEY_free(ecdh_b[i]);
2576     }
2577 # endif
2578
2579     apps_shutdown();
2580     OPENSSL_EXIT(mret);
2581 }
2582
2583 static void print_message(const char *s, long num, int length)
2584 {
2585 # ifdef SIGALRM
2586     BIO_printf(bio_err,
2587                mr ? "+DT:%s:%d:%d\n"
2588                : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2589     (void)BIO_flush(bio_err);
2590     alarm(SECONDS);
2591 # else
2592     BIO_printf(bio_err,
2593                mr ? "+DN:%s:%ld:%d\n"
2594                : "Doing %s %ld times on %d size blocks: ", s, num, length);
2595     (void)BIO_flush(bio_err);
2596 # endif
2597 }
2598
2599 static void pkey_print_message(const char *str, const char *str2, long num,
2600                                int bits, int tm)
2601 {
2602 # ifdef SIGALRM
2603     BIO_printf(bio_err,
2604                mr ? "+DTP:%d:%s:%s:%d\n"
2605                : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2606     (void)BIO_flush(bio_err);
2607     alarm(tm);
2608 # else
2609     BIO_printf(bio_err,
2610                mr ? "+DNP:%ld:%d:%s:%s\n"
2611                : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2612     (void)BIO_flush(bio_err);
2613 # endif
2614 }
2615
2616 static void print_result(int alg, int run_no, int count, double time_used)
2617 {
2618     BIO_printf(bio_err,
2619                mr ? "+R:%d:%s:%f\n"
2620                : "%d %s's in %.2fs\n", count, names[alg], time_used);
2621     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2622 }
2623
2624 # ifndef NO_FORK
2625 static char *sstrsep(char **string, const char *delim)
2626 {
2627     char isdelim[256];
2628     char *token = *string;
2629
2630     if (**string == 0)
2631         return NULL;
2632
2633     memset(isdelim, 0, sizeof isdelim);
2634     isdelim[0] = 1;
2635
2636     while (*delim) {
2637         isdelim[(unsigned char)(*delim)] = 1;
2638         delim++;
2639     }
2640
2641     while (!isdelim[(unsigned char)(**string)]) {
2642         (*string)++;
2643     }
2644
2645     if (**string) {
2646         **string = 0;
2647         (*string)++;
2648     }
2649
2650     return token;
2651 }
2652
2653 static int do_multi(int multi)
2654 {
2655     int n;
2656     int fd[2];
2657     int *fds;
2658     static char sep[] = ":";
2659
2660     fds = malloc(multi * sizeof *fds);
2661     for (n = 0; n < multi; ++n) {
2662         if (pipe(fd) == -1) {
2663             fprintf(stderr, "pipe failure\n");
2664             exit(1);
2665         }
2666         fflush(stdout);
2667         fflush(stderr);
2668         if (fork()) {
2669             close(fd[1]);
2670             fds[n] = fd[0];
2671         } else {
2672             close(fd[0]);
2673             close(1);
2674             if (dup(fd[1]) == -1) {
2675                 fprintf(stderr, "dup failed\n");
2676                 exit(1);
2677             }
2678             close(fd[1]);
2679             mr = 1;
2680             usertime = 0;
2681             free(fds);
2682             return 0;
2683         }
2684         printf("Forked child %d\n", n);
2685     }
2686
2687     /* for now, assume the pipe is long enough to take all the output */
2688     for (n = 0; n < multi; ++n) {
2689         FILE *f;
2690         char buf[1024];
2691         char *p;
2692
2693         f = fdopen(fds[n], "r");
2694         while (fgets(buf, sizeof buf, f)) {
2695             p = strchr(buf, '\n');
2696             if (p)
2697                 *p = '\0';
2698             if (buf[0] != '+') {
2699                 fprintf(stderr, "Don't understand line '%s' from child %d\n",
2700                         buf, n);
2701                 continue;
2702             }
2703             printf("Got: %s from %d\n", buf, n);
2704             if (!strncmp(buf, "+F:", 3)) {
2705                 int alg;
2706                 int j;
2707
2708                 p = buf + 3;
2709                 alg = atoi(sstrsep(&p, sep));
2710                 sstrsep(&p, sep);
2711                 for (j = 0; j < SIZE_NUM; ++j)
2712                     results[alg][j] += atof(sstrsep(&p, sep));
2713             } else if (!strncmp(buf, "+F2:", 4)) {
2714                 int k;
2715                 double d;
2716
2717                 p = buf + 4;
2718                 k = atoi(sstrsep(&p, sep));
2719                 sstrsep(&p, sep);
2720
2721                 d = atof(sstrsep(&p, sep));
2722                 if (n)
2723                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
2724                 else
2725                     rsa_results[k][0] = d;
2726
2727                 d = atof(sstrsep(&p, sep));
2728                 if (n)
2729                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
2730                 else
2731                     rsa_results[k][1] = d;
2732             } else if (!strncmp(buf, "+F2:", 4)) {
2733                 int k;
2734                 double d;
2735
2736                 p = buf + 4;
2737                 k = atoi(sstrsep(&p, sep));
2738                 sstrsep(&p, sep);
2739
2740                 d = atof(sstrsep(&p, sep));
2741                 if (n)
2742                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
2743                 else
2744                     rsa_results[k][0] = d;
2745
2746                 d = atof(sstrsep(&p, sep));
2747                 if (n)
2748                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
2749                 else
2750                     rsa_results[k][1] = d;
2751             }
2752 #  ifndef OPENSSL_NO_DSA
2753             else if (!strncmp(buf, "+F3:", 4)) {
2754                 int k;
2755                 double d;
2756
2757                 p = buf + 4;
2758                 k = atoi(sstrsep(&p, sep));
2759                 sstrsep(&p, sep);
2760
2761                 d = atof(sstrsep(&p, sep));
2762                 if (n)
2763                     dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
2764                 else
2765                     dsa_results[k][0] = d;
2766
2767                 d = atof(sstrsep(&p, sep));
2768                 if (n)
2769                     dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
2770                 else
2771                     dsa_results[k][1] = d;
2772             }
2773 #  endif
2774 #  ifndef OPENSSL_NO_ECDSA
2775             else if (!strncmp(buf, "+F4:", 4)) {
2776                 int k;
2777                 double d;
2778
2779                 p = buf + 4;
2780                 k = atoi(sstrsep(&p, sep));
2781                 sstrsep(&p, sep);
2782
2783                 d = atof(sstrsep(&p, sep));
2784                 if (n)
2785                     ecdsa_results[k][0] =
2786                         1 / (1 / ecdsa_results[k][0] + 1 / d);
2787                 else
2788                     ecdsa_results[k][0] = d;
2789
2790                 d = atof(sstrsep(&p, sep));
2791                 if (n)
2792                     ecdsa_results[k][1] =
2793                         1 / (1 / ecdsa_results[k][1] + 1 / d);
2794                 else
2795                     ecdsa_results[k][1] = d;
2796             }
2797 #  endif
2798
2799 #  ifndef OPENSSL_NO_ECDH
2800             else if (!strncmp(buf, "+F5:", 4)) {
2801                 int k;
2802                 double d;
2803
2804                 p = buf + 4;
2805                 k = atoi(sstrsep(&p, sep));
2806                 sstrsep(&p, sep);
2807
2808                 d = atof(sstrsep(&p, sep));
2809                 if (n)
2810                     ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
2811                 else
2812                     ecdh_results[k][0] = d;
2813
2814             }
2815 #  endif
2816
2817             else if (!strncmp(buf, "+H:", 3)) {
2818             } else
2819                 fprintf(stderr, "Unknown type '%s' from child %d\n", buf, n);
2820         }
2821
2822         fclose(f);
2823     }
2824     free(fds);
2825     return 1;
2826 }
2827 # endif
2828
2829 static void multiblock_speed(const EVP_CIPHER *evp_cipher)
2830 {
2831     static int mblengths[] =
2832         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
2833     int j, count, num = sizeof(lengths) / sizeof(lengths[0]);
2834     const char *alg_name;
2835     unsigned char *inp, *out, no_key[32], no_iv[16];
2836     EVP_CIPHER_CTX ctx;
2837     double d = 0.0;
2838
2839     inp = OPENSSL_malloc(mblengths[num - 1]);
2840     out = OPENSSL_malloc(mblengths[num - 1] + 1024);
2841
2842     EVP_CIPHER_CTX_init(&ctx);
2843     EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, no_key, no_iv);
2844     EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
2845                         no_key);
2846     alg_name = OBJ_nid2ln(evp_cipher->nid);
2847
2848     for (j = 0; j < num; j++) {
2849         print_message(alg_name, 0, mblengths[j]);
2850         Time_F(START);
2851         for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
2852             unsigned char aad[13];
2853             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
2854             size_t len = mblengths[j];
2855             int packlen;
2856
2857             memset(aad, 0, 8);  /* avoid uninitialized values */
2858             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
2859             aad[9] = 3;         /* version */
2860             aad[10] = 2;
2861             aad[11] = 0;        /* length */
2862             aad[12] = 0;
2863             mb_param.out = NULL;
2864             mb_param.inp = aad;
2865             mb_param.len = len;
2866             mb_param.interleave = 8;
2867
2868             packlen = EVP_CIPHER_CTX_ctrl(&ctx,
2869                                           EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
2870                                           sizeof(mb_param), &mb_param);
2871
2872             if (packlen > 0) {
2873                 mb_param.out = out;
2874                 mb_param.inp = inp;
2875                 mb_param.len = len;
2876                 EVP_CIPHER_CTX_ctrl(&ctx,
2877                                     EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
2878                                     sizeof(mb_param), &mb_param);
2879             } else {
2880                 int pad;
2881
2882                 RAND_bytes(out, 16);
2883                 len += 16;
2884                 aad[11] = len >> 8;
2885                 aad[12] = len;
2886                 pad = EVP_CIPHER_CTX_ctrl(&ctx,
2887                                           EVP_CTRL_AEAD_TLS1_AAD, 13, aad);
2888                 EVP_Cipher(&ctx, out, inp, len + pad);
2889             }
2890         }
2891         d = Time_F(STOP);
2892         BIO_printf(bio_err,
2893                    mr ? "+R:%d:%s:%f\n"
2894                    : "%d %s's in %.2fs\n", count, "evp", d);
2895         results[D_EVP][j] = ((double)count) / d * mblengths[j];
2896     }
2897
2898     if (mr) {
2899         fprintf(stdout, "+H");
2900         for (j = 0; j < num; j++)
2901             fprintf(stdout, ":%d", mblengths[j]);
2902         fprintf(stdout, "\n");
2903         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
2904         for (j = 0; j < num; j++)
2905             fprintf(stdout, ":%.2f", results[D_EVP][j]);
2906         fprintf(stdout, "\n");
2907     } else {
2908         fprintf(stdout,
2909                 "The 'numbers' are in 1000s of bytes per second processed.\n");
2910         fprintf(stdout, "type                    ");
2911         for (j = 0; j < num; j++)
2912             fprintf(stdout, "%7d bytes", mblengths[j]);
2913         fprintf(stdout, "\n");
2914         fprintf(stdout, "%-24s", alg_name);
2915
2916         for (j = 0; j < num; j++) {
2917             if (results[D_EVP][j] > 10000)
2918                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
2919             else
2920                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
2921         }
2922         fprintf(stdout, "\n");
2923     }
2924
2925     OPENSSL_free(inp);
2926     OPENSSL_free(out);
2927 }
2928 #endif