DOS and Windows do not like unistd.h
[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 /* most of this code has been pilfered from my libdes speed.c program */
60
61 #undef SECONDS
62 #define SECONDS         3       
63 #define RSA_SECONDS     10
64 #define DSA_SECONDS     10
65
66 /* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
67 /* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
68
69 #undef PROG
70 #define PROG speed_main
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <signal.h>
75 #include <string.h>
76 #include <math.h>
77 #include "apps.h"
78 #ifdef OPENSSL_NO_STDIO
79 #define APPS_WIN16
80 #endif
81 #include <openssl/crypto.h>
82 #include <openssl/rand.h>
83 #include <openssl/err.h>
84 #include <openssl/evp.h>
85 #include <openssl/objects.h>
86 #if !defined(OPENSSL_SYS_MSDOS)
87 #include OPENSSL_UNISTD
88 #endif
89
90 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(OPENSSL_SYS_MACOSX)
91 # define USE_TOD
92 #elif !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC))
93 # define TIMES
94 #endif
95 #if !defined(_UNICOS) && !defined(__OpenBSD__) && !defined(sgi) && !defined(__FreeBSD__) && !(defined(__bsdi) || defined(__bsdi__)) && !defined(_AIX) && !defined(OPENSSL_SYS_MPE) && !defined(__NetBSD__) /* FIXME */
96 # define TIMEB
97 #endif
98
99 #ifndef _IRIX
100 # include <time.h>
101 #endif
102 #ifdef TIMES
103 # include <sys/types.h>
104 # include <sys/times.h>
105 #endif
106 #ifdef USE_TOD
107 # include <sys/time.h>
108 # include <sys/resource.h>
109 #endif
110
111 /* Depending on the VMS version, the tms structure is perhaps defined.
112    The __TMS macro will show if it was.  If it wasn't defined, we should
113    undefine TIMES, since that tells the rest of the program how things
114    should be handled.                           -- Richard Levitte */
115 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
116 #undef TIMES
117 #endif
118
119 #ifdef TIMEB
120 #include <sys/timeb.h>
121 #endif
122
123 #if !defined(TIMES) && !defined(TIMEB) && !defined(USE_TOD)
124 #error "It seems neither struct tms nor struct timeb is supported in this platform!"
125 #endif
126
127 #if defined(sun) || defined(__ultrix)
128 #define _POSIX_SOURCE
129 #include <limits.h>
130 #include <sys/param.h>
131 #endif
132
133 #ifndef OPENSSL_NO_DES
134 #include <openssl/des_old.h>
135 #endif
136 #ifndef OPENSSL_NO_MD2
137 #include <openssl/md2.h>
138 #endif
139 #ifndef OPENSSL_NO_MDC2
140 #include <openssl/mdc2.h>
141 #endif
142 #ifndef OPENSSL_NO_MD4
143 #include <openssl/md4.h>
144 #endif
145 #ifndef OPENSSL_NO_MD5
146 #include <openssl/md5.h>
147 #endif
148 #ifndef OPENSSL_NO_HMAC
149 #include <openssl/hmac.h>
150 #endif
151 #include <openssl/evp.h>
152 #ifndef OPENSSL_NO_SHA
153 #include <openssl/sha.h>
154 #endif
155 #ifndef OPENSSL_NO_RIPEMD
156 #include <openssl/ripemd.h>
157 #endif
158 #ifndef OPENSSL_NO_RC4
159 #include <openssl/rc4.h>
160 #endif
161 #ifndef OPENSSL_NO_RC5
162 #include <openssl/rc5.h>
163 #endif
164 #ifndef OPENSSL_NO_RC2
165 #include <openssl/rc2.h>
166 #endif
167 #ifndef OPENSSL_NO_IDEA
168 #include <openssl/idea.h>
169 #endif
170 #ifndef OPENSSL_NO_BF
171 #include <openssl/blowfish.h>
172 #endif
173 #ifndef OPENSSL_NO_CAST
174 #include <openssl/cast.h>
175 #endif
176 #ifndef OPENSSL_NO_RSA
177 #include <openssl/rsa.h>
178 #include "./testrsa.h"
179 #endif
180 #include <openssl/x509.h>
181 #ifndef OPENSSL_NO_DSA
182 #include "./testdsa.h"
183 #endif
184
185 /* The following if from times(3) man page.  It may need to be changed */
186 #ifndef HZ
187 # ifdef _SC_CLK_TCK
188 #  define HZ ((double)sysconf(_SC_CLK_TCK))
189 # else
190 #  ifndef CLK_TCK
191 #   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
192 #    define HZ  100.0
193 #   else /* _BSD_CLK_TCK_ */
194 #    define HZ ((double)_BSD_CLK_TCK_)
195 #   endif
196 #  else /* CLK_TCK */
197 #   define HZ ((double)CLK_TCK)
198 #  endif
199 # endif
200 #endif
201
202 #if !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_OS2)
203 # define HAVE_FORK 1
204 #endif
205
206 #undef BUFSIZE
207 #define BUFSIZE ((long)1024*8+1)
208 int run=0;
209
210 static char ftime_used = 0, times_used = 0, gettimeofday_used = 0, getrusage_used = 0;
211 static int mr=0;
212 static int usertime=1;
213
214 static double Time_F(int s);
215 static void print_message(const char *s,long num,int length);
216 static void pkey_print_message(char *str,char *str2,long num,int bits,int sec);
217 static void print_result(int alg,int run_no,int count,double time_used);
218 #ifdef HAVE_FORK
219 static int do_multi(int multi);
220 #endif
221 #ifdef SIGALRM
222 #if defined(__STDC__) || defined(sgi) || defined(_AIX)
223 #define SIGRETTYPE void
224 #else
225 #define SIGRETTYPE int
226 #endif 
227
228 #define ALGOR_NUM       16
229 #define SIZE_NUM        5
230 #define RSA_NUM         4
231 #define DSA_NUM         3
232 static const char *names[ALGOR_NUM]={
233   "md2","mdc2","md4","md5","hmac(md5)","sha1","rmd160","rc4",
234   "des cbc","des ede3","idea cbc",
235   "rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc"};
236 static double results[ALGOR_NUM][SIZE_NUM];
237 static int lengths[SIZE_NUM]={8,64,256,1024,8*1024};
238 static double rsa_results[RSA_NUM][2];
239 static double dsa_results[DSA_NUM][2];
240
241 static SIGRETTYPE sig_done(int sig);
242 static SIGRETTYPE sig_done(int sig)
243         {
244         signal(SIGALRM,sig_done);
245         run=0;
246 #ifdef LINT
247         sig=sig;
248 #endif
249         }
250 #endif
251
252 #define START   0
253 #define STOP    1
254
255 static double Time_F(int s)
256         {
257         double ret;
258
259 #ifdef USE_TOD
260         if(usertime)
261             {
262                 static struct rusage tstart,tend;
263
264                 getrusage_used = 1;
265                 if (s == START)
266                         {
267                         getrusage(RUSAGE_SELF,&tstart);
268                         return(0);
269                         }
270                 else
271                         {
272                         long i;
273
274                         getrusage(RUSAGE_SELF,&tend);
275                         i=(long)tend.ru_utime.tv_usec-(long)tstart.ru_utime.tv_usec;
276                         ret=((double)(tend.ru_utime.tv_sec-tstart.ru_utime.tv_sec))
277                           +((double)i)/1000000.0;
278                         return((ret < 0.001)?0.001:ret);
279                         }
280                 }
281         else
282                 {
283                 static struct timeval tstart,tend;
284                 long i;
285
286                 gettimeofday_used = 1;
287                 if (s == START)
288                         {
289                         gettimeofday(&tstart,NULL);
290                         return(0);
291                         }
292                 else
293                         {
294                         gettimeofday(&tend,NULL);
295                         i=(long)tend.tv_usec-(long)tstart.tv_usec;
296                         ret=((double)(tend.tv_sec-tstart.tv_sec))+((double)i)/1000000.0;
297                         return((ret < 0.001)?0.001:ret);
298                         }
299                 }
300 #else  /* ndef USE_TOD */
301                 
302 # ifdef TIMES
303         if (usertime)
304                 {
305                 static struct tms tstart,tend;
306
307                 times_used = 1;
308                 if (s == START)
309                         {
310                         times(&tstart);
311                         return(0);
312                         }
313                 else
314                         {
315                         times(&tend);
316                         ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
317                         return((ret < 1e-3)?1e-3:ret);
318                         }
319                 }
320 # endif /* times() */
321 # if defined(TIMES) && defined(TIMEB)
322         else
323 # endif
324 # ifdef TIMEB
325                 {
326                 static struct timeb tstart,tend;
327                 long i;
328
329                 ftime_used = 1;
330                 if (s == START)
331                         {
332                         ftime(&tstart);
333                         return(0);
334                         }
335                 else
336                         {
337                         ftime(&tend);
338                         i=(long)tend.millitm-(long)tstart.millitm;
339                         ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
340                         return((ret < 0.001)?0.001:ret);
341                         }
342                 }
343 # endif
344 #endif
345         }
346
347 int MAIN(int, char **);
348
349 int MAIN(int argc, char **argv)
350         {
351         ENGINE *e;
352         unsigned char *buf=NULL,*buf2=NULL;
353         int mret=1;
354         long count,rsa_count,save_count=0;
355         int i,j,k;
356 #ifndef OPENSSL_NO_RSA
357         unsigned rsa_num;
358 #endif
359 #ifndef OPENSSL_NO_MD2
360         unsigned char md2[MD2_DIGEST_LENGTH];
361 #endif
362 #ifndef OPENSSL_NO_MDC2
363         unsigned char mdc2[MDC2_DIGEST_LENGTH];
364 #endif
365 #ifndef OPENSSL_NO_MD4
366         unsigned char md4[MD4_DIGEST_LENGTH];
367 #endif
368 #ifndef OPENSSL_NO_MD5
369         unsigned char md5[MD5_DIGEST_LENGTH];
370         unsigned char hmac[MD5_DIGEST_LENGTH];
371 #endif
372 #ifndef OPENSSL_NO_SHA
373         unsigned char sha[SHA_DIGEST_LENGTH];
374 #endif
375 #ifndef OPENSSL_NO_RIPEMD
376         unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
377 #endif
378 #ifndef OPENSSL_NO_RC4
379         RC4_KEY rc4_ks;
380 #endif
381 #ifndef OPENSSL_NO_RC5
382         RC5_32_KEY rc5_ks;
383 #endif
384 #ifndef OPENSSL_NO_RC2
385         RC2_KEY rc2_ks;
386 #endif
387 #ifndef OPENSSL_NO_IDEA
388         IDEA_KEY_SCHEDULE idea_ks;
389 #endif
390 #ifndef OPENSSL_NO_BF
391         BF_KEY bf_ks;
392 #endif
393 #ifndef OPENSSL_NO_CAST
394         CAST_KEY cast_ks;
395 #endif
396         static unsigned char key16[16]=
397                 {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
398                  0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
399         unsigned char iv[8];
400 #ifndef OPENSSL_NO_DES
401         DES_cblock *buf_as_des_cblock = NULL;
402         static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
403         static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
404         static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
405         DES_key_schedule sch;
406         DES_key_schedule sch2;
407         DES_key_schedule sch3;
408 #endif
409 #define D_MD2           0
410 #define D_MDC2          1
411 #define D_MD4           2
412 #define D_MD5           3
413 #define D_HMAC          4
414 #define D_SHA1          5
415 #define D_RMD160        6
416 #define D_RC4           7
417 #define D_CBC_DES       8
418 #define D_EDE3_DES      9
419 #define D_CBC_IDEA      10
420 #define D_CBC_RC2       11
421 #define D_CBC_RC5       12
422 #define D_CBC_BF        13
423 #define D_CBC_CAST      14
424 #define D_EVP           15
425         double d;
426         long c[ALGOR_NUM][SIZE_NUM];
427 #define R_DSA_512       0
428 #define R_DSA_1024      1
429 #define R_DSA_2048      2
430 #define R_RSA_512       0
431 #define R_RSA_1024      1
432 #define R_RSA_2048      2
433 #define R_RSA_4096      3
434 #ifndef OPENSSL_NO_RSA
435         RSA *rsa_key[RSA_NUM];
436         long rsa_c[RSA_NUM][2];
437         static unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096};
438         static unsigned char *rsa_data[RSA_NUM]=
439                 {test512,test1024,test2048,test4096};
440         static int rsa_data_length[RSA_NUM]={
441                 sizeof(test512),sizeof(test1024),
442                 sizeof(test2048),sizeof(test4096)};
443 #endif
444 #ifndef OPENSSL_NO_DSA
445         DSA *dsa_key[DSA_NUM];
446         long dsa_c[DSA_NUM][2];
447         static unsigned int dsa_bits[DSA_NUM]={512,1024,2048};
448 #endif
449         int rsa_doit[RSA_NUM];
450         int dsa_doit[DSA_NUM];
451         int doit[ALGOR_NUM];
452         int pr_header=0;
453         const EVP_CIPHER *evp=NULL;
454         int decrypt=0;
455 #ifdef HAVE_FORK
456         int multi=0;
457 #endif
458
459 #ifndef TIMES
460         usertime=-1;
461 #endif
462
463         apps_startup();
464         memset(results, 0, sizeof(results));
465 #ifndef OPENSSL_NO_DSA
466         memset(dsa_key,0,sizeof(dsa_key));
467 #endif
468
469         if (bio_err == NULL)
470                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
471                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
472
473 #ifndef OPENSSL_NO_RSA
474         memset(rsa_key,0,sizeof(rsa_key));
475         for (i=0; i<RSA_NUM; i++)
476                 rsa_key[i]=NULL;
477 #endif
478
479         if ((buf=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
480                 {
481                 BIO_printf(bio_err,"out of memory\n");
482                 goto end;
483                 }
484 #ifndef OPENSSL_NO_DES
485         buf_as_des_cblock = (des_cblock *)buf;
486 #endif
487         if ((buf2=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
488                 {
489                 BIO_printf(bio_err,"out of memory\n");
490                 goto end;
491                 }
492
493         memset(c,0,sizeof(c));
494         memset(iv,0,sizeof(iv));
495
496         for (i=0; i<ALGOR_NUM; i++)
497                 doit[i]=0;
498         for (i=0; i<RSA_NUM; i++)
499                 rsa_doit[i]=0;
500         for (i=0; i<DSA_NUM; i++)
501                 dsa_doit[i]=0;
502         
503         j=0;
504         argc--;
505         argv++;
506         while (argc)
507                 {
508                 if      ((argc > 0) && (strcmp(*argv,"-elapsed") == 0))
509                         {
510                         usertime = 0;
511                         j--;    /* Otherwise, -elapsed gets confused with
512                                    an algorithm. */
513                         }
514                 else if ((argc > 0) && (strcmp(*argv,"-evp") == 0))
515                         {
516                         argc--;
517                         argv++;
518                         if(argc == 0)
519                                 {
520                                 BIO_printf(bio_err,"no EVP given\n");
521                                 goto end;
522                                 }
523                         evp=EVP_get_cipherbyname(*argv);
524                         if(!evp)
525                                 {
526                                 BIO_printf(bio_err,"%s is an unknown cipher\n",*argv);
527                                 goto end;
528                                 }
529                         doit[D_EVP]=1;
530                         }
531                 else if (argc > 0 && !strcmp(*argv,"-decrypt"))
532                         {
533                         decrypt=1;
534                         j--;    /* Otherwise, -elapsed gets confused with
535                                    an algorithm. */
536                         }
537                 else if ((argc > 0) && (strcmp(*argv,"-engine") == 0))
538                         {
539                         argc--;
540                         argv++;
541                         if(argc == 0)
542                                 {
543                                 BIO_printf(bio_err,"no engine given\n");
544                                 goto end;
545                                 }
546                         e = setup_engine(bio_err, *argv, 0);
547                         /* j will be increased again further down.  We just
548                            don't want speed to confuse an engine with an
549                            algorithm, especially when none is given (which
550                            means all of them should be run) */
551                         j--;
552                         }
553 #ifdef HAVE_FORK
554                 else if ((argc > 0) && (strcmp(*argv,"-multi") == 0))
555                         {
556                         argc--;
557                         argv++;
558                         if(argc == 0)
559                                 {
560                                 BIO_printf(bio_err,"no multi count given\n");
561                                 goto end;
562                                 }
563                         multi=atoi(argv[0]);
564                         if(multi <= 0)
565                             {
566                                 BIO_printf(bio_err,"bad multi count\n");
567                                 goto end;
568                                 }                               
569                         j--;    /* Otherwise, -mr gets confused with
570                                    an algorithm. */
571                         }
572 #endif
573                 else if (argc > 0 && !strcmp(*argv,"-mr"))
574                         {
575                         mr=1;
576                         j--;    /* Otherwise, -mr gets confused with
577                                    an algorithm. */
578                         }
579                 else
580 #ifndef OPENSSL_NO_MD2
581                 if      (strcmp(*argv,"md2") == 0) doit[D_MD2]=1;
582                 else
583 #endif
584 #ifndef OPENSSL_NO_MDC2
585                         if (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;
586                 else
587 #endif
588 #ifndef OPENSSL_NO_MD4
589                         if (strcmp(*argv,"md4") == 0) doit[D_MD4]=1;
590                 else
591 #endif
592 #ifndef OPENSSL_NO_MD5
593                         if (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;
594                 else
595 #endif
596 #ifndef OPENSSL_NO_MD5
597                         if (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;
598                 else
599 #endif
600 #ifndef OPENSSL_NO_SHA
601                         if (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;
602                 else
603                         if (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1;
604                 else
605 #endif
606 #ifndef OPENSSL_NO_RIPEMD
607                         if (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1;
608                 else
609                         if (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1;
610                 else
611                         if (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1;
612                 else
613 #endif
614 #ifndef OPENSSL_NO_RC4
615                         if (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;
616                 else 
617 #endif
618 #ifndef OPENSSL_NO_DES
619                         if (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1;
620                 else    if (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1;
621                 else
622 #endif
623 #ifndef OPENSSL_NO_RSA
624 #if 0 /* was: #ifdef RSAref */
625                         if (strcmp(*argv,"rsaref") == 0) 
626                         {
627                         RSA_set_default_openssl_method(RSA_PKCS1_RSAref());
628                         j--;
629                         }
630                 else
631 #endif
632 #ifndef RSA_NULL
633                         if (strcmp(*argv,"openssl") == 0) 
634                         {
635                         RSA_set_default_method(RSA_PKCS1_SSLeay());
636                         j--;
637                         }
638                 else
639 #endif
640 #endif /* !OPENSSL_NO_RSA */
641                      if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2;
642                 else if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2;
643                 else if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2;
644                 else if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2;
645                 else if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2;
646                 else if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2;
647                 else if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2;
648                 else
649 #ifndef OPENSSL_NO_RC2
650                      if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;
651                 else if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;
652                 else
653 #endif
654 #ifndef OPENSSL_NO_RC5
655                      if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;
656                 else if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;
657                 else
658 #endif
659 #ifndef OPENSSL_NO_IDEA
660                      if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;
661                 else if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;
662                 else
663 #endif
664 #ifndef OPENSSL_NO_BF
665                      if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;
666                 else if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;
667                 else if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;
668                 else
669 #endif
670 #ifndef OPENSSL_NO_CAST
671                      if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1;
672                 else if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1;
673                 else if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1;
674                 else
675 #endif
676 #ifndef OPENSSL_NO_DES
677                         if (strcmp(*argv,"des") == 0)
678                         {
679                         doit[D_CBC_DES]=1;
680                         doit[D_EDE3_DES]=1;
681                         }
682                 else
683 #endif
684 #ifndef OPENSSL_NO_RSA
685                         if (strcmp(*argv,"rsa") == 0)
686                         {
687                         rsa_doit[R_RSA_512]=1;
688                         rsa_doit[R_RSA_1024]=1;
689                         rsa_doit[R_RSA_2048]=1;
690                         rsa_doit[R_RSA_4096]=1;
691                         }
692                 else
693 #endif
694 #ifndef OPENSSL_NO_DSA
695                         if (strcmp(*argv,"dsa") == 0)
696                         {
697                         dsa_doit[R_DSA_512]=1;
698                         dsa_doit[R_DSA_1024]=1;
699                         }
700                 else
701 #endif
702                         {
703                         BIO_printf(bio_err,"Error: bad option or value\n");
704                         BIO_printf(bio_err,"\n");
705                         BIO_printf(bio_err,"Available values:\n");
706 #ifndef OPENSSL_NO_MD2
707                         BIO_printf(bio_err,"md2      ");
708 #endif
709 #ifndef OPENSSL_NO_MDC2
710                         BIO_printf(bio_err,"mdc2     ");
711 #endif
712 #ifndef OPENSSL_NO_MD4
713                         BIO_printf(bio_err,"md4      ");
714 #endif
715 #ifndef OPENSSL_NO_MD5
716                         BIO_printf(bio_err,"md5      ");
717 #ifndef OPENSSL_NO_HMAC
718                         BIO_printf(bio_err,"hmac     ");
719 #endif
720 #endif
721 #ifndef OPENSSL_NO_SHA1
722                         BIO_printf(bio_err,"sha1     ");
723 #endif
724 #ifndef OPENSSL_NO_RIPEMD160
725                         BIO_printf(bio_err,"rmd160");
726 #endif
727 #if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \
728     !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \
729     !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160)
730                         BIO_printf(bio_err,"\n");
731 #endif
732
733 #ifndef OPENSSL_NO_IDEA
734                         BIO_printf(bio_err,"idea-cbc ");
735 #endif
736 #ifndef OPENSSL_NO_RC2
737                         BIO_printf(bio_err,"rc2-cbc  ");
738 #endif
739 #ifndef OPENSSL_NO_RC5
740                         BIO_printf(bio_err,"rc5-cbc  ");
741 #endif
742 #ifndef OPENSSL_NO_BF
743                         BIO_printf(bio_err,"bf-cbc");
744 #endif
745 #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \
746     !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)
747                         BIO_printf(bio_err,"\n");
748 #endif
749
750                         BIO_printf(bio_err,"des-cbc  des-ede3 ");
751 #ifndef OPENSSL_NO_RC4
752                         BIO_printf(bio_err,"rc4");
753 #endif
754                         BIO_printf(bio_err,"\n");
755
756 #ifndef OPENSSL_NO_RSA
757                         BIO_printf(bio_err,"rsa512   rsa1024  rsa2048  rsa4096\n");
758 #endif
759
760 #ifndef OPENSSL_NO_DSA
761                         BIO_printf(bio_err,"dsa512   dsa1024  dsa2048\n");
762 #endif
763
764 #ifndef OPENSSL_NO_IDEA
765                         BIO_printf(bio_err,"idea     ");
766 #endif
767 #ifndef OPENSSL_NO_RC2
768                         BIO_printf(bio_err,"rc2      ");
769 #endif
770 #ifndef OPENSSL_NO_DES
771                         BIO_printf(bio_err,"des      ");
772 #endif
773 #ifndef OPENSSL_NO_RSA
774                         BIO_printf(bio_err,"rsa      ");
775 #endif
776 #ifndef OPENSSL_NO_BF
777                         BIO_printf(bio_err,"blowfish");
778 #endif
779 #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \
780     !defined(OPENSSL_NO_DES) || !defined(OPENSSL_NO_RSA) || \
781     !defined(OPENSSL_NO_BF)
782                         BIO_printf(bio_err,"\n");
783 #endif
784
785                         BIO_printf(bio_err,"\n");
786                         BIO_printf(bio_err,"Available options:\n");
787 #ifdef TIMES
788                         BIO_printf(bio_err,"-elapsed        measure time in real time instead of CPU user time.\n");
789 #endif
790                         BIO_printf(bio_err,"-engine e       use engine e, possibly a hardware device.\n");
791                         BIO_printf(bio_err,"-evp e          use EVP e.\n");
792                         BIO_printf(bio_err,"-decrypt        time decryption instead of encryption (only EVP).\n");
793                         BIO_printf(bio_err,"-mr             produce machine readable output.\n");
794 #ifdef HAVE_FORK
795                         BIO_printf(bio_err,"-multi n        run n benchmarks in parallel.\n");
796 #endif
797                         goto end;
798                         }
799                 argc--;
800                 argv++;
801                 j++;
802                 }
803
804 #ifdef HAVE_FORK
805         if(multi && do_multi(multi))
806                 goto show_res;
807 #endif
808
809         if (j == 0)
810                 {
811                 for (i=0; i<ALGOR_NUM; i++)
812                         {
813                         if (i != D_EVP)
814                                 doit[i]=1;
815                         }
816                 for (i=0; i<RSA_NUM; i++)
817                         rsa_doit[i]=1;
818                 for (i=0; i<DSA_NUM; i++)
819                         dsa_doit[i]=1;
820                 }
821         for (i=0; i<ALGOR_NUM; i++)
822                 if (doit[i]) pr_header++;
823
824         if (usertime == 0 && !mr)
825                 BIO_printf(bio_err,"You have chosen to measure elapsed time instead of user CPU time.\n");
826         if (usertime <= 0 && !mr)
827                 {
828                 BIO_printf(bio_err,"To get the most accurate results, try to run this\n");
829                 BIO_printf(bio_err,"program when this computer is idle.\n");
830                 }
831
832 #ifndef OPENSSL_NO_RSA
833         for (i=0; i<RSA_NUM; i++)
834                 {
835                 const unsigned char *p;
836
837                 p=rsa_data[i];
838                 rsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]);
839                 if (rsa_key[i] == NULL)
840                         {
841                         BIO_printf(bio_err,"internal error loading RSA key number %d\n",i);
842                         goto end;
843                         }
844 #if 0
845                 else
846                         {
847                         BIO_printf(bio_err,mr ? "+RK:%d:"
848                                    : "Loaded RSA key, %d bit modulus and e= 0x",
849                                    BN_num_bits(rsa_key[i]->n));
850                         BN_print(bio_err,rsa_key[i]->e);
851                         BIO_printf(bio_err,"\n");
852                         }
853 #endif
854                 }
855 #endif
856
857 #ifndef OPENSSL_NO_DSA
858         dsa_key[0]=get_dsa512();
859         dsa_key[1]=get_dsa1024();
860         dsa_key[2]=get_dsa2048();
861 #endif
862
863 #ifndef OPENSSL_NO_DES
864         DES_set_key_unchecked(&key,&sch);
865         DES_set_key_unchecked(&key2,&sch2);
866         DES_set_key_unchecked(&key3,&sch3);
867 #endif
868 #ifndef OPENSSL_NO_IDEA
869         idea_set_encrypt_key(key16,&idea_ks);
870 #endif
871 #ifndef OPENSSL_NO_RC4
872         RC4_set_key(&rc4_ks,16,key16);
873 #endif
874 #ifndef OPENSSL_NO_RC2
875         RC2_set_key(&rc2_ks,16,key16,128);
876 #endif
877 #ifndef OPENSSL_NO_RC5
878         RC5_32_set_key(&rc5_ks,16,key16,12);
879 #endif
880 #ifndef OPENSSL_NO_BF
881         BF_set_key(&bf_ks,16,key16);
882 #endif
883 #ifndef OPENSSL_NO_CAST
884         CAST_set_key(&cast_ks,16,key16);
885 #endif
886 #ifndef OPENSSL_NO_RSA
887         memset(rsa_c,0,sizeof(rsa_c));
888 #endif
889 #ifndef SIGALRM
890 #ifndef OPENSSL_NO_DES
891         BIO_printf(bio_err,"First we calculate the approximate speed ...\n");
892         count=10;
893         do      {
894                 long i;
895                 count*=2;
896                 Time_F(START,usertime);
897                 for (i=count; i; i--)
898                         des_ecb_encrypt(buf_as_des_cblock,buf_as_des_cblock,
899                                 &sch,DES_ENCRYPT);
900                 d=Time_F(STOP);
901                 } while (d <3);
902         save_count=count;
903         c[D_MD2][0]=count/10;
904         c[D_MDC2][0]=count/10;
905         c[D_MD4][0]=count;
906         c[D_MD5][0]=count;
907         c[D_HMAC][0]=count;
908         c[D_SHA1][0]=count;
909         c[D_RMD160][0]=count;
910         c[D_RC4][0]=count*5;
911         c[D_CBC_DES][0]=count;
912         c[D_EDE3_DES][0]=count/3;
913         c[D_CBC_IDEA][0]=count;
914         c[D_CBC_RC2][0]=count;
915         c[D_CBC_RC5][0]=count;
916         c[D_CBC_BF][0]=count;
917         c[D_CBC_CAST][0]=count;
918
919         for (i=1; i<SIZE_NUM; i++)
920                 {
921                 c[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];
922                 c[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];
923                 c[D_MD4][i]=c[D_MD4][0]*4*lengths[0]/lengths[i];
924                 c[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];
925                 c[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];
926                 c[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];
927                 c[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];
928                 }
929         for (i=1; i<SIZE_NUM; i++)
930                 {
931                 long l0,l1;
932
933                 l0=(long)lengths[i-1];
934                 l1=(long)lengths[i];
935                 c[D_RC4][i]=c[D_RC4][i-1]*l0/l1;
936                 c[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;
937                 c[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;
938                 c[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;
939                 c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;
940                 c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
941                 c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
942                 c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
943                 }
944 #ifndef OPENSSL_NO_RSA
945         rsa_c[R_RSA_512][0]=count/2000;
946         rsa_c[R_RSA_512][1]=count/400;
947         for (i=1; i<RSA_NUM; i++)
948                 {
949                 rsa_c[i][0]=rsa_c[i-1][0]/8;
950                 rsa_c[i][1]=rsa_c[i-1][1]/4;
951                 if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
952                         rsa_doit[i]=0;
953                 else
954                         {
955                         if (rsa_c[i][0] == 0)
956                                 {
957                                 rsa_c[i][0]=1;
958                                 rsa_c[i][1]=20;
959                                 }
960                         }                               
961                 }
962 #endif
963
964 #ifndef OPENSSL_NO_DSA
965         dsa_c[R_DSA_512][0]=count/1000;
966         dsa_c[R_DSA_512][1]=count/1000/2;
967         for (i=1; i<DSA_NUM; i++)
968                 {
969                 dsa_c[i][0]=dsa_c[i-1][0]/4;
970                 dsa_c[i][1]=dsa_c[i-1][1]/4;
971                 if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
972                         dsa_doit[i]=0;
973                 else
974                         {
975                         if (dsa_c[i] == 0)
976                                 {
977                                 dsa_c[i][0]=1;
978                                 dsa_c[i][1]=1;
979                                 }
980                         }                               
981                 }
982 #endif
983
984 #define COND(d) (count < (d))
985 #define COUNT(d) (d)
986 #else
987 /* not worth fixing */
988 # error "You cannot disable DES on systems without SIGALRM."
989 #endif /* OPENSSL_NO_DES */
990 #else
991 #define COND(c) (run)
992 #define COUNT(d) (count)
993         signal(SIGALRM,sig_done);
994 #endif /* SIGALRM */
995
996 #ifndef OPENSSL_NO_MD2
997         if (doit[D_MD2])
998                 {
999                 for (j=0; j<SIZE_NUM; j++)
1000                         {
1001                         print_message(names[D_MD2],c[D_MD2][j],lengths[j]);
1002                         Time_F(START);
1003                         for (count=0,run=1; COND(c[D_MD2][j]); count++)
1004                                 EVP_Digest(buf,(unsigned long)lengths[j],&(md2[0]),NULL,EVP_md2(),NULL);
1005                         d=Time_F(STOP);
1006                         print_result(D_MD2,j,count,d);
1007                         }
1008                 }
1009 #endif
1010 #ifndef OPENSSL_NO_MDC2
1011         if (doit[D_MDC2])
1012                 {
1013                 for (j=0; j<SIZE_NUM; j++)
1014                         {
1015                         print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);
1016                         Time_F(START);
1017                         for (count=0,run=1; COND(c[D_MDC2][j]); count++)
1018                                 EVP_Digest(buf,(unsigned long)lengths[j],&(mdc2[0]),NULL,EVP_mdc2(),NULL);
1019                         d=Time_F(STOP);
1020                         print_result(D_MDC2,j,count,d);
1021                         }
1022                 }
1023 #endif
1024
1025 #ifndef OPENSSL_NO_MD4
1026         if (doit[D_MD4])
1027                 {
1028                 for (j=0; j<SIZE_NUM; j++)
1029                         {
1030                         print_message(names[D_MD4],c[D_MD4][j],lengths[j]);
1031                         Time_F(START);
1032                         for (count=0,run=1; COND(c[D_MD4][j]); count++)
1033                                 EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md4[0]),NULL,EVP_md4(),NULL);
1034                         d=Time_F(STOP);
1035                         print_result(D_MD4,j,count,d);
1036                         }
1037                 }
1038 #endif
1039
1040 #ifndef OPENSSL_NO_MD5
1041         if (doit[D_MD5])
1042                 {
1043                 for (j=0; j<SIZE_NUM; j++)
1044                         {
1045                         print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
1046                         Time_F(START);
1047                         for (count=0,run=1; COND(c[D_MD5][j]); count++)
1048                                 EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md5[0]),NULL,EVP_get_digestbyname("md5"),NULL);
1049                         d=Time_F(STOP);
1050                         print_result(D_MD5,j,count,d);
1051                         }
1052                 }
1053 #endif
1054
1055 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)
1056         if (doit[D_HMAC])
1057                 {
1058                 HMAC_CTX hctx;
1059
1060                 HMAC_CTX_init(&hctx);
1061                 HMAC_Init(&hctx,(unsigned char *)"This is a key...",
1062                         16,EVP_md5());
1063
1064                 for (j=0; j<SIZE_NUM; j++)
1065                         {
1066                         print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);
1067                         Time_F(START);
1068                         for (count=0,run=1; COND(c[D_HMAC][j]); count++)
1069                                 {
1070                                 HMAC_Init(&hctx,NULL,0,NULL);
1071                                 HMAC_Update(&hctx,buf,lengths[j]);
1072                                 HMAC_Final(&hctx,&(hmac[0]),NULL);
1073                                 }
1074                         d=Time_F(STOP);
1075                         print_result(D_HMAC,j,count,d);
1076                         }
1077                 HMAC_CTX_cleanup(&hctx);
1078                 }
1079 #endif
1080 #ifndef OPENSSL_NO_SHA
1081         if (doit[D_SHA1])
1082                 {
1083                 for (j=0; j<SIZE_NUM; j++)
1084                         {
1085                         print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);
1086                         Time_F(START);
1087                         for (count=0,run=1; COND(c[D_SHA1][j]); count++)
1088                                 EVP_Digest(buf,(unsigned long)lengths[j],&(sha[0]),NULL,EVP_sha1(),NULL);
1089                         d=Time_F(STOP);
1090                         print_result(D_SHA1,j,count,d);
1091                         }
1092                 }
1093 #endif
1094 #ifndef OPENSSL_NO_RIPEMD
1095         if (doit[D_RMD160])
1096                 {
1097                 for (j=0; j<SIZE_NUM; j++)
1098                         {
1099                         print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);
1100                         Time_F(START);
1101                         for (count=0,run=1; COND(c[D_RMD160][j]); count++)
1102                                 EVP_Digest(buf,(unsigned long)lengths[j],&(rmd160[0]),NULL,EVP_ripemd160(),NULL);
1103                         d=Time_F(STOP);
1104                         print_result(D_RMD160,j,count,d);
1105                         }
1106                 }
1107 #endif
1108 #ifndef OPENSSL_NO_RC4
1109         if (doit[D_RC4])
1110                 {
1111                 for (j=0; j<SIZE_NUM; j++)
1112                         {
1113                         print_message(names[D_RC4],c[D_RC4][j],lengths[j]);
1114                         Time_F(START);
1115                         for (count=0,run=1; COND(c[D_RC4][j]); count++)
1116                                 RC4(&rc4_ks,(unsigned int)lengths[j],
1117                                         buf,buf);
1118                         d=Time_F(STOP);
1119                         print_result(D_RC4,j,count,d);
1120                         }
1121                 }
1122 #endif
1123 #ifndef OPENSSL_NO_DES
1124         if (doit[D_CBC_DES])
1125                 {
1126                 for (j=0; j<SIZE_NUM; j++)
1127                         {
1128                         print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);
1129                         Time_F(START);
1130                         for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
1131                                 DES_ncbc_encrypt(buf,buf,lengths[j],&sch,
1132                                                  &iv,DES_ENCRYPT);
1133                         d=Time_F(STOP);
1134                         print_result(D_CBC_DES,j,count,d);
1135                         }
1136                 }
1137
1138         if (doit[D_EDE3_DES])
1139                 {
1140                 for (j=0; j<SIZE_NUM; j++)
1141                         {
1142                         print_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);
1143                         Time_F(START);
1144                         for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
1145                                 DES_ede3_cbc_encrypt(buf,buf,lengths[j],
1146                                                      &sch,&sch2,&sch3,
1147                                                      &iv,DES_ENCRYPT);
1148                         d=Time_F(STOP);
1149                         print_result(D_EDE3_DES,j,count,d);
1150                         }
1151                 }
1152 #endif
1153 #ifndef OPENSSL_NO_IDEA
1154         if (doit[D_CBC_IDEA])
1155                 {
1156                 for (j=0; j<SIZE_NUM; j++)
1157                         {
1158                         print_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);
1159                         Time_F(START);
1160                         for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)
1161                                 idea_cbc_encrypt(buf,buf,
1162                                         (unsigned long)lengths[j],&idea_ks,
1163                                         iv,IDEA_ENCRYPT);
1164                         d=Time_F(STOP);
1165                         print_result(D_CBC_IDEA,j,count,d);
1166                         }
1167                 }
1168 #endif
1169 #ifndef OPENSSL_NO_RC2
1170         if (doit[D_CBC_RC2])
1171                 {
1172                 for (j=0; j<SIZE_NUM; j++)
1173                         {
1174                         print_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);
1175                         Time_F(START);
1176                         for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)
1177                                 RC2_cbc_encrypt(buf,buf,
1178                                         (unsigned long)lengths[j],&rc2_ks,
1179                                         iv,RC2_ENCRYPT);
1180                         d=Time_F(STOP);
1181                         print_result(D_CBC_RC2,j,count,d);
1182                         }
1183                 }
1184 #endif
1185 #ifndef OPENSSL_NO_RC5
1186         if (doit[D_CBC_RC5])
1187                 {
1188                 for (j=0; j<SIZE_NUM; j++)
1189                         {
1190                         print_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);
1191                         Time_F(START);
1192                         for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)
1193                                 RC5_32_cbc_encrypt(buf,buf,
1194                                         (unsigned long)lengths[j],&rc5_ks,
1195                                         iv,RC5_ENCRYPT);
1196                         d=Time_F(STOP);
1197                         print_result(D_CBC_RC5,j,count,d);
1198                         }
1199                 }
1200 #endif
1201 #ifndef OPENSSL_NO_BF
1202         if (doit[D_CBC_BF])
1203                 {
1204                 for (j=0; j<SIZE_NUM; j++)
1205                         {
1206                         print_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);
1207                         Time_F(START);
1208                         for (count=0,run=1; COND(c[D_CBC_BF][j]); count++)
1209                                 BF_cbc_encrypt(buf,buf,
1210                                         (unsigned long)lengths[j],&bf_ks,
1211                                         iv,BF_ENCRYPT);
1212                         d=Time_F(STOP);
1213                         print_result(D_CBC_BF,j,count,d);
1214                         }
1215                 }
1216 #endif
1217 #ifndef OPENSSL_NO_CAST
1218         if (doit[D_CBC_CAST])
1219                 {
1220                 for (j=0; j<SIZE_NUM; j++)
1221                         {
1222                         print_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);
1223                         Time_F(START);
1224                         for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)
1225                                 CAST_cbc_encrypt(buf,buf,
1226                                         (unsigned long)lengths[j],&cast_ks,
1227                                         iv,CAST_ENCRYPT);
1228                         d=Time_F(STOP);
1229                         print_result(D_CBC_CAST,j,count,d);
1230                         }
1231                 }
1232 #endif
1233
1234         if (doit[D_EVP])
1235                 {
1236                 for (j=0; j<SIZE_NUM; j++)
1237                         {
1238                         EVP_CIPHER_CTX ctx;
1239                         int outl;
1240
1241                         names[D_EVP]=OBJ_nid2ln(evp->nid);
1242                         print_message(names[D_EVP],save_count,
1243                                                   lengths[j]);
1244                         EVP_CIPHER_CTX_init(&ctx);
1245                         if(decrypt)
1246                                 EVP_DecryptInit_ex(&ctx,evp,NULL,key16,iv);
1247                         else
1248                                 EVP_EncryptInit_ex(&ctx,evp,NULL,key16,iv);
1249                                 
1250                         Time_F(START);
1251                         if(decrypt)
1252                                 for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
1253                                         EVP_DecryptUpdate(&ctx,buf,&outl,buf,lengths[j]);
1254                         else
1255                                 for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
1256                                         EVP_EncryptUpdate(&ctx,buf,&outl,buf,lengths[j]);
1257                         if(decrypt)
1258                                 EVP_DecryptFinal_ex(&ctx,buf,&outl);
1259                         else
1260                                 EVP_EncryptFinal_ex(&ctx,buf,&outl);
1261                         d=Time_F(STOP);
1262                         print_result(D_EVP,j,count,d);
1263                         }
1264                 }
1265
1266         RAND_pseudo_bytes(buf,36);
1267 #ifndef OPENSSL_NO_RSA
1268         for (j=0; j<RSA_NUM; j++)
1269                 {
1270                 int ret;
1271                 if (!rsa_doit[j]) continue;
1272                 ret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]);
1273                 if (ret == 0)
1274                         {
1275                         BIO_printf(bio_err,"RSA sign failure.  No RSA sign will be done.\n");
1276                         ERR_print_errors(bio_err);
1277                         rsa_count=1;
1278                         }
1279                 else
1280                         {
1281                         pkey_print_message("private","rsa",
1282                                 rsa_c[j][0],rsa_bits[j],
1283                                 RSA_SECONDS);
1284 /*                      RSA_blinding_on(rsa_key[j],NULL); */
1285                         Time_F(START);
1286                         for (count=0,run=1; COND(rsa_c[j][0]); count++)
1287                                 {
1288                                 ret=RSA_sign(NID_md5_sha1, buf,36, buf2,
1289                                         &rsa_num, rsa_key[j]);
1290                                 if (ret == 0)
1291                                         {
1292                                         BIO_printf(bio_err,
1293                                                 "RSA sign failure\n");
1294                                         ERR_print_errors(bio_err);
1295                                         count=1;
1296                                         break;
1297                                         }
1298                                 }
1299                         d=Time_F(STOP);
1300                         BIO_printf(bio_err,mr ? "+R1:%ld:%d:%.2f\n"
1301                                    : "%ld %d bit private RSA's in %.2fs\n",
1302                                    count,rsa_bits[j],d);
1303                         rsa_results[j][0]=d/(double)count;
1304                         rsa_count=count;
1305                         }
1306
1307 #if 1
1308                 ret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]);
1309                 if (ret <= 0)
1310                         {
1311                         BIO_printf(bio_err,"RSA verify failure.  No RSA verify will be done.\n");
1312                         ERR_print_errors(bio_err);
1313                         rsa_doit[j] = 0;
1314                         }
1315                 else
1316                         {
1317                         pkey_print_message("public","rsa",
1318                                 rsa_c[j][1],rsa_bits[j],
1319                                 RSA_SECONDS);
1320                         Time_F(START);
1321                         for (count=0,run=1; COND(rsa_c[j][1]); count++)
1322                                 {
1323                                 ret=RSA_verify(NID_md5_sha1, buf,36, buf2,
1324                                         rsa_num, rsa_key[j]);
1325                                 if (ret == 0)
1326                                         {
1327                                         BIO_printf(bio_err,
1328                                                 "RSA verify failure\n");
1329                                         ERR_print_errors(bio_err);
1330                                         count=1;
1331                                         break;
1332                                         }
1333                                 }
1334                         d=Time_F(STOP);
1335                         BIO_printf(bio_err,mr ? "+R2:%ld:%d:%.2f\n"
1336                                    : "%ld %d bit public RSA's in %.2fs\n",
1337                                    count,rsa_bits[j],d);
1338                         rsa_results[j][1]=d/(double)count;
1339                         }
1340 #endif
1341
1342                 if (rsa_count <= 1)
1343                         {
1344                         /* if longer than 10s, don't do any more */
1345                         for (j++; j<RSA_NUM; j++)
1346                                 rsa_doit[j]=0;
1347                         }
1348                 }
1349 #endif
1350
1351         RAND_pseudo_bytes(buf,20);
1352 #ifndef OPENSSL_NO_DSA
1353         if (RAND_status() != 1)
1354                 {
1355                 RAND_seed(rnd_seed, sizeof rnd_seed);
1356                 rnd_fake = 1;
1357                 }
1358         for (j=0; j<DSA_NUM; j++)
1359                 {
1360                 unsigned int kk;
1361                 int ret;
1362
1363                 if (!dsa_doit[j]) continue;
1364 /*              DSA_generate_key(dsa_key[j]); */
1365 /*              DSA_sign_setup(dsa_key[j],NULL); */
1366                 ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1367                         &kk,dsa_key[j]);
1368                 if (ret == 0)
1369                         {
1370                         BIO_printf(bio_err,"DSA sign failure.  No DSA sign will be done.\n");
1371                         ERR_print_errors(bio_err);
1372                         rsa_count=1;
1373                         }
1374                 else
1375                         {
1376                         pkey_print_message("sign","dsa",
1377                                 dsa_c[j][0],dsa_bits[j],
1378                                 DSA_SECONDS);
1379                         Time_F(START);
1380                         for (count=0,run=1; COND(dsa_c[j][0]); count++)
1381                                 {
1382                                 ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1383                                         &kk,dsa_key[j]);
1384                                 if (ret == 0)
1385                                         {
1386                                         BIO_printf(bio_err,
1387                                                 "DSA sign failure\n");
1388                                         ERR_print_errors(bio_err);
1389                                         count=1;
1390                                         break;
1391                                         }
1392                                 }
1393                         d=Time_F(STOP);
1394                         BIO_printf(bio_err,mr ? "+R3:%ld:%d:%.2f\n"
1395                                    : "%ld %d bit DSA signs in %.2fs\n",
1396                                    count,dsa_bits[j],d);
1397                         dsa_results[j][0]=d/(double)count;
1398                         rsa_count=count;
1399                         }
1400
1401                 ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1402                         kk,dsa_key[j]);
1403                 if (ret <= 0)
1404                         {
1405                         BIO_printf(bio_err,"DSA verify failure.  No DSA verify will be done.\n");
1406                         ERR_print_errors(bio_err);
1407                         dsa_doit[j] = 0;
1408                         }
1409                 else
1410                         {
1411                         pkey_print_message("verify","dsa",
1412                                 dsa_c[j][1],dsa_bits[j],
1413                                 DSA_SECONDS);
1414                         Time_F(START);
1415                         for (count=0,run=1; COND(dsa_c[j][1]); count++)
1416                                 {
1417                                 ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1418                                         kk,dsa_key[j]);
1419                                 if (ret <= 0)
1420                                         {
1421                                         BIO_printf(bio_err,
1422                                                 "DSA verify failure\n");
1423                                         ERR_print_errors(bio_err);
1424                                         count=1;
1425                                         break;
1426                                         }
1427                                 }
1428                         d=Time_F(STOP);
1429                         BIO_printf(bio_err,mr ? "+R4:%ld:%d:%.2f\n"
1430                                    : "%ld %d bit DSA verify in %.2fs\n",
1431                                    count,dsa_bits[j],d);
1432                         dsa_results[j][1]=d/(double)count;
1433                         }
1434
1435                 if (rsa_count <= 1)
1436                         {
1437                         /* if longer than 10s, don't do any more */
1438                         for (j++; j<DSA_NUM; j++)
1439                                 dsa_doit[j]=0;
1440                         }
1441                 }
1442         if (rnd_fake) RAND_cleanup();
1443 #endif
1444 show_res:
1445         if(!mr)
1446                 {
1447                 fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION));
1448         fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON));
1449                 printf("options:");
1450                 printf("%s ",BN_options());
1451 #ifndef OPENSSL_NO_MD2
1452                 printf("%s ",MD2_options());
1453 #endif
1454 #ifndef OPENSSL_NO_RC4
1455                 printf("%s ",RC4_options());
1456 #endif
1457 #ifndef OPENSSL_NO_DES
1458                 printf("%s ",des_options());
1459 #endif
1460 #ifndef OPENSSL_NO_IDEA
1461                 printf("%s ",idea_options());
1462 #endif
1463 #ifndef OPENSSL_NO_BF
1464                 printf("%s ",BF_options());
1465 #endif
1466                 fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
1467                 printf("available timing options: ");
1468 #ifdef TIMES
1469                 printf("TIMES ");
1470 #endif
1471 #ifdef TIMEB
1472                 printf("TIMEB ");
1473 #endif
1474 #ifdef USE_TOD
1475                 printf("USE_TOD ");
1476 #endif
1477 #ifdef HZ
1478 #define as_string(s) (#s)
1479                 printf("HZ=%g", HZ);
1480 # ifdef _SC_CLK_TCK
1481                 printf(" [sysconf value]");
1482 # endif
1483 #endif
1484                 printf("\n");
1485                 printf("timing function used: %s%s%s%s%s%s%s\n",
1486                        (ftime_used ? "ftime" : ""),
1487                        (ftime_used + times_used > 1 ? "," : ""),
1488                        (times_used ? "times" : ""),
1489                        (ftime_used + times_used + gettimeofday_used > 1 ? "," : ""),
1490                        (gettimeofday_used ? "gettimeofday" : ""),
1491                        (ftime_used + times_used + gettimeofday_used + getrusage_used > 1 ? "," : ""),
1492                        (getrusage_used ? "getrusage" : ""));
1493                 }
1494
1495         if (pr_header)
1496                 {
1497                 if(mr)
1498                         fprintf(stdout,"+H");
1499                 else
1500                         {
1501                         fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n"); 
1502                         fprintf(stdout,"type        ");
1503                         }
1504                 for (j=0;  j<SIZE_NUM; j++)
1505                         fprintf(stdout,mr ? ":%d" : "%7d bytes",lengths[j]);
1506                 fprintf(stdout,"\n");
1507                 }
1508
1509         for (k=0; k<ALGOR_NUM; k++)
1510                 {
1511                 if (!doit[k]) continue;
1512                 if(mr)
1513                         fprintf(stdout,"+F:%d:%s",k,names[k]);
1514                 else
1515                         fprintf(stdout,"%-13s",names[k]);
1516                 for (j=0; j<SIZE_NUM; j++)
1517                         {
1518                         if (results[k][j] > 10000 && !mr)
1519                                 fprintf(stdout," %11.2fk",results[k][j]/1e3);
1520                         else
1521                                 fprintf(stdout,mr ? ":%.2f" : " %11.2f ",results[k][j]);
1522                         }
1523                 fprintf(stdout,"\n");
1524                 }
1525 #ifndef OPENSSL_NO_RSA
1526         j=1;
1527         for (k=0; k<RSA_NUM; k++)
1528                 {
1529                 if (!rsa_doit[k]) continue;
1530                 if (j && !mr)
1531                         {
1532                         printf("%18ssign    verify    sign/s verify/s\n"," ");
1533                         j=0;
1534                         }
1535                 if(mr)
1536                         fprintf(stdout,"+F2:%u:%u:%f:%f\n",
1537                                 k,rsa_bits[k],rsa_results[k][0],
1538                                 rsa_results[k][1]);
1539                 else
1540                         fprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n",
1541                                 rsa_bits[k],rsa_results[k][0],rsa_results[k][1],
1542                                 1.0/rsa_results[k][0],1.0/rsa_results[k][1]);
1543                 }
1544 #endif
1545 #ifndef OPENSSL_NO_DSA
1546         j=1;
1547         for (k=0; k<DSA_NUM; k++)
1548                 {
1549                 if (!dsa_doit[k]) continue;
1550                 if (j && !mr)
1551                         {
1552                         printf("%18ssign    verify    sign/s verify/s\n"," ");
1553                         j=0;
1554                         }
1555                 if(mr)
1556                         fprintf(stdout,"+F3:%u:%u:%f:%f\n",
1557                                 k,dsa_bits[k],dsa_results[k][0],dsa_results[k][1]);
1558                 else
1559                         fprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n",
1560                                 dsa_bits[k],dsa_results[k][0],dsa_results[k][1],
1561                                 1.0/dsa_results[k][0],1.0/dsa_results[k][1]);
1562                 }
1563 #endif
1564         mret=0;
1565 end:
1566         ERR_print_errors(bio_err);
1567         if (buf != NULL) OPENSSL_free(buf);
1568         if (buf2 != NULL) OPENSSL_free(buf2);
1569 #ifndef OPENSSL_NO_RSA
1570         for (i=0; i<RSA_NUM; i++)
1571                 if (rsa_key[i] != NULL)
1572                         RSA_free(rsa_key[i]);
1573 #endif
1574 #ifndef OPENSSL_NO_DSA
1575         for (i=0; i<DSA_NUM; i++)
1576                 if (dsa_key[i] != NULL)
1577                         DSA_free(dsa_key[i]);
1578 #endif
1579         apps_shutdown();
1580         EXIT(mret);
1581         }
1582
1583 static void print_message(const char *s, long num, int length)
1584         {
1585 #ifdef SIGALRM
1586         BIO_printf(bio_err,mr ? "+DT:%s:%d:%d\n"
1587                    : "Doing %s for %ds on %d size blocks: ",s,SECONDS,length);
1588         (void)BIO_flush(bio_err);
1589         alarm(SECONDS);
1590 #else
1591         BIO_printf(bio_err,mr ? "+DN:%s:%ld:%d\n"
1592                    : "Doing %s %ld times on %d size blocks: ",s,num,length);
1593         (void)BIO_flush(bio_err);
1594 #endif
1595 #ifdef LINT
1596         num=num;
1597 #endif
1598         }
1599
1600 static void pkey_print_message(char *str, char *str2, long num, int bits,
1601              int tm)
1602         {
1603 #ifdef SIGALRM
1604         BIO_printf(bio_err,mr ? "+DTP:%d:%s:%s:%d\n"
1605                            : "Doing %d bit %s %s's for %ds: ",bits,str,str2,tm);
1606         (void)BIO_flush(bio_err);
1607         alarm(RSA_SECONDS);
1608 #else
1609         BIO_printf(bio_err,mr ? "+DNP:%ld:%d:%s:%s\n"
1610                            : "Doing %ld %d bit %s %s's: ",num,bits,str,str2);
1611         (void)BIO_flush(bio_err);
1612 #endif
1613 #ifdef LINT
1614         num=num;
1615 #endif
1616         }
1617
1618 static void print_result(int alg,int run_no,int count,double time_used)
1619         {
1620         BIO_printf(bio_err,mr ? "+R:%ld:%s:%f\n"
1621                    : "%ld %s's in %.2fs\n",count,names[alg],time_used);
1622         results[alg][run_no]=((double)count)/time_used*lengths[run_no];
1623         }
1624
1625 static char *sstrsep(char **string, const char *delim)
1626     {
1627     char isdelim[256];
1628     char *token = *string;
1629
1630     if (**string == 0)
1631         return NULL;
1632
1633     memset(isdelim, 0, 256);
1634     isdelim[0] = 1;
1635
1636     while (*delim)
1637         {
1638         isdelim[(unsigned char)(*delim)] = 1;
1639         delim++;
1640         }
1641
1642     while (!isdelim[(unsigned char)(**string)])
1643         {
1644         (*string)++;
1645         }
1646
1647     if (**string)
1648         {
1649         **string = 0;
1650         (*string)++;
1651         }
1652
1653     return token;
1654     }
1655
1656 #ifdef HAVE_FORK
1657 static int do_multi(int multi)
1658         {
1659         int n;
1660         int fd[2];
1661         int *fds;
1662         static char sep[]=":";
1663
1664         fds=malloc(multi*sizeof *fds);
1665         for(n=0 ; n < multi ; ++n)
1666                 {
1667                 pipe(fd);
1668                 if(fork())
1669                         {
1670                         close(fd[1]);
1671                         fds[n]=fd[0];
1672                         }
1673                 else
1674                         {
1675                         close(fd[0]);
1676                         close(1);
1677                         dup(fd[1]);
1678                         close(fd[1]);
1679                         mr=1;
1680                         usertime=0;
1681                         return 0;
1682                         }
1683                 printf("Forked child %d\n",n);
1684                 }
1685
1686         /* for now, assume the pipe is long enough to take all the output */
1687         for(n=0 ; n < multi ; ++n)
1688                 {
1689                 FILE *f;
1690                 char buf[1024];
1691                 char *p;
1692
1693                 f=fdopen(fds[n],"r");
1694                 while(fgets(buf,sizeof buf,f))
1695                         {
1696                         p=strchr(buf,'\n');
1697                         if(p)
1698                                 *p='\0';
1699                         if(buf[0] != '+')
1700                                 {
1701                                 fprintf(stderr,"Don't understand line '%s' from child %d\n",
1702                                                 buf,n);
1703                                 continue;
1704                                 }
1705                         printf("Got: %s from %d\n",buf,n);
1706                         if(!strncmp(buf,"+F:",3))
1707                                 {
1708                                 int alg;
1709                                 int j;
1710
1711                                 p=buf+3;
1712                                 alg=atoi(sstrsep(&p,sep));
1713                                 sstrsep(&p,sep);
1714                                 for(j=0 ; j < SIZE_NUM ; ++j)
1715                                         results[alg][j]+=atof(sstrsep(&p,sep));
1716                                 }
1717                         else if(!strncmp(buf,"+F2:",4))
1718                                 {
1719                                 int k;
1720                                 double d;
1721                                 
1722                                 p=buf+4;
1723                                 k=atoi(sstrsep(&p,sep));
1724                                 sstrsep(&p,sep);
1725
1726                                 d=atof(sstrsep(&p,sep));
1727                                 if(n)
1728                                         rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
1729                                 else
1730                                         rsa_results[k][0]=d;
1731
1732                                 d=atof(sstrsep(&p,sep));
1733                                 if(n)
1734                                         rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
1735                                 else
1736                                         rsa_results[k][1]=d;
1737                                 }
1738                         else if(!strncmp(buf,"+F2:",4))
1739                                 {
1740                                 int k;
1741                                 double d;
1742                                 
1743                                 p=buf+4;
1744                                 k=atoi(sstrsep(&p,sep));
1745                                 sstrsep(&p,sep);
1746
1747                                 d=atof(sstrsep(&p,sep));
1748                                 if(n)
1749                                         rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
1750                                 else
1751                                         rsa_results[k][0]=d;
1752
1753                                 d=atof(sstrsep(&p,sep));
1754                                 if(n)
1755                                         rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
1756                                 else
1757                                         rsa_results[k][1]=d;
1758                                 }
1759                         else if(!strncmp(buf,"+F3:",4))
1760                                 {
1761                                 int k;
1762                                 double d;
1763                                 
1764                                 p=buf+4;
1765                                 k=atoi(sstrsep(&p,sep));
1766                                 sstrsep(&p,sep);
1767
1768                                 d=atof(sstrsep(&p,sep));
1769                                 if(n)
1770                                         dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);
1771                                 else
1772                                         dsa_results[k][0]=d;
1773
1774                                 d=atof(sstrsep(&p,sep));
1775                                 if(n)
1776                                         dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);
1777                                 else
1778                                         dsa_results[k][1]=d;
1779                                 }
1780                         else if(!strncmp(buf,"+H:",3))
1781                                 {
1782                                 }
1783                         else
1784                                 fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
1785                         }
1786                 }
1787         return 1;
1788         }
1789 #endif