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