xref: /onnv-gate/usr/src/common/openssl/crypto/des/speed.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/des/speed.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
600Sstevel@tonic-gate /* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
630Sstevel@tonic-gate #define TIMES
640Sstevel@tonic-gate #endif
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <stdio.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <openssl/e_os2.h>
690Sstevel@tonic-gate #include OPENSSL_UNISTD_IO
700Sstevel@tonic-gate OPENSSL_DECLARE_EXIT
710Sstevel@tonic-gate 
72*2139Sjp161948 #ifndef OPENSSL_SYS_NETWARE
730Sstevel@tonic-gate #include <signal.h>
74*2139Sjp161948 #define crypt(c,s) (des_crypt((c),(s)))
75*2139Sjp161948 #endif
76*2139Sjp161948 
770Sstevel@tonic-gate #ifndef _IRIX
780Sstevel@tonic-gate #include <time.h>
790Sstevel@tonic-gate #endif
800Sstevel@tonic-gate #ifdef TIMES
810Sstevel@tonic-gate #include <sys/types.h>
820Sstevel@tonic-gate #include <sys/times.h>
830Sstevel@tonic-gate #endif
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* Depending on the VMS version, the tms structure is perhaps defined.
860Sstevel@tonic-gate    The __TMS macro will show if it was.  If it wasn't defined, we should
870Sstevel@tonic-gate    undefine TIMES, since that tells the rest of the program how things
880Sstevel@tonic-gate    should be handled.				-- Richard Levitte */
890Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
900Sstevel@tonic-gate #undef TIMES
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #ifndef TIMES
940Sstevel@tonic-gate #include <sys/timeb.h>
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #if defined(sun) || defined(__ultrix)
980Sstevel@tonic-gate #define _POSIX_SOURCE
990Sstevel@tonic-gate #include <limits.h>
1000Sstevel@tonic-gate #include <sys/param.h>
1010Sstevel@tonic-gate #endif
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #include <openssl/des.h>
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* The following if from times(3) man page.  It may need to be changed */
1060Sstevel@tonic-gate #ifndef HZ
1070Sstevel@tonic-gate # ifndef CLK_TCK
1080Sstevel@tonic-gate #  ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
1090Sstevel@tonic-gate #   define HZ	100.0
1100Sstevel@tonic-gate #  else /* _BSD_CLK_TCK_ */
1110Sstevel@tonic-gate #   define HZ ((double)_BSD_CLK_TCK_)
1120Sstevel@tonic-gate #  endif
1130Sstevel@tonic-gate # else /* CLK_TCK */
1140Sstevel@tonic-gate #  define HZ ((double)CLK_TCK)
1150Sstevel@tonic-gate # endif
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #define BUFSIZE	((long)1024)
1190Sstevel@tonic-gate long run=0;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate double Time_F(int s);
1220Sstevel@tonic-gate #ifdef SIGALRM
1230Sstevel@tonic-gate #if defined(__STDC__) || defined(sgi) || defined(_AIX)
1240Sstevel@tonic-gate #define SIGRETTYPE void
1250Sstevel@tonic-gate #else
1260Sstevel@tonic-gate #define SIGRETTYPE int
1270Sstevel@tonic-gate #endif
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate SIGRETTYPE sig_done(int sig);
sig_done(int sig)1300Sstevel@tonic-gate SIGRETTYPE sig_done(int sig)
1310Sstevel@tonic-gate 	{
1320Sstevel@tonic-gate 	signal(SIGALRM,sig_done);
1330Sstevel@tonic-gate 	run=0;
1340Sstevel@tonic-gate #ifdef LINT
1350Sstevel@tonic-gate 	sig=sig;
1360Sstevel@tonic-gate #endif
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #define START	0
1410Sstevel@tonic-gate #define STOP	1
1420Sstevel@tonic-gate 
Time_F(int s)1430Sstevel@tonic-gate double Time_F(int s)
1440Sstevel@tonic-gate 	{
1450Sstevel@tonic-gate 	double ret;
1460Sstevel@tonic-gate #ifdef TIMES
1470Sstevel@tonic-gate 	static struct tms tstart,tend;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (s == START)
1500Sstevel@tonic-gate 		{
1510Sstevel@tonic-gate 		times(&tstart);
1520Sstevel@tonic-gate 		return(0);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	else
1550Sstevel@tonic-gate 		{
1560Sstevel@tonic-gate 		times(&tend);
1570Sstevel@tonic-gate 		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
1580Sstevel@tonic-gate 		return((ret == 0.0)?1e-6:ret);
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate #else /* !times() */
1610Sstevel@tonic-gate 	static struct timeb tstart,tend;
1620Sstevel@tonic-gate 	long i;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (s == START)
1650Sstevel@tonic-gate 		{
1660Sstevel@tonic-gate 		ftime(&tstart);
1670Sstevel@tonic-gate 		return(0);
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	else
1700Sstevel@tonic-gate 		{
1710Sstevel@tonic-gate 		ftime(&tend);
1720Sstevel@tonic-gate 		i=(long)tend.millitm-(long)tstart.millitm;
1730Sstevel@tonic-gate 		ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
1740Sstevel@tonic-gate 		return((ret == 0.0)?1e-6:ret);
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate #endif
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
main(int argc,char ** argv)1790Sstevel@tonic-gate int main(int argc, char **argv)
1800Sstevel@tonic-gate 	{
1810Sstevel@tonic-gate 	long count;
1820Sstevel@tonic-gate 	static unsigned char buf[BUFSIZE];
1830Sstevel@tonic-gate 	static DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
1840Sstevel@tonic-gate 	static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
1850Sstevel@tonic-gate 	static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
1860Sstevel@tonic-gate 	DES_key_schedule sch,sch2,sch3;
1870Sstevel@tonic-gate 	double a,b,c,d,e;
1880Sstevel@tonic-gate #ifndef SIGALRM
1890Sstevel@tonic-gate 	long ca,cb,cc,cd,ce;
1900Sstevel@tonic-gate #endif
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate #ifndef TIMES
1930Sstevel@tonic-gate 	printf("To get the most accurate results, try to run this\n");
1940Sstevel@tonic-gate 	printf("program when this computer is idle.\n");
1950Sstevel@tonic-gate #endif
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	DES_set_key_unchecked(&key2,&sch2);
1980Sstevel@tonic-gate 	DES_set_key_unchecked(&key3,&sch3);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #ifndef SIGALRM
2010Sstevel@tonic-gate 	printf("First we calculate the approximate speed ...\n");
2020Sstevel@tonic-gate 	DES_set_key_unchecked(&key,&sch);
2030Sstevel@tonic-gate 	count=10;
2040Sstevel@tonic-gate 	do	{
2050Sstevel@tonic-gate 		long i;
2060Sstevel@tonic-gate 		DES_LONG data[2];
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		count*=2;
2090Sstevel@tonic-gate 		Time_F(START);
2100Sstevel@tonic-gate 		for (i=count; i; i--)
2110Sstevel@tonic-gate 			DES_encrypt1(data,&sch,DES_ENCRYPT);
2120Sstevel@tonic-gate 		d=Time_F(STOP);
2130Sstevel@tonic-gate 		} while (d < 3.0);
2140Sstevel@tonic-gate 	ca=count;
2150Sstevel@tonic-gate 	cb=count*3;
2160Sstevel@tonic-gate 	cc=count*3*8/BUFSIZE+1;
2170Sstevel@tonic-gate 	cd=count*8/BUFSIZE+1;
2180Sstevel@tonic-gate 	ce=count/20+1;
2190Sstevel@tonic-gate 	printf("Doing set_key %ld times\n",ca);
2200Sstevel@tonic-gate #define COND(d)	(count != (d))
2210Sstevel@tonic-gate #define COUNT(d) (d)
2220Sstevel@tonic-gate #else
2230Sstevel@tonic-gate #define COND(c)	(run)
2240Sstevel@tonic-gate #define COUNT(d) (count)
2250Sstevel@tonic-gate 	signal(SIGALRM,sig_done);
2260Sstevel@tonic-gate 	printf("Doing set_key for 10 seconds\n");
2270Sstevel@tonic-gate 	alarm(10);
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	Time_F(START);
2310Sstevel@tonic-gate 	for (count=0,run=1; COND(ca); count++)
2320Sstevel@tonic-gate 		DES_set_key_unchecked(&key,&sch);
2330Sstevel@tonic-gate 	d=Time_F(STOP);
2340Sstevel@tonic-gate 	printf("%ld set_key's in %.2f seconds\n",count,d);
2350Sstevel@tonic-gate 	a=((double)COUNT(ca))/d;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate #ifdef SIGALRM
2380Sstevel@tonic-gate 	printf("Doing DES_encrypt's for 10 seconds\n");
2390Sstevel@tonic-gate 	alarm(10);
2400Sstevel@tonic-gate #else
2410Sstevel@tonic-gate 	printf("Doing DES_encrypt %ld times\n",cb);
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 	Time_F(START);
2440Sstevel@tonic-gate 	for (count=0,run=1; COND(cb); count++)
2450Sstevel@tonic-gate 		{
2460Sstevel@tonic-gate 		DES_LONG data[2];
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		DES_encrypt1(data,&sch,DES_ENCRYPT);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	d=Time_F(STOP);
2510Sstevel@tonic-gate 	printf("%ld DES_encrypt's in %.2f second\n",count,d);
2520Sstevel@tonic-gate 	b=((double)COUNT(cb)*8)/d;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #ifdef SIGALRM
2550Sstevel@tonic-gate 	printf("Doing DES_cbc_encrypt on %ld byte blocks for 10 seconds\n",
2560Sstevel@tonic-gate 		BUFSIZE);
2570Sstevel@tonic-gate 	alarm(10);
2580Sstevel@tonic-gate #else
2590Sstevel@tonic-gate 	printf("Doing DES_cbc_encrypt %ld times on %ld byte blocks\n",cc,
2600Sstevel@tonic-gate 		BUFSIZE);
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate 	Time_F(START);
2630Sstevel@tonic-gate 	for (count=0,run=1; COND(cc); count++)
2640Sstevel@tonic-gate 		DES_ncbc_encrypt(buf,buf,BUFSIZE,&sch,
2650Sstevel@tonic-gate 			&key,DES_ENCRYPT);
2660Sstevel@tonic-gate 	d=Time_F(STOP);
2670Sstevel@tonic-gate 	printf("%ld DES_cbc_encrypt's of %ld byte blocks in %.2f second\n",
2680Sstevel@tonic-gate 		count,BUFSIZE,d);
2690Sstevel@tonic-gate 	c=((double)COUNT(cc)*BUFSIZE)/d;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate #ifdef SIGALRM
2720Sstevel@tonic-gate 	printf("Doing DES_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
2730Sstevel@tonic-gate 		BUFSIZE);
2740Sstevel@tonic-gate 	alarm(10);
2750Sstevel@tonic-gate #else
2760Sstevel@tonic-gate 	printf("Doing DES_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,
2770Sstevel@tonic-gate 		BUFSIZE);
2780Sstevel@tonic-gate #endif
2790Sstevel@tonic-gate 	Time_F(START);
2800Sstevel@tonic-gate 	for (count=0,run=1; COND(cd); count++)
2810Sstevel@tonic-gate 		DES_ede3_cbc_encrypt(buf,buf,BUFSIZE,
2820Sstevel@tonic-gate 			&sch,
2830Sstevel@tonic-gate 			&sch2,
2840Sstevel@tonic-gate 			&sch3,
2850Sstevel@tonic-gate 			&key,
2860Sstevel@tonic-gate 			DES_ENCRYPT);
2870Sstevel@tonic-gate 	d=Time_F(STOP);
2880Sstevel@tonic-gate 	printf("%ld DES_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
2890Sstevel@tonic-gate 		count,BUFSIZE,d);
2900Sstevel@tonic-gate 	d=((double)COUNT(cd)*BUFSIZE)/d;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate #ifdef SIGALRM
2930Sstevel@tonic-gate 	printf("Doing crypt for 10 seconds\n");
2940Sstevel@tonic-gate 	alarm(10);
2950Sstevel@tonic-gate #else
2960Sstevel@tonic-gate 	printf("Doing crypt %ld times\n",ce);
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate 	Time_F(START);
2990Sstevel@tonic-gate 	for (count=0,run=1; COND(ce); count++)
3000Sstevel@tonic-gate 		crypt("testing1","ef");
3010Sstevel@tonic-gate 	e=Time_F(STOP);
3020Sstevel@tonic-gate 	printf("%ld crypts in %.2f second\n",count,e);
3030Sstevel@tonic-gate 	e=((double)COUNT(ce))/e;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	printf("set_key            per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
3060Sstevel@tonic-gate 	printf("DES raw ecb bytes  per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
3070Sstevel@tonic-gate 	printf("DES cbc bytes      per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
3080Sstevel@tonic-gate 	printf("DES ede cbc bytes  per sec = %12.2f (%9.3fuS)\n",d,8.0e6/d);
3090Sstevel@tonic-gate 	printf("crypt              per sec = %12.2f (%9.3fuS)\n",e,1.0e6/e);
3100Sstevel@tonic-gate 	exit(0);
3110Sstevel@tonic-gate #if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
3120Sstevel@tonic-gate 	return(0);
3130Sstevel@tonic-gate #endif
3140Sstevel@tonic-gate 	}
315