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