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