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