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