xref: /onnv-gate/usr/src/common/openssl/crypto/idea/idea_spd.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/idea/idea_spd.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 #endif
75*2139Sjp161948 
760Sstevel@tonic-gate #ifndef _IRIX
770Sstevel@tonic-gate #include <time.h>
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate #ifdef TIMES
800Sstevel@tonic-gate #include <sys/types.h>
810Sstevel@tonic-gate #include <sys/times.h>
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /* Depending on the VMS version, the tms structure is perhaps defined.
850Sstevel@tonic-gate    The __TMS macro will show if it was.  If it wasn't defined, we should
860Sstevel@tonic-gate    undefine TIMES, since that tells the rest of the program how things
870Sstevel@tonic-gate    should be handled.				-- Richard Levitte */
880Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
890Sstevel@tonic-gate #undef TIMES
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #ifndef TIMES
930Sstevel@tonic-gate #include <sys/timeb.h>
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #if defined(sun) || defined(__ultrix)
970Sstevel@tonic-gate #define _POSIX_SOURCE
980Sstevel@tonic-gate #include <limits.h>
990Sstevel@tonic-gate #include <sys/param.h>
1000Sstevel@tonic-gate #endif
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #include <openssl/idea.h>
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* The following if from times(3) man page.  It may need to be changed */
1050Sstevel@tonic-gate #ifndef HZ
1060Sstevel@tonic-gate #ifndef CLK_TCK
1070Sstevel@tonic-gate #define HZ	100.0
1080Sstevel@tonic-gate #else /* CLK_TCK */
1090Sstevel@tonic-gate #define HZ ((double)CLK_TCK)
1100Sstevel@tonic-gate #endif
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #define BUFSIZE	((long)1024)
1140Sstevel@tonic-gate long run=0;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate double Time_F(int s);
1170Sstevel@tonic-gate #ifdef SIGALRM
1180Sstevel@tonic-gate #if defined(__STDC__) || defined(sgi) || defined(_AIX)
1190Sstevel@tonic-gate #define SIGRETTYPE void
1200Sstevel@tonic-gate #else
1210Sstevel@tonic-gate #define SIGRETTYPE int
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate SIGRETTYPE sig_done(int sig);
sig_done(int sig)1250Sstevel@tonic-gate SIGRETTYPE sig_done(int sig)
1260Sstevel@tonic-gate 	{
1270Sstevel@tonic-gate 	signal(SIGALRM,sig_done);
1280Sstevel@tonic-gate 	run=0;
1290Sstevel@tonic-gate #ifdef LINT
1300Sstevel@tonic-gate 	sig=sig;
1310Sstevel@tonic-gate #endif
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate #define START	0
1360Sstevel@tonic-gate #define STOP	1
1370Sstevel@tonic-gate 
Time_F(int s)1380Sstevel@tonic-gate double Time_F(int s)
1390Sstevel@tonic-gate 	{
1400Sstevel@tonic-gate 	double ret;
1410Sstevel@tonic-gate #ifdef TIMES
1420Sstevel@tonic-gate 	static struct tms tstart,tend;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (s == START)
1450Sstevel@tonic-gate 		{
1460Sstevel@tonic-gate 		times(&tstart);
1470Sstevel@tonic-gate 		return(0);
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 	else
1500Sstevel@tonic-gate 		{
1510Sstevel@tonic-gate 		times(&tend);
1520Sstevel@tonic-gate 		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
1530Sstevel@tonic-gate 		return((ret == 0.0)?1e-6:ret);
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate #else /* !times() */
1560Sstevel@tonic-gate 	static struct timeb tstart,tend;
1570Sstevel@tonic-gate 	long i;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (s == START)
1600Sstevel@tonic-gate 		{
1610Sstevel@tonic-gate 		ftime(&tstart);
1620Sstevel@tonic-gate 		return(0);
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 	else
1650Sstevel@tonic-gate 		{
1660Sstevel@tonic-gate 		ftime(&tend);
1670Sstevel@tonic-gate 		i=(long)tend.millitm-(long)tstart.millitm;
1680Sstevel@tonic-gate 		ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
1690Sstevel@tonic-gate 		return((ret == 0.0)?1e-6:ret);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
main(int argc,char ** argv)1740Sstevel@tonic-gate int main(int argc, char **argv)
1750Sstevel@tonic-gate 	{
1760Sstevel@tonic-gate 	long count;
1770Sstevel@tonic-gate 	static unsigned char buf[BUFSIZE];
1780Sstevel@tonic-gate 	static unsigned char key[] ={
1790Sstevel@tonic-gate 			0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
1800Sstevel@tonic-gate 			0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
1810Sstevel@tonic-gate 			};
1820Sstevel@tonic-gate 	IDEA_KEY_SCHEDULE sch;
1830Sstevel@tonic-gate 	double a,aa,b,c,d;
1840Sstevel@tonic-gate #ifndef SIGALRM
1850Sstevel@tonic-gate 	long ca,cca,cb,cc;
1860Sstevel@tonic-gate #endif
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate #ifndef TIMES
1890Sstevel@tonic-gate 	printf("To get the most accurate results, try to run this\n");
1900Sstevel@tonic-gate 	printf("program when this computer is idle.\n");
1910Sstevel@tonic-gate #endif
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate #ifndef SIGALRM
1940Sstevel@tonic-gate 	printf("First we calculate the approximate speed ...\n");
1950Sstevel@tonic-gate 	idea_set_encrypt_key(key,&sch);
1960Sstevel@tonic-gate 	count=10;
1970Sstevel@tonic-gate 	do	{
1980Sstevel@tonic-gate 		long i;
1990Sstevel@tonic-gate 		IDEA_INT data[2];
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		count*=2;
2020Sstevel@tonic-gate 		Time_F(START);
2030Sstevel@tonic-gate 		for (i=count; i; i--)
2040Sstevel@tonic-gate 			idea_encrypt(data,&sch);
2050Sstevel@tonic-gate 		d=Time_F(STOP);
2060Sstevel@tonic-gate 		} while (d < 3.0);
2070Sstevel@tonic-gate 	ca=count/4;
2080Sstevel@tonic-gate 	cca=count/200;
2090Sstevel@tonic-gate 	cb=count;
2100Sstevel@tonic-gate 	cc=count*8/BUFSIZE+1;
2110Sstevel@tonic-gate 	printf("idea_set_encrypt_key %ld times\n",ca);
2120Sstevel@tonic-gate #define COND(d)	(count <= (d))
2130Sstevel@tonic-gate #define COUNT(d) (d)
2140Sstevel@tonic-gate #else
2150Sstevel@tonic-gate #define COND(c)	(run)
2160Sstevel@tonic-gate #define COUNT(d) (count)
2170Sstevel@tonic-gate 	signal(SIGALRM,sig_done);
2180Sstevel@tonic-gate 	printf("Doing idea_set_encrypt_key for 10 seconds\n");
2190Sstevel@tonic-gate 	alarm(10);
2200Sstevel@tonic-gate #endif
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	Time_F(START);
2230Sstevel@tonic-gate 	for (count=0,run=1; COND(ca); count+=4)
2240Sstevel@tonic-gate 		{
2250Sstevel@tonic-gate 		idea_set_encrypt_key(key,&sch);
2260Sstevel@tonic-gate 		idea_set_encrypt_key(key,&sch);
2270Sstevel@tonic-gate 		idea_set_encrypt_key(key,&sch);
2280Sstevel@tonic-gate 		idea_set_encrypt_key(key,&sch);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 	d=Time_F(STOP);
2310Sstevel@tonic-gate 	printf("%ld idea idea_set_encrypt_key's in %.2f seconds\n",count,d);
2320Sstevel@tonic-gate 	a=((double)COUNT(ca))/d;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #ifdef SIGALRM
2350Sstevel@tonic-gate 	printf("Doing idea_set_decrypt_key for 10 seconds\n");
2360Sstevel@tonic-gate 	alarm(10);
2370Sstevel@tonic-gate #else
2380Sstevel@tonic-gate 	printf("Doing idea_set_decrypt_key %ld times\n",cca);
2390Sstevel@tonic-gate #endif
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	Time_F(START);
2420Sstevel@tonic-gate 	for (count=0,run=1; COND(cca); count+=4)
2430Sstevel@tonic-gate 		{
2440Sstevel@tonic-gate 		idea_set_decrypt_key(&sch,&sch);
2450Sstevel@tonic-gate 		idea_set_decrypt_key(&sch,&sch);
2460Sstevel@tonic-gate 		idea_set_decrypt_key(&sch,&sch);
2470Sstevel@tonic-gate 		idea_set_decrypt_key(&sch,&sch);
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 	d=Time_F(STOP);
2500Sstevel@tonic-gate 	printf("%ld idea idea_set_decrypt_key's in %.2f seconds\n",count,d);
2510Sstevel@tonic-gate 	aa=((double)COUNT(cca))/d;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #ifdef SIGALRM
2540Sstevel@tonic-gate 	printf("Doing idea_encrypt's for 10 seconds\n");
2550Sstevel@tonic-gate 	alarm(10);
2560Sstevel@tonic-gate #else
2570Sstevel@tonic-gate 	printf("Doing idea_encrypt %ld times\n",cb);
2580Sstevel@tonic-gate #endif
2590Sstevel@tonic-gate 	Time_F(START);
2600Sstevel@tonic-gate 	for (count=0,run=1; COND(cb); count+=4)
2610Sstevel@tonic-gate 		{
2620Sstevel@tonic-gate 		unsigned long data[2];
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		idea_encrypt(data,&sch);
2650Sstevel@tonic-gate 		idea_encrypt(data,&sch);
2660Sstevel@tonic-gate 		idea_encrypt(data,&sch);
2670Sstevel@tonic-gate 		idea_encrypt(data,&sch);
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 	d=Time_F(STOP);
2700Sstevel@tonic-gate 	printf("%ld idea_encrypt's in %.2f second\n",count,d);
2710Sstevel@tonic-gate 	b=((double)COUNT(cb)*8)/d;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate #ifdef SIGALRM
2740Sstevel@tonic-gate 	printf("Doing idea_cbc_encrypt on %ld byte blocks for 10 seconds\n",
2750Sstevel@tonic-gate 		BUFSIZE);
2760Sstevel@tonic-gate 	alarm(10);
2770Sstevel@tonic-gate #else
2780Sstevel@tonic-gate 	printf("Doing idea_cbc_encrypt %ld times on %ld byte blocks\n",cc,
2790Sstevel@tonic-gate 		BUFSIZE);
2800Sstevel@tonic-gate #endif
2810Sstevel@tonic-gate 	Time_F(START);
2820Sstevel@tonic-gate 	for (count=0,run=1; COND(cc); count++)
2830Sstevel@tonic-gate 		idea_cbc_encrypt(buf,buf,BUFSIZE,&sch,
2840Sstevel@tonic-gate 			&(key[0]),IDEA_ENCRYPT);
2850Sstevel@tonic-gate 	d=Time_F(STOP);
2860Sstevel@tonic-gate 	printf("%ld idea_cbc_encrypt's of %ld byte blocks in %.2f second\n",
2870Sstevel@tonic-gate 		count,BUFSIZE,d);
2880Sstevel@tonic-gate 	c=((double)COUNT(cc)*BUFSIZE)/d;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	printf("IDEA set_encrypt_key per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
2910Sstevel@tonic-gate 	printf("IDEA set_decrypt_key per sec = %12.2f (%9.3fuS)\n",aa,1.0e6/aa);
2920Sstevel@tonic-gate 	printf("IDEA raw ecb bytes   per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
2930Sstevel@tonic-gate 	printf("IDEA cbc     bytes   per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
2940Sstevel@tonic-gate 	exit(0);
2950Sstevel@tonic-gate #if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
2960Sstevel@tonic-gate 	return(0);
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
300