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