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