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