17f2cf3924aec9641df71ab480c3bfcfe9f7848f
[openssl.git] / apps / speed.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #undef SECONDS
12 #define SECONDS                 3
13 #define RSA_SECONDS             10
14 #define DSA_SECONDS             10
15 #define ECDSA_SECONDS   10
16 #define ECDH_SECONDS    10
17 #define EdDSA_SECONDS   10
18 #define SM2_SECONDS     10
19
20 /* We need to use some deprecated APIs */
21 #define OPENSSL_SUPPRESS_DEPRECATED
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include "apps.h"
28 #include "progs.h"
29 #include <openssl/crypto.h>
30 #include <openssl/rand.h>
31 #include <openssl/err.h>
32 #include <openssl/evp.h>
33 #include <openssl/objects.h>
34 #include <openssl/async.h>
35 #if !defined(OPENSSL_SYS_MSDOS)
36 # include <unistd.h>
37 #endif
38
39 #if defined(_WIN32)
40 # include <windows.h>
41 #endif
42
43 #include <openssl/bn.h>
44 #ifndef OPENSSL_NO_DES
45 # include <openssl/des.h>
46 #endif
47 #ifndef OPENSSL_NO_DEPRECATED_3_0
48 #include <openssl/aes.h>
49 #endif
50 #ifndef OPENSSL_NO_CAMELLIA
51 # include <openssl/camellia.h>
52 #endif
53 #ifndef OPENSSL_NO_MD2
54 # include <openssl/md2.h>
55 #endif
56 #ifndef OPENSSL_NO_MDC2
57 # include <openssl/mdc2.h>
58 #endif
59 #ifndef OPENSSL_NO_MD4
60 # include <openssl/md4.h>
61 #endif
62 #ifndef OPENSSL_NO_MD5
63 # include <openssl/md5.h>
64 #endif
65 #include <openssl/hmac.h>
66 #ifndef OPENSSL_NO_CMAC
67 #include <openssl/cmac.h>
68 #endif
69 #include <openssl/sha.h>
70 #ifndef OPENSSL_NO_RMD160
71 # include <openssl/ripemd.h>
72 #endif
73 #ifndef OPENSSL_NO_WHIRLPOOL
74 # include <openssl/whrlpool.h>
75 #endif
76 #ifndef OPENSSL_NO_RC4
77 # include <openssl/rc4.h>
78 #endif
79 #ifndef OPENSSL_NO_RC5
80 # include <openssl/rc5.h>
81 #endif
82 #ifndef OPENSSL_NO_RC2
83 # include <openssl/rc2.h>
84 #endif
85 #ifndef OPENSSL_NO_IDEA
86 # include <openssl/idea.h>
87 #endif
88 #ifndef OPENSSL_NO_SEED
89 # include <openssl/seed.h>
90 #endif
91 #ifndef OPENSSL_NO_BF
92 # include <openssl/blowfish.h>
93 #endif
94 #ifndef OPENSSL_NO_CAST
95 # include <openssl/cast.h>
96 #endif
97 #ifndef OPENSSL_NO_RSA
98 # include <openssl/rsa.h>
99 # include "./testrsa.h"
100 #endif
101 #include <openssl/x509.h>
102 #ifndef OPENSSL_NO_DSA
103 # include <openssl/dsa.h>
104 # include "./testdsa.h"
105 #endif
106 #ifndef OPENSSL_NO_EC
107 # include <openssl/ec.h>
108 #endif
109 #include <openssl/modes.h>
110
111 #ifndef HAVE_FORK
112 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
113 #  define HAVE_FORK 0
114 # else
115 #  define HAVE_FORK 1
116 # endif
117 #endif
118
119 #if HAVE_FORK
120 # undef NO_FORK
121 #else
122 # define NO_FORK
123 #endif
124
125 #define MAX_MISALIGNMENT 63
126 #define MAX_ECDH_SIZE   256
127 #define MISALIGN        64
128
129 typedef struct openssl_speed_sec_st {
130     int sym;
131     int rsa;
132     int dsa;
133     int ecdsa;
134     int ecdh;
135     int eddsa;
136     int sm2;
137 } openssl_speed_sec_t;
138
139 static volatile int run = 0;
140
141 static int mr = 0;  /* machine-readeable output format to merge fork results */
142 static int usertime = 1;
143
144 static double Time_F(int s);
145 static void print_message(const char *s, long num, int length, int tm);
146 static void pkey_print_message(const char *str, const char *str2,
147                                long num, unsigned int bits, int sec);
148 static void print_result(int alg, int run_no, int count, double time_used);
149 #ifndef NO_FORK
150 static int do_multi(int multi, int size_num);
151 #endif
152
153 static const int lengths_list[] = {
154     16, 64, 256, 1024, 8 * 1024, 16 * 1024
155 };
156 #define SIZE_NUM         OSSL_NELEM(lengths_list)
157 static const int *lengths = lengths_list;
158
159 static const int aead_lengths_list[] = {
160     2, 31, 136, 1024, 8 * 1024, 16 * 1024
161 };
162
163 #define START   0
164 #define STOP    1
165
166 #ifdef SIGALRM
167
168 static void alarmed(int sig)
169 {
170     signal(SIGALRM, alarmed);
171     run = 0;
172 }
173
174 static double Time_F(int s)
175 {
176     double ret = app_tminterval(s, usertime);
177     if (s == STOP)
178         alarm(0);
179     return ret;
180 }
181
182 #elif defined(_WIN32)
183
184 # define SIGALRM -1
185
186 static unsigned int lapse;
187 static volatile unsigned int schlock;
188 static void alarm_win32(unsigned int secs)
189 {
190     lapse = secs * 1000;
191 }
192
193 # define alarm alarm_win32
194
195 static DWORD WINAPI sleepy(VOID * arg)
196 {
197     schlock = 1;
198     Sleep(lapse);
199     run = 0;
200     return 0;
201 }
202
203 static double Time_F(int s)
204 {
205     double ret;
206     static HANDLE thr;
207
208     if (s == START) {
209         schlock = 0;
210         thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
211         if (thr == NULL) {
212             DWORD err = GetLastError();
213             BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
214             ExitProcess(err);
215         }
216         while (!schlock)
217             Sleep(0);           /* scheduler spinlock */
218         ret = app_tminterval(s, usertime);
219     } else {
220         ret = app_tminterval(s, usertime);
221         if (run)
222             TerminateThread(thr, 0);
223         CloseHandle(thr);
224     }
225
226     return ret;
227 }
228 #else
229 static double Time_F(int s)
230 {
231     return app_tminterval(s, usertime);
232 }
233 #endif
234
235 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
236                              const openssl_speed_sec_t *seconds);
237
238 static int opt_found(const char *name, unsigned int *result,
239                      const OPT_PAIR pairs[], unsigned int nbelem)
240 {
241     unsigned int idx;
242
243     for (idx = 0; idx < nbelem; ++idx, pairs++)
244         if (strcmp(name, pairs->name) == 0) {
245             *result = pairs->retval;
246             return 1;
247         }
248     return 0;
249 }
250 #define opt_found(value, pairs, result)\
251     opt_found(value, result, pairs, OSSL_NELEM(pairs))
252
253 typedef enum OPTION_choice {
254     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
255     OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
256     OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM,
257     OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC
258 } OPTION_CHOICE;
259
260 const OPTIONS speed_options[] = {
261     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [algorithm...]\n"},
262
263     OPT_SECTION("General"),
264     {"help", OPT_HELP, '-', "Display this summary"},
265     {"mb", OPT_MB, '-',
266      "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
267     {"mr", OPT_MR, '-', "Produce machine readable output"},
268 #ifndef NO_FORK
269     {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
270 #endif
271 #ifndef OPENSSL_NO_ASYNC
272     {"async_jobs", OPT_ASYNCJOBS, 'p',
273      "Enable async mode and start specified number of jobs"},
274 #endif
275 #ifndef OPENSSL_NO_ENGINE
276     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
277 #endif
278     {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
279
280     OPT_SECTION("Selection"),
281     {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
282 #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 #ifndef OPENSSL_NO_DSA
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 #ifndef OPENSSL_NO_RSA
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 #ifndef OPENSSL_NO_RSA
546     RSA *rsa_key[RSA_NUM];
547 #endif
548 #ifndef OPENSSL_NO_DSA
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 #ifndef OPENSSL_NO_RSA
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 #ifndef OPENSSL_NO_DSA
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 #ifndef OPENSSL_NO_RSA
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 #ifndef OPENSSL_NO_DSA
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             if (!opt_int(opt_arg(), &primes))
1711                 goto end;
1712             break;
1713         case OPT_SECONDS:
1714             seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1715                         = seconds.ecdh = seconds.eddsa
1716                         = seconds.sm2 = atoi(opt_arg());
1717             break;
1718         case OPT_BYTES:
1719             lengths_single = atoi(opt_arg());
1720             lengths = &lengths_single;
1721             size_num = 1;
1722             break;
1723         case OPT_AEAD:
1724             aead = 1;
1725             break;
1726         }
1727     }
1728     argc = opt_num_rest();
1729     argv = opt_rest();
1730
1731     /* Remaining arguments are algorithms. */
1732     for (; *argv; argv++) {
1733         const char *algo = *argv;
1734
1735         if (opt_found(algo, doit_choices, &i)) {
1736             doit[i] = 1;
1737             continue;
1738         }
1739 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1740         if (strcmp(algo, "des") == 0) {
1741             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1742             continue;
1743         }
1744 #endif
1745         if (strcmp(algo, "sha") == 0) {
1746             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1747             continue;
1748         }
1749 #ifndef OPENSSL_NO_RSA
1750         if (strcmp(algo, "openssl") == 0) /* just for compatibility */
1751             continue;
1752         if (strncmp(algo, "rsa", 3) == 0) {
1753             if (algo[3] == '\0') {
1754                 memset(rsa_doit, 1, sizeof(rsa_doit));
1755                 continue;
1756             }
1757             if (opt_found(algo, rsa_choices, &i)) {
1758                 rsa_doit[i] = 1;
1759                 continue;
1760             }
1761         }
1762 #endif
1763 #ifndef OPENSSL_NO_DSA
1764         if (strncmp(algo, "dsa", 3) == 0) {
1765             if (algo[3] == '\0') {
1766                 memset(dsa_doit, 1, sizeof(dsa_doit));
1767                 continue;
1768             }
1769             if (opt_found(algo, dsa_choices, &i)) {
1770                 dsa_doit[i] = 2;
1771                 continue;
1772             }
1773         }
1774 #endif
1775 #ifndef OPENSSL_NO_DEPRECATED_3_0
1776         if (strcmp(algo, "aes") == 0) {
1777             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1778             continue;
1779         }
1780 #endif
1781 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1782         if (strcmp(algo, "camellia") == 0) {
1783             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1784             continue;
1785         }
1786 #endif
1787 #ifndef OPENSSL_NO_EC
1788         if (strncmp(algo, "ecdsa", 5) == 0) {
1789             if (algo[5] == '\0') {
1790                 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
1791                 continue;
1792             }
1793             if (opt_found(algo, ecdsa_choices, &i)) {
1794                 ecdsa_doit[i] = 2;
1795                 continue;
1796             }
1797         }
1798         if (strncmp(algo, "ecdh", 4) == 0) {
1799             if (algo[4] == '\0') {
1800                 memset(ecdh_doit, 1, sizeof(ecdh_doit));
1801                 continue;
1802             }
1803             if (opt_found(algo, ecdh_choices, &i)) {
1804                 ecdh_doit[i] = 2;
1805                 continue;
1806             }
1807         }
1808         if (strcmp(algo, "eddsa") == 0) {
1809             memset(eddsa_doit, 1, sizeof(eddsa_doit));
1810             continue;
1811         }
1812         if (opt_found(algo, eddsa_choices, &i)) {
1813             eddsa_doit[i] = 2;
1814             continue;
1815         }
1816 # ifndef OPENSSL_NO_SM2
1817         if (strcmp(algo, "sm2") == 0) {
1818             memset(sm2_doit, 1, sizeof(sm2_doit));
1819             continue;
1820         }
1821         if (opt_found(algo, sm2_choices, &i)) {
1822             sm2_doit[i] = 2;
1823             continue;
1824         }
1825 # endif
1826 #endif  /* OPENSSL_NO_EC */
1827         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
1828         goto end;
1829     }
1830
1831     /* Sanity checks */
1832     if (aead) {
1833         if (evp_cipher == NULL) {
1834             BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
1835             goto end;
1836         } else if (!(EVP_CIPHER_flags(evp_cipher) &
1837                      EVP_CIPH_FLAG_AEAD_CIPHER)) {
1838             BIO_printf(bio_err, "%s is not an AEAD cipher\n",
1839                        OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1840             goto end;
1841         }
1842     }
1843     if (multiblock) {
1844         if (evp_cipher == NULL) {
1845             BIO_printf(bio_err,"-mb can be used only with a multi-block"
1846                                " capable cipher\n");
1847             goto end;
1848         } else if (!(EVP_CIPHER_flags(evp_cipher) &
1849                      EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1850             BIO_printf(bio_err, "%s is not a multi-block capable\n",
1851                        OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1852             goto end;
1853         } else if (async_jobs > 0) {
1854             BIO_printf(bio_err, "Async mode is not supported with -mb");
1855             goto end;
1856         }
1857     }
1858
1859     /* Initialize the job pool if async mode is enabled */
1860     if (async_jobs > 0) {
1861         async_init = ASYNC_init_thread(async_jobs, async_jobs);
1862         if (!async_init) {
1863             BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1864             goto end;
1865         }
1866     }
1867
1868     loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1869     loopargs =
1870         app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1871     memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1872
1873     for (i = 0; i < loopargs_len; i++) {
1874         if (async_jobs > 0) {
1875             loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1876             if (loopargs[i].wait_ctx == NULL) {
1877                 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1878                 goto end;
1879             }
1880         }
1881
1882         buflen = lengths[size_num - 1];
1883         if (buflen < 36)    /* size of random vector in RSA benchmark */
1884             buflen = 36;
1885         buflen += MAX_MISALIGNMENT + 1;
1886         loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
1887         loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
1888         memset(loopargs[i].buf_malloc, 0, buflen);
1889         memset(loopargs[i].buf2_malloc, 0, buflen);
1890
1891         /* Align the start of buffers on a 64 byte boundary */
1892         loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1893         loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1894 #ifndef OPENSSL_NO_EC
1895         loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1896         loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1897 #endif
1898     }
1899
1900 #ifndef NO_FORK
1901     if (multi && do_multi(multi, size_num))
1902         goto show_res;
1903 #endif
1904
1905     /* Initialize the engine after the fork */
1906     e = setup_engine(engine_id, 0);
1907
1908     /* No parameters; turn on everything. */
1909     if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC] && !doit[D_EVP_CMAC]) {
1910         memset(doit, 1, sizeof(doit));
1911         doit[D_EVP] = doit[D_EVP_HMAC] = doit[D_EVP_CMAC] = 0;
1912 #ifndef OPENSSL_NO_RSA
1913         memset(rsa_doit, 1, sizeof(rsa_doit));
1914 #endif
1915 #ifndef OPENSSL_NO_DSA
1916         memset(dsa_doit, 1, sizeof(dsa_doit));
1917 #endif
1918 #ifndef OPENSSL_NO_EC
1919         memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
1920         memset(ecdh_doit, 1, sizeof(ecdh_doit));
1921         memset(eddsa_doit, 1, sizeof(eddsa_doit));
1922 # ifndef OPENSSL_NO_SM2
1923         memset(sm2_doit, 1, sizeof(sm2_doit));
1924 # endif
1925 #endif
1926     }
1927     for (i = 0; i < ALGOR_NUM; i++)
1928         if (doit[i])
1929             pr_header++;
1930
1931     if (usertime == 0 && !mr)
1932         BIO_printf(bio_err,
1933                    "You have chosen to measure elapsed time "
1934                    "instead of user CPU time.\n");
1935
1936 #ifndef OPENSSL_NO_RSA
1937     for (i = 0; i < loopargs_len; i++) {
1938         if (primes > RSA_DEFAULT_PRIME_NUM) {
1939             /* for multi-prime RSA, skip this */
1940             break;
1941         }
1942         for (k = 0; k < RSA_NUM; k++) {
1943             const unsigned char *p = rsa_keys[k].data;
1944
1945             loopargs[i].rsa_key[k] =
1946                 d2i_RSAPrivateKey(NULL, &p, rsa_keys[k].length);
1947             if (loopargs[i].rsa_key[k] == NULL) {
1948                 BIO_printf(bio_err,
1949                            "internal error loading RSA key number %d\n", k);
1950                 goto end;
1951             }
1952         }
1953     }
1954 #endif
1955 #ifndef OPENSSL_NO_DSA
1956     for (i = 0; i < loopargs_len; i++) {
1957         loopargs[i].dsa_key[0] = get_dsa(512);
1958         loopargs[i].dsa_key[1] = get_dsa(1024);
1959         loopargs[i].dsa_key[2] = get_dsa(2048);
1960     }
1961 #endif
1962 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1963     if (doit[D_CBC_DES] || doit[D_EDE3_DES]) {
1964         static DES_cblock keys[] = {
1965             { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }, /* keys[0] */
1966             { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }, /* keys[1] */
1967             { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 }  /* keys[3] */
1968         };
1969         DES_set_key_unchecked(&keys[0], &sch[0]);
1970         DES_set_key_unchecked(&keys[1], &sch[1]);
1971         DES_set_key_unchecked(&keys[2], &sch[2]);
1972     }
1973 #endif
1974 #ifndef OPENSSL_NO_DEPRECATED_3_0
1975     AES_set_encrypt_key(key16, 128, &aes_ks1);
1976     AES_set_encrypt_key(key24, 192, &aes_ks2);
1977     AES_set_encrypt_key(key32, 256, &aes_ks3);
1978 #endif
1979 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1980     if (doit[D_CBC_128_CML] || doit[D_CBC_192_CML] || doit[D_CBC_256_CML]) {
1981         Camellia_set_key(key16, 128, &camellia_ks[0]);
1982         Camellia_set_key(key24, 192, &camellia_ks[1]);
1983         Camellia_set_key(key32, 256, &camellia_ks[2]);
1984     }
1985 #endif
1986 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1987     if (doit[D_CBC_IDEA])
1988         IDEA_set_encrypt_key(key16, &idea_ks);
1989 #endif
1990 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1991     if (doit[D_CBC_SEED])
1992         SEED_set_key(key16, &seed_ks);
1993 #endif
1994 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1995     if (doit[D_RC4])
1996         RC4_set_key(&rc4_ks, 16, key16);
1997 #endif
1998 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1999     if (doit[D_CBC_RC2])
2000         RC2_set_key(&rc2_ks, 16, key16, 128);
2001 #endif
2002 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2003     if (doit[D_CBC_RC5])
2004         if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
2005             BIO_printf(bio_err, "Failed setting RC5 key\n");
2006             goto end;
2007         }
2008 #endif
2009 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2010     if (doit[D_CBC_BF])
2011         BF_set_key(&bf_ks, 16, key16);
2012 #endif
2013 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2014     if (doit[D_CBC_CAST]) 
2015         CAST_set_key(&cast_ks, 16, key16);
2016 #endif
2017 #ifndef SIGALRM
2018 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2019     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
2020     count = 10;
2021     do {
2022         long it;
2023         count *= 2;
2024         Time_F(START);
2025         for (it = count; it; it--)
2026             DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
2027                             (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
2028         d = Time_F(STOP);
2029     } while (d < 3);
2030     c[D_MD2][0] = count / 10;
2031     c[D_MDC2][0] = count / 10;
2032     c[D_MD4][0] = count;
2033     c[D_MD5][0] = count;
2034     c[D_HMAC][0] = count;
2035     c[D_SHA1][0] = count;
2036     c[D_RMD160][0] = count;
2037     c[D_RC4][0] = count * 5;
2038     c[D_CBC_DES][0] = count;
2039     c[D_EDE3_DES][0] = count / 3;
2040     c[D_CBC_IDEA][0] = count;
2041     c[D_CBC_SEED][0] = count;
2042     c[D_CBC_RC2][0] = count;
2043     c[D_CBC_RC5][0] = count;
2044     c[D_CBC_BF][0] = count;
2045     c[D_CBC_CAST][0] = count;
2046     c[D_CBC_128_AES][0] = count;
2047     c[D_CBC_192_AES][0] = count;
2048     c[D_CBC_256_AES][0] = count;
2049     c[D_CBC_128_CML][0] = count;
2050     c[D_CBC_192_CML][0] = count;
2051     c[D_CBC_256_CML][0] = count;
2052     c[D_EVP][0] = count;
2053     c[D_SHA256][0] = count;
2054     c[D_SHA512][0] = count;
2055     c[D_WHIRLPOOL][0] = count;
2056     c[D_IGE_128_AES][0] = count;
2057     c[D_IGE_192_AES][0] = count;
2058     c[D_IGE_256_AES][0] = count;
2059     c[D_GHASH][0] = count;
2060     c[D_RAND][0] = count;
2061     c[D_EVP_HMAC][0] = count;
2062     c[D_EVP_CMAC][0] = count;
2063
2064     for (i = 1; i < size_num; i++) {
2065         long l0 = (long)lengths[0];
2066         long l1 = (long)lengths[i];
2067
2068         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
2069         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
2070         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
2071         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
2072         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
2073         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
2074         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
2075         c[D_EVP][i] = = c[D_EVP][0] * 4 * l0 / l1;
2076         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
2077         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
2078         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
2079         c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
2080         c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
2081         c[D_EVP_HMAC][i] = = c[D_EVP_HMAC][0] * 4 * l0 / l1;
2082         c[D_EVP_CMAC][i] = = c[D_EVP_CMAC][0] * 4 * l0 / l1;
2083
2084         l0 = (long)lengths[i - 1];
2085
2086         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
2087         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
2088         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
2089         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
2090         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
2091         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
2092         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
2093         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
2094         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
2095         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
2096         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
2097         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
2098         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
2099         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
2100         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
2101         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
2102         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
2103         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
2104     }
2105
2106 #  ifndef OPENSSL_NO_RSA
2107     rsa_c[R_RSA_512][0] = count / 2000;
2108     rsa_c[R_RSA_512][1] = count / 400;
2109     for (i = 1; i < RSA_NUM; i++) {
2110         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
2111         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
2112         if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
2113             rsa_doit[i] = 0;
2114         else {
2115             if (rsa_c[i][0] == 0) {
2116                 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2117                 rsa_c[i][1] = 20;
2118             }
2119         }
2120     }
2121 #  endif
2122
2123 #  ifndef OPENSSL_NO_DSA
2124     dsa_c[R_DSA_512][0] = count / 1000;
2125     dsa_c[R_DSA_512][1] = count / 1000 / 2;
2126     for (i = 1; i < DSA_NUM; i++) {
2127         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
2128         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
2129         if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
2130             dsa_doit[i] = 0;
2131         else {
2132             if (dsa_c[i][0] == 0) {
2133                 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2134                 dsa_c[i][1] = 1;
2135             }
2136         }
2137     }
2138 #  endif
2139
2140 #  ifndef OPENSSL_NO_EC
2141     ecdsa_c[R_EC_P160][0] = count / 1000;
2142     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
2143     for (i = R_EC_P192; i <= R_EC_P521; i++) {
2144         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2145         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2146         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2147             ecdsa_doit[i] = 0;
2148         else {
2149             if (ecdsa_c[i][0] == 0) {
2150                 ecdsa_c[i][0] = 1;
2151                 ecdsa_c[i][1] = 1;
2152             }
2153         }
2154     }
2155 #   ifndef OPENSSL_NO_EC2M
2156     ecdsa_c[R_EC_K163][0] = count / 1000;
2157     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
2158     for (i = R_EC_K233; i <= R_EC_K571; i++) {
2159         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2160         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2161         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2162             ecdsa_doit[i] = 0;
2163         else {
2164             if (ecdsa_c[i][0] == 0) {
2165                 ecdsa_c[i][0] = 1;
2166                 ecdsa_c[i][1] = 1;
2167             }
2168         }
2169     }
2170     ecdsa_c[R_EC_B163][0] = count / 1000;
2171     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
2172     for (i = R_EC_B233; i <= R_EC_B571; i++) {
2173         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2174         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2175         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2176             ecdsa_doit[i] = 0;
2177         else {
2178             if (ecdsa_c[i][0] == 0) {
2179                 ecdsa_c[i][0] = 1;
2180                 ecdsa_c[i][1] = 1;
2181             }
2182         }
2183     }
2184 #   endif
2185
2186     ecdh_c[R_EC_P160][0] = count / 1000;
2187     for (i = R_EC_P192; i <= R_EC_P521; i++) {
2188         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2189         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2190             ecdh_doit[i] = 0;
2191         else {
2192             if (ecdh_c[i][0] == 0) {
2193                 ecdh_c[i][0] = 1;
2194             }
2195         }
2196     }
2197 #   ifndef OPENSSL_NO_EC2M
2198     ecdh_c[R_EC_K163][0] = count / 1000;
2199     for (i = R_EC_K233; i <= R_EC_K571; i++) {
2200         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2201         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2202             ecdh_doit[i] = 0;
2203         else {
2204             if (ecdh_c[i][0] == 0) {
2205                 ecdh_c[i][0] = 1;
2206             }
2207         }
2208     }
2209     ecdh_c[R_EC_B163][0] = count / 1000;
2210     for (i = R_EC_B233; i <= R_EC_B571; i++) {
2211         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2212         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2213             ecdh_doit[i] = 0;
2214         else {
2215             if (ecdh_c[i][0] == 0) {
2216                 ecdh_c[i][0] = 1;
2217             }
2218         }
2219     }
2220 #   endif
2221     /* repeated code good to factorize */
2222     ecdh_c[R_EC_BRP256R1][0] = count / 1000;
2223     for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
2224         ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2225         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2226             ecdh_doit[i] = 0;
2227         else {
2228             if (ecdh_c[i][0] == 0) {
2229                 ecdh_c[i][0] = 1;
2230             }
2231         }
2232     }
2233     ecdh_c[R_EC_BRP256T1][0] = count / 1000;
2234     for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
2235         ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2236         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2237             ecdh_doit[i] = 0;
2238         else {
2239             if (ecdh_c[i][0] == 0) {
2240                 ecdh_c[i][0] = 1;
2241             }
2242         }
2243     }
2244     /* default iteration count for the last two EC Curves */
2245     ecdh_c[R_EC_X25519][0] = count / 1800;
2246     ecdh_c[R_EC_X448][0] = count / 7200;
2247
2248     eddsa_c[R_EC_Ed25519][0] = count / 1800;
2249     eddsa_c[R_EC_Ed448][0] = count / 7200;
2250
2251 #   ifndef OPENSSL_NO_SM2
2252     sm2_c[R_EC_SM2P256][0] = count / 1800;
2253 #   endif
2254 #  endif                          /* OPENSSL_NO_EC */
2255
2256 # else
2257 /* not worth fixing */
2258 #  error "You cannot disable DES on systems without SIGALRM."
2259 # endif                         /* OPENSSL_NO_DES */
2260 #elif SIGALRM > 0
2261     signal(SIGALRM, alarmed);
2262 #endif                          /* SIGALRM */
2263
2264 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2265     if (doit[D_MD2]) {
2266         for (testnum = 0; testnum < size_num; testnum++) {
2267             print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
2268                           seconds.sym);
2269             Time_F(START);
2270             count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
2271             d = Time_F(STOP);
2272             print_result(D_MD2, testnum, count, d);
2273         }
2274     }
2275 #endif
2276 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2277     if (doit[D_MDC2]) {
2278         for (testnum = 0; testnum < size_num; testnum++) {
2279             print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
2280                           seconds.sym);
2281             Time_F(START);
2282             count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
2283             d = Time_F(STOP);
2284             print_result(D_MDC2, testnum, count, d);
2285             if (count < 0)
2286                 break;
2287         }
2288     }
2289 #endif
2290
2291 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2292     if (doit[D_MD4]) {
2293         for (testnum = 0; testnum < size_num; testnum++) {
2294             print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
2295                           seconds.sym);
2296             Time_F(START);
2297             count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
2298             d = Time_F(STOP);
2299             print_result(D_MD4, testnum, count, d);
2300             if (count < 0)
2301                 break;
2302         }
2303     }
2304 #endif
2305
2306 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2307     if (doit[D_MD5]) {
2308         for (testnum = 0; testnum < size_num; testnum++) {
2309             print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
2310                           seconds.sym);
2311             Time_F(START);
2312             count = run_benchmark(async_jobs, MD5_loop, loopargs);
2313             d = Time_F(STOP);
2314             print_result(D_MD5, testnum, count, d);
2315         }
2316     }
2317
2318 # ifndef OPENSSL_NO_DEPRECATED_3_0
2319     if (doit[D_HMAC]) {
2320         static const char hmac_key[] = "This is a key...";
2321         int len = strlen(hmac_key);
2322
2323         for (i = 0; i < loopargs_len; i++) {
2324             loopargs[i].hctx = HMAC_CTX_new();
2325             if (loopargs[i].hctx == NULL) {
2326                 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2327                 exit(1);
2328             }
2329
2330             HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2331         }
2332         for (testnum = 0; testnum < size_num; testnum++) {
2333             print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
2334                           seconds.sym);
2335             Time_F(START);
2336             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2337             d = Time_F(STOP);
2338             print_result(D_HMAC, testnum, count, d);
2339         }
2340         for (i = 0; i < loopargs_len; i++)
2341             HMAC_CTX_free(loopargs[i].hctx);
2342     }
2343 # endif
2344 #endif
2345 #ifndef OPENSSL_NO_DEPRECATED_3_0
2346     if (doit[D_SHA1]) {
2347         for (testnum = 0; testnum < size_num; testnum++) {
2348             print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2349                           seconds.sym);
2350             Time_F(START);
2351             count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2352             d = Time_F(STOP);
2353             print_result(D_SHA1, testnum, count, d);
2354         }
2355     }
2356     if (doit[D_SHA256]) {
2357         for (testnum = 0; testnum < size_num; testnum++) {
2358             print_message(names[D_SHA256], c[D_SHA256][testnum],
2359                           lengths[testnum], seconds.sym);
2360             Time_F(START);
2361             count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2362             d = Time_F(STOP);
2363             print_result(D_SHA256, testnum, count, d);
2364         }
2365     }
2366     if (doit[D_SHA512]) {
2367         for (testnum = 0; testnum < size_num; testnum++) {
2368             print_message(names[D_SHA512], c[D_SHA512][testnum],
2369                           lengths[testnum], seconds.sym);
2370             Time_F(START);
2371             count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2372             d = Time_F(STOP);
2373             print_result(D_SHA512, testnum, count, d);
2374         }
2375     }
2376 #endif
2377 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2378     if (doit[D_WHIRLPOOL]) {
2379         for (testnum = 0; testnum < size_num; testnum++) {
2380             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2381                           lengths[testnum], seconds.sym);
2382             Time_F(START);
2383             count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2384             d = Time_F(STOP);
2385             print_result(D_WHIRLPOOL, testnum, count, d);
2386         }
2387     }
2388 #endif
2389
2390 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2391     if (doit[D_RMD160]) {
2392         for (testnum = 0; testnum < size_num; testnum++) {
2393             print_message(names[D_RMD160], c[D_RMD160][testnum],
2394                           lengths[testnum], seconds.sym);
2395             Time_F(START);
2396             count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2397             d = Time_F(STOP);
2398             print_result(D_RMD160, testnum, count, d);
2399             if (count < 0)
2400                 break;
2401         }
2402     }
2403 #endif
2404 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2405     if (doit[D_RC4]) {
2406         for (testnum = 0; testnum < size_num; testnum++) {
2407             print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2408                           seconds.sym);
2409             Time_F(START);
2410             count = run_benchmark(async_jobs, RC4_loop, loopargs);
2411             d = Time_F(STOP);
2412             print_result(D_RC4, testnum, count, d);
2413         }
2414     }
2415 #endif
2416 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2417     if (doit[D_CBC_DES]) {
2418         for (testnum = 0; testnum < size_num; testnum++) {
2419             print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2420                           lengths[testnum], seconds.sym);
2421             Time_F(START);
2422             count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2423             d = Time_F(STOP);
2424             print_result(D_CBC_DES, testnum, count, d);
2425         }
2426     }
2427
2428     if (doit[D_EDE3_DES]) {
2429         for (testnum = 0; testnum < size_num; testnum++) {
2430             print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2431                           lengths[testnum], seconds.sym);
2432             Time_F(START);
2433             count =
2434                 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2435             d = Time_F(STOP);
2436             print_result(D_EDE3_DES, testnum, count, d);
2437         }
2438     }
2439 #endif
2440
2441 #ifndef OPENSSL_NO_DEPRECATED_3_0
2442     if (doit[D_CBC_128_AES]) {
2443         for (testnum = 0; testnum < size_num; testnum++) {
2444             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2445                           lengths[testnum], seconds.sym);
2446             Time_F(START);
2447             count =
2448                 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2449             d = Time_F(STOP);
2450             print_result(D_CBC_128_AES, testnum, count, d);
2451         }
2452     }
2453     if (doit[D_CBC_192_AES]) {
2454         for (testnum = 0; testnum < size_num; testnum++) {
2455             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2456                           lengths[testnum], seconds.sym);
2457             Time_F(START);
2458             count =
2459                 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2460             d = Time_F(STOP);
2461             print_result(D_CBC_192_AES, testnum, count, d);
2462         }
2463     }
2464     if (doit[D_CBC_256_AES]) {
2465         for (testnum = 0; testnum < size_num; testnum++) {
2466             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2467                           lengths[testnum], seconds.sym);
2468             Time_F(START);
2469             count =
2470                 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2471             d = Time_F(STOP);
2472             print_result(D_CBC_256_AES, testnum, count, d);
2473         }
2474     }
2475
2476
2477     if (doit[D_IGE_128_AES]) {
2478         for (testnum = 0; testnum < size_num; testnum++) {
2479             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2480                           lengths[testnum], seconds.sym);
2481             Time_F(START);
2482             count =
2483                 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2484             d = Time_F(STOP);
2485             print_result(D_IGE_128_AES, testnum, count, d);
2486         }
2487     }
2488     if (doit[D_IGE_192_AES]) {
2489         for (testnum = 0; testnum < size_num; testnum++) {
2490             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2491                           lengths[testnum], seconds.sym);
2492             Time_F(START);
2493             count =
2494                 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2495             d = Time_F(STOP);
2496             print_result(D_IGE_192_AES, testnum, count, d);
2497         }
2498     }
2499     if (doit[D_IGE_256_AES]) {
2500         for (testnum = 0; testnum < size_num; testnum++) {
2501             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2502                           lengths[testnum], seconds.sym);
2503             Time_F(START);
2504             count =
2505                 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2506             d = Time_F(STOP);
2507             print_result(D_IGE_256_AES, testnum, count, d);
2508         }
2509     }
2510     if (doit[D_GHASH]) {
2511         for (i = 0; i < loopargs_len; i++) {
2512             loopargs[i].gcm_ctx =
2513                 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2514             CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2515                                 (unsigned char *)"0123456789ab", 12);
2516         }
2517
2518         for (testnum = 0; testnum < size_num; testnum++) {
2519             print_message(names[D_GHASH], c[D_GHASH][testnum],
2520                           lengths[testnum], seconds.sym);
2521             Time_F(START);
2522             count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2523             d = Time_F(STOP);
2524             print_result(D_GHASH, testnum, count, d);
2525         }
2526         for (i = 0; i < loopargs_len; i++)
2527             CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2528     }
2529 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
2530 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2531     if (doit[D_CBC_128_CML]) {
2532         if (async_jobs > 0) {
2533             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2534                        names[D_CBC_128_CML]);
2535             doit[D_CBC_128_CML] = 0;
2536         }
2537         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2538             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2539                           lengths[testnum], seconds.sym);
2540             Time_F(START);
2541             for (count = 0; COND(c[D_CBC_128_CML][testnum]); count++)
2542                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2543                                      (size_t)lengths[testnum], &camellia_ks[0],
2544                                      iv, CAMELLIA_ENCRYPT);
2545             d = Time_F(STOP);
2546             print_result(D_CBC_128_CML, testnum, count, d);
2547         }
2548     }
2549     if (doit[D_CBC_192_CML]) {
2550         if (async_jobs > 0) {
2551             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2552                        names[D_CBC_192_CML]);
2553             doit[D_CBC_192_CML] = 0;
2554         }
2555         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2556             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2557                           lengths[testnum], seconds.sym);
2558             if (async_jobs > 0) {
2559                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2560                 exit(1);
2561             }
2562             Time_F(START);
2563             for (count = 0; COND(c[D_CBC_192_CML][testnum]); count++)
2564                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2565                                      (size_t)lengths[testnum], &camellia_ks[1],
2566                                      iv, CAMELLIA_ENCRYPT);
2567             d = Time_F(STOP);
2568             print_result(D_CBC_192_CML, testnum, count, d);
2569         }
2570     }
2571     if (doit[D_CBC_256_CML]) {
2572         if (async_jobs > 0) {
2573             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2574                        names[D_CBC_256_CML]);
2575             doit[D_CBC_256_CML] = 0;
2576         }
2577         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2578             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2579                           lengths[testnum], seconds.sym);
2580             Time_F(START);
2581             for (count = 0; COND(c[D_CBC_256_CML][testnum]); count++)
2582                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2583                                      (size_t)lengths[testnum], &camellia_ks[2],
2584                                      iv, CAMELLIA_ENCRYPT);
2585             d = Time_F(STOP);
2586             print_result(D_CBC_256_CML, testnum, count, d);
2587         }
2588     }
2589 #endif
2590 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2591     if (doit[D_CBC_IDEA]) {
2592         if (async_jobs > 0) {
2593             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2594                        names[D_CBC_IDEA]);
2595             doit[D_CBC_IDEA] = 0;
2596         }
2597         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2598             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2599                           lengths[testnum], seconds.sym);
2600             Time_F(START);
2601             for (count = 0; COND(c[D_CBC_IDEA][testnum]); count++)
2602                 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2603                                  (size_t)lengths[testnum], &idea_ks,
2604                                  iv, IDEA_ENCRYPT);
2605             d = Time_F(STOP);
2606             print_result(D_CBC_IDEA, testnum, count, d);
2607         }
2608     }
2609 #endif
2610 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2611     if (doit[D_CBC_SEED]) {
2612         if (async_jobs > 0) {
2613             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2614                        names[D_CBC_SEED]);
2615             doit[D_CBC_SEED] = 0;
2616         }
2617         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2618             print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2619                           lengths[testnum], seconds.sym);
2620             Time_F(START);
2621             for (count = 0; COND(c[D_CBC_SEED][testnum]); count++)
2622                 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2623                                  (size_t)lengths[testnum], &seed_ks, iv, 1);
2624             d = Time_F(STOP);
2625             print_result(D_CBC_SEED, testnum, count, d);
2626         }
2627     }
2628 #endif
2629 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2630     if (doit[D_CBC_RC2]) {
2631         if (async_jobs > 0) {
2632             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2633                        names[D_CBC_RC2]);
2634             doit[D_CBC_RC2] = 0;
2635         }
2636         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2637             print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2638                           lengths[testnum], seconds.sym);
2639             if (async_jobs > 0) {
2640                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2641                 exit(1);
2642             }
2643             Time_F(START);
2644             for (count = 0; COND(c[D_CBC_RC2][testnum]); count++)
2645                 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2646                                 (size_t)lengths[testnum], &rc2_ks,
2647                                 iv, RC2_ENCRYPT);
2648             d = Time_F(STOP);
2649             print_result(D_CBC_RC2, testnum, count, d);
2650         }
2651     }
2652 #endif
2653 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2654     if (doit[D_CBC_RC5]) {
2655         if (async_jobs > 0) {
2656             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2657                        names[D_CBC_RC5]);
2658             doit[D_CBC_RC5] = 0;
2659         }
2660         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2661             print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2662                           lengths[testnum], seconds.sym);
2663             if (async_jobs > 0) {
2664                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2665                 exit(1);
2666             }
2667             Time_F(START);
2668             for (count = 0; COND(c[D_CBC_RC5][testnum]); count++)
2669                 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2670                                    (size_t)lengths[testnum], &rc5_ks,
2671                                    iv, RC5_ENCRYPT);
2672             d = Time_F(STOP);
2673             print_result(D_CBC_RC5, testnum, count, d);
2674         }
2675     }
2676 #endif
2677 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2678     if (doit[D_CBC_BF]) {
2679         if (async_jobs > 0) {
2680             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2681                        names[D_CBC_BF]);
2682             doit[D_CBC_BF] = 0;
2683         }
2684         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2685             print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2686                           lengths[testnum], seconds.sym);
2687             Time_F(START);
2688             for (count = 0; COND(c[D_CBC_BF][testnum]); count++)
2689                 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2690                                (size_t)lengths[testnum], &bf_ks,
2691                                iv, BF_ENCRYPT);
2692             d = Time_F(STOP);
2693             print_result(D_CBC_BF, testnum, count, d);
2694         }
2695     }
2696 #endif
2697 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2698     if (doit[D_CBC_CAST]) {
2699         if (async_jobs > 0) {
2700             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2701                        names[D_CBC_CAST]);
2702             doit[D_CBC_CAST] = 0;
2703         }
2704         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2705             print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2706                           lengths[testnum], seconds.sym);
2707             Time_F(START);
2708             for (count = 0; COND(c[D_CBC_CAST][testnum]); count++)
2709                 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2710                                  (size_t)lengths[testnum], &cast_ks,
2711                                  iv, CAST_ENCRYPT);
2712             d = Time_F(STOP);
2713             print_result(D_CBC_CAST, testnum, count, d);
2714         }
2715     }
2716 #endif
2717     if (doit[D_RAND]) {
2718         for (testnum = 0; testnum < size_num; testnum++) {
2719             print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2720                           seconds.sym);
2721             Time_F(START);
2722             count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2723             d = Time_F(STOP);
2724             print_result(D_RAND, testnum, count, d);
2725         }
2726     }
2727
2728     if (doit[D_EVP]) {
2729         if (evp_cipher != NULL) {
2730             int (*loopfunc) (void *) = EVP_Update_loop;
2731
2732             if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
2733                                EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2734                 multiblock_speed(evp_cipher, lengths_single, &seconds);
2735                 ret = 0;
2736                 goto end;
2737             }
2738
2739             names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2740
2741             if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
2742                 loopfunc = EVP_Update_loop_ccm;
2743             } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
2744                                 EVP_CIPH_FLAG_AEAD_CIPHER)) {
2745                 loopfunc = EVP_Update_loop_aead;
2746                 if (lengths == lengths_list) {
2747                     lengths = aead_lengths_list;
2748                     size_num = OSSL_NELEM(aead_lengths_list);
2749                 }
2750             }
2751
2752             for (testnum = 0; testnum < size_num; testnum++) {
2753                 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2754                               seconds.sym);
2755
2756                 for (k = 0; k < loopargs_len; k++) {
2757                     loopargs[k].ctx = EVP_CIPHER_CTX_new();
2758                     if (loopargs[k].ctx == NULL) {
2759                         BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
2760                         exit(1);
2761                     }
2762                     if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2763                                            NULL, iv, decrypt ? 0 : 1)) {
2764                         BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2765                         ERR_print_errors(bio_err);
2766                         exit(1);
2767                     }
2768
2769                     EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2770
2771                     keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
2772                     loopargs[k].key = app_malloc(keylen, "evp_cipher key");
2773                     EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
2774                     if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
2775                                            loopargs[k].key, NULL, -1)) {
2776                         BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2777                         ERR_print_errors(bio_err);
2778                         exit(1);
2779                     }
2780                     OPENSSL_clear_free(loopargs[k].key, keylen);
2781
2782                     /* SIV mode only allows for a single Update operation */
2783                     if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
2784                         EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_SET_SPEED, 1, NULL);
2785                 }
2786
2787                 Time_F(START);
2788                 count = run_benchmark(async_jobs, loopfunc, loopargs);
2789                 d = Time_F(STOP);
2790                 for (k = 0; k < loopargs_len; k++) {
2791                     EVP_CIPHER_CTX_free(loopargs[k].ctx);
2792                 }
2793                 print_result(D_EVP, testnum, count, d);
2794             }
2795         } else if (evp_md != NULL) {
2796             names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2797
2798             for (testnum = 0; testnum < size_num; testnum++) {
2799                 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2800                               seconds.sym);
2801                 Time_F(START);
2802                 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2803                 d = Time_F(STOP);
2804                 print_result(D_EVP, testnum, count, d);
2805             }
2806         }
2807     }
2808
2809 #ifndef OPENSSL_NO_DEPRECATED_3_0
2810     if (doit[D_EVP_HMAC] && evp_hmac_md != NULL) {
2811         const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
2812
2813         evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
2814                                    "HMAC name");
2815         sprintf(evp_hmac_name, "HMAC(%s)", md_name);
2816         names[D_EVP_HMAC] = evp_hmac_name;
2817
2818         for (testnum = 0; testnum < size_num; testnum++) {
2819             print_message(names[D_EVP_HMAC], c[D_EVP_HMAC][testnum], lengths[testnum],
2820                           seconds.sym);
2821             Time_F(START);
2822             count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
2823             d = Time_F(STOP);
2824             print_result(D_EVP_HMAC, testnum, count, d);
2825         }
2826     }
2827 #endif
2828
2829 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2830     if (doit[D_EVP_CMAC] && evp_cmac_cipher != NULL) {
2831         const char *cipher_name = OBJ_nid2ln(EVP_CIPHER_type(evp_cmac_cipher));
2832
2833         evp_cmac_name = app_malloc(sizeof("CMAC()") + strlen(cipher_name),
2834                                    "CMAC name");
2835         sprintf(evp_cmac_name, "CMAC(%s)", cipher_name);
2836         names[D_EVP_CMAC] = evp_cmac_name;
2837
2838         for (i = 0; i < loopargs_len; i++) {
2839             loopargs[i].cmac_ctx = CMAC_CTX_new();
2840             if (loopargs[i].cmac_ctx == NULL) {
2841                 BIO_printf(bio_err, "CMAC malloc failure, exiting...");
2842                 exit(1);
2843             }
2844         }
2845         for (testnum = 0; testnum < size_num; testnum++) {
2846             print_message(names[D_EVP_CMAC], c[D_EVP_CMAC][testnum], lengths[testnum],
2847                           seconds.sym);
2848             Time_F(START);
2849             count = run_benchmark(async_jobs, EVP_CMAC_loop, loopargs);
2850             d = Time_F(STOP);
2851             print_result(D_EVP_CMAC, testnum, count, d);
2852         }
2853         for (i = 0; i < loopargs_len; i++)
2854             CMAC_CTX_free(loopargs[i].cmac_ctx);
2855     }
2856 #endif
2857
2858     for (i = 0; i < loopargs_len; i++)
2859         if (RAND_bytes(loopargs[i].buf, 36) <= 0)
2860             goto end;
2861
2862 #ifndef OPENSSL_NO_RSA
2863     for (testnum = 0; testnum < RSA_NUM; testnum++) {
2864         int st = 0;
2865         if (!rsa_doit[testnum])
2866             continue;
2867         for (i = 0; i < loopargs_len; i++) {
2868             if (primes > RSA_DEFAULT_PRIME_NUM) {
2869                 /* we haven't set keys yet,  generate multi-prime RSA keys */
2870                 BIGNUM *bn = BN_new();
2871
2872                 if (bn == NULL)
2873                     goto end;
2874                 if (!BN_set_word(bn, RSA_F4)) {
2875                     BN_free(bn);
2876                     goto end;
2877                 }
2878
2879                 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
2880                            rsa_choices[testnum].name);
2881
2882                 loopargs[i].rsa_key[testnum] = RSA_new();
2883                 if (loopargs[i].rsa_key[testnum] == NULL) {
2884                     BN_free(bn);
2885                     goto end;
2886                 }
2887
2888                 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
2889                                                   rsa_keys[testnum].bits,
2890                                                   primes, bn, NULL)) {
2891                     BN_free(bn);
2892                     goto end;
2893                 }
2894                 BN_free(bn);
2895             }
2896             st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2897                           &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2898             if (st == 0)
2899                 break;
2900         }
2901         if (st == 0) {
2902             BIO_printf(bio_err,
2903                        "RSA sign failure.  No RSA sign will be done.\n");
2904             ERR_print_errors(bio_err);
2905             rsa_count = 1;
2906         } else {
2907             pkey_print_message("private", "rsa",
2908                                rsa_c[testnum][0], rsa_keys[testnum].bits,
2909                                seconds.rsa);
2910             /* RSA_blinding_on(rsa_key[testnum],NULL); */
2911             Time_F(START);
2912             count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2913             d = Time_F(STOP);
2914             BIO_printf(bio_err,
2915                        mr ? "+R1:%ld:%d:%.2f\n"
2916                        : "%ld %u bits private RSA's in %.2fs\n",
2917                        count, rsa_keys[testnum].bits, d);
2918             rsa_results[testnum][0] = (double)count / d;
2919             rsa_count = count;
2920         }
2921
2922         for (i = 0; i < loopargs_len; i++) {
2923             st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2924                             loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2925             if (st <= 0)
2926                 break;
2927         }
2928         if (st <= 0) {
2929             BIO_printf(bio_err,
2930                        "RSA verify failure.  No RSA verify will be done.\n");
2931             ERR_print_errors(bio_err);
2932             rsa_doit[testnum] = 0;
2933         } else {
2934             pkey_print_message("public", "rsa",
2935                                rsa_c[testnum][1], rsa_keys[testnum].bits,
2936                                seconds.rsa);
2937             Time_F(START);
2938             count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2939             d = Time_F(STOP);
2940             BIO_printf(bio_err,
2941                        mr ? "+R2:%ld:%d:%.2f\n"
2942                        : "%ld %u bits public RSA's in %.2fs\n",
2943                        count, rsa_keys[testnum].bits, d);
2944             rsa_results[testnum][1] = (double)count / d;
2945         }
2946
2947         if (rsa_count <= 1) {
2948             /* if longer than 10s, don't do any more */
2949             stop_it(rsa_doit, testnum);
2950         }
2951     }
2952 #endif                          /* OPENSSL_NO_RSA */
2953
2954     for (i = 0; i < loopargs_len; i++)
2955         if (RAND_bytes(loopargs[i].buf, 36) <= 0)
2956             goto end;
2957
2958 #ifndef OPENSSL_NO_DSA
2959     for (testnum = 0; testnum < DSA_NUM; testnum++) {
2960         int st = 0;
2961         if (!dsa_doit[testnum])
2962             continue;
2963
2964         /* DSA_generate_key(dsa_key[testnum]); */
2965         /* DSA_sign_setup(dsa_key[testnum],NULL); */
2966         for (i = 0; i < loopargs_len; i++) {
2967             st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2968                           &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2969             if (st == 0)
2970                 break;
2971         }
2972         if (st == 0) {
2973             BIO_printf(bio_err,
2974                        "DSA sign failure.  No DSA sign will be done.\n");
2975             ERR_print_errors(bio_err);
2976             rsa_count = 1;
2977         } else {
2978             pkey_print_message("sign", "dsa",
2979                                dsa_c[testnum][0], dsa_bits[testnum],
2980                                seconds.dsa);
2981             Time_F(START);
2982             count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2983             d = Time_F(STOP);
2984             BIO_printf(bio_err,
2985                        mr ? "+R3:%ld:%u:%.2f\n"
2986                        : "%ld %u bits DSA signs in %.2fs\n",
2987                        count, dsa_bits[testnum], d);
2988             dsa_results[testnum][0] = (double)count / d;
2989             rsa_count = count;
2990         }
2991
2992         for (i = 0; i < loopargs_len; i++) {
2993             st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2994                             loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2995             if (st <= 0)
2996                 break;
2997         }
2998         if (st <= 0) {
2999             BIO_printf(bio_err,
3000                        "DSA verify failure.  No DSA verify will be done.\n");
3001             ERR_print_errors(bio_err);
3002             dsa_doit[testnum] = 0;
3003         } else {
3004             pkey_print_message("verify", "dsa",
3005                                dsa_c[testnum][1], dsa_bits[testnum],
3006                                seconds.dsa);
3007             Time_F(START);
3008             count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
3009             d = Time_F(STOP);
3010             BIO_printf(bio_err,
3011                        mr ? "+R4:%ld:%u:%.2f\n"
3012                        : "%ld %u bits DSA verify in %.2fs\n",
3013                        count, dsa_bits[testnum], d);
3014             dsa_results[testnum][1] = (double)count / d;
3015         }
3016
3017         if (rsa_count <= 1) {
3018             /* if longer than 10s, don't do any more */
3019             stop_it(dsa_doit, testnum);
3020         }
3021     }
3022 #endif                          /* OPENSSL_NO_DSA */
3023
3024 #ifndef OPENSSL_NO_EC
3025 # ifndef OPENSSL_NO_DEPRECATED_3_0
3026     for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
3027         int st = 1;
3028
3029         if (!ecdsa_doit[testnum])
3030             continue;           /* Ignore Curve */
3031         for (i = 0; i < loopargs_len; i++) {
3032             loopargs[i].ecdsa[testnum] =
3033                 EC_KEY_new_by_curve_name(ec_curves[testnum].nid);
3034             if (loopargs[i].ecdsa[testnum] == NULL) {
3035                 st = 0;
3036                 break;
3037             }
3038         }
3039         if (st == 0) {
3040             BIO_printf(bio_err, "ECDSA failure.\n");
3041             ERR_print_errors(bio_err);
3042             rsa_count = 1;
3043         } else {
3044             for (i = 0; i < loopargs_len; i++) {
3045                 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
3046                 /* Perform ECDSA signature test */
3047                 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
3048                 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3049                                 &loopargs[i].siglen,
3050                                 loopargs[i].ecdsa[testnum]);
3051                 if (st == 0)
3052                     break;
3053             }
3054             if (st == 0) {
3055                 BIO_printf(bio_err,
3056                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
3057                 ERR_print_errors(bio_err);
3058                 rsa_count = 1;
3059             } else {
3060                 pkey_print_message("sign", "ecdsa",
3061                                    ecdsa_c[testnum][0],
3062                                    ec_curves[testnum].bits, seconds.ecdsa);
3063                 Time_F(START);
3064                 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
3065                 d = Time_F(STOP);
3066
3067                 BIO_printf(bio_err,
3068                            mr ? "+R5:%ld:%u:%.2f\n" :
3069                            "%ld %u bits ECDSA signs in %.2fs \n",
3070                            count, ec_curves[testnum].bits, d);
3071                 ecdsa_results[testnum][0] = (double)count / d;
3072                 rsa_count = count;
3073             }
3074
3075             /* Perform ECDSA verification test */
3076             for (i = 0; i < loopargs_len; i++) {
3077                 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3078                                   loopargs[i].siglen,
3079                                   loopargs[i].ecdsa[testnum]);
3080                 if (st != 1)
3081                     break;
3082             }
3083             if (st != 1) {
3084                 BIO_printf(bio_err,
3085                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
3086                 ERR_print_errors(bio_err);
3087                 ecdsa_doit[testnum] = 0;
3088             } else {
3089                 pkey_print_message("verify", "ecdsa",
3090                                    ecdsa_c[testnum][1],
3091                                    ec_curves[testnum].bits, seconds.ecdsa);
3092                 Time_F(START);
3093                 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
3094                 d = Time_F(STOP);
3095                 BIO_printf(bio_err,
3096                            mr ? "+R6:%ld:%u:%.2f\n"
3097                            : "%ld %u bits ECDSA verify in %.2fs\n",
3098                            count, ec_curves[testnum].bits, d);
3099                 ecdsa_results[testnum][1] = (double)count / d;
3100             }
3101
3102             if (rsa_count <= 1) {
3103                 /* if longer than 10s, don't do any more */
3104                 stop_it(ecdsa_doit, testnum);
3105             }
3106         }
3107     }
3108 # endif
3109
3110     for (testnum = 0; testnum < EC_NUM; testnum++) {
3111         int ecdh_checks = 1;
3112
3113         if (!ecdh_doit[testnum])
3114             continue;
3115
3116         for (i = 0; i < loopargs_len; i++) {
3117             EVP_PKEY_CTX *kctx = NULL;
3118             EVP_PKEY_CTX *test_ctx = NULL;
3119             EVP_PKEY_CTX *ctx = NULL;
3120             EVP_PKEY *key_A = NULL;
3121             EVP_PKEY *key_B = NULL;
3122             size_t outlen;
3123             size_t test_outlen;
3124
3125             /* Ensure that the error queue is empty */
3126             if (ERR_peek_error()) {
3127                 BIO_printf(bio_err,
3128                            "WARNING: the error queue contains previous unhandled errors.\n");
3129                 ERR_print_errors(bio_err);
3130             }
3131
3132             /* Let's try to create a ctx directly from the NID: this works for
3133              * curves like Curve25519 that are not implemented through the low
3134              * level EC interface.
3135              * If this fails we try creating a EVP_PKEY_EC generic param ctx,
3136              * then we set the curve by NID before deriving the actual keygen
3137              * ctx for that specific curve. */
3138             kctx = EVP_PKEY_CTX_new_id(ec_curves[testnum].nid, NULL); /* keygen ctx from NID */
3139             if (!kctx) {
3140                 EVP_PKEY_CTX *pctx = NULL;
3141                 EVP_PKEY *params = NULL;
3142
3143                 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
3144                  * "int_ctx_new:unsupported algorithm" error was added to the
3145                  * error queue.
3146                  * We remove it from the error queue as we are handling it. */
3147                 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
3148                 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
3149                     /* check that the error origin matches */
3150                     ERR_GET_LIB(error) == ERR_LIB_EVP &&
3151                     ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
3152                     ERR_get_error(); /* pop error from queue */
3153                 if (ERR_peek_error()) {
3154                     BIO_printf(bio_err,
3155                                "Unhandled error in the error queue during ECDH init.\n");
3156                     ERR_print_errors(bio_err);
3157                     rsa_count = 1;
3158                     break;
3159                 }
3160
3161                 /* Create the context for parameter generation */
3162                 if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
3163                        /* Initialise the parameter generation */
3164                        !EVP_PKEY_paramgen_init(pctx) ||
3165                        /* Set the curve by NID */
3166                        !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3167                                                                ec_curves
3168                                                                [testnum].nid) ||
3169                        /* Create the parameter object params */
3170                        !EVP_PKEY_paramgen(pctx, &params)) {
3171                     ecdh_checks = 0;
3172                     BIO_printf(bio_err, "ECDH EC params init failure.\n");
3173                     ERR_print_errors(bio_err);
3174                     rsa_count = 1;
3175                     break;
3176                 }
3177                 /* Create the context for the key generation */
3178                 kctx = EVP_PKEY_CTX_new(params, NULL);
3179
3180                 EVP_PKEY_free(params);
3181                 params = NULL;
3182                 EVP_PKEY_CTX_free(pctx);
3183                 pctx = NULL;
3184             }
3185             if (kctx == NULL ||      /* keygen ctx is not null */
3186                 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
3187                 ecdh_checks = 0;
3188                 BIO_printf(bio_err, "ECDH keygen failure.\n");
3189                 ERR_print_errors(bio_err);
3190                 rsa_count = 1;
3191                 break;
3192             }
3193
3194             if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
3195                 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
3196                 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
3197                 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
3198                 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
3199                 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
3200                 outlen == 0 ||  /* ensure outlen is a valid size */
3201                 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
3202                 ecdh_checks = 0;
3203                 BIO_printf(bio_err, "ECDH key generation failure.\n");
3204                 ERR_print_errors(bio_err);
3205                 rsa_count = 1;
3206                 break;
3207             }
3208
3209             /* Here we perform a test run, comparing the output of a*B and b*A;
3210              * we try this here and assume that further EVP_PKEY_derive calls
3211              * never fail, so we can skip checks in the actually benchmarked
3212              * code, for maximum performance. */
3213             if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
3214                 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
3215                 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
3216                 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
3217                 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
3218                 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
3219                 test_outlen != outlen /* compare output length */ ) {
3220                 ecdh_checks = 0;
3221                 BIO_printf(bio_err, "ECDH computation failure.\n");
3222                 ERR_print_errors(bio_err);
3223                 rsa_count = 1;
3224                 break;
3225             }
3226
3227             /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
3228             if (CRYPTO_memcmp(loopargs[i].secret_a,
3229                               loopargs[i].secret_b, outlen)) {
3230                 ecdh_checks = 0;
3231                 BIO_printf(bio_err, "ECDH computations don't match.\n");
3232                 ERR_print_errors(bio_err);
3233                 rsa_count = 1;
3234                 break;
3235             }
3236
3237             loopargs[i].ecdh_ctx[testnum] = ctx;
3238             loopargs[i].outlen[testnum] = outlen;
3239
3240             EVP_PKEY_free(key_A);
3241             EVP_PKEY_free(key_B);
3242             EVP_PKEY_CTX_free(kctx);
3243             kctx = NULL;
3244             EVP_PKEY_CTX_free(test_ctx);
3245             test_ctx = NULL;
3246         }
3247         if (ecdh_checks != 0) {
3248             pkey_print_message("", "ecdh",
3249                                ecdh_c[testnum][0],
3250                                ec_curves[testnum].bits, seconds.ecdh);
3251             Time_F(START);
3252             count =
3253                 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
3254             d = Time_F(STOP);
3255             BIO_printf(bio_err,
3256                        mr ? "+R7:%ld:%d:%.2f\n" :
3257                        "%ld %u-bits ECDH ops in %.2fs\n", count,
3258                        ec_curves[testnum].bits, d);
3259             ecdh_results[testnum][0] = (double)count / d;
3260             rsa_count = count;
3261         }
3262
3263         if (rsa_count <= 1) {
3264             /* if longer than 10s, don't do any more */
3265             stop_it(ecdh_doit, testnum);
3266         }
3267     }
3268
3269     for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
3270         int st = 1;
3271         EVP_PKEY *ed_pkey = NULL;
3272         EVP_PKEY_CTX *ed_pctx = NULL;
3273
3274         if (!eddsa_doit[testnum])
3275             continue;           /* Ignore Curve */
3276         for (i = 0; i < loopargs_len; i++) {
3277             loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
3278             if (loopargs[i].eddsa_ctx[testnum] == NULL) {
3279                 st = 0;
3280                 break;
3281             }
3282
3283             if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid, NULL))
3284                     == NULL
3285                 || EVP_PKEY_keygen_init(ed_pctx) <= 0
3286                 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
3287                 st = 0;
3288                 EVP_PKEY_CTX_free(ed_pctx);
3289                 break;
3290             }
3291             EVP_PKEY_CTX_free(ed_pctx);
3292
3293             if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
3294                                     NULL, ed_pkey)) {
3295                 st = 0;
3296                 EVP_PKEY_free(ed_pkey);
3297                 break;
3298             }
3299             EVP_PKEY_free(ed_pkey);
3300         }
3301         if (st == 0) {
3302             BIO_printf(bio_err, "EdDSA failure.\n");
3303             ERR_print_errors(bio_err);
3304             rsa_count = 1;
3305         } else {
3306             for (i = 0; i < loopargs_len; i++) {
3307                 /* Perform EdDSA signature test */
3308                 loopargs[i].sigsize = ed_curves[testnum].sigsize;
3309                 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
3310                                     loopargs[i].buf2, &loopargs[i].sigsize,
3311                                     loopargs[i].buf, 20);
3312                 if (st == 0)
3313                     break;
3314             }
3315             if (st == 0) {
3316                 BIO_printf(bio_err,
3317                            "EdDSA sign failure.  No EdDSA sign will be done.\n");
3318                 ERR_print_errors(bio_err);
3319                 rsa_count = 1;
3320             } else {
3321                 pkey_print_message("sign", ed_curves[testnum].name,
3322                                    eddsa_c[testnum][0],
3323                                    ed_curves[testnum].bits, seconds.eddsa);
3324                 Time_F(START);
3325                 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
3326                 d = Time_F(STOP);
3327
3328                 BIO_printf(bio_err,
3329                            mr ? "+R8:%ld:%u:%s:%.2f\n" :
3330                            "%ld %u bits %s signs in %.2fs \n",
3331                            count, ed_curves[testnum].bits,
3332                            ed_curves[testnum].name, d);
3333                 eddsa_results[testnum][0] = (double)count / d;
3334                 rsa_count = count;
3335             }
3336
3337             /* Perform EdDSA verification test */
3338             for (i = 0; i < loopargs_len; i++) {
3339                 st = EVP_DigestVerify(loopargs[i].eddsa_ctx[testnum],
3340                                       loopargs[i].buf2, loopargs[i].sigsize,
3341                                       loopargs[i].buf, 20);
3342                 if (st != 1)
3343                     break;
3344             }
3345             if (st != 1) {
3346                 BIO_printf(bio_err,
3347                            "EdDSA verify failure.  No EdDSA verify will be done.\n");
3348                 ERR_print_errors(bio_err);
3349                 eddsa_doit[testnum] = 0;
3350             } else {
3351                 pkey_print_message("verify", ed_curves[testnum].name,
3352                                    eddsa_c[testnum][1],
3353                                    ed_curves[testnum].bits, seconds.eddsa);
3354                 Time_F(START);
3355                 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
3356                 d = Time_F(STOP);
3357                 BIO_printf(bio_err,
3358                            mr ? "+R9:%ld:%u:%s:%.2f\n"
3359                            : "%ld %u bits %s verify in %.2fs\n",
3360                            count, ed_curves[testnum].bits,
3361                            ed_curves[testnum].name, d);
3362                 eddsa_results[testnum][1] = (double)count / d;
3363             }
3364
3365             if (rsa_count <= 1) {
3366                 /* if longer than 10s, don't do any more */
3367                 stop_it(eddsa_doit, testnum);
3368             }
3369         }
3370     }
3371
3372 # ifndef OPENSSL_NO_SM2
3373     for (testnum = 0; testnum < SM2_NUM; testnum++) {
3374         int st = 1;
3375         EVP_PKEY *sm2_pkey = NULL;
3376
3377         if (!sm2_doit[testnum])
3378             continue;           /* Ignore Curve */
3379         /* Init signing and verification */
3380         for (i = 0; i < loopargs_len; i++) {
3381             EVP_PKEY_CTX *sm2_pctx = NULL;
3382             EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
3383             EVP_PKEY_CTX *pctx = NULL;
3384             st = 0;
3385
3386             loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
3387             loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
3388             if (loopargs[i].sm2_ctx[testnum] == NULL
3389                     || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
3390                 break;
3391
3392             /* SM2 keys are generated as normal EC keys with a special curve */
3393             st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
3394                 || EVP_PKEY_keygen_init(pctx) <= 0
3395                 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3396                     sm2_curves[testnum].nid) <= 0
3397                 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
3398             EVP_PKEY_CTX_free(pctx);
3399             if (st == 0)
3400                 break;
3401
3402             st = 0; /* set back to zero */
3403             /* attach it sooner to rely on main final cleanup */
3404             loopargs[i].sm2_pkey[testnum] = sm2_pkey;
3405             loopargs[i].sigsize = EVP_PKEY_size(sm2_pkey);
3406
3407             sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3408             sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3409             if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
3410                 EVP_PKEY_CTX_free(sm2_vfy_pctx);
3411                 break;
3412             }
3413
3414             /* attach them directly to respective ctx */
3415             EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
3416             EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
3417
3418             /*
3419              * No need to allow user to set an explicit ID here, just use
3420              * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
3421              */
3422             if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
3423                 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
3424                 break;
3425
3426             if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
3427                                     EVP_sm3(), NULL, sm2_pkey))
3428                 break;
3429             if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
3430                                       EVP_sm3(), NULL, sm2_pkey))
3431                 break;
3432             st = 1;         /* mark loop as succeeded */
3433         }
3434         if (st == 0) {
3435             BIO_printf(bio_err, "SM2 init failure.\n");
3436             ERR_print_errors(bio_err);
3437             rsa_count = 1;
3438         } else {
3439             for (i = 0; i < loopargs_len; i++) {
3440                 size_t sm2_sigsize = loopargs[i].sigsize;
3441
3442                 /* Perform SM2 signature test */
3443                 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
3444                                     loopargs[i].buf2, &sm2_sigsize,
3445                                     loopargs[i].buf, 20);
3446                 if (st == 0)
3447                     break;
3448             }
3449             if (st == 0) {
3450                 BIO_printf(bio_err,
3451                            "SM2 sign failure.  No SM2 sign will be done.\n");
3452                 ERR_print_errors(bio_err);
3453                 rsa_count = 1;
3454             } else {
3455                 pkey_print_message("sign", sm2_curves[testnum].name,
3456                                    sm2_c[testnum][0],
3457                                    sm2_curves[testnum].bits, seconds.sm2);
3458                 Time_F(START);
3459                 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
3460                 d = Time_F(STOP);
3461
3462                 BIO_printf(bio_err,
3463                            mr ? "+R8:%ld:%u:%s:%.2f\n" :
3464                            "%ld %u bits %s signs in %.2fs \n",
3465                            count, sm2_curves[testnum].bits,
3466                            sm2_curves[testnum].name, d);
3467                 sm2_results[testnum][0] = (double)count / d;
3468                 rsa_count = count;
3469             }
3470
3471             /* Perform SM2 verification test */
3472             for (i = 0; i < loopargs_len; i++) {
3473                 st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
3474                                       loopargs[i].buf2, loopargs[i].sigsize,
3475                                       loopargs[i].buf, 20);
3476                 if (st != 1)
3477                     break;
3478             }
3479             if (st != 1) {
3480                 BIO_printf(bio_err,
3481                            "SM2 verify failure.  No SM2 verify will be done.\n");
3482                 ERR_print_errors(bio_err);
3483                 sm2_doit[testnum] = 0;
3484             } else {
3485                 pkey_print_message("verify", sm2_curves[testnum].name,
3486                                    sm2_c[testnum][1],
3487                                    sm2_curves[testnum].bits, seconds.sm2);
3488                 Time_F(START);
3489                 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
3490                 d = Time_F(STOP);
3491                 BIO_printf(bio_err,
3492                            mr ? "+R9:%ld:%u:%s:%.2f\n"
3493                            : "%ld %u bits %s verify in %.2fs\n",
3494                            count, sm2_curves[testnum].bits,
3495                            sm2_curves[testnum].name, d);
3496                 sm2_results[testnum][1] = (double)count / d;
3497             }
3498
3499             if (rsa_count <= 1) {
3500                 /* if longer than 10s, don't do any more */
3501                 for (testnum++; testnum < SM2_NUM; testnum++)
3502                     sm2_doit[testnum] = 0;
3503             }
3504         }
3505     }
3506 # endif                         /* OPENSSL_NO_SM2 */
3507
3508 #endif                          /* OPENSSL_NO_EC */
3509 #ifndef NO_FORK
3510  show_res:
3511 #endif
3512     if (!mr) {
3513         printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
3514         printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
3515         printf("options:");
3516         printf("%s ", BN_options());
3517 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3518         printf("%s ", MD2_options());
3519 #endif
3520 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3521         printf("%s ", RC4_options());
3522 #endif
3523 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3524         printf("%s ", DES_options());
3525 #endif
3526 #ifndef OPENSSL_NO_DEPRECATED_3_0
3527         printf("%s ", AES_options());
3528 #endif
3529 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3530         printf("%s ", IDEA_options());
3531 #endif
3532 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3533         printf("%s ", BF_options());
3534 #endif
3535         printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
3536         printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
3537     }
3538
3539     if (pr_header) {
3540         if (mr)
3541             printf("+H");
3542         else {
3543             printf
3544                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
3545             printf("type        ");
3546         }
3547         for (testnum = 0; testnum < size_num; testnum++)
3548             printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
3549         printf("\n");
3550     }
3551
3552     for (k = 0; k < ALGOR_NUM; k++) {
3553         if (!doit[k])
3554             continue;
3555         if (mr)
3556             printf("+F:%u:%s", k, names[k]);
3557         else
3558             printf("%-13s", names[k]);
3559         for (testnum = 0; testnum < size_num; testnum++) {
3560             if (results[k][testnum] > 10000 && !mr)
3561                 printf(" %11.2fk", results[k][testnum] / 1e3);
3562             else
3563                 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
3564         }
3565         printf("\n");
3566     }
3567 #ifndef OPENSSL_NO_RSA
3568     testnum = 1;
3569     for (k = 0; k < RSA_NUM; k++) {
3570         if (!rsa_doit[k])
3571             continue;
3572         if (testnum && !mr) {
3573             printf("%18ssign    verify    sign/s verify/s\n", " ");
3574             testnum = 0;
3575         }
3576         if (mr)
3577             printf("+F2:%u:%u:%f:%f\n",
3578                    k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1]);
3579         else
3580             printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3581                    rsa_keys[k].bits, 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
3582                    rsa_results[k][0], rsa_results[k][1]);
3583     }
3584 #endif
3585 #ifndef OPENSSL_NO_DSA
3586     testnum = 1;
3587     for (k = 0; k < DSA_NUM; k++) {
3588         if (!dsa_doit[k])
3589             continue;
3590         if (testnum && !mr) {
3591             printf("%18ssign    verify    sign/s verify/s\n", " ");
3592             testnum = 0;
3593         }
3594         if (mr)
3595             printf("+F3:%u:%u:%f:%f\n",
3596                    k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
3597         else
3598             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3599                    dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
3600                    dsa_results[k][0], dsa_results[k][1]);
3601     }
3602 #endif
3603 #ifndef OPENSSL_NO_EC
3604     testnum = 1;
3605     for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
3606         if (!ecdsa_doit[k])
3607             continue;
3608         if (testnum && !mr) {
3609             printf("%30ssign    verify    sign/s verify/s\n", " ");
3610             testnum = 0;
3611         }
3612
3613         if (mr)
3614             printf("+F4:%u:%u:%f:%f\n",
3615                    k, ec_curves[k].bits,
3616                    ecdsa_results[k][0], ecdsa_results[k][1]);
3617         else
3618             printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3619                    ec_curves[k].bits, ec_curves[k].name,
3620                    1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
3621                    ecdsa_results[k][0], ecdsa_results[k][1]);
3622     }
3623
3624     testnum = 1;
3625     for (k = 0; k < EC_NUM; k++) {
3626         if (!ecdh_doit[k])
3627             continue;
3628         if (testnum && !mr) {
3629             printf("%30sop      op/s\n", " ");
3630             testnum = 0;
3631         }
3632         if (mr)
3633             printf("+F5:%u:%u:%f:%f\n",
3634                    k, ec_curves[k].bits,
3635                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3636
3637         else
3638             printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
3639                    ec_curves[k].bits, ec_curves[k].name,
3640                    1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3641     }
3642
3643     testnum = 1;
3644     for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
3645         if (!eddsa_doit[k])
3646             continue;
3647         if (testnum && !mr) {
3648             printf("%30ssign    verify    sign/s verify/s\n", " ");
3649             testnum = 0;
3650         }
3651
3652         if (mr)
3653             printf("+F6:%u:%u:%s:%f:%f\n",
3654                    k, ed_curves[k].bits, ed_curves[k].name,
3655                    eddsa_results[k][0], eddsa_results[k][1]);
3656         else
3657             printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3658                    ed_curves[k].bits, ed_curves[k].name,
3659                    1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
3660                    eddsa_results[k][0], eddsa_results[k][1]);
3661     }
3662
3663 # ifndef OPENSSL_NO_SM2
3664     testnum = 1;
3665     for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
3666         if (!sm2_doit[k])
3667             continue;
3668         if (testnum && !mr) {
3669             printf("%30ssign    verify    sign/s verify/s\n", " ");
3670             testnum = 0;
3671         }
3672
3673         if (mr)
3674             printf("+F6:%u:%u:%s:%f:%f\n",
3675                    k, sm2_curves[k].bits, sm2_curves[k].name,
3676                    sm2_results[k][0], sm2_results[k][1]);
3677         else
3678             printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3679                    sm2_curves[k].bits, sm2_curves[k].name,
3680                    1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
3681                    sm2_results[k][0], sm2_results[k][1]);
3682     }
3683 # endif
3684 #endif                          /* OPENSSL_NO_EC */
3685
3686     ret = 0;
3687
3688  end:
3689     ERR_print_errors(bio_err);
3690     for (i = 0; i < loopargs_len; i++) {
3691         OPENSSL_free(loopargs[i].buf_malloc);
3692         OPENSSL_free(loopargs[i].buf2_malloc);
3693
3694 #ifndef OPENSSL_NO_RSA
3695         for (k = 0; k < RSA_NUM; k++)
3696             RSA_free(loopargs[i].rsa_key[k]);
3697 #endif
3698 #ifndef OPENSSL_NO_DSA
3699         for (k = 0; k < DSA_NUM; k++)
3700             DSA_free(loopargs[i].dsa_key[k]);
3701 #endif
3702 #ifndef OPENSSL_NO_EC
3703         for (k = 0; k < ECDSA_NUM; k++)
3704             EC_KEY_free(loopargs[i].ecdsa[k]);
3705         for (k = 0; k < EC_NUM; k++)
3706             EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
3707         for (k = 0; k < EdDSA_NUM; k++)
3708             EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
3709 # ifndef OPENSSL_NO_SM2
3710         for (k = 0; k < SM2_NUM; k++) {
3711             EVP_PKEY_CTX *pctx = NULL;
3712
3713             /* free signing ctx */
3714             if (loopargs[i].sm2_ctx[k] != NULL
3715                     && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
3716                 EVP_PKEY_CTX_free(pctx);
3717             EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
3718             /* free verification ctx */
3719             if (loopargs[i].sm2_vfy_ctx[k] != NULL
3720                     && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
3721                 EVP_PKEY_CTX_free(pctx);
3722             EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
3723             /* free pkey */
3724             EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
3725         }
3726 # endif
3727         OPENSSL_free(loopargs[i].secret_a);
3728         OPENSSL_free(loopargs[i].secret_b);
3729 #endif
3730     }
3731 #ifndef OPENSSL_NO_DEPRECATED_3_0
3732     OPENSSL_free(evp_hmac_name);
3733 #endif
3734 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3735     OPENSSL_free(evp_cmac_name);
3736 #endif
3737
3738     if (async_jobs > 0) {
3739         for (i = 0; i < loopargs_len; i++)
3740             ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
3741     }
3742
3743     if (async_init) {
3744         ASYNC_cleanup_thread();
3745     }
3746     OPENSSL_free(loopargs);
3747     release_engine(e);
3748     return ret;
3749 }
3750
3751 static void print_message(const char *s, long num, int length, int tm)
3752 {
3753 #ifdef SIGALRM
3754     BIO_printf(bio_err,
3755                mr ? "+DT:%s:%d:%d\n"
3756                : "Doing %s for %ds on %d size blocks: ", s, tm, length);
3757     (void)BIO_flush(bio_err);
3758     run = 1;
3759     alarm(tm);
3760 #else
3761     BIO_printf(bio_err,
3762                mr ? "+DN:%s:%ld:%d\n"
3763                : "Doing %s %ld times on %d size blocks: ", s, num, length);
3764     (void)BIO_flush(bio_err);
3765 #endif
3766 }
3767
3768 static void pkey_print_message(const char *str, const char *str2, long num,
3769                                unsigned int bits, int tm)
3770 {
3771 #ifdef SIGALRM
3772     BIO_printf(bio_err,
3773                mr ? "+DTP:%d:%s:%s:%d\n"
3774                : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
3775     (void)BIO_flush(bio_err);
3776     run = 1;
3777     alarm(tm);
3778 #else
3779     BIO_printf(bio_err,
3780                mr ? "+DNP:%ld:%d:%s:%s\n"
3781                : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
3782     (void)BIO_flush(bio_err);
3783 #endif
3784 }
3785
3786 static void print_result(int alg, int run_no, int count, double time_used)
3787 {
3788     if (count == -1) {
3789         BIO_printf(bio_err, "%s error!\n", names[alg]);
3790         ERR_print_errors(bio_err);
3791         /* exit(1);  disable exit until default provider enabled */
3792         return;
3793     }
3794     BIO_printf(bio_err,
3795                mr ? "+R:%d:%s:%f\n"
3796                : "%d %s's in %.2fs\n", count, names[alg], time_used);
3797     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
3798 }
3799
3800 #ifndef NO_FORK
3801 static char *sstrsep(char **string, const char *delim)
3802 {
3803     char isdelim[256];
3804     char *token = *string;
3805
3806     if (**string == 0)
3807         return NULL;
3808
3809     memset(isdelim, 0, sizeof(isdelim));
3810     isdelim[0] = 1;
3811
3812     while (*delim) {
3813         isdelim[(unsigned char)(*delim)] = 1;
3814         delim++;
3815     }
3816
3817     while (!isdelim[(unsigned char)(**string)]) {
3818         (*string)++;
3819     }
3820
3821     if (**string) {
3822         **string = 0;
3823         (*string)++;
3824     }
3825
3826     return token;
3827 }
3828
3829 static int do_multi(int multi, int size_num)
3830 {
3831     int n;
3832     int fd[2];
3833     int *fds;
3834     static char sep[] = ":";
3835
3836     fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
3837     for (n = 0; n < multi; ++n) {
3838         if (pipe(fd) == -1) {
3839             BIO_printf(bio_err, "pipe failure\n");
3840             exit(1);
3841         }
3842         fflush(stdout);
3843         (void)BIO_flush(bio_err);
3844         if (fork()) {
3845             close(fd[1]);
3846             fds[n] = fd[0];
3847         } else {
3848             close(fd[0]);
3849             close(1);
3850             if (dup(fd[1]) == -1) {
3851                 BIO_printf(bio_err, "dup failed\n");
3852                 exit(1);
3853             }
3854             close(fd[1]);
3855             mr = 1;
3856             usertime = 0;
3857             OPENSSL_free(fds);
3858             return 0;
3859         }
3860         printf("Forked child %d\n", n);
3861     }
3862
3863     /* for now, assume the pipe is long enough to take all the output */
3864     for (n = 0; n < multi; ++n) {
3865         FILE *f;
3866         char buf[1024];
3867         char *p;
3868
3869         f = fdopen(fds[n], "r");
3870         while (fgets(buf, sizeof(buf), f)) {
3871             p = strchr(buf, '\n');
3872             if (p)
3873                 *p = '\0';
3874             if (buf[0] != '+') {
3875                 BIO_printf(bio_err,
3876                            "Don't understand line '%s' from child %d\n", buf,
3877                            n);
3878                 continue;
3879             }
3880             printf("Got: %s from %d\n", buf, n);
3881             if (strncmp(buf, "+F:", 3) == 0) {
3882                 int alg;
3883                 int j;
3884
3885                 p = buf + 3;
3886                 alg = atoi(sstrsep(&p, sep));
3887                 sstrsep(&p, sep);
3888                 for (j = 0; j < size_num; ++j)
3889                     results[alg][j] += atof(sstrsep(&p, sep));
3890             } else if (strncmp(buf, "+F2:", 4) == 0) {
3891                 int k;
3892                 double d;
3893
3894                 p = buf + 4;
3895                 k = atoi(sstrsep(&p, sep));
3896                 sstrsep(&p, sep);
3897
3898                 d = atof(sstrsep(&p, sep));
3899                 rsa_results[k][0] += d;
3900
3901                 d = atof(sstrsep(&p, sep));
3902                 rsa_results[k][1] += d;
3903             }
3904 # ifndef OPENSSL_NO_DSA
3905             else if (strncmp(buf, "+F3:", 4) == 0) {
3906                 int k;
3907                 double d;
3908
3909                 p = buf + 4;
3910                 k = atoi(sstrsep(&p, sep));
3911                 sstrsep(&p, sep);
3912
3913                 d = atof(sstrsep(&p, sep));
3914                 dsa_results[k][0] += d;
3915
3916                 d = atof(sstrsep(&p, sep));
3917                 dsa_results[k][1] += d;
3918             }
3919 # endif
3920 # ifndef OPENSSL_NO_EC
3921             else if (strncmp(buf, "+F4:", 4) == 0) {
3922                 int k;
3923                 double d;
3924
3925                 p = buf + 4;
3926                 k = atoi(sstrsep(&p, sep));
3927                 sstrsep(&p, sep);
3928
3929                 d = atof(sstrsep(&p, sep));
3930                 ecdsa_results[k][0] += d;
3931
3932                 d = atof(sstrsep(&p, sep));
3933                 ecdsa_results[k][1] += d;
3934             } else if (strncmp(buf, "+F5:", 4) == 0) {
3935                 int k;
3936                 double d;
3937
3938                 p = buf + 4;
3939                 k = atoi(sstrsep(&p, sep));
3940                 sstrsep(&p, sep);
3941
3942                 d = atof(sstrsep(&p, sep));
3943                 ecdh_results[k][0] += d;
3944             } else if (strncmp(buf, "+F6:", 4) == 0) {
3945                 int k;
3946                 double d;
3947
3948                 p = buf + 4;
3949                 k = atoi(sstrsep(&p, sep));
3950                 sstrsep(&p, sep);
3951                 sstrsep(&p, sep);
3952
3953                 d = atof(sstrsep(&p, sep));
3954                 eddsa_results[k][0] += d;
3955
3956                 d = atof(sstrsep(&p, sep));
3957                 eddsa_results[k][1] += d;
3958             }
3959 #  ifndef OPENSSL_NO_SM2
3960             else if (strncmp(buf, "+F7:", 4) == 0) {
3961                 int k;
3962                 double d;
3963
3964                 p = buf + 4;
3965                 k = atoi(sstrsep(&p, sep));
3966                 sstrsep(&p, sep);
3967
3968                 d = atof(sstrsep(&p, sep));
3969                 sm2_results[k][0] += d;
3970
3971                 d = atof(sstrsep(&p, sep));
3972                 sm2_results[k][1] += d;
3973             }
3974 #  endif /* OPENSSL_NO_SM2 */
3975 # endif
3976
3977             else if (strncmp(buf, "+H:", 3) == 0) {
3978                 ;
3979             } else
3980                 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
3981                            n);
3982         }
3983
3984         fclose(f);
3985     }
3986     OPENSSL_free(fds);
3987     return 1;
3988 }
3989 #endif
3990
3991 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
3992                              const openssl_speed_sec_t *seconds)
3993 {
3994     static const int mblengths_list[] =
3995         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3996     const int *mblengths = mblengths_list;
3997     int j, count, keylen, num = OSSL_NELEM(mblengths_list);
3998     const char *alg_name;
3999     unsigned char *inp, *out, *key, no_key[32], no_iv[16];
4000     EVP_CIPHER_CTX *ctx;
4001     double d = 0.0;
4002
4003     if (lengths_single) {
4004         mblengths = &lengths_single;
4005         num = 1;
4006     }
4007
4008     inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
4009     out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
4010     ctx = EVP_CIPHER_CTX_new();
4011     EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
4012
4013     keylen = EVP_CIPHER_CTX_key_length(ctx);
4014     key = app_malloc(keylen, "evp_cipher key");
4015     EVP_CIPHER_CTX_rand_key(ctx, key);
4016     EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
4017     OPENSSL_clear_free(key, keylen);
4018
4019     EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
4020     alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
4021
4022     for (j = 0; j < num; j++) {
4023         print_message(alg_name, 0, mblengths[j], seconds->sym);
4024         Time_F(START);
4025         for (count = 0; run && count < 0x7fffffff; count++) {
4026             unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
4027             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
4028             size_t len = mblengths[j];
4029             int packlen;
4030
4031             memset(aad, 0, 8);  /* avoid uninitialized values */
4032             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
4033             aad[9] = 3;         /* version */
4034             aad[10] = 2;
4035             aad[11] = 0;        /* length */
4036             aad[12] = 0;
4037             mb_param.out = NULL;
4038             mb_param.inp = aad;
4039             mb_param.len = len;
4040             mb_param.interleave = 8;
4041
4042             packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
4043                                           sizeof(mb_param), &mb_param);
4044
4045             if (packlen > 0) {
4046                 mb_param.out = out;
4047                 mb_param.inp = inp;
4048                 mb_param.len = len;
4049                 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
4050                                     sizeof(mb_param), &mb_param);
4051             } else {
4052                 int pad;
4053
4054                 RAND_bytes(out, 16);
4055                 len += 16;
4056                 aad[11] = (unsigned char)(len >> 8);
4057                 aad[12] = (unsigned char)(len);
4058                 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
4059                                           EVP_AEAD_TLS1_AAD_LEN, aad);
4060                 EVP_Cipher(ctx, out, inp, len + pad);
4061             }
4062         }
4063         d = Time_F(STOP);
4064         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
4065                    : "%d %s's in %.2fs\n", count, "evp", d);
4066         results[D_EVP][j] = ((double)count) / d * mblengths[j];
4067     }
4068
4069     if (mr) {
4070         fprintf(stdout, "+H");
4071         for (j = 0; j < num; j++)
4072             fprintf(stdout, ":%d", mblengths[j]);
4073         fprintf(stdout, "\n");
4074         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
4075         for (j = 0; j < num; j++)
4076             fprintf(stdout, ":%.2f", results[D_EVP][j]);
4077         fprintf(stdout, "\n");
4078     } else {
4079         fprintf(stdout,
4080                 "The 'numbers' are in 1000s of bytes per second processed.\n");
4081         fprintf(stdout, "type                    ");
4082         for (j = 0; j < num; j++)
4083             fprintf(stdout, "%7d bytes", mblengths[j]);
4084         fprintf(stdout, "\n");
4085         fprintf(stdout, "%-24s", alg_name);
4086
4087         for (j = 0; j < num; j++) {
4088             if (results[D_EVP][j] > 10000)
4089                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
4090             else
4091                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
4092         }
4093         fprintf(stdout, "\n");
4094     }
4095
4096     OPENSSL_free(inp);
4097     OPENSSL_free(out);
4098     EVP_CIPHER_CTX_free(ctx);
4099 }