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