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