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