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