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