e867448015992bcc6935fe776d41c7f3391ede83
[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;
1287     int ret, count;
1288     EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1289     const size_t max_size = EVP_PKEY_size(sm2_pkey[testnum]);
1290
1291     for (count = 0; COND(sm2_c[testnum][0]); count++) {
1292         sm2sigsize = max_size;
1293
1294         if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
1295                                 NULL, sm2_pkey[testnum])) {
1296             BIO_printf(bio_err, "SM2 init sign failure\n");
1297             ERR_print_errors(bio_err);
1298             count = -1;
1299             break;
1300         }
1301         ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
1302                              buf, 20);
1303         if (ret == 0) {
1304             BIO_printf(bio_err, "SM2 sign failure\n");
1305             ERR_print_errors(bio_err);
1306             count = -1;
1307             break;
1308         }
1309         /* update the latest returned size and always use the fixed buffer size */
1310         tempargs->sigsize = sm2sigsize;
1311     }
1312
1313     return count;
1314 }
1315
1316 static int SM2_verify_loop(void *args)
1317 {
1318     loopargs_t *tempargs = *(loopargs_t **) args;
1319     unsigned char *buf = tempargs->buf;
1320     EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
1321     unsigned char *sm2sig = tempargs->buf2;
1322     size_t sm2sigsize = tempargs->sigsize;
1323     int ret, count;
1324     EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1325
1326     for (count = 0; COND(sm2_c[testnum][1]); count++) {
1327         if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
1328                                   NULL, sm2_pkey[testnum])) {
1329             BIO_printf(bio_err, "SM2 verify init failure\n");
1330             ERR_print_errors(bio_err);
1331             count = -1;
1332             break;
1333         }
1334         ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
1335                                buf, 20);
1336         if (ret != 1) {
1337             BIO_printf(bio_err, "SM2 verify failure\n");
1338             ERR_print_errors(bio_err);
1339             count = -1;
1340             break;
1341         }
1342     }
1343     return count;
1344 }
1345 # endif                         /* OPENSSL_NO_SM2 */
1346 #endif                          /* OPENSSL_NO_EC */
1347
1348 static int run_benchmark(int async_jobs,
1349                          int (*loop_function) (void *), loopargs_t * loopargs)
1350 {
1351     int job_op_count = 0;
1352     int total_op_count = 0;
1353     int num_inprogress = 0;
1354     int error = 0, i = 0, ret = 0;
1355     OSSL_ASYNC_FD job_fd = 0;
1356     size_t num_job_fds = 0;
1357
1358     if (async_jobs == 0) {
1359         return loop_function((void *)&loopargs);
1360     }
1361
1362     for (i = 0; i < async_jobs && !error; i++) {
1363         loopargs_t *looparg_item = loopargs + i;
1364
1365         /* Copy pointer content (looparg_t item address) into async context */
1366         ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1367                               &job_op_count, loop_function,
1368                               (void *)&looparg_item, sizeof(looparg_item));
1369         switch (ret) {
1370         case ASYNC_PAUSE:
1371             ++num_inprogress;
1372             break;
1373         case ASYNC_FINISH:
1374             if (job_op_count == -1) {
1375                 error = 1;
1376             } else {
1377                 total_op_count += job_op_count;
1378             }
1379             break;
1380         case ASYNC_NO_JOBS:
1381         case ASYNC_ERR:
1382             BIO_printf(bio_err, "Failure in the job\n");
1383             ERR_print_errors(bio_err);
1384             error = 1;
1385             break;
1386         }
1387     }
1388
1389     while (num_inprogress > 0) {
1390 #if defined(OPENSSL_SYS_WINDOWS)
1391         DWORD avail = 0;
1392 #elif defined(OPENSSL_SYS_UNIX)
1393         int select_result = 0;
1394         OSSL_ASYNC_FD max_fd = 0;
1395         fd_set waitfdset;
1396
1397         FD_ZERO(&waitfdset);
1398
1399         for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1400             if (loopargs[i].inprogress_job == NULL)
1401                 continue;
1402
1403             if (!ASYNC_WAIT_CTX_get_all_fds
1404                 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1405                 || num_job_fds > 1) {
1406                 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1407                 ERR_print_errors(bio_err);
1408                 error = 1;
1409                 break;
1410             }
1411             ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1412                                        &num_job_fds);
1413             FD_SET(job_fd, &waitfdset);
1414             if (job_fd > max_fd)
1415                 max_fd = job_fd;
1416         }
1417
1418         if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1419             BIO_printf(bio_err,
1420                        "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1421                        "Decrease the value of async_jobs\n",
1422                        max_fd, FD_SETSIZE);
1423             ERR_print_errors(bio_err);
1424             error = 1;
1425             break;
1426         }
1427
1428         select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1429         if (select_result == -1 && errno == EINTR)
1430             continue;
1431
1432         if (select_result == -1) {
1433             BIO_printf(bio_err, "Failure in the select\n");
1434             ERR_print_errors(bio_err);
1435             error = 1;
1436             break;
1437         }
1438
1439         if (select_result == 0)
1440             continue;
1441 #endif
1442
1443         for (i = 0; i < async_jobs; i++) {
1444             if (loopargs[i].inprogress_job == NULL)
1445                 continue;
1446
1447             if (!ASYNC_WAIT_CTX_get_all_fds
1448                 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1449                 || num_job_fds > 1) {
1450                 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1451                 ERR_print_errors(bio_err);
1452                 error = 1;
1453                 break;
1454             }
1455             ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1456                                        &num_job_fds);
1457
1458 #if defined(OPENSSL_SYS_UNIX)
1459             if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1460                 continue;
1461 #elif defined(OPENSSL_SYS_WINDOWS)
1462             if (num_job_fds == 1
1463                 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
1464                 && avail > 0)
1465                 continue;
1466 #endif
1467
1468             ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1469                                   loopargs[i].wait_ctx, &job_op_count,
1470                                   loop_function, (void *)(loopargs + i),
1471                                   sizeof(loopargs_t));
1472             switch (ret) {
1473             case ASYNC_PAUSE:
1474                 break;
1475             case ASYNC_FINISH:
1476                 if (job_op_count == -1) {
1477                     error = 1;
1478                 } else {
1479                     total_op_count += job_op_count;
1480                 }
1481                 --num_inprogress;
1482                 loopargs[i].inprogress_job = NULL;
1483                 break;
1484             case ASYNC_NO_JOBS:
1485             case ASYNC_ERR:
1486                 --num_inprogress;
1487                 loopargs[i].inprogress_job = NULL;
1488                 BIO_printf(bio_err, "Failure in the job\n");
1489                 ERR_print_errors(bio_err);
1490                 error = 1;
1491                 break;
1492             }
1493         }
1494     }
1495
1496     return error ? -1 : total_op_count;
1497 }
1498
1499 static EVP_MD *obtain_md(const char *name)
1500 {
1501     EVP_MD *md = NULL;
1502
1503     /* Look through providers' digests */
1504     ERR_set_mark();
1505     md = EVP_MD_fetch(NULL, name, NULL);
1506     ERR_pop_to_mark();
1507     if (md != NULL) {
1508         fetched_alg = 1;
1509         return md;
1510     }
1511
1512     return (EVP_MD *)EVP_get_digestbyname(name);
1513 }
1514
1515 static EVP_CIPHER *obtain_cipher(const char *name)
1516 {
1517     EVP_CIPHER *cipher = NULL;
1518
1519     /* Look through providers' ciphers */
1520     ERR_set_mark();
1521     cipher = EVP_CIPHER_fetch(NULL, name, NULL);
1522     ERR_pop_to_mark();
1523     if (cipher != NULL) {
1524         fetched_alg = 1;
1525         return cipher;
1526     }
1527
1528     return (EVP_CIPHER *)EVP_get_cipherbyname(name);
1529 }
1530
1531 #define stop_it(do_it, test_num)\
1532     memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
1533
1534 int speed_main(int argc, char **argv)
1535 {
1536     ENGINE *e = NULL;
1537     loopargs_t *loopargs = NULL;
1538     const char *prog;
1539     const char *engine_id = NULL;
1540     EVP_CIPHER *evp_cipher = NULL;
1541     double d = 0.0;
1542     OPTION_CHOICE o;
1543     int async_init = 0, multiblock = 0, pr_header = 0;
1544     uint8_t doit[ALGOR_NUM] = { 0 };
1545     int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
1546     long count = 0;
1547     unsigned int size_num = SIZE_NUM;
1548     unsigned int i, k, loopargs_len = 0, async_jobs = 0;
1549     int keylen;
1550     int buflen;
1551 #ifndef NO_FORK
1552     int multi = 0;
1553 #endif
1554 #if !defined(OPENSSL_NO_DEPRECATED_3_0)         \
1555     || !defined(OPENSSL_NO_DSA)                 \
1556     || !defined(OPENSSL_NO_DH)                  \
1557     || !defined(OPENSSL_NO_EC)
1558      long op_count = 1;
1559 #endif
1560     openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
1561                                     ECDSA_SECONDS, ECDH_SECONDS,
1562                                     EdDSA_SECONDS, SM2_SECONDS,
1563                                     FFDH_SECONDS };
1564
1565     /* What follows are the buffers and key material. */
1566 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1567     RC5_32_KEY rc5_ks;
1568 #endif
1569 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1570     RC2_KEY rc2_ks;
1571 #endif
1572 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1573     IDEA_KEY_SCHEDULE idea_ks;
1574 #endif
1575 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1576     SEED_KEY_SCHEDULE seed_ks;
1577 #endif
1578 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1579     BF_KEY bf_ks;
1580 #endif
1581 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1582     CAST_KEY cast_ks;
1583 #endif
1584 #ifndef OPENSSL_NO_DEPRECATED_3_0
1585     static const unsigned char key16[16] = {
1586         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1587         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1588     };
1589     static const unsigned char key24[24] = {
1590         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1591         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1592         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1593     };
1594     static const unsigned char key32[32] = {
1595         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1596         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1597         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1598         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1599     };
1600 #endif
1601 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1602     CAMELLIA_KEY camellia_ks[3];
1603 #endif
1604 #ifndef OPENSSL_NO_DEPRECATED_3_0
1605     static const struct {
1606         const unsigned char *data;
1607         unsigned int length;
1608         unsigned int bits;
1609     } rsa_keys[] = {
1610         {   test512,   sizeof(test512),   512 },
1611         {  test1024,  sizeof(test1024),  1024 },
1612         {  test2048,  sizeof(test2048),  2048 },
1613         {  test3072,  sizeof(test3072),  3072 },
1614         {  test4096,  sizeof(test4096),  4092 },
1615         {  test7680,  sizeof(test7680),  7680 },
1616         { test15360, sizeof(test15360), 15360 }
1617     };
1618     uint8_t rsa_doit[RSA_NUM] = { 0 };
1619     int primes = RSA_DEFAULT_PRIME_NUM;
1620 #endif
1621 #ifndef OPENSSL_NO_DH
1622     typedef struct ffdh_params_st {
1623         const char *name;
1624         unsigned int nid;
1625         unsigned int bits;
1626     } FFDH_PARAMS;
1627
1628     static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
1629         {"ffdh2048", NID_ffdhe2048, 2048},
1630         {"ffdh3072", NID_ffdhe3072, 3072},
1631         {"ffdh4096", NID_ffdhe4096, 4096},
1632         {"ffdh6144", NID_ffdhe6144, 6144},
1633         {"ffdh8192", NID_ffdhe8192, 8192}
1634     };
1635     uint8_t ffdh_doit[FFDH_NUM] = { 0 };
1636
1637 #endif /* OPENSSL_NO_DH */
1638 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1639     static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1640     uint8_t dsa_doit[DSA_NUM] = { 0 };
1641 #endif
1642 #ifndef OPENSSL_NO_EC
1643     typedef struct ec_curve_st {
1644         const char *name;
1645         unsigned int nid;
1646         unsigned int bits;
1647         size_t sigsize; /* only used for EdDSA curves */
1648     } EC_CURVE;
1649     /*
1650      * We only test over the following curves as they are representative, To
1651      * add tests over more curves, simply add the curve NID and curve name to
1652      * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
1653      * lists accordingly.
1654      */
1655     static const EC_CURVE ec_curves[EC_NUM] = {
1656         /* Prime Curves */
1657         {"secp160r1", NID_secp160r1, 160},
1658         {"nistp192", NID_X9_62_prime192v1, 192},
1659         {"nistp224", NID_secp224r1, 224},
1660         {"nistp256", NID_X9_62_prime256v1, 256},
1661         {"nistp384", NID_secp384r1, 384},
1662         {"nistp521", NID_secp521r1, 521},
1663 # ifndef OPENSSL_NO_EC2M
1664         /* Binary Curves */
1665         {"nistk163", NID_sect163k1, 163},
1666         {"nistk233", NID_sect233k1, 233},
1667         {"nistk283", NID_sect283k1, 283},
1668         {"nistk409", NID_sect409k1, 409},
1669         {"nistk571", NID_sect571k1, 571},
1670         {"nistb163", NID_sect163r2, 163},
1671         {"nistb233", NID_sect233r1, 233},
1672         {"nistb283", NID_sect283r1, 283},
1673         {"nistb409", NID_sect409r1, 409},
1674         {"nistb571", NID_sect571r1, 571},
1675 # endif
1676         {"brainpoolP256r1", NID_brainpoolP256r1, 256},
1677         {"brainpoolP256t1", NID_brainpoolP256t1, 256},
1678         {"brainpoolP384r1", NID_brainpoolP384r1, 384},
1679         {"brainpoolP384t1", NID_brainpoolP384t1, 384},
1680         {"brainpoolP512r1", NID_brainpoolP512r1, 512},
1681         {"brainpoolP512t1", NID_brainpoolP512t1, 512},
1682         /* Other and ECDH only ones */
1683         {"X25519", NID_X25519, 253},
1684         {"X448", NID_X448, 448}
1685     };
1686     static const EC_CURVE ed_curves[EdDSA_NUM] = {
1687         /* EdDSA */
1688         {"Ed25519", NID_ED25519, 253, 64},
1689         {"Ed448", NID_ED448, 456, 114}
1690     };
1691 # ifndef OPENSSL_NO_SM2
1692     static const EC_CURVE sm2_curves[SM2_NUM] = {
1693         /* SM2 */
1694         {"CurveSM2", NID_sm2, 256}
1695     };
1696     uint8_t sm2_doit[SM2_NUM] = { 0 };
1697 # endif
1698     uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
1699     uint8_t ecdh_doit[EC_NUM] = { 0 };
1700     uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
1701
1702     /* checks declarated curves against choices list. */
1703     OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
1704     OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
1705
1706     OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
1707     OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
1708
1709     OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
1710     OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
1711
1712 # ifndef OPENSSL_NO_SM2
1713     OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
1714     OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
1715 # endif
1716 #endif                          /* ndef OPENSSL_NO_EC */
1717
1718     prog = opt_init(argc, argv, speed_options);
1719     while ((o = opt_next()) != OPT_EOF) {
1720         switch (o) {
1721         case OPT_EOF:
1722         case OPT_ERR:
1723  opterr:
1724             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1725             goto end;
1726         case OPT_HELP:
1727             opt_help(speed_options);
1728             ret = 0;
1729             goto end;
1730         case OPT_ELAPSED:
1731             usertime = 0;
1732             break;
1733         case OPT_EVP:
1734             if (doit[D_EVP]) {
1735                 BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog);
1736                 goto opterr;
1737             }
1738             evp_md = NULL;
1739             evp_cipher = obtain_cipher(opt_arg());
1740             if (evp_cipher == NULL)
1741                 evp_md = obtain_md(opt_arg());
1742             if (evp_cipher == NULL && evp_md == NULL) {
1743                 BIO_printf(bio_err,
1744                            "%s: %s is an unknown cipher or digest\n",
1745                            prog, opt_arg());
1746                 goto end;
1747             }
1748             doit[D_EVP] = 1;
1749             break;
1750         case OPT_HMAC:
1751 #ifndef OPENSSL_NO_DEPRECATED_3_0
1752             evp_hmac_md = EVP_get_digestbyname(opt_arg());
1753             if (evp_hmac_md == NULL) {
1754                 BIO_printf(bio_err, "%s: %s is an unknown digest\n",
1755                            prog, opt_arg());
1756                 goto end;
1757             }
1758             doit[D_EVP_HMAC] = 1;
1759             break;
1760 #endif
1761         case OPT_CMAC:
1762 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1763             evp_cmac_cipher = EVP_get_cipherbyname(opt_arg());
1764             if (evp_cmac_cipher == NULL) {
1765                 BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
1766                            prog, opt_arg());
1767                 goto end;
1768             }
1769             doit[D_EVP_CMAC] = 1;
1770 #endif
1771             break;
1772         case OPT_DECRYPT:
1773             decrypt = 1;
1774             break;
1775         case OPT_ENGINE:
1776             /*
1777              * In a forked execution, an engine might need to be
1778              * initialised by each child process, not by the parent.
1779              * So store the name here and run setup_engine() later on.
1780              */
1781             engine_id = opt_arg();
1782             break;
1783         case OPT_MULTI:
1784 #ifndef NO_FORK
1785             multi = atoi(opt_arg());
1786 #endif
1787             break;
1788         case OPT_ASYNCJOBS:
1789 #ifndef OPENSSL_NO_ASYNC
1790             async_jobs = atoi(opt_arg());
1791             if (!ASYNC_is_capable()) {
1792                 BIO_printf(bio_err,
1793                            "%s: async_jobs specified but async not supported\n",
1794                            prog);
1795                 goto opterr;
1796             }
1797             if (async_jobs > 99999) {
1798                 BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
1799                 goto opterr;
1800             }
1801 #endif
1802             break;
1803         case OPT_MISALIGN:
1804             if (!opt_int(opt_arg(), &misalign))
1805                 goto end;
1806             if (misalign > MISALIGN) {
1807                 BIO_printf(bio_err,
1808                            "%s: Maximum offset is %d\n", prog, MISALIGN);
1809                 goto opterr;
1810             }
1811             break;
1812         case OPT_MR:
1813             mr = 1;
1814             break;
1815         case OPT_MB:
1816             multiblock = 1;
1817 #ifdef OPENSSL_NO_MULTIBLOCK
1818             BIO_printf(bio_err,
1819                        "%s: -mb specified but multi-block support is disabled\n",
1820                        prog);
1821             goto end;
1822 #endif
1823             break;
1824         case OPT_R_CASES:
1825             if (!opt_rand(o))
1826                 goto end;
1827             break;
1828         case OPT_PROV_CASES:
1829             if (!opt_provider(o))
1830                 goto end;
1831             break;
1832         case OPT_PRIMES:
1833 #ifndef OPENSSL_NO_DEPRECATED_3_0
1834             if (!opt_int(opt_arg(), &primes))
1835                 goto end;
1836 #endif
1837             break;
1838         case OPT_SECONDS:
1839             seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1840                         = seconds.ecdh = seconds.eddsa
1841                         = seconds.sm2 = seconds.ffdh = atoi(opt_arg());
1842             break;
1843         case OPT_BYTES:
1844             lengths_single = atoi(opt_arg());
1845             lengths = &lengths_single;
1846             size_num = 1;
1847             break;
1848         case OPT_AEAD:
1849             aead = 1;
1850             break;
1851         }
1852     }
1853
1854     /* Remaining arguments are algorithms. */
1855     argc = opt_num_rest();
1856     argv = opt_rest();
1857
1858     app_RAND_load();
1859     for (; *argv; argv++) {
1860         const char *algo = *argv;
1861
1862         if (opt_found(algo, doit_choices, &i)) {
1863             doit[i] = 1;
1864             continue;
1865         }
1866 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1867         if (strcmp(algo, "des") == 0) {
1868             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1869             continue;
1870         }
1871 #endif
1872         if (strcmp(algo, "sha") == 0) {
1873             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1874             continue;
1875         }
1876 #ifndef OPENSSL_NO_DEPRECATED_3_0
1877         if (strcmp(algo, "openssl") == 0) /* just for compatibility */
1878             continue;
1879         if (strncmp(algo, "rsa", 3) == 0) {
1880             if (algo[3] == '\0') {
1881                 memset(rsa_doit, 1, sizeof(rsa_doit));
1882                 continue;
1883             }
1884             if (opt_found(algo, rsa_choices, &i)) {
1885                 rsa_doit[i] = 1;
1886                 continue;
1887             }
1888         }
1889 #endif
1890 #ifndef OPENSSL_NO_DH
1891         if (strncmp(algo, "ffdh", 4) == 0) {
1892             if (algo[4] == '\0') {
1893                 memset(ffdh_doit, 1, sizeof(ffdh_doit));
1894                 continue;
1895             }
1896             if (opt_found(algo, ffdh_choices, &i)) {
1897                 ffdh_doit[i] = 2;
1898                 continue;
1899             }
1900         }
1901 #endif
1902 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1903         if (strncmp(algo, "dsa", 3) == 0) {
1904             if (algo[3] == '\0') {
1905                 memset(dsa_doit, 1, sizeof(dsa_doit));
1906                 continue;
1907             }
1908             if (opt_found(algo, dsa_choices, &i)) {
1909                 dsa_doit[i] = 2;
1910                 continue;
1911             }
1912         }
1913 #endif
1914 #ifndef OPENSSL_NO_DEPRECATED_3_0
1915         if (strcmp(algo, "aes") == 0) {
1916             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1917             continue;
1918         }
1919 #endif
1920 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1921         if (strcmp(algo, "camellia") == 0) {
1922             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1923             continue;
1924         }
1925 #endif
1926 #ifndef OPENSSL_NO_EC
1927         if (strncmp(algo, "ecdsa", 5) == 0) {
1928             if (algo[5] == '\0') {
1929                 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
1930                 continue;
1931             }
1932             if (opt_found(algo, ecdsa_choices, &i)) {
1933                 ecdsa_doit[i] = 2;
1934                 continue;
1935             }
1936         }
1937         if (strncmp(algo, "ecdh", 4) == 0) {
1938             if (algo[4] == '\0') {
1939                 memset(ecdh_doit, 1, sizeof(ecdh_doit));
1940                 continue;
1941             }
1942             if (opt_found(algo, ecdh_choices, &i)) {
1943                 ecdh_doit[i] = 2;
1944                 continue;
1945             }
1946         }
1947         if (strcmp(algo, "eddsa") == 0) {
1948             memset(eddsa_doit, 1, sizeof(eddsa_doit));
1949             continue;
1950         }
1951         if (opt_found(algo, eddsa_choices, &i)) {
1952             eddsa_doit[i] = 2;
1953             continue;
1954         }
1955 # ifndef OPENSSL_NO_SM2
1956         if (strcmp(algo, "sm2") == 0) {
1957             memset(sm2_doit, 1, sizeof(sm2_doit));
1958             continue;
1959         }
1960         if (opt_found(algo, sm2_choices, &i)) {
1961             sm2_doit[i] = 2;
1962             continue;
1963         }
1964 # endif
1965 #endif  /* OPENSSL_NO_EC */
1966         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
1967         goto end;
1968     }
1969
1970     /* Sanity checks */
1971     if (aead) {
1972         if (evp_cipher == NULL) {
1973             BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
1974             goto end;
1975         } else if (!(EVP_CIPHER_flags(evp_cipher) &
1976                      EVP_CIPH_FLAG_AEAD_CIPHER)) {
1977             BIO_printf(bio_err, "%s is not an AEAD cipher\n",
1978                        OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1979             goto end;
1980         }
1981     }
1982     if (multiblock) {
1983         if (evp_cipher == NULL) {
1984             BIO_printf(bio_err,"-mb can be used only with a multi-block"
1985                                " capable cipher\n");
1986             goto end;
1987         } else if (!(EVP_CIPHER_flags(evp_cipher) &
1988                      EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1989             BIO_printf(bio_err, "%s is not a multi-block capable\n",
1990                        OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1991             goto end;
1992         } else if (async_jobs > 0) {
1993             BIO_printf(bio_err, "Async mode is not supported with -mb");
1994             goto end;
1995         }
1996     }
1997
1998     /* Initialize the job pool if async mode is enabled */
1999     if (async_jobs > 0) {
2000         async_init = ASYNC_init_thread(async_jobs, async_jobs);
2001         if (!async_init) {
2002             BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
2003             goto end;
2004         }
2005     }
2006
2007     loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
2008     loopargs =
2009         app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
2010     memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
2011
2012     for (i = 0; i < loopargs_len; i++) {
2013         if (async_jobs > 0) {
2014             loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
2015             if (loopargs[i].wait_ctx == NULL) {
2016                 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
2017                 goto end;
2018             }
2019         }
2020
2021         buflen = lengths[size_num - 1];
2022         if (buflen < 36)    /* size of random vector in RSA benchmark */
2023             buflen = 36;
2024         buflen += MAX_MISALIGNMENT + 1;
2025         loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
2026         loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
2027         memset(loopargs[i].buf_malloc, 0, buflen);
2028         memset(loopargs[i].buf2_malloc, 0, buflen);
2029
2030         /* Align the start of buffers on a 64 byte boundary */
2031         loopargs[i].buf = loopargs[i].buf_malloc + misalign;
2032         loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
2033 #ifndef OPENSSL_NO_EC
2034         loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
2035         loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
2036 #endif
2037 #ifndef OPENSSL_NO_DH
2038         loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
2039         loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
2040 #endif
2041     }
2042
2043 #ifndef NO_FORK
2044     if (multi && do_multi(multi, size_num))
2045         goto show_res;
2046 #endif
2047
2048     /* Initialize the engine after the fork */
2049     e = setup_engine(engine_id, 0);
2050
2051     /* No parameters; turn on everything. */
2052     if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC] && !doit[D_EVP_CMAC]) {
2053         memset(doit, 1, sizeof(doit));
2054         doit[D_EVP] = doit[D_EVP_HMAC] = doit[D_EVP_CMAC] = 0;
2055 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2056         doit[D_MDC2] = 0;
2057 #endif
2058 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2059         doit[D_MD4] = 0;
2060 #endif
2061 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2062         doit[D_RMD160] = 0;
2063 #endif
2064 #ifndef OPENSSL_NO_DEPRECATED_3_0
2065         memset(rsa_doit, 1, sizeof(rsa_doit));
2066 #endif
2067 #ifndef OPENSSL_NO_DH
2068         memset(ffdh_doit, 1, sizeof(ffdh_doit));
2069 #endif
2070 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2071         memset(dsa_doit, 1, sizeof(dsa_doit));
2072 #endif
2073 #ifndef OPENSSL_NO_EC
2074         memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
2075         memset(ecdh_doit, 1, sizeof(ecdh_doit));
2076         memset(eddsa_doit, 1, sizeof(eddsa_doit));
2077 # ifndef OPENSSL_NO_SM2
2078         memset(sm2_doit, 1, sizeof(sm2_doit));
2079 # endif
2080 #endif
2081     }
2082     for (i = 0; i < ALGOR_NUM; i++)
2083         if (doit[i])
2084             pr_header++;
2085
2086     if (usertime == 0 && !mr)
2087         BIO_printf(bio_err,
2088                    "You have chosen to measure elapsed time "
2089                    "instead of user CPU time.\n");
2090
2091 #ifndef OPENSSL_NO_DEPRECATED_3_0
2092     for (i = 0; i < loopargs_len; i++) {
2093         if (primes > RSA_DEFAULT_PRIME_NUM) {
2094             /* for multi-prime RSA, skip this */
2095             break;
2096         }
2097         for (k = 0; k < RSA_NUM; k++) {
2098             const unsigned char *p = rsa_keys[k].data;
2099
2100             loopargs[i].rsa_key[k] =
2101                 d2i_RSAPrivateKey(NULL, &p, rsa_keys[k].length);
2102             if (loopargs[i].rsa_key[k] == NULL) {
2103                 BIO_printf(bio_err,
2104                            "internal error loading RSA key number %d\n", k);
2105                 goto end;
2106             }
2107         }
2108     }
2109 #endif
2110 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2111     for (i = 0; i < loopargs_len; i++) {
2112         loopargs[i].dsa_key[0] = get_dsa(512);
2113         loopargs[i].dsa_key[1] = get_dsa(1024);
2114         loopargs[i].dsa_key[2] = get_dsa(2048);
2115     }
2116 #endif
2117 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2118     if (doit[D_CBC_DES] || doit[D_EDE3_DES]) {
2119         static DES_cblock keys[] = {
2120             { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }, /* keys[0] */
2121             { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }, /* keys[1] */
2122             { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 }  /* keys[3] */
2123         };
2124         DES_set_key_unchecked(&keys[0], &sch[0]);
2125         DES_set_key_unchecked(&keys[1], &sch[1]);
2126         DES_set_key_unchecked(&keys[2], &sch[2]);
2127     }
2128 #endif
2129 #ifndef OPENSSL_NO_DEPRECATED_3_0
2130     AES_set_encrypt_key(key16, 128, &aes_ks1);
2131     AES_set_encrypt_key(key24, 192, &aes_ks2);
2132     AES_set_encrypt_key(key32, 256, &aes_ks3);
2133 #endif
2134 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2135     if (doit[D_CBC_128_CML] || doit[D_CBC_192_CML] || doit[D_CBC_256_CML]) {
2136         Camellia_set_key(key16, 128, &camellia_ks[0]);
2137         Camellia_set_key(key24, 192, &camellia_ks[1]);
2138         Camellia_set_key(key32, 256, &camellia_ks[2]);
2139     }
2140 #endif
2141 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2142     if (doit[D_CBC_IDEA])
2143         IDEA_set_encrypt_key(key16, &idea_ks);
2144 #endif
2145 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2146     if (doit[D_CBC_SEED])
2147         SEED_set_key(key16, &seed_ks);
2148 #endif
2149 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2150     if (doit[D_RC4])
2151         RC4_set_key(&rc4_ks, 16, key16);
2152 #endif
2153 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2154     if (doit[D_CBC_RC2])
2155         RC2_set_key(&rc2_ks, 16, key16, 128);
2156 #endif
2157 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2158     if (doit[D_CBC_RC5])
2159         if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
2160             BIO_printf(bio_err, "Failed setting RC5 key\n");
2161             goto end;
2162         }
2163 #endif
2164 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2165     if (doit[D_CBC_BF])
2166         BF_set_key(&bf_ks, 16, key16);
2167 #endif
2168 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2169     if (doit[D_CBC_CAST]) 
2170         CAST_set_key(&cast_ks, 16, key16);
2171 #endif
2172 #ifndef SIGALRM
2173 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2174     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
2175     count = 10;
2176     do {
2177         long it;
2178         count *= 2;
2179         Time_F(START);
2180         for (it = count; it; it--)
2181             DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
2182                             (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
2183         d = Time_F(STOP);
2184     } while (d < 3);
2185     c[D_MD2][0] = count / 10;
2186     c[D_MDC2][0] = count / 10;
2187     c[D_MD4][0] = count;
2188     c[D_MD5][0] = count;
2189     c[D_HMAC][0] = count;
2190     c[D_SHA1][0] = count;
2191     c[D_RMD160][0] = count;
2192     c[D_RC4][0] = count * 5;
2193     c[D_CBC_DES][0] = count;
2194     c[D_EDE3_DES][0] = count / 3;
2195     c[D_CBC_IDEA][0] = count;
2196     c[D_CBC_SEED][0] = count;
2197     c[D_CBC_RC2][0] = count;
2198     c[D_CBC_RC5][0] = count;
2199     c[D_CBC_BF][0] = count;
2200     c[D_CBC_CAST][0] = count;
2201     c[D_CBC_128_AES][0] = count;
2202     c[D_CBC_192_AES][0] = count;
2203     c[D_CBC_256_AES][0] = count;
2204     c[D_CBC_128_CML][0] = count;
2205     c[D_CBC_192_CML][0] = count;
2206     c[D_CBC_256_CML][0] = count;
2207     c[D_EVP][0] = count;
2208     c[D_SHA256][0] = count;
2209     c[D_SHA512][0] = count;
2210     c[D_WHIRLPOOL][0] = count;
2211     c[D_IGE_128_AES][0] = count;
2212     c[D_IGE_192_AES][0] = count;
2213     c[D_IGE_256_AES][0] = count;
2214     c[D_GHASH][0] = count;
2215     c[D_RAND][0] = count;
2216     c[D_EVP_HMAC][0] = count;
2217     c[D_EVP_CMAC][0] = count;
2218
2219     for (i = 1; i < size_num; i++) {
2220         long l0 = (long)lengths[0];
2221         long l1 = (long)lengths[i];
2222
2223         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
2224         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
2225         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
2226         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
2227         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
2228         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
2229         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
2230         c[D_EVP][i] = = c[D_EVP][0] * 4 * l0 / l1;
2231         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
2232         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
2233         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
2234         c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
2235         c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
2236         c[D_EVP_HMAC][i] = = c[D_EVP_HMAC][0] * 4 * l0 / l1;
2237         c[D_EVP_CMAC][i] = = c[D_EVP_CMAC][0] * 4 * l0 / l1;
2238
2239         l0 = (long)lengths[i - 1];
2240
2241         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
2242         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
2243         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
2244         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
2245         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
2246         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
2247         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
2248         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
2249         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
2250         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
2251         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
2252         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
2253         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
2254         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
2255         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
2256         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
2257         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
2258         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
2259     }
2260
2261 #  ifndef OPENSSL_NO_DEPRECATED_3_0
2262     rsa_c[R_RSA_512][0] = count / 2000;
2263     rsa_c[R_RSA_512][1] = count / 400;
2264     for (i = 1; i < RSA_NUM; i++) {
2265         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
2266         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
2267         if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
2268             rsa_doit[i] = 0;
2269         else {
2270             if (rsa_c[i][0] == 0) {
2271                 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2272                 rsa_c[i][1] = 20;
2273             }
2274         }
2275     }
2276 #  endif
2277
2278 #  if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2279     dsa_c[R_DSA_512][0] = count / 1000;
2280     dsa_c[R_DSA_512][1] = count / 1000 / 2;
2281     for (i = 1; i < DSA_NUM; i++) {
2282         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
2283         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
2284         if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
2285             dsa_doit[i] = 0;
2286         else {
2287             if (dsa_c[i][0] == 0) {
2288                 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2289                 dsa_c[i][1] = 1;
2290             }
2291         }
2292     }
2293 #  endif
2294
2295 #  ifndef OPENSSL_NO_EC
2296     ecdsa_c[R_EC_P160][0] = count / 1000;
2297     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
2298     for (i = R_EC_P192; i <= R_EC_P521; i++) {
2299         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2300         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2301         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2302             ecdsa_doit[i] = 0;
2303         else {
2304             if (ecdsa_c[i][0] == 0) {
2305                 ecdsa_c[i][0] = 1;
2306                 ecdsa_c[i][1] = 1;
2307             }
2308         }
2309     }
2310 #   ifndef OPENSSL_NO_EC2M
2311     ecdsa_c[R_EC_K163][0] = count / 1000;
2312     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
2313     for (i = R_EC_K233; i <= R_EC_K571; i++) {
2314         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2315         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2316         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2317             ecdsa_doit[i] = 0;
2318         else {
2319             if (ecdsa_c[i][0] == 0) {
2320                 ecdsa_c[i][0] = 1;
2321                 ecdsa_c[i][1] = 1;
2322             }
2323         }
2324     }
2325     ecdsa_c[R_EC_B163][0] = count / 1000;
2326     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
2327     for (i = R_EC_B233; i <= R_EC_B571; i++) {
2328         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2329         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2330         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2331             ecdsa_doit[i] = 0;
2332         else {
2333             if (ecdsa_c[i][0] == 0) {
2334                 ecdsa_c[i][0] = 1;
2335                 ecdsa_c[i][1] = 1;
2336             }
2337         }
2338     }
2339 #   endif
2340
2341     ecdh_c[R_EC_P160][0] = count / 1000;
2342     for (i = R_EC_P192; i <= R_EC_P521; i++) {
2343         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2344         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2345             ecdh_doit[i] = 0;
2346         else {
2347             if (ecdh_c[i][0] == 0) {
2348                 ecdh_c[i][0] = 1;
2349             }
2350         }
2351     }
2352 #   ifndef OPENSSL_NO_EC2M
2353     ecdh_c[R_EC_K163][0] = count / 1000;
2354     for (i = R_EC_K233; i <= R_EC_K571; i++) {
2355         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2356         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2357             ecdh_doit[i] = 0;
2358         else {
2359             if (ecdh_c[i][0] == 0) {
2360                 ecdh_c[i][0] = 1;
2361             }
2362         }
2363     }
2364     ecdh_c[R_EC_B163][0] = count / 1000;
2365     for (i = R_EC_B233; i <= R_EC_B571; i++) {
2366         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2367         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2368             ecdh_doit[i] = 0;
2369         else {
2370             if (ecdh_c[i][0] == 0) {
2371                 ecdh_c[i][0] = 1;
2372             }
2373         }
2374     }
2375 #   endif
2376     /* repeated code good to factorize */
2377     ecdh_c[R_EC_BRP256R1][0] = count / 1000;
2378     for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
2379         ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2380         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2381             ecdh_doit[i] = 0;
2382         else {
2383             if (ecdh_c[i][0] == 0) {
2384                 ecdh_c[i][0] = 1;
2385             }
2386         }
2387     }
2388     ecdh_c[R_EC_BRP256T1][0] = count / 1000;
2389     for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
2390         ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2391         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2392             ecdh_doit[i] = 0;
2393         else {
2394             if (ecdh_c[i][0] == 0) {
2395                 ecdh_c[i][0] = 1;
2396             }
2397         }
2398     }
2399     /* default iteration count for the last two EC Curves */
2400     ecdh_c[R_EC_X25519][0] = count / 1800;
2401     ecdh_c[R_EC_X448][0] = count / 7200;
2402
2403     eddsa_c[R_EC_Ed25519][0] = count / 1800;
2404     eddsa_c[R_EC_Ed448][0] = count / 7200;
2405
2406 #   ifndef OPENSSL_NO_SM2
2407     sm2_c[R_EC_SM2P256][0] = count / 1800;
2408 #   endif
2409 #  endif                          /* OPENSSL_NO_EC */
2410
2411 #  ifndef OPENSSL_NO_DH
2412     ffdh_c[R_FFDH_2048][0] = count / 1000;
2413     for (i = R_FFDH_3072; i <= R_FFDH_8192; i++) {
2414         ffdh_c[i][0] = ffdh_c[i - 1][0] / 2;
2415         if (ffdh_doit[i] <= 1 && ffdh_c[i][0] == 0) {
2416             ffdh_doit[i] = 0;
2417         } else {
2418             if (ffdh_c[i][0] == 0)
2419                 ffdh_c[i][0] = 1;
2420         }
2421     }
2422 #  endif /* OPENSSL_NO_DH */
2423
2424 # else
2425 /* not worth fixing */
2426 #  error "You cannot disable DES on systems without SIGALRM."
2427 # endif                         /* OPENSSL_NO_DES */
2428 #elif SIGALRM > 0
2429     signal(SIGALRM, alarmed);
2430 #endif                          /* SIGALRM */
2431
2432 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2433     if (doit[D_MD2]) {
2434         for (testnum = 0; testnum < size_num; testnum++) {
2435             print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
2436                           seconds.sym);
2437             Time_F(START);
2438             count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
2439             d = Time_F(STOP);
2440             print_result(D_MD2, testnum, count, d);
2441         }
2442     }
2443 #endif
2444 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2445     if (doit[D_MDC2]) {
2446         for (testnum = 0; testnum < size_num; testnum++) {
2447             print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
2448                           seconds.sym);
2449             Time_F(START);
2450             count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
2451             d = Time_F(STOP);
2452             print_result(D_MDC2, testnum, count, d);
2453             if (count < 0)
2454                 break;
2455         }
2456     }
2457 #endif
2458
2459 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2460     if (doit[D_MD4]) {
2461         for (testnum = 0; testnum < size_num; testnum++) {
2462             print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
2463                           seconds.sym);
2464             Time_F(START);
2465             count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
2466             d = Time_F(STOP);
2467             print_result(D_MD4, testnum, count, d);
2468             if (count < 0)
2469                 break;
2470         }
2471     }
2472 #endif
2473
2474 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2475     if (doit[D_MD5]) {
2476         for (testnum = 0; testnum < size_num; testnum++) {
2477             print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
2478                           seconds.sym);
2479             Time_F(START);
2480             count = run_benchmark(async_jobs, MD5_loop, loopargs);
2481             d = Time_F(STOP);
2482             print_result(D_MD5, testnum, count, d);
2483         }
2484     }
2485
2486 # ifndef OPENSSL_NO_DEPRECATED_3_0
2487     if (doit[D_HMAC]) {
2488         static const char hmac_key[] = "This is a key...";
2489         int len = strlen(hmac_key);
2490
2491         for (i = 0; i < loopargs_len; i++) {
2492             loopargs[i].hctx = HMAC_CTX_new();
2493             if (loopargs[i].hctx == NULL) {
2494                 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2495                 exit(1);
2496             }
2497
2498             HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2499         }
2500         for (testnum = 0; testnum < size_num; testnum++) {
2501             print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
2502                           seconds.sym);
2503             Time_F(START);
2504             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2505             d = Time_F(STOP);
2506             print_result(D_HMAC, testnum, count, d);
2507         }
2508         for (i = 0; i < loopargs_len; i++)
2509             HMAC_CTX_free(loopargs[i].hctx);
2510     }
2511 # endif
2512 #endif
2513 #ifndef OPENSSL_NO_DEPRECATED_3_0
2514     if (doit[D_SHA1]) {
2515         for (testnum = 0; testnum < size_num; testnum++) {
2516             print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2517                           seconds.sym);
2518             Time_F(START);
2519             count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2520             d = Time_F(STOP);
2521             print_result(D_SHA1, testnum, count, d);
2522         }
2523     }
2524     if (doit[D_SHA256]) {
2525         for (testnum = 0; testnum < size_num; testnum++) {
2526             print_message(names[D_SHA256], c[D_SHA256][testnum],
2527                           lengths[testnum], seconds.sym);
2528             Time_F(START);
2529             count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2530             d = Time_F(STOP);
2531             print_result(D_SHA256, testnum, count, d);
2532         }
2533     }
2534     if (doit[D_SHA512]) {
2535         for (testnum = 0; testnum < size_num; testnum++) {
2536             print_message(names[D_SHA512], c[D_SHA512][testnum],
2537                           lengths[testnum], seconds.sym);
2538             Time_F(START);
2539             count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2540             d = Time_F(STOP);
2541             print_result(D_SHA512, testnum, count, d);
2542         }
2543     }
2544 #endif
2545 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2546     if (doit[D_WHIRLPOOL]) {
2547         for (testnum = 0; testnum < size_num; testnum++) {
2548             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2549                           lengths[testnum], seconds.sym);
2550             Time_F(START);
2551             count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2552             d = Time_F(STOP);
2553             print_result(D_WHIRLPOOL, testnum, count, d);
2554         }
2555     }
2556 #endif
2557
2558 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2559     if (doit[D_RMD160]) {
2560         for (testnum = 0; testnum < size_num; testnum++) {
2561             print_message(names[D_RMD160], c[D_RMD160][testnum],
2562                           lengths[testnum], seconds.sym);
2563             Time_F(START);
2564             count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2565             d = Time_F(STOP);
2566             print_result(D_RMD160, testnum, count, d);
2567             if (count < 0)
2568                 break;
2569         }
2570     }
2571 #endif
2572 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2573     if (doit[D_RC4]) {
2574         for (testnum = 0; testnum < size_num; testnum++) {
2575             print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2576                           seconds.sym);
2577             Time_F(START);
2578             count = run_benchmark(async_jobs, RC4_loop, loopargs);
2579             d = Time_F(STOP);
2580             print_result(D_RC4, testnum, count, d);
2581         }
2582     }
2583 #endif
2584 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2585     if (doit[D_CBC_DES]) {
2586         for (testnum = 0; testnum < size_num; testnum++) {
2587             print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2588                           lengths[testnum], seconds.sym);
2589             Time_F(START);
2590             count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2591             d = Time_F(STOP);
2592             print_result(D_CBC_DES, testnum, count, d);
2593         }
2594     }
2595
2596     if (doit[D_EDE3_DES]) {
2597         for (testnum = 0; testnum < size_num; testnum++) {
2598             print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2599                           lengths[testnum], seconds.sym);
2600             Time_F(START);
2601             count =
2602                 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2603             d = Time_F(STOP);
2604             print_result(D_EDE3_DES, testnum, count, d);
2605         }
2606     }
2607 #endif
2608
2609 #ifndef OPENSSL_NO_DEPRECATED_3_0
2610     if (doit[D_CBC_128_AES]) {
2611         for (testnum = 0; testnum < size_num; testnum++) {
2612             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2613                           lengths[testnum], seconds.sym);
2614             Time_F(START);
2615             count =
2616                 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2617             d = Time_F(STOP);
2618             print_result(D_CBC_128_AES, testnum, count, d);
2619         }
2620     }
2621     if (doit[D_CBC_192_AES]) {
2622         for (testnum = 0; testnum < size_num; testnum++) {
2623             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2624                           lengths[testnum], seconds.sym);
2625             Time_F(START);
2626             count =
2627                 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2628             d = Time_F(STOP);
2629             print_result(D_CBC_192_AES, testnum, count, d);
2630         }
2631     }
2632     if (doit[D_CBC_256_AES]) {
2633         for (testnum = 0; testnum < size_num; testnum++) {
2634             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2635                           lengths[testnum], seconds.sym);
2636             Time_F(START);
2637             count =
2638                 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2639             d = Time_F(STOP);
2640             print_result(D_CBC_256_AES, testnum, count, d);
2641         }
2642     }
2643
2644
2645     if (doit[D_IGE_128_AES]) {
2646         for (testnum = 0; testnum < size_num; testnum++) {
2647             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2648                           lengths[testnum], seconds.sym);
2649             Time_F(START);
2650             count =
2651                 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2652             d = Time_F(STOP);
2653             print_result(D_IGE_128_AES, testnum, count, d);
2654         }
2655     }
2656     if (doit[D_IGE_192_AES]) {
2657         for (testnum = 0; testnum < size_num; testnum++) {
2658             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2659                           lengths[testnum], seconds.sym);
2660             Time_F(START);
2661             count =
2662                 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2663             d = Time_F(STOP);
2664             print_result(D_IGE_192_AES, testnum, count, d);
2665         }
2666     }
2667     if (doit[D_IGE_256_AES]) {
2668         for (testnum = 0; testnum < size_num; testnum++) {
2669             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2670                           lengths[testnum], seconds.sym);
2671             Time_F(START);
2672             count =
2673                 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2674             d = Time_F(STOP);
2675             print_result(D_IGE_256_AES, testnum, count, d);
2676         }
2677     }
2678     if (doit[D_GHASH]) {
2679         for (i = 0; i < loopargs_len; i++) {
2680             loopargs[i].gcm_ctx =
2681                 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2682             CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2683                                 (unsigned char *)"0123456789ab", 12);
2684         }
2685
2686         for (testnum = 0; testnum < size_num; testnum++) {
2687             print_message(names[D_GHASH], c[D_GHASH][testnum],
2688                           lengths[testnum], seconds.sym);
2689             Time_F(START);
2690             count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2691             d = Time_F(STOP);
2692             print_result(D_GHASH, testnum, count, d);
2693         }
2694         for (i = 0; i < loopargs_len; i++)
2695             CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2696     }
2697 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
2698 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2699     if (doit[D_CBC_128_CML]) {
2700         if (async_jobs > 0) {
2701             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2702                        names[D_CBC_128_CML]);
2703             doit[D_CBC_128_CML] = 0;
2704         }
2705         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2706             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2707                           lengths[testnum], seconds.sym);
2708             Time_F(START);
2709             for (count = 0; COND(c[D_CBC_128_CML][testnum]); count++)
2710                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2711                                      (size_t)lengths[testnum], &camellia_ks[0],
2712                                      iv, CAMELLIA_ENCRYPT);
2713             d = Time_F(STOP);
2714             print_result(D_CBC_128_CML, testnum, count, d);
2715         }
2716     }
2717     if (doit[D_CBC_192_CML]) {
2718         if (async_jobs > 0) {
2719             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2720                        names[D_CBC_192_CML]);
2721             doit[D_CBC_192_CML] = 0;
2722         }
2723         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2724             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2725                           lengths[testnum], seconds.sym);
2726             if (async_jobs > 0) {
2727                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2728                 exit(1);
2729             }
2730             Time_F(START);
2731             for (count = 0; COND(c[D_CBC_192_CML][testnum]); count++)
2732                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2733                                      (size_t)lengths[testnum], &camellia_ks[1],
2734                                      iv, CAMELLIA_ENCRYPT);
2735             d = Time_F(STOP);
2736             print_result(D_CBC_192_CML, testnum, count, d);
2737         }
2738     }
2739     if (doit[D_CBC_256_CML]) {
2740         if (async_jobs > 0) {
2741             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2742                        names[D_CBC_256_CML]);
2743             doit[D_CBC_256_CML] = 0;
2744         }
2745         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2746             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2747                           lengths[testnum], seconds.sym);
2748             Time_F(START);
2749             for (count = 0; COND(c[D_CBC_256_CML][testnum]); count++)
2750                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2751                                      (size_t)lengths[testnum], &camellia_ks[2],
2752                                      iv, CAMELLIA_ENCRYPT);
2753             d = Time_F(STOP);
2754             print_result(D_CBC_256_CML, testnum, count, d);
2755         }
2756     }
2757 #endif
2758 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2759     if (doit[D_CBC_IDEA]) {
2760         if (async_jobs > 0) {
2761             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2762                        names[D_CBC_IDEA]);
2763             doit[D_CBC_IDEA] = 0;
2764         }
2765         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2766             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2767                           lengths[testnum], seconds.sym);
2768             Time_F(START);
2769             for (count = 0; COND(c[D_CBC_IDEA][testnum]); count++)
2770                 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2771                                  (size_t)lengths[testnum], &idea_ks,
2772                                  iv, IDEA_ENCRYPT);
2773             d = Time_F(STOP);
2774             print_result(D_CBC_IDEA, testnum, count, d);
2775         }
2776     }
2777 #endif
2778 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2779     if (doit[D_CBC_SEED]) {
2780         if (async_jobs > 0) {
2781             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2782                        names[D_CBC_SEED]);
2783             doit[D_CBC_SEED] = 0;
2784         }
2785         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2786             print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2787                           lengths[testnum], seconds.sym);
2788             Time_F(START);
2789             for (count = 0; COND(c[D_CBC_SEED][testnum]); count++)
2790                 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2791                                  (size_t)lengths[testnum], &seed_ks, iv, 1);
2792             d = Time_F(STOP);
2793             print_result(D_CBC_SEED, testnum, count, d);
2794         }
2795     }
2796 #endif
2797 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2798     if (doit[D_CBC_RC2]) {
2799         if (async_jobs > 0) {
2800             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2801                        names[D_CBC_RC2]);
2802             doit[D_CBC_RC2] = 0;
2803         }
2804         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2805             print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2806                           lengths[testnum], seconds.sym);
2807             if (async_jobs > 0) {
2808                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2809                 exit(1);
2810             }
2811             Time_F(START);
2812             for (count = 0; COND(c[D_CBC_RC2][testnum]); count++)
2813                 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2814                                 (size_t)lengths[testnum], &rc2_ks,
2815                                 iv, RC2_ENCRYPT);
2816             d = Time_F(STOP);
2817             print_result(D_CBC_RC2, testnum, count, d);
2818         }
2819     }
2820 #endif
2821 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2822     if (doit[D_CBC_RC5]) {
2823         if (async_jobs > 0) {
2824             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2825                        names[D_CBC_RC5]);
2826             doit[D_CBC_RC5] = 0;
2827         }
2828         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2829             print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2830                           lengths[testnum], seconds.sym);
2831             if (async_jobs > 0) {
2832                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2833                 exit(1);
2834             }
2835             Time_F(START);
2836             for (count = 0; COND(c[D_CBC_RC5][testnum]); count++)
2837                 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2838                                    (size_t)lengths[testnum], &rc5_ks,
2839                                    iv, RC5_ENCRYPT);
2840             d = Time_F(STOP);
2841             print_result(D_CBC_RC5, testnum, count, d);
2842         }
2843     }
2844 #endif
2845 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2846     if (doit[D_CBC_BF]) {
2847         if (async_jobs > 0) {
2848             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2849                        names[D_CBC_BF]);
2850             doit[D_CBC_BF] = 0;
2851         }
2852         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2853             print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2854                           lengths[testnum], seconds.sym);
2855             Time_F(START);
2856             for (count = 0; COND(c[D_CBC_BF][testnum]); count++)
2857                 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2858                                (size_t)lengths[testnum], &bf_ks,
2859                                iv, BF_ENCRYPT);
2860             d = Time_F(STOP);
2861             print_result(D_CBC_BF, testnum, count, d);
2862         }
2863     }
2864 #endif
2865 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2866     if (doit[D_CBC_CAST]) {
2867         if (async_jobs > 0) {
2868             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2869                        names[D_CBC_CAST]);
2870             doit[D_CBC_CAST] = 0;
2871         }
2872         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2873             print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2874                           lengths[testnum], seconds.sym);
2875             Time_F(START);
2876             for (count = 0; COND(c[D_CBC_CAST][testnum]); count++)
2877                 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2878                                  (size_t)lengths[testnum], &cast_ks,
2879                                  iv, CAST_ENCRYPT);
2880             d = Time_F(STOP);
2881             print_result(D_CBC_CAST, testnum, count, d);
2882         }
2883     }
2884 #endif
2885     if (doit[D_RAND]) {
2886         for (testnum = 0; testnum < size_num; testnum++) {
2887             print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2888                           seconds.sym);
2889             Time_F(START);
2890             count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2891             d = Time_F(STOP);
2892             print_result(D_RAND, testnum, count, d);
2893         }
2894     }
2895
2896     if (doit[D_EVP]) {
2897         if (evp_cipher != NULL) {
2898             int (*loopfunc) (void *) = EVP_Update_loop;
2899
2900             if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
2901                                EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2902                 multiblock_speed(evp_cipher, lengths_single, &seconds);
2903                 ret = 0;
2904                 goto end;
2905             }
2906
2907             names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2908
2909             if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
2910                 loopfunc = EVP_Update_loop_ccm;
2911             } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
2912                                 EVP_CIPH_FLAG_AEAD_CIPHER)) {
2913                 loopfunc = EVP_Update_loop_aead;
2914                 if (lengths == lengths_list) {
2915                     lengths = aead_lengths_list;
2916                     size_num = OSSL_NELEM(aead_lengths_list);
2917                 }
2918             }
2919
2920             for (testnum = 0; testnum < size_num; testnum++) {
2921                 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2922                               seconds.sym);
2923
2924                 for (k = 0; k < loopargs_len; k++) {
2925                     loopargs[k].ctx = EVP_CIPHER_CTX_new();
2926                     if (loopargs[k].ctx == NULL) {
2927                         BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
2928                         exit(1);
2929                     }
2930                     if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2931                                            NULL, iv, decrypt ? 0 : 1)) {
2932                         BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2933                         ERR_print_errors(bio_err);
2934                         exit(1);
2935                     }
2936
2937                     EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2938
2939                     keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
2940                     loopargs[k].key = app_malloc(keylen, "evp_cipher key");
2941                     EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
2942                     if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
2943                                            loopargs[k].key, NULL, -1)) {
2944                         BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2945                         ERR_print_errors(bio_err);
2946                         exit(1);
2947                     }
2948                     OPENSSL_clear_free(loopargs[k].key, keylen);
2949
2950                     /* SIV mode only allows for a single Update operation */
2951                     if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
2952                         EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_SET_SPEED, 1, NULL);
2953                 }
2954
2955                 Time_F(START);
2956                 count = run_benchmark(async_jobs, loopfunc, loopargs);
2957                 d = Time_F(STOP);
2958                 for (k = 0; k < loopargs_len; k++) {
2959                     EVP_CIPHER_CTX_free(loopargs[k].ctx);
2960                 }
2961                 print_result(D_EVP, testnum, count, d);
2962             }
2963         } else if (evp_md != NULL) {
2964             names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2965
2966             for (testnum = 0; testnum < size_num; testnum++) {
2967                 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2968                               seconds.sym);
2969                 Time_F(START);
2970                 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2971                 d = Time_F(STOP);
2972                 print_result(D_EVP, testnum, count, d);
2973             }
2974         }
2975     }
2976
2977 #ifndef OPENSSL_NO_DEPRECATED_3_0
2978     if (doit[D_EVP_HMAC] && evp_hmac_md != NULL) {
2979         const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
2980
2981         evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
2982                                    "HMAC name");
2983         sprintf(evp_hmac_name, "HMAC(%s)", md_name);
2984         names[D_EVP_HMAC] = evp_hmac_name;
2985
2986         for (testnum = 0; testnum < size_num; testnum++) {
2987             print_message(names[D_EVP_HMAC], c[D_EVP_HMAC][testnum], lengths[testnum],
2988                           seconds.sym);
2989             Time_F(START);
2990             count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
2991             d = Time_F(STOP);
2992             print_result(D_EVP_HMAC, testnum, count, d);
2993         }
2994     }
2995 #endif
2996
2997 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2998     if (doit[D_EVP_CMAC] && evp_cmac_cipher != NULL) {
2999         const char *cipher_name = OBJ_nid2ln(EVP_CIPHER_type(evp_cmac_cipher));
3000
3001         evp_cmac_name = app_malloc(sizeof("CMAC()") + strlen(cipher_name),
3002                                    "CMAC name");
3003         sprintf(evp_cmac_name, "CMAC(%s)", cipher_name);
3004         names[D_EVP_CMAC] = evp_cmac_name;
3005
3006         for (i = 0; i < loopargs_len; i++) {
3007             loopargs[i].cmac_ctx = CMAC_CTX_new();
3008             if (loopargs[i].cmac_ctx == NULL) {
3009                 BIO_printf(bio_err, "CMAC malloc failure, exiting...");
3010                 exit(1);
3011             }
3012         }
3013         for (testnum = 0; testnum < size_num; testnum++) {
3014             print_message(names[D_EVP_CMAC], c[D_EVP_CMAC][testnum], lengths[testnum],
3015                           seconds.sym);
3016             Time_F(START);
3017             count = run_benchmark(async_jobs, EVP_CMAC_loop, loopargs);
3018             d = Time_F(STOP);
3019             print_result(D_EVP_CMAC, testnum, count, d);
3020         }
3021         for (i = 0; i < loopargs_len; i++)
3022             CMAC_CTX_free(loopargs[i].cmac_ctx);
3023     }
3024 #endif
3025
3026     for (i = 0; i < loopargs_len; i++)
3027         if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3028             goto end;
3029
3030 #ifndef OPENSSL_NO_DEPRECATED_3_0
3031     for (testnum = 0; testnum < RSA_NUM; testnum++) {
3032         int st = 0;
3033         if (!rsa_doit[testnum])
3034             continue;
3035         for (i = 0; i < loopargs_len; i++) {
3036             if (primes > RSA_DEFAULT_PRIME_NUM) {
3037                 /* we haven't set keys yet,  generate multi-prime RSA keys */
3038                 BIGNUM *bn = BN_new();
3039
3040                 if (bn == NULL)
3041                     goto end;
3042                 if (!BN_set_word(bn, RSA_F4)) {
3043                     BN_free(bn);
3044                     goto end;
3045                 }
3046
3047                 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
3048                            rsa_choices[testnum].name);
3049
3050                 loopargs[i].rsa_key[testnum] = RSA_new();
3051                 if (loopargs[i].rsa_key[testnum] == NULL) {
3052                     BN_free(bn);
3053                     goto end;
3054                 }
3055
3056                 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
3057                                                   rsa_keys[testnum].bits,
3058                                                   primes, bn, NULL)) {
3059                     BN_free(bn);
3060                     goto end;
3061                 }
3062                 BN_free(bn);
3063             }
3064             st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3065                           &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3066             if (st == 0)
3067                 break;
3068         }
3069         if (st == 0) {
3070             BIO_printf(bio_err,
3071                        "RSA sign failure.  No RSA sign will be done.\n");
3072             ERR_print_errors(bio_err);
3073             op_count = 1;
3074         } else {
3075             pkey_print_message("private", "rsa",
3076                                rsa_c[testnum][0], rsa_keys[testnum].bits,
3077                                seconds.rsa);
3078             /* RSA_blinding_on(rsa_key[testnum],NULL); */
3079             Time_F(START);
3080             count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
3081             d = Time_F(STOP);
3082             BIO_printf(bio_err,
3083                        mr ? "+R1:%ld:%d:%.2f\n"
3084                        : "%ld %u bits private RSA's in %.2fs\n",
3085                        count, rsa_keys[testnum].bits, d);
3086             rsa_results[testnum][0] = (double)count / d;
3087             op_count = count;
3088         }
3089
3090         for (i = 0; i < loopargs_len; i++) {
3091             st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3092                             loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3093             if (st <= 0)
3094                 break;
3095         }
3096         if (st <= 0) {
3097             BIO_printf(bio_err,
3098                        "RSA verify failure.  No RSA verify will be done.\n");
3099             ERR_print_errors(bio_err);
3100             rsa_doit[testnum] = 0;
3101         } else {
3102             pkey_print_message("public", "rsa",
3103                                rsa_c[testnum][1], rsa_keys[testnum].bits,
3104                                seconds.rsa);
3105             Time_F(START);
3106             count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
3107             d = Time_F(STOP);
3108             BIO_printf(bio_err,
3109                        mr ? "+R2:%ld:%d:%.2f\n"
3110                        : "%ld %u bits public RSA's in %.2fs\n",
3111                        count, rsa_keys[testnum].bits, d);
3112             rsa_results[testnum][1] = (double)count / d;
3113         }
3114
3115         if (op_count <= 1) {
3116             /* if longer than 10s, don't do any more */
3117             stop_it(rsa_doit, testnum);
3118         }
3119     }
3120 #endif  /* OPENSSL_NO_DEPRECATED_3_0 */
3121
3122     for (i = 0; i < loopargs_len; i++)
3123         if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3124             goto end;
3125
3126 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3127     for (testnum = 0; testnum < DSA_NUM; testnum++) {
3128         int st = 0;
3129         if (!dsa_doit[testnum])
3130             continue;
3131
3132         /* DSA_generate_key(dsa_key[testnum]); */
3133         /* DSA_sign_setup(dsa_key[testnum],NULL); */
3134         for (i = 0; i < loopargs_len; i++) {
3135             st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3136                           &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3137             if (st == 0)
3138                 break;
3139         }
3140         if (st == 0) {
3141             BIO_printf(bio_err,
3142                        "DSA sign failure.  No DSA sign will be done.\n");
3143             ERR_print_errors(bio_err);
3144             op_count = 1;
3145         } else {
3146             pkey_print_message("sign", "dsa",
3147                                dsa_c[testnum][0], dsa_bits[testnum],
3148                                seconds.dsa);
3149             Time_F(START);
3150             count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
3151             d = Time_F(STOP);
3152             BIO_printf(bio_err,
3153                        mr ? "+R3:%ld:%u:%.2f\n"
3154                        : "%ld %u bits DSA signs in %.2fs\n",
3155                        count, dsa_bits[testnum], d);
3156             dsa_results[testnum][0] = (double)count / d;
3157             op_count = count;
3158         }
3159
3160         for (i = 0; i < loopargs_len; i++) {
3161             st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3162                             loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3163             if (st <= 0)
3164                 break;
3165         }
3166         if (st <= 0) {
3167             BIO_printf(bio_err,
3168                        "DSA verify failure.  No DSA verify will be done.\n");
3169             ERR_print_errors(bio_err);
3170             dsa_doit[testnum] = 0;
3171         } else {
3172             pkey_print_message("verify", "dsa",
3173                                dsa_c[testnum][1], dsa_bits[testnum],
3174                                seconds.dsa);
3175             Time_F(START);
3176             count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
3177             d = Time_F(STOP);
3178             BIO_printf(bio_err,
3179                        mr ? "+R4:%ld:%u:%.2f\n"
3180                        : "%ld %u bits DSA verify in %.2fs\n",
3181                        count, dsa_bits[testnum], d);
3182             dsa_results[testnum][1] = (double)count / d;
3183         }
3184
3185         if (op_count <= 1) {
3186             /* if longer than 10s, don't do any more */
3187             stop_it(dsa_doit, testnum);
3188         }
3189     }
3190 #endif                          /* OPENSSL_NO_DSA */
3191
3192 #ifndef OPENSSL_NO_EC
3193 # ifndef OPENSSL_NO_DEPRECATED_3_0
3194     for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
3195         int st = 1;
3196
3197         if (!ecdsa_doit[testnum])
3198             continue;           /* Ignore Curve */
3199         for (i = 0; i < loopargs_len; i++) {
3200             loopargs[i].ecdsa[testnum] =
3201                 EC_KEY_new_by_curve_name(ec_curves[testnum].nid);
3202             if (loopargs[i].ecdsa[testnum] == NULL) {
3203                 st = 0;
3204                 break;
3205             }
3206         }
3207         if (st == 0) {
3208             BIO_printf(bio_err, "ECDSA failure.\n");
3209             ERR_print_errors(bio_err);
3210             op_count = 1;
3211         } else {
3212             for (i = 0; i < loopargs_len; i++) {
3213                 /* Perform ECDSA signature test */
3214                 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
3215                 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3216                                 &loopargs[i].siglen,
3217                                 loopargs[i].ecdsa[testnum]);
3218                 if (st == 0)
3219                     break;
3220             }
3221             if (st == 0) {
3222                 BIO_printf(bio_err,
3223                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
3224                 ERR_print_errors(bio_err);
3225                 op_count = 1;
3226             } else {
3227                 pkey_print_message("sign", "ecdsa",
3228                                    ecdsa_c[testnum][0],
3229                                    ec_curves[testnum].bits, seconds.ecdsa);
3230                 Time_F(START);
3231                 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
3232                 d = Time_F(STOP);
3233
3234                 BIO_printf(bio_err,
3235                            mr ? "+R5:%ld:%u:%.2f\n" :
3236                            "%ld %u bits ECDSA signs in %.2fs \n",
3237                            count, ec_curves[testnum].bits, d);
3238                 ecdsa_results[testnum][0] = (double)count / d;
3239                 op_count = count;
3240             }
3241
3242             /* Perform ECDSA verification test */
3243             for (i = 0; i < loopargs_len; i++) {
3244                 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3245                                   loopargs[i].siglen,
3246                                   loopargs[i].ecdsa[testnum]);
3247                 if (st != 1)
3248                     break;
3249             }
3250             if (st != 1) {
3251                 BIO_printf(bio_err,
3252                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
3253                 ERR_print_errors(bio_err);
3254                 ecdsa_doit[testnum] = 0;
3255             } else {
3256                 pkey_print_message("verify", "ecdsa",
3257                                    ecdsa_c[testnum][1],
3258                                    ec_curves[testnum].bits, seconds.ecdsa);
3259                 Time_F(START);
3260                 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
3261                 d = Time_F(STOP);
3262                 BIO_printf(bio_err,
3263                            mr ? "+R6:%ld:%u:%.2f\n"
3264                            : "%ld %u bits ECDSA verify in %.2fs\n",
3265                            count, ec_curves[testnum].bits, d);
3266                 ecdsa_results[testnum][1] = (double)count / d;
3267             }
3268
3269             if (op_count <= 1) {
3270                 /* if longer than 10s, don't do any more */
3271                 stop_it(ecdsa_doit, testnum);
3272             }
3273         }
3274     }
3275 # endif /* OPENSSL_NO_DEPRECATED_3_0 */
3276
3277     for (testnum = 0; testnum < EC_NUM; testnum++) {
3278         int ecdh_checks = 1;
3279
3280         if (!ecdh_doit[testnum])
3281             continue;
3282
3283         for (i = 0; i < loopargs_len; i++) {
3284             EVP_PKEY_CTX *kctx = NULL;
3285             EVP_PKEY_CTX *test_ctx = NULL;
3286             EVP_PKEY_CTX *ctx = NULL;
3287             EVP_PKEY *key_A = NULL;
3288             EVP_PKEY *key_B = NULL;
3289             size_t outlen;
3290             size_t test_outlen;
3291
3292             /* Ensure that the error queue is empty */
3293             if (ERR_peek_error()) {
3294                 BIO_printf(bio_err,
3295                            "WARNING: the error queue contains previous unhandled errors.\n");
3296                 ERR_print_errors(bio_err);
3297             }
3298
3299             /* Let's try to create a ctx directly from the NID: this works for
3300              * curves like Curve25519 that are not implemented through the low
3301              * level EC interface.
3302              * If this fails we try creating a EVP_PKEY_EC generic param ctx,
3303              * then we set the curve by NID before deriving the actual keygen
3304              * ctx for that specific curve. */
3305             kctx = EVP_PKEY_CTX_new_id(ec_curves[testnum].nid, NULL); /* keygen ctx from NID */
3306             if (!kctx) {
3307                 EVP_PKEY_CTX *pctx = NULL;
3308                 EVP_PKEY *params = NULL;
3309
3310                 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
3311                  * "int_ctx_new:unsupported algorithm" error was added to the
3312                  * error queue.
3313                  * We remove it from the error queue as we are handling it. */
3314                 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
3315                 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
3316                     /* check that the error origin matches */
3317                     ERR_GET_LIB(error) == ERR_LIB_EVP &&
3318                     ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
3319                     ERR_get_error(); /* pop error from queue */
3320                 if (ERR_peek_error()) {
3321                     BIO_printf(bio_err,
3322                                "Unhandled error in the error queue during ECDH init.\n");
3323                     ERR_print_errors(bio_err);
3324                     op_count = 1;
3325                     break;
3326                 }
3327
3328                 /* Create the context for parameter generation */
3329                 if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
3330                        /* Initialise the parameter generation */
3331                        !EVP_PKEY_paramgen_init(pctx) ||
3332                        /* Set the curve by NID */
3333                        !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3334                                                                ec_curves
3335                                                                [testnum].nid) ||
3336                        /* Create the parameter object params */
3337                        !EVP_PKEY_paramgen(pctx, &params)) {
3338                     ecdh_checks = 0;
3339                     BIO_printf(bio_err, "ECDH EC params init failure.\n");
3340                     ERR_print_errors(bio_err);
3341                     op_count = 1;
3342                     break;
3343                 }
3344                 /* Create the context for the key generation */
3345                 kctx = EVP_PKEY_CTX_new(params, NULL);
3346
3347                 EVP_PKEY_free(params);
3348                 params = NULL;
3349                 EVP_PKEY_CTX_free(pctx);
3350                 pctx = NULL;
3351             }
3352             if (kctx == NULL ||      /* keygen ctx is not null */
3353                 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
3354                 ecdh_checks = 0;
3355                 BIO_printf(bio_err, "ECDH keygen failure.\n");
3356                 ERR_print_errors(bio_err);
3357                 op_count = 1;
3358                 break;
3359             }
3360
3361             if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
3362                 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
3363                 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
3364                 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
3365                 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
3366                 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
3367                 outlen == 0 ||  /* ensure outlen is a valid size */
3368                 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
3369                 ecdh_checks = 0;
3370                 BIO_printf(bio_err, "ECDH key generation failure.\n");
3371                 ERR_print_errors(bio_err);
3372                 op_count = 1;
3373                 break;
3374             }
3375
3376             /* Here we perform a test run, comparing the output of a*B and b*A;
3377              * we try this here and assume that further EVP_PKEY_derive calls
3378              * never fail, so we can skip checks in the actually benchmarked
3379              * code, for maximum performance. */
3380             if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
3381                 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
3382                 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
3383                 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
3384                 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
3385                 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
3386                 test_outlen != outlen /* compare output length */ ) {
3387                 ecdh_checks = 0;
3388                 BIO_printf(bio_err, "ECDH computation failure.\n");
3389                 ERR_print_errors(bio_err);
3390                 op_count = 1;
3391                 break;
3392             }
3393
3394             /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
3395             if (CRYPTO_memcmp(loopargs[i].secret_a,
3396                               loopargs[i].secret_b, outlen)) {
3397                 ecdh_checks = 0;
3398                 BIO_printf(bio_err, "ECDH computations don't match.\n");
3399                 ERR_print_errors(bio_err);
3400                 op_count = 1;
3401                 break;
3402             }
3403
3404             loopargs[i].ecdh_ctx[testnum] = ctx;
3405             loopargs[i].outlen[testnum] = outlen;
3406
3407             EVP_PKEY_free(key_A);
3408             EVP_PKEY_free(key_B);
3409             EVP_PKEY_CTX_free(kctx);
3410             kctx = NULL;
3411             EVP_PKEY_CTX_free(test_ctx);
3412             test_ctx = NULL;
3413         }
3414         if (ecdh_checks != 0) {
3415             pkey_print_message("", "ecdh",
3416                                ecdh_c[testnum][0],
3417                                ec_curves[testnum].bits, seconds.ecdh);
3418             Time_F(START);
3419             count =
3420                 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
3421             d = Time_F(STOP);
3422             BIO_printf(bio_err,
3423                        mr ? "+R7:%ld:%d:%.2f\n" :
3424                        "%ld %u-bits ECDH ops in %.2fs\n", count,
3425                        ec_curves[testnum].bits, d);
3426             ecdh_results[testnum][0] = (double)count / d;
3427             op_count = count;
3428         }
3429
3430         if (op_count <= 1) {
3431             /* if longer than 10s, don't do any more */
3432             stop_it(ecdh_doit, testnum);
3433         }
3434     }
3435
3436     for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
3437         int st = 1;
3438         EVP_PKEY *ed_pkey = NULL;
3439         EVP_PKEY_CTX *ed_pctx = NULL;
3440
3441         if (!eddsa_doit[testnum])
3442             continue;           /* Ignore Curve */
3443         for (i = 0; i < loopargs_len; i++) {
3444             loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
3445             if (loopargs[i].eddsa_ctx[testnum] == NULL) {
3446                 st = 0;
3447                 break;
3448             }
3449             loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
3450             if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
3451                 st = 0;
3452                 break;
3453             }
3454
3455             if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid, NULL))
3456                     == NULL
3457                 || EVP_PKEY_keygen_init(ed_pctx) <= 0
3458                 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
3459                 st = 0;
3460                 EVP_PKEY_CTX_free(ed_pctx);
3461                 break;
3462             }
3463             EVP_PKEY_CTX_free(ed_pctx);
3464
3465             if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
3466                                     NULL, ed_pkey)) {
3467                 st = 0;
3468                 EVP_PKEY_free(ed_pkey);
3469                 break;
3470             }
3471            if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
3472                                      NULL, NULL, ed_pkey)) {
3473                 st = 0;
3474                 EVP_PKEY_free(ed_pkey);
3475                 break;
3476             }
3477
3478             EVP_PKEY_free(ed_pkey);
3479             ed_pkey = NULL;
3480         }
3481         if (st == 0) {
3482             BIO_printf(bio_err, "EdDSA failure.\n");
3483             ERR_print_errors(bio_err);
3484             op_count = 1;
3485         } else {
3486             for (i = 0; i < loopargs_len; i++) {
3487                 /* Perform EdDSA signature test */
3488                 loopargs[i].sigsize = ed_curves[testnum].sigsize;
3489                 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
3490                                     loopargs[i].buf2, &loopargs[i].sigsize,
3491                                     loopargs[i].buf, 20);
3492                 if (st == 0)
3493                     break;
3494             }
3495             if (st == 0) {
3496                 BIO_printf(bio_err,
3497                            "EdDSA sign failure.  No EdDSA sign will be done.\n");
3498                 ERR_print_errors(bio_err);
3499                 op_count = 1;
3500             } else {
3501                 pkey_print_message("sign", ed_curves[testnum].name,
3502                                    eddsa_c[testnum][0],
3503                                    ed_curves[testnum].bits, seconds.eddsa);
3504                 Time_F(START);
3505                 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
3506                 d = Time_F(STOP);
3507
3508                 BIO_printf(bio_err,
3509                            mr ? "+R8:%ld:%u:%s:%.2f\n" :
3510                            "%ld %u bits %s signs in %.2fs \n",
3511                            count, ed_curves[testnum].bits,
3512                            ed_curves[testnum].name, d);
3513                 eddsa_results[testnum][0] = (double)count / d;
3514                 op_count = count;
3515             }
3516             /* Perform EdDSA verification test */
3517             for (i = 0; i < loopargs_len; i++) {
3518                 st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
3519                                       loopargs[i].buf2, loopargs[i].sigsize,
3520                                       loopargs[i].buf, 20);
3521                 if (st != 1)
3522                     break;
3523             }
3524             if (st != 1) {
3525                 BIO_printf(bio_err,
3526                            "EdDSA verify failure.  No EdDSA verify will be done.\n");
3527                 ERR_print_errors(bio_err);
3528                 eddsa_doit[testnum] = 0;
3529             } else {
3530                 pkey_print_message("verify", ed_curves[testnum].name,
3531                                    eddsa_c[testnum][1],
3532                                    ed_curves[testnum].bits, seconds.eddsa);
3533                 Time_F(START);
3534                 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
3535                 d = Time_F(STOP);
3536                 BIO_printf(bio_err,
3537                            mr ? "+R9:%ld:%u:%s:%.2f\n"
3538                            : "%ld %u bits %s verify in %.2fs\n",
3539                            count, ed_curves[testnum].bits,
3540                            ed_curves[testnum].name, d);
3541                 eddsa_results[testnum][1] = (double)count / d;
3542             }
3543
3544             if (op_count <= 1) {
3545                 /* if longer than 10s, don't do any more */
3546                 stop_it(eddsa_doit, testnum);
3547             }
3548         }
3549     }
3550
3551 # ifndef OPENSSL_NO_SM2
3552     for (testnum = 0; testnum < SM2_NUM; testnum++) {
3553         int st = 1;
3554         EVP_PKEY *sm2_pkey = NULL;
3555
3556         if (!sm2_doit[testnum])
3557             continue;           /* Ignore Curve */
3558         /* Init signing and verification */
3559         for (i = 0; i < loopargs_len; i++) {
3560             EVP_PKEY_CTX *sm2_pctx = NULL;
3561             EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
3562             EVP_PKEY_CTX *pctx = NULL;
3563             st = 0;
3564
3565             loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
3566             loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
3567             if (loopargs[i].sm2_ctx[testnum] == NULL
3568                     || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
3569                 break;
3570
3571             sm2_pkey = NULL;
3572
3573             st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL)) == NULL
3574                 || EVP_PKEY_keygen_init(pctx) <= 0
3575                 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3576                     sm2_curves[testnum].nid) <= 0
3577                 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
3578             EVP_PKEY_CTX_free(pctx);
3579             if (st == 0)
3580                 break;
3581
3582             st = 0; /* set back to zero */
3583             /* attach it sooner to rely on main final cleanup */
3584             loopargs[i].sm2_pkey[testnum] = sm2_pkey;
3585             loopargs[i].sigsize = EVP_PKEY_size(sm2_pkey);
3586
3587             sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3588             sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3589             if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
3590                 EVP_PKEY_CTX_free(sm2_vfy_pctx);
3591                 break;
3592             }
3593
3594             /* attach them directly to respective ctx */
3595             EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
3596             EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
3597
3598             /*
3599              * No need to allow user to set an explicit ID here, just use
3600              * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
3601              */
3602             if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
3603                 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
3604                 break;
3605
3606             if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
3607                                     EVP_sm3(), NULL, sm2_pkey))
3608                 break;
3609             if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
3610                                       EVP_sm3(), NULL, sm2_pkey))
3611                 break;
3612             st = 1;         /* mark loop as succeeded */
3613         }
3614         if (st == 0) {
3615             BIO_printf(bio_err, "SM2 init failure.\n");
3616             ERR_print_errors(bio_err);
3617             op_count = 1;
3618         } else {
3619             for (i = 0; i < loopargs_len; i++) {
3620                 /* Perform SM2 signature test */
3621                 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
3622                                     loopargs[i].buf2, &loopargs[i].sigsize,
3623                                     loopargs[i].buf, 20);
3624                 if (st == 0)
3625                     break;
3626             }
3627             if (st == 0) {
3628                 BIO_printf(bio_err,
3629                            "SM2 sign failure.  No SM2 sign will be done.\n");
3630                 ERR_print_errors(bio_err);
3631                 op_count = 1;
3632             } else {
3633                 pkey_print_message("sign", sm2_curves[testnum].name,
3634                                    sm2_c[testnum][0],
3635                                    sm2_curves[testnum].bits, seconds.sm2);
3636                 Time_F(START);
3637                 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
3638                 d = Time_F(STOP);
3639
3640                 BIO_printf(bio_err,
3641                            mr ? "+R10:%ld:%u:%s:%.2f\n" :
3642                            "%ld %u bits %s signs in %.2fs \n",
3643                            count, sm2_curves[testnum].bits,
3644                            sm2_curves[testnum].name, d);
3645                 sm2_results[testnum][0] = (double)count / d;
3646                 op_count = count;
3647             }
3648
3649             /* Perform SM2 verification test */
3650             for (i = 0; i < loopargs_len; i++) {
3651                 st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
3652                                       loopargs[i].buf2, loopargs[i].sigsize,
3653                                       loopargs[i].buf, 20);
3654                 if (st != 1)
3655                     break;
3656             }
3657             if (st != 1) {
3658                 BIO_printf(bio_err,
3659                            "SM2 verify failure.  No SM2 verify will be done.\n");
3660                 ERR_print_errors(bio_err);
3661                 sm2_doit[testnum] = 0;
3662             } else {
3663                 pkey_print_message("verify", sm2_curves[testnum].name,
3664                                    sm2_c[testnum][1],
3665                                    sm2_curves[testnum].bits, seconds.sm2);
3666                 Time_F(START);
3667                 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
3668                 d = Time_F(STOP);
3669                 BIO_printf(bio_err,
3670                            mr ? "+R11:%ld:%u:%s:%.2f\n"
3671                            : "%ld %u bits %s verify in %.2fs\n",
3672                            count, sm2_curves[testnum].bits,
3673                            sm2_curves[testnum].name, d);
3674                 sm2_results[testnum][1] = (double)count / d;
3675             }
3676
3677             if (op_count <= 1) {
3678                 /* if longer than 10s, don't do any more */
3679                 for (testnum++; testnum < SM2_NUM; testnum++)
3680                     sm2_doit[testnum] = 0;
3681             }
3682         }
3683     }
3684 # endif                         /* OPENSSL_NO_SM2 */
3685 #endif                          /* OPENSSL_NO_EC */
3686
3687 #ifndef OPENSSL_NO_DH
3688     for (testnum = 0; testnum < FFDH_NUM; testnum++) {
3689         int ffdh_checks = 1;
3690
3691         if (!ffdh_doit[testnum])
3692             continue;
3693
3694         for (i = 0; i < loopargs_len; i++) {
3695             EVP_PKEY *pkey_A = NULL;
3696             EVP_PKEY *pkey_B = NULL;
3697             EVP_PKEY_CTX *ffdh_ctx = NULL;
3698             EVP_PKEY_CTX *test_ctx = NULL;
3699             size_t secret_size;
3700             size_t test_out;
3701
3702             /* Ensure that the error queue is empty */
3703             if (ERR_peek_error()) {
3704                 BIO_printf(bio_err,
3705                            "WARNING: the error queue contains previous unhandled errors.\n");
3706                 ERR_print_errors(bio_err);
3707             }
3708
3709             pkey_A = EVP_PKEY_new();
3710             if (!pkey_A) {
3711                 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3712                 ERR_print_errors(bio_err);
3713                 op_count = 1;
3714                 ffdh_checks = 0;
3715                 break;
3716             }
3717             pkey_B = EVP_PKEY_new();
3718             if (!pkey_B) {
3719                 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3720                 ERR_print_errors(bio_err);
3721                 op_count = 1;
3722                 ffdh_checks = 0;
3723                 break;
3724             }
3725
3726             ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
3727             if (!ffdh_ctx) {
3728                 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3729                 ERR_print_errors(bio_err);
3730                 op_count = 1;
3731                 ffdh_checks = 0;
3732                 break;
3733             }
3734
3735             if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
3736                 BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
3737                 ERR_print_errors(bio_err);
3738                 op_count = 1;
3739                 ffdh_checks = 0;
3740                 break;
3741             }
3742             if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
3743                 BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
3744                 ERR_print_errors(bio_err);
3745                 op_count = 1;
3746                 ffdh_checks = 0;
3747                 break;
3748             }
3749
3750             if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
3751                 EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
3752                 BIO_printf(bio_err, "FFDH key generation failure.\n");
3753                 ERR_print_errors(bio_err);
3754                 op_count = 1;
3755                 ffdh_checks = 0;
3756                 break;
3757             }
3758
3759             EVP_PKEY_CTX_free(ffdh_ctx);
3760
3761             /* check if the derivation works correctly both ways so that
3762              * we know if future derive calls will fail, and we can skip
3763              * error checking in benchmarked code */
3764             ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
3765             if (!ffdh_ctx) {
3766                 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3767                 ERR_print_errors(bio_err);
3768                 op_count = 1;
3769                 ffdh_checks = 0;
3770                 break;
3771             }
3772             if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
3773                 BIO_printf(bio_err, "FFDH derivation context init failure.\n");
3774                 ERR_print_errors(bio_err);
3775                 op_count = 1;
3776                 ffdh_checks = 0;
3777                 break;
3778             }
3779             if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
3780                 BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
3781                 ERR_print_errors(bio_err);
3782                 op_count = 1;
3783                 ffdh_checks = 0;
3784                 break;
3785             }
3786             if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
3787                 BIO_printf(bio_err, "Checking size of shared secret failed.\n");
3788                 ERR_print_errors(bio_err);
3789                 op_count = 1;
3790                 ffdh_checks = 0;
3791                 break;
3792             }
3793             if (secret_size > MAX_FFDH_SIZE) {
3794                 BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
3795                 op_count = 1;
3796                 ffdh_checks = 0;
3797                 break;
3798             }
3799             if (EVP_PKEY_derive(ffdh_ctx,
3800                                 loopargs[i].secret_ff_a,
3801                                 &secret_size) <= 0) {
3802                 BIO_printf(bio_err, "Shared secret derive failure.\n");
3803                 ERR_print_errors(bio_err);
3804                 op_count = 1;
3805                 ffdh_checks = 0;
3806                 break;
3807             }
3808             /* Now check from side B */
3809             test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
3810             if (!test_ctx) {
3811                 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3812                 ERR_print_errors(bio_err);
3813                 op_count = 1;
3814                 ffdh_checks = 0;
3815                 break;
3816             }
3817             if (!EVP_PKEY_derive_init(test_ctx) ||
3818                 !EVP_PKEY_derive_set_peer(test_ctx, pkey_A) ||
3819                 !EVP_PKEY_derive(test_ctx, NULL, &test_out) ||
3820                 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) ||
3821                 test_out != secret_size) {
3822                 BIO_printf(bio_err, "FFDH computation failure.\n");
3823                 op_count = 1;
3824                 ffdh_checks = 0;
3825                 break;
3826             }
3827
3828             /* compare the computed secrets */
3829             if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
3830                               loopargs[i].secret_ff_b, secret_size)) {
3831                 BIO_printf(bio_err, "FFDH computations don't match.\n");
3832                 ERR_print_errors(bio_err);
3833                 op_count = 1;
3834                 ffdh_checks = 0;
3835                 break;
3836             }
3837
3838             loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
3839
3840             EVP_PKEY_free(pkey_A);
3841             pkey_A = NULL;
3842             EVP_PKEY_free(pkey_B);
3843             pkey_B = NULL;
3844             EVP_PKEY_CTX_free(test_ctx);
3845             test_ctx = NULL;
3846         }
3847         if (ffdh_checks != 0) {
3848             pkey_print_message("", "ffdh", ffdh_c[testnum][0],
3849                                ffdh_params[testnum].bits, seconds.ffdh);
3850             Time_F(START);
3851             count =
3852                 run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
3853             d = Time_F(STOP);
3854             BIO_printf(bio_err,
3855                        mr ? "+R12:%ld:%d:%.2f\n" :
3856                        "%ld %u-bits FFDH ops in %.2fs\n", count,
3857                        ffdh_params[testnum].bits, d);
3858             ffdh_results[testnum][0] = (double)count / d;
3859             op_count = count;
3860         };
3861         if (op_count <= 1) {
3862             /* if longer than 10s, don't do any more */
3863             stop_it(ffdh_doit, testnum);
3864         }
3865     }
3866 #endif  /* OPENSSL_NO_DH */
3867 #ifndef NO_FORK
3868  show_res:
3869 #endif
3870     if (!mr) {
3871         printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
3872         printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
3873         printf("options:");
3874         printf("%s ", BN_options());
3875 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3876         printf("%s ", MD2_options());
3877 #endif
3878 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3879         printf("%s ", RC4_options());
3880 #endif
3881 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3882         printf("%s ", DES_options());
3883 #endif
3884 #ifndef OPENSSL_NO_DEPRECATED_3_0
3885         printf("%s ", AES_options());
3886 #endif
3887 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3888         printf("%s ", IDEA_options());
3889 #endif
3890 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3891         printf("%s ", BF_options());
3892 #endif
3893         printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
3894         printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
3895     }
3896
3897     if (pr_header) {
3898         if (mr)
3899             printf("+H");
3900         else {
3901             printf
3902                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
3903             printf("type        ");
3904         }
3905         for (testnum = 0; testnum < size_num; testnum++)
3906             printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
3907         printf("\n");
3908     }
3909
3910     for (k = 0; k < ALGOR_NUM; k++) {
3911         if (!doit[k])
3912             continue;
3913         if (mr)
3914             printf("+F:%u:%s", k, names[k]);
3915         else
3916             printf("%-13s", names[k]);
3917         for (testnum = 0; testnum < size_num; testnum++) {
3918             if (results[k][testnum] > 10000 && !mr)
3919                 printf(" %11.2fk", results[k][testnum] / 1e3);
3920             else
3921                 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
3922         }
3923         printf("\n");
3924     }
3925 #ifndef OPENSSL_NO_DEPRECATED_3_0
3926     testnum = 1;
3927     for (k = 0; k < RSA_NUM; k++) {
3928         if (!rsa_doit[k])
3929             continue;
3930         if (testnum && !mr) {
3931             printf("%18ssign    verify    sign/s verify/s\n", " ");
3932             testnum = 0;
3933         }
3934         if (mr)
3935             printf("+F2:%u:%u:%f:%f\n",
3936                    k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1]);
3937         else
3938             printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3939                    rsa_keys[k].bits, 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
3940                    rsa_results[k][0], rsa_results[k][1]);
3941     }
3942 #endif
3943 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3944     testnum = 1;
3945     for (k = 0; k < DSA_NUM; k++) {
3946         if (!dsa_doit[k])
3947             continue;
3948         if (testnum && !mr) {
3949             printf("%18ssign    verify    sign/s verify/s\n", " ");
3950             testnum = 0;
3951         }
3952         if (mr)
3953             printf("+F3:%u:%u:%f:%f\n",
3954                    k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
3955         else
3956             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3957                    dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
3958                    dsa_results[k][0], dsa_results[k][1]);
3959     }
3960 #endif
3961 #ifndef OPENSSL_NO_EC
3962     testnum = 1;
3963     for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
3964         if (!ecdsa_doit[k])
3965             continue;
3966         if (testnum && !mr) {
3967             printf("%30ssign    verify    sign/s verify/s\n", " ");
3968             testnum = 0;
3969         }
3970
3971         if (mr)
3972             printf("+F4:%u:%u:%f:%f\n",
3973                    k, ec_curves[k].bits,
3974                    ecdsa_results[k][0], ecdsa_results[k][1]);
3975         else
3976             printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3977                    ec_curves[k].bits, ec_curves[k].name,
3978                    1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
3979                    ecdsa_results[k][0], ecdsa_results[k][1]);
3980     }
3981
3982     testnum = 1;
3983     for (k = 0; k < EC_NUM; k++) {
3984         if (!ecdh_doit[k])
3985             continue;
3986         if (testnum && !mr) {
3987             printf("%30sop      op/s\n", " ");
3988             testnum = 0;
3989         }
3990         if (mr)
3991             printf("+F5:%u:%u:%f:%f\n",
3992                    k, ec_curves[k].bits,
3993                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3994
3995         else
3996             printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
3997                    ec_curves[k].bits, ec_curves[k].name,
3998                    1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3999     }
4000
4001     testnum = 1;
4002     for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
4003         if (!eddsa_doit[k])
4004             continue;
4005         if (testnum && !mr) {
4006             printf("%30ssign    verify    sign/s verify/s\n", " ");
4007             testnum = 0;
4008         }
4009
4010         if (mr)
4011             printf("+F6:%u:%u:%s:%f:%f\n",
4012                    k, ed_curves[k].bits, ed_curves[k].name,
4013                    eddsa_results[k][0], eddsa_results[k][1]);
4014         else
4015             printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
4016                    ed_curves[k].bits, ed_curves[k].name,
4017                    1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
4018                    eddsa_results[k][0], eddsa_results[k][1]);
4019     }
4020
4021 # ifndef OPENSSL_NO_SM2
4022     testnum = 1;
4023     for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
4024         if (!sm2_doit[k])
4025             continue;
4026         if (testnum && !mr) {
4027             printf("%30ssign    verify    sign/s verify/s\n", " ");
4028             testnum = 0;
4029         }
4030
4031         if (mr)
4032             printf("+F7:%u:%u:%s:%f:%f\n",
4033                    k, sm2_curves[k].bits, sm2_curves[k].name,
4034                    sm2_results[k][0], sm2_results[k][1]);
4035         else
4036             printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
4037                    sm2_curves[k].bits, sm2_curves[k].name,
4038                    1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
4039                    sm2_results[k][0], sm2_results[k][1]);
4040     }
4041 # endif
4042 #endif                          /* OPENSSL_NO_EC */
4043 #ifndef OPENSSL_NO_DH
4044     testnum = 1;
4045     for (k = 0; k < FFDH_NUM; k++) {
4046         if (!ffdh_doit[k])
4047             continue;
4048         if (testnum && !mr) {
4049             printf("%23sop     op/s\n", " ");
4050             testnum = 0;
4051         }
4052         if (mr)
4053             printf("+F8:%u:%u:%f:%f\n",
4054                    k, ffdh_params[k].bits,
4055                    ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
4056
4057         else
4058             printf("%4u bits ffdh %8.4fs %8.1f\n",
4059                    ffdh_params[k].bits,
4060                    1.0 / ffdh_results[k][0], ffdh_results[k][0]);
4061     }
4062 #endif /* OPENSSL_NO_DH */
4063
4064     ret = 0;
4065
4066  end:
4067     ERR_print_errors(bio_err);
4068     for (i = 0; i < loopargs_len; i++) {
4069         OPENSSL_free(loopargs[i].buf_malloc);
4070         OPENSSL_free(loopargs[i].buf2_malloc);
4071
4072 #ifndef OPENSSL_NO_DEPRECATED_3_0
4073         for (k = 0; k < RSA_NUM; k++)
4074             RSA_free(loopargs[i].rsa_key[k]);
4075 #endif
4076 #ifndef OPENSSL_NO_DH
4077         OPENSSL_free(loopargs[i].secret_ff_a);
4078         OPENSSL_free(loopargs[i].secret_ff_b);
4079         for (k = 0; k < FFDH_NUM; k++) {
4080             EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
4081         }
4082 #endif
4083 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4084         for (k = 0; k < DSA_NUM; k++)
4085             DSA_free(loopargs[i].dsa_key[k]);
4086 #endif
4087 #ifndef OPENSSL_NO_EC
4088 # if !defined(OPENSSL_NO_DEPRECATED_3_0)
4089         for (k = 0; k < ECDSA_NUM; k++)
4090             EC_KEY_free(loopargs[i].ecdsa[k]);
4091 # endif
4092         for (k = 0; k < EC_NUM; k++)
4093             EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
4094         for (k = 0; k < EdDSA_NUM; k++) {
4095             EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
4096             EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
4097         }
4098 # ifndef OPENSSL_NO_SM2
4099         for (k = 0; k < SM2_NUM; k++) {
4100             EVP_PKEY_CTX *pctx = NULL;
4101
4102             /* free signing ctx */
4103             if (loopargs[i].sm2_ctx[k] != NULL
4104                     && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
4105                 EVP_PKEY_CTX_free(pctx);
4106             EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
4107             /* free verification ctx */
4108             if (loopargs[i].sm2_vfy_ctx[k] != NULL
4109                     && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
4110                 EVP_PKEY_CTX_free(pctx);
4111             EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
4112             /* free pkey */
4113             EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
4114         }
4115 # endif
4116         OPENSSL_free(loopargs[i].secret_a);
4117         OPENSSL_free(loopargs[i].secret_b);
4118 #endif
4119     }
4120 #ifndef OPENSSL_NO_DEPRECATED_3_0
4121     OPENSSL_free(evp_hmac_name);
4122 #endif
4123 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4124     OPENSSL_free(evp_cmac_name);
4125 #endif
4126
4127     if (async_jobs > 0) {
4128         for (i = 0; i < loopargs_len; i++)
4129             ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
4130     }
4131
4132     if (async_init) {
4133         ASYNC_cleanup_thread();
4134     }
4135     OPENSSL_free(loopargs);
4136     release_engine(e);
4137     if (fetched_alg) {
4138         EVP_MD_free(evp_md);
4139         EVP_CIPHER_free(evp_cipher);
4140     }
4141     return ret;
4142 }
4143
4144 static void print_message(const char *s, long num, int length, int tm)
4145 {
4146 #ifdef SIGALRM
4147     BIO_printf(bio_err,
4148                mr ? "+DT:%s:%d:%d\n"
4149                : "Doing %s for %ds on %d size blocks: ", s, tm, length);
4150     (void)BIO_flush(bio_err);
4151     run = 1;
4152     alarm(tm);
4153 #else
4154     BIO_printf(bio_err,
4155                mr ? "+DN:%s:%ld:%d\n"
4156                : "Doing %s %ld times on %d size blocks: ", s, num, length);
4157     (void)BIO_flush(bio_err);
4158 #endif
4159 }
4160
4161 #if !defined(OPENSSL_NO_DEPRECATED_3_0)         \
4162     || !defined(OPENSSL_NO_DSA)                 \
4163     || !defined(OPENSSL_NO_DH)                  \
4164     || !defined(OPENSSL_NO_EC)
4165 static void pkey_print_message(const char *str, const char *str2, long num,
4166                                unsigned int bits, int tm)
4167 {
4168 # ifdef SIGALRM
4169     BIO_printf(bio_err,
4170                mr ? "+DTP:%d:%s:%s:%d\n"
4171                : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
4172     (void)BIO_flush(bio_err);
4173     run = 1;
4174     alarm(tm);
4175 # else
4176     BIO_printf(bio_err,
4177                mr ? "+DNP:%ld:%d:%s:%s\n"
4178                : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
4179     (void)BIO_flush(bio_err);
4180 # endif
4181 }
4182 #endif
4183
4184 static void print_result(int alg, int run_no, int count, double time_used)
4185 {
4186     if (count == -1) {
4187         BIO_printf(bio_err, "%s error!\n", names[alg]);
4188         ERR_print_errors(bio_err);
4189         /* exit(1);  disable exit until default provider enabled */
4190         return;
4191     }
4192     BIO_printf(bio_err,
4193                mr ? "+R:%d:%s:%f\n"
4194                : "%d %s's in %.2fs\n", count, names[alg], time_used);
4195     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
4196 }
4197
4198 #ifndef NO_FORK
4199 static char *sstrsep(char **string, const char *delim)
4200 {
4201     char isdelim[256];
4202     char *token = *string;
4203
4204     if (**string == 0)
4205         return NULL;
4206
4207     memset(isdelim, 0, sizeof(isdelim));
4208     isdelim[0] = 1;
4209
4210     while (*delim) {
4211         isdelim[(unsigned char)(*delim)] = 1;
4212         delim++;
4213     }
4214
4215     while (!isdelim[(unsigned char)(**string)]) {
4216         (*string)++;
4217     }
4218
4219     if (**string) {
4220         **string = 0;
4221         (*string)++;
4222     }
4223
4224     return token;
4225 }
4226
4227 static int do_multi(int multi, int size_num)
4228 {
4229     int n;
4230     int fd[2];
4231     int *fds;
4232     static char sep[] = ":";
4233
4234     fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
4235     for (n = 0; n < multi; ++n) {
4236         if (pipe(fd) == -1) {
4237             BIO_printf(bio_err, "pipe failure\n");
4238             exit(1);
4239         }
4240         fflush(stdout);
4241         (void)BIO_flush(bio_err);
4242         if (fork()) {
4243             close(fd[1]);
4244             fds[n] = fd[0];
4245         } else {
4246             close(fd[0]);
4247             close(1);
4248             if (dup(fd[1]) == -1) {
4249                 BIO_printf(bio_err, "dup failed\n");
4250                 exit(1);
4251             }
4252             close(fd[1]);
4253             mr = 1;
4254             usertime = 0;
4255             OPENSSL_free(fds);
4256             return 0;
4257         }
4258         printf("Forked child %d\n", n);
4259     }
4260
4261     /* for now, assume the pipe is long enough to take all the output */
4262     for (n = 0; n < multi; ++n) {
4263         FILE *f;
4264         char buf[1024];
4265         char *p;
4266
4267         f = fdopen(fds[n], "r");
4268         while (fgets(buf, sizeof(buf), f)) {
4269             p = strchr(buf, '\n');
4270             if (p)
4271                 *p = '\0';
4272             if (buf[0] != '+') {
4273                 BIO_printf(bio_err,
4274                            "Don't understand line '%s' from child %d\n", buf,
4275                            n);
4276                 continue;
4277             }
4278             printf("Got: %s from %d\n", buf, n);
4279             if (strncmp(buf, "+F:", 3) == 0) {
4280                 int alg;
4281                 int j;
4282
4283                 p = buf + 3;
4284                 alg = atoi(sstrsep(&p, sep));
4285                 sstrsep(&p, sep);
4286                 for (j = 0; j < size_num; ++j)
4287                     results[alg][j] += atof(sstrsep(&p, sep));
4288             }
4289 #ifndef OPENSSL_NO_DEPRECATED_3_0
4290             else if (strncmp(buf, "+F2:", 4) == 0) {
4291                 int k;
4292                 double d;
4293
4294                 p = buf + 4;
4295                 k = atoi(sstrsep(&p, sep));
4296                 sstrsep(&p, sep);
4297
4298                 d = atof(sstrsep(&p, sep));
4299                 rsa_results[k][0] += d;
4300
4301                 d = atof(sstrsep(&p, sep));
4302                 rsa_results[k][1] += d;
4303             }
4304 #endif
4305 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4306             else if (strncmp(buf, "+F3:", 4) == 0) {
4307                 int k;
4308                 double d;
4309
4310                 p = buf + 4;
4311                 k = atoi(sstrsep(&p, sep));
4312                 sstrsep(&p, sep);
4313
4314                 d = atof(sstrsep(&p, sep));
4315                 dsa_results[k][0] += d;
4316
4317                 d = atof(sstrsep(&p, sep));
4318                 dsa_results[k][1] += d;
4319             }
4320 # endif
4321 # ifndef OPENSSL_NO_EC
4322             else if (strncmp(buf, "+F4:", 4) == 0) {
4323                 int k;
4324                 double d;
4325
4326                 p = buf + 4;
4327                 k = atoi(sstrsep(&p, sep));
4328                 sstrsep(&p, sep);
4329
4330                 d = atof(sstrsep(&p, sep));
4331                 ecdsa_results[k][0] += d;
4332
4333                 d = atof(sstrsep(&p, sep));
4334                 ecdsa_results[k][1] += d;
4335             } else if (strncmp(buf, "+F5:", 4) == 0) {
4336                 int k;
4337                 double d;
4338
4339                 p = buf + 4;
4340                 k = atoi(sstrsep(&p, sep));
4341                 sstrsep(&p, sep);
4342
4343                 d = atof(sstrsep(&p, sep));
4344                 ecdh_results[k][0] += d;
4345             } else if (strncmp(buf, "+F6:", 4) == 0) {
4346                 int k;
4347                 double d;
4348
4349                 p = buf + 4;
4350                 k = atoi(sstrsep(&p, sep));
4351                 sstrsep(&p, sep);
4352                 sstrsep(&p, sep);
4353
4354                 d = atof(sstrsep(&p, sep));
4355                 eddsa_results[k][0] += d;
4356
4357                 d = atof(sstrsep(&p, sep));
4358                 eddsa_results[k][1] += d;
4359             }
4360 #  ifndef OPENSSL_NO_SM2
4361             else if (strncmp(buf, "+F7:", 4) == 0) {
4362                 int k;
4363                 double d;
4364
4365                 p = buf + 4;
4366                 k = atoi(sstrsep(&p, sep));
4367                 sstrsep(&p, sep);
4368                 sstrsep(&p, sep);
4369
4370                 d = atof(sstrsep(&p, sep));
4371                 sm2_results[k][0] += d;
4372
4373                 d = atof(sstrsep(&p, sep));
4374                 sm2_results[k][1] += d;
4375             }
4376 #  endif /* OPENSSL_NO_SM2 */
4377 # endif /* OPENSSL_NO_EC */
4378 # ifndef OPENSSL_NO_DH
4379             else if (strncmp(buf, "+F8:", 4) == 0) {
4380                 int k;
4381                 double d;
4382
4383                 p = buf + 4;
4384                 k = atoi(sstrsep(&p, sep));
4385                 sstrsep(&p, sep);
4386
4387                 d = atof(sstrsep(&p, sep));
4388                 ffdh_results[k][0] += d;
4389             }
4390 # endif /* OPENSSL_NO_DH */
4391
4392             else if (strncmp(buf, "+H:", 3) == 0) {
4393                 ;
4394             } else
4395                 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
4396                            n);
4397         }
4398
4399         fclose(f);
4400     }
4401     OPENSSL_free(fds);
4402     return 1;
4403 }
4404 #endif
4405
4406 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
4407                              const openssl_speed_sec_t *seconds)
4408 {
4409     static const int mblengths_list[] =
4410         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
4411     const int *mblengths = mblengths_list;
4412     int j, count, keylen, num = OSSL_NELEM(mblengths_list);
4413     const char *alg_name;
4414     unsigned char *inp, *out, *key, no_key[32], no_iv[16];
4415     EVP_CIPHER_CTX *ctx;
4416     double d = 0.0;
4417
4418     if (lengths_single) {
4419         mblengths = &lengths_single;
4420         num = 1;
4421     }
4422
4423     inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
4424     out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
4425     ctx = EVP_CIPHER_CTX_new();
4426     EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
4427
4428     keylen = EVP_CIPHER_CTX_key_length(ctx);
4429     key = app_malloc(keylen, "evp_cipher key");
4430     EVP_CIPHER_CTX_rand_key(ctx, key);
4431     EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
4432     OPENSSL_clear_free(key, keylen);
4433
4434     EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
4435     alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
4436
4437     for (j = 0; j < num; j++) {
4438         print_message(alg_name, 0, mblengths[j], seconds->sym);
4439         Time_F(START);
4440         for (count = 0; run && count < 0x7fffffff; count++) {
4441             unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
4442             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
4443             size_t len = mblengths[j];
4444             int packlen;
4445
4446             memset(aad, 0, 8);  /* avoid uninitialized values */
4447             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
4448             aad[9] = 3;         /* version */
4449             aad[10] = 2;
4450             aad[11] = 0;        /* length */
4451             aad[12] = 0;
4452             mb_param.out = NULL;
4453             mb_param.inp = aad;
4454             mb_param.len = len;
4455             mb_param.interleave = 8;
4456
4457             packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
4458                                           sizeof(mb_param), &mb_param);
4459
4460             if (packlen > 0) {
4461                 mb_param.out = out;
4462                 mb_param.inp = inp;
4463                 mb_param.len = len;
4464                 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
4465                                     sizeof(mb_param), &mb_param);
4466             } else {
4467                 int pad;
4468
4469                 RAND_bytes(out, 16);
4470                 len += 16;
4471                 aad[11] = (unsigned char)(len >> 8);
4472                 aad[12] = (unsigned char)(len);
4473                 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
4474                                           EVP_AEAD_TLS1_AAD_LEN, aad);
4475                 EVP_Cipher(ctx, out, inp, len + pad);
4476             }
4477         }
4478         d = Time_F(STOP);
4479         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
4480                    : "%d %s's in %.2fs\n", count, "evp", d);
4481         results[D_EVP][j] = ((double)count) / d * mblengths[j];
4482     }
4483
4484     if (mr) {
4485         fprintf(stdout, "+H");
4486         for (j = 0; j < num; j++)
4487             fprintf(stdout, ":%d", mblengths[j]);
4488         fprintf(stdout, "\n");
4489         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
4490         for (j = 0; j < num; j++)
4491             fprintf(stdout, ":%.2f", results[D_EVP][j]);
4492         fprintf(stdout, "\n");
4493     } else {
4494         fprintf(stdout,
4495                 "The 'numbers' are in 1000s of bytes per second processed.\n");
4496         fprintf(stdout, "type                    ");
4497         for (j = 0; j < num; j++)
4498             fprintf(stdout, "%7d bytes", mblengths[j]);
4499         fprintf(stdout, "\n");
4500         fprintf(stdout, "%-24s", alg_name);
4501
4502         for (j = 0; j < num; j++) {
4503             if (results[D_EVP][j] > 10000)
4504                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
4505             else
4506                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
4507         }
4508         fprintf(stdout, "\n");
4509     }
4510
4511     OPENSSL_free(inp);
4512     OPENSSL_free(out);
4513     EVP_CIPHER_CTX_free(ctx);
4514 }