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