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