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