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