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