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