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