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