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