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