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