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