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