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