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