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