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