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