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