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