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