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