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