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