Previously this x509 command line was working, restore that
[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     size_t loop;
1245
1246     /* What follows are the buffers and key material. */
1247 #ifndef OPENSSL_NO_RC5
1248     RC5_32_KEY rc5_ks;
1249 #endif
1250 #ifndef OPENSSL_NO_RC2
1251     RC2_KEY rc2_ks;
1252 #endif
1253 #ifndef OPENSSL_NO_IDEA
1254     IDEA_KEY_SCHEDULE idea_ks;
1255 #endif
1256 #ifndef OPENSSL_NO_SEED
1257     SEED_KEY_SCHEDULE seed_ks;
1258 #endif
1259 #ifndef OPENSSL_NO_BF
1260     BF_KEY bf_ks;
1261 #endif
1262 #ifndef OPENSSL_NO_CAST
1263     CAST_KEY cast_ks;
1264 #endif
1265     static const unsigned char key16[16] = {
1266         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1267         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1268     };
1269     static const unsigned char key24[24] = {
1270         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1271         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1272         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1273     };
1274     static const unsigned char key32[32] = {
1275         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1276         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1277         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1278         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1279     };
1280 #ifndef OPENSSL_NO_CAMELLIA
1281     static const unsigned char ckey24[24] = {
1282         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1283         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1284         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1285     };
1286     static const unsigned char ckey32[32] = {
1287         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1288         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1289         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1290         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1291     };
1292     CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1293 #endif
1294 #ifndef OPENSSL_NO_DES
1295     static DES_cblock key = {
1296         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
1297     };
1298     static DES_cblock key2 = {
1299         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1300     };
1301     static DES_cblock key3 = {
1302         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1303     };
1304 #endif
1305 #ifndef OPENSSL_NO_RSA
1306     static const unsigned int rsa_bits[RSA_NUM] = {
1307         512, 1024, 2048, 3072, 4096, 7680, 15360
1308     };
1309     static const unsigned char *rsa_data[RSA_NUM] = {
1310         test512, test1024, test2048, test3072, test4096, test7680, test15360
1311     };
1312     static const int rsa_data_length[RSA_NUM] = {
1313         sizeof(test512), sizeof(test1024),
1314         sizeof(test2048), sizeof(test3072),
1315         sizeof(test4096), sizeof(test7680),
1316         sizeof(test15360)
1317     };
1318     int rsa_doit[RSA_NUM] = { 0 };
1319 #endif
1320 #ifndef OPENSSL_NO_DSA
1321     static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1322     int dsa_doit[DSA_NUM] = { 0 };
1323 #endif
1324 #ifndef OPENSSL_NO_EC
1325     /*
1326      * We only test over the following curves as they are representative, To
1327      * add tests over more curves, simply add the curve NID and curve name to
1328      * the following arrays and increase the EC_NUM value accordingly.
1329      */
1330     static const unsigned int test_curves[EC_NUM] = {
1331         /* Prime Curves */
1332         NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
1333         NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
1334         /* Binary Curves */
1335         NID_sect163k1, NID_sect233k1, NID_sect283k1,
1336         NID_sect409k1, NID_sect571k1, NID_sect163r2,
1337         NID_sect233r1, NID_sect283r1, NID_sect409r1,
1338         NID_sect571r1,
1339         /* Other */
1340         NID_X25519
1341     };
1342     static const char *test_curves_names[EC_NUM] = {
1343         /* Prime Curves */
1344         "secp160r1", "nistp192", "nistp224",
1345         "nistp256", "nistp384", "nistp521",
1346         /* Binary Curves */
1347         "nistk163", "nistk233", "nistk283",
1348         "nistk409", "nistk571", "nistb163",
1349         "nistb233", "nistb283", "nistb409",
1350         "nistb571",
1351         /* Other */
1352         "X25519"
1353     };
1354     static const int test_curves_bits[EC_NUM] = {
1355         160, 192, 224,
1356         256, 384, 521,
1357         163, 233, 283,
1358         409, 571, 163,
1359         233, 283, 409,
1360         571, 253 /* X25519 */
1361     };
1362
1363     int ecdsa_doit[EC_NUM] = { 0 };
1364     int ecdh_doit[EC_NUM] = { 0 };
1365 #endif                          /* ndef OPENSSL_NO_EC */
1366
1367     prog = opt_init(argc, argv, speed_options);
1368     while ((o = opt_next()) != OPT_EOF) {
1369         switch (o) {
1370         case OPT_EOF:
1371         case OPT_ERR:
1372  opterr:
1373             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1374             goto end;
1375         case OPT_HELP:
1376             opt_help(speed_options);
1377             ret = 0;
1378             goto end;
1379         case OPT_ELAPSED:
1380             usertime = 0;
1381             break;
1382         case OPT_EVP:
1383             evp_md = NULL;
1384             evp_cipher = EVP_get_cipherbyname(opt_arg());
1385             if (evp_cipher == NULL)
1386                 evp_md = EVP_get_digestbyname(opt_arg());
1387             if (evp_cipher == NULL && evp_md == NULL) {
1388                 BIO_printf(bio_err,
1389                            "%s: %s is an unknown cipher or digest\n",
1390                            prog, opt_arg());
1391                 goto end;
1392             }
1393             doit[D_EVP] = 1;
1394             break;
1395         case OPT_DECRYPT:
1396             decrypt = 1;
1397             break;
1398         case OPT_ENGINE:
1399             /*
1400              * In a forked execution, an engine might need to be
1401              * initialised by each child process, not by the parent.
1402              * So store the name here and run setup_engine() later on.
1403              */
1404             engine_id = opt_arg();
1405             break;
1406         case OPT_MULTI:
1407 #ifndef NO_FORK
1408             multi = atoi(opt_arg());
1409 #endif
1410             break;
1411         case OPT_ASYNCJOBS:
1412 #ifndef OPENSSL_NO_ASYNC
1413             async_jobs = atoi(opt_arg());
1414             if (!ASYNC_is_capable()) {
1415                 BIO_printf(bio_err,
1416                            "%s: async_jobs specified but async not supported\n",
1417                            prog);
1418                 goto opterr;
1419             }
1420             if (async_jobs > 99999) {
1421                 BIO_printf(bio_err,
1422                            "%s: too many async_jobs\n",
1423                            prog);
1424                 goto opterr;
1425             }
1426 #endif
1427             break;
1428         case OPT_MISALIGN:
1429             if (!opt_int(opt_arg(), &misalign))
1430                 goto end;
1431             if (misalign > MISALIGN) {
1432                 BIO_printf(bio_err,
1433                            "%s: Maximum offset is %d\n", prog, MISALIGN);
1434                 goto opterr;
1435             }
1436             break;
1437         case OPT_MR:
1438             mr = 1;
1439             break;
1440         case OPT_MB:
1441             multiblock = 1;
1442 #ifdef OPENSSL_NO_MULTIBLOCK
1443             BIO_printf(bio_err,
1444                        "%s: -mb specified but multi-block support is disabled\n",
1445                        prog);
1446             goto end;
1447 #endif
1448             break;
1449         }
1450     }
1451     argc = opt_num_rest();
1452     argv = opt_rest();
1453
1454     /* Remaining arguments are algorithms. */
1455     for ( ; *argv; argv++) {
1456         if (found(*argv, doit_choices, &i)) {
1457             doit[i] = 1;
1458             continue;
1459         }
1460 #ifndef OPENSSL_NO_DES
1461         if (strcmp(*argv, "des") == 0) {
1462             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1463             continue;
1464         }
1465 #endif
1466         if (strcmp(*argv, "sha") == 0) {
1467             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1468             continue;
1469         }
1470 #ifndef OPENSSL_NO_RSA
1471         if (strcmp(*argv, "openssl") == 0)
1472             continue;
1473         if (strcmp(*argv, "rsa") == 0) {
1474             rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
1475                 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
1476                 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
1477                 rsa_doit[R_RSA_15360] = 1;
1478             continue;
1479         }
1480         if (found(*argv, rsa_choices, &i)) {
1481             rsa_doit[i] = 1;
1482             continue;
1483         }
1484 #endif
1485 #ifndef OPENSSL_NO_DSA
1486         if (strcmp(*argv, "dsa") == 0) {
1487             dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1488                 dsa_doit[R_DSA_2048] = 1;
1489             continue;
1490         }
1491         if (found(*argv, dsa_choices, &i)) {
1492             dsa_doit[i] = 2;
1493             continue;
1494         }
1495 #endif
1496         if (strcmp(*argv, "aes") == 0) {
1497             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =
1498                 doit[D_CBC_256_AES] = 1;
1499             continue;
1500         }
1501 #ifndef OPENSSL_NO_CAMELLIA
1502         if (strcmp(*argv, "camellia") == 0) {
1503             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =
1504                 doit[D_CBC_256_CML] = 1;
1505             continue;
1506         }
1507 #endif
1508 #ifndef OPENSSL_NO_EC
1509         if (strcmp(*argv, "ecdsa") == 0) {
1510             for (loop = 0; loop < OSSL_NELEM(ecdsa_choices); loop++)
1511                 ecdsa_doit[ecdsa_choices[loop].retval] = 1;
1512             continue;
1513         }
1514         if (found(*argv, ecdsa_choices, &i)) {
1515             ecdsa_doit[i] = 2;
1516             continue;
1517         }
1518         if (strcmp(*argv, "ecdh") == 0) {
1519             for (loop = 0; loop < OSSL_NELEM(ecdh_choices); loop++)
1520                 ecdh_doit[ecdh_choices[loop].retval] = 1;
1521             continue;
1522         }
1523         if (found(*argv, ecdh_choices, &i)) {
1524             ecdh_doit[i] = 2;
1525             continue;
1526         }
1527 #endif
1528         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1529         goto end;
1530     }
1531
1532     /* Initialize the job pool if async mode is enabled */
1533     if (async_jobs > 0) {
1534         async_init = ASYNC_init_thread(async_jobs, async_jobs);
1535         if (!async_init) {
1536             BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1537             goto end;
1538         }
1539     }
1540
1541     loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1542     loopargs = app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1543     memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1544
1545     for (i = 0; i < loopargs_len; i++) {
1546         if (async_jobs > 0) {
1547             loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1548             if (loopargs[i].wait_ctx == NULL) {
1549                 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1550                 goto end;
1551             }
1552         }
1553
1554         loopargs[i].buf_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1555         loopargs[i].buf2_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1556         /* Align the start of buffers on a 64 byte boundary */
1557         loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1558         loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1559 #ifndef OPENSSL_NO_EC
1560         loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1561         loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1562 #endif
1563     }
1564
1565 #ifndef NO_FORK
1566     if (multi && do_multi(multi))
1567         goto show_res;
1568 #endif
1569
1570     /* Initialize the engine after the fork */
1571     e = setup_engine(engine_id, 0);
1572
1573     /* No parameters; turn on everything. */
1574     if ((argc == 0) && !doit[D_EVP]) {
1575         for (i = 0; i < ALGOR_NUM; i++)
1576             if (i != D_EVP)
1577                 doit[i] = 1;
1578 #ifndef OPENSSL_NO_RSA
1579         for (i = 0; i < RSA_NUM; i++)
1580             rsa_doit[i] = 1;
1581 #endif
1582 #ifndef OPENSSL_NO_DSA
1583         for (i = 0; i < DSA_NUM; i++)
1584             dsa_doit[i] = 1;
1585 #endif
1586 #ifndef OPENSSL_NO_EC
1587         for (loop = 0; loop < OSSL_NELEM(ecdsa_choices); loop++)
1588             ecdsa_doit[ecdsa_choices[loop].retval] = 1;
1589         for (loop = 0; loop < OSSL_NELEM(ecdh_choices); loop++)
1590             ecdh_doit[ecdh_choices[loop].retval] = 1;
1591 #endif
1592     }
1593     for (i = 0; i < ALGOR_NUM; i++)
1594         if (doit[i])
1595             pr_header++;
1596
1597     if (usertime == 0 && !mr)
1598         BIO_printf(bio_err,
1599                    "You have chosen to measure elapsed time "
1600                    "instead of user CPU time.\n");
1601
1602 #ifndef OPENSSL_NO_RSA
1603     for (i = 0; i < loopargs_len; i++) {
1604         for (k = 0; k < RSA_NUM; k++) {
1605             const unsigned char *p;
1606
1607             p = rsa_data[k];
1608             loopargs[i].rsa_key[k] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1609             if (loopargs[i].rsa_key[k] == NULL) {
1610                 BIO_printf(bio_err, "internal error loading RSA key number %d\n",
1611                         k);
1612                 goto end;
1613             }
1614         }
1615     }
1616 #endif
1617 #ifndef OPENSSL_NO_DSA
1618     for (i = 0; i < loopargs_len; i++) {
1619         loopargs[i].dsa_key[0] = get_dsa512();
1620         loopargs[i].dsa_key[1] = get_dsa1024();
1621         loopargs[i].dsa_key[2] = get_dsa2048();
1622     }
1623 #endif
1624 #ifndef OPENSSL_NO_DES
1625     DES_set_key_unchecked(&key, &sch);
1626     DES_set_key_unchecked(&key2, &sch2);
1627     DES_set_key_unchecked(&key3, &sch3);
1628 #endif
1629     AES_set_encrypt_key(key16, 128, &aes_ks1);
1630     AES_set_encrypt_key(key24, 192, &aes_ks2);
1631     AES_set_encrypt_key(key32, 256, &aes_ks3);
1632 #ifndef OPENSSL_NO_CAMELLIA
1633     Camellia_set_key(key16, 128, &camellia_ks1);
1634     Camellia_set_key(ckey24, 192, &camellia_ks2);
1635     Camellia_set_key(ckey32, 256, &camellia_ks3);
1636 #endif
1637 #ifndef OPENSSL_NO_IDEA
1638     IDEA_set_encrypt_key(key16, &idea_ks);
1639 #endif
1640 #ifndef OPENSSL_NO_SEED
1641     SEED_set_key(key16, &seed_ks);
1642 #endif
1643 #ifndef OPENSSL_NO_RC4
1644     RC4_set_key(&rc4_ks, 16, key16);
1645 #endif
1646 #ifndef OPENSSL_NO_RC2
1647     RC2_set_key(&rc2_ks, 16, key16, 128);
1648 #endif
1649 #ifndef OPENSSL_NO_RC5
1650     RC5_32_set_key(&rc5_ks, 16, key16, 12);
1651 #endif
1652 #ifndef OPENSSL_NO_BF
1653     BF_set_key(&bf_ks, 16, key16);
1654 #endif
1655 #ifndef OPENSSL_NO_CAST
1656     CAST_set_key(&cast_ks, 16, key16);
1657 #endif
1658 #ifndef SIGALRM
1659 # ifndef OPENSSL_NO_DES
1660     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1661     count = 10;
1662     do {
1663         long it;
1664         count *= 2;
1665         Time_F(START);
1666         for (it = count; it; it--)
1667             DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
1668                             (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1669         d = Time_F(STOP);
1670     } while (d < 3);
1671     save_count = count;
1672     c[D_MD2][0] = count / 10;
1673     c[D_MDC2][0] = count / 10;
1674     c[D_MD4][0] = count;
1675     c[D_MD5][0] = count;
1676     c[D_HMAC][0] = count;
1677     c[D_SHA1][0] = count;
1678     c[D_RMD160][0] = count;
1679     c[D_RC4][0] = count * 5;
1680     c[D_CBC_DES][0] = count;
1681     c[D_EDE3_DES][0] = count / 3;
1682     c[D_CBC_IDEA][0] = count;
1683     c[D_CBC_SEED][0] = count;
1684     c[D_CBC_RC2][0] = count;
1685     c[D_CBC_RC5][0] = count;
1686     c[D_CBC_BF][0] = count;
1687     c[D_CBC_CAST][0] = count;
1688     c[D_CBC_128_AES][0] = count;
1689     c[D_CBC_192_AES][0] = count;
1690     c[D_CBC_256_AES][0] = count;
1691     c[D_CBC_128_CML][0] = count;
1692     c[D_CBC_192_CML][0] = count;
1693     c[D_CBC_256_CML][0] = count;
1694     c[D_SHA256][0] = count;
1695     c[D_SHA512][0] = count;
1696     c[D_WHIRLPOOL][0] = count;
1697     c[D_IGE_128_AES][0] = count;
1698     c[D_IGE_192_AES][0] = count;
1699     c[D_IGE_256_AES][0] = count;
1700     c[D_GHASH][0] = count;
1701
1702     for (i = 1; i < SIZE_NUM; i++) {
1703         long l0, l1;
1704
1705         l0 = (long)lengths[0];
1706         l1 = (long)lengths[i];
1707
1708         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1709         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1710         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1711         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1712         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1713         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1714         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1715         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1716         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1717         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1718         c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1719
1720         l0 = (long)lengths[i - 1];
1721
1722         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1723         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1724         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1725         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1726         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1727         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1728         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1729         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1730         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1731         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1732         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1733         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1734         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1735         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1736         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1737         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1738         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1739         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1740     }
1741
1742 #  ifndef OPENSSL_NO_RSA
1743     rsa_c[R_RSA_512][0] = count / 2000;
1744     rsa_c[R_RSA_512][1] = count / 400;
1745     for (i = 1; i < RSA_NUM; i++) {
1746         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1747         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1748         if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
1749             rsa_doit[i] = 0;
1750         else {
1751             if (rsa_c[i][0] == 0) {
1752                 rsa_c[i][0] = 1;            /* Set minimum iteration Nb to 1. */
1753                 rsa_c[i][1] = 20;
1754             }
1755         }
1756     }
1757 #  endif
1758
1759 #  ifndef OPENSSL_NO_DSA
1760     dsa_c[R_DSA_512][0] = count / 1000;
1761     dsa_c[R_DSA_512][1] = count / 1000 / 2;
1762     for (i = 1; i < DSA_NUM; i++) {
1763         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1764         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1765         if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
1766             dsa_doit[i] = 0;
1767         else {
1768             if (dsa_c[i][0] == 0) {
1769                 dsa_c[i][0] = 1;            /* Set minimum iteration Nb to 1. */
1770                 dsa_c[i][1] = 1;
1771             }
1772         }
1773     }
1774 #  endif
1775
1776 #  ifndef OPENSSL_NO_EC
1777     ecdsa_c[R_EC_P160][0] = count / 1000;
1778     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1779     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1780         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1781         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1782         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1783             ecdsa_doit[i] = 0;
1784         else {
1785             if (ecdsa_c[i][0] == 0) {
1786                 ecdsa_c[i][0] = 1;
1787                 ecdsa_c[i][1] = 1;
1788             }
1789         }
1790     }
1791     ecdsa_c[R_EC_K163][0] = count / 1000;
1792     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1793     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1794         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1795         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1796         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1797             ecdsa_doit[i] = 0;
1798         else {
1799             if (ecdsa_c[i][0] == 0) {
1800                 ecdsa_c[i][0] = 1;
1801                 ecdsa_c[i][1] = 1;
1802             }
1803         }
1804     }
1805     ecdsa_c[R_EC_B163][0] = count / 1000;
1806     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1807     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1808         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1809         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1810         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1811             ecdsa_doit[i] = 0;
1812         else {
1813             if (ecdsa_c[i][0] == 0) {
1814                 ecdsa_c[i][0] = 1;
1815                 ecdsa_c[i][1] = 1;
1816             }
1817         }
1818     }
1819
1820     ecdh_c[R_EC_P160][0] = count / 1000;
1821     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1822         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1823         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1824             ecdh_doit[i] = 0;
1825         else {
1826             if (ecdh_c[i][0] == 0) {
1827                 ecdh_c[i][0] = 1;
1828             }
1829         }
1830     }
1831     ecdh_c[R_EC_K163][0] = count / 1000;
1832     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1833         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1834         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1835             ecdh_doit[i] = 0;
1836         else {
1837             if (ecdh_c[i][0] == 0) {
1838                 ecdh_c[i][0] = 1;
1839             }
1840         }
1841     }
1842     ecdh_c[R_EC_B163][0] = count / 1000;
1843     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1844         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1845         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1846             ecdh_doit[i] = 0;
1847         else {
1848             if (ecdh_c[i][0] == 0) {
1849                 ecdh_c[i][0] = 1;
1850             }
1851         }
1852     }
1853 #  endif
1854
1855 # else
1856 /* not worth fixing */
1857 #  error "You cannot disable DES on systems without SIGALRM."
1858 # endif                        /* OPENSSL_NO_DES */
1859 #else
1860 # ifndef _WIN32
1861     signal(SIGALRM, sig_done);
1862 # endif
1863 #endif                         /* SIGALRM */
1864
1865 #ifndef OPENSSL_NO_MD2
1866     if (doit[D_MD2]) {
1867         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1868             print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
1869             Time_F(START);
1870             count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1871             d = Time_F(STOP);
1872             print_result(D_MD2, testnum, count, d);
1873         }
1874     }
1875 #endif
1876 #ifndef OPENSSL_NO_MDC2
1877     if (doit[D_MDC2]) {
1878         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1879             print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
1880             Time_F(START);
1881             count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1882             d = Time_F(STOP);
1883             print_result(D_MDC2, testnum, count, d);
1884         }
1885     }
1886 #endif
1887
1888 #ifndef OPENSSL_NO_MD4
1889     if (doit[D_MD4]) {
1890         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1891             print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
1892             Time_F(START);
1893             count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1894             d = Time_F(STOP);
1895             print_result(D_MD4, testnum, count, d);
1896         }
1897     }
1898 #endif
1899
1900 #ifndef OPENSSL_NO_MD5
1901     if (doit[D_MD5]) {
1902         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1903             print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);
1904             Time_F(START);
1905             count = run_benchmark(async_jobs, MD5_loop, loopargs);
1906             d = Time_F(STOP);
1907             print_result(D_MD5, testnum, count, d);
1908         }
1909     }
1910
1911     if (doit[D_HMAC]) {
1912         static const char hmac_key[] = "This is a key...";
1913         int len = strlen(hmac_key);
1914
1915         for (i = 0; i < loopargs_len; i++) {
1916             loopargs[i].hctx = HMAC_CTX_new();
1917             if (loopargs[i].hctx == NULL) {
1918                 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
1919                 exit(1);
1920             }
1921
1922             HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
1923         }
1924         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1925             print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);
1926             Time_F(START);
1927             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
1928             d = Time_F(STOP);
1929             print_result(D_HMAC, testnum, count, d);
1930         }
1931         for (i = 0; i < loopargs_len; i++) {
1932             HMAC_CTX_free(loopargs[i].hctx);
1933         }
1934     }
1935 #endif
1936     if (doit[D_SHA1]) {
1937         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1938             print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);
1939             Time_F(START);
1940             count = run_benchmark(async_jobs, SHA1_loop, loopargs);
1941             d = Time_F(STOP);
1942             print_result(D_SHA1, testnum, count, d);
1943         }
1944     }
1945     if (doit[D_SHA256]) {
1946         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1947             print_message(names[D_SHA256], c[D_SHA256][testnum], lengths[testnum]);
1948             Time_F(START);
1949             count = run_benchmark(async_jobs, SHA256_loop, loopargs);
1950             d = Time_F(STOP);
1951             print_result(D_SHA256, testnum, count, d);
1952         }
1953     }
1954     if (doit[D_SHA512]) {
1955         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1956             print_message(names[D_SHA512], c[D_SHA512][testnum], lengths[testnum]);
1957             Time_F(START);
1958             count = run_benchmark(async_jobs, SHA512_loop, loopargs);
1959             d = Time_F(STOP);
1960             print_result(D_SHA512, testnum, count, d);
1961         }
1962     }
1963
1964 #ifndef OPENSSL_NO_WHIRLPOOL
1965     if (doit[D_WHIRLPOOL]) {
1966         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1967             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum], lengths[testnum]);
1968             Time_F(START);
1969             count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
1970             d = Time_F(STOP);
1971             print_result(D_WHIRLPOOL, testnum, count, d);
1972         }
1973     }
1974 #endif
1975
1976 #ifndef OPENSSL_NO_RMD160
1977     if (doit[D_RMD160]) {
1978         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1979             print_message(names[D_RMD160], c[D_RMD160][testnum], lengths[testnum]);
1980             Time_F(START);
1981             count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
1982             d = Time_F(STOP);
1983             print_result(D_RMD160, testnum, count, d);
1984         }
1985     }
1986 #endif
1987 #ifndef OPENSSL_NO_RC4
1988     if (doit[D_RC4]) {
1989         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1990             print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
1991             Time_F(START);
1992             count = run_benchmark(async_jobs, RC4_loop, loopargs);
1993             d = Time_F(STOP);
1994             print_result(D_RC4, testnum, count, d);
1995         }
1996     }
1997 #endif
1998 #ifndef OPENSSL_NO_DES
1999     if (doit[D_CBC_DES]) {
2000         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2001             print_message(names[D_CBC_DES], c[D_CBC_DES][testnum], lengths[testnum]);
2002             Time_F(START);
2003             count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2004             d = Time_F(STOP);
2005             print_result(D_CBC_DES, testnum, count, d);
2006         }
2007     }
2008
2009     if (doit[D_EDE3_DES]) {
2010         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2011             print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum], lengths[testnum]);
2012             Time_F(START);
2013             count = run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2014             d = Time_F(STOP);
2015             print_result(D_EDE3_DES, testnum, count, d);
2016         }
2017     }
2018 #endif
2019
2020     if (doit[D_CBC_128_AES]) {
2021         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2022             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2023                           lengths[testnum]);
2024             Time_F(START);
2025             count = run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2026             d = Time_F(STOP);
2027             print_result(D_CBC_128_AES, testnum, count, d);
2028         }
2029     }
2030     if (doit[D_CBC_192_AES]) {
2031         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2032             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2033                           lengths[testnum]);
2034             Time_F(START);
2035             count = run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2036             d = Time_F(STOP);
2037             print_result(D_CBC_192_AES, testnum, count, d);
2038         }
2039     }
2040     if (doit[D_CBC_256_AES]) {
2041         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2042             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2043                           lengths[testnum]);
2044             Time_F(START);
2045             count = run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2046             d = Time_F(STOP);
2047             print_result(D_CBC_256_AES, testnum, count, d);
2048         }
2049     }
2050
2051     if (doit[D_IGE_128_AES]) {
2052         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2053             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2054                           lengths[testnum]);
2055             Time_F(START);
2056             count = run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2057             d = Time_F(STOP);
2058             print_result(D_IGE_128_AES, testnum, count, d);
2059         }
2060     }
2061     if (doit[D_IGE_192_AES]) {
2062         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2063             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2064                           lengths[testnum]);
2065             Time_F(START);
2066             count = run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2067             d = Time_F(STOP);
2068             print_result(D_IGE_192_AES, testnum, count, d);
2069         }
2070     }
2071     if (doit[D_IGE_256_AES]) {
2072         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2073             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2074                           lengths[testnum]);
2075             Time_F(START);
2076             count = run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2077             d = Time_F(STOP);
2078             print_result(D_IGE_256_AES, testnum, count, d);
2079         }
2080     }
2081     if (doit[D_GHASH]) {
2082         for (i = 0; i < loopargs_len; i++) {
2083             loopargs[i].gcm_ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2084             CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx, (unsigned char *)"0123456789ab", 12);
2085         }
2086
2087         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2088             print_message(names[D_GHASH], c[D_GHASH][testnum], lengths[testnum]);
2089             Time_F(START);
2090             count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2091             d = Time_F(STOP);
2092             print_result(D_GHASH, testnum, count, d);
2093         }
2094         for (i = 0; i < loopargs_len; i++)
2095             CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2096     }
2097
2098 #ifndef OPENSSL_NO_CAMELLIA
2099     if (doit[D_CBC_128_CML]) {
2100         if (async_jobs > 0) {
2101             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2102                        names[D_CBC_128_CML]);
2103             doit[D_CBC_128_CML] = 0;
2104         }
2105         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2106             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2107                           lengths[testnum]);
2108             Time_F(START);
2109             for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2110                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2111                                      (size_t)lengths[testnum], &camellia_ks1,
2112                                      iv, CAMELLIA_ENCRYPT);
2113             d = Time_F(STOP);
2114             print_result(D_CBC_128_CML, testnum, count, d);
2115         }
2116     }
2117     if (doit[D_CBC_192_CML]) {
2118         if (async_jobs > 0) {
2119             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2120                        names[D_CBC_192_CML]);
2121             doit[D_CBC_192_CML] = 0;
2122         }
2123         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2124             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2125                           lengths[testnum]);
2126             if (async_jobs > 0) {
2127                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2128                 exit(1);
2129             }
2130             Time_F(START);
2131             for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2132                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2133                                      (size_t)lengths[testnum], &camellia_ks2,
2134                                      iv, CAMELLIA_ENCRYPT);
2135             d = Time_F(STOP);
2136             print_result(D_CBC_192_CML, testnum, count, d);
2137         }
2138     }
2139     if (doit[D_CBC_256_CML]) {
2140         if (async_jobs > 0) {
2141             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2142                        names[D_CBC_256_CML]);
2143             doit[D_CBC_256_CML] = 0;
2144         }
2145         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2146             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2147                           lengths[testnum]);
2148             Time_F(START);
2149             for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2150                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2151                                      (size_t)lengths[testnum], &camellia_ks3,
2152                                      iv, CAMELLIA_ENCRYPT);
2153             d = Time_F(STOP);
2154             print_result(D_CBC_256_CML, testnum, count, d);
2155         }
2156     }
2157 #endif
2158 #ifndef OPENSSL_NO_IDEA
2159     if (doit[D_CBC_IDEA]) {
2160         if (async_jobs > 0) {
2161             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2162                        names[D_CBC_IDEA]);
2163             doit[D_CBC_IDEA] = 0;
2164         }
2165         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2166             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum], lengths[testnum]);
2167             Time_F(START);
2168             for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
2169                 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2170                                  (size_t)lengths[testnum], &idea_ks,
2171                                  iv, IDEA_ENCRYPT);
2172             d = Time_F(STOP);
2173             print_result(D_CBC_IDEA, testnum, count, d);
2174         }
2175     }
2176 #endif
2177 #ifndef OPENSSL_NO_SEED
2178     if (doit[D_CBC_SEED]) {
2179         if (async_jobs > 0) {
2180             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2181                        names[D_CBC_SEED]);
2182             doit[D_CBC_SEED] = 0;
2183         }
2184         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2185             print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum], lengths[testnum]);
2186             Time_F(START);
2187             for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2188                 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2189                                  (size_t)lengths[testnum], &seed_ks, iv, 1);
2190             d = Time_F(STOP);
2191             print_result(D_CBC_SEED, testnum, count, d);
2192         }
2193     }
2194 #endif
2195 #ifndef OPENSSL_NO_RC2
2196     if (doit[D_CBC_RC2]) {
2197         if (async_jobs > 0) {
2198             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2199                        names[D_CBC_RC2]);
2200             doit[D_CBC_RC2] = 0;
2201         }
2202         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2203             print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum], lengths[testnum]);
2204             if (async_jobs > 0) {
2205                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2206                 exit(1);
2207             }
2208             Time_F(START);
2209             for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2210                 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2211                                 (size_t)lengths[testnum], &rc2_ks,
2212                                 iv, RC2_ENCRYPT);
2213             d = Time_F(STOP);
2214             print_result(D_CBC_RC2, testnum, count, d);
2215         }
2216     }
2217 #endif
2218 #ifndef OPENSSL_NO_RC5
2219     if (doit[D_CBC_RC5]) {
2220         if (async_jobs > 0) {
2221             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2222                        names[D_CBC_RC5]);
2223             doit[D_CBC_RC5] = 0;
2224         }
2225         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2226             print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum], lengths[testnum]);
2227             if (async_jobs > 0) {
2228                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2229                 exit(1);
2230             }
2231             Time_F(START);
2232             for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2233                 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2234                                    (size_t)lengths[testnum], &rc5_ks,
2235                                    iv, RC5_ENCRYPT);
2236             d = Time_F(STOP);
2237             print_result(D_CBC_RC5, testnum, count, d);
2238         }
2239     }
2240 #endif
2241 #ifndef OPENSSL_NO_BF
2242     if (doit[D_CBC_BF]) {
2243         if (async_jobs > 0) {
2244             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2245                        names[D_CBC_BF]);
2246             doit[D_CBC_BF] = 0;
2247         }
2248         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2249             print_message(names[D_CBC_BF], c[D_CBC_BF][testnum], lengths[testnum]);
2250             Time_F(START);
2251             for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2252                 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2253                                (size_t)lengths[testnum], &bf_ks,
2254                                iv, BF_ENCRYPT);
2255             d = Time_F(STOP);
2256             print_result(D_CBC_BF, testnum, count, d);
2257         }
2258     }
2259 #endif
2260 #ifndef OPENSSL_NO_CAST
2261     if (doit[D_CBC_CAST]) {
2262         if (async_jobs > 0) {
2263             BIO_printf(bio_err, "Async mode is not supported with %s\n",
2264                        names[D_CBC_CAST]);
2265             doit[D_CBC_CAST] = 0;
2266         }
2267         for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2268             print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum], lengths[testnum]);
2269             Time_F(START);
2270             for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2271                 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2272                                  (size_t)lengths[testnum], &cast_ks,
2273                                  iv, CAST_ENCRYPT);
2274             d = Time_F(STOP);
2275             print_result(D_CBC_CAST, testnum, count, d);
2276         }
2277     }
2278 #endif
2279
2280     if (doit[D_EVP]) {
2281         if (multiblock && evp_cipher) {
2282             if (!
2283                 (EVP_CIPHER_flags(evp_cipher) &
2284                  EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2285                 BIO_printf(bio_err, "%s is not multi-block capable\n",
2286                            OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2287                 goto end;
2288             }
2289             if (async_jobs > 0) {
2290                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2291                 exit(1);
2292             }
2293             multiblock_speed(evp_cipher);
2294             ret = 0;
2295             goto end;
2296         }
2297         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2298             if (evp_cipher) {
2299
2300                 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2301                 /*
2302                  * -O3 -fschedule-insns messes up an optimization here!
2303                  * names[D_EVP] somehow becomes NULL
2304                  */
2305                 print_message(names[D_EVP], save_count, lengths[testnum]);
2306
2307                 for (k = 0; k < loopargs_len; k++) {
2308                     loopargs[k].ctx = EVP_CIPHER_CTX_new();
2309                     if (decrypt)
2310                         EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
2311                     else
2312                         EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
2313                     EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2314                 }
2315
2316                 Time_F(START);
2317                 count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
2318                 d = Time_F(STOP);
2319                 for (k = 0; k < loopargs_len; k++) {
2320                     EVP_CIPHER_CTX_free(loopargs[k].ctx);
2321                 }
2322             }
2323             if (evp_md) {
2324                 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2325                 print_message(names[D_EVP], save_count, lengths[testnum]);
2326                 Time_F(START);
2327                 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2328                 d = Time_F(STOP);
2329             }
2330             print_result(D_EVP, testnum, count, d);
2331         }
2332     }
2333
2334     for (i = 0; i < loopargs_len; i++)
2335         RAND_bytes(loopargs[i].buf, 36);
2336
2337 #ifndef OPENSSL_NO_RSA
2338     for (testnum = 0; testnum < RSA_NUM; testnum++) {
2339         int st = 0;
2340         if (!rsa_doit[testnum])
2341             continue;
2342         for (i = 0; i < loopargs_len; i++) {
2343             st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2344                           &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2345             if (st == 0)
2346                 break;
2347         }
2348         if (st == 0) {
2349             BIO_printf(bio_err,
2350                        "RSA sign failure.  No RSA sign will be done.\n");
2351             ERR_print_errors(bio_err);
2352             rsa_count = 1;
2353         } else {
2354             pkey_print_message("private", "rsa",
2355                                rsa_c[testnum][0], rsa_bits[testnum], RSA_SECONDS);
2356             /* RSA_blinding_on(rsa_key[testnum],NULL); */
2357             Time_F(START);
2358             count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2359             d = Time_F(STOP);
2360             BIO_printf(bio_err,
2361                        mr ? "+R1:%ld:%d:%.2f\n"
2362                        : "%ld %d bit private RSA's in %.2fs\n",
2363                        count, rsa_bits[testnum], d);
2364             rsa_results[testnum][0] = d / (double)count;
2365             rsa_count = count;
2366         }
2367
2368         for (i = 0; i < loopargs_len; i++) {
2369             st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2370                             loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2371             if (st <= 0)
2372                 break;
2373         }
2374         if (st <= 0) {
2375             BIO_printf(bio_err,
2376                        "RSA verify failure.  No RSA verify will be done.\n");
2377             ERR_print_errors(bio_err);
2378             rsa_doit[testnum] = 0;
2379         } else {
2380             pkey_print_message("public", "rsa",
2381                                rsa_c[testnum][1], rsa_bits[testnum], RSA_SECONDS);
2382             Time_F(START);
2383             count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2384             d = Time_F(STOP);
2385             BIO_printf(bio_err,
2386                        mr ? "+R2:%ld:%d:%.2f\n"
2387                        : "%ld %d bit public RSA's in %.2fs\n",
2388                        count, rsa_bits[testnum], d);
2389             rsa_results[testnum][1] = d / (double)count;
2390         }
2391
2392         if (rsa_count <= 1) {
2393             /* if longer than 10s, don't do any more */
2394             for (testnum++; testnum < RSA_NUM; testnum++)
2395                 rsa_doit[testnum] = 0;
2396         }
2397     }
2398 #endif                          /* OPENSSL_NO_RSA */
2399
2400     for (i = 0; i < loopargs_len; i++)
2401         RAND_bytes(loopargs[i].buf, 36);
2402
2403 #ifndef OPENSSL_NO_DSA
2404     if (RAND_status() != 1) {
2405         RAND_seed(rnd_seed, sizeof(rnd_seed));
2406     }
2407     for (testnum = 0; testnum < DSA_NUM; testnum++) {
2408         int st = 0;
2409         if (!dsa_doit[testnum])
2410             continue;
2411
2412         /* DSA_generate_key(dsa_key[testnum]); */
2413         /* DSA_sign_setup(dsa_key[testnum],NULL); */
2414         for (i = 0; i < loopargs_len; i++) {
2415             st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2416                           &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2417             if (st == 0)
2418                 break;
2419         }
2420         if (st == 0) {
2421             BIO_printf(bio_err,
2422                        "DSA sign failure.  No DSA sign will be done.\n");
2423             ERR_print_errors(bio_err);
2424             rsa_count = 1;
2425         } else {
2426             pkey_print_message("sign", "dsa",
2427                                dsa_c[testnum][0], dsa_bits[testnum], DSA_SECONDS);
2428             Time_F(START);
2429             count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2430             d = Time_F(STOP);
2431             BIO_printf(bio_err,
2432                        mr ? "+R3:%ld:%d:%.2f\n"
2433                        : "%ld %d bit DSA signs in %.2fs\n",
2434                        count, dsa_bits[testnum], d);
2435             dsa_results[testnum][0] = d / (double)count;
2436             rsa_count = count;
2437         }
2438
2439         for (i = 0; i < loopargs_len; i++) {
2440             st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2441                             loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2442             if (st <= 0)
2443                 break;
2444         }
2445         if (st <= 0) {
2446             BIO_printf(bio_err,
2447                        "DSA verify failure.  No DSA verify will be done.\n");
2448             ERR_print_errors(bio_err);
2449             dsa_doit[testnum] = 0;
2450         } else {
2451             pkey_print_message("verify", "dsa",
2452                                dsa_c[testnum][1], dsa_bits[testnum], DSA_SECONDS);
2453             Time_F(START);
2454             count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2455             d = Time_F(STOP);
2456             BIO_printf(bio_err,
2457                        mr ? "+R4:%ld:%d:%.2f\n"
2458                        : "%ld %d bit DSA verify in %.2fs\n",
2459                        count, dsa_bits[testnum], d);
2460             dsa_results[testnum][1] = d / (double)count;
2461         }
2462
2463         if (rsa_count <= 1) {
2464             /* if longer than 10s, don't do any more */
2465             for (testnum++; testnum < DSA_NUM; testnum++)
2466                 dsa_doit[testnum] = 0;
2467         }
2468     }
2469 #endif                          /* OPENSSL_NO_DSA */
2470
2471 #ifndef OPENSSL_NO_EC
2472     if (RAND_status() != 1) {
2473         RAND_seed(rnd_seed, sizeof(rnd_seed));
2474     }
2475     for (testnum = 0; testnum < EC_NUM; testnum++) {
2476         int st = 1;
2477
2478         if (!ecdsa_doit[testnum])
2479             continue;           /* Ignore Curve */
2480         for (i = 0; i < loopargs_len; i++) {
2481             loopargs[i].ecdsa[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2482             if (loopargs[i].ecdsa[testnum] == NULL) {
2483                 st = 0;
2484                 break;
2485             }
2486         }
2487         if (st == 0) {
2488             BIO_printf(bio_err, "ECDSA failure.\n");
2489             ERR_print_errors(bio_err);
2490             rsa_count = 1;
2491         } else {
2492             for (i = 0; i < loopargs_len; i++) {
2493                 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
2494                 /* Perform ECDSA signature test */
2495                 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
2496                 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2497                                 &loopargs[i].siglen, loopargs[i].ecdsa[testnum]);
2498                 if (st == 0)
2499                     break;
2500             }
2501             if (st == 0) {
2502                 BIO_printf(bio_err,
2503                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
2504                 ERR_print_errors(bio_err);
2505                 rsa_count = 1;
2506             } else {
2507                 pkey_print_message("sign", "ecdsa",
2508                                    ecdsa_c[testnum][0],
2509                                    test_curves_bits[testnum], ECDSA_SECONDS);
2510                 Time_F(START);
2511                 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2512                 d = Time_F(STOP);
2513
2514                 BIO_printf(bio_err,
2515                            mr ? "+R5:%ld:%d:%.2f\n" :
2516                            "%ld %d bit ECDSA signs in %.2fs \n",
2517                            count, test_curves_bits[testnum], d);
2518                 ecdsa_results[testnum][0] = d / (double)count;
2519                 rsa_count = count;
2520             }
2521
2522             /* Perform ECDSA verification test */
2523             for (i = 0; i < loopargs_len; i++) {
2524                 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2525                                   loopargs[i].siglen, loopargs[i].ecdsa[testnum]);
2526                 if (st != 1)
2527                     break;
2528             }
2529             if (st != 1) {
2530                 BIO_printf(bio_err,
2531                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
2532                 ERR_print_errors(bio_err);
2533                 ecdsa_doit[testnum] = 0;
2534             } else {
2535                 pkey_print_message("verify", "ecdsa",
2536                                    ecdsa_c[testnum][1],
2537                                    test_curves_bits[testnum], ECDSA_SECONDS);
2538                 Time_F(START);
2539                 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2540                 d = Time_F(STOP);
2541                 BIO_printf(bio_err,
2542                            mr ? "+R6:%ld:%d:%.2f\n"
2543                            : "%ld %d bit ECDSA verify in %.2fs\n",
2544                            count, test_curves_bits[testnum], d);
2545                 ecdsa_results[testnum][1] = d / (double)count;
2546             }
2547
2548             if (rsa_count <= 1) {
2549                 /* if longer than 10s, don't do any more */
2550                 for (testnum++; testnum < EC_NUM; testnum++)
2551                     ecdsa_doit[testnum] = 0;
2552             }
2553         }
2554     }
2555
2556     if (RAND_status() != 1) {
2557         RAND_seed(rnd_seed, sizeof(rnd_seed));
2558     }
2559     for (testnum = 0; testnum < EC_NUM; testnum++) {
2560         int ecdh_checks = 1;
2561
2562         if (!ecdh_doit[testnum])
2563             continue;
2564         for (i = 0; i < loopargs_len; i++) {
2565             loopargs[i].ecdh_a[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2566             loopargs[i].ecdh_b[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2567             if (loopargs[i].ecdh_a[testnum] == NULL ||
2568                 loopargs[i].ecdh_b[testnum] == NULL) {
2569                 ecdh_checks = 0;
2570                 break;
2571             }
2572         }
2573         if (ecdh_checks == 0) {
2574             BIO_printf(bio_err, "ECDH failure.\n");
2575             ERR_print_errors(bio_err);
2576             rsa_count = 1;
2577         } else {
2578             for (i = 0; i < loopargs_len; i++) {
2579                 /* generate two ECDH key pairs */
2580                 if (!EC_KEY_generate_key(loopargs[i].ecdh_a[testnum]) ||
2581                         !EC_KEY_generate_key(loopargs[i].ecdh_b[testnum])) {
2582                     BIO_printf(bio_err, "ECDH key generation failure.\n");
2583                     ERR_print_errors(bio_err);
2584                     ecdh_checks = 0;
2585                     rsa_count = 1;
2586                 } else {
2587                     int secret_size_a, secret_size_b;
2588                     /*
2589                      * If field size is not more than 24 octets, then use SHA-1
2590                      * hash of result; otherwise, use result (see section 4.8 of
2591                      * draft-ietf-tls-ecc-03.txt).
2592                      */
2593                     int field_size = EC_GROUP_get_degree(
2594                             EC_KEY_get0_group(loopargs[i].ecdh_a[testnum]));
2595
2596                     if (field_size <= 24 * 8) {                 /* 192 bits */
2597                         loopargs[i].outlen = KDF1_SHA1_len;
2598                         loopargs[i].kdf = KDF1_SHA1;
2599                     } else {
2600                         loopargs[i].outlen = (field_size + 7) / 8;
2601                         loopargs[i].kdf = NULL;
2602                     }
2603                     secret_size_a =
2604                         ECDH_compute_key(loopargs[i].secret_a, loopargs[i].outlen,
2605                                 EC_KEY_get0_public_key(loopargs[i].ecdh_b[testnum]),
2606                                 loopargs[i].ecdh_a[testnum], loopargs[i].kdf);
2607                     secret_size_b =
2608                         ECDH_compute_key(loopargs[i].secret_b, loopargs[i].outlen,
2609                                 EC_KEY_get0_public_key(loopargs[i].ecdh_a[testnum]),
2610                                 loopargs[i].ecdh_b[testnum], loopargs[i].kdf);
2611                     if (secret_size_a != secret_size_b)
2612                         ecdh_checks = 0;
2613                     else
2614                         ecdh_checks = 1;
2615
2616                     for (k = 0; k < secret_size_a && ecdh_checks == 1; k++) {
2617                         if (loopargs[i].secret_a[k] != loopargs[i].secret_b[k])
2618                             ecdh_checks = 0;
2619                     }
2620
2621                     if (ecdh_checks == 0) {
2622                         BIO_printf(bio_err, "ECDH computations don't match.\n");
2623                         ERR_print_errors(bio_err);
2624                         rsa_count = 1;
2625                         break;
2626                     }
2627                 }
2628             }
2629             if (ecdh_checks != 0) {
2630                 pkey_print_message("", "ecdh",
2631                         ecdh_c[testnum][0],
2632                         test_curves_bits[testnum], ECDH_SECONDS);
2633                 Time_F(START);
2634                 count = run_benchmark(async_jobs, ECDH_compute_key_loop, loopargs);
2635                 d = Time_F(STOP);
2636                 BIO_printf(bio_err,
2637                         mr ? "+R7:%ld:%d:%.2f\n" :
2638                         "%ld %d-bit ECDH ops in %.2fs\n", count,
2639                         test_curves_bits[testnum], d);
2640                 ecdh_results[testnum][0] = d / (double)count;
2641                 rsa_count = count;
2642             }
2643         }
2644
2645         if (rsa_count <= 1) {
2646             /* if longer than 10s, don't do any more */
2647             for (testnum++; testnum < EC_NUM; testnum++)
2648                 ecdh_doit[testnum] = 0;
2649         }
2650     }
2651 #endif                          /* OPENSSL_NO_EC */
2652 #ifndef NO_FORK
2653  show_res:
2654 #endif
2655     if (!mr) {
2656         printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
2657         printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2658         printf("options:");
2659         printf("%s ", BN_options());
2660 #ifndef OPENSSL_NO_MD2
2661         printf("%s ", MD2_options());
2662 #endif
2663 #ifndef OPENSSL_NO_RC4
2664         printf("%s ", RC4_options());
2665 #endif
2666 #ifndef OPENSSL_NO_DES
2667         printf("%s ", DES_options());
2668 #endif
2669         printf("%s ", AES_options());
2670 #ifndef OPENSSL_NO_IDEA
2671         printf("%s ", IDEA_options());
2672 #endif
2673 #ifndef OPENSSL_NO_BF
2674         printf("%s ", BF_options());
2675 #endif
2676         printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2677     }
2678
2679     if (pr_header) {
2680         if (mr)
2681             printf("+H");
2682         else {
2683             printf
2684                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2685             printf("type        ");
2686         }
2687         for (testnum = 0; testnum < SIZE_NUM; testnum++)
2688             printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2689         printf("\n");
2690     }
2691
2692     for (k = 0; k < ALGOR_NUM; k++) {
2693         if (!doit[k])
2694             continue;
2695         if (mr)
2696             printf("+F:%d:%s", k, names[k]);
2697         else
2698             printf("%-13s", names[k]);
2699         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2700             if (results[k][testnum] > 10000 && !mr)
2701                 printf(" %11.2fk", results[k][testnum] / 1e3);
2702             else
2703                 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2704         }
2705         printf("\n");
2706     }
2707 #ifndef OPENSSL_NO_RSA
2708     testnum = 1;
2709     for (k = 0; k < RSA_NUM; k++) {
2710         if (!rsa_doit[k])
2711             continue;
2712         if (testnum && !mr) {
2713             printf("%18ssign    verify    sign/s verify/s\n", " ");
2714             testnum = 0;
2715         }
2716         if (mr)
2717             printf("+F2:%u:%u:%f:%f\n",
2718                    k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2719         else
2720             printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2721                    rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2722                    1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
2723     }
2724 #endif
2725 #ifndef OPENSSL_NO_DSA
2726     testnum = 1;
2727     for (k = 0; k < DSA_NUM; k++) {
2728         if (!dsa_doit[k])
2729             continue;
2730         if (testnum && !mr) {
2731             printf("%18ssign    verify    sign/s verify/s\n", " ");
2732             testnum = 0;
2733         }
2734         if (mr)
2735             printf("+F3:%u:%u:%f:%f\n",
2736                    k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2737         else
2738             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2739                    dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2740                    1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
2741     }
2742 #endif
2743 #ifndef OPENSSL_NO_EC
2744     testnum = 1;
2745     for (k = 0; k < EC_NUM; k++) {
2746         if (!ecdsa_doit[k])
2747             continue;
2748         if (testnum && !mr) {
2749             printf("%30ssign    verify    sign/s verify/s\n", " ");
2750             testnum = 0;
2751         }
2752
2753         if (mr)
2754             printf("+F4:%u:%u:%f:%f\n",
2755                    k, test_curves_bits[k],
2756                    ecdsa_results[k][0], ecdsa_results[k][1]);
2757         else
2758             printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2759                    test_curves_bits[k],
2760                    test_curves_names[k],
2761                    ecdsa_results[k][0], ecdsa_results[k][1],
2762                    1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
2763     }
2764
2765     testnum = 1;
2766     for (k = 0; k < EC_NUM; k++) {
2767         if (!ecdh_doit[k])
2768             continue;
2769         if (testnum && !mr) {
2770             printf("%30sop      op/s\n", " ");
2771             testnum = 0;
2772         }
2773         if (mr)
2774             printf("+F5:%u:%u:%f:%f\n",
2775                    k, test_curves_bits[k],
2776                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2777
2778         else
2779             printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2780                    test_curves_bits[k],
2781                    test_curves_names[k],
2782                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2783     }
2784 #endif
2785
2786     ret = 0;
2787
2788  end:
2789     ERR_print_errors(bio_err);
2790     for (i = 0; i < loopargs_len; i++) {
2791         OPENSSL_free(loopargs[i].buf_malloc);
2792         OPENSSL_free(loopargs[i].buf2_malloc);
2793
2794 #ifndef OPENSSL_NO_RSA
2795         for (k = 0; k < RSA_NUM; k++)
2796             RSA_free(loopargs[i].rsa_key[k]);
2797 #endif
2798 #ifndef OPENSSL_NO_DSA
2799         for (k = 0; k < DSA_NUM; k++)
2800             DSA_free(loopargs[i].dsa_key[k]);
2801 #endif
2802 #ifndef OPENSSL_NO_EC
2803         for (k = 0; k < EC_NUM; k++) {
2804             EC_KEY_free(loopargs[i].ecdsa[k]);
2805             EC_KEY_free(loopargs[i].ecdh_a[k]);
2806             EC_KEY_free(loopargs[i].ecdh_b[k]);
2807         }
2808         OPENSSL_free(loopargs[i].secret_a);
2809         OPENSSL_free(loopargs[i].secret_b);
2810 #endif
2811     }
2812
2813     if (async_jobs > 0) {
2814         for (i = 0; i < loopargs_len; i++)
2815             ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
2816     }
2817
2818     if (async_init) {
2819         ASYNC_cleanup_thread();
2820     }
2821     OPENSSL_free(loopargs);
2822     release_engine(e);
2823     return (ret);
2824 }
2825
2826 static void print_message(const char *s, long num, int length)
2827 {
2828 #ifdef SIGALRM
2829     BIO_printf(bio_err,
2830                mr ? "+DT:%s:%d:%d\n"
2831                : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2832     (void)BIO_flush(bio_err);
2833     alarm(SECONDS);
2834 #else
2835     BIO_printf(bio_err,
2836                mr ? "+DN:%s:%ld:%d\n"
2837                : "Doing %s %ld times on %d size blocks: ", s, num, length);
2838     (void)BIO_flush(bio_err);
2839 #endif
2840 }
2841
2842 static void pkey_print_message(const char *str, const char *str2, long num,
2843                                int bits, int tm)
2844 {
2845 #ifdef SIGALRM
2846     BIO_printf(bio_err,
2847                mr ? "+DTP:%d:%s:%s:%d\n"
2848                : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2849     (void)BIO_flush(bio_err);
2850     alarm(tm);
2851 #else
2852     BIO_printf(bio_err,
2853                mr ? "+DNP:%ld:%d:%s:%s\n"
2854                : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2855     (void)BIO_flush(bio_err);
2856 #endif
2857 }
2858
2859 static void print_result(int alg, int run_no, int count, double time_used)
2860 {
2861     if (count == -1) {
2862         BIO_puts(bio_err, "EVP error!\n");
2863         exit(1);
2864     }
2865     BIO_printf(bio_err,
2866                mr ? "+R:%d:%s:%f\n"
2867                : "%d %s's in %.2fs\n", count, names[alg], time_used);
2868     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2869 }
2870
2871 #ifndef NO_FORK
2872 static char *sstrsep(char **string, const char *delim)
2873 {
2874     char isdelim[256];
2875     char *token = *string;
2876
2877     if (**string == 0)
2878         return NULL;
2879
2880     memset(isdelim, 0, sizeof(isdelim));
2881     isdelim[0] = 1;
2882
2883     while (*delim) {
2884         isdelim[(unsigned char)(*delim)] = 1;
2885         delim++;
2886     }
2887
2888     while (!isdelim[(unsigned char)(**string)]) {
2889         (*string)++;
2890     }
2891
2892     if (**string) {
2893         **string = 0;
2894         (*string)++;
2895     }
2896
2897     return token;
2898 }
2899
2900 static int do_multi(int multi)
2901 {
2902     int n;
2903     int fd[2];
2904     int *fds;
2905     static char sep[] = ":";
2906
2907     fds = malloc(sizeof(*fds) * multi);
2908     for (n = 0; n < multi; ++n) {
2909         if (pipe(fd) == -1) {
2910             BIO_printf(bio_err, "pipe failure\n");
2911             exit(1);
2912         }
2913         fflush(stdout);
2914         (void)BIO_flush(bio_err);
2915         if (fork()) {
2916             close(fd[1]);
2917             fds[n] = fd[0];
2918         } else {
2919             close(fd[0]);
2920             close(1);
2921             if (dup(fd[1]) == -1) {
2922                 BIO_printf(bio_err, "dup failed\n");
2923                 exit(1);
2924             }
2925             close(fd[1]);
2926             mr = 1;
2927             usertime = 0;
2928             free(fds);
2929             return 0;
2930         }
2931         printf("Forked child %d\n", n);
2932     }
2933
2934     /* for now, assume the pipe is long enough to take all the output */
2935     for (n = 0; n < multi; ++n) {
2936         FILE *f;
2937         char buf[1024];
2938         char *p;
2939
2940         f = fdopen(fds[n], "r");
2941         while (fgets(buf, sizeof(buf), f)) {
2942             p = strchr(buf, '\n');
2943             if (p)
2944                 *p = '\0';
2945             if (buf[0] != '+') {
2946                 BIO_printf(bio_err, "Don't understand line '%s' from child %d\n",
2947                         buf, n);
2948                 continue;
2949             }
2950             printf("Got: %s from %d\n", buf, n);
2951             if (strncmp(buf, "+F:", 3) == 0) {
2952                 int alg;
2953                 int j;
2954
2955                 p = buf + 3;
2956                 alg = atoi(sstrsep(&p, sep));
2957                 sstrsep(&p, sep);
2958                 for (j = 0; j < SIZE_NUM; ++j)
2959                     results[alg][j] += atof(sstrsep(&p, sep));
2960             } else if (strncmp(buf, "+F2:", 4) == 0) {
2961                 int k;
2962                 double d;
2963
2964                 p = buf + 4;
2965                 k = atoi(sstrsep(&p, sep));
2966                 sstrsep(&p, sep);
2967
2968                 d = atof(sstrsep(&p, sep));
2969                 if (n)
2970                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
2971                 else
2972                     rsa_results[k][0] = d;
2973
2974                 d = atof(sstrsep(&p, sep));
2975                 if (n)
2976                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
2977                 else
2978                     rsa_results[k][1] = d;
2979             }
2980 # ifndef OPENSSL_NO_DSA
2981             else if (strncmp(buf, "+F3:", 4) == 0) {
2982                 int k;
2983                 double d;
2984
2985                 p = buf + 4;
2986                 k = atoi(sstrsep(&p, sep));
2987                 sstrsep(&p, sep);
2988
2989                 d = atof(sstrsep(&p, sep));
2990                 if (n)
2991                     dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
2992                 else
2993                     dsa_results[k][0] = d;
2994
2995                 d = atof(sstrsep(&p, sep));
2996                 if (n)
2997                     dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
2998                 else
2999                     dsa_results[k][1] = d;
3000             }
3001 # endif
3002 # ifndef OPENSSL_NO_EC
3003             else if (strncmp(buf, "+F4:", 4) == 0) {
3004                 int k;
3005                 double d;
3006
3007                 p = buf + 4;
3008                 k = atoi(sstrsep(&p, sep));
3009                 sstrsep(&p, sep);
3010
3011                 d = atof(sstrsep(&p, sep));
3012                 if (n)
3013                     ecdsa_results[k][0] =
3014                         1 / (1 / ecdsa_results[k][0] + 1 / d);
3015                 else
3016                     ecdsa_results[k][0] = d;
3017
3018                 d = atof(sstrsep(&p, sep));
3019                 if (n)
3020                     ecdsa_results[k][1] =
3021                         1 / (1 / ecdsa_results[k][1] + 1 / d);
3022                 else
3023                     ecdsa_results[k][1] = d;
3024             } else if (strncmp(buf, "+F5:", 4) == 0) {
3025                 int k;
3026                 double d;
3027
3028                 p = buf + 4;
3029                 k = atoi(sstrsep(&p, sep));
3030                 sstrsep(&p, sep);
3031
3032                 d = atof(sstrsep(&p, sep));
3033                 if (n)
3034                     ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
3035                 else
3036                     ecdh_results[k][0] = d;
3037
3038             }
3039 # endif
3040
3041             else if (strncmp(buf, "+H:", 3) == 0) {
3042                 ;
3043             } else
3044                 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf, n);
3045         }
3046
3047         fclose(f);
3048     }
3049     free(fds);
3050     return 1;
3051 }
3052 #endif
3053
3054 static void multiblock_speed(const EVP_CIPHER *evp_cipher)
3055 {
3056     static int mblengths[] =
3057         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3058     int j, count, num = OSSL_NELEM(mblengths);
3059     const char *alg_name;
3060     unsigned char *inp, *out, no_key[32], no_iv[16];
3061     EVP_CIPHER_CTX *ctx;
3062     double d = 0.0;
3063
3064     inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
3065     out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3066     ctx = EVP_CIPHER_CTX_new();
3067     EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3068     EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
3069                         no_key);
3070     alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3071
3072     for (j = 0; j < num; j++) {
3073         print_message(alg_name, 0, mblengths[j]);
3074         Time_F(START);
3075         for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3076             unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3077             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
3078             size_t len = mblengths[j];
3079             int packlen;
3080
3081             memset(aad, 0, 8);  /* avoid uninitialized values */
3082             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
3083             aad[9] = 3;         /* version */
3084             aad[10] = 2;
3085             aad[11] = 0;        /* length */
3086             aad[12] = 0;
3087             mb_param.out = NULL;
3088             mb_param.inp = aad;
3089             mb_param.len = len;
3090             mb_param.interleave = 8;
3091
3092             packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3093                                           sizeof(mb_param), &mb_param);
3094
3095             if (packlen > 0) {
3096                 mb_param.out = out;
3097                 mb_param.inp = inp;
3098                 mb_param.len = len;
3099                 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3100                                     sizeof(mb_param), &mb_param);
3101             } else {
3102                 int pad;
3103
3104                 RAND_bytes(out, 16);
3105                 len += 16;
3106                 aad[11] = len >> 8;
3107                 aad[12] = len;
3108                 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3109                                           EVP_AEAD_TLS1_AAD_LEN, aad);
3110                 EVP_Cipher(ctx, out, inp, len + pad);
3111             }
3112         }
3113         d = Time_F(STOP);
3114         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3115                    : "%d %s's in %.2fs\n", count, "evp", d);
3116         results[D_EVP][j] = ((double)count) / d * mblengths[j];
3117     }
3118
3119     if (mr) {
3120         fprintf(stdout, "+H");
3121         for (j = 0; j < num; j++)
3122             fprintf(stdout, ":%d", mblengths[j]);
3123         fprintf(stdout, "\n");
3124         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
3125         for (j = 0; j < num; j++)
3126             fprintf(stdout, ":%.2f", results[D_EVP][j]);
3127         fprintf(stdout, "\n");
3128     } else {
3129         fprintf(stdout,
3130                 "The 'numbers' are in 1000s of bytes per second processed.\n");
3131         fprintf(stdout, "type                    ");
3132         for (j = 0; j < num; j++)
3133             fprintf(stdout, "%7d bytes", mblengths[j]);
3134         fprintf(stdout, "\n");
3135         fprintf(stdout, "%-24s", alg_name);
3136
3137         for (j = 0; j < num; j++) {
3138             if (results[D_EVP][j] > 10000)
3139                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
3140             else
3141                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
3142         }
3143         fprintf(stdout, "\n");
3144     }
3145
3146     OPENSSL_free(inp);
3147     OPENSSL_free(out);
3148     EVP_CIPHER_CTX_free(ctx);
3149 }