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