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