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