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