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