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