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