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