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