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