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