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