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