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