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