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