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