Convert CRYPTO_LOCK_BIO to new multi-threading API
[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 #if defined(OPENSSL_SYS_UNIX)
1122     fd_set waitfdset;
1123     OSSL_ASYNC_FD max_fd = 0;
1124 #endif
1125
1126     run = 1;
1127
1128     if (async_jobs == 0) {
1129         return loop_function((void *)loopargs);
1130     }
1131
1132
1133     for (i = 0; i < async_jobs && !error; i++) {
1134         switch (ASYNC_start_job(&(loopargs[i].inprogress_job), loopargs[i].wait_ctx,
1135                                 &job_op_count, loop_function,
1136                                 (void *)(loopargs + i), sizeof(loopargs_t))) {
1137             case ASYNC_PAUSE:
1138                 ++num_inprogress;
1139                 break;
1140             case ASYNC_FINISH:
1141                 if (job_op_count == -1) {
1142                     error = 1;
1143                 } else {
1144                     total_op_count += job_op_count;
1145                 }
1146                 break;
1147             case ASYNC_NO_JOBS:
1148             case ASYNC_ERR:
1149                 BIO_printf(bio_err, "Failure in the job\n");
1150                 ERR_print_errors(bio_err);
1151                 error = 1;
1152                 break;
1153         }
1154     }
1155
1156 #if defined(OPENSSL_SYS_UNIX)
1157     FD_ZERO(&waitfdset);
1158
1159     /* Add to the wait set all the fds that are already in the WAIT_CTX
1160      * This is required when the same ctx is used multiple times
1161      * For the purpose of speed, each job can be associated to at most one fd
1162      */
1163     for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1164         if (loopargs[i].inprogress_job == NULL)
1165             continue;
1166
1167         if (!ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, NULL, &num_job_fds)
1168                 || num_job_fds > 1) {
1169             BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1170             ERR_print_errors(bio_err);
1171             error = 1;
1172             break;
1173         }
1174         ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd, &num_job_fds);
1175         FD_SET(job_fd, &waitfdset);
1176         if (job_fd > max_fd)
1177             max_fd = job_fd;
1178     }
1179 #endif
1180
1181     while (num_inprogress > 0) {
1182 #if defined(OPENSSL_SYS_UNIX)
1183         int select_result = 0;
1184         struct timeval select_timeout;
1185         select_timeout.tv_sec = 0;
1186         select_timeout.tv_usec = 0;
1187
1188         for (i = 0; i < async_jobs; i++) {
1189             if (loopargs[i].inprogress_job != NULL) {
1190                 /* Consider only changed fds to minimize the operations on waitfdset */
1191                 OSSL_ASYNC_FD add_fd, del_fd;
1192                 size_t num_add_fds, num_del_fds;
1193                 if (!ASYNC_WAIT_CTX_get_changed_fds(loopargs[i].wait_ctx, NULL,
1194                                                     &num_add_fds, NULL, &num_del_fds)) {
1195                     BIO_printf(bio_err, "Failure in ASYNC_WAIT_CTX\n");
1196                     ERR_print_errors(bio_err);
1197                     error = 1;
1198                     break;
1199                 }
1200                 if (num_add_fds > 1 || num_del_fds > 1) {
1201                     BIO_printf(bio_err, "Too many fds have changed in ASYNC_WAIT_CTX\n");
1202                     ERR_print_errors(bio_err);
1203                     error = 1;
1204                     break;
1205                 }
1206                 if (num_add_fds == 0 && num_del_fds == 0)
1207                     continue;
1208
1209                 ASYNC_WAIT_CTX_get_changed_fds(loopargs[i].wait_ctx, &add_fd, &num_add_fds,
1210                                                &del_fd, &num_del_fds);
1211
1212                 if (num_del_fds == 1)
1213                     FD_CLR(del_fd, &waitfdset);
1214
1215                 if (num_add_fds == 1) {
1216                     FD_SET(add_fd, &waitfdset);
1217                     if (add_fd > max_fd)
1218                         max_fd = add_fd;
1219                 }
1220             }
1221         }
1222         select_result = select(max_fd + 1, &waitfdset, NULL, NULL, &select_timeout);
1223
1224         if (select_result == -1 && errno == EINTR)
1225             continue;
1226
1227         if (select_result == -1) {
1228                 BIO_printf(bio_err, "Failure in the select\n");
1229                 ERR_print_errors(bio_err);
1230                 error = 1;
1231                 break;
1232         }
1233
1234         if (select_result == 0)
1235             continue;
1236
1237 #elif defined(OPENSSL_SYS_WINDOWS)
1238         DWORD avail = 0;
1239 #endif
1240
1241         for (i = 0; i < async_jobs; i++) {
1242             if (loopargs[i].inprogress_job == NULL)
1243                 continue;
1244
1245             if (!ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, NULL, &num_job_fds)
1246                     || num_job_fds > 1) {
1247                 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1248                 ERR_print_errors(bio_err);
1249                 error = 1;
1250                 break;
1251             }
1252             ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd, &num_job_fds);
1253
1254 #if defined(OPENSSL_SYS_UNIX)
1255             if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1256                 continue;
1257 #elif defined(OPENSSL_SYS_WINDOWS)
1258             if (num_job_fds == 1 &&
1259                     !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL) && avail > 0)
1260                 continue;
1261 #endif
1262
1263             switch (ASYNC_start_job(&(loopargs[i].inprogress_job), loopargs[i].wait_ctx,
1264                         &job_op_count, loop_function, (void *)(loopargs + i),
1265                         sizeof(loopargs_t))) {
1266                 case ASYNC_PAUSE:
1267                     break;
1268                 case ASYNC_FINISH:
1269                     if (job_op_count == -1) {
1270                         error = 1;
1271                     } else {
1272                         total_op_count += job_op_count;
1273                     }
1274                     --num_inprogress;
1275 #if defined(OPENSSL_SYS_UNIX)
1276                     FD_CLR(job_fd, &waitfdset);
1277 #endif
1278                     loopargs[i].inprogress_job = NULL;
1279                     break;
1280                 case ASYNC_NO_JOBS:
1281                 case ASYNC_ERR:
1282                     --num_inprogress;
1283                     loopargs[i].inprogress_job = NULL;
1284                     BIO_printf(bio_err, "Failure in the job\n");
1285                     ERR_print_errors(bio_err);
1286                     error = 1;
1287                     break;
1288             }
1289         }
1290     }
1291
1292     return error ? -1 : total_op_count;
1293 }
1294
1295 int speed_main(int argc, char **argv)
1296 {
1297     loopargs_t *loopargs = NULL;
1298     int loopargs_len = 0;
1299     char *prog;
1300     const EVP_CIPHER *evp_cipher = NULL;
1301     double d = 0.0;
1302     OPTION_CHOICE o;
1303     int multiblock = 0, doit[ALGOR_NUM], pr_header = 0;
1304     int dsa_doit[DSA_NUM], rsa_doit[RSA_NUM];
1305     int ret = 1, i, k, misalign = 0;
1306     long c[ALGOR_NUM][SIZE_NUM], count = 0, save_count = 0;
1307 #ifndef NO_FORK
1308     int multi = 0;
1309 #endif
1310     int async_jobs = 0;
1311     /* What follows are the buffers and key material. */
1312 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
1313     long rsa_count = 1;
1314 #endif
1315 #ifndef OPENSSL_NO_RC5
1316     RC5_32_KEY rc5_ks;
1317 #endif
1318 #ifndef OPENSSL_NO_RC2
1319     RC2_KEY rc2_ks;
1320 #endif
1321 #ifndef OPENSSL_NO_IDEA
1322     IDEA_KEY_SCHEDULE idea_ks;
1323 #endif
1324 #ifndef OPENSSL_NO_SEED
1325     SEED_KEY_SCHEDULE seed_ks;
1326 #endif
1327 #ifndef OPENSSL_NO_BF
1328     BF_KEY bf_ks;
1329 #endif
1330 #ifndef OPENSSL_NO_CAST
1331     CAST_KEY cast_ks;
1332 #endif
1333     static const unsigned char key16[16] = {
1334         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1335         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1336     };
1337 #ifndef OPENSSL_NO_AES
1338     static const unsigned char key24[24] = {
1339         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1340         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1341         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1342     };
1343     static const unsigned char key32[32] = {
1344         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1345         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1346         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1347         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1348     };
1349 #endif
1350 #ifndef OPENSSL_NO_CAMELLIA
1351     static const unsigned char ckey24[24] = {
1352         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1353         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1354         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1355     };
1356     static const unsigned char ckey32[32] = {
1357         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1358         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1359         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1360         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1361     };
1362     CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1363 #endif
1364 #ifndef OPENSSL_NO_DES
1365     static DES_cblock key = {
1366         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
1367     };
1368     static DES_cblock key2 = {
1369         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1370     };
1371     static DES_cblock key3 = {
1372         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1373     };
1374 #endif
1375 #ifndef OPENSSL_NO_RSA
1376     static unsigned int rsa_bits[RSA_NUM] = {
1377         512, 1024, 2048, 3072, 4096, 7680, 15360
1378     };
1379     static unsigned char *rsa_data[RSA_NUM] = {
1380         test512, test1024, test2048, test3072, test4096, test7680, test15360
1381     };
1382     static int rsa_data_length[RSA_NUM] = {
1383         sizeof(test512), sizeof(test1024),
1384         sizeof(test2048), sizeof(test3072),
1385         sizeof(test4096), sizeof(test7680),
1386         sizeof(test15360)
1387     };
1388 #endif
1389 #ifndef OPENSSL_NO_DSA
1390     static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1391 #endif
1392 #ifndef OPENSSL_NO_EC
1393     /*
1394      * We only test over the following curves as they are representative, To
1395      * add tests over more curves, simply add the curve NID and curve name to
1396      * the following arrays and increase the EC_NUM value accordingly.
1397      */
1398     static unsigned int test_curves[EC_NUM] = {
1399         /* Prime Curves */
1400         NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
1401         NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
1402         /* Binary Curves */
1403         NID_sect163k1, NID_sect233k1, NID_sect283k1,
1404         NID_sect409k1, NID_sect571k1, NID_sect163r2,
1405         NID_sect233r1, NID_sect283r1, NID_sect409r1,
1406         NID_sect571r1,
1407         /* Other */
1408         NID_X25519
1409     };
1410     static const char *test_curves_names[EC_NUM] = {
1411         /* Prime Curves */
1412         "secp160r1", "nistp192", "nistp224",
1413         "nistp256", "nistp384", "nistp521",
1414         /* Binary Curves */
1415         "nistk163", "nistk233", "nistk283",
1416         "nistk409", "nistk571", "nistb163",
1417         "nistb233", "nistb283", "nistb409",
1418         "nistb571",
1419         /* Other */
1420         "X25519"
1421     };
1422     static int test_curves_bits[EC_NUM] = {
1423         160, 192, 224,
1424         256, 384, 521,
1425         163, 233, 283,
1426         409, 571, 163,
1427         233, 283, 409,
1428         571, 253 /* X25519 */
1429     };
1430 #endif
1431 #ifndef OPENSSL_NO_EC
1432     int ecdsa_doit[EC_NUM];
1433     int secret_size_a, secret_size_b;
1434     int ecdh_checks = 1;
1435     int secret_idx = 0;
1436     long ecdh_c[EC_NUM][2];
1437     int ecdh_doit[EC_NUM];
1438 #endif
1439
1440     memset(results, 0, sizeof(results));
1441
1442     memset(c, 0, sizeof(c));
1443     memset(DES_iv, 0, sizeof(DES_iv));
1444     memset(iv, 0, sizeof(iv));
1445
1446     for (i = 0; i < ALGOR_NUM; i++)
1447         doit[i] = 0;
1448     for (i = 0; i < RSA_NUM; i++)
1449         rsa_doit[i] = 0;
1450     for (i = 0; i < DSA_NUM; i++)
1451         dsa_doit[i] = 0;
1452 #ifndef OPENSSL_NO_EC
1453     for (i = 0; i < EC_NUM; i++)
1454         ecdsa_doit[i] = 0;
1455     for (i = 0; i < EC_NUM; i++)
1456         ecdh_doit[i] = 0;
1457 #endif
1458
1459     misalign = 0;
1460
1461     prog = opt_init(argc, argv, speed_options);
1462     while ((o = opt_next()) != OPT_EOF) {
1463         switch (o) {
1464         case OPT_EOF:
1465         case OPT_ERR:
1466  opterr:
1467             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1468             goto end;
1469         case OPT_HELP:
1470             opt_help(speed_options);
1471             ret = 0;
1472             goto end;
1473         case OPT_ELAPSED:
1474             usertime = 0;
1475             break;
1476         case OPT_EVP:
1477             evp_cipher = EVP_get_cipherbyname(opt_arg());
1478             if (evp_cipher == NULL)
1479                 evp_md = EVP_get_digestbyname(opt_arg());
1480             if (evp_cipher == NULL && evp_md == NULL) {
1481                 BIO_printf(bio_err,
1482                            "%s: %s  an unknown cipher or digest\n",
1483                            prog, opt_arg());
1484                 goto end;
1485             }
1486             doit[D_EVP] = 1;
1487             break;
1488         case OPT_DECRYPT:
1489             decrypt = 1;
1490             break;
1491         case OPT_ENGINE:
1492             /*
1493              * In a forked execution, an engine might need to be
1494              * initialised by each child process, not by the parent.
1495              * So store the name here and run setup_engine() later on.
1496              */
1497             engine_id = opt_arg();
1498             break;
1499         case OPT_MULTI:
1500 #ifndef NO_FORK
1501             multi = atoi(opt_arg());
1502 #endif
1503             break;
1504         case OPT_ASYNCJOBS:
1505 #ifndef OPENSSL_NO_ASYNC
1506             async_jobs = atoi(opt_arg());
1507             if (!ASYNC_is_capable()) {
1508                 BIO_printf(bio_err,
1509                            "%s: async_jobs specified but async not supported\n",
1510                            prog);
1511                 goto opterr;
1512             }
1513 #endif
1514             break;
1515         case OPT_MISALIGN:
1516             if (!opt_int(opt_arg(), &misalign))
1517                 goto end;
1518             if (misalign > MISALIGN) {
1519                 BIO_printf(bio_err,
1520                            "%s: Maximum offset is %d\n", prog, MISALIGN);
1521                 goto opterr;
1522             }
1523             break;
1524         case OPT_MR:
1525             mr = 1;
1526             break;
1527         case OPT_MB:
1528             multiblock = 1;
1529             break;
1530         }
1531     }
1532     argc = opt_num_rest();
1533     argv = opt_rest();
1534
1535     /* Remaining arguments are algorithms. */
1536     for ( ; *argv; argv++) {
1537         if (found(*argv, doit_choices, &i)) {
1538             doit[i] = 1;
1539             continue;
1540         }
1541 #ifndef OPENSSL_NO_DES
1542         if (strcmp(*argv, "des") == 0) {
1543             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1544             continue;
1545         }
1546 #endif
1547         if (strcmp(*argv, "sha") == 0) {
1548             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1549             continue;
1550         }
1551 #ifndef OPENSSL_NO_RSA
1552 # ifndef RSA_NULL
1553         if (strcmp(*argv, "openssl") == 0) {
1554             RSA_set_default_method(RSA_PKCS1_OpenSSL());
1555             continue;
1556         }
1557 # endif
1558         if (strcmp(*argv, "rsa") == 0) {
1559             rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
1560                 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
1561                 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
1562                 rsa_doit[R_RSA_15360] = 1;
1563             continue;
1564         }
1565         if (found(*argv, rsa_choices, &i)) {
1566             rsa_doit[i] = 1;
1567             continue;
1568         }
1569 #endif
1570 #ifndef OPENSSL_NO_DSA
1571         if (strcmp(*argv, "dsa") == 0) {
1572             dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1573                 dsa_doit[R_DSA_2048] = 1;
1574             continue;
1575         }
1576         if (found(*argv, dsa_choices, &i)) {
1577             dsa_doit[i] = 2;
1578             continue;
1579         }
1580 #endif
1581 #ifndef OPENSSL_NO_AES
1582         if (strcmp(*argv, "aes") == 0) {
1583             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =
1584                 doit[D_CBC_256_AES] = 1;
1585             continue;
1586         }
1587 #endif
1588 #ifndef OPENSSL_NO_CAMELLIA
1589         if (strcmp(*argv, "camellia") == 0) {
1590             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =
1591                 doit[D_CBC_256_CML] = 1;
1592             continue;
1593         }
1594 #endif
1595 #ifndef OPENSSL_NO_EC
1596         if (strcmp(*argv, "ecdsa") == 0) {
1597             for (i = 0; i < EC_NUM; i++)
1598                 ecdsa_doit[i] = 1;
1599             continue;
1600         }
1601         if (found(*argv, ecdsa_choices, &i)) {
1602             ecdsa_doit[i] = 2;
1603             continue;
1604         }
1605         if (strcmp(*argv, "ecdh") == 0) {
1606             for (i = 0; i < EC_NUM; i++)
1607                 ecdh_doit[i] = 1;
1608             continue;
1609         }
1610         if (found(*argv, ecdh_choices, &i)) {
1611             ecdh_doit[i] = 2;
1612             continue;
1613         }
1614 #endif
1615         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1616         goto end;
1617     }
1618
1619     /* Initialize the job pool if async mode is enabled */
1620     if (async_jobs > 0) {
1621         if (!ASYNC_init_thread(async_jobs, async_jobs)) {
1622             BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1623             goto end;
1624         }
1625     }
1626
1627     loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1628     loopargs = app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1629     memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1630
1631     for (i = 0; i < loopargs_len; i++) {
1632         if (async_jobs > 0) {
1633             loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1634             if (loopargs[i].wait_ctx == NULL) {
1635                 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1636                 goto end;
1637             }
1638         }
1639
1640         loopargs[i].buf_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1641         loopargs[i].buf2_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1642         /* Align the start of buffers on a 64 byte boundary */
1643         loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1644         loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1645         loopargs[i].siglen = app_malloc(sizeof(unsigned int), "signature length");
1646 #ifndef OPENSSL_NO_EC
1647         loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1648         loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1649 #endif
1650     }
1651
1652 #ifndef NO_FORK
1653     if (multi && do_multi(multi))
1654         goto show_res;
1655 #endif
1656
1657     /* Initialize the engine after the fork */
1658     (void)setup_engine(engine_id, 0);
1659
1660     /* No parameters; turn on everything. */
1661     if ((argc == 0) && !doit[D_EVP]) {
1662         for (i = 0; i < ALGOR_NUM; i++)
1663             if (i != D_EVP)
1664                 doit[i] = 1;
1665         for (i = 0; i < RSA_NUM; i++)
1666             rsa_doit[i] = 1;
1667         for (i = 0; i < DSA_NUM; i++)
1668             dsa_doit[i] = 1;
1669 #ifndef OPENSSL_NO_EC
1670         for (i = 0; i < EC_NUM; i++)
1671             ecdsa_doit[i] = 1;
1672         for (i = 0; i < EC_NUM; i++)
1673             ecdh_doit[i] = 1;
1674 #endif
1675     }
1676     for (i = 0; i < ALGOR_NUM; i++)
1677         if (doit[i])
1678             pr_header++;
1679
1680     if (usertime == 0 && !mr)
1681         BIO_printf(bio_err,
1682                    "You have chosen to measure elapsed time "
1683                    "instead of user CPU time.\n");
1684
1685 #ifndef OPENSSL_NO_RSA
1686     for (i = 0; i < loopargs_len; i++) {
1687         for (k = 0; k < RSA_NUM; k++) {
1688             const unsigned char *p;
1689
1690             p = rsa_data[k];
1691             loopargs[i].rsa_key[k] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1692             if (loopargs[i].rsa_key[k] == NULL) {
1693                 BIO_printf(bio_err, "internal error loading RSA key number %d\n",
1694                         k);
1695                 goto end;
1696             }
1697         }
1698     }
1699 #endif
1700 #ifndef OPENSSL_NO_DSA
1701     for (i = 0; i < loopargs_len; i++) {
1702         loopargs[i].dsa_key[0] = get_dsa512();
1703         loopargs[i].dsa_key[1] = get_dsa1024();
1704         loopargs[i].dsa_key[2] = get_dsa2048();
1705     }
1706 #endif
1707 #ifndef OPENSSL_NO_DES
1708     DES_set_key_unchecked(&key, &sch);
1709     DES_set_key_unchecked(&key2, &sch2);
1710     DES_set_key_unchecked(&key3, &sch3);
1711 #endif
1712 #ifndef OPENSSL_NO_AES
1713     AES_set_encrypt_key(key16, 128, &aes_ks1);
1714     AES_set_encrypt_key(key24, 192, &aes_ks2);
1715     AES_set_encrypt_key(key32, 256, &aes_ks3);
1716 #endif
1717 #ifndef OPENSSL_NO_CAMELLIA
1718     Camellia_set_key(key16, 128, &camellia_ks1);
1719     Camellia_set_key(ckey24, 192, &camellia_ks2);
1720     Camellia_set_key(ckey32, 256, &camellia_ks3);
1721 #endif
1722 #ifndef OPENSSL_NO_IDEA
1723     idea_set_encrypt_key(key16, &idea_ks);
1724 #endif
1725 #ifndef OPENSSL_NO_SEED
1726     SEED_set_key(key16, &seed_ks);
1727 #endif
1728 #ifndef OPENSSL_NO_RC4
1729     RC4_set_key(&rc4_ks, 16, key16);
1730 #endif
1731 #ifndef OPENSSL_NO_RC2
1732     RC2_set_key(&rc2_ks, 16, key16, 128);
1733 #endif
1734 #ifndef OPENSSL_NO_RC5
1735     RC5_32_set_key(&rc5_ks, 16, key16, 12);
1736 #endif
1737 #ifndef OPENSSL_NO_BF
1738     BF_set_key(&bf_ks, 16, key16);
1739 #endif
1740 #ifndef OPENSSL_NO_CAST
1741     CAST_set_key(&cast_ks, 16, key16);
1742 #endif
1743 #ifndef OPENSSL_NO_RSA
1744     memset(rsa_c, 0, sizeof(rsa_c));
1745 #endif
1746 #ifndef SIGALRM
1747 # ifndef OPENSSL_NO_DES
1748     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1749     count = 10;
1750     do {
1751         long it;
1752         count *= 2;
1753         Time_F(START);
1754         for (it = count; it; it--)
1755             DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
1756                             (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1757         d = Time_F(STOP);
1758     } while (d < 3);
1759     save_count = count;
1760     c[D_MD2][0] = count / 10;
1761     c[D_MDC2][0] = count / 10;
1762     c[D_MD4][0] = count;
1763     c[D_MD5][0] = count;
1764     c[D_HMAC][0] = count;
1765     c[D_SHA1][0] = count;
1766     c[D_RMD160][0] = count;
1767     c[D_RC4][0] = count * 5;
1768     c[D_CBC_DES][0] = count;
1769     c[D_EDE3_DES][0] = count / 3;
1770     c[D_CBC_IDEA][0] = count;
1771     c[D_CBC_SEED][0] = count;
1772     c[D_CBC_RC2][0] = count;
1773     c[D_CBC_RC5][0] = count;
1774     c[D_CBC_BF][0] = count;
1775     c[D_CBC_CAST][0] = count;
1776     c[D_CBC_128_AES][0] = count;
1777     c[D_CBC_192_AES][0] = count;
1778     c[D_CBC_256_AES][0] = count;
1779     c[D_CBC_128_CML][0] = count;
1780     c[D_CBC_192_CML][0] = count;
1781     c[D_CBC_256_CML][0] = count;
1782     c[D_SHA256][0] = count;
1783     c[D_SHA512][0] = count;
1784     c[D_WHIRLPOOL][0] = count;
1785     c[D_IGE_128_AES][0] = count;
1786     c[D_IGE_192_AES][0] = count;
1787     c[D_IGE_256_AES][0] = count;
1788     c[D_GHASH][0] = count;
1789
1790     for (i = 1; i < SIZE_NUM; i++) {
1791         long l0, l1;
1792
1793         l0 = (long)lengths[0];
1794         l1 = (long)lengths[i];
1795
1796         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1797         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1798         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1799         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1800         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1801         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1802         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1803         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1804         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1805         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1806         c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1807
1808         l0 = (long)lengths[i - 1];
1809
1810         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1811         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1812         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1813         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1814         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1815         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1816         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1817         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1818         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1819         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1820         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1821         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1822         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1823         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1824         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1825         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1826         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1827         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1828     }
1829
1830 #  ifndef OPENSSL_NO_RSA
1831     rsa_c[R_RSA_512][0] = count / 2000;
1832     rsa_c[R_RSA_512][1] = count / 400;
1833     for (i = 1; i < RSA_NUM; i++) {
1834         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1835         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1836         if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
1837             rsa_doit[i] = 0;
1838         else {
1839             if (rsa_c[i][0] == 0) {
1840                 rsa_c[i][0] = 1;
1841                 rsa_c[i][1] = 20;
1842             }
1843         }
1844     }
1845 #  endif
1846
1847 #  ifndef OPENSSL_NO_DSA
1848     dsa_c[R_DSA_512][0] = count / 1000;
1849     dsa_c[R_DSA_512][1] = count / 1000 / 2;
1850     for (i = 1; i < DSA_NUM; i++) {
1851         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1852         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1853         if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
1854             dsa_doit[i] = 0;
1855         else {
1856             if (dsa_c[i] == 0) {
1857                 dsa_c[i][0] = 1;
1858                 dsa_c[i][1] = 1;
1859             }
1860         }
1861     }
1862 #  endif
1863
1864 #  ifndef OPENSSL_NO_EC
1865     ecdsa_c[R_EC_P160][0] = count / 1000;
1866     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1867     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1868         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1869         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1870         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1871             ecdsa_doit[i] = 0;
1872         else {
1873             if (ecdsa_c[i] == 0) {
1874                 ecdsa_c[i][0] = 1;
1875                 ecdsa_c[i][1] = 1;
1876             }
1877         }
1878     }
1879     ecdsa_c[R_EC_K163][0] = count / 1000;
1880     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1881     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1882         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1883         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1884         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1885             ecdsa_doit[i] = 0;
1886         else {
1887             if (ecdsa_c[i] == 0) {
1888                 ecdsa_c[i][0] = 1;
1889                 ecdsa_c[i][1] = 1;
1890             }
1891         }
1892     }
1893     ecdsa_c[R_EC_B163][0] = count / 1000;
1894     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1895     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1896         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1897         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1898         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1899             ecdsa_doit[i] = 0;
1900         else {
1901             if (ecdsa_c[i] == 0) {
1902                 ecdsa_c[i][0] = 1;
1903                 ecdsa_c[i][1] = 1;
1904             }
1905         }
1906     }
1907
1908     ecdh_c[R_EC_P160][0] = count / 1000;
1909     ecdh_c[R_EC_P160][1] = count / 1000;
1910     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1911         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1912         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1913         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1914             ecdh_doit[i] = 0;
1915         else {
1916             if (ecdh_c[i] == 0) {
1917                 ecdh_c[i][0] = 1;
1918                 ecdh_c[i][1] = 1;
1919             }
1920         }
1921     }
1922     ecdh_c[R_EC_K163][0] = count / 1000;
1923     ecdh_c[R_EC_K163][1] = count / 1000;
1924     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1925         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1926         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1927         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1928             ecdh_doit[i] = 0;
1929         else {
1930             if (ecdh_c[i] == 0) {
1931                 ecdh_c[i][0] = 1;
1932                 ecdh_c[i][1] = 1;
1933             }
1934         }
1935     }
1936     ecdh_c[R_EC_B163][0] = count / 1000;
1937     ecdh_c[R_EC_B163][1] = count / 1000;
1938     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1939         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1940         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1941         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1942             ecdh_doit[i] = 0;
1943         else {
1944             if (ecdh_c[i] == 0) {
1945                 ecdh_c[i][0] = 1;
1946                 ecdh_c[i][1] = 1;
1947             }
1948         }
1949     }
1950 #  endif
1951
1952 # else
1953 /* not worth fixing */
1954 #  error "You cannot disable DES on systems without SIGALRM."
1955 # endif                        /* OPENSSL_NO_DES */
1956 #else
1957 # ifndef _WIN32
1958     signal(SIGALRM, sig_done);
1959 # endif
1960 #endif                         /* SIGALRM */
1961
1962 #ifndef OPENSSL_NO_MD2
1963     if (doit[D_MD2]) {
1964         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1965             print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
1966             Time_F(START);
1967             count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1968             d = Time_F(STOP);
1969             print_result(D_MD2, testnum, count, d);
1970         }
1971     }
1972 #endif
1973 #ifndef OPENSSL_NO_MDC2
1974     if (doit[D_MDC2]) {
1975         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1976             print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
1977             Time_F(START);
1978             count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1979             d = Time_F(STOP);
1980             print_result(D_MDC2, testnum, count, d);
1981         }
1982     }
1983 #endif
1984
1985 #ifndef OPENSSL_NO_MD4
1986     if (doit[D_MD4]) {
1987         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1988             print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
1989             Time_F(START);
1990             count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1991             d = Time_F(STOP);
1992             print_result(D_MD4, testnum, count, d);
1993         }
1994     }
1995 #endif
1996
1997 #ifndef OPENSSL_NO_MD5
1998     if (doit[D_MD5]) {
1999         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2000             print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);
2001             Time_F(START);
2002             count = run_benchmark(async_jobs, MD5_loop, loopargs);
2003             d = Time_F(STOP);
2004             print_result(D_MD5, testnum, count, d);
2005         }
2006     }
2007 #endif
2008
2009 #ifndef OPENSSL_NO_MD5
2010     if (doit[D_HMAC]) {
2011         for (i = 0; i < loopargs_len; i++) {
2012             loopargs[i].hctx = HMAC_CTX_new();
2013             if (loopargs[i].hctx == NULL) {
2014                 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2015                 exit(1);
2016             }
2017
2018             HMAC_Init_ex(loopargs[i].hctx, (unsigned char *)"This is a key...",
2019                     16, EVP_md5(), NULL);
2020         }
2021         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2022             print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);
2023             Time_F(START);
2024             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2025             d = Time_F(STOP);
2026             print_result(D_HMAC, testnum, count, d);
2027         }
2028         for (i = 0; i < loopargs_len; i++) {
2029             HMAC_CTX_free(loopargs[i].hctx);
2030         }
2031     }
2032 #endif
2033     if (doit[D_SHA1]) {
2034         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2035             print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);
2036             Time_F(START);
2037             count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2038             d = Time_F(STOP);
2039             print_result(D_SHA1, testnum, count, d);
2040         }
2041     }
2042     if (doit[D_SHA256]) {
2043         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2044             print_message(names[D_SHA256], c[D_SHA256][testnum], lengths[testnum]);
2045             Time_F(START);
2046             count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2047             d = Time_F(STOP);
2048             print_result(D_SHA256, testnum, count, d);
2049         }
2050     }
2051     if (doit[D_SHA512]) {
2052         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2053             print_message(names[D_SHA512], c[D_SHA512][testnum], lengths[testnum]);
2054             Time_F(START);
2055             count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2056             d = Time_F(STOP);
2057             print_result(D_SHA512, testnum, count, d);
2058         }
2059     }
2060
2061 #ifndef OPENSSL_NO_WHIRLPOOL
2062     if (doit[D_WHIRLPOOL]) {
2063         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2064             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum], lengths[testnum]);
2065             Time_F(START);
2066             count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2067             d = Time_F(STOP);
2068             print_result(D_WHIRLPOOL, testnum, count, d);
2069         }
2070     }
2071 #endif
2072
2073 #ifndef OPENSSL_NO_RMD160
2074     if (doit[D_RMD160]) {
2075         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2076             print_message(names[D_RMD160], c[D_RMD160][testnum], lengths[testnum]);
2077             Time_F(START);
2078             count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2079             d = Time_F(STOP);
2080             print_result(D_RMD160, testnum, count, d);
2081         }
2082     }
2083 #endif
2084 #ifndef OPENSSL_NO_RC4
2085     if (doit[D_RC4]) {
2086         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2087             print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
2088             Time_F(START);
2089             count = run_benchmark(async_jobs, RC4_loop, loopargs);
2090             d = Time_F(STOP);
2091             print_result(D_RC4, testnum, count, d);
2092         }
2093     }
2094 #endif
2095 #ifndef OPENSSL_NO_DES
2096     if (doit[D_CBC_DES]) {
2097         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2098             print_message(names[D_CBC_DES], c[D_CBC_DES][testnum], lengths[testnum]);
2099             Time_F(START);
2100             count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2101             d = Time_F(STOP);
2102             print_result(D_CBC_DES, testnum, count, d);
2103         }
2104     }
2105
2106     if (doit[D_EDE3_DES]) {
2107         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2108             print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum], lengths[testnum]);
2109             Time_F(START);
2110             count = run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2111             d = Time_F(STOP);
2112             print_result(D_EDE3_DES, testnum, count, d);
2113         }
2114     }
2115 #endif
2116 #ifndef OPENSSL_NO_AES
2117     if (doit[D_CBC_128_AES]) {
2118         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2119             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2120                           lengths[testnum]);
2121             Time_F(START);
2122             count = run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2123             d = Time_F(STOP);
2124             print_result(D_CBC_128_AES, testnum, count, d);
2125         }
2126     }
2127     if (doit[D_CBC_192_AES]) {
2128         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2129             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2130                           lengths[testnum]);
2131             Time_F(START);
2132             count = run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2133             d = Time_F(STOP);
2134             print_result(D_CBC_192_AES, testnum, count, d);
2135         }
2136     }
2137     if (doit[D_CBC_256_AES]) {
2138         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2139             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2140                           lengths[testnum]);
2141             Time_F(START);
2142             count = run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2143             d = Time_F(STOP);
2144             print_result(D_CBC_256_AES, testnum, count, d);
2145         }
2146     }
2147
2148     if (doit[D_IGE_128_AES]) {
2149         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2150             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2151                           lengths[testnum]);
2152             Time_F(START);
2153             count = run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2154             d = Time_F(STOP);
2155             print_result(D_IGE_128_AES, testnum, count, d);
2156         }
2157     }
2158     if (doit[D_IGE_192_AES]) {
2159         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2160             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2161                           lengths[testnum]);
2162             Time_F(START);
2163             count = run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2164             d = Time_F(STOP);
2165             print_result(D_IGE_192_AES, testnum, count, d);
2166         }
2167     }
2168     if (doit[D_IGE_256_AES]) {
2169         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2170             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2171                           lengths[testnum]);
2172             Time_F(START);
2173             count = run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2174             d = Time_F(STOP);
2175             print_result(D_IGE_256_AES, testnum, count, d);
2176         }
2177     }
2178     if (doit[D_GHASH]) {
2179         for (i = 0; i < loopargs_len; i++) {
2180             loopargs[i].gcm_ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2181             CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx, (unsigned char *)"0123456789ab", 12);
2182         }
2183
2184         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2185             print_message(names[D_GHASH], c[D_GHASH][testnum], lengths[testnum]);
2186             Time_F(START);
2187             count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2188             d = Time_F(STOP);
2189             print_result(D_GHASH, testnum, count, d);
2190         }
2191         for (i = 0; i < loopargs_len; i++)
2192             CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2193     }
2194 #endif
2195 #ifndef OPENSSL_NO_CAMELLIA
2196     if (doit[D_CBC_128_CML]) {
2197         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2198             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2199                           lengths[testnum]);
2200             if (async_jobs > 0) {
2201                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2202                 exit(1);
2203             }
2204             Time_F(START);
2205             for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2206                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2207                                      (unsigned long)lengths[testnum], &camellia_ks1,
2208                                      iv, CAMELLIA_ENCRYPT);
2209             d = Time_F(STOP);
2210             print_result(D_CBC_128_CML, testnum, count, d);
2211         }
2212     }
2213     if (doit[D_CBC_192_CML]) {
2214         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2215             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2216                           lengths[testnum]);
2217             if (async_jobs > 0) {
2218                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2219                 exit(1);
2220             }
2221             Time_F(START);
2222             for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2223                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2224                                      (unsigned long)lengths[testnum], &camellia_ks2,
2225                                      iv, CAMELLIA_ENCRYPT);
2226             d = Time_F(STOP);
2227             print_result(D_CBC_192_CML, testnum, count, d);
2228         }
2229     }
2230     if (doit[D_CBC_256_CML]) {
2231         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2232             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2233                           lengths[testnum]);
2234             if (async_jobs > 0) {
2235                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2236                 exit(1);
2237             }
2238             Time_F(START);
2239             for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2240                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2241                                      (unsigned long)lengths[testnum], &camellia_ks3,
2242                                      iv, CAMELLIA_ENCRYPT);
2243             d = Time_F(STOP);
2244             print_result(D_CBC_256_CML, testnum, count, d);
2245         }
2246     }
2247 #endif
2248 #ifndef OPENSSL_NO_IDEA
2249     if (doit[D_CBC_IDEA]) {
2250         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2251             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum], lengths[testnum]);
2252             if (async_jobs > 0) {
2253                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2254                 exit(1);
2255             }
2256             Time_F(START);
2257             for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
2258                 idea_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2259                                  (unsigned long)lengths[testnum], &idea_ks,
2260                                  iv, IDEA_ENCRYPT);
2261             d = Time_F(STOP);
2262             print_result(D_CBC_IDEA, testnum, count, d);
2263         }
2264     }
2265 #endif
2266 #ifndef OPENSSL_NO_SEED
2267     if (doit[D_CBC_SEED]) {
2268         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2269             print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum], lengths[testnum]);
2270             if (async_jobs > 0) {
2271                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2272                 exit(1);
2273             }
2274             Time_F(START);
2275             for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2276                 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2277                                  (unsigned long)lengths[testnum], &seed_ks, iv, 1);
2278             d = Time_F(STOP);
2279             print_result(D_CBC_SEED, testnum, count, d);
2280         }
2281     }
2282 #endif
2283 #ifndef OPENSSL_NO_RC2
2284     if (doit[D_CBC_RC2]) {
2285         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2286             print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum], lengths[testnum]);
2287             if (async_jobs > 0) {
2288                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2289                 exit(1);
2290             }
2291             Time_F(START);
2292             for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2293                 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2294                                 (unsigned long)lengths[testnum], &rc2_ks,
2295                                 iv, RC2_ENCRYPT);
2296             d = Time_F(STOP);
2297             print_result(D_CBC_RC2, testnum, count, d);
2298         }
2299     }
2300 #endif
2301 #ifndef OPENSSL_NO_RC5
2302     if (doit[D_CBC_RC5]) {
2303         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2304             print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum], lengths[testnum]);
2305             if (async_jobs > 0) {
2306                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2307                 exit(1);
2308             }
2309             Time_F(START);
2310             for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2311                 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2312                                    (unsigned long)lengths[testnum], &rc5_ks,
2313                                    iv, RC5_ENCRYPT);
2314             d = Time_F(STOP);
2315             print_result(D_CBC_RC5, testnum, count, d);
2316         }
2317     }
2318 #endif
2319 #ifndef OPENSSL_NO_BF
2320     if (doit[D_CBC_BF]) {
2321         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2322             print_message(names[D_CBC_BF], c[D_CBC_BF][testnum], lengths[testnum]);
2323             if (async_jobs > 0) {
2324                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2325                 exit(1);
2326             }
2327             Time_F(START);
2328             for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2329                 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2330                                (unsigned long)lengths[testnum], &bf_ks,
2331                                iv, BF_ENCRYPT);
2332             d = Time_F(STOP);
2333             print_result(D_CBC_BF, testnum, count, d);
2334         }
2335     }
2336 #endif
2337 #ifndef OPENSSL_NO_CAST
2338     if (doit[D_CBC_CAST]) {
2339         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2340             print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum], lengths[testnum]);
2341             if (async_jobs > 0) {
2342                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2343                 exit(1);
2344             }
2345             Time_F(START);
2346             for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2347                 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2348                                  (unsigned long)lengths[testnum], &cast_ks,
2349                                  iv, CAST_ENCRYPT);
2350             d = Time_F(STOP);
2351             print_result(D_CBC_CAST, testnum, count, d);
2352         }
2353     }
2354 #endif
2355
2356     if (doit[D_EVP]) {
2357 #ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
2358         if (multiblock && evp_cipher) {
2359             if (!
2360                 (EVP_CIPHER_flags(evp_cipher) &
2361                  EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2362                 BIO_printf(bio_err, "%s is not multi-block capable\n",
2363                            OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2364                 goto end;
2365             }
2366             if (async_jobs > 0) {
2367                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2368                 exit(1);
2369             }
2370             multiblock_speed(evp_cipher);
2371             ret = 0;
2372             goto end;
2373         }
2374 #endif
2375         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2376             if (evp_cipher) {
2377
2378                 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2379                 /*
2380                  * -O3 -fschedule-insns messes up an optimization here!
2381                  * names[D_EVP] somehow becomes NULL
2382                  */
2383                 print_message(names[D_EVP], save_count, lengths[testnum]);
2384
2385                 for (k = 0; k < loopargs_len; k++) {
2386                     loopargs[k].ctx = EVP_CIPHER_CTX_new();
2387                     if (decrypt)
2388                         EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
2389                     else
2390                         EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
2391                     EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2392                 }
2393
2394                 Time_F(START);
2395                 count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
2396                 d = Time_F(STOP);
2397                 for (k = 0; k < loopargs_len; k++) {
2398                     EVP_CIPHER_CTX_free(loopargs[k].ctx);
2399                 }
2400             }
2401             if (evp_md) {
2402                 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2403                 print_message(names[D_EVP], save_count, lengths[testnum]);
2404                 Time_F(START);
2405                 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2406                 d = Time_F(STOP);
2407             }
2408             print_result(D_EVP, testnum, count, d);
2409         }
2410     }
2411
2412     for (i = 0; i < loopargs_len; i++)
2413         RAND_bytes(loopargs[i].buf, 36);
2414
2415 #ifndef OPENSSL_NO_RSA
2416     for (testnum = 0; testnum < RSA_NUM; testnum++) {
2417         int st = 0;
2418         if (!rsa_doit[testnum])
2419             continue;
2420         for (i = 0; i < loopargs_len; i++) {
2421             st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2422                           loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2423             if (st == 0)
2424                 break;
2425         }
2426         if (st == 0) {
2427             BIO_printf(bio_err,
2428                        "RSA sign failure.  No RSA sign will be done.\n");
2429             ERR_print_errors(bio_err);
2430             rsa_count = 1;
2431         } else {
2432             pkey_print_message("private", "rsa",
2433                                rsa_c[testnum][0], rsa_bits[testnum], RSA_SECONDS);
2434             /* RSA_blinding_on(rsa_key[testnum],NULL); */
2435             Time_F(START);
2436             count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2437             d = Time_F(STOP);
2438             BIO_printf(bio_err,
2439                        mr ? "+R1:%ld:%d:%.2f\n"
2440                        : "%ld %d bit private RSA's in %.2fs\n",
2441                        count, rsa_bits[testnum], d);
2442             rsa_results[testnum][0] = d / (double)count;
2443             rsa_count = count;
2444         }
2445
2446         for (i = 0; i < loopargs_len; i++) {
2447             st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2448                             *(loopargs[i].siglen), loopargs[i].rsa_key[testnum]);
2449             if (st <= 0)
2450                 break;
2451         }
2452         if (st <= 0) {
2453             BIO_printf(bio_err,
2454                        "RSA verify failure.  No RSA verify will be done.\n");
2455             ERR_print_errors(bio_err);
2456             rsa_doit[testnum] = 0;
2457         } else {
2458             pkey_print_message("public", "rsa",
2459                                rsa_c[testnum][1], rsa_bits[testnum], RSA_SECONDS);
2460             Time_F(START);
2461             count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2462             d = Time_F(STOP);
2463             BIO_printf(bio_err,
2464                        mr ? "+R2:%ld:%d:%.2f\n"
2465                        : "%ld %d bit public RSA's in %.2fs\n",
2466                        count, rsa_bits[testnum], d);
2467             rsa_results[testnum][1] = d / (double)count;
2468         }
2469
2470         if (rsa_count <= 1) {
2471             /* if longer than 10s, don't do any more */
2472             for (testnum++; testnum < RSA_NUM; testnum++)
2473                 rsa_doit[testnum] = 0;
2474         }
2475     }
2476 #endif
2477
2478     for (i = 0; i < loopargs_len; i++)
2479         RAND_bytes(loopargs[i].buf, 36);
2480
2481 #ifndef OPENSSL_NO_DSA
2482     if (RAND_status() != 1) {
2483         RAND_seed(rnd_seed, sizeof rnd_seed);
2484         rnd_fake = 1;
2485     }
2486     for (testnum = 0; testnum < DSA_NUM; testnum++) {
2487         int st = 0;
2488         if (!dsa_doit[testnum])
2489             continue;
2490
2491         /* DSA_generate_key(dsa_key[testnum]); */
2492         /* DSA_sign_setup(dsa_key[testnum],NULL); */
2493         for (i = 0; i < loopargs_len; i++) {
2494             st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2495                           loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2496             if (st == 0)
2497                 break;
2498         }
2499         if (st == 0) {
2500             BIO_printf(bio_err,
2501                        "DSA sign failure.  No DSA sign will be done.\n");
2502             ERR_print_errors(bio_err);
2503             rsa_count = 1;
2504         } else {
2505             pkey_print_message("sign", "dsa",
2506                                dsa_c[testnum][0], dsa_bits[testnum], DSA_SECONDS);
2507             Time_F(START);
2508             count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2509             d = Time_F(STOP);
2510             BIO_printf(bio_err,
2511                        mr ? "+R3:%ld:%d:%.2f\n"
2512                        : "%ld %d bit DSA signs in %.2fs\n",
2513                        count, dsa_bits[testnum], d);
2514             dsa_results[testnum][0] = d / (double)count;
2515             rsa_count = count;
2516         }
2517
2518         for (i = 0; i < loopargs_len; i++) {
2519             st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2520                             *(loopargs[i].siglen), loopargs[i].dsa_key[testnum]);
2521             if (st <= 0)
2522                 break;
2523         }
2524         if (st <= 0) {
2525             BIO_printf(bio_err,
2526                        "DSA verify failure.  No DSA verify will be done.\n");
2527             ERR_print_errors(bio_err);
2528             dsa_doit[testnum] = 0;
2529         } else {
2530             pkey_print_message("verify", "dsa",
2531                                dsa_c[testnum][1], dsa_bits[testnum], DSA_SECONDS);
2532             Time_F(START);
2533             count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2534             d = Time_F(STOP);
2535             BIO_printf(bio_err,
2536                        mr ? "+R4:%ld:%d:%.2f\n"
2537                        : "%ld %d bit DSA verify in %.2fs\n",
2538                        count, dsa_bits[testnum], d);
2539             dsa_results[testnum][1] = d / (double)count;
2540         }
2541
2542         if (rsa_count <= 1) {
2543             /* if longer than 10s, don't do any more */
2544             for (testnum++; testnum < DSA_NUM; testnum++)
2545                 dsa_doit[testnum] = 0;
2546         }
2547     }
2548     if (rnd_fake)
2549         RAND_cleanup();
2550 #endif
2551
2552 #ifndef OPENSSL_NO_EC
2553     if (RAND_status() != 1) {
2554         RAND_seed(rnd_seed, sizeof rnd_seed);
2555         rnd_fake = 1;
2556     }
2557     for (testnum = 0; testnum < EC_NUM; testnum++) {
2558         int st = 1;
2559
2560         if (!ecdsa_doit[testnum])
2561             continue;           /* Ignore Curve */
2562         for (i = 0; i < loopargs_len; i++) {
2563             loopargs[i].ecdsa[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2564             if (loopargs[i].ecdsa[testnum] == NULL) {
2565                 st = 0;
2566                 break;
2567             }
2568         }
2569         if (st == 0) {
2570             BIO_printf(bio_err, "ECDSA failure.\n");
2571             ERR_print_errors(bio_err);
2572             rsa_count = 1;
2573         } else {
2574             for (i = 0; i < loopargs_len; i++) {
2575                 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
2576                 /* Perform ECDSA signature test */
2577                 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
2578                 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2579                                 loopargs[i].siglen, loopargs[i].ecdsa[testnum]);
2580                 if (st == 0)
2581                     break;
2582             }
2583             if (st == 0) {
2584                 BIO_printf(bio_err,
2585                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
2586                 ERR_print_errors(bio_err);
2587                 rsa_count = 1;
2588             } else {
2589                 pkey_print_message("sign", "ecdsa",
2590                                    ecdsa_c[testnum][0],
2591                                    test_curves_bits[testnum], ECDSA_SECONDS);
2592                 Time_F(START);
2593                 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2594                 d = Time_F(STOP);
2595
2596                 BIO_printf(bio_err,
2597                            mr ? "+R5:%ld:%d:%.2f\n" :
2598                            "%ld %d bit ECDSA signs in %.2fs \n",
2599                            count, test_curves_bits[testnum], d);
2600                 ecdsa_results[testnum][0] = d / (double)count;
2601                 rsa_count = count;
2602             }
2603
2604             /* Perform ECDSA verification test */
2605             for (i = 0; i < loopargs_len; i++) {
2606                 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2607                                   *(loopargs[i].siglen), loopargs[i].ecdsa[testnum]);
2608                 if (st != 1)
2609                     break;
2610             }
2611             if (st != 1) {
2612                 BIO_printf(bio_err,
2613                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
2614                 ERR_print_errors(bio_err);
2615                 ecdsa_doit[testnum] = 0;
2616             } else {
2617                 pkey_print_message("verify", "ecdsa",
2618                                    ecdsa_c[testnum][1],
2619                                    test_curves_bits[testnum], ECDSA_SECONDS);
2620                 Time_F(START);
2621                 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2622                 d = Time_F(STOP);
2623                 BIO_printf(bio_err,
2624                            mr ? "+R6:%ld:%d:%.2f\n"
2625                            : "%ld %d bit ECDSA verify in %.2fs\n",
2626                            count, test_curves_bits[testnum], d);
2627                 ecdsa_results[testnum][1] = d / (double)count;
2628             }
2629
2630             if (rsa_count <= 1) {
2631                 /* if longer than 10s, don't do any more */
2632                 for (testnum++; testnum < EC_NUM; testnum++)
2633                     ecdsa_doit[testnum] = 0;
2634             }
2635         }
2636     }
2637     if (rnd_fake)
2638         RAND_cleanup();
2639 #endif
2640
2641 #ifndef OPENSSL_NO_EC
2642     if (RAND_status() != 1) {
2643         RAND_seed(rnd_seed, sizeof rnd_seed);
2644         rnd_fake = 1;
2645     }
2646     for (testnum = 0; testnum < EC_NUM; testnum++) {
2647         if (!ecdh_doit[testnum])
2648             continue;
2649         for (i = 0; i < loopargs_len; i++) {
2650             loopargs[i].ecdh_a[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2651             loopargs[i].ecdh_b[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2652             if (loopargs[i].ecdh_a[testnum] == NULL ||
2653                 loopargs[i].ecdh_b[testnum] == NULL) {
2654                 ecdh_checks = 0;
2655                 break;
2656             }
2657         }
2658         if (ecdh_checks == 0) {
2659             BIO_printf(bio_err, "ECDH failure.\n");
2660             ERR_print_errors(bio_err);
2661             rsa_count = 1;
2662         } else {
2663             for (i = 0; i < loopargs_len; i++) {
2664                 /* generate two ECDH key pairs */
2665                 if (!EC_KEY_generate_key(loopargs[i].ecdh_a[testnum]) ||
2666                         !EC_KEY_generate_key(loopargs[i].ecdh_b[testnum])) {
2667                     BIO_printf(bio_err, "ECDH key generation failure.\n");
2668                     ERR_print_errors(bio_err);
2669                     ecdh_checks = 0;
2670                     rsa_count = 1;
2671                 } else {
2672                     /*
2673                      * If field size is not more than 24 octets, then use SHA-1
2674                      * hash of result; otherwise, use result (see section 4.8 of
2675                      * draft-ietf-tls-ecc-03.txt).
2676                      */
2677                     int field_size;
2678                     field_size =
2679                         EC_GROUP_get_degree(EC_KEY_get0_group(loopargs[i].ecdh_a[testnum]));
2680                     if (field_size <= 24 * 8) {
2681                         outlen = KDF1_SHA1_len;
2682                         kdf = KDF1_SHA1;
2683                     } else {
2684                         outlen = (field_size + 7) / 8;
2685                         kdf = NULL;
2686                     }
2687                     secret_size_a =
2688                         ECDH_compute_key(loopargs[i].secret_a, outlen,
2689                                 EC_KEY_get0_public_key(loopargs[i].ecdh_b[testnum]),
2690                                 loopargs[i].ecdh_a[testnum], kdf);
2691                     secret_size_b =
2692                         ECDH_compute_key(loopargs[i].secret_b, outlen,
2693                                 EC_KEY_get0_public_key(loopargs[i].ecdh_a[testnum]),
2694                                 loopargs[i].ecdh_b[testnum], kdf);
2695                     if (secret_size_a != secret_size_b)
2696                         ecdh_checks = 0;
2697                     else
2698                         ecdh_checks = 1;
2699
2700                     for (secret_idx = 0; (secret_idx < secret_size_a)
2701                             && (ecdh_checks == 1); secret_idx++) {
2702                         if (loopargs[i].secret_a[secret_idx] != loopargs[i].secret_b[secret_idx])
2703                             ecdh_checks = 0;
2704                     }
2705
2706                     if (ecdh_checks == 0) {
2707                         BIO_printf(bio_err, "ECDH computations don't match.\n");
2708                         ERR_print_errors(bio_err);
2709                         rsa_count = 1;
2710                         break;
2711                     }
2712                 }
2713                 if (ecdh_checks != 0) {
2714                     pkey_print_message("", "ecdh",
2715                             ecdh_c[testnum][0],
2716                             test_curves_bits[testnum], ECDH_SECONDS);
2717                     Time_F(START);
2718                     count = run_benchmark(async_jobs, ECDH_compute_key_loop, loopargs);
2719                     d = Time_F(STOP);
2720                     BIO_printf(bio_err,
2721                             mr ? "+R7:%ld:%d:%.2f\n" :
2722                             "%ld %d-bit ECDH ops in %.2fs\n", count,
2723                             test_curves_bits[testnum], d);
2724                     ecdh_results[testnum][0] = d / (double)count;
2725                     rsa_count = count;
2726                 }
2727             }
2728         }
2729
2730         if (rsa_count <= 1) {
2731             /* if longer than 10s, don't do any more */
2732             for (testnum++; testnum < EC_NUM; testnum++)
2733                 ecdh_doit[testnum] = 0;
2734         }
2735     }
2736     if (rnd_fake)
2737         RAND_cleanup();
2738 #endif
2739 #ifndef NO_FORK
2740  show_res:
2741 #endif
2742     if (!mr) {
2743         printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
2744         printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2745         printf("options:");
2746         printf("%s ", BN_options());
2747 #ifndef OPENSSL_NO_MD2
2748         printf("%s ", MD2_options());
2749 #endif
2750 #ifndef OPENSSL_NO_RC4
2751         printf("%s ", RC4_options());
2752 #endif
2753 #ifndef OPENSSL_NO_DES
2754         printf("%s ", DES_options());
2755 #endif
2756 #ifndef OPENSSL_NO_AES
2757         printf("%s ", AES_options());
2758 #endif
2759 #ifndef OPENSSL_NO_IDEA
2760         printf("%s ", idea_options());
2761 #endif
2762 #ifndef OPENSSL_NO_BF
2763         printf("%s ", BF_options());
2764 #endif
2765         printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2766     }
2767
2768     if (pr_header) {
2769         if (mr)
2770             printf("+H");
2771         else {
2772             printf
2773                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2774             printf("type        ");
2775         }
2776         for (testnum = 0; testnum < SIZE_NUM; testnum++)
2777             printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2778         printf("\n");
2779     }
2780
2781     for (k = 0; k < ALGOR_NUM; k++) {
2782         if (!doit[k])
2783             continue;
2784         if (mr)
2785             printf("+F:%d:%s", k, names[k]);
2786         else
2787             printf("%-13s", names[k]);
2788         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2789             if (results[k][testnum] > 10000 && !mr)
2790                 printf(" %11.2fk", results[k][testnum] / 1e3);
2791             else
2792                 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2793         }
2794         printf("\n");
2795     }
2796 #ifndef OPENSSL_NO_RSA
2797     testnum = 1;
2798     for (k = 0; k < RSA_NUM; k++) {
2799         if (!rsa_doit[k])
2800             continue;
2801         if (testnum && !mr) {
2802             printf("%18ssign    verify    sign/s verify/s\n", " ");
2803             testnum = 0;
2804         }
2805         if (mr)
2806             printf("+F2:%u:%u:%f:%f\n",
2807                    k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2808         else
2809             printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2810                    rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2811                    1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
2812     }
2813 #endif
2814 #ifndef OPENSSL_NO_DSA
2815     testnum = 1;
2816     for (k = 0; k < DSA_NUM; k++) {
2817         if (!dsa_doit[k])
2818             continue;
2819         if (testnum && !mr) {
2820             printf("%18ssign    verify    sign/s verify/s\n", " ");
2821             testnum = 0;
2822         }
2823         if (mr)
2824             printf("+F3:%u:%u:%f:%f\n",
2825                    k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2826         else
2827             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2828                    dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2829                    1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
2830     }
2831 #endif
2832 #ifndef OPENSSL_NO_EC
2833     testnum = 1;
2834     for (k = 0; k < EC_NUM; k++) {
2835         if (!ecdsa_doit[k])
2836             continue;
2837         if (testnum && !mr) {
2838             printf("%30ssign    verify    sign/s verify/s\n", " ");
2839             testnum = 0;
2840         }
2841
2842         if (mr)
2843             printf("+F4:%u:%u:%f:%f\n",
2844                    k, test_curves_bits[k],
2845                    ecdsa_results[k][0], ecdsa_results[k][1]);
2846         else
2847             printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2848                    test_curves_bits[k],
2849                    test_curves_names[k],
2850                    ecdsa_results[k][0], ecdsa_results[k][1],
2851                    1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
2852     }
2853 #endif
2854
2855 #ifndef OPENSSL_NO_EC
2856     testnum = 1;
2857     for (k = 0; k < EC_NUM; k++) {
2858         if (!ecdh_doit[k])
2859             continue;
2860         if (testnum && !mr) {
2861             printf("%30sop      op/s\n", " ");
2862             testnum = 0;
2863         }
2864         if (mr)
2865             printf("+F5:%u:%u:%f:%f\n",
2866                    k, test_curves_bits[k],
2867                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2868
2869         else
2870             printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2871                    test_curves_bits[k],
2872                    test_curves_names[k],
2873                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2874     }
2875 #endif
2876
2877     ret = 0;
2878
2879  end:
2880     ERR_print_errors(bio_err);
2881     for (i = 0; i < loopargs_len; i++) {
2882         OPENSSL_free(loopargs[i].buf_malloc);
2883         OPENSSL_free(loopargs[i].buf2_malloc);
2884         OPENSSL_free(loopargs[i].siglen);
2885     }
2886 #ifndef OPENSSL_NO_RSA
2887     for (i = 0; i < loopargs_len; i++) {
2888         for (k = 0; k < RSA_NUM; k++)
2889             RSA_free(loopargs[i].rsa_key[k]);
2890     }
2891 #endif
2892 #ifndef OPENSSL_NO_DSA
2893     for (i = 0; i < loopargs_len; i++) {
2894         for (k = 0; k < DSA_NUM; k++)
2895             DSA_free(loopargs[i].dsa_key[k]);
2896     }
2897 #endif
2898
2899 #ifndef OPENSSL_NO_EC
2900     for (i = 0; i < loopargs_len; i++) {
2901         for (k = 0; k < EC_NUM; k++) {
2902             EC_KEY_free(loopargs[i].ecdsa[k]);
2903             EC_KEY_free(loopargs[i].ecdh_a[k]);
2904             EC_KEY_free(loopargs[i].ecdh_b[k]);
2905         }
2906         OPENSSL_free(loopargs[i].secret_a);
2907         OPENSSL_free(loopargs[i].secret_b);
2908     }
2909 #endif
2910     if (async_jobs > 0) {
2911         for (i = 0; i < loopargs_len; i++)
2912             ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
2913
2914         ASYNC_cleanup_thread();
2915     }
2916     OPENSSL_free(loopargs);
2917     return (ret);
2918 }
2919
2920 static void print_message(const char *s, long num, int length)
2921 {
2922 #ifdef SIGALRM
2923     BIO_printf(bio_err,
2924                mr ? "+DT:%s:%d:%d\n"
2925                : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2926     (void)BIO_flush(bio_err);
2927     alarm(SECONDS);
2928 #else
2929     BIO_printf(bio_err,
2930                mr ? "+DN:%s:%ld:%d\n"
2931                : "Doing %s %ld times on %d size blocks: ", s, num, length);
2932     (void)BIO_flush(bio_err);
2933 #endif
2934 }
2935
2936 static void pkey_print_message(const char *str, const char *str2, long num,
2937                                int bits, int tm)
2938 {
2939 #ifdef SIGALRM
2940     BIO_printf(bio_err,
2941                mr ? "+DTP:%d:%s:%s:%d\n"
2942                : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2943     (void)BIO_flush(bio_err);
2944     alarm(tm);
2945 #else
2946     BIO_printf(bio_err,
2947                mr ? "+DNP:%ld:%d:%s:%s\n"
2948                : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2949     (void)BIO_flush(bio_err);
2950 #endif
2951 }
2952
2953 static void print_result(int alg, int run_no, int count, double time_used)
2954 {
2955     BIO_printf(bio_err,
2956                mr ? "+R:%d:%s:%f\n"
2957                : "%d %s's in %.2fs\n", count, names[alg], time_used);
2958     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2959 }
2960
2961 #ifndef NO_FORK
2962 static char *sstrsep(char **string, const char *delim)
2963 {
2964     char isdelim[256];
2965     char *token = *string;
2966
2967     if (**string == 0)
2968         return NULL;
2969
2970     memset(isdelim, 0, sizeof isdelim);
2971     isdelim[0] = 1;
2972
2973     while (*delim) {
2974         isdelim[(unsigned char)(*delim)] = 1;
2975         delim++;
2976     }
2977
2978     while (!isdelim[(unsigned char)(**string)]) {
2979         (*string)++;
2980     }
2981
2982     if (**string) {
2983         **string = 0;
2984         (*string)++;
2985     }
2986
2987     return token;
2988 }
2989
2990 static int do_multi(int multi)
2991 {
2992     int n;
2993     int fd[2];
2994     int *fds;
2995     static char sep[] = ":";
2996
2997     fds = malloc(sizeof(*fds) * multi);
2998     for (n = 0; n < multi; ++n) {
2999         if (pipe(fd) == -1) {
3000             BIO_printf(bio_err, "pipe failure\n");
3001             exit(1);
3002         }
3003         fflush(stdout);
3004         (void)BIO_flush(bio_err);
3005         if (fork()) {
3006             close(fd[1]);
3007             fds[n] = fd[0];
3008         } else {
3009             close(fd[0]);
3010             close(1);
3011             if (dup(fd[1]) == -1) {
3012                 BIO_printf(bio_err, "dup failed\n");
3013                 exit(1);
3014             }
3015             close(fd[1]);
3016             mr = 1;
3017             usertime = 0;
3018             free(fds);
3019             return 0;
3020         }
3021         printf("Forked child %d\n", n);
3022     }
3023
3024     /* for now, assume the pipe is long enough to take all the output */
3025     for (n = 0; n < multi; ++n) {
3026         FILE *f;
3027         char buf[1024];
3028         char *p;
3029
3030         f = fdopen(fds[n], "r");
3031         while (fgets(buf, sizeof buf, f)) {
3032             p = strchr(buf, '\n');
3033             if (p)
3034                 *p = '\0';
3035             if (buf[0] != '+') {
3036                 BIO_printf(bio_err, "Don't understand line '%s' from child %d\n",
3037                         buf, n);
3038                 continue;
3039             }
3040             printf("Got: %s from %d\n", buf, n);
3041             if (strncmp(buf, "+F:", 3) == 0) {
3042                 int alg;
3043                 int j;
3044
3045                 p = buf + 3;
3046                 alg = atoi(sstrsep(&p, sep));
3047                 sstrsep(&p, sep);
3048                 for (j = 0; j < SIZE_NUM; ++j)
3049                     results[alg][j] += atof(sstrsep(&p, sep));
3050             } else if (strncmp(buf, "+F2:", 4) == 0) {
3051                 int k;
3052                 double d;
3053
3054                 p = buf + 4;
3055                 k = atoi(sstrsep(&p, sep));
3056                 sstrsep(&p, sep);
3057
3058                 d = atof(sstrsep(&p, sep));
3059                 if (n)
3060                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
3061                 else
3062                     rsa_results[k][0] = d;
3063
3064                 d = atof(sstrsep(&p, sep));
3065                 if (n)
3066                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
3067                 else
3068                     rsa_results[k][1] = d;
3069             }
3070 # ifndef OPENSSL_NO_DSA
3071             else if (strncmp(buf, "+F3:", 4) == 0) {
3072                 int k;
3073                 double d;
3074
3075                 p = buf + 4;
3076                 k = atoi(sstrsep(&p, sep));
3077                 sstrsep(&p, sep);
3078
3079                 d = atof(sstrsep(&p, sep));
3080                 if (n)
3081                     dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
3082                 else
3083                     dsa_results[k][0] = d;
3084
3085                 d = atof(sstrsep(&p, sep));
3086                 if (n)
3087                     dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
3088                 else
3089                     dsa_results[k][1] = d;
3090             }
3091 # endif
3092 # ifndef OPENSSL_NO_EC
3093             else if (strncmp(buf, "+F4:", 4) == 0) {
3094                 int k;
3095                 double d;
3096
3097                 p = buf + 4;
3098                 k = atoi(sstrsep(&p, sep));
3099                 sstrsep(&p, sep);
3100
3101                 d = atof(sstrsep(&p, sep));
3102                 if (n)
3103                     ecdsa_results[k][0] =
3104                         1 / (1 / ecdsa_results[k][0] + 1 / d);
3105                 else
3106                     ecdsa_results[k][0] = d;
3107
3108                 d = atof(sstrsep(&p, sep));
3109                 if (n)
3110                     ecdsa_results[k][1] =
3111                         1 / (1 / ecdsa_results[k][1] + 1 / d);
3112                 else
3113                     ecdsa_results[k][1] = d;
3114             }
3115 # endif
3116
3117 # ifndef OPENSSL_NO_EC
3118             else if (strncmp(buf, "+F5:", 4) == 0) {
3119                 int k;
3120                 double d;
3121
3122                 p = buf + 4;
3123                 k = atoi(sstrsep(&p, sep));
3124                 sstrsep(&p, sep);
3125
3126                 d = atof(sstrsep(&p, sep));
3127                 if (n)
3128                     ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
3129                 else
3130                     ecdh_results[k][0] = d;
3131
3132             }
3133 # endif
3134
3135             else if (strncmp(buf, "+H:", 3) == 0) {
3136                 ;
3137             } else
3138                 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf, n);
3139         }
3140
3141         fclose(f);
3142     }
3143     free(fds);
3144     return 1;
3145 }
3146 #endif
3147
3148 static void multiblock_speed(const EVP_CIPHER *evp_cipher)
3149 {
3150     static int mblengths[] =
3151         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3152     int j, count, num = OSSL_NELEM(mblengths);
3153     const char *alg_name;
3154     unsigned char *inp, *out, no_key[32], no_iv[16];
3155     EVP_CIPHER_CTX *ctx;
3156     double d = 0.0;
3157
3158     inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
3159     out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3160     ctx = EVP_CIPHER_CTX_new();
3161     EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3162     EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
3163                         no_key);
3164     alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3165
3166     for (j = 0; j < num; j++) {
3167         print_message(alg_name, 0, mblengths[j]);
3168         Time_F(START);
3169         for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3170             unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3171             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
3172             size_t len = mblengths[j];
3173             int packlen;
3174
3175             memset(aad, 0, 8);  /* avoid uninitialized values */
3176             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
3177             aad[9] = 3;         /* version */
3178             aad[10] = 2;
3179             aad[11] = 0;        /* length */
3180             aad[12] = 0;
3181             mb_param.out = NULL;
3182             mb_param.inp = aad;
3183             mb_param.len = len;
3184             mb_param.interleave = 8;
3185
3186             packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3187                                           sizeof(mb_param), &mb_param);
3188
3189             if (packlen > 0) {
3190                 mb_param.out = out;
3191                 mb_param.inp = inp;
3192                 mb_param.len = len;
3193                 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3194                                     sizeof(mb_param), &mb_param);
3195             } else {
3196                 int pad;
3197
3198                 RAND_bytes(out, 16);
3199                 len += 16;
3200                 aad[11] = len >> 8;
3201                 aad[12] = len;
3202                 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3203                                           EVP_AEAD_TLS1_AAD_LEN, aad);
3204                 EVP_Cipher(ctx, out, inp, len + pad);
3205             }
3206         }
3207         d = Time_F(STOP);
3208         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3209                    : "%d %s's in %.2fs\n", count, "evp", d);
3210         results[D_EVP][j] = ((double)count) / d * mblengths[j];
3211     }
3212
3213     if (mr) {
3214         fprintf(stdout, "+H");
3215         for (j = 0; j < num; j++)
3216             fprintf(stdout, ":%d", mblengths[j]);
3217         fprintf(stdout, "\n");
3218         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
3219         for (j = 0; j < num; j++)
3220             fprintf(stdout, ":%.2f", results[D_EVP][j]);
3221         fprintf(stdout, "\n");
3222     } else {
3223         fprintf(stdout,
3224                 "The 'numbers' are in 1000s of bytes per second processed.\n");
3225         fprintf(stdout, "type                    ");
3226         for (j = 0; j < num; j++)
3227             fprintf(stdout, "%7d bytes", mblengths[j]);
3228         fprintf(stdout, "\n");
3229         fprintf(stdout, "%-24s", alg_name);
3230
3231         for (j = 0; j < num; j++) {
3232             if (results[D_EVP][j] > 10000)
3233                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
3234             else
3235                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
3236         }
3237         fprintf(stdout, "\n");
3238     }
3239
3240     OPENSSL_free(inp);
3241     OPENSSL_free(out);
3242     EVP_CIPHER_CTX_free(ctx);
3243 }