speed.c revision 57511
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 57511 2000-02-26 13:13:03Z peter $
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			if (strcmp(*argv,"openssl") == 0)
433			{
434			RSA_set_default_method(RSA_PKCS1());
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_as_des_cblock,buf_as_des_cblock,
617				&(sch[0]),DES_ENCRYPT);
618		d=Time_F(STOP);
619		} while (d <3);
620	c[D_MD2][0]=count/10;
621	c[D_MDC2][0]=count/10;
622	c[D_MD5][0]=count;
623	c[D_HMAC][0]=count;
624	c[D_SHA1][0]=count;
625	c[D_RMD160][0]=count;
626	c[D_RC4][0]=count*5;
627	c[D_CBC_DES][0]=count;
628	c[D_EDE3_DES][0]=count/3;
629	c[D_CBC_IDEA][0]=count;
630	c[D_CBC_RC2][0]=count;
631	c[D_CBC_RC5][0]=count;
632	c[D_CBC_BF][0]=count;
633	c[D_CBC_CAST][0]=count;
634
635	for (i=1; i<SIZE_NUM; i++)
636		{
637		c[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];
638		c[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];
639		c[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];
640		c[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];
641		c[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];
642		c[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];
643		}
644	for (i=1; i<SIZE_NUM; i++)
645		{
646		long l0,l1;
647
648		l0=(long)lengths[i-1];
649		l1=(long)lengths[i];
650		c[D_RC4][i]=c[D_RC4][i-1]*l0/l1;
651		c[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;
652		c[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;
653		c[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;
654		c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;
655		c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
656		c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
657		c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
658		}
659#ifndef NO_RSA
660	rsa_c[R_RSA_512][0]=count/2000;
661	rsa_c[R_RSA_512][1]=count/400;
662	for (i=1; i<RSA_NUM; i++)
663		{
664		rsa_c[i][0]=rsa_c[i-1][0]/8;
665		rsa_c[i][1]=rsa_c[i-1][1]/4;
666		if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
667			rsa_doit[i]=0;
668		else
669			{
670			if (rsa_c[i][0] == 0)
671				{
672				rsa_c[i][0]=1;
673				rsa_c[i][1]=20;
674				}
675			}
676		}
677#endif
678
679	dsa_c[R_DSA_512][0]=count/1000;
680	dsa_c[R_DSA_512][1]=count/1000/2;
681	for (i=1; i<DSA_NUM; i++)
682		{
683		dsa_c[i][0]=dsa_c[i-1][0]/4;
684		dsa_c[i][1]=dsa_c[i-1][1]/4;
685		if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
686			dsa_doit[i]=0;
687		else
688			{
689			if (dsa_c[i] == 0)
690				{
691				dsa_c[i][0]=1;
692				dsa_c[i][1]=1;
693				}
694			}
695		}
696
697#define COND(d)	(count < (d))
698#define COUNT(d) (d)
699#else
700#define COND(c)	(run)
701#define COUNT(d) (count)
702	signal(SIGALRM,sig_done);
703#endif
704
705#ifndef NO_MD2
706	if (doit[D_MD2])
707		{
708		for (j=0; j<SIZE_NUM; j++)
709			{
710			print_message(names[D_MD2],c[D_MD2][j],lengths[j]);
711			Time_F(START);
712			for (count=0,run=1; COND(c[D_MD2][j]); count++)
713				MD2(buf,(unsigned long)lengths[j],&(md2[0]));
714			d=Time_F(STOP);
715			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
716				count,names[D_MD2],d);
717			results[D_MD2][j]=((double)count)/d*lengths[j];
718			}
719		}
720#endif
721#ifndef NO_MDC2
722	if (doit[D_MDC2])
723		{
724		for (j=0; j<SIZE_NUM; j++)
725			{
726			print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);
727			Time_F(START);
728			for (count=0,run=1; COND(c[D_MDC2][j]); count++)
729				MDC2(buf,(unsigned long)lengths[j],&(mdc2[0]));
730			d=Time_F(STOP);
731			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
732				count,names[D_MDC2],d);
733			results[D_MDC2][j]=((double)count)/d*lengths[j];
734			}
735		}
736#endif
737
738#ifndef NO_MD5
739	if (doit[D_MD5])
740		{
741		for (j=0; j<SIZE_NUM; j++)
742			{
743			print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
744			Time_F(START);
745			for (count=0,run=1; COND(c[D_MD5][j]); count++)
746				MD5(&(buf[0]),(unsigned long)lengths[j],&(md5[0]));
747			d=Time_F(STOP);
748			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
749				count,names[D_MD5],d);
750			results[D_MD5][j]=((double)count)/d*lengths[j];
751			}
752		}
753#endif
754
755#if !defined(NO_MD5) && !defined(NO_HMAC)
756	if (doit[D_HMAC])
757		{
758		HMAC_CTX hctx;
759		HMAC_Init(&hctx,(unsigned char *)"This is a key...",
760			16,EVP_md5());
761
762		for (j=0; j<SIZE_NUM; j++)
763			{
764			print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);
765			Time_F(START);
766			for (count=0,run=1; COND(c[D_HMAC][j]); count++)
767				{
768				HMAC_Init(&hctx,NULL,0,NULL);
769                                HMAC_Update(&hctx,buf,lengths[j]);
770                                HMAC_Final(&hctx,&(hmac[0]),NULL);
771				}
772			d=Time_F(STOP);
773			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
774				count,names[D_HMAC],d);
775			results[D_HMAC][j]=((double)count)/d*lengths[j];
776			}
777		}
778#endif
779#ifndef NO_SHA
780	if (doit[D_SHA1])
781		{
782		for (j=0; j<SIZE_NUM; j++)
783			{
784			print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);
785			Time_F(START);
786			for (count=0,run=1; COND(c[D_SHA1][j]); count++)
787				SHA1(buf,(unsigned long)lengths[j],&(sha[0]));
788			d=Time_F(STOP);
789			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
790				count,names[D_SHA1],d);
791			results[D_SHA1][j]=((double)count)/d*lengths[j];
792			}
793		}
794#endif
795#ifndef NO_RIPEMD
796	if (doit[D_RMD160])
797		{
798		for (j=0; j<SIZE_NUM; j++)
799			{
800			print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);
801			Time_F(START);
802			for (count=0,run=1; COND(c[D_RMD160][j]); count++)
803				RIPEMD160(buf,(unsigned long)lengths[j],&(rmd160[0]));
804			d=Time_F(STOP);
805			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
806				count,names[D_RMD160],d);
807			results[D_RMD160][j]=((double)count)/d*lengths[j];
808			}
809		}
810#endif
811#ifndef NO_RC4
812	if (doit[D_RC4])
813		{
814		for (j=0; j<SIZE_NUM; j++)
815			{
816			print_message(names[D_RC4],c[D_RC4][j],lengths[j]);
817			Time_F(START);
818			for (count=0,run=1; COND(c[D_RC4][j]); count++)
819				RC4(&rc4_ks,(unsigned int)lengths[j],
820					buf,buf);
821			d=Time_F(STOP);
822			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
823				count,names[D_RC4],d);
824			results[D_RC4][j]=((double)count)/d*lengths[j];
825			}
826		}
827#endif
828#ifndef NO_DES
829	if (doit[D_CBC_DES])
830		{
831		for (j=0; j<SIZE_NUM; j++)
832			{
833			print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);
834			Time_F(START);
835			for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
836				des_ncbc_encrypt(buf,buf,lengths[j],sch,
837						 &iv,DES_ENCRYPT);
838			d=Time_F(STOP);
839			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
840				count,names[D_CBC_DES],d);
841			results[D_CBC_DES][j]=((double)count)/d*lengths[j];
842			}
843		}
844
845	if (doit[D_EDE3_DES])
846		{
847		for (j=0; j<SIZE_NUM; j++)
848			{
849			print_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);
850			Time_F(START);
851			for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
852				des_ede3_cbc_encrypt(buf,buf,lengths[j],
853						     sch,sch2,sch3,
854						     &iv,DES_ENCRYPT);
855			d=Time_F(STOP);
856			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
857				count,names[D_EDE3_DES],d);
858			results[D_EDE3_DES][j]=((double)count)/d*lengths[j];
859			}
860		}
861#endif
862#ifndef NO_IDEA
863	if (doit[D_CBC_IDEA])
864		{
865		for (j=0; j<SIZE_NUM; j++)
866			{
867			print_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);
868			Time_F(START);
869			for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)
870				idea_cbc_encrypt(buf,buf,
871					(unsigned long)lengths[j],&idea_ks,
872					iv,IDEA_ENCRYPT);
873			d=Time_F(STOP);
874			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
875				count,names[D_CBC_IDEA],d);
876			results[D_CBC_IDEA][j]=((double)count)/d*lengths[j];
877			}
878		}
879#endif
880#ifndef NO_RC2
881	if (doit[D_CBC_RC2])
882		{
883		for (j=0; j<SIZE_NUM; j++)
884			{
885			print_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);
886			Time_F(START);
887			for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)
888				RC2_cbc_encrypt(buf,buf,
889					(unsigned long)lengths[j],&rc2_ks,
890					iv,RC2_ENCRYPT);
891			d=Time_F(STOP);
892			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
893				count,names[D_CBC_RC2],d);
894			results[D_CBC_RC2][j]=((double)count)/d*lengths[j];
895			}
896		}
897#endif
898#ifndef NO_RC5
899	if (doit[D_CBC_RC5])
900		{
901		for (j=0; j<SIZE_NUM; j++)
902			{
903			print_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);
904			Time_F(START);
905			for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)
906				RC5_32_cbc_encrypt(buf,buf,
907					(unsigned long)lengths[j],&rc5_ks,
908					iv,RC5_ENCRYPT);
909			d=Time_F(STOP);
910			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
911				count,names[D_CBC_RC5],d);
912			results[D_CBC_RC5][j]=((double)count)/d*lengths[j];
913			}
914		}
915#endif
916#ifndef NO_BF
917	if (doit[D_CBC_BF])
918		{
919		for (j=0; j<SIZE_NUM; j++)
920			{
921			print_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);
922			Time_F(START);
923			for (count=0,run=1; COND(c[D_CBC_BF][j]); count++)
924				BF_cbc_encrypt(buf,buf,
925					(unsigned long)lengths[j],&bf_ks,
926					iv,BF_ENCRYPT);
927			d=Time_F(STOP);
928			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
929				count,names[D_CBC_BF],d);
930			results[D_CBC_BF][j]=((double)count)/d*lengths[j];
931			}
932		}
933#endif
934#ifndef NO_CAST
935	if (doit[D_CBC_CAST])
936		{
937		for (j=0; j<SIZE_NUM; j++)
938			{
939			print_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);
940			Time_F(START);
941			for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)
942				CAST_cbc_encrypt(buf,buf,
943					(unsigned long)lengths[j],&cast_ks,
944					iv,CAST_ENCRYPT);
945			d=Time_F(STOP);
946			BIO_printf(bio_err,"%ld %s's in %.2fs\n",
947				count,names[D_CBC_CAST],d);
948			results[D_CBC_CAST][j]=((double)count)/d*lengths[j];
949			}
950		}
951#endif
952
953	RAND_bytes(buf,30);
954#ifndef NO_RSA
955	for (j=0; j<RSA_NUM; j++)
956		{
957		if (!rsa_doit[j]) continue;
958		rsa_num=RSA_private_encrypt(30,buf,buf2,rsa_key[j],
959			RSA_PKCS1_PADDING);
960		pkey_print_message("private","rsa",rsa_c[j][0],rsa_bits[j],
961			RSA_SECONDS);
962/*		RSA_blinding_on(rsa_key[j],NULL); */
963		Time_F(START);
964		for (count=0,run=1; COND(rsa_c[j][0]); count++)
965			{
966			rsa_num=RSA_private_encrypt(30,buf,buf2,rsa_key[j],
967				RSA_PKCS1_PADDING);
968			if (rsa_num <= 0)
969				{
970				BIO_printf(bio_err,"RSA private encrypt failure\n");
971				ERR_print_errors(bio_err);
972				count=1;
973				break;
974				}
975			}
976		d=Time_F(STOP);
977		BIO_printf(bio_err,"%ld %d bit private RSA's in %.2fs\n",
978			count,rsa_bits[j],d);
979		rsa_results[j][0]=d/(double)count;
980		rsa_count=count;
981
982#if 1
983		rsa_num2=RSA_public_decrypt(rsa_num,buf2,buf,rsa_key[j],
984			RSA_PKCS1_PADDING);
985		pkey_print_message("public","rsa",rsa_c[j][1],rsa_bits[j],
986			RSA_SECONDS);
987		Time_F(START);
988		for (count=0,run=1; COND(rsa_c[j][1]); count++)
989			{
990			rsa_num2=RSA_public_decrypt(rsa_num,buf2,buf,rsa_key[j],
991				RSA_PKCS1_PADDING);
992			if (rsa_num2 <= 0)
993				{
994				BIO_printf(bio_err,"RSA public encrypt failure\n");
995				ERR_print_errors(bio_err);
996				count=1;
997				break;
998				}
999			}
1000		d=Time_F(STOP);
1001		BIO_printf(bio_err,"%ld %d bit public RSA's in %.2fs\n",
1002			count,rsa_bits[j],d);
1003		rsa_results[j][1]=d/(double)count;
1004#endif
1005
1006		if (rsa_count <= 1)
1007			{
1008			/* if longer than 10s, don't do any more */
1009			for (j++; j<RSA_NUM; j++)
1010				rsa_doit[j]=0;
1011			}
1012		}
1013#endif
1014
1015	RAND_bytes(buf,20);
1016#ifndef NO_DSA
1017	for (j=0; j<DSA_NUM; j++)
1018		{
1019		unsigned int kk;
1020
1021		if (!dsa_doit[j]) continue;
1022		DSA_generate_key(dsa_key[j]);
1023/*		DSA_sign_setup(dsa_key[j],NULL); */
1024		rsa_num=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1025			&kk,dsa_key[j]);
1026		pkey_print_message("sign","dsa",dsa_c[j][0],dsa_bits[j],
1027			DSA_SECONDS);
1028		Time_F(START);
1029		for (count=0,run=1; COND(dsa_c[j][0]); count++)
1030			{
1031			rsa_num=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1032				&kk,dsa_key[j]);
1033			if (rsa_num <= 0)
1034				{
1035				BIO_printf(bio_err,"DSA sign failure\n");
1036				ERR_print_errors(bio_err);
1037				count=1;
1038				break;
1039				}
1040			}
1041		d=Time_F(STOP);
1042		BIO_printf(bio_err,"%ld %d bit DSA signs in %.2fs\n",
1043			count,dsa_bits[j],d);
1044		dsa_results[j][0]=d/(double)count;
1045		rsa_count=count;
1046
1047		rsa_num2=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1048			kk,dsa_key[j]);
1049		pkey_print_message("verify","dsa",dsa_c[j][1],dsa_bits[j],
1050			DSA_SECONDS);
1051		Time_F(START);
1052		for (count=0,run=1; COND(dsa_c[j][1]); count++)
1053			{
1054			rsa_num2=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1055				kk,dsa_key[j]);
1056			if (rsa_num2 <= 0)
1057				{
1058				BIO_printf(bio_err,"DSA verify failure\n");
1059				ERR_print_errors(bio_err);
1060				count=1;
1061				break;
1062				}
1063			}
1064		d=Time_F(STOP);
1065		BIO_printf(bio_err,"%ld %d bit DSA verify in %.2fs\n",
1066			count,dsa_bits[j],d);
1067		dsa_results[j][1]=d/(double)count;
1068
1069		if (rsa_count <= 1)
1070			{
1071			/* if longer than 10s, don't do any more */
1072			for (j++; j<DSA_NUM; j++)
1073				dsa_doit[j]=0;
1074			}
1075		}
1076#endif
1077
1078	fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION));
1079        fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON));
1080	printf("options:");
1081	printf("%s ",BN_options());
1082#ifndef NO_MD2
1083	printf("%s ",MD2_options());
1084#endif
1085#ifndef NO_RC4
1086	printf("%s ",RC4_options());
1087#endif
1088#ifndef NO_DES
1089	printf("%s ",des_options());
1090#endif
1091#ifndef NO_IDEA
1092	printf("%s ",idea_options());
1093#endif
1094#ifndef NO_BF
1095	printf("%s ",BF_options());
1096#endif
1097	fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
1098
1099	if (pr_header)
1100		{
1101		fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n");
1102		fprintf(stdout,"type        ");
1103		for (j=0;  j<SIZE_NUM; j++)
1104			fprintf(stdout,"%7d bytes",lengths[j]);
1105		fprintf(stdout,"\n");
1106		}
1107
1108	for (k=0; k<ALGOR_NUM; k++)
1109		{
1110		if (!doit[k]) continue;
1111		fprintf(stdout,"%-13s",names[k]);
1112		for (j=0; j<SIZE_NUM; j++)
1113			{
1114			if (results[k][j] > 10000)
1115				fprintf(stdout," %11.2fk",results[k][j]/1e3);
1116			else
1117				fprintf(stdout," %11.2f ",results[k][j]);
1118			}
1119		fprintf(stdout,"\n");
1120		}
1121#ifndef NO_RSA
1122	j=1;
1123	for (k=0; k<RSA_NUM; k++)
1124		{
1125		if (!rsa_doit[k]) continue;
1126		if (j)
1127			{
1128			printf("%18ssign    verify    sign/s verify/s\n"," ");
1129			j=0;
1130			}
1131		fprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f",
1132			rsa_bits[k],rsa_results[k][0],rsa_results[k][1],
1133			1.0/rsa_results[k][0],1.0/rsa_results[k][1]);
1134		fprintf(stdout,"\n");
1135		}
1136#endif
1137#ifndef NO_DSA
1138	j=1;
1139	for (k=0; k<DSA_NUM; k++)
1140		{
1141		if (!dsa_doit[k]) continue;
1142		if (j)	{
1143			printf("%18ssign    verify    sign/s verify/s\n"," ");
1144			j=0;
1145			}
1146		fprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f",
1147			dsa_bits[k],dsa_results[k][0],dsa_results[k][1],
1148			1.0/dsa_results[k][0],1.0/dsa_results[k][1]);
1149		fprintf(stdout,"\n");
1150		}
1151#endif
1152	ret=0;
1153end:
1154	if (buf != NULL) Free(buf);
1155	if (buf2 != NULL) Free(buf2);
1156#ifndef NO_RSA
1157	for (i=0; i<RSA_NUM; i++)
1158		if (rsa_key[i] != NULL)
1159			RSA_free(rsa_key[i]);
1160#endif
1161#ifndef NO_DSA
1162	for (i=0; i<DSA_NUM; i++)
1163		if (dsa_key[i] != NULL)
1164			DSA_free(dsa_key[i]);
1165#endif
1166	EXIT(ret);
1167	}
1168
1169static void print_message(char *s, long num, int length)
1170	{
1171#ifdef SIGALRM
1172	BIO_printf(bio_err,"Doing %s for %ds on %d size blocks: ",s,SECONDS,length);
1173	(void)BIO_flush(bio_err);
1174	alarm(SECONDS);
1175#else
1176	BIO_printf(bio_err,"Doing %s %ld times on %d size blocks: ",s,num,length);
1177	(void)BIO_flush(bio_err);
1178#endif
1179#ifdef LINT
1180	num=num;
1181#endif
1182	}
1183
1184static void pkey_print_message(char *str, char *str2, long num, int bits,
1185	     int tm)
1186	{
1187#ifdef SIGALRM
1188	BIO_printf(bio_err,"Doing %d bit %s %s's for %ds: ",bits,str,str2,tm);
1189	(void)BIO_flush(bio_err);
1190	alarm(RSA_SECONDS);
1191#else
1192	BIO_printf(bio_err,"Doing %ld %d bit %s %s's: ",num,bits,str,str2);
1193	(void)BIO_flush(bio_err);
1194#endif
1195#ifdef LINT
1196	num=num;
1197#endif
1198	}
1199
1200