6ba7a2ece723aa0f075650c1ef406e2b2d26e838
[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 # if 1
2032         ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);
2033         if (ret <= 0) {
2034             BIO_printf(bio_err,
2035                        "RSA verify failure.  No RSA verify will be done.\n");
2036             ERR_print_errors(bio_err);
2037             rsa_doit[j] = 0;
2038         } else {
2039             pkey_print_message("public", "rsa",
2040                                rsa_c[j][1], rsa_bits[j], RSA_SECONDS);
2041             Time_F(START);
2042             for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {
2043                 ret = RSA_verify(NID_md5_sha1, buf, 36, buf2,
2044                                  rsa_num, rsa_key[j]);
2045                 if (ret <= 0) {
2046                     BIO_printf(bio_err, "RSA verify failure\n");
2047                     ERR_print_errors(bio_err);
2048                     count = 1;
2049                     break;
2050                 }
2051             }
2052             d = Time_F(STOP);
2053             BIO_printf(bio_err,
2054                        mr ? "+R2:%ld:%d:%.2f\n"
2055                        : "%ld %d bit public RSA's in %.2fs\n",
2056                        count, rsa_bits[j], d);
2057             rsa_results[j][1] = d / (double)count;
2058         }
2059 # endif
2060
2061         if (rsa_count <= 1) {
2062             /* if longer than 10s, don't do any more */
2063             for (j++; j < RSA_NUM; j++)
2064                 rsa_doit[j] = 0;
2065         }
2066     }
2067 #endif
2068
2069     RAND_pseudo_bytes(buf, 20);
2070 #ifndef OPENSSL_NO_DSA
2071     if (RAND_status() != 1) {
2072         RAND_seed(rnd_seed, sizeof rnd_seed);
2073         rnd_fake = 1;
2074     }
2075     for (j = 0; j < DSA_NUM; j++) {
2076         unsigned int kk;
2077         int ret;
2078
2079         if (!dsa_doit[j])
2080             continue;
2081
2082         /* DSA_generate_key(dsa_key[j]); */
2083         /* DSA_sign_setup(dsa_key[j],NULL); */
2084         ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
2085         if (ret == 0) {
2086             BIO_printf(bio_err,
2087                        "DSA sign failure.  No DSA sign will be done.\n");
2088             ERR_print_errors(bio_err);
2089             rsa_count = 1;
2090         } else {
2091             pkey_print_message("sign", "dsa",
2092                                dsa_c[j][0], dsa_bits[j], DSA_SECONDS);
2093             Time_F(START);
2094             for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {
2095                 ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
2096                 if (ret == 0) {
2097                     BIO_printf(bio_err, "DSA sign failure\n");
2098                     ERR_print_errors(bio_err);
2099                     count = 1;
2100                     break;
2101                 }
2102             }
2103             d = Time_F(STOP);
2104             BIO_printf(bio_err,
2105                        mr ? "+R3:%ld:%d:%.2f\n"
2106                        : "%ld %d bit DSA signs in %.2fs\n",
2107                        count, dsa_bits[j], d);
2108             dsa_results[j][0] = d / (double)count;
2109             rsa_count = count;
2110         }
2111
2112         ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
2113         if (ret <= 0) {
2114             BIO_printf(bio_err,
2115                        "DSA verify failure.  No DSA verify will be done.\n");
2116             ERR_print_errors(bio_err);
2117             dsa_doit[j] = 0;
2118         } else {
2119             pkey_print_message("verify", "dsa",
2120                                dsa_c[j][1], dsa_bits[j], DSA_SECONDS);
2121             Time_F(START);
2122             for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {
2123                 ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
2124                 if (ret <= 0) {
2125                     BIO_printf(bio_err, "DSA verify failure\n");
2126                     ERR_print_errors(bio_err);
2127                     count = 1;
2128                     break;
2129                 }
2130             }
2131             d = Time_F(STOP);
2132             BIO_printf(bio_err,
2133                        mr ? "+R4:%ld:%d:%.2f\n"
2134                        : "%ld %d bit DSA verify in %.2fs\n",
2135                        count, dsa_bits[j], d);
2136             dsa_results[j][1] = d / (double)count;
2137         }
2138
2139         if (rsa_count <= 1) {
2140             /* if longer than 10s, don't do any more */
2141             for (j++; j < DSA_NUM; j++)
2142                 dsa_doit[j] = 0;
2143         }
2144     }
2145     if (rnd_fake)
2146         RAND_cleanup();
2147 #endif
2148
2149 #ifndef OPENSSL_NO_ECDSA
2150     if (RAND_status() != 1) {
2151         RAND_seed(rnd_seed, sizeof rnd_seed);
2152         rnd_fake = 1;
2153     }
2154     for (j = 0; j < EC_NUM; j++) {
2155         int ret;
2156
2157         if (!ecdsa_doit[j])
2158             continue;           /* Ignore Curve */
2159         ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);
2160         if (ecdsa[j] == NULL) {
2161             BIO_printf(bio_err, "ECDSA failure.\n");
2162             ERR_print_errors(bio_err);
2163             rsa_count = 1;
2164         } else {
2165 # if 1
2166             EC_KEY_precompute_mult(ecdsa[j], NULL);
2167 # endif
2168             /* Perform ECDSA signature test */
2169             EC_KEY_generate_key(ecdsa[j]);
2170             ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);
2171             if (ret == 0) {
2172                 BIO_printf(bio_err,
2173                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
2174                 ERR_print_errors(bio_err);
2175                 rsa_count = 1;
2176             } else {
2177                 pkey_print_message("sign", "ecdsa",
2178                                    ecdsa_c[j][0],
2179                                    test_curves_bits[j], ECDSA_SECONDS);
2180
2181                 Time_F(START);
2182                 for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {
2183                     ret = ECDSA_sign(0, buf, 20,
2184                                      ecdsasig, &ecdsasiglen, ecdsa[j]);
2185                     if (ret == 0) {
2186                         BIO_printf(bio_err, "ECDSA sign failure\n");
2187                         ERR_print_errors(bio_err);
2188                         count = 1;
2189                         break;
2190                     }
2191                 }
2192                 d = Time_F(STOP);
2193
2194                 BIO_printf(bio_err,
2195                            mr ? "+R5:%ld:%d:%.2f\n" :
2196                            "%ld %d bit ECDSA signs in %.2fs \n",
2197                            count, test_curves_bits[j], d);
2198                 ecdsa_results[j][0] = d / (double)count;
2199                 rsa_count = count;
2200             }
2201
2202             /* Perform ECDSA verification test */
2203             ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
2204             if (ret != 1) {
2205                 BIO_printf(bio_err,
2206                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
2207                 ERR_print_errors(bio_err);
2208                 ecdsa_doit[j] = 0;
2209             } else {
2210                 pkey_print_message("verify", "ecdsa",
2211                                    ecdsa_c[j][1],
2212                                    test_curves_bits[j], ECDSA_SECONDS);
2213                 Time_F(START);
2214                 for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {
2215                     ret =
2216                         ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,
2217                                      ecdsa[j]);
2218                     if (ret != 1) {
2219                         BIO_printf(bio_err, "ECDSA verify failure\n");
2220                         ERR_print_errors(bio_err);
2221                         count = 1;
2222                         break;
2223                     }
2224                 }
2225                 d = Time_F(STOP);
2226                 BIO_printf(bio_err,
2227                            mr ? "+R6:%ld:%d:%.2f\n"
2228                            : "%ld %d bit ECDSA verify in %.2fs\n",
2229                            count, test_curves_bits[j], d);
2230                 ecdsa_results[j][1] = d / (double)count;
2231             }
2232
2233             if (rsa_count <= 1) {
2234                 /* if longer than 10s, don't do any more */
2235                 for (j++; j < EC_NUM; j++)
2236                     ecdsa_doit[j] = 0;
2237             }
2238         }
2239     }
2240     if (rnd_fake)
2241         RAND_cleanup();
2242 #endif
2243
2244 #ifndef OPENSSL_NO_ECDH
2245     if (RAND_status() != 1) {
2246         RAND_seed(rnd_seed, sizeof rnd_seed);
2247         rnd_fake = 1;
2248     }
2249     for (j = 0; j < EC_NUM; j++) {
2250         if (!ecdh_doit[j])
2251             continue;
2252         ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);
2253         ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);
2254         if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {
2255             BIO_printf(bio_err, "ECDH failure.\n");
2256             ERR_print_errors(bio_err);
2257             rsa_count = 1;
2258         } else {
2259             /* generate two ECDH key pairs */
2260             if (!EC_KEY_generate_key(ecdh_a[j]) ||
2261                 !EC_KEY_generate_key(ecdh_b[j])) {
2262                 BIO_printf(bio_err, "ECDH key generation failure.\n");
2263                 ERR_print_errors(bio_err);
2264                 rsa_count = 1;
2265             } else {
2266                 /*
2267                  * If field size is not more than 24 octets, then use SHA-1
2268                  * hash of result; otherwise, use result (see section 4.8 of
2269                  * draft-ietf-tls-ecc-03.txt).
2270                  */
2271                 int field_size, outlen;
2272                 void *(*kdf) (const void *in, size_t inlen, void *out,
2273                               size_t *xoutlen);
2274                 field_size =
2275                     EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));
2276                 if (field_size <= 24 * 8) {
2277                     outlen = KDF1_SHA1_len;
2278                     kdf = KDF1_SHA1;
2279                 } else {
2280                     outlen = (field_size + 7) / 8;
2281                     kdf = NULL;
2282                 }
2283                 secret_size_a =
2284                     ECDH_compute_key(secret_a, outlen,
2285                                      EC_KEY_get0_public_key(ecdh_b[j]),
2286                                      ecdh_a[j], kdf);
2287                 secret_size_b =
2288                     ECDH_compute_key(secret_b, outlen,
2289                                      EC_KEY_get0_public_key(ecdh_a[j]),
2290                                      ecdh_b[j], kdf);
2291                 if (secret_size_a != secret_size_b)
2292                     ecdh_checks = 0;
2293                 else
2294                     ecdh_checks = 1;
2295
2296                 for (secret_idx = 0; (secret_idx < secret_size_a)
2297                      && (ecdh_checks == 1); secret_idx++) {
2298                     if (secret_a[secret_idx] != secret_b[secret_idx])
2299                         ecdh_checks = 0;
2300                 }
2301
2302                 if (ecdh_checks == 0) {
2303                     BIO_printf(bio_err, "ECDH computations don't match.\n");
2304                     ERR_print_errors(bio_err);
2305                     rsa_count = 1;
2306                 }
2307
2308                 pkey_print_message("", "ecdh",
2309                                    ecdh_c[j][0],
2310                                    test_curves_bits[j], ECDH_SECONDS);
2311                 Time_F(START);
2312                 for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {
2313                     ECDH_compute_key(secret_a, outlen,
2314                                      EC_KEY_get0_public_key(ecdh_b[j]),
2315                                      ecdh_a[j], kdf);
2316                 }
2317                 d = Time_F(STOP);
2318                 BIO_printf(bio_err,
2319                            mr ? "+R7:%ld:%d:%.2f\n" :
2320                            "%ld %d-bit ECDH ops in %.2fs\n", count,
2321                            test_curves_bits[j], d);
2322                 ecdh_results[j][0] = d / (double)count;
2323                 rsa_count = count;
2324             }
2325         }
2326
2327         if (rsa_count <= 1) {
2328             /* if longer than 10s, don't do any more */
2329             for (j++; j < EC_NUM; j++)
2330                 ecdh_doit[j] = 0;
2331         }
2332     }
2333     if (rnd_fake)
2334         RAND_cleanup();
2335 #endif
2336 #ifndef NO_FORK
2337  show_res:
2338 #endif
2339     if (!mr) {
2340         fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_VERSION));
2341         fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_BUILT_ON));
2342         printf("options:");
2343         printf("%s ", BN_options());
2344 #ifndef OPENSSL_NO_MD2
2345         printf("%s ", MD2_options());
2346 #endif
2347 #ifndef OPENSSL_NO_RC4
2348         printf("%s ", RC4_options());
2349 #endif
2350 #ifndef OPENSSL_NO_DES
2351         printf("%s ", DES_options());
2352 #endif
2353 #ifndef OPENSSL_NO_AES
2354         printf("%s ", AES_options());
2355 #endif
2356 #ifndef OPENSSL_NO_IDEA
2357         printf("%s ", idea_options());
2358 #endif
2359 #ifndef OPENSSL_NO_BF
2360         printf("%s ", BF_options());
2361 #endif
2362         fprintf(stdout, "\n%s\n", SSLeay_version(SSLEAY_CFLAGS));
2363     }
2364
2365     if (pr_header) {
2366         if (mr)
2367             fprintf(stdout, "+H");
2368         else {
2369             fprintf(stdout,
2370                     "The 'numbers' are in 1000s of bytes per second processed.\n");
2371             fprintf(stdout, "type        ");
2372         }
2373         for (j = 0; j < SIZE_NUM; j++)
2374             fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]);
2375         fprintf(stdout, "\n");
2376     }
2377
2378     for (k = 0; k < ALGOR_NUM; k++) {
2379         if (!doit[k])
2380             continue;
2381         if (mr)
2382             fprintf(stdout, "+F:%d:%s", k, names[k]);
2383         else
2384             fprintf(stdout, "%-13s", names[k]);
2385         for (j = 0; j < SIZE_NUM; j++) {
2386             if (results[k][j] > 10000 && !mr)
2387                 fprintf(stdout, " %11.2fk", results[k][j] / 1e3);
2388             else
2389                 fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]);
2390         }
2391         fprintf(stdout, "\n");
2392     }
2393 #ifndef OPENSSL_NO_RSA
2394     j = 1;
2395     for (k = 0; k < RSA_NUM; k++) {
2396         if (!rsa_doit[k])
2397             continue;
2398         if (j && !mr) {
2399             printf("%18ssign    verify    sign/s verify/s\n", " ");
2400             j = 0;
2401         }
2402         if (mr)
2403             fprintf(stdout, "+F2:%u:%u:%f:%f\n",
2404                     k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2405         else
2406             fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2407                     rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2408                     1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
2409     }
2410 #endif
2411 #ifndef OPENSSL_NO_DSA
2412     j = 1;
2413     for (k = 0; k < DSA_NUM; k++) {
2414         if (!dsa_doit[k])
2415             continue;
2416         if (j && !mr) {
2417             printf("%18ssign    verify    sign/s verify/s\n", " ");
2418             j = 0;
2419         }
2420         if (mr)
2421             fprintf(stdout, "+F3:%u:%u:%f:%f\n",
2422                     k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2423         else
2424             fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2425                     dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2426                     1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
2427     }
2428 #endif
2429 #ifndef OPENSSL_NO_ECDSA
2430     j = 1;
2431     for (k = 0; k < EC_NUM; k++) {
2432         if (!ecdsa_doit[k])
2433             continue;
2434         if (j && !mr) {
2435             printf("%30ssign    verify    sign/s verify/s\n", " ");
2436             j = 0;
2437         }
2438
2439         if (mr)
2440             fprintf(stdout, "+F4:%u:%u:%f:%f\n",
2441                     k, test_curves_bits[k],
2442                     ecdsa_results[k][0], ecdsa_results[k][1]);
2443         else
2444             fprintf(stdout,
2445                     "%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2446                     test_curves_bits[k],
2447                     test_curves_names[k],
2448                     ecdsa_results[k][0], ecdsa_results[k][1],
2449                     1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
2450     }
2451 #endif
2452
2453 #ifndef OPENSSL_NO_ECDH
2454     j = 1;
2455     for (k = 0; k < EC_NUM; k++) {
2456         if (!ecdh_doit[k])
2457             continue;
2458         if (j && !mr) {
2459             printf("%30sop      op/s\n", " ");
2460             j = 0;
2461         }
2462         if (mr)
2463             fprintf(stdout, "+F5:%u:%u:%f:%f\n",
2464                     k, test_curves_bits[k],
2465                     ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2466
2467         else
2468             fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\n",
2469                     test_curves_bits[k],
2470                     test_curves_names[k],
2471                     ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2472     }
2473 #endif
2474
2475     mret = 0;
2476
2477  end:
2478     ERR_print_errors(bio_err);
2479     if (buf_malloc != NULL)
2480         OPENSSL_free(buf_malloc);
2481     if (buf2_malloc != NULL)
2482         OPENSSL_free(buf2_malloc);
2483 #ifndef OPENSSL_NO_RSA
2484     for (i = 0; i < RSA_NUM; i++)
2485         if (rsa_key[i] != NULL)
2486             RSA_free(rsa_key[i]);
2487 #endif
2488 #ifndef OPENSSL_NO_DSA
2489     for (i = 0; i < DSA_NUM; i++)
2490         if (dsa_key[i] != NULL)
2491             DSA_free(dsa_key[i]);
2492 #endif
2493
2494 #ifndef OPENSSL_NO_ECDSA
2495     for (i = 0; i < EC_NUM; i++)
2496         if (ecdsa[i] != NULL)
2497             EC_KEY_free(ecdsa[i]);
2498 #endif
2499 #ifndef OPENSSL_NO_ECDH
2500     for (i = 0; i < EC_NUM; i++) {
2501         if (ecdh_a[i] != NULL)
2502             EC_KEY_free(ecdh_a[i]);
2503         if (ecdh_b[i] != NULL)
2504             EC_KEY_free(ecdh_b[i]);
2505     }
2506 #endif
2507
2508     apps_shutdown();
2509     OPENSSL_EXIT(mret);
2510 }
2511
2512 static void print_message(const char *s, long num, int length)
2513 {
2514 #ifdef SIGALRM
2515     BIO_printf(bio_err,
2516                mr ? "+DT:%s:%d:%d\n"
2517                : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2518     (void)BIO_flush(bio_err);
2519     alarm(SECONDS);
2520 #else
2521     BIO_printf(bio_err,
2522                mr ? "+DN:%s:%ld:%d\n"
2523                : "Doing %s %ld times on %d size blocks: ", s, num, length);
2524     (void)BIO_flush(bio_err);
2525 #endif
2526 }
2527
2528 static void pkey_print_message(const char *str, const char *str2, long num,
2529                                int bits, int tm)
2530 {
2531 #ifdef SIGALRM
2532     BIO_printf(bio_err,
2533                mr ? "+DTP:%d:%s:%s:%d\n"
2534                : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2535     (void)BIO_flush(bio_err);
2536     alarm(tm);
2537 #else
2538     BIO_printf(bio_err,
2539                mr ? "+DNP:%ld:%d:%s:%s\n"
2540                : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2541     (void)BIO_flush(bio_err);
2542 #endif
2543 }
2544
2545 static void print_result(int alg, int run_no, int count, double time_used)
2546 {
2547     BIO_printf(bio_err,
2548                mr ? "+R:%d:%s:%f\n"
2549                : "%d %s's in %.2fs\n", count, names[alg], time_used);
2550     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2551 }
2552
2553 #ifndef NO_FORK
2554 static char *sstrsep(char **string, const char *delim)
2555 {
2556     char isdelim[256];
2557     char *token = *string;
2558
2559     if (**string == 0)
2560         return NULL;
2561
2562     memset(isdelim, 0, sizeof isdelim);
2563     isdelim[0] = 1;
2564
2565     while (*delim) {
2566         isdelim[(unsigned char)(*delim)] = 1;
2567         delim++;
2568     }
2569
2570     while (!isdelim[(unsigned char)(**string)]) {
2571         (*string)++;
2572     }
2573
2574     if (**string) {
2575         **string = 0;
2576         (*string)++;
2577     }
2578
2579     return token;
2580 }
2581
2582 static int do_multi(int multi)
2583 {
2584     int n;
2585     int fd[2];
2586     int *fds;
2587     static char sep[] = ":";
2588
2589     fds = malloc(multi * sizeof *fds);
2590     for (n = 0; n < multi; ++n) {
2591         if (pipe(fd) == -1) {
2592             fprintf(stderr, "pipe failure\n");
2593             exit(1);
2594         }
2595         fflush(stdout);
2596         fflush(stderr);
2597         if (fork()) {
2598             close(fd[1]);
2599             fds[n] = fd[0];
2600         } else {
2601             close(fd[0]);
2602             close(1);
2603             if (dup(fd[1]) == -1) {
2604                 fprintf(stderr, "dup failed\n");
2605                 exit(1);
2606             }
2607             close(fd[1]);
2608             mr = 1;
2609             usertime = 0;
2610             free(fds);
2611             return 0;
2612         }
2613         printf("Forked child %d\n", n);
2614     }
2615
2616     /* for now, assume the pipe is long enough to take all the output */
2617     for (n = 0; n < multi; ++n) {
2618         FILE *f;
2619         char buf[1024];
2620         char *p;
2621
2622         f = fdopen(fds[n], "r");
2623         while (fgets(buf, sizeof buf, f)) {
2624             p = strchr(buf, '\n');
2625             if (p)
2626                 *p = '\0';
2627             if (buf[0] != '+') {
2628                 fprintf(stderr, "Don't understand line '%s' from child %d\n",
2629                         buf, n);
2630                 continue;
2631             }
2632             printf("Got: %s from %d\n", buf, n);
2633             if (!strncmp(buf, "+F:", 3)) {
2634                 int alg;
2635                 int j;
2636
2637                 p = buf + 3;
2638                 alg = atoi(sstrsep(&p, sep));
2639                 sstrsep(&p, sep);
2640                 for (j = 0; j < SIZE_NUM; ++j)
2641                     results[alg][j] += atof(sstrsep(&p, sep));
2642             } else if (!strncmp(buf, "+F2:", 4)) {
2643                 int k;
2644                 double d;
2645
2646                 p = buf + 4;
2647                 k = atoi(sstrsep(&p, sep));
2648                 sstrsep(&p, sep);
2649
2650                 d = atof(sstrsep(&p, sep));
2651                 if (n)
2652                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
2653                 else
2654                     rsa_results[k][0] = d;
2655
2656                 d = atof(sstrsep(&p, sep));
2657                 if (n)
2658                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
2659                 else
2660                     rsa_results[k][1] = d;
2661             } else if (!strncmp(buf, "+F2:", 4)) {
2662                 int k;
2663                 double d;
2664
2665                 p = buf + 4;
2666                 k = atoi(sstrsep(&p, sep));
2667                 sstrsep(&p, sep);
2668
2669                 d = atof(sstrsep(&p, sep));
2670                 if (n)
2671                     rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
2672                 else
2673                     rsa_results[k][0] = d;
2674
2675                 d = atof(sstrsep(&p, sep));
2676                 if (n)
2677                     rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
2678                 else
2679                     rsa_results[k][1] = d;
2680             }
2681 # ifndef OPENSSL_NO_DSA
2682             else if (!strncmp(buf, "+F3:", 4)) {
2683                 int k;
2684                 double d;
2685
2686                 p = buf + 4;
2687                 k = atoi(sstrsep(&p, sep));
2688                 sstrsep(&p, sep);
2689
2690                 d = atof(sstrsep(&p, sep));
2691                 if (n)
2692                     dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
2693                 else
2694                     dsa_results[k][0] = d;
2695
2696                 d = atof(sstrsep(&p, sep));
2697                 if (n)
2698                     dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
2699                 else
2700                     dsa_results[k][1] = d;
2701             }
2702 # endif
2703 # ifndef OPENSSL_NO_ECDSA
2704             else if (!strncmp(buf, "+F4:", 4)) {
2705                 int k;
2706                 double d;
2707
2708                 p = buf + 4;
2709                 k = atoi(sstrsep(&p, sep));
2710                 sstrsep(&p, sep);
2711
2712                 d = atof(sstrsep(&p, sep));
2713                 if (n)
2714                     ecdsa_results[k][0] =
2715                         1 / (1 / ecdsa_results[k][0] + 1 / d);
2716                 else
2717                     ecdsa_results[k][0] = d;
2718
2719                 d = atof(sstrsep(&p, sep));
2720                 if (n)
2721                     ecdsa_results[k][1] =
2722                         1 / (1 / ecdsa_results[k][1] + 1 / d);
2723                 else
2724                     ecdsa_results[k][1] = d;
2725             }
2726 # endif
2727
2728 # ifndef OPENSSL_NO_ECDH
2729             else if (!strncmp(buf, "+F5:", 4)) {
2730                 int k;
2731                 double d;
2732
2733                 p = buf + 4;
2734                 k = atoi(sstrsep(&p, sep));
2735                 sstrsep(&p, sep);
2736
2737                 d = atof(sstrsep(&p, sep));
2738                 if (n)
2739                     ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
2740                 else
2741                     ecdh_results[k][0] = d;
2742
2743             }
2744 # endif
2745
2746             else if (!strncmp(buf, "+H:", 3)) {
2747             } else
2748                 fprintf(stderr, "Unknown type '%s' from child %d\n", buf, n);
2749         }
2750
2751         fclose(f);
2752     }
2753     free(fds);
2754     return 1;
2755 }
2756 #endif
2757
2758 static void multiblock_speed(const EVP_CIPHER *evp_cipher)
2759 {
2760     static int mblengths[] =
2761         { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
2762     int j, count, num = sizeof(lengths) / sizeof(lengths[0]);
2763     const char *alg_name;
2764     unsigned char *inp, *out, no_key[32], no_iv[16];
2765     EVP_CIPHER_CTX ctx;
2766     double d = 0.0;
2767
2768     inp = OPENSSL_malloc(mblengths[num - 1]);
2769     out = OPENSSL_malloc(mblengths[num - 1] + 1024);
2770
2771     EVP_CIPHER_CTX_init(&ctx);
2772     EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, no_key, no_iv);
2773     EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
2774                         no_key);
2775     alg_name = OBJ_nid2ln(evp_cipher->nid);
2776
2777     for (j = 0; j < num; j++) {
2778         print_message(alg_name, 0, mblengths[j]);
2779         Time_F(START);
2780         for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
2781             unsigned char aad[13];
2782             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
2783             size_t len = mblengths[j];
2784             int packlen;
2785
2786             memset(aad, 0, 8);  /* avoid uninitialized values */
2787             aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
2788             aad[9] = 3;         /* version */
2789             aad[10] = 2;
2790             aad[11] = 0;        /* length */
2791             aad[12] = 0;
2792             mb_param.out = NULL;
2793             mb_param.inp = aad;
2794             mb_param.len = len;
2795             mb_param.interleave = 8;
2796
2797             packlen = EVP_CIPHER_CTX_ctrl(&ctx,
2798                                           EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
2799                                           sizeof(mb_param), &mb_param);
2800
2801             if (packlen > 0) {
2802                 mb_param.out = out;
2803                 mb_param.inp = inp;
2804                 mb_param.len = len;
2805                 EVP_CIPHER_CTX_ctrl(&ctx,
2806                                     EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
2807                                     sizeof(mb_param), &mb_param);
2808             } else {
2809                 int pad;
2810
2811                 RAND_bytes(out, 16);
2812                 len += 16;
2813                 aad[11] = len >> 8;
2814                 aad[12] = len;
2815                 pad = EVP_CIPHER_CTX_ctrl(&ctx,
2816                                           EVP_CTRL_AEAD_TLS1_AAD, 13, aad);
2817                 EVP_Cipher(&ctx, out, inp, len + pad);
2818             }
2819         }
2820         d = Time_F(STOP);
2821         BIO_printf(bio_err,
2822                    mr ? "+R:%d:%s:%f\n"
2823                    : "%d %s's in %.2fs\n", count, "evp", d);
2824         results[D_EVP][j] = ((double)count) / d * mblengths[j];
2825     }
2826
2827     if (mr) {
2828         fprintf(stdout, "+H");
2829         for (j = 0; j < num; j++)
2830             fprintf(stdout, ":%d", mblengths[j]);
2831         fprintf(stdout, "\n");
2832         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
2833         for (j = 0; j < num; j++)
2834             fprintf(stdout, ":%.2f", results[D_EVP][j]);
2835         fprintf(stdout, "\n");
2836     } else {
2837         fprintf(stdout,
2838                 "The 'numbers' are in 1000s of bytes per second processed.\n");
2839         fprintf(stdout, "type                    ");
2840         for (j = 0; j < num; j++)
2841             fprintf(stdout, "%7d bytes", mblengths[j]);
2842         fprintf(stdout, "\n");
2843         fprintf(stdout, "%-24s", alg_name);
2844
2845         for (j = 0; j < num; j++) {
2846             if (results[D_EVP][j] > 10000)
2847                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
2848             else
2849                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
2850         }
2851         fprintf(stdout, "\n");
2852     }
2853
2854     OPENSSL_free(inp);
2855     OPENSSL_free(out);
2856 }