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