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