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