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