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