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