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