Get rid of some unnecessary casts and add a necessary one.
[openssl.git] / apps / speed.c
1 /* apps/speed.c */
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 NO_STDIO
79 #define APPS_WIN16
80 #endif
81 #include <openssl/crypto.h>
82 #include <openssl/rand.h>
83 #include <openssl/err.h>
84
85 #if !defined(MSDOS) && (!defined(VMS) || defined(__DECC))
86 #define TIMES
87 #endif
88
89 #ifndef _IRIX
90 #include <time.h>
91 #endif
92 #ifdef TIMES
93 #include <sys/types.h>
94 #include <sys/times.h>
95 #endif
96
97 /* Depending on the VMS version, the tms structure is perhaps defined.
98    The __TMS macro will show if it was.  If it wasn't defined, we should
99    undefine TIMES, since that tells the rest of the program how things
100    should be handled.                           -- Richard Levitte */
101 #if defined(VMS) && defined(__DECC) && !defined(__TMS)
102 #undef TIMES
103 #endif
104
105 #ifndef TIMES
106 #include <sys/timeb.h>
107 #endif
108
109 #if defined(sun) || defined(__ultrix)
110 #define _POSIX_SOURCE
111 #include <limits.h>
112 #include <sys/param.h>
113 #endif
114
115 #ifndef NO_DES
116 #include <openssl/des.h>
117 #endif
118 #ifndef NO_MD2
119 #include <openssl/md2.h>
120 #endif
121 #ifndef NO_MDC2
122 #include <openssl/mdc2.h>
123 #endif
124 #ifndef NO_MD5
125 #include <openssl/md5.h>
126 #include <openssl/hmac.h>
127 #include <openssl/evp.h>
128 #endif
129 #ifndef NO_SHA
130 #include <openssl/sha.h>
131 #endif
132 #ifndef NO_RIPEMD
133 #include <openssl/ripemd.h>
134 #endif
135 #ifndef NO_RC4
136 #include <openssl/rc4.h>
137 #endif
138 #ifndef NO_RC5
139 #include <openssl/rc5.h>
140 #endif
141 #ifndef NO_RC2
142 #include <openssl/rc2.h>
143 #endif
144 #ifndef NO_IDEA
145 #include <openssl/idea.h>
146 #endif
147 #ifndef NO_BF
148 #include <openssl/blowfish.h>
149 #endif
150 #ifndef NO_CAST
151 #include <openssl/cast.h>
152 #endif
153 #ifndef NO_RSA
154 #include <openssl/rsa.h>
155 #include "./testrsa.h"
156 #endif
157 #include <openssl/x509.h>
158 #ifndef NO_DSA
159 #include "./testdsa.h"
160 #endif
161
162 /* The following if from times(3) man page.  It may need to be changed */
163 #ifndef HZ
164 # ifndef CLK_TCK
165 #  ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
166 #   define HZ   100.0
167 #  else /* _BSD_CLK_TCK_ */
168 #   define HZ ((double)_BSD_CLK_TCK_)
169 #  endif
170 # else /* CLK_TCK */
171 #  define HZ ((double)CLK_TCK)
172 # endif
173 #endif
174
175 #undef BUFSIZE
176 #define BUFSIZE ((long)1024*8+1)
177 int run=0;
178
179 static double Time_F(int s);
180 static void print_message(char *s,long num,int length);
181 static void pkey_print_message(char *str,char *str2,long num,int bits,int sec);
182 #ifdef SIGALRM
183 #if defined(__STDC__) || defined(sgi) || defined(_AIX)
184 #define SIGRETTYPE void
185 #else
186 #define SIGRETTYPE int
187 #endif 
188
189 static SIGRETTYPE sig_done(int sig);
190 static SIGRETTYPE sig_done(int sig)
191         {
192         signal(SIGALRM,sig_done);
193         run=0;
194 #ifdef LINT
195         sig=sig;
196 #endif
197         }
198 #endif
199
200 #define START   0
201 #define STOP    1
202
203 static double Time_F(int s)
204         {
205         double ret;
206 #ifdef TIMES
207         static struct tms tstart,tend;
208
209         if (s == START)
210                 {
211                 times(&tstart);
212                 return(0);
213                 }
214         else
215                 {
216                 times(&tend);
217                 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
218                 return((ret < 1e-3)?1e-3:ret);
219                 }
220 #else /* !times() */
221         static struct timeb tstart,tend;
222         long i;
223
224         if (s == START)
225                 {
226                 ftime(&tstart);
227                 return(0);
228                 }
229         else
230                 {
231                 ftime(&tend);
232                 i=(long)tend.millitm-(long)tstart.millitm;
233                 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
234                 return((ret < 0.001)?0.001:ret);
235                 }
236 #endif
237         }
238
239 int MAIN(int argc, char **argv)
240         {
241         unsigned char *buf=NULL,*buf2=NULL;
242         des_cblock *buf_as_des_cblock = NULL;
243         int ret=1;
244 #define ALGOR_NUM       14
245 #define SIZE_NUM        5
246 #define RSA_NUM         4
247 #define DSA_NUM         3
248         long count,rsa_count;
249         int i,j,k,rsa_num,rsa_num2;
250 #ifndef NO_MD2
251         unsigned char md2[MD2_DIGEST_LENGTH];
252 #endif
253 #ifndef NO_MDC2
254         unsigned char mdc2[MDC2_DIGEST_LENGTH];
255 #endif
256 #ifndef NO_MD5
257         unsigned char md5[MD5_DIGEST_LENGTH];
258         unsigned char hmac[MD5_DIGEST_LENGTH];
259 #endif
260 #ifndef NO_SHA
261         unsigned char sha[SHA_DIGEST_LENGTH];
262 #endif
263 #ifndef NO_RIPEMD
264         unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
265 #endif
266 #ifndef NO_RC4
267         RC4_KEY rc4_ks;
268 #endif
269 #ifndef NO_RC5
270         RC5_32_KEY rc5_ks;
271 #endif
272 #ifndef NO_RC2
273         RC2_KEY rc2_ks;
274 #endif
275 #ifndef NO_IDEA
276         IDEA_KEY_SCHEDULE idea_ks;
277 #endif
278 #ifndef NO_BF
279         BF_KEY bf_ks;
280 #endif
281 #ifndef NO_CAST
282         CAST_KEY cast_ks;
283 #endif
284         static unsigned char key16[16]=
285                 {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
286                  0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
287         unsigned char iv[8];
288 #ifndef NO_DES
289         static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
290         static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
291         static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
292         des_key_schedule sch,sch2,sch3;
293 #endif
294 #define D_MD2           0
295 #define D_MDC2          1
296 #define D_MD5           2
297 #define D_HMAC          3
298 #define D_SHA1          4
299 #define D_RMD160        5
300 #define D_RC4           6
301 #define D_CBC_DES       7
302 #define D_EDE3_DES      8
303 #define D_CBC_IDEA      9
304 #define D_CBC_RC2       10
305 #define D_CBC_RC5       11
306 #define D_CBC_BF        12
307 #define D_CBC_CAST      13
308         double d,results[ALGOR_NUM][SIZE_NUM];
309         static int lengths[SIZE_NUM]={8,64,256,1024,8*1024};
310         long c[ALGOR_NUM][SIZE_NUM];
311         static char *names[ALGOR_NUM]={
312                 "md2","mdc2","md5","hmac(md5)","sha1","rmd160","rc4",
313                 "des cbc","des ede3","idea cbc",
314                 "rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc"};
315 #define R_DSA_512       0
316 #define R_DSA_1024      1
317 #define R_DSA_2048      2
318 #define R_RSA_512       0
319 #define R_RSA_1024      1
320 #define R_RSA_2048      2
321 #define R_RSA_4096      3
322 #ifndef NO_RSA
323         RSA *rsa_key[RSA_NUM];
324         long rsa_c[RSA_NUM][2];
325         double rsa_results[RSA_NUM][2];
326         static unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096};
327         static unsigned char *rsa_data[RSA_NUM]=
328                 {test512,test1024,test2048,test4096};
329         static int rsa_data_length[RSA_NUM]={
330                 sizeof(test512),sizeof(test1024),
331                 sizeof(test2048),sizeof(test4096)};
332 #endif
333 #ifndef NO_DSA
334         DSA *dsa_key[DSA_NUM];
335         long dsa_c[DSA_NUM][2];
336         double dsa_results[DSA_NUM][2];
337         static unsigned int dsa_bits[DSA_NUM]={512,1024,2048};
338 #endif
339         int rsa_doit[RSA_NUM];
340         int dsa_doit[DSA_NUM];
341         int doit[ALGOR_NUM];
342         int pr_header=0;
343
344         apps_startup();
345 #ifndef NO_DSA
346         memset(dsa_key,0,sizeof(dsa_key));
347 #endif
348
349         if (bio_err == NULL)
350                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
351                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
352
353 #ifndef NO_RSA
354         memset(rsa_key,0,sizeof(rsa_key));
355         for (i=0; i<RSA_NUM; i++)
356                 rsa_key[i]=NULL;
357 #endif
358
359         if ((buf=(unsigned char *)Malloc((int)BUFSIZE)) == NULL)
360                 {
361                 BIO_printf(bio_err,"out of memory\n");
362                 goto end;
363                 }
364         buf_as_des_cblock = (des_cblock *)buf;
365         if ((buf2=(unsigned char *)Malloc((int)BUFSIZE)) == NULL)
366                 {
367                 BIO_printf(bio_err,"out of memory\n");
368                 goto end;
369                 }
370
371         memset(c,0,sizeof(c));
372         memset(iv,0,sizeof(iv));
373
374         for (i=0; i<ALGOR_NUM; i++)
375                 doit[i]=0;
376         for (i=0; i<RSA_NUM; i++)
377                 rsa_doit[i]=0;
378         for (i=0; i<DSA_NUM; i++)
379                 dsa_doit[i]=0;
380         
381         j=0;
382         argc--;
383         argv++;
384         while (argc)
385                 {
386 #ifndef NO_MD2
387                 if      (strcmp(*argv,"md2") == 0) doit[D_MD2]=1;
388                 else
389 #endif
390 #ifndef NO_MDC2
391                         if (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;
392                 else
393 #endif
394 #ifndef NO_MD5
395                         if (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;
396                 else
397 #endif
398 #ifndef NO_MD5
399                         if (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;
400                 else
401 #endif
402 #ifndef NO_SHA
403                         if (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;
404                 else
405                         if (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1;
406                 else
407 #endif
408 #ifndef NO_RIPEMD
409                         if (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1;
410                 else
411                         if (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1;
412                 else
413                         if (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1;
414                 else
415 #endif
416 #ifndef NO_RC4
417                         if (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;
418                 else 
419 #endif
420 #ifndef NO_DEF
421                         if (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1;
422                 else    if (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1;
423                 else
424 #endif
425 #ifndef NO_RSA
426 #ifdef RSAref
427                         if (strcmp(*argv,"rsaref") == 0) 
428                         {
429                         RSA_set_default_method(RSA_PKCS1_RSAref());
430                         j--;
431                         }
432                 else
433 #endif
434                         if (strcmp(*argv,"openssl") == 0) 
435                         {
436                         RSA_set_default_method(RSA_PKCS1_SSLeay());
437                         j--;
438                         }
439                 else
440 #endif /* !NO_RSA */
441                      if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2;
442                 else if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2;
443                 else if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2;
444                 else if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2;
445                 else if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2;
446                 else if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2;
447                 else if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2;
448                 else
449 #ifndef NO_RC2
450                      if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;
451                 else if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;
452                 else
453 #endif
454 #ifndef NO_RC5
455                      if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;
456                 else if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;
457                 else
458 #endif
459 #ifndef NO_IDEA
460                      if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;
461                 else if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;
462                 else
463 #endif
464 #ifndef NO_BF
465                      if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;
466                 else if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;
467                 else if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;
468                 else
469 #endif
470 #ifndef NO_CAST
471                      if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1;
472                 else if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1;
473                 else if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1;
474                 else
475 #endif
476 #ifndef NO_DES
477                         if (strcmp(*argv,"des") == 0)
478                         {
479                         doit[D_CBC_DES]=1;
480                         doit[D_EDE3_DES]=1;
481                         }
482                 else
483 #endif
484 #ifndef NO_RSA
485                         if (strcmp(*argv,"rsa") == 0)
486                         {
487                         rsa_doit[R_RSA_512]=1;
488                         rsa_doit[R_RSA_1024]=1;
489                         rsa_doit[R_RSA_2048]=1;
490                         rsa_doit[R_RSA_4096]=1;
491                         }
492                 else
493 #endif
494 #ifndef NO_DSA
495                         if (strcmp(*argv,"dsa") == 0)
496                         {
497                         dsa_doit[R_DSA_512]=1;
498                         dsa_doit[R_DSA_1024]=1;
499                         }
500                 else
501 #endif
502                         {
503                         BIO_printf(bio_err,"bad value, pick one of\n");
504                         BIO_printf(bio_err,"md2      mdc2       md5      hmac      sha1    rmd160\n");
505 #ifndef NO_IDEA
506                         BIO_printf(bio_err,"idea-cbc ");
507 #endif
508 #ifndef NO_RC2
509                         BIO_printf(bio_err,"rc2-cbc  ");
510 #endif
511 #ifndef NO_RC5
512                         BIO_printf(bio_err,"rc5-cbc  ");
513 #endif
514 #ifndef NO_BF
515                         BIO_printf(bio_err,"bf-cbc");
516 #endif
517 #if !defined(NO_IDEA) && !defined(NO_RC2) && !defined(NO_BF) && !defined(NO_RC5)
518                         BIO_printf(bio_err,"\n");
519 #endif
520                         BIO_printf(bio_err,"des-cbc  des-ede3 ");
521 #ifndef NO_RC4
522                         BIO_printf(bio_err,"rc4");
523 #endif
524 #ifndef NO_RSA
525                         BIO_printf(bio_err,"\nrsa512   rsa1024  rsa2048  rsa4096\n");
526 #endif
527 #ifndef NO_DSA
528                         BIO_printf(bio_err,"\ndsa512   dsa1024  dsa2048\n");
529 #endif
530                         BIO_printf(bio_err,"idea     rc2      des      rsa    blowfish\n");
531                         goto end;
532                         }
533                 argc--;
534                 argv++;
535                 j++;
536                 }
537
538         if (j == 0)
539                 {
540                 for (i=0; i<ALGOR_NUM; i++)
541                         doit[i]=1;
542                 for (i=0; i<RSA_NUM; i++)
543                         rsa_doit[i]=1;
544                 for (i=0; i<DSA_NUM; i++)
545                         dsa_doit[i]=1;
546                 }
547         for (i=0; i<ALGOR_NUM; i++)
548                 if (doit[i]) pr_header++;
549
550 #ifndef TIMES
551         BIO_printf(bio_err,"To get the most accurate results, try to run this\n");
552         BIO_printf(bio_err,"program when this computer is idle.\n");
553 #endif
554
555 #ifndef NO_RSA
556         for (i=0; i<RSA_NUM; i++)
557                 {
558                 unsigned char *p;
559
560                 p=rsa_data[i];
561                 rsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]);
562                 if (rsa_key[i] == NULL)
563                         {
564                         BIO_printf(bio_err,"internal error loading RSA key number %d\n",i);
565                         goto end;
566                         }
567 #if 0
568                 else
569                         {
570                         BIO_printf(bio_err,"Loaded RSA key, %d bit modulus and e= 0x",BN_num_bits(rsa_key[i]->n));
571                         BN_print(bio_err,rsa_key[i]->e);
572                         BIO_printf(bio_err,"\n");
573                         }
574 #endif
575                 }
576 #endif
577
578 #ifndef NO_DSA
579         dsa_key[0]=get_dsa512();
580         dsa_key[1]=get_dsa1024();
581         dsa_key[2]=get_dsa2048();
582 #endif
583
584 #ifndef NO_DES
585         des_set_key(&key,sch);
586         des_set_key(&key2,sch2);
587         des_set_key(&key3,sch3);
588 #endif
589 #ifndef NO_IDEA
590         idea_set_encrypt_key(key16,&idea_ks);
591 #endif
592 #ifndef NO_RC4
593         RC4_set_key(&rc4_ks,16,key16);
594 #endif
595 #ifndef NO_RC2
596         RC2_set_key(&rc2_ks,16,key16,128);
597 #endif
598 #ifndef NO_RC5
599         RC5_32_set_key(&rc5_ks,16,key16,12);
600 #endif
601 #ifndef NO_BF
602         BF_set_key(&bf_ks,16,key16);
603 #endif
604 #ifndef NO_CAST
605         CAST_set_key(&cast_ks,16,key16);
606 #endif
607 #ifndef NO_RSA
608         memset(rsa_c,0,sizeof(rsa_c));
609 #endif
610 #ifndef SIGALRM
611         BIO_printf(bio_err,"First we calculate the approximate speed ...\n");
612         count=10;
613         do      {
614                 long i;
615                 count*=2;
616                 Time_F(START);
617                 for (i=count; i; i--)
618                         des_ecb_encrypt(buf_as_des_cblock,buf_as_des_cblock,
619                                 &(sch[0]),DES_ENCRYPT);
620                 d=Time_F(STOP);
621                 } while (d <3);
622         c[D_MD2][0]=count/10;
623         c[D_MDC2][0]=count/10;
624         c[D_MD5][0]=count;
625         c[D_HMAC][0]=count;
626         c[D_SHA1][0]=count;
627         c[D_RMD160][0]=count;
628         c[D_RC4][0]=count*5;
629         c[D_CBC_DES][0]=count;
630         c[D_EDE3_DES][0]=count/3;
631         c[D_CBC_IDEA][0]=count;
632         c[D_CBC_RC2][0]=count;
633         c[D_CBC_RC5][0]=count;
634         c[D_CBC_BF][0]=count;
635         c[D_CBC_CAST][0]=count;
636
637         for (i=1; i<SIZE_NUM; i++)
638                 {
639                 c[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];
640                 c[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];
641                 c[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];
642                 c[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];
643                 c[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];
644                 c[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];
645                 }
646         for (i=1; i<SIZE_NUM; i++)
647                 {
648                 long l0,l1;
649
650                 l0=(long)lengths[i-1];
651                 l1=(long)lengths[i];
652                 c[D_RC4][i]=c[D_RC4][i-1]*l0/l1;
653                 c[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;
654                 c[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;
655                 c[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;
656                 c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;
657                 c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
658                 c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
659                 c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
660                 }
661 #ifndef NO_RSA
662         rsa_c[R_RSA_512][0]=count/2000;
663         rsa_c[R_RSA_512][1]=count/400;
664         for (i=1; i<RSA_NUM; i++)
665                 {
666                 rsa_c[i][0]=rsa_c[i-1][0]/8;
667                 rsa_c[i][1]=rsa_c[i-1][1]/4;
668                 if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
669                         rsa_doit[i]=0;
670                 else
671                         {
672                         if (rsa_c[i][0] == 0)
673                                 {
674                                 rsa_c[i][0]=1;
675                                 rsa_c[i][1]=20;
676                                 }
677                         }                               
678                 }
679 #endif
680
681         dsa_c[R_DSA_512][0]=count/1000;
682         dsa_c[R_DSA_512][1]=count/1000/2;
683         for (i=1; i<DSA_NUM; i++)
684                 {
685                 dsa_c[i][0]=dsa_c[i-1][0]/4;
686                 dsa_c[i][1]=dsa_c[i-1][1]/4;
687                 if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
688                         dsa_doit[i]=0;
689                 else
690                         {
691                         if (dsa_c[i] == 0)
692                                 {
693                                 dsa_c[i][0]=1;
694                                 dsa_c[i][1]=1;
695                                 }
696                         }                               
697                 }
698
699 #define COND(d) (count < (d))
700 #define COUNT(d) (d)
701 #else
702 #define COND(c) (run)
703 #define COUNT(d) (count)
704         signal(SIGALRM,sig_done);
705 #endif
706
707 #ifndef NO_MD2
708         if (doit[D_MD2])
709                 {
710                 for (j=0; j<SIZE_NUM; j++)
711                         {
712                         print_message(names[D_MD2],c[D_MD2][j],lengths[j]);
713                         Time_F(START);
714                         for (count=0,run=1; COND(c[D_MD2][j]); count++)
715                                 MD2(buf,(unsigned long)lengths[j],&(md2[0]));
716                         d=Time_F(STOP);
717                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
718                                 count,names[D_MD2],d);
719                         results[D_MD2][j]=((double)count)/d*lengths[j];
720                         }
721                 }
722 #endif
723 #ifndef NO_MDC2
724         if (doit[D_MDC2])
725                 {
726                 for (j=0; j<SIZE_NUM; j++)
727                         {
728                         print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);
729                         Time_F(START);
730                         for (count=0,run=1; COND(c[D_MDC2][j]); count++)
731                                 MDC2(buf,(unsigned long)lengths[j],&(mdc2[0]));
732                         d=Time_F(STOP);
733                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
734                                 count,names[D_MDC2],d);
735                         results[D_MDC2][j]=((double)count)/d*lengths[j];
736                         }
737                 }
738 #endif
739
740 #ifndef NO_MD5
741         if (doit[D_MD5])
742                 {
743                 for (j=0; j<SIZE_NUM; j++)
744                         {
745                         print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
746                         Time_F(START);
747                         for (count=0,run=1; COND(c[D_MD5][j]); count++)
748                                 MD5(&(buf[0]),(unsigned long)lengths[j],&(md5[0]));
749                         d=Time_F(STOP);
750                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
751                                 count,names[D_MD5],d);
752                         results[D_MD5][j]=((double)count)/d*lengths[j];
753                         }
754                 }
755 #endif
756
757 #ifndef NO_MD5
758         if (doit[D_HMAC])
759                 {
760                 HMAC_CTX hctx;
761                 HMAC_Init(&hctx,(unsigned char *)"This is a key...",
762                         16,EVP_md5());
763
764                 for (j=0; j<SIZE_NUM; j++)
765                         {
766                         print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);
767                         Time_F(START);
768                         for (count=0,run=1; COND(c[D_HMAC][j]); count++)
769                                 {
770                                 HMAC_Init(&hctx,NULL,0,NULL);
771                                 HMAC_Update(&hctx,buf,lengths[j]);
772                                 HMAC_Final(&hctx,&(hmac[0]),NULL);
773                                 }
774                         d=Time_F(STOP);
775                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
776                                 count,names[D_HMAC],d);
777                         results[D_HMAC][j]=((double)count)/d*lengths[j];
778                         }
779                 }
780 #endif
781 #ifndef NO_SHA
782         if (doit[D_SHA1])
783                 {
784                 for (j=0; j<SIZE_NUM; j++)
785                         {
786                         print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);
787                         Time_F(START);
788                         for (count=0,run=1; COND(c[D_SHA1][j]); count++)
789                                 SHA1(buf,(unsigned long)lengths[j],&(sha[0]));
790                         d=Time_F(STOP);
791                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
792                                 count,names[D_SHA1],d);
793                         results[D_SHA1][j]=((double)count)/d*lengths[j];
794                         }
795                 }
796 #endif
797 #ifndef NO_RIPEMD
798         if (doit[D_RMD160])
799                 {
800                 for (j=0; j<SIZE_NUM; j++)
801                         {
802                         print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);
803                         Time_F(START);
804                         for (count=0,run=1; COND(c[D_RMD160][j]); count++)
805                                 RIPEMD160(buf,(unsigned long)lengths[j],&(rmd160[0]));
806                         d=Time_F(STOP);
807                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
808                                 count,names[D_RMD160],d);
809                         results[D_RMD160][j]=((double)count)/d*lengths[j];
810                         }
811                 }
812 #endif
813 #ifndef NO_RC4
814         if (doit[D_RC4])
815                 {
816                 for (j=0; j<SIZE_NUM; j++)
817                         {
818                         print_message(names[D_RC4],c[D_RC4][j],lengths[j]);
819                         Time_F(START);
820                         for (count=0,run=1; COND(c[D_RC4][j]); count++)
821                                 RC4(&rc4_ks,(unsigned int)lengths[j],
822                                         buf,buf);
823                         d=Time_F(STOP);
824                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
825                                 count,names[D_RC4],d);
826                         results[D_RC4][j]=((double)count)/d*lengths[j];
827                         }
828                 }
829 #endif
830 #ifndef NO_DES
831         if (doit[D_CBC_DES])
832                 {
833                 for (j=0; j<SIZE_NUM; j++)
834                         {
835                         print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);
836                         Time_F(START);
837                         for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
838                                 des_ncbc_encrypt(buf,buf,lengths[j],sch,
839                                                  &iv,DES_ENCRYPT);
840                         d=Time_F(STOP);
841                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
842                                 count,names[D_CBC_DES],d);
843                         results[D_CBC_DES][j]=((double)count)/d*lengths[j];
844                         }
845                 }
846
847         if (doit[D_EDE3_DES])
848                 {
849                 for (j=0; j<SIZE_NUM; j++)
850                         {
851                         print_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);
852                         Time_F(START);
853                         for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
854                                 des_ede3_cbc_encrypt(buf,buf,lengths[j],
855                                                      sch,sch2,sch3,
856                                                      &iv,DES_ENCRYPT);
857                         d=Time_F(STOP);
858                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
859                                 count,names[D_EDE3_DES],d);
860                         results[D_EDE3_DES][j]=((double)count)/d*lengths[j];
861                         }
862                 }
863 #endif
864 #ifndef NO_IDEA
865         if (doit[D_CBC_IDEA])
866                 {
867                 for (j=0; j<SIZE_NUM; j++)
868                         {
869                         print_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);
870                         Time_F(START);
871                         for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)
872                                 idea_cbc_encrypt(buf,buf,
873                                         (unsigned long)lengths[j],&idea_ks,
874                                         iv,IDEA_ENCRYPT);
875                         d=Time_F(STOP);
876                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
877                                 count,names[D_CBC_IDEA],d);
878                         results[D_CBC_IDEA][j]=((double)count)/d*lengths[j];
879                         }
880                 }
881 #endif
882 #ifndef NO_RC2
883         if (doit[D_CBC_RC2])
884                 {
885                 for (j=0; j<SIZE_NUM; j++)
886                         {
887                         print_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);
888                         Time_F(START);
889                         for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)
890                                 RC2_cbc_encrypt(buf,buf,
891                                         (unsigned long)lengths[j],&rc2_ks,
892                                         iv,RC2_ENCRYPT);
893                         d=Time_F(STOP);
894                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
895                                 count,names[D_CBC_RC2],d);
896                         results[D_CBC_RC2][j]=((double)count)/d*lengths[j];
897                         }
898                 }
899 #endif
900 #ifndef NO_RC5
901         if (doit[D_CBC_RC5])
902                 {
903                 for (j=0; j<SIZE_NUM; j++)
904                         {
905                         print_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);
906                         Time_F(START);
907                         for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)
908                                 RC5_32_cbc_encrypt(buf,buf,
909                                         (unsigned long)lengths[j],&rc5_ks,
910                                         iv,RC5_ENCRYPT);
911                         d=Time_F(STOP);
912                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
913                                 count,names[D_CBC_RC5],d);
914                         results[D_CBC_RC5][j]=((double)count)/d*lengths[j];
915                         }
916                 }
917 #endif
918 #ifndef NO_BF
919         if (doit[D_CBC_BF])
920                 {
921                 for (j=0; j<SIZE_NUM; j++)
922                         {
923                         print_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);
924                         Time_F(START);
925                         for (count=0,run=1; COND(c[D_CBC_BF][j]); count++)
926                                 BF_cbc_encrypt(buf,buf,
927                                         (unsigned long)lengths[j],&bf_ks,
928                                         iv,BF_ENCRYPT);
929                         d=Time_F(STOP);
930                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
931                                 count,names[D_CBC_BF],d);
932                         results[D_CBC_BF][j]=((double)count)/d*lengths[j];
933                         }
934                 }
935 #endif
936 #ifndef NO_CAST
937         if (doit[D_CBC_CAST])
938                 {
939                 for (j=0; j<SIZE_NUM; j++)
940                         {
941                         print_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);
942                         Time_F(START);
943                         for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)
944                                 CAST_cbc_encrypt(buf,buf,
945                                         (unsigned long)lengths[j],&cast_ks,
946                                         iv,CAST_ENCRYPT);
947                         d=Time_F(STOP);
948                         BIO_printf(bio_err,"%ld %s's in %.2fs\n",
949                                 count,names[D_CBC_CAST],d);
950                         results[D_CBC_CAST][j]=((double)count)/d*lengths[j];
951                         }
952                 }
953 #endif
954
955         RAND_bytes(buf,30);
956 #ifndef NO_RSA
957         for (j=0; j<RSA_NUM; j++)
958                 {
959                 if (!rsa_doit[j]) continue;
960                 rsa_num=RSA_private_encrypt(30,buf,buf2,rsa_key[j],
961                         RSA_PKCS1_PADDING);
962                 pkey_print_message("private","rsa",rsa_c[j][0],rsa_bits[j],
963                         RSA_SECONDS);
964 /*              RSA_blinding_on(rsa_key[j],NULL); */
965                 Time_F(START);
966                 for (count=0,run=1; COND(rsa_c[j][0]); count++)
967                         {
968                         rsa_num=RSA_private_encrypt(30,buf,buf2,rsa_key[j],
969                                 RSA_PKCS1_PADDING);
970                         if (rsa_num <= 0)
971                                 {
972                                 BIO_printf(bio_err,"RSA private encrypt failure\n");
973                                 ERR_print_errors(bio_err);
974                                 count=1;
975                                 break;
976                                 }
977                         }
978                 d=Time_F(STOP);
979                 BIO_printf(bio_err,"%ld %d bit private RSA's in %.2fs\n",
980                         count,rsa_bits[j],d);
981                 rsa_results[j][0]=d/(double)count;
982                 rsa_count=count;
983
984 #if 1
985                 rsa_num2=RSA_public_decrypt(rsa_num,buf2,buf,rsa_key[j],
986                         RSA_PKCS1_PADDING);
987                 pkey_print_message("public","rsa",rsa_c[j][1],rsa_bits[j],
988                         RSA_SECONDS);
989                 Time_F(START);
990                 for (count=0,run=1; COND(rsa_c[j][1]); count++)
991                         {
992                         rsa_num2=RSA_public_decrypt(rsa_num,buf2,buf,rsa_key[j],
993                                 RSA_PKCS1_PADDING);
994                         if (rsa_num2 <= 0)
995                                 {
996                                 BIO_printf(bio_err,"RSA public encrypt failure\n");
997                                 ERR_print_errors(bio_err);
998                                 count=1;
999                                 break;
1000                                 }
1001                         }
1002                 d=Time_F(STOP);
1003                 BIO_printf(bio_err,"%ld %d bit public RSA's in %.2fs\n",
1004                         count,rsa_bits[j],d);
1005                 rsa_results[j][1]=d/(double)count;
1006 #endif
1007
1008                 if (rsa_count <= 1)
1009                         {
1010                         /* if longer than 10s, don't do any more */
1011                         for (j++; j<RSA_NUM; j++)
1012                                 rsa_doit[j]=0;
1013                         }
1014                 }
1015 #endif
1016
1017         RAND_bytes(buf,20);
1018 #ifndef NO_DSA
1019         for (j=0; j<DSA_NUM; j++)
1020                 {
1021                 unsigned int kk;
1022
1023                 if (!dsa_doit[j]) continue;
1024                 DSA_generate_key(dsa_key[j]);
1025 /*              DSA_sign_setup(dsa_key[j],NULL); */
1026                 rsa_num=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1027                         &kk,dsa_key[j]);
1028                 pkey_print_message("sign","dsa",dsa_c[j][0],dsa_bits[j],
1029                         DSA_SECONDS);
1030                 Time_F(START);
1031                 for (count=0,run=1; COND(dsa_c[j][0]); count++)
1032                         {
1033                         rsa_num=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1034                                 &kk,dsa_key[j]);
1035                         if (rsa_num <= 0)
1036                                 {
1037                                 BIO_printf(bio_err,"DSA sign failure\n");
1038                                 ERR_print_errors(bio_err);
1039                                 count=1;
1040                                 break;
1041                                 }
1042                         }
1043                 d=Time_F(STOP);
1044                 BIO_printf(bio_err,"%ld %d bit DSA signs in %.2fs\n",
1045                         count,dsa_bits[j],d);
1046                 dsa_results[j][0]=d/(double)count;
1047                 rsa_count=count;
1048
1049                 rsa_num2=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1050                         kk,dsa_key[j]);
1051                 pkey_print_message("verify","dsa",dsa_c[j][1],dsa_bits[j],
1052                         DSA_SECONDS);
1053                 Time_F(START);
1054                 for (count=0,run=1; COND(dsa_c[j][1]); count++)
1055                         {
1056                         rsa_num2=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1057                                 kk,dsa_key[j]);
1058                         if (rsa_num2 <= 0)
1059                                 {
1060                                 BIO_printf(bio_err,"DSA verify failure\n");
1061                                 ERR_print_errors(bio_err);
1062                                 count=1;
1063                                 break;
1064                                 }
1065                         }
1066                 d=Time_F(STOP);
1067                 BIO_printf(bio_err,"%ld %d bit DSA verify in %.2fs\n",
1068                         count,dsa_bits[j],d);
1069                 dsa_results[j][1]=d/(double)count;
1070
1071                 if (rsa_count <= 1)
1072                         {
1073                         /* if longer than 10s, don't do any more */
1074                         for (j++; j<DSA_NUM; j++)
1075                                 dsa_doit[j]=0;
1076                         }
1077                 }
1078 #endif
1079
1080         fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION));
1081         fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON));
1082         printf("options:");
1083         printf("%s ",BN_options());
1084 #ifndef NO_MD2
1085         printf("%s ",MD2_options());
1086 #endif
1087 #ifndef NO_RC4
1088         printf("%s ",RC4_options());
1089 #endif
1090 #ifndef NO_DES
1091         printf("%s ",des_options());
1092 #endif
1093 #ifndef NO_IDEA
1094         printf("%s ",idea_options());
1095 #endif
1096 #ifndef NO_BF
1097         printf("%s ",BF_options());
1098 #endif
1099         fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
1100
1101         if (pr_header)
1102                 {
1103                 fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n"); 
1104                 fprintf(stdout,"type        ");
1105                 for (j=0;  j<SIZE_NUM; j++)
1106                         fprintf(stdout,"%7d bytes",lengths[j]);
1107                 fprintf(stdout,"\n");
1108                 }
1109
1110         for (k=0; k<ALGOR_NUM; k++)
1111                 {
1112                 if (!doit[k]) continue;
1113                 fprintf(stdout,"%-13s",names[k]);
1114                 for (j=0; j<SIZE_NUM; j++)
1115                         {
1116                         if (results[k][j] > 10000)
1117                                 fprintf(stdout," %11.2fk",results[k][j]/1e3);
1118                         else
1119                                 fprintf(stdout," %11.2f ",results[k][j]);
1120                         }
1121                 fprintf(stdout,"\n");
1122                 }
1123 #ifndef NO_RSA
1124         j=1;
1125         for (k=0; k<RSA_NUM; k++)
1126                 {
1127                 if (!rsa_doit[k]) continue;
1128                 if (j)
1129                         {
1130                         printf("%18ssign    verify    sign/s verify/s\n"," ");
1131                         j=0;
1132                         }
1133                 fprintf(stdout,"rsa %4d bits %8.4fs %8.4fs %8.1f %8.1f",
1134                         rsa_bits[k],rsa_results[k][0],rsa_results[k][1],
1135                         1.0/rsa_results[k][0],1.0/rsa_results[k][1]);
1136                 fprintf(stdout,"\n");
1137                 }
1138 #endif
1139 #ifndef NO_DSA
1140         j=1;
1141         for (k=0; k<DSA_NUM; k++)
1142                 {
1143                 if (!dsa_doit[k]) continue;
1144                 if (j)  {
1145                         printf("%18ssign    verify    sign/s verify/s\n"," ");
1146                         j=0;
1147                         }
1148                 fprintf(stdout,"dsa %4d bits %8.4fs %8.4fs %8.1f %8.1f",
1149                         dsa_bits[k],dsa_results[k][0],dsa_results[k][1],
1150                         1.0/dsa_results[k][0],1.0/dsa_results[k][1]);
1151                 fprintf(stdout,"\n");
1152                 }
1153 #endif
1154         ret=0;
1155 end:
1156         if (buf != NULL) Free(buf);
1157         if (buf2 != NULL) Free(buf2);
1158 #ifndef NO_RSA
1159         for (i=0; i<RSA_NUM; i++)
1160                 if (rsa_key[i] != NULL)
1161                         RSA_free(rsa_key[i]);
1162 #endif
1163 #ifndef NO_DSA
1164         for (i=0; i<DSA_NUM; i++)
1165                 if (dsa_key[i] != NULL)
1166                         DSA_free(dsa_key[i]);
1167 #endif
1168         EXIT(ret);
1169         }
1170
1171 static void print_message(char *s, long num, int length)
1172         {
1173 #ifdef SIGALRM
1174         BIO_printf(bio_err,"Doing %s for %ds on %d size blocks: ",s,SECONDS,length);
1175         BIO_flush(bio_err);
1176         alarm(SECONDS);
1177 #else
1178         BIO_printf(bio_err,"Doing %s %ld times on %d size blocks: ",s,num,length);
1179         BIO_flush(bio_err);
1180 #endif
1181 #ifdef LINT
1182         num=num;
1183 #endif
1184         }
1185
1186 static void pkey_print_message(char *str, char *str2, long num, int bits,
1187              int tm)
1188         {
1189 #ifdef SIGALRM
1190         BIO_printf(bio_err,"Doing %d bit %s %s's for %ds: ",bits,str,str2,tm);
1191         BIO_flush(bio_err);
1192         alarm(RSA_SECONDS);
1193 #else
1194         BIO_printf(bio_err,"Doing %ld %d bit %s %s's: ",num,bits,str,str2);
1195         BIO_flush(bio_err);
1196 #endif
1197 #ifdef LINT
1198         num=num;
1199 #endif
1200         }
1201