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