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