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