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