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