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