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