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