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