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