Fix no-des
[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 #ifndef OPENSSL_NO_DES
1390     memset(DES_iv, 0, sizeof(DES_iv));
1391 #endif
1392     memset(iv, 0, sizeof(iv));
1393
1394     for (i = 0; i < ALGOR_NUM; i++)
1395         doit[i] = 0;
1396     for (i = 0; i < RSA_NUM; i++)
1397         rsa_doit[i] = 0;
1398     for (i = 0; i < DSA_NUM; i++)
1399         dsa_doit[i] = 0;
1400 #ifndef OPENSSL_NO_EC
1401     for (i = 0; i < EC_NUM; i++)
1402         ecdsa_doit[i] = 0;
1403     for (i = 0; i < EC_NUM; i++)
1404         ecdh_doit[i] = 0;
1405 #endif
1406
1407     misalign = 0;
1408
1409     prog = opt_init(argc, argv, speed_options);
1410     while ((o = opt_next()) != OPT_EOF) {
1411         switch (o) {
1412         case OPT_EOF:
1413         case OPT_ERR:
1414  opterr:
1415             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1416             goto end;
1417         case OPT_HELP:
1418             opt_help(speed_options);
1419             ret = 0;
1420             goto end;
1421         case OPT_ELAPSED:
1422             usertime = 0;
1423             break;
1424         case OPT_EVP:
1425             evp_cipher = EVP_get_cipherbyname(opt_arg());
1426             if (evp_cipher == NULL)
1427                 evp_md = EVP_get_digestbyname(opt_arg());
1428             if (evp_cipher == NULL && evp_md == NULL) {
1429                 BIO_printf(bio_err,
1430                            "%s: %s  an unknown cipher or digest\n",
1431                            prog, opt_arg());
1432                 goto end;
1433             }
1434             doit[D_EVP] = 1;
1435             break;
1436         case OPT_DECRYPT:
1437             decrypt = 1;
1438             break;
1439         case OPT_ENGINE:
1440             /*
1441              * In a forked execution, an engine might need to be
1442              * initialised by each child process, not by the parent.
1443              * So store the name here and run setup_engine() later on.
1444              */
1445             engine_id = opt_arg();
1446             break;
1447         case OPT_MULTI:
1448 #ifndef NO_FORK
1449             multi = atoi(opt_arg());
1450 #endif
1451             break;
1452         case OPT_ASYNCJOBS:
1453 #ifndef OPENSSL_NO_ASYNC
1454             async_jobs = atoi(opt_arg());
1455             if (!ASYNC_is_capable()) {
1456                 BIO_printf(bio_err,
1457                            "%s: async_jobs specified but async not supported\n",
1458                            prog);
1459                 goto opterr;
1460             }
1461 #endif
1462             break;
1463         case OPT_MISALIGN:
1464             if (!opt_int(opt_arg(), &misalign))
1465                 goto end;
1466             if (misalign > MISALIGN) {
1467                 BIO_printf(bio_err,
1468                            "%s: Maximum offset is %d\n", prog, MISALIGN);
1469                 goto opterr;
1470             }
1471             break;
1472         case OPT_MR:
1473             mr = 1;
1474             break;
1475         case OPT_MB:
1476             multiblock = 1;
1477             break;
1478         }
1479     }
1480     argc = opt_num_rest();
1481     argv = opt_rest();
1482
1483     /* Remaining arguments are algorithms. */
1484     for ( ; *argv; argv++) {
1485         if (found(*argv, doit_choices, &i)) {
1486             doit[i] = 1;
1487             continue;
1488         }
1489 #ifndef OPENSSL_NO_DES
1490         if (strcmp(*argv, "des") == 0) {
1491             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1492             continue;
1493         }
1494 #endif
1495         if (strcmp(*argv, "sha") == 0) {
1496             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1497             continue;
1498         }
1499 #ifndef OPENSSL_NO_RSA
1500 # ifndef RSA_NULL
1501         if (strcmp(*argv, "openssl") == 0) {
1502             RSA_set_default_method(RSA_PKCS1_OpenSSL());
1503             continue;
1504         }
1505 # endif
1506         if (strcmp(*argv, "rsa") == 0) {
1507             rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
1508                 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
1509                 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
1510                 rsa_doit[R_RSA_15360] = 1;
1511             continue;
1512         }
1513         if (found(*argv, rsa_choices, &i)) {
1514             rsa_doit[i] = 1;
1515             continue;
1516         }
1517 #endif
1518 #ifndef OPENSSL_NO_DSA
1519         if (strcmp(*argv, "dsa") == 0) {
1520             dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1521                 dsa_doit[R_DSA_2048] = 1;
1522             continue;
1523         }
1524         if (found(*argv, dsa_choices, &i)) {
1525             dsa_doit[i] = 2;
1526             continue;
1527         }
1528 #endif
1529 #ifndef OPENSSL_NO_AES
1530         if (strcmp(*argv, "aes") == 0) {
1531             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =
1532                 doit[D_CBC_256_AES] = 1;
1533             continue;
1534         }
1535 #endif
1536 #ifndef OPENSSL_NO_CAMELLIA
1537         if (strcmp(*argv, "camellia") == 0) {
1538             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =
1539                 doit[D_CBC_256_CML] = 1;
1540             continue;
1541         }
1542 #endif
1543 #ifndef OPENSSL_NO_EC
1544         if (strcmp(*argv, "ecdsa") == 0) {
1545             for (i = 0; i < EC_NUM; i++)
1546                 ecdsa_doit[i] = 1;
1547             continue;
1548         }
1549         if (found(*argv, ecdsa_choices, &i)) {
1550             ecdsa_doit[i] = 2;
1551             continue;
1552         }
1553         if (strcmp(*argv, "ecdh") == 0) {
1554             for (i = 0; i < EC_NUM; i++)
1555                 ecdh_doit[i] = 1;
1556             continue;
1557         }
1558         if (found(*argv, ecdh_choices, &i)) {
1559             ecdh_doit[i] = 2;
1560             continue;
1561         }
1562 #endif
1563         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1564         goto end;
1565     }
1566
1567     /* Initialize the job pool if async mode is enabled */
1568     if (async_jobs > 0) {
1569         if (!ASYNC_init_thread(async_jobs, async_jobs)) {
1570             BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1571             goto end;
1572         }
1573     }
1574
1575     loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1576     loopargs = app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1577     memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1578
1579     for (i = 0; i < loopargs_len; i++) {
1580         if (async_jobs > 0) {
1581             loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1582             if (loopargs[i].wait_ctx == NULL) {
1583                 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1584                 goto end;
1585             }
1586         }
1587
1588         loopargs[i].buf_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1589         loopargs[i].buf2_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1590         /* Align the start of buffers on a 64 byte boundary */
1591         loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1592         loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1593         loopargs[i].siglen = app_malloc(sizeof(unsigned int), "signature length");
1594 #ifndef OPENSSL_NO_EC
1595         loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1596         loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1597 #endif
1598     }
1599
1600 #ifndef NO_FORK
1601     if (multi && do_multi(multi))
1602         goto show_res;
1603 #endif
1604
1605     /* Initialize the engine after the fork */
1606     (void)setup_engine(engine_id, 0);
1607
1608     /* No parameters; turn on everything. */
1609     if ((argc == 0) && !doit[D_EVP]) {
1610         for (i = 0; i < ALGOR_NUM; i++)
1611             if (i != D_EVP)
1612                 doit[i] = 1;
1613         for (i = 0; i < RSA_NUM; i++)
1614             rsa_doit[i] = 1;
1615         for (i = 0; i < DSA_NUM; i++)
1616             dsa_doit[i] = 1;
1617 #ifndef OPENSSL_NO_EC
1618         for (i = 0; i < EC_NUM; i++)
1619             ecdsa_doit[i] = 1;
1620         for (i = 0; i < EC_NUM; i++)
1621             ecdh_doit[i] = 1;
1622 #endif
1623     }
1624     for (i = 0; i < ALGOR_NUM; i++)
1625         if (doit[i])
1626             pr_header++;
1627
1628     if (usertime == 0 && !mr)
1629         BIO_printf(bio_err,
1630                    "You have chosen to measure elapsed time "
1631                    "instead of user CPU time.\n");
1632
1633 #ifndef OPENSSL_NO_RSA
1634     for (i = 0; i < loopargs_len; i++) {
1635         for (k = 0; k < RSA_NUM; k++) {
1636             const unsigned char *p;
1637
1638             p = rsa_data[k];
1639             loopargs[i].rsa_key[k] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1640             if (loopargs[i].rsa_key[k] == NULL) {
1641                 BIO_printf(bio_err, "internal error loading RSA key number %d\n",
1642                         k);
1643                 goto end;
1644             }
1645         }
1646     }
1647 #endif
1648 #ifndef OPENSSL_NO_DSA
1649     for (i = 0; i < loopargs_len; i++) {
1650         loopargs[i].dsa_key[0] = get_dsa512();
1651         loopargs[i].dsa_key[1] = get_dsa1024();
1652         loopargs[i].dsa_key[2] = get_dsa2048();
1653     }
1654 #endif
1655 #ifndef OPENSSL_NO_DES
1656     DES_set_key_unchecked(&key, &sch);
1657     DES_set_key_unchecked(&key2, &sch2);
1658     DES_set_key_unchecked(&key3, &sch3);
1659 #endif
1660 #ifndef OPENSSL_NO_AES
1661     AES_set_encrypt_key(key16, 128, &aes_ks1);
1662     AES_set_encrypt_key(key24, 192, &aes_ks2);
1663     AES_set_encrypt_key(key32, 256, &aes_ks3);
1664 #endif
1665 #ifndef OPENSSL_NO_CAMELLIA
1666     Camellia_set_key(key16, 128, &camellia_ks1);
1667     Camellia_set_key(ckey24, 192, &camellia_ks2);
1668     Camellia_set_key(ckey32, 256, &camellia_ks3);
1669 #endif
1670 #ifndef OPENSSL_NO_IDEA
1671     idea_set_encrypt_key(key16, &idea_ks);
1672 #endif
1673 #ifndef OPENSSL_NO_SEED
1674     SEED_set_key(key16, &seed_ks);
1675 #endif
1676 #ifndef OPENSSL_NO_RC4
1677     RC4_set_key(&rc4_ks, 16, key16);
1678 #endif
1679 #ifndef OPENSSL_NO_RC2
1680     RC2_set_key(&rc2_ks, 16, key16, 128);
1681 #endif
1682 #ifndef OPENSSL_NO_RC5
1683     RC5_32_set_key(&rc5_ks, 16, key16, 12);
1684 #endif
1685 #ifndef OPENSSL_NO_BF
1686     BF_set_key(&bf_ks, 16, key16);
1687 #endif
1688 #ifndef OPENSSL_NO_CAST
1689     CAST_set_key(&cast_ks, 16, key16);
1690 #endif
1691 #ifndef OPENSSL_NO_RSA
1692     memset(rsa_c, 0, sizeof(rsa_c));
1693 #endif
1694 #ifndef SIGALRM
1695 # ifndef OPENSSL_NO_DES
1696     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1697     count = 10;
1698     do {
1699         long it;
1700         count *= 2;
1701         Time_F(START);
1702         for (it = count; it; it--)
1703             DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
1704                             (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1705         d = Time_F(STOP);
1706     } while (d < 3);
1707     save_count = count;
1708     c[D_MD2][0] = count / 10;
1709     c[D_MDC2][0] = count / 10;
1710     c[D_MD4][0] = count;
1711     c[D_MD5][0] = count;
1712     c[D_HMAC][0] = count;
1713     c[D_SHA1][0] = count;
1714     c[D_RMD160][0] = count;
1715     c[D_RC4][0] = count * 5;
1716     c[D_CBC_DES][0] = count;
1717     c[D_EDE3_DES][0] = count / 3;
1718     c[D_CBC_IDEA][0] = count;
1719     c[D_CBC_SEED][0] = count;
1720     c[D_CBC_RC2][0] = count;
1721     c[D_CBC_RC5][0] = count;
1722     c[D_CBC_BF][0] = count;
1723     c[D_CBC_CAST][0] = count;
1724     c[D_CBC_128_AES][0] = count;
1725     c[D_CBC_192_AES][0] = count;
1726     c[D_CBC_256_AES][0] = count;
1727     c[D_CBC_128_CML][0] = count;
1728     c[D_CBC_192_CML][0] = count;
1729     c[D_CBC_256_CML][0] = count;
1730     c[D_SHA256][0] = count;
1731     c[D_SHA512][0] = count;
1732     c[D_WHIRLPOOL][0] = count;
1733     c[D_IGE_128_AES][0] = count;
1734     c[D_IGE_192_AES][0] = count;
1735     c[D_IGE_256_AES][0] = count;
1736     c[D_GHASH][0] = count;
1737
1738     for (i = 1; i < SIZE_NUM; i++) {
1739         long l0, l1;
1740
1741         l0 = (long)lengths[0];
1742         l1 = (long)lengths[i];
1743
1744         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1745         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1746         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1747         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1748         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1749         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1750         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1751         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1752         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1753         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1754         c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1755
1756         l0 = (long)lengths[i - 1];
1757
1758         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1759         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1760         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1761         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1762         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1763         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1764         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1765         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1766         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1767         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1768         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1769         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1770         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1771         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1772         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1773         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1774         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1775         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1776     }
1777
1778 #  ifndef OPENSSL_NO_RSA
1779     rsa_c[R_RSA_512][0] = count / 2000;
1780     rsa_c[R_RSA_512][1] = count / 400;
1781     for (i = 1; i < RSA_NUM; i++) {
1782         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1783         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1784         if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
1785             rsa_doit[i] = 0;
1786         else {
1787             if (rsa_c[i][0] == 0) {
1788                 rsa_c[i][0] = 1;
1789                 rsa_c[i][1] = 20;
1790             }
1791         }
1792     }
1793 #  endif
1794
1795 #  ifndef OPENSSL_NO_DSA
1796     dsa_c[R_DSA_512][0] = count / 1000;
1797     dsa_c[R_DSA_512][1] = count / 1000 / 2;
1798     for (i = 1; i < DSA_NUM; i++) {
1799         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1800         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1801         if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
1802             dsa_doit[i] = 0;
1803         else {
1804             if (dsa_c[i] == 0) {
1805                 dsa_c[i][0] = 1;
1806                 dsa_c[i][1] = 1;
1807             }
1808         }
1809     }
1810 #  endif
1811
1812 #  ifndef OPENSSL_NO_EC
1813     ecdsa_c[R_EC_P160][0] = count / 1000;
1814     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1815     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1816         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1817         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1818         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1819             ecdsa_doit[i] = 0;
1820         else {
1821             if (ecdsa_c[i] == 0) {
1822                 ecdsa_c[i][0] = 1;
1823                 ecdsa_c[i][1] = 1;
1824             }
1825         }
1826     }
1827     ecdsa_c[R_EC_K163][0] = count / 1000;
1828     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1829     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1830         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1831         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1832         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1833             ecdsa_doit[i] = 0;
1834         else {
1835             if (ecdsa_c[i] == 0) {
1836                 ecdsa_c[i][0] = 1;
1837                 ecdsa_c[i][1] = 1;
1838             }
1839         }
1840     }
1841     ecdsa_c[R_EC_B163][0] = count / 1000;
1842     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1843     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1844         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1845         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1846         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1847             ecdsa_doit[i] = 0;
1848         else {
1849             if (ecdsa_c[i] == 0) {
1850                 ecdsa_c[i][0] = 1;
1851                 ecdsa_c[i][1] = 1;
1852             }
1853         }
1854     }
1855
1856     ecdh_c[R_EC_P160][0] = count / 1000;
1857     ecdh_c[R_EC_P160][1] = count / 1000;
1858     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1859         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1860         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1861         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1862             ecdh_doit[i] = 0;
1863         else {
1864             if (ecdh_c[i] == 0) {
1865                 ecdh_c[i][0] = 1;
1866                 ecdh_c[i][1] = 1;
1867             }
1868         }
1869     }
1870     ecdh_c[R_EC_K163][0] = count / 1000;
1871     ecdh_c[R_EC_K163][1] = count / 1000;
1872     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1873         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1874         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1875         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1876             ecdh_doit[i] = 0;
1877         else {
1878             if (ecdh_c[i] == 0) {
1879                 ecdh_c[i][0] = 1;
1880                 ecdh_c[i][1] = 1;
1881             }
1882         }
1883     }
1884     ecdh_c[R_EC_B163][0] = count / 1000;
1885     ecdh_c[R_EC_B163][1] = count / 1000;
1886     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1887         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1888         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1889         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1890             ecdh_doit[i] = 0;
1891         else {
1892             if (ecdh_c[i] == 0) {
1893                 ecdh_c[i][0] = 1;
1894                 ecdh_c[i][1] = 1;
1895             }
1896         }
1897     }
1898 #  endif
1899
1900 # else
1901 /* not worth fixing */
1902 #  error "You cannot disable DES on systems without SIGALRM."
1903 # endif                        /* OPENSSL_NO_DES */
1904 #else
1905 # ifndef _WIN32
1906     signal(SIGALRM, sig_done);
1907 # endif
1908 #endif                         /* SIGALRM */
1909
1910 #ifndef OPENSSL_NO_MD2
1911     if (doit[D_MD2]) {
1912         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1913             print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
1914             Time_F(START);
1915             count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1916             d = Time_F(STOP);
1917             print_result(D_MD2, testnum, count, d);
1918         }
1919     }
1920 #endif
1921 #ifndef OPENSSL_NO_MDC2
1922     if (doit[D_MDC2]) {
1923         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1924             print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
1925             Time_F(START);
1926             count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1927             d = Time_F(STOP);
1928             print_result(D_MDC2, testnum, count, d);
1929         }
1930     }
1931 #endif
1932
1933 #ifndef OPENSSL_NO_MD4
1934     if (doit[D_MD4]) {
1935         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1936             print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
1937             Time_F(START);
1938             count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1939             d = Time_F(STOP);
1940             print_result(D_MD4, testnum, count, d);
1941         }
1942     }
1943 #endif
1944
1945 #ifndef OPENSSL_NO_MD5
1946     if (doit[D_MD5]) {
1947         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1948             print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);
1949             Time_F(START);
1950             count = run_benchmark(async_jobs, MD5_loop, loopargs);
1951             d = Time_F(STOP);
1952             print_result(D_MD5, testnum, count, d);
1953         }
1954     }
1955 #endif
1956
1957 #ifndef OPENSSL_NO_MD5
1958     if (doit[D_HMAC]) {
1959         for (i = 0; i < loopargs_len; i++) {
1960             loopargs[i].hctx = HMAC_CTX_new();
1961             if (loopargs[i].hctx == NULL) {
1962                 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
1963                 exit(1);
1964             }
1965
1966             HMAC_Init_ex(loopargs[i].hctx, (unsigned char *)"This is a key...",
1967                     16, EVP_md5(), NULL);
1968         }
1969         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1970             print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);
1971             Time_F(START);
1972             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
1973             d = Time_F(STOP);
1974             print_result(D_HMAC, testnum, count, d);
1975         }
1976         for (i = 0; i < loopargs_len; i++) {
1977             HMAC_CTX_free(loopargs[i].hctx);
1978         }
1979     }
1980 #endif
1981     if (doit[D_SHA1]) {
1982         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1983             print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);
1984             Time_F(START);
1985             count = run_benchmark(async_jobs, SHA1_loop, loopargs);
1986             d = Time_F(STOP);
1987             print_result(D_SHA1, testnum, count, d);
1988         }
1989     }
1990     if (doit[D_SHA256]) {
1991         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1992             print_message(names[D_SHA256], c[D_SHA256][testnum], lengths[testnum]);
1993             Time_F(START);
1994             count = run_benchmark(async_jobs, SHA256_loop, loopargs);
1995             d = Time_F(STOP);
1996             print_result(D_SHA256, testnum, count, d);
1997         }
1998     }
1999     if (doit[D_SHA512]) {
2000         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2001             print_message(names[D_SHA512], c[D_SHA512][testnum], lengths[testnum]);
2002             Time_F(START);
2003             count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2004             d = Time_F(STOP);
2005             print_result(D_SHA512, testnum, count, d);
2006         }
2007     }
2008
2009 #ifndef OPENSSL_NO_WHIRLPOOL
2010     if (doit[D_WHIRLPOOL]) {
2011         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2012             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum], lengths[testnum]);
2013             Time_F(START);
2014             count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2015             d = Time_F(STOP);
2016             print_result(D_WHIRLPOOL, testnum, count, d);
2017         }
2018     }
2019 #endif
2020
2021 #ifndef OPENSSL_NO_RMD160
2022     if (doit[D_RMD160]) {
2023         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2024             print_message(names[D_RMD160], c[D_RMD160][testnum], lengths[testnum]);
2025             Time_F(START);
2026             count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2027             d = Time_F(STOP);
2028             print_result(D_RMD160, testnum, count, d);
2029         }
2030     }
2031 #endif
2032 #ifndef OPENSSL_NO_RC4
2033     if (doit[D_RC4]) {
2034         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2035             print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
2036             Time_F(START);
2037             count = run_benchmark(async_jobs, RC4_loop, loopargs);
2038             d = Time_F(STOP);
2039             print_result(D_RC4, testnum, count, d);
2040         }
2041     }
2042 #endif
2043 #ifndef OPENSSL_NO_DES
2044     if (doit[D_CBC_DES]) {
2045         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2046             print_message(names[D_CBC_DES], c[D_CBC_DES][testnum], lengths[testnum]);
2047             Time_F(START);
2048             count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2049             d = Time_F(STOP);
2050             print_result(D_CBC_DES, testnum, count, d);
2051         }
2052     }
2053
2054     if (doit[D_EDE3_DES]) {
2055         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2056             print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum], lengths[testnum]);
2057             Time_F(START);
2058             count = run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2059             d = Time_F(STOP);
2060             print_result(D_EDE3_DES, testnum, count, d);
2061         }
2062     }
2063 #endif
2064 #ifndef OPENSSL_NO_AES
2065     if (doit[D_CBC_128_AES]) {
2066         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2067             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2068                           lengths[testnum]);
2069             Time_F(START);
2070             count = run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2071             d = Time_F(STOP);
2072             print_result(D_CBC_128_AES, testnum, count, d);
2073         }
2074     }
2075     if (doit[D_CBC_192_AES]) {
2076         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2077             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2078                           lengths[testnum]);
2079             Time_F(START);
2080             count = run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2081             d = Time_F(STOP);
2082             print_result(D_CBC_192_AES, testnum, count, d);
2083         }
2084     }
2085     if (doit[D_CBC_256_AES]) {
2086         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2087             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2088                           lengths[testnum]);
2089             Time_F(START);
2090             count = run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2091             d = Time_F(STOP);
2092             print_result(D_CBC_256_AES, testnum, count, d);
2093         }
2094     }
2095
2096     if (doit[D_IGE_128_AES]) {
2097         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2098             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2099                           lengths[testnum]);
2100             Time_F(START);
2101             count = run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2102             d = Time_F(STOP);
2103             print_result(D_IGE_128_AES, testnum, count, d);
2104         }
2105     }
2106     if (doit[D_IGE_192_AES]) {
2107         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2108             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2109                           lengths[testnum]);
2110             Time_F(START);
2111             count = run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2112             d = Time_F(STOP);
2113             print_result(D_IGE_192_AES, testnum, count, d);
2114         }
2115     }
2116     if (doit[D_IGE_256_AES]) {
2117         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2118             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2119                           lengths[testnum]);
2120             Time_F(START);
2121             count = run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2122             d = Time_F(STOP);
2123             print_result(D_IGE_256_AES, testnum, count, d);
2124         }
2125     }
2126     if (doit[D_GHASH]) {
2127         for (i = 0; i < loopargs_len; i++) {
2128             loopargs[i].gcm_ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2129             CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx, (unsigned char *)"0123456789ab", 12);
2130         }
2131
2132         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2133             print_message(names[D_GHASH], c[D_GHASH][testnum], lengths[testnum]);
2134             Time_F(START);
2135             count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2136             d = Time_F(STOP);
2137             print_result(D_GHASH, testnum, count, d);
2138         }
2139         for (i = 0; i < loopargs_len; i++)
2140             CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2141     }
2142 #endif
2143 #ifndef OPENSSL_NO_CAMELLIA
2144     if (doit[D_CBC_128_CML]) {
2145         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2146             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2147                           lengths[testnum]);
2148             if (async_jobs > 0) {
2149                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2150                 exit(1);
2151             }
2152             Time_F(START);
2153             for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2154                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2155                                      (unsigned long)lengths[testnum], &camellia_ks1,
2156                                      iv, CAMELLIA_ENCRYPT);
2157             d = Time_F(STOP);
2158             print_result(D_CBC_128_CML, testnum, count, d);
2159         }
2160     }
2161     if (doit[D_CBC_192_CML]) {
2162         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2163             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2164                           lengths[testnum]);
2165             if (async_jobs > 0) {
2166                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2167                 exit(1);
2168             }
2169             Time_F(START);
2170             for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2171                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2172                                      (unsigned long)lengths[testnum], &camellia_ks2,
2173                                      iv, CAMELLIA_ENCRYPT);
2174             d = Time_F(STOP);
2175             print_result(D_CBC_192_CML, testnum, count, d);
2176         }
2177     }
2178     if (doit[D_CBC_256_CML]) {
2179         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2180             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2181                           lengths[testnum]);
2182             if (async_jobs > 0) {
2183                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2184                 exit(1);
2185             }
2186             Time_F(START);
2187             for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2188                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2189                                      (unsigned long)lengths[testnum], &camellia_ks3,
2190                                      iv, CAMELLIA_ENCRYPT);
2191             d = Time_F(STOP);
2192             print_result(D_CBC_256_CML, testnum, count, d);
2193         }
2194     }
2195 #endif
2196 #ifndef OPENSSL_NO_IDEA
2197     if (doit[D_CBC_IDEA]) {
2198         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2199             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum], lengths[testnum]);
2200             if (async_jobs > 0) {
2201                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2202                 exit(1);
2203             }
2204             Time_F(START);
2205             for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
2206                 idea_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2207                                  (unsigned long)lengths[testnum], &idea_ks,
2208                                  iv, IDEA_ENCRYPT);
2209             d = Time_F(STOP);
2210             print_result(D_CBC_IDEA, testnum, count, d);
2211         }
2212     }
2213 #endif
2214 #ifndef OPENSSL_NO_SEED
2215     if (doit[D_CBC_SEED]) {
2216         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2217             print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum], lengths[testnum]);
2218             if (async_jobs > 0) {
2219                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2220                 exit(1);
2221             }
2222             Time_F(START);
2223             for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2224                 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2225                                  (unsigned long)lengths[testnum], &seed_ks, iv, 1);
2226             d = Time_F(STOP);
2227             print_result(D_CBC_SEED, testnum, count, d);
2228         }
2229     }
2230 #endif
2231 #ifndef OPENSSL_NO_RC2
2232     if (doit[D_CBC_RC2]) {
2233         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2234             print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum], lengths[testnum]);
2235             if (async_jobs > 0) {
2236                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2237                 exit(1);
2238             }
2239             Time_F(START);
2240             for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2241                 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2242                                 (unsigned long)lengths[testnum], &rc2_ks,
2243                                 iv, RC2_ENCRYPT);
2244             d = Time_F(STOP);
2245             print_result(D_CBC_RC2, testnum, count, d);
2246         }
2247     }
2248 #endif
2249 #ifndef OPENSSL_NO_RC5
2250     if (doit[D_CBC_RC5]) {
2251         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2252             print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum], lengths[testnum]);
2253             if (async_jobs > 0) {
2254                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2255                 exit(1);
2256             }
2257             Time_F(START);
2258             for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2259                 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2260                                    (unsigned long)lengths[testnum], &rc5_ks,
2261                                    iv, RC5_ENCRYPT);
2262             d = Time_F(STOP);
2263             print_result(D_CBC_RC5, testnum, count, d);
2264         }
2265     }
2266 #endif
2267 #ifndef OPENSSL_NO_BF
2268     if (doit[D_CBC_BF]) {
2269         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2270             print_message(names[D_CBC_BF], c[D_CBC_BF][testnum], lengths[testnum]);
2271             if (async_jobs > 0) {
2272                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2273                 exit(1);
2274             }
2275             Time_F(START);
2276             for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2277                 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2278                                (unsigned long)lengths[testnum], &bf_ks,
2279                                iv, BF_ENCRYPT);
2280             d = Time_F(STOP);
2281             print_result(D_CBC_BF, testnum, count, d);
2282         }
2283     }
2284 #endif
2285 #ifndef OPENSSL_NO_CAST
2286     if (doit[D_CBC_CAST]) {
2287         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2288             print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum], lengths[testnum]);
2289             if (async_jobs > 0) {
2290                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2291                 exit(1);
2292             }
2293             Time_F(START);
2294             for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2295                 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2296                                  (unsigned long)lengths[testnum], &cast_ks,
2297                                  iv, CAST_ENCRYPT);
2298             d = Time_F(STOP);
2299             print_result(D_CBC_CAST, testnum, count, d);
2300         }
2301     }
2302 #endif
2303
2304     if (doit[D_EVP]) {
2305 #ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
2306         if (multiblock && evp_cipher) {
2307             if (!
2308                 (EVP_CIPHER_flags(evp_cipher) &
2309                  EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2310                 BIO_printf(bio_err, "%s is not multi-block capable\n",
2311                            OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2312                 goto end;
2313             }
2314             if (async_jobs > 0) {
2315                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2316                 exit(1);
2317             }
2318             multiblock_speed(evp_cipher);
2319             ret = 0;
2320             goto end;
2321         }
2322 #endif
2323         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2324             if (evp_cipher) {
2325
2326                 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2327                 /*
2328                  * -O3 -fschedule-insns messes up an optimization here!
2329                  * names[D_EVP] somehow becomes NULL
2330                  */
2331                 print_message(names[D_EVP], save_count, lengths[testnum]);
2332
2333                 for (k = 0; k < loopargs_len; k++) {
2334                     loopargs[k].ctx = EVP_CIPHER_CTX_new();
2335                     if (decrypt)
2336                         EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
2337                     else
2338                         EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
2339                     EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2340                 }
2341
2342                 Time_F(START);
2343                 count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
2344                 d = Time_F(STOP);
2345                 for (k = 0; k < loopargs_len; k++) {
2346                     EVP_CIPHER_CTX_free(loopargs[k].ctx);
2347                 }
2348             }
2349             if (evp_md) {
2350                 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2351                 print_message(names[D_EVP], save_count, lengths[testnum]);
2352                 Time_F(START);
2353                 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2354                 d = Time_F(STOP);
2355             }
2356             print_result(D_EVP, testnum, count, d);
2357         }
2358     }
2359
2360     for (i = 0; i < loopargs_len; i++)
2361         RAND_bytes(loopargs[i].buf, 36);
2362
2363 #ifndef OPENSSL_NO_RSA
2364     for (testnum = 0; testnum < RSA_NUM; testnum++) {
2365         int st = 0;
2366         if (!rsa_doit[testnum])
2367             continue;
2368         for (i = 0; i < loopargs_len; i++) {
2369             st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2370                           loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2371             if (st == 0)
2372                 break;
2373         }
2374         if (st == 0) {
2375             BIO_printf(bio_err,
2376                        "RSA sign failure.  No RSA sign will be done.\n");
2377             ERR_print_errors(bio_err);
2378             rsa_count = 1;
2379         } else {
2380             pkey_print_message("private", "rsa",
2381                                rsa_c[testnum][0], rsa_bits[testnum], RSA_SECONDS);
2382             /* RSA_blinding_on(rsa_key[testnum],NULL); */
2383             Time_F(START);
2384             count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2385             d = Time_F(STOP);
2386             BIO_printf(bio_err,
2387                        mr ? "+R1:%ld:%d:%.2f\n"
2388                        : "%ld %d bit private RSA's in %.2fs\n",
2389                        count, rsa_bits[testnum], d);
2390             rsa_results[testnum][0] = d / (double)count;
2391             rsa_count = count;
2392         }
2393
2394         for (i = 0; i < loopargs_len; i++) {
2395             st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2396                             *(loopargs[i].siglen), loopargs[i].rsa_key[testnum]);
2397             if (st <= 0)
2398                 break;
2399         }
2400         if (st <= 0) {
2401             BIO_printf(bio_err,
2402                        "RSA verify failure.  No RSA verify will be done.\n");
2403             ERR_print_errors(bio_err);
2404             rsa_doit[testnum] = 0;
2405         } else {
2406             pkey_print_message("public", "rsa",
2407                                rsa_c[testnum][1], rsa_bits[testnum], RSA_SECONDS);
2408             Time_F(START);
2409             count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2410             d = Time_F(STOP);
2411             BIO_printf(bio_err,
2412                        mr ? "+R2:%ld:%d:%.2f\n"
2413                        : "%ld %d bit public RSA's in %.2fs\n",
2414                        count, rsa_bits[testnum], d);
2415             rsa_results[testnum][1] = d / (double)count;
2416         }
2417
2418         if (rsa_count <= 1) {
2419             /* if longer than 10s, don't do any more */
2420             for (testnum++; testnum < RSA_NUM; testnum++)
2421                 rsa_doit[testnum] = 0;
2422         }
2423     }
2424 #endif
2425
2426     for (i = 0; i < loopargs_len; i++)
2427         RAND_bytes(loopargs[i].buf, 36);
2428
2429 #ifndef OPENSSL_NO_DSA
2430     if (RAND_status() != 1) {
2431         RAND_seed(rnd_seed, sizeof rnd_seed);
2432         rnd_fake = 1;
2433     }
2434     for (testnum = 0; testnum < DSA_NUM; testnum++) {
2435         int st = 0;
2436         if (!dsa_doit[testnum])
2437             continue;
2438
2439         /* DSA_generate_key(dsa_key[testnum]); */
2440         /* DSA_sign_setup(dsa_key[testnum],NULL); */
2441         for (i = 0; i < loopargs_len; i++) {
2442             st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2443                           loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2444             if (st == 0)
2445                 break;
2446         }
2447         if (st == 0) {
2448             BIO_printf(bio_err,
2449                        "DSA sign failure.  No DSA sign will be done.\n");
2450             ERR_print_errors(bio_err);
2451             rsa_count = 1;
2452         } else {
2453             pkey_print_message("sign", "dsa",
2454                                dsa_c[testnum][0], dsa_bits[testnum], DSA_SECONDS);
2455             Time_F(START);
2456             count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2457             d = Time_F(STOP);
2458             BIO_printf(bio_err,
2459                        mr ? "+R3:%ld:%d:%.2f\n"
2460                        : "%ld %d bit DSA signs in %.2fs\n",
2461                        count, dsa_bits[testnum], d);
2462             dsa_results[testnum][0] = d / (double)count;
2463             rsa_count = count;
2464         }
2465
2466         for (i = 0; i < loopargs_len; i++) {
2467             st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2468                             *(loopargs[i].siglen), loopargs[i].dsa_key[testnum]);
2469             if (st <= 0)
2470                 break;
2471         }
2472         if (st <= 0) {
2473             BIO_printf(bio_err,
2474                        "DSA verify failure.  No DSA verify will be done.\n");
2475             ERR_print_errors(bio_err);
2476             dsa_doit[testnum] = 0;
2477         } else {
2478             pkey_print_message("verify", "dsa",
2479                                dsa_c[testnum][1], dsa_bits[testnum], DSA_SECONDS);
2480             Time_F(START);
2481             count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2482             d = Time_F(STOP);
2483             BIO_printf(bio_err,
2484                        mr ? "+R4:%ld:%d:%.2f\n"
2485                        : "%ld %d bit DSA verify in %.2fs\n",
2486                        count, dsa_bits[testnum], d);
2487             dsa_results[testnum][1] = d / (double)count;
2488         }
2489
2490         if (rsa_count <= 1) {
2491             /* if longer than 10s, don't do any more */
2492             for (testnum++; testnum < DSA_NUM; testnum++)
2493                 dsa_doit[testnum] = 0;
2494         }
2495     }
2496     if (rnd_fake)
2497         RAND_cleanup();
2498 #endif
2499
2500 #ifndef OPENSSL_NO_EC
2501     if (RAND_status() != 1) {
2502         RAND_seed(rnd_seed, sizeof rnd_seed);
2503         rnd_fake = 1;
2504     }
2505     for (testnum = 0; testnum < EC_NUM; testnum++) {
2506         int st = 1;
2507
2508         if (!ecdsa_doit[testnum])
2509             continue;           /* Ignore Curve */
2510         for (i = 0; i < loopargs_len; i++) {
2511             loopargs[i].ecdsa[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2512             if (loopargs[i].ecdsa[testnum] == NULL) {
2513                 st = 0;
2514                 break;
2515             }
2516         }
2517         if (st == 0) {
2518             BIO_printf(bio_err, "ECDSA failure.\n");
2519             ERR_print_errors(bio_err);
2520             rsa_count = 1;
2521         } else {
2522             for (i = 0; i < loopargs_len; i++) {
2523                 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
2524                 /* Perform ECDSA signature test */
2525                 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
2526                 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2527                                 loopargs[i].siglen, loopargs[i].ecdsa[testnum]);
2528                 if (st == 0)
2529                     break;
2530             }
2531             if (st == 0) {
2532                 BIO_printf(bio_err,
2533                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
2534                 ERR_print_errors(bio_err);
2535                 rsa_count = 1;
2536             } else {
2537                 pkey_print_message("sign", "ecdsa",
2538                                    ecdsa_c[testnum][0],
2539                                    test_curves_bits[testnum], ECDSA_SECONDS);
2540                 Time_F(START);
2541                 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2542                 d = Time_F(STOP);
2543
2544                 BIO_printf(bio_err,
2545                            mr ? "+R5:%ld:%d:%.2f\n" :
2546                            "%ld %d bit ECDSA signs in %.2fs \n",
2547                            count, test_curves_bits[testnum], d);
2548                 ecdsa_results[testnum][0] = d / (double)count;
2549                 rsa_count = count;
2550             }
2551
2552             /* Perform ECDSA verification test */
2553             for (i = 0; i < loopargs_len; i++) {
2554                 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2555                                   *(loopargs[i].siglen), loopargs[i].ecdsa[testnum]);
2556                 if (st != 1)
2557                     break;
2558             }
2559             if (st != 1) {
2560                 BIO_printf(bio_err,
2561                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
2562                 ERR_print_errors(bio_err);
2563                 ecdsa_doit[testnum] = 0;
2564             } else {
2565                 pkey_print_message("verify", "ecdsa",
2566                                    ecdsa_c[testnum][1],
2567                                    test_curves_bits[testnum], ECDSA_SECONDS);
2568                 Time_F(START);
2569                 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2570                 d = Time_F(STOP);
2571                 BIO_printf(bio_err,
2572                            mr ? "+R6:%ld:%d:%.2f\n"
2573                            : "%ld %d bit ECDSA verify in %.2fs\n",
2574                            count, test_curves_bits[testnum], d);
2575                 ecdsa_results[testnum][1] = d / (double)count;
2576             }
2577
2578             if (rsa_count <= 1) {
2579                 /* if longer than 10s, don't do any more */
2580                 for (testnum++; testnum < EC_NUM; testnum++)
2581                     ecdsa_doit[testnum] = 0;
2582             }
2583         }
2584     }
2585     if (rnd_fake)
2586         RAND_cleanup();
2587 #endif
2588
2589 #ifndef OPENSSL_NO_EC
2590     if (RAND_status() != 1) {
2591         RAND_seed(rnd_seed, sizeof rnd_seed);
2592         rnd_fake = 1;
2593     }
2594     for (testnum = 0; testnum < EC_NUM; testnum++) {
2595         if (!ecdh_doit[testnum])
2596             continue;
2597         for (i = 0; i < loopargs_len; i++) {
2598             loopargs[i].ecdh_a[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2599             loopargs[i].ecdh_b[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
2600             if (loopargs[i].ecdh_a[testnum] == NULL ||
2601                 loopargs[i].ecdh_b[testnum] == NULL) {
2602                 ecdh_checks = 0;
2603                 break;
2604             }
2605         }
2606         if (ecdh_checks == 0) {
2607             BIO_printf(bio_err, "ECDH failure.\n");
2608             ERR_print_errors(bio_err);
2609             rsa_count = 1;
2610         } else {
2611             for (i = 0; i < loopargs_len; i++) {
2612                 /* generate two ECDH key pairs */
2613                 if (!EC_KEY_generate_key(loopargs[i].ecdh_a[testnum]) ||
2614                         !EC_KEY_generate_key(loopargs[i].ecdh_b[testnum])) {
2615                     BIO_printf(bio_err, "ECDH key generation failure.\n");
2616                     ERR_print_errors(bio_err);
2617                     ecdh_checks = 0;
2618                     rsa_count = 1;
2619                 } else {
2620                     /*
2621                      * If field size is not more than 24 octets, then use SHA-1
2622                      * hash of result; otherwise, use result (see section 4.8 of
2623                      * draft-ietf-tls-ecc-03.txt).
2624                      */
2625                     int field_size;
2626                     field_size =
2627                         EC_GROUP_get_degree(EC_KEY_get0_group(loopargs[i].ecdh_a[testnum]));
2628                     if (field_size <= 24 * 8) {
2629                         outlen = KDF1_SHA1_len;
2630                         kdf = KDF1_SHA1;
2631                     } else {
2632                         outlen = (field_size + 7) / 8;
2633                         kdf = NULL;
2634                     }
2635                     secret_size_a =
2636                         ECDH_compute_key(loopargs[i].secret_a, outlen,
2637                                 EC_KEY_get0_public_key(loopargs[i].ecdh_b[testnum]),
2638                                 loopargs[i].ecdh_a[testnum], kdf);
2639                     secret_size_b =
2640                         ECDH_compute_key(loopargs[i].secret_b, outlen,
2641                                 EC_KEY_get0_public_key(loopargs[i].ecdh_a[testnum]),
2642                                 loopargs[i].ecdh_b[testnum], kdf);
2643                     if (secret_size_a != secret_size_b)
2644                         ecdh_checks = 0;
2645                     else
2646                         ecdh_checks = 1;
2647
2648                     for (secret_idx = 0; (secret_idx < secret_size_a)
2649                             && (ecdh_checks == 1); secret_idx++) {
2650                         if (loopargs[i].secret_a[secret_idx] != loopargs[i].secret_b[secret_idx])
2651                             ecdh_checks = 0;
2652                     }
2653
2654                     if (ecdh_checks == 0) {
2655                         BIO_printf(bio_err, "ECDH computations don't match.\n");
2656                         ERR_print_errors(bio_err);
2657                         rsa_count = 1;
2658                         break;
2659                     }
2660                 }
2661                 if (ecdh_checks != 0) {
2662                     pkey_print_message("", "ecdh",
2663                             ecdh_c[testnum][0],
2664                             test_curves_bits[testnum], ECDH_SECONDS);
2665                     Time_F(START);
2666                     count = run_benchmark(async_jobs, ECDH_compute_key_loop, loopargs);
2667                     d = Time_F(STOP);
2668                     BIO_printf(bio_err,
2669                             mr ? "+R7:%ld:%d:%.2f\n" :
2670                             "%ld %d-bit ECDH ops in %.2fs\n", count,
2671                             test_curves_bits[testnum], d);
2672                     ecdh_results[testnum][0] = d / (double)count;
2673                     rsa_count = count;
2674                 }
2675             }
2676         }
2677
2678         if (rsa_count <= 1) {
2679             /* if longer than 10s, don't do any more */
2680             for (testnum++; testnum < EC_NUM; testnum++)
2681                 ecdh_doit[testnum] = 0;
2682         }
2683     }
2684     if (rnd_fake)
2685         RAND_cleanup();
2686 #endif
2687 #ifndef NO_FORK
2688  show_res:
2689 #endif
2690     if (!mr) {
2691         printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
2692         printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2693         printf("options:");
2694         printf("%s ", BN_options());
2695 #ifndef OPENSSL_NO_MD2
2696         printf("%s ", MD2_options());
2697 #endif
2698 #ifndef OPENSSL_NO_RC4
2699         printf("%s ", RC4_options());
2700 #endif
2701 #ifndef OPENSSL_NO_DES
2702         printf("%s ", DES_options());
2703 #endif
2704 #ifndef OPENSSL_NO_AES
2705         printf("%s ", AES_options());
2706 #endif
2707 #ifndef OPENSSL_NO_IDEA
2708         printf("%s ", idea_options());
2709 #endif
2710 #ifndef OPENSSL_NO_BF
2711         printf("%s ", BF_options());
2712 #endif
2713         printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2714     }
2715
2716     if (pr_header) {
2717         if (mr)
2718             printf("+H");
2719         else {
2720             printf
2721                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2722             printf("type        ");
2723         }
2724         for (testnum = 0; testnum < SIZE_NUM; testnum++)
2725             printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2726         printf("\n");
2727     }
2728
2729     for (k = 0; k < ALGOR_NUM; k++) {
2730         if (!doit[k])
2731             continue;
2732         if (mr)
2733             printf("+F:%d:%s", k, names[k]);
2734         else
2735             printf("%-13s", names[k]);
2736         for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2737             if (results[k][testnum] > 10000 && !mr)
2738                 printf(" %11.2fk", results[k][testnum] / 1e3);
2739             else
2740                 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2741         }
2742         printf("\n");
2743     }
2744 #ifndef OPENSSL_NO_RSA
2745     testnum = 1;
2746     for (k = 0; k < RSA_NUM; k++) {
2747         if (!rsa_doit[k])
2748             continue;
2749         if (testnum && !mr) {
2750             printf("%18ssign    verify    sign/s verify/s\n", " ");
2751             testnum = 0;
2752         }
2753         if (mr)
2754             printf("+F2:%u:%u:%f:%f\n",
2755                    k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2756         else
2757             printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2758                    rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2759                    1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
2760     }
2761 #endif
2762 #ifndef OPENSSL_NO_DSA
2763     testnum = 1;
2764     for (k = 0; k < DSA_NUM; k++) {
2765         if (!dsa_doit[k])
2766             continue;
2767         if (testnum && !mr) {
2768             printf("%18ssign    verify    sign/s verify/s\n", " ");
2769             testnum = 0;
2770         }
2771         if (mr)
2772             printf("+F3:%u:%u:%f:%f\n",
2773                    k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2774         else
2775             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2776                    dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2777                    1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
2778     }
2779 #endif
2780 #ifndef OPENSSL_NO_EC
2781     testnum = 1;
2782     for (k = 0; k < EC_NUM; k++) {
2783         if (!ecdsa_doit[k])
2784             continue;
2785         if (testnum && !mr) {
2786             printf("%30ssign    verify    sign/s verify/s\n", " ");
2787             testnum = 0;
2788         }
2789
2790         if (mr)
2791             printf("+F4:%u:%u:%f:%f\n",
2792                    k, test_curves_bits[k],
2793                    ecdsa_results[k][0], ecdsa_results[k][1]);
2794         else
2795             printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2796                    test_curves_bits[k],
2797                    test_curves_names[k],
2798                    ecdsa_results[k][0], ecdsa_results[k][1],
2799                    1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
2800     }
2801 #endif
2802
2803 #ifndef OPENSSL_NO_EC
2804     testnum = 1;
2805     for (k = 0; k < EC_NUM; k++) {
2806         if (!ecdh_doit[k])
2807             continue;
2808         if (testnum && !mr) {
2809             printf("%30sop      op/s\n", " ");
2810             testnum = 0;
2811         }
2812         if (mr)
2813             printf("+F5:%u:%u:%f:%f\n",
2814                    k, test_curves_bits[k],
2815                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2816
2817         else
2818             printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2819                    test_curves_bits[k],
2820                    test_curves_names[k],
2821                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2822     }
2823 #endif
2824
2825     ret = 0;
2826
2827  end:
2828     ERR_print_errors(bio_err);
2829     for (i = 0; i < loopargs_len; i++) {
2830         OPENSSL_free(loopargs[i].buf_malloc);
2831         OPENSSL_free(loopargs[i].buf2_malloc);
2832         OPENSSL_free(loopargs[i].siglen);
2833     }
2834 #ifndef OPENSSL_NO_RSA
2835     for (i = 0; i < loopargs_len; i++) {
2836         for (k = 0; k < RSA_NUM; k++)
2837             RSA_free(loopargs[i].rsa_key[k]);
2838     }
2839 #endif
2840 #ifndef OPENSSL_NO_DSA
2841     for (i = 0; i < loopargs_len; i++) {
2842         for (k = 0; k < DSA_NUM; k++)
2843             DSA_free(loopargs[i].dsa_key[k]);
2844     }
2845 #endif
2846
2847 #ifndef OPENSSL_NO_EC
2848     for (i = 0; i < loopargs_len; i++) {
2849         for (k = 0; k < EC_NUM; k++) {
2850             EC_KEY_free(loopargs[i].ecdsa[k]);
2851             EC_KEY_free(loopargs[i].ecdh_a[k]);
2852             EC_KEY_free(loopargs[i].ecdh_b[k]);
2853         }
2854         OPENSSL_free(loopargs[i].secret_a);
2855         OPENSSL_free(loopargs[i].secret_b);
2856     }
2857 #endif
2858     if (async_jobs > 0) {
2859         for (i = 0; i < loopargs_len; i++)
2860             ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
2861
2862         ASYNC_cleanup_thread();
2863     }
2864     OPENSSL_free(loopargs);
2865     return (ret);
2866 }
2867
2868 static void print_message(const char *s, long num, int length)
2869 {
2870 #ifdef SIGALRM
2871     BIO_printf(bio_err,
2872                mr ? "+DT:%s:%d:%d\n"
2873                : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2874     (void)BIO_flush(bio_err);
2875     alarm(SECONDS);
2876 #else
2877     BIO_printf(bio_err,
2878                mr ? "+DN:%s:%ld:%d\n"
2879                : "Doing %s %ld times on %d size blocks: ", s, num, length);
2880     (void)BIO_flush(bio_err);
2881 #endif
2882 }
2883
2884 static void pkey_print_message(const char *str, const char *str2, long num,
2885                                int bits, int tm)
2886 {
2887 #ifdef SIGALRM
2888     BIO_printf(bio_err,
2889                mr ? "+DTP:%d:%s:%s:%d\n"
2890                : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2891     (void)BIO_flush(bio_err);
2892     alarm(tm);
2893 #else
2894     BIO_printf(bio_err,
2895                mr ? "+DNP:%ld:%d:%s:%s\n"
2896                : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2897     (void)BIO_flush(bio_err);
2898 #endif
2899 }
2900
2901 static void print_result(int alg, int run_no, int count, double time_used)
2902 {
2903     BIO_printf(bio_err,
2904                mr ? "+R:%d:%s:%f\n"
2905                : "%d %s's in %.2fs\n", count, names[alg], time_used);
2906     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2907 }
2908
2909 #ifndef NO_FORK
2910 static char *sstrsep(char **string, const char *delim)
2911 {
2912     char isdelim[256];
2913     char *token = *string;
2914
2915     if (**string == 0)
2916         return NULL;
2917
2918     memset(isdelim, 0, sizeof isdelim);
2919     isdelim[0] = 1;
2920
2921     while (*delim) {
2922         isdelim[(unsigned char)(*delim)] = 1;
2923         delim++;
2924     }
2925
2926     while (!isdelim[(unsigned char)(**string)]) {
2927         (*string)++;
2928     }
2929
2930     if (**string) {
2931         **string = 0;
2932         (*string)++;
2933     }
2934
2935     return token;
2936 }
2937
2938 static int do_multi(int multi)
2939 {
2940     int n;
2941     int fd[2];
2942     int *fds;
2943     static char sep[] = ":";
2944
2945     fds = malloc(sizeof(*fds) * multi);
2946     for (n = 0; n < multi; ++n) {
2947         if (pipe(fd) == -1) {
2948             BIO_printf(bio_err, "pipe failure\n");
2949             exit(1);
2950         }
2951         fflush(stdout);
2952         (void)BIO_flush(bio_err);
2953         if (fork()) {
2954             close(fd[1]);
2955             fds[n] = fd[0];
2956         } else {
2957             close(fd[0]);
2958             close(1);
2959             if (dup(fd[1]) == -1) {
2960                 BIO_printf(bio_err, "dup failed\n");
2961                 exit(1);
2962             }
2963             close(fd[1]);
2964             mr = 1;
2965             usertime = 0;
2966             free(fds);
2967             return 0;
2968         }
2969         printf("Forked child %d\n", n);
2970     }
2971
2972     /* for now, assume the pipe is long enough to take all the output */
2973     for (n = 0; n < multi; ++n) {
2974         FILE *f;
2975         char buf[1024];
2976         char *p;
2977
2978         f = fdopen(fds[n], "r");
2979         while (fgets(buf, sizeof buf, f)) {
2980             p = strchr(buf, '\n');
2981             if (p)
2982                 *p = '\0';
2983             if (buf[0] != '+') {
2984                 BIO_printf(bio_err, "Don't understand line '%s' from child %d\n",
2985                         buf, n);
2986                 continue;
2987             }
2988             printf("Got: %s from %d\n", buf, n);
2989             if (strncmp(buf, "+F:", 3) == 0) {
2990                 int alg;
2991                 int j;
2992
2993                 p = buf + 3;
2994                 alg = atoi(sstrsep(&p, sep));
2995                 sstrsep(&p, sep);
2996                 for (j = 0; j < SIZE_NUM; ++j)
2997                     results[alg][j] += atof(sstrsep(&p, sep));
2998             } else if (strncmp(buf, "+F2:", 4) == 0) {
2999                 int k;
3000                 double d;
3001
3002                 p = buf + 4;
3003                 k = atoi(sstrsep(&p, sep));
3004                 sstrsep(&p, sep);
3005
3006                 d = atof(sstrsep(&p, sep));
3007                 if (n)
3008                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
3009                 else
3010                     rsa_results[k][0] = d;
3011
3012                 d = atof(sstrsep(&p, sep));
3013                 if (n)
3014                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
3015                 else
3016                     rsa_results[k][1] = d;
3017             }
3018 # ifndef OPENSSL_NO_DSA
3019             else if (strncmp(buf, "+F3:", 4) == 0) {
3020                 int k;
3021                 double d;
3022
3023                 p = buf + 4;
3024                 k = atoi(sstrsep(&p, sep));
3025                 sstrsep(&p, sep);
3026
3027                 d = atof(sstrsep(&p, sep));
3028                 if (n)
3029                     dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
3030                 else
3031                     dsa_results[k][0] = d;
3032
3033                 d = atof(sstrsep(&p, sep));
3034                 if (n)
3035                     dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
3036                 else
3037                     dsa_results[k][1] = d;
3038             }
3039 # endif
3040 # ifndef OPENSSL_NO_EC
3041             else if (strncmp(buf, "+F4:", 4) == 0) {
3042                 int k;
3043                 double d;
3044
3045                 p = buf + 4;
3046                 k = atoi(sstrsep(&p, sep));
3047                 sstrsep(&p, sep);
3048
3049                 d = atof(sstrsep(&p, sep));
3050                 if (n)
3051                     ecdsa_results[k][0] =
3052                         1 / (1 / ecdsa_results[k][0] + 1 / d);
3053                 else
3054                     ecdsa_results[k][0] = d;
3055
3056                 d = atof(sstrsep(&p, sep));
3057                 if (n)
3058                     ecdsa_results[k][1] =
3059                         1 / (1 / ecdsa_results[k][1] + 1 / d);
3060                 else
3061                     ecdsa_results[k][1] = d;
3062             }
3063 # endif
3064
3065 # ifndef OPENSSL_NO_EC
3066             else if (strncmp(buf, "+F5:", 4) == 0) {
3067                 int k;
3068                 double d;
3069
3070                 p = buf + 4;
3071                 k = atoi(sstrsep(&p, sep));
3072                 sstrsep(&p, sep);
3073
3074                 d = atof(sstrsep(&p, sep));
3075                 if (n)
3076                     ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
3077                 else
3078                     ecdh_results[k][0] = d;
3079
3080             }
3081 # endif
3082
3083             else if (strncmp(buf, "+H:", 3) == 0) {
3084                 ;
3085             } else
3086                 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf, n);
3087         }
3088
3089         fclose(f);
3090     }
3091     free(fds);
3092     return 1;
3093 }
3094 #endif
3095
3096 static void multiblock_speed(const EVP_CIPHER *evp_cipher)
3097 {
3098     static int mblengths[] =
3099         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3100     int j, count, num = OSSL_NELEM(mblengths);
3101     const char *alg_name;
3102     unsigned char *inp, *out, no_key[32], no_iv[16];
3103     EVP_CIPHER_CTX *ctx;
3104     double d = 0.0;
3105
3106     inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
3107     out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3108     ctx = EVP_CIPHER_CTX_new();
3109     EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3110     EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
3111                         no_key);
3112     alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3113
3114     for (j = 0; j < num; j++) {
3115         print_message(alg_name, 0, mblengths[j]);
3116         Time_F(START);
3117         for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3118             unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3119             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
3120             size_t len = mblengths[j];
3121             int packlen;
3122
3123             memset(aad, 0, 8);  /* avoid uninitialized values */
3124             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
3125             aad[9] = 3;         /* version */
3126             aad[10] = 2;
3127             aad[11] = 0;        /* length */
3128             aad[12] = 0;
3129             mb_param.out = NULL;
3130             mb_param.inp = aad;
3131             mb_param.len = len;
3132             mb_param.interleave = 8;
3133
3134             packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3135                                           sizeof(mb_param), &mb_param);
3136
3137             if (packlen > 0) {
3138                 mb_param.out = out;
3139                 mb_param.inp = inp;
3140                 mb_param.len = len;
3141                 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3142                                     sizeof(mb_param), &mb_param);
3143             } else {
3144                 int pad;
3145
3146                 RAND_bytes(out, 16);
3147                 len += 16;
3148                 aad[11] = len >> 8;
3149                 aad[12] = len;
3150                 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3151                                           EVP_AEAD_TLS1_AAD_LEN, aad);
3152                 EVP_Cipher(ctx, out, inp, len + pad);
3153             }
3154         }
3155         d = Time_F(STOP);
3156         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3157                    : "%d %s's in %.2fs\n", count, "evp", d);
3158         results[D_EVP][j] = ((double)count) / d * mblengths[j];
3159     }
3160
3161     if (mr) {
3162         fprintf(stdout, "+H");
3163         for (j = 0; j < num; j++)
3164             fprintf(stdout, ":%d", mblengths[j]);
3165         fprintf(stdout, "\n");
3166         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
3167         for (j = 0; j < num; j++)
3168             fprintf(stdout, ":%.2f", results[D_EVP][j]);
3169         fprintf(stdout, "\n");
3170     } else {
3171         fprintf(stdout,
3172                 "The 'numbers' are in 1000s of bytes per second processed.\n");
3173         fprintf(stdout, "type                    ");
3174         for (j = 0; j < num; j++)
3175             fprintf(stdout, "%7d bytes", mblengths[j]);
3176         fprintf(stdout, "\n");
3177         fprintf(stdout, "%-24s", alg_name);
3178
3179         for (j = 0; j < num; j++) {
3180             if (results[D_EVP][j] > 10000)
3181                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
3182             else
3183                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
3184         }
3185         fprintf(stdout, "\n");
3186     }
3187
3188     OPENSSL_free(inp);
3189     OPENSSL_free(out);
3190     EVP_CIPHER_CTX_free(ctx);
3191 }