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