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