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