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