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