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