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