1a01d333b7f8752f5f30f266e9b109a262b6afbc
[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 #ifndef OPENSSL_NO_ENGINE
839         case OPT_ENGINE:
840             setup_engine(opt_arg(), 0);
841             break;
842 #endif
843 #ifndef NO_FORK
844         case OPT_MULTI:
845             multi = atoi(opt_arg());
846             break;
847 #endif
848         case OPT_MISALIGN:
849             if (!opt_int(opt_arg(), &misalign))
850                 goto end;
851             if (misalign > MISALIGN) {
852                 BIO_printf(bio_err,
853                            "%s: Maximum offset is %d\n", prog, MISALIGN);
854                 goto opterr;
855             }
856             buf = buf_malloc + misalign;
857             buf2 = buf2_malloc + misalign;
858             break;
859         case OPT_MR:
860             mr = 1;
861             break;
862         case OPT_MB:
863             multiblock = 1;
864             break;
865         }
866     }
867     argc = opt_num_rest();
868     argv = opt_rest();
869
870     /* Remaining arguments are algorithms. */
871     for ( ; *argv; argv++) {
872         if (found(*argv, doit_choices, &i)) {
873             doit[i] = 1;
874             continue;
875         }
876 #ifndef OPENSSL_NO_DES
877         if (strcmp(*argv, "des") == 0) {
878             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
879             continue;
880         }
881 #endif
882         if (strcmp(*argv, "sha") == 0) {
883             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
884             continue;
885         }
886 #ifndef OPENSSL_NO_RSA
887 # ifndef RSA_NULL
888         if (strcmp(*argv, "openssl") == 0) {
889             RSA_set_default_method(RSA_PKCS1_SSLeay());
890             continue;
891         }
892 # endif
893         if (strcmp(*argv, "rsa") == 0) {
894             rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
895                 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
896                 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
897                 rsa_doit[R_RSA_15360] = 1;
898             continue;
899         }
900         if (found(*argv, rsa_choices, &i)) {
901             rsa_doit[i] = 1;
902             continue;
903         }
904 #endif
905 #ifndef OPENSSL_NO_DSA
906         if (strcmp(*argv, "dsa") == 0) {
907             dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
908                 dsa_doit[R_DSA_2048] = 1;
909             continue;
910         }
911         if (found(*argv, dsa_choices, &i)) {
912             dsa_doit[i] = 2;
913             continue;
914         }
915 #endif
916 #ifndef OPENSSL_NO_AES
917         if (strcmp(*argv, "aes") == 0) {
918             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =
919                 doit[D_CBC_256_AES] = 1;
920             continue;
921         }
922 #endif
923 #ifndef OPENSSL_NO_CAMELLIA
924         if (strcmp(*argv, "camellia") == 0) {
925             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =
926                 doit[D_CBC_256_CML] = 1;
927             continue;
928         }
929 #endif
930 #ifndef OPENSSL_NO_EC
931         if (strcmp(*argv, "ecdsa") == 0) {
932             for (i = 0; i < EC_NUM; i++)
933                 ecdsa_doit[i] = 1;
934             continue;
935         }
936         if (found(*argv, ecdsa_choices, &i)) {
937             ecdsa_doit[i] = 2;
938             continue;
939         }
940         if (strcmp(*argv, "ecdh") == 0) {
941             for (i = 0; i < EC_NUM; i++)
942                 ecdh_doit[i] = 1;
943             continue;
944         }
945         if (found(*argv, ecdh_choices, &i)) {
946             ecdh_doit[i] = 2;
947             continue;
948         }
949 #endif
950         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
951         goto end;
952     }
953
954 #ifndef NO_FORK
955     if (multi && do_multi(multi))
956         goto show_res;
957 #endif
958
959     /* No parameters; turn on everything. */
960     if (argc == 0) {
961         for (i = 0; i < ALGOR_NUM; i++)
962             if (i != D_EVP)
963                 doit[i] = 1;
964         for (i = 0; i < RSA_NUM; i++)
965             rsa_doit[i] = 1;
966         for (i = 0; i < DSA_NUM; i++)
967             dsa_doit[i] = 1;
968 #ifndef OPENSSL_NO_EC
969         for (i = 0; i < EC_NUM; i++)
970             ecdsa_doit[i] = 1;
971         for (i = 0; i < EC_NUM; i++)
972             ecdh_doit[i] = 1;
973 #endif
974     }
975     for (i = 0; i < ALGOR_NUM; i++)
976         if (doit[i])
977             pr_header++;
978
979     if (usertime == 0 && !mr)
980         BIO_printf(bio_err,
981                    "You have chosen to measure elapsed time "
982                    "instead of user CPU time.\n");
983
984 #ifndef OPENSSL_NO_RSA
985     for (i = 0; i < RSA_NUM; i++) {
986         const unsigned char *p;
987
988         p = rsa_data[i];
989         rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]);
990         if (rsa_key[i] == NULL) {
991             BIO_printf(bio_err, "internal error loading RSA key number %d\n",
992                        i);
993             goto end;
994         }
995     }
996 #endif
997
998 #ifndef OPENSSL_NO_DSA
999     dsa_key[0] = get_dsa512();
1000     dsa_key[1] = get_dsa1024();
1001     dsa_key[2] = get_dsa2048();
1002 #endif
1003
1004 #ifndef OPENSSL_NO_DES
1005     DES_set_key_unchecked(&key, &sch);
1006     DES_set_key_unchecked(&key2, &sch2);
1007     DES_set_key_unchecked(&key3, &sch3);
1008 #endif
1009 #ifndef OPENSSL_NO_AES
1010     AES_set_encrypt_key(key16, 128, &aes_ks1);
1011     AES_set_encrypt_key(key24, 192, &aes_ks2);
1012     AES_set_encrypt_key(key32, 256, &aes_ks3);
1013 #endif
1014 #ifndef OPENSSL_NO_CAMELLIA
1015     Camellia_set_key(key16, 128, &camellia_ks1);
1016     Camellia_set_key(ckey24, 192, &camellia_ks2);
1017     Camellia_set_key(ckey32, 256, &camellia_ks3);
1018 #endif
1019 #ifndef OPENSSL_NO_IDEA
1020     idea_set_encrypt_key(key16, &idea_ks);
1021 #endif
1022 #ifndef OPENSSL_NO_SEED
1023     SEED_set_key(key16, &seed_ks);
1024 #endif
1025 #ifndef OPENSSL_NO_RC4
1026     RC4_set_key(&rc4_ks, 16, key16);
1027 #endif
1028 #ifndef OPENSSL_NO_RC2
1029     RC2_set_key(&rc2_ks, 16, key16, 128);
1030 #endif
1031 #ifndef OPENSSL_NO_RC5
1032     RC5_32_set_key(&rc5_ks, 16, key16, 12);
1033 #endif
1034 #ifndef OPENSSL_NO_BF
1035     BF_set_key(&bf_ks, 16, key16);
1036 #endif
1037 #ifndef OPENSSL_NO_CAST
1038     CAST_set_key(&cast_ks, 16, key16);
1039 #endif
1040 #ifndef OPENSSL_NO_RSA
1041     memset(rsa_c, 0, sizeof(rsa_c));
1042 #endif
1043 #ifndef SIGALRM
1044 # ifndef OPENSSL_NO_DES
1045     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1046     count = 10;
1047     do {
1048         long it;
1049         count *= 2;
1050         Time_F(START);
1051         for (it = count; it; it--)
1052             DES_ecb_encrypt((DES_cblock *)buf,
1053                             (DES_cblock *)buf, &sch, DES_ENCRYPT);
1054         d = Time_F(STOP);
1055     } while (d < 3);
1056     save_count = count;
1057     c[D_MD2][0] = count / 10;
1058     c[D_MDC2][0] = count / 10;
1059     c[D_MD4][0] = count;
1060     c[D_MD5][0] = count;
1061     c[D_HMAC][0] = count;
1062     c[D_SHA1][0] = count;
1063     c[D_RMD160][0] = count;
1064     c[D_RC4][0] = count * 5;
1065     c[D_CBC_DES][0] = count;
1066     c[D_EDE3_DES][0] = count / 3;
1067     c[D_CBC_IDEA][0] = count;
1068     c[D_CBC_SEED][0] = count;
1069     c[D_CBC_RC2][0] = count;
1070     c[D_CBC_RC5][0] = count;
1071     c[D_CBC_BF][0] = count;
1072     c[D_CBC_CAST][0] = count;
1073     c[D_CBC_128_AES][0] = count;
1074     c[D_CBC_192_AES][0] = count;
1075     c[D_CBC_256_AES][0] = count;
1076     c[D_CBC_128_CML][0] = count;
1077     c[D_CBC_192_CML][0] = count;
1078     c[D_CBC_256_CML][0] = count;
1079     c[D_SHA256][0] = count;
1080     c[D_SHA512][0] = count;
1081     c[D_WHIRLPOOL][0] = count;
1082     c[D_IGE_128_AES][0] = count;
1083     c[D_IGE_192_AES][0] = count;
1084     c[D_IGE_256_AES][0] = count;
1085     c[D_GHASH][0] = count;
1086
1087     for (i = 1; i < SIZE_NUM; i++) {
1088         long l0, l1;
1089
1090         l0 = (long)lengths[0];
1091         l1 = (long)lengths[i];
1092
1093         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1094         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1095         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1096         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1097         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1098         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1099         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1100         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1101         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1102         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1103
1104         l0 = (long)lengths[i - 1];
1105
1106         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1107         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1108         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1109         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1110         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1111         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1112         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1113         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1114         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1115         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1116         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1117         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1118         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1119         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1120         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1121         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1122         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1123         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1124     }
1125
1126 #  ifndef OPENSSL_NO_RSA
1127     rsa_c[R_RSA_512][0] = count / 2000;
1128     rsa_c[R_RSA_512][1] = count / 400;
1129     for (i = 1; i < RSA_NUM; i++) {
1130         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1131         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1132         if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
1133             rsa_doit[i] = 0;
1134         else {
1135             if (rsa_c[i][0] == 0) {
1136                 rsa_c[i][0] = 1;
1137                 rsa_c[i][1] = 20;
1138             }
1139         }
1140     }
1141 #  endif
1142
1143 #  ifndef OPENSSL_NO_DSA
1144     dsa_c[R_DSA_512][0] = count / 1000;
1145     dsa_c[R_DSA_512][1] = count / 1000 / 2;
1146     for (i = 1; i < DSA_NUM; i++) {
1147         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1148         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1149         if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
1150             dsa_doit[i] = 0;
1151         else {
1152             if (dsa_c[i] == 0) {
1153                 dsa_c[i][0] = 1;
1154                 dsa_c[i][1] = 1;
1155             }
1156         }
1157     }
1158 #  endif
1159
1160 #  ifndef OPENSSL_NO_EC
1161     ecdsa_c[R_EC_P160][0] = count / 1000;
1162     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1163     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1164         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1165         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1166         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1167             ecdsa_doit[i] = 0;
1168         else {
1169             if (ecdsa_c[i] == 0) {
1170                 ecdsa_c[i][0] = 1;
1171                 ecdsa_c[i][1] = 1;
1172             }
1173         }
1174     }
1175     ecdsa_c[R_EC_K163][0] = count / 1000;
1176     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1177     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1178         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1179         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1180         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1181             ecdsa_doit[i] = 0;
1182         else {
1183             if (ecdsa_c[i] == 0) {
1184                 ecdsa_c[i][0] = 1;
1185                 ecdsa_c[i][1] = 1;
1186             }
1187         }
1188     }
1189     ecdsa_c[R_EC_B163][0] = count / 1000;
1190     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1191     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1192         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1193         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1194         if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
1195             ecdsa_doit[i] = 0;
1196         else {
1197             if (ecdsa_c[i] == 0) {
1198                 ecdsa_c[i][0] = 1;
1199                 ecdsa_c[i][1] = 1;
1200             }
1201         }
1202     }
1203
1204     ecdh_c[R_EC_P160][0] = count / 1000;
1205     ecdh_c[R_EC_P160][1] = count / 1000;
1206     for (i = R_EC_P192; i <= R_EC_P521; i++) {
1207         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1208         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1209         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1210             ecdh_doit[i] = 0;
1211         else {
1212             if (ecdh_c[i] == 0) {
1213                 ecdh_c[i][0] = 1;
1214                 ecdh_c[i][1] = 1;
1215             }
1216         }
1217     }
1218     ecdh_c[R_EC_K163][0] = count / 1000;
1219     ecdh_c[R_EC_K163][1] = count / 1000;
1220     for (i = R_EC_K233; i <= R_EC_K571; i++) {
1221         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1222         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1223         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1224             ecdh_doit[i] = 0;
1225         else {
1226             if (ecdh_c[i] == 0) {
1227                 ecdh_c[i][0] = 1;
1228                 ecdh_c[i][1] = 1;
1229             }
1230         }
1231     }
1232     ecdh_c[R_EC_B163][0] = count / 1000;
1233     ecdh_c[R_EC_B163][1] = count / 1000;
1234     for (i = R_EC_B233; i <= R_EC_B571; i++) {
1235         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1236         ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
1237         if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
1238             ecdh_doit[i] = 0;
1239         else {
1240             if (ecdh_c[i] == 0) {
1241                 ecdh_c[i][0] = 1;
1242                 ecdh_c[i][1] = 1;
1243             }
1244         }
1245     }
1246 #  endif
1247
1248 #  define COND(d) (count < (d))
1249 #  define COUNT(d) (d)
1250 # else
1251 /* not worth fixing */
1252 #  error "You cannot disable DES on systems without SIGALRM."
1253 # endif                        /* OPENSSL_NO_DES */
1254 #else
1255 # define COND(c) (run && count<0x7fffffff)
1256 # define COUNT(d) (count)
1257 # ifndef _WIN32
1258     signal(SIGALRM, sig_done);
1259 # endif
1260 #endif                         /* SIGALRM */
1261
1262 #ifndef OPENSSL_NO_MD2
1263     if (doit[D_MD2]) {
1264         for (j = 0; j < SIZE_NUM; j++) {
1265             print_message(names[D_MD2], c[D_MD2][j], lengths[j]);
1266             Time_F(START);
1267             for (count = 0, run = 1; COND(c[D_MD2][j]); count++)
1268                 EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL,
1269                            EVP_md2(), NULL);
1270             d = Time_F(STOP);
1271             print_result(D_MD2, j, count, d);
1272         }
1273     }
1274 #endif
1275 #ifndef OPENSSL_NO_MDC2
1276     if (doit[D_MDC2]) {
1277         for (j = 0; j < SIZE_NUM; j++) {
1278             print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);
1279             Time_F(START);
1280             for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)
1281                 EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL,
1282                            EVP_mdc2(), NULL);
1283             d = Time_F(STOP);
1284             print_result(D_MDC2, j, count, d);
1285         }
1286     }
1287 #endif
1288
1289 #ifndef OPENSSL_NO_MD4
1290     if (doit[D_MD4]) {
1291         for (j = 0; j < SIZE_NUM; j++) {
1292             print_message(names[D_MD4], c[D_MD4][j], lengths[j]);
1293             Time_F(START);
1294             for (count = 0, run = 1; COND(c[D_MD4][j]); count++)
1295                 EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]),
1296                            NULL, EVP_md4(), NULL);
1297             d = Time_F(STOP);
1298             print_result(D_MD4, j, count, d);
1299         }
1300     }
1301 #endif
1302
1303 #ifndef OPENSSL_NO_MD5
1304     if (doit[D_MD5]) {
1305         for (j = 0; j < SIZE_NUM; j++) {
1306             print_message(names[D_MD5], c[D_MD5][j], lengths[j]);
1307             Time_F(START);
1308             for (count = 0, run = 1; COND(c[D_MD5][j]); count++)
1309                 MD5(buf, lengths[j], md5);
1310             d = Time_F(STOP);
1311             print_result(D_MD5, j, count, d);
1312         }
1313     }
1314 #endif
1315
1316 #if !defined(OPENSSL_NO_MD5)
1317     if (doit[D_HMAC]) {
1318         HMAC_CTX hctx;
1319
1320         HMAC_CTX_init(&hctx);
1321         HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...",
1322                      16, EVP_md5(), NULL);
1323
1324         for (j = 0; j < SIZE_NUM; j++) {
1325             print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);
1326             Time_F(START);
1327             for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {
1328                 HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);
1329                 HMAC_Update(&hctx, buf, lengths[j]);
1330                 HMAC_Final(&hctx, &(hmac[0]), NULL);
1331             }
1332             d = Time_F(STOP);
1333             print_result(D_HMAC, j, count, d);
1334         }
1335         HMAC_CTX_cleanup(&hctx);
1336     }
1337 #endif
1338     if (doit[D_SHA1]) {
1339         for (j = 0; j < SIZE_NUM; j++) {
1340             print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);
1341             Time_F(START);
1342             for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)
1343                 SHA1(buf, lengths[j], sha);
1344             d = Time_F(STOP);
1345             print_result(D_SHA1, j, count, d);
1346         }
1347     }
1348     if (doit[D_SHA256]) {
1349         for (j = 0; j < SIZE_NUM; j++) {
1350             print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]);
1351             Time_F(START);
1352             for (count = 0, run = 1; COND(c[D_SHA256][j]); count++)
1353                 SHA256(buf, lengths[j], sha256);
1354             d = Time_F(STOP);
1355             print_result(D_SHA256, j, count, d);
1356         }
1357     }
1358     if (doit[D_SHA512]) {
1359         for (j = 0; j < SIZE_NUM; j++) {
1360             print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]);
1361             Time_F(START);
1362             for (count = 0, run = 1; COND(c[D_SHA512][j]); count++)
1363                 SHA512(buf, lengths[j], sha512);
1364             d = Time_F(STOP);
1365             print_result(D_SHA512, j, count, d);
1366         }
1367     }
1368
1369 #ifndef OPENSSL_NO_WHIRLPOOL
1370     if (doit[D_WHIRLPOOL]) {
1371         for (j = 0; j < SIZE_NUM; j++) {
1372             print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]);
1373             Time_F(START);
1374             for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++)
1375                 WHIRLPOOL(buf, lengths[j], whirlpool);
1376             d = Time_F(STOP);
1377             print_result(D_WHIRLPOOL, j, count, d);
1378         }
1379     }
1380 #endif
1381
1382 #ifndef OPENSSL_NO_RMD160
1383     if (doit[D_RMD160]) {
1384         for (j = 0; j < SIZE_NUM; j++) {
1385             print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]);
1386             Time_F(START);
1387             for (count = 0, run = 1; COND(c[D_RMD160][j]); count++)
1388                 EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL,
1389                            EVP_ripemd160(), NULL);
1390             d = Time_F(STOP);
1391             print_result(D_RMD160, j, count, d);
1392         }
1393     }
1394 #endif
1395 #ifndef OPENSSL_NO_RC4
1396     if (doit[D_RC4]) {
1397         for (j = 0; j < SIZE_NUM; j++) {
1398             print_message(names[D_RC4], c[D_RC4][j], lengths[j]);
1399             Time_F(START);
1400             for (count = 0, run = 1; COND(c[D_RC4][j]); count++)
1401                 RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf);
1402             d = Time_F(STOP);
1403             print_result(D_RC4, j, count, d);
1404         }
1405     }
1406 #endif
1407 #ifndef OPENSSL_NO_DES
1408     if (doit[D_CBC_DES]) {
1409         for (j = 0; j < SIZE_NUM; j++) {
1410             print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]);
1411             Time_F(START);
1412             for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++)
1413                 DES_ncbc_encrypt(buf, buf, lengths[j], &sch,
1414                                  &DES_iv, DES_ENCRYPT);
1415             d = Time_F(STOP);
1416             print_result(D_CBC_DES, j, count, d);
1417         }
1418     }
1419
1420     if (doit[D_EDE3_DES]) {
1421         for (j = 0; j < SIZE_NUM; j++) {
1422             print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]);
1423             Time_F(START);
1424             for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++)
1425                 DES_ede3_cbc_encrypt(buf, buf, lengths[j],
1426                                      &sch, &sch2, &sch3,
1427                                      &DES_iv, DES_ENCRYPT);
1428             d = Time_F(STOP);
1429             print_result(D_EDE3_DES, j, count, d);
1430         }
1431     }
1432 #endif
1433 #ifndef OPENSSL_NO_AES
1434     if (doit[D_CBC_128_AES]) {
1435         for (j = 0; j < SIZE_NUM; j++) {
1436             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j],
1437                           lengths[j]);
1438             Time_F(START);
1439             for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++)
1440                 AES_cbc_encrypt(buf, buf,
1441                                 (unsigned long)lengths[j], &aes_ks1,
1442                                 iv, AES_ENCRYPT);
1443             d = Time_F(STOP);
1444             print_result(D_CBC_128_AES, j, count, d);
1445         }
1446     }
1447     if (doit[D_CBC_192_AES]) {
1448         for (j = 0; j < SIZE_NUM; j++) {
1449             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j],
1450                           lengths[j]);
1451             Time_F(START);
1452             for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++)
1453                 AES_cbc_encrypt(buf, buf,
1454                                 (unsigned long)lengths[j], &aes_ks2,
1455                                 iv, AES_ENCRYPT);
1456             d = Time_F(STOP);
1457             print_result(D_CBC_192_AES, j, count, d);
1458         }
1459     }
1460     if (doit[D_CBC_256_AES]) {
1461         for (j = 0; j < SIZE_NUM; j++) {
1462             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j],
1463                           lengths[j]);
1464             Time_F(START);
1465             for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++)
1466                 AES_cbc_encrypt(buf, buf,
1467                                 (unsigned long)lengths[j], &aes_ks3,
1468                                 iv, AES_ENCRYPT);
1469             d = Time_F(STOP);
1470             print_result(D_CBC_256_AES, j, count, d);
1471         }
1472     }
1473
1474     if (doit[D_IGE_128_AES]) {
1475         for (j = 0; j < SIZE_NUM; j++) {
1476             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j],
1477                           lengths[j]);
1478             Time_F(START);
1479             for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++)
1480                 AES_ige_encrypt(buf, buf2,
1481                                 (unsigned long)lengths[j], &aes_ks1,
1482                                 iv, AES_ENCRYPT);
1483             d = Time_F(STOP);
1484             print_result(D_IGE_128_AES, j, count, d);
1485         }
1486     }
1487     if (doit[D_IGE_192_AES]) {
1488         for (j = 0; j < SIZE_NUM; j++) {
1489             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j],
1490                           lengths[j]);
1491             Time_F(START);
1492             for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++)
1493                 AES_ige_encrypt(buf, buf2,
1494                                 (unsigned long)lengths[j], &aes_ks2,
1495                                 iv, AES_ENCRYPT);
1496             d = Time_F(STOP);
1497             print_result(D_IGE_192_AES, j, count, d);
1498         }
1499     }
1500     if (doit[D_IGE_256_AES]) {
1501         for (j = 0; j < SIZE_NUM; j++) {
1502             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j],
1503                           lengths[j]);
1504             Time_F(START);
1505             for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++)
1506                 AES_ige_encrypt(buf, buf2,
1507                                 (unsigned long)lengths[j], &aes_ks3,
1508                                 iv, AES_ENCRYPT);
1509             d = Time_F(STOP);
1510             print_result(D_IGE_256_AES, j, count, d);
1511         }
1512     }
1513     if (doit[D_GHASH]) {
1514         GCM128_CONTEXT *ctx =
1515             CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
1516         CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12);
1517
1518         for (j = 0; j < SIZE_NUM; j++) {
1519             print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]);
1520             Time_F(START);
1521             for (count = 0, run = 1; COND(c[D_GHASH][j]); count++)
1522                 CRYPTO_gcm128_aad(ctx, buf, lengths[j]);
1523             d = Time_F(STOP);
1524             print_result(D_GHASH, j, count, d);
1525         }
1526         CRYPTO_gcm128_release(ctx);
1527     }
1528 #endif
1529 #ifndef OPENSSL_NO_CAMELLIA
1530     if (doit[D_CBC_128_CML]) {
1531         for (j = 0; j < SIZE_NUM; j++) {
1532             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j],
1533                           lengths[j]);
1534             Time_F(START);
1535             for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++)
1536                 Camellia_cbc_encrypt(buf, buf,
1537                                      (unsigned long)lengths[j], &camellia_ks1,
1538                                      iv, CAMELLIA_ENCRYPT);
1539             d = Time_F(STOP);
1540             print_result(D_CBC_128_CML, j, count, d);
1541         }
1542     }
1543     if (doit[D_CBC_192_CML]) {
1544         for (j = 0; j < SIZE_NUM; j++) {
1545             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j],
1546                           lengths[j]);
1547             Time_F(START);
1548             for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++)
1549                 Camellia_cbc_encrypt(buf, buf,
1550                                      (unsigned long)lengths[j], &camellia_ks2,
1551                                      iv, CAMELLIA_ENCRYPT);
1552             d = Time_F(STOP);
1553             print_result(D_CBC_192_CML, j, count, d);
1554         }
1555     }
1556     if (doit[D_CBC_256_CML]) {
1557         for (j = 0; j < SIZE_NUM; j++) {
1558             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j],
1559                           lengths[j]);
1560             Time_F(START);
1561             for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++)
1562                 Camellia_cbc_encrypt(buf, buf,
1563                                      (unsigned long)lengths[j], &camellia_ks3,
1564                                      iv, CAMELLIA_ENCRYPT);
1565             d = Time_F(STOP);
1566             print_result(D_CBC_256_CML, j, count, d);
1567         }
1568     }
1569 #endif
1570 #ifndef OPENSSL_NO_IDEA
1571     if (doit[D_CBC_IDEA]) {
1572         for (j = 0; j < SIZE_NUM; j++) {
1573             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]);
1574             Time_F(START);
1575             for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++)
1576                 idea_cbc_encrypt(buf, buf,
1577                                  (unsigned long)lengths[j], &idea_ks,
1578                                  iv, IDEA_ENCRYPT);
1579             d = Time_F(STOP);
1580             print_result(D_CBC_IDEA, j, count, d);
1581         }
1582     }
1583 #endif
1584 #ifndef OPENSSL_NO_SEED
1585     if (doit[D_CBC_SEED]) {
1586         for (j = 0; j < SIZE_NUM; j++) {
1587             print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]);
1588             Time_F(START);
1589             for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++)
1590                 SEED_cbc_encrypt(buf, buf,
1591                                  (unsigned long)lengths[j], &seed_ks, iv, 1);
1592             d = Time_F(STOP);
1593             print_result(D_CBC_SEED, j, count, d);
1594         }
1595     }
1596 #endif
1597 #ifndef OPENSSL_NO_RC2
1598     if (doit[D_CBC_RC2]) {
1599         for (j = 0; j < SIZE_NUM; j++) {
1600             print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]);
1601             Time_F(START);
1602             for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++)
1603                 RC2_cbc_encrypt(buf, buf,
1604                                 (unsigned long)lengths[j], &rc2_ks,
1605                                 iv, RC2_ENCRYPT);
1606             d = Time_F(STOP);
1607             print_result(D_CBC_RC2, j, count, d);
1608         }
1609     }
1610 #endif
1611 #ifndef OPENSSL_NO_RC5
1612     if (doit[D_CBC_RC5]) {
1613         for (j = 0; j < SIZE_NUM; j++) {
1614             print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]);
1615             Time_F(START);
1616             for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++)
1617                 RC5_32_cbc_encrypt(buf, buf,
1618                                    (unsigned long)lengths[j], &rc5_ks,
1619                                    iv, RC5_ENCRYPT);
1620             d = Time_F(STOP);
1621             print_result(D_CBC_RC5, j, count, d);
1622         }
1623     }
1624 #endif
1625 #ifndef OPENSSL_NO_BF
1626     if (doit[D_CBC_BF]) {
1627         for (j = 0; j < SIZE_NUM; j++) {
1628             print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]);
1629             Time_F(START);
1630             for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++)
1631                 BF_cbc_encrypt(buf, buf,
1632                                (unsigned long)lengths[j], &bf_ks,
1633                                iv, BF_ENCRYPT);
1634             d = Time_F(STOP);
1635             print_result(D_CBC_BF, j, count, d);
1636         }
1637     }
1638 #endif
1639 #ifndef OPENSSL_NO_CAST
1640     if (doit[D_CBC_CAST]) {
1641         for (j = 0; j < SIZE_NUM; j++) {
1642             print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]);
1643             Time_F(START);
1644             for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++)
1645                 CAST_cbc_encrypt(buf, buf,
1646                                  (unsigned long)lengths[j], &cast_ks,
1647                                  iv, CAST_ENCRYPT);
1648             d = Time_F(STOP);
1649             print_result(D_CBC_CAST, j, count, d);
1650         }
1651     }
1652 #endif
1653
1654     if (doit[D_EVP]) {
1655 #ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
1656         if (multiblock && evp_cipher) {
1657             if (!
1658                 (EVP_CIPHER_flags(evp_cipher) &
1659                  EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1660                 fprintf(stderr, "%s is not multi-block capable\n",
1661                         OBJ_nid2ln(evp_cipher->nid));
1662                 goto end;
1663             }
1664             multiblock_speed(evp_cipher);
1665             ret = 0;
1666             goto end;
1667         }
1668 #endif
1669         for (j = 0; j < SIZE_NUM; j++) {
1670             if (evp_cipher) {
1671                 EVP_CIPHER_CTX ctx;
1672                 int outl;
1673
1674                 names[D_EVP] = OBJ_nid2ln(evp_cipher->nid);
1675                 /*
1676                  * -O3 -fschedule-insns messes up an optimization here!
1677                  * names[D_EVP] somehow becomes NULL
1678                  */
1679                 print_message(names[D_EVP], save_count, lengths[j]);
1680
1681                 EVP_CIPHER_CTX_init(&ctx);
1682                 if (decrypt)
1683                     EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
1684                 else
1685                     EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
1686                 EVP_CIPHER_CTX_set_padding(&ctx, 0);
1687
1688                 Time_F(START);
1689                 if (decrypt)
1690                     for (count = 0, run = 1;
1691                          COND(save_count * 4 * lengths[0] / lengths[j]);
1692                          count++)
1693                         EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
1694                 else
1695                     for (count = 0, run = 1;
1696                          COND(save_count * 4 * lengths[0] / lengths[j]);
1697                          count++)
1698                         EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
1699                 if (decrypt)
1700                     EVP_DecryptFinal_ex(&ctx, buf, &outl);
1701                 else
1702                     EVP_EncryptFinal_ex(&ctx, buf, &outl);
1703                 d = Time_F(STOP);
1704                 EVP_CIPHER_CTX_cleanup(&ctx);
1705             }
1706             if (evp_md) {
1707                 names[D_EVP] = OBJ_nid2ln(evp_md->type);
1708                 print_message(names[D_EVP], save_count, lengths[j]);
1709
1710                 Time_F(START);
1711                 for (count = 0, run = 1;
1712                      COND(save_count * 4 * lengths[0] / lengths[j]); count++)
1713                     EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL);
1714
1715                 d = Time_F(STOP);
1716             }
1717             print_result(D_EVP, j, count, d);
1718         }
1719     }
1720
1721     RAND_bytes(buf, 36);
1722 #ifndef OPENSSL_NO_RSA
1723     for (j = 0; j < RSA_NUM; j++) {
1724         int st;
1725         if (!rsa_doit[j])
1726             continue;
1727         st = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]);
1728         if (st == 0) {
1729             BIO_printf(bio_err,
1730                        "RSA sign failure.  No RSA sign will be done.\n");
1731             ERR_print_errors(bio_err);
1732             rsa_count = 1;
1733         } else {
1734             pkey_print_message("private", "rsa",
1735                                rsa_c[j][0], rsa_bits[j], RSA_SECONDS);
1736             /* RSA_blinding_on(rsa_key[j],NULL); */
1737             Time_F(START);
1738             for (count = 0, run = 1; COND(rsa_c[j][0]); count++) {
1739                 st = RSA_sign(NID_md5_sha1, buf, 36, buf2,
1740                               &rsa_num, rsa_key[j]);
1741                 if (st == 0) {
1742                     BIO_printf(bio_err, "RSA sign failure\n");
1743                     ERR_print_errors(bio_err);
1744                     count = 1;
1745                     break;
1746                 }
1747             }
1748             d = Time_F(STOP);
1749             BIO_printf(bio_err,
1750                        mr ? "+R1:%ld:%d:%.2f\n"
1751                        : "%ld %d bit private RSA's in %.2fs\n",
1752                        count, rsa_bits[j], d);
1753             rsa_results[j][0] = d / (double)count;
1754             rsa_count = count;
1755         }
1756
1757         st = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);
1758         if (st <= 0) {
1759             BIO_printf(bio_err,
1760                        "RSA verify failure.  No RSA verify will be done.\n");
1761             ERR_print_errors(bio_err);
1762             rsa_doit[j] = 0;
1763         } else {
1764             pkey_print_message("public", "rsa",
1765                                rsa_c[j][1], rsa_bits[j], RSA_SECONDS);
1766             Time_F(START);
1767             for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {
1768                 st = RSA_verify(NID_md5_sha1, buf, 36, buf2,
1769                                 rsa_num, rsa_key[j]);
1770                 if (st <= 0) {
1771                     BIO_printf(bio_err, "RSA verify failure\n");
1772                     ERR_print_errors(bio_err);
1773                     count = 1;
1774                     break;
1775                 }
1776             }
1777             d = Time_F(STOP);
1778             BIO_printf(bio_err,
1779                        mr ? "+R2:%ld:%d:%.2f\n"
1780                        : "%ld %d bit public RSA's in %.2fs\n",
1781                        count, rsa_bits[j], d);
1782             rsa_results[j][1] = d / (double)count;
1783         }
1784
1785         if (rsa_count <= 1) {
1786             /* if longer than 10s, don't do any more */
1787             for (j++; j < RSA_NUM; j++)
1788                 rsa_doit[j] = 0;
1789         }
1790     }
1791 #endif
1792
1793     RAND_bytes(buf, 20);
1794 #ifndef OPENSSL_NO_DSA
1795     if (RAND_status() != 1) {
1796         RAND_seed(rnd_seed, sizeof rnd_seed);
1797         rnd_fake = 1;
1798     }
1799     for (j = 0; j < DSA_NUM; j++) {
1800         unsigned int kk;
1801         int st;
1802
1803         if (!dsa_doit[j])
1804             continue;
1805
1806         /* DSA_generate_key(dsa_key[j]); */
1807         /* DSA_sign_setup(dsa_key[j],NULL); */
1808         st = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
1809         if (st == 0) {
1810             BIO_printf(bio_err,
1811                        "DSA sign failure.  No DSA sign will be done.\n");
1812             ERR_print_errors(bio_err);
1813             rsa_count = 1;
1814         } else {
1815             pkey_print_message("sign", "dsa",
1816                                dsa_c[j][0], dsa_bits[j], DSA_SECONDS);
1817             Time_F(START);
1818             for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {
1819                 st = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
1820                 if (st == 0) {
1821                     BIO_printf(bio_err, "DSA sign failure\n");
1822                     ERR_print_errors(bio_err);
1823                     count = 1;
1824                     break;
1825                 }
1826             }
1827             d = Time_F(STOP);
1828             BIO_printf(bio_err,
1829                        mr ? "+R3:%ld:%d:%.2f\n"
1830                        : "%ld %d bit DSA signs in %.2fs\n",
1831                        count, dsa_bits[j], d);
1832             dsa_results[j][0] = d / (double)count;
1833             rsa_count = count;
1834         }
1835
1836         st = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
1837         if (st <= 0) {
1838             BIO_printf(bio_err,
1839                        "DSA verify failure.  No DSA verify will be done.\n");
1840             ERR_print_errors(bio_err);
1841             dsa_doit[j] = 0;
1842         } else {
1843             pkey_print_message("verify", "dsa",
1844                                dsa_c[j][1], dsa_bits[j], DSA_SECONDS);
1845             Time_F(START);
1846             for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {
1847                 st = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
1848                 if (st <= 0) {
1849                     BIO_printf(bio_err, "DSA verify failure\n");
1850                     ERR_print_errors(bio_err);
1851                     count = 1;
1852                     break;
1853                 }
1854             }
1855             d = Time_F(STOP);
1856             BIO_printf(bio_err,
1857                        mr ? "+R4:%ld:%d:%.2f\n"
1858                        : "%ld %d bit DSA verify in %.2fs\n",
1859                        count, dsa_bits[j], d);
1860             dsa_results[j][1] = d / (double)count;
1861         }
1862
1863         if (rsa_count <= 1) {
1864             /* if longer than 10s, don't do any more */
1865             for (j++; j < DSA_NUM; j++)
1866                 dsa_doit[j] = 0;
1867         }
1868     }
1869     if (rnd_fake)
1870         RAND_cleanup();
1871 #endif
1872
1873 #ifndef OPENSSL_NO_EC
1874     if (RAND_status() != 1) {
1875         RAND_seed(rnd_seed, sizeof rnd_seed);
1876         rnd_fake = 1;
1877     }
1878     for (j = 0; j < EC_NUM; j++) {
1879         int st;
1880
1881         if (!ecdsa_doit[j])
1882             continue;           /* Ignore Curve */
1883         ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);
1884         if (ecdsa[j] == NULL) {
1885             BIO_printf(bio_err, "ECDSA failure.\n");
1886             ERR_print_errors(bio_err);
1887             rsa_count = 1;
1888         } else {
1889             EC_KEY_precompute_mult(ecdsa[j], NULL);
1890             /* Perform ECDSA signature test */
1891             EC_KEY_generate_key(ecdsa[j]);
1892             st = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);
1893             if (st == 0) {
1894                 BIO_printf(bio_err,
1895                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
1896                 ERR_print_errors(bio_err);
1897                 rsa_count = 1;
1898             } else {
1899                 pkey_print_message("sign", "ecdsa",
1900                                    ecdsa_c[j][0],
1901                                    test_curves_bits[j], ECDSA_SECONDS);
1902
1903                 Time_F(START);
1904                 for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {
1905                     st = ECDSA_sign(0, buf, 20,
1906                                     ecdsasig, &ecdsasiglen, ecdsa[j]);
1907                     if (st == 0) {
1908                         BIO_printf(bio_err, "ECDSA sign failure\n");
1909                         ERR_print_errors(bio_err);
1910                         count = 1;
1911                         break;
1912                     }
1913                 }
1914                 d = Time_F(STOP);
1915
1916                 BIO_printf(bio_err,
1917                            mr ? "+R5:%ld:%d:%.2f\n" :
1918                            "%ld %d bit ECDSA signs in %.2fs \n",
1919                            count, test_curves_bits[j], d);
1920                 ecdsa_results[j][0] = d / (double)count;
1921                 rsa_count = count;
1922             }
1923
1924             /* Perform ECDSA verification test */
1925             st = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
1926             if (st != 1) {
1927                 BIO_printf(bio_err,
1928                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
1929                 ERR_print_errors(bio_err);
1930                 ecdsa_doit[j] = 0;
1931             } else {
1932                 pkey_print_message("verify", "ecdsa",
1933                                    ecdsa_c[j][1],
1934                                    test_curves_bits[j], ECDSA_SECONDS);
1935                 Time_F(START);
1936                 for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {
1937                     st = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,
1938                                       ecdsa[j]);
1939                     if (st != 1) {
1940                         BIO_printf(bio_err, "ECDSA verify failure\n");
1941                         ERR_print_errors(bio_err);
1942                         count = 1;
1943                         break;
1944                     }
1945                 }
1946                 d = Time_F(STOP);
1947                 BIO_printf(bio_err,
1948                            mr ? "+R6:%ld:%d:%.2f\n"
1949                            : "%ld %d bit ECDSA verify in %.2fs\n",
1950                            count, test_curves_bits[j], d);
1951                 ecdsa_results[j][1] = d / (double)count;
1952             }
1953
1954             if (rsa_count <= 1) {
1955                 /* if longer than 10s, don't do any more */
1956                 for (j++; j < EC_NUM; j++)
1957                     ecdsa_doit[j] = 0;
1958             }
1959         }
1960     }
1961     if (rnd_fake)
1962         RAND_cleanup();
1963 #endif
1964
1965 #ifndef OPENSSL_NO_EC
1966     if (RAND_status() != 1) {
1967         RAND_seed(rnd_seed, sizeof rnd_seed);
1968         rnd_fake = 1;
1969     }
1970     for (j = 0; j < EC_NUM; j++) {
1971         if (!ecdh_doit[j])
1972             continue;
1973         ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);
1974         ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);
1975         if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {
1976             BIO_printf(bio_err, "ECDH failure.\n");
1977             ERR_print_errors(bio_err);
1978             rsa_count = 1;
1979         } else {
1980             /* generate two ECDH key pairs */
1981             if (!EC_KEY_generate_key(ecdh_a[j]) ||
1982                 !EC_KEY_generate_key(ecdh_b[j])) {
1983                 BIO_printf(bio_err, "ECDH key generation failure.\n");
1984                 ERR_print_errors(bio_err);
1985                 rsa_count = 1;
1986             } else {
1987                 /*
1988                  * If field size is not more than 24 octets, then use SHA-1
1989                  * hash of result; otherwise, use result (see section 4.8 of
1990                  * draft-ietf-tls-ecc-03.txt).
1991                  */
1992                 int field_size, outlen;
1993                 void *(*kdf) (const void *in, size_t inlen, void *out,
1994                               size_t *xoutlen);
1995                 field_size =
1996                     EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));
1997                 if (field_size <= 24 * 8) {
1998                     outlen = KDF1_SHA1_len;
1999                     kdf = KDF1_SHA1;
2000                 } else {
2001                     outlen = (field_size + 7) / 8;
2002                     kdf = NULL;
2003                 }
2004                 secret_size_a =
2005                     ECDH_compute_key(secret_a, outlen,
2006                                      EC_KEY_get0_public_key(ecdh_b[j]),
2007                                      ecdh_a[j], kdf);
2008                 secret_size_b =
2009                     ECDH_compute_key(secret_b, outlen,
2010                                      EC_KEY_get0_public_key(ecdh_a[j]),
2011                                      ecdh_b[j], kdf);
2012                 if (secret_size_a != secret_size_b)
2013                     ecdh_checks = 0;
2014                 else
2015                     ecdh_checks = 1;
2016
2017                 for (secret_idx = 0; (secret_idx < secret_size_a)
2018                      && (ecdh_checks == 1); secret_idx++) {
2019                     if (secret_a[secret_idx] != secret_b[secret_idx])
2020                         ecdh_checks = 0;
2021                 }
2022
2023                 if (ecdh_checks == 0) {
2024                     BIO_printf(bio_err, "ECDH computations don't match.\n");
2025                     ERR_print_errors(bio_err);
2026                     rsa_count = 1;
2027                 }
2028
2029                 pkey_print_message("", "ecdh",
2030                                    ecdh_c[j][0],
2031                                    test_curves_bits[j], ECDH_SECONDS);
2032                 Time_F(START);
2033                 for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {
2034                     ECDH_compute_key(secret_a, outlen,
2035                                      EC_KEY_get0_public_key(ecdh_b[j]),
2036                                      ecdh_a[j], kdf);
2037                 }
2038                 d = Time_F(STOP);
2039                 BIO_printf(bio_err,
2040                            mr ? "+R7:%ld:%d:%.2f\n" :
2041                            "%ld %d-bit ECDH ops in %.2fs\n", count,
2042                            test_curves_bits[j], d);
2043                 ecdh_results[j][0] = d / (double)count;
2044                 rsa_count = count;
2045             }
2046         }
2047
2048         if (rsa_count <= 1) {
2049             /* if longer than 10s, don't do any more */
2050             for (j++; j < EC_NUM; j++)
2051                 ecdh_doit[j] = 0;
2052         }
2053     }
2054     if (rnd_fake)
2055         RAND_cleanup();
2056 #endif
2057 #ifndef NO_FORK
2058  show_res:
2059 #endif
2060     if (!mr) {
2061         printf("%s\n", SSLeay_version(SSLEAY_VERSION));
2062         printf("%s\n", SSLeay_version(SSLEAY_BUILT_ON));
2063         printf("options:");
2064         printf("%s ", BN_options());
2065 #ifndef OPENSSL_NO_MD2
2066         printf("%s ", MD2_options());
2067 #endif
2068 #ifndef OPENSSL_NO_RC4
2069         printf("%s ", RC4_options());
2070 #endif
2071 #ifndef OPENSSL_NO_DES
2072         printf("%s ", DES_options());
2073 #endif
2074 #ifndef OPENSSL_NO_AES
2075         printf("%s ", AES_options());
2076 #endif
2077 #ifndef OPENSSL_NO_IDEA
2078         printf("%s ", idea_options());
2079 #endif
2080 #ifndef OPENSSL_NO_BF
2081         printf("%s ", BF_options());
2082 #endif
2083         printf("\n%s\n", SSLeay_version(SSLEAY_CFLAGS));
2084     }
2085
2086     if (pr_header) {
2087         if (mr)
2088             printf("+H");
2089         else {
2090             printf
2091                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2092             printf("type        ");
2093         }
2094         for (j = 0; j < SIZE_NUM; j++)
2095             printf(mr ? ":%d" : "%7d bytes", lengths[j]);
2096         printf("\n");
2097     }
2098
2099     for (k = 0; k < ALGOR_NUM; k++) {
2100         if (!doit[k])
2101             continue;
2102         if (mr)
2103             printf("+F:%d:%s", k, names[k]);
2104         else
2105             printf("%-13s", names[k]);
2106         for (j = 0; j < SIZE_NUM; j++) {
2107             if (results[k][j] > 10000 && !mr)
2108                 printf(" %11.2fk", results[k][j] / 1e3);
2109             else
2110                 printf(mr ? ":%.2f" : " %11.2f ", results[k][j]);
2111         }
2112         printf("\n");
2113     }
2114 #ifndef OPENSSL_NO_RSA
2115     j = 1;
2116     for (k = 0; k < RSA_NUM; k++) {
2117         if (!rsa_doit[k])
2118             continue;
2119         if (j && !mr) {
2120             printf("%18ssign    verify    sign/s verify/s\n", " ");
2121             j = 0;
2122         }
2123         if (mr)
2124             printf("+F2:%u:%u:%f:%f\n",
2125                    k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2126         else
2127             printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2128                    rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2129                    1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
2130     }
2131 #endif
2132 #ifndef OPENSSL_NO_DSA
2133     j = 1;
2134     for (k = 0; k < DSA_NUM; k++) {
2135         if (!dsa_doit[k])
2136             continue;
2137         if (j && !mr) {
2138             printf("%18ssign    verify    sign/s verify/s\n", " ");
2139             j = 0;
2140         }
2141         if (mr)
2142             printf("+F3:%u:%u:%f:%f\n",
2143                    k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2144         else
2145             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2146                    dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2147                    1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
2148     }
2149 #endif
2150 #ifndef OPENSSL_NO_EC
2151     j = 1;
2152     for (k = 0; k < EC_NUM; k++) {
2153         if (!ecdsa_doit[k])
2154             continue;
2155         if (j && !mr) {
2156             printf("%30ssign    verify    sign/s verify/s\n", " ");
2157             j = 0;
2158         }
2159
2160         if (mr)
2161             printf("+F4:%u:%u:%f:%f\n",
2162                    k, test_curves_bits[k],
2163                    ecdsa_results[k][0], ecdsa_results[k][1]);
2164         else
2165             printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2166                    test_curves_bits[k],
2167                    test_curves_names[k],
2168                    ecdsa_results[k][0], ecdsa_results[k][1],
2169                    1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
2170     }
2171 #endif
2172
2173 #ifndef OPENSSL_NO_EC
2174     j = 1;
2175     for (k = 0; k < EC_NUM; k++) {
2176         if (!ecdh_doit[k])
2177             continue;
2178         if (j && !mr) {
2179             printf("%30sop      op/s\n", " ");
2180             j = 0;
2181         }
2182         if (mr)
2183             printf("+F5:%u:%u:%f:%f\n",
2184                    k, test_curves_bits[k],
2185                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2186
2187         else
2188             printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2189                    test_curves_bits[k],
2190                    test_curves_names[k],
2191                    ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2192     }
2193 #endif
2194
2195     ret = 0;
2196
2197  end:
2198     ERR_print_errors(bio_err);
2199     OPENSSL_free(save_buf);
2200     OPENSSL_free(save_buf2);
2201 #ifndef OPENSSL_NO_RSA
2202     for (i = 0; i < RSA_NUM; i++)
2203         RSA_free(rsa_key[i]);
2204 #endif
2205 #ifndef OPENSSL_NO_DSA
2206     for (i = 0; i < DSA_NUM; i++)
2207         DSA_free(dsa_key[i]);
2208 #endif
2209
2210 #ifndef OPENSSL_NO_EC
2211     for (i = 0; i < EC_NUM; i++) {
2212         EC_KEY_free(ecdsa[i]);
2213         EC_KEY_free(ecdh_a[i]);
2214         EC_KEY_free(ecdh_b[i]);
2215     }
2216 #endif
2217
2218     return (ret);
2219 }
2220
2221 static void print_message(const char *s, long num, int length)
2222 {
2223 #ifdef SIGALRM
2224     BIO_printf(bio_err,
2225                mr ? "+DT:%s:%d:%d\n"
2226                : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2227     (void)BIO_flush(bio_err);
2228     alarm(SECONDS);
2229 #else
2230     BIO_printf(bio_err,
2231                mr ? "+DN:%s:%ld:%d\n"
2232                : "Doing %s %ld times on %d size blocks: ", s, num, length);
2233     (void)BIO_flush(bio_err);
2234 #endif
2235 }
2236
2237 static void pkey_print_message(const char *str, const char *str2, long num,
2238                                int bits, int tm)
2239 {
2240 #ifdef SIGALRM
2241     BIO_printf(bio_err,
2242                mr ? "+DTP:%d:%s:%s:%d\n"
2243                : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2244     (void)BIO_flush(bio_err);
2245     alarm(tm);
2246 #else
2247     BIO_printf(bio_err,
2248                mr ? "+DNP:%ld:%d:%s:%s\n"
2249                : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2250     (void)BIO_flush(bio_err);
2251 #endif
2252 }
2253
2254 static void print_result(int alg, int run_no, int count, double time_used)
2255 {
2256     BIO_printf(bio_err,
2257                mr ? "+R:%d:%s:%f\n"
2258                : "%d %s's in %.2fs\n", count, names[alg], time_used);
2259     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2260 }
2261
2262 #ifndef NO_FORK
2263 static char *sstrsep(char **string, const char *delim)
2264 {
2265     char isdelim[256];
2266     char *token = *string;
2267
2268     if (**string == 0)
2269         return NULL;
2270
2271     memset(isdelim, 0, sizeof isdelim);
2272     isdelim[0] = 1;
2273
2274     while (*delim) {
2275         isdelim[(unsigned char)(*delim)] = 1;
2276         delim++;
2277     }
2278
2279     while (!isdelim[(unsigned char)(**string)]) {
2280         (*string)++;
2281     }
2282
2283     if (**string) {
2284         **string = 0;
2285         (*string)++;
2286     }
2287
2288     return token;
2289 }
2290
2291 static int do_multi(int multi)
2292 {
2293     int n;
2294     int fd[2];
2295     int *fds;
2296     static char sep[] = ":";
2297
2298     fds = malloc(multi * sizeof *fds);
2299     for (n = 0; n < multi; ++n) {
2300         if (pipe(fd) == -1) {
2301             fprintf(stderr, "pipe failure\n");
2302             exit(1);
2303         }
2304         fflush(stdout);
2305         fflush(stderr);
2306         if (fork()) {
2307             close(fd[1]);
2308             fds[n] = fd[0];
2309         } else {
2310             close(fd[0]);
2311             close(1);
2312             if (dup(fd[1]) == -1) {
2313                 fprintf(stderr, "dup failed\n");
2314                 exit(1);
2315             }
2316             close(fd[1]);
2317             mr = 1;
2318             usertime = 0;
2319             free(fds);
2320             return 0;
2321         }
2322         printf("Forked child %d\n", n);
2323     }
2324
2325     /* for now, assume the pipe is long enough to take all the output */
2326     for (n = 0; n < multi; ++n) {
2327         FILE *f;
2328         char buf[1024];
2329         char *p;
2330
2331         f = fdopen(fds[n], "r");
2332         while (fgets(buf, sizeof buf, f)) {
2333             p = strchr(buf, '\n');
2334             if (p)
2335                 *p = '\0';
2336             if (buf[0] != '+') {
2337                 fprintf(stderr, "Don't understand line '%s' from child %d\n",
2338                         buf, n);
2339                 continue;
2340             }
2341             printf("Got: %s from %d\n", buf, n);
2342             if (!strncmp(buf, "+F:", 3)) {
2343                 int alg;
2344                 int j;
2345
2346                 p = buf + 3;
2347                 alg = atoi(sstrsep(&p, sep));
2348                 sstrsep(&p, sep);
2349                 for (j = 0; j < SIZE_NUM; ++j)
2350                     results[alg][j] += atof(sstrsep(&p, sep));
2351             } else if (!strncmp(buf, "+F2:", 4)) {
2352                 int k;
2353                 double d;
2354
2355                 p = buf + 4;
2356                 k = atoi(sstrsep(&p, sep));
2357                 sstrsep(&p, sep);
2358
2359                 d = atof(sstrsep(&p, sep));
2360                 if (n)
2361                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
2362                 else
2363                     rsa_results[k][0] = d;
2364
2365                 d = atof(sstrsep(&p, sep));
2366                 if (n)
2367                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
2368                 else
2369                     rsa_results[k][1] = d;
2370             }
2371 # ifndef OPENSSL_NO_DSA
2372             else if (!strncmp(buf, "+F3:", 4)) {
2373                 int k;
2374                 double d;
2375
2376                 p = buf + 4;
2377                 k = atoi(sstrsep(&p, sep));
2378                 sstrsep(&p, sep);
2379
2380                 d = atof(sstrsep(&p, sep));
2381                 if (n)
2382                     dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
2383                 else
2384                     dsa_results[k][0] = d;
2385
2386                 d = atof(sstrsep(&p, sep));
2387                 if (n)
2388                     dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
2389                 else
2390                     dsa_results[k][1] = d;
2391             }
2392 # endif
2393 # ifndef OPENSSL_NO_EC
2394             else if (!strncmp(buf, "+F4:", 4)) {
2395                 int k;
2396                 double d;
2397
2398                 p = buf + 4;
2399                 k = atoi(sstrsep(&p, sep));
2400                 sstrsep(&p, sep);
2401
2402                 d = atof(sstrsep(&p, sep));
2403                 if (n)
2404                     ecdsa_results[k][0] =
2405                         1 / (1 / ecdsa_results[k][0] + 1 / d);
2406                 else
2407                     ecdsa_results[k][0] = d;
2408
2409                 d = atof(sstrsep(&p, sep));
2410                 if (n)
2411                     ecdsa_results[k][1] =
2412                         1 / (1 / ecdsa_results[k][1] + 1 / d);
2413                 else
2414                     ecdsa_results[k][1] = d;
2415             }
2416 # endif
2417
2418 # ifndef OPENSSL_NO_EC
2419             else if (!strncmp(buf, "+F5:", 4)) {
2420                 int k;
2421                 double d;
2422
2423                 p = buf + 4;
2424                 k = atoi(sstrsep(&p, sep));
2425                 sstrsep(&p, sep);
2426
2427                 d = atof(sstrsep(&p, sep));
2428                 if (n)
2429                     ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
2430                 else
2431                     ecdh_results[k][0] = d;
2432
2433             }
2434 # endif
2435
2436             else if (!strncmp(buf, "+H:", 3)) {
2437                 ;
2438             } else
2439                 fprintf(stderr, "Unknown type '%s' from child %d\n", buf, n);
2440         }
2441
2442         fclose(f);
2443     }
2444     free(fds);
2445     return 1;
2446 }
2447 #endif
2448
2449 static void multiblock_speed(const EVP_CIPHER *evp_cipher)
2450 {
2451     static int mblengths[] =
2452         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
2453     int j, count, num = sizeof(lengths) / sizeof(lengths[0]);
2454     const char *alg_name;
2455     unsigned char *inp, *out, no_key[32], no_iv[16];
2456     EVP_CIPHER_CTX ctx;
2457     double d = 0.0;
2458
2459     inp = OPENSSL_malloc(mblengths[num - 1]);
2460     out = OPENSSL_malloc(mblengths[num - 1] + 1024);
2461     if (!inp || !out) {
2462         BIO_printf(bio_err, "Out of memory\n");
2463         goto end;
2464     }
2465
2466     EVP_CIPHER_CTX_init(&ctx);
2467     EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, no_key, no_iv);
2468     EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
2469                         no_key);
2470     alg_name = OBJ_nid2ln(evp_cipher->nid);
2471
2472     for (j = 0; j < num; j++) {
2473         print_message(alg_name, 0, mblengths[j]);
2474         Time_F(START);
2475         for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
2476             unsigned char aad[13];
2477             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
2478             size_t len = mblengths[j];
2479             int packlen;
2480
2481             memset(aad, 0, 8);  /* avoid uninitialized values */
2482             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
2483             aad[9] = 3;         /* version */
2484             aad[10] = 2;
2485             aad[11] = 0;        /* length */
2486             aad[12] = 0;
2487             mb_param.out = NULL;
2488             mb_param.inp = aad;
2489             mb_param.len = len;
2490             mb_param.interleave = 8;
2491
2492             packlen = EVP_CIPHER_CTX_ctrl(&ctx,
2493                                           EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
2494                                           sizeof(mb_param), &mb_param);
2495
2496             if (packlen > 0) {
2497                 mb_param.out = out;
2498                 mb_param.inp = inp;
2499                 mb_param.len = len;
2500                 EVP_CIPHER_CTX_ctrl(&ctx,
2501                                     EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
2502                                     sizeof(mb_param), &mb_param);
2503             } else {
2504                 int pad;
2505
2506                 RAND_bytes(out, 16);
2507                 len += 16;
2508                 aad[11] = len >> 8;
2509                 aad[12] = len;
2510                 pad = EVP_CIPHER_CTX_ctrl(&ctx,
2511                                           EVP_CTRL_AEAD_TLS1_AAD, 13, aad);
2512                 EVP_Cipher(&ctx, out, inp, len + pad);
2513             }
2514         }
2515         d = Time_F(STOP);
2516         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
2517                    : "%d %s's in %.2fs\n", count, "evp", d);
2518         results[D_EVP][j] = ((double)count) / d * mblengths[j];
2519     }
2520
2521     if (mr) {
2522         fprintf(stdout, "+H");
2523         for (j = 0; j < num; j++)
2524             fprintf(stdout, ":%d", mblengths[j]);
2525         fprintf(stdout, "\n");
2526         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
2527         for (j = 0; j < num; j++)
2528             fprintf(stdout, ":%.2f", results[D_EVP][j]);
2529         fprintf(stdout, "\n");
2530     } else {
2531         fprintf(stdout,
2532                 "The 'numbers' are in 1000s of bytes per second processed.\n");
2533         fprintf(stdout, "type                    ");
2534         for (j = 0; j < num; j++)
2535             fprintf(stdout, "%7d bytes", mblengths[j]);
2536         fprintf(stdout, "\n");
2537         fprintf(stdout, "%-24s", alg_name);
2538
2539         for (j = 0; j < num; j++) {
2540             if (results[D_EVP][j] > 10000)
2541                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
2542             else
2543                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
2544         }
2545         fprintf(stdout, "\n");
2546     }
2547
2548 end:
2549     if (inp)
2550         OPENSSL_free(inp);
2551     if (out)
2552         OPENSSL_free(out);
2553 }