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