xref: /onnv-gate/usr/src/common/openssl/crypto/bf/bf_opts.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bf/bf_opts.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 /* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
600Sstevel@tonic-gate  * This is for machines with 64k code segment size restrictions. */
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/blowfish.h>
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #define BF_DEFAULT_OPTIONS
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #undef BF_ENC
1070Sstevel@tonic-gate #define BF_encrypt  BF_encrypt_normal
1080Sstevel@tonic-gate #undef HEADER_BF_LOCL_H
1090Sstevel@tonic-gate #include "bf_enc.c"
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #define BF_PTR
1120Sstevel@tonic-gate #undef BF_PTR2
1130Sstevel@tonic-gate #undef BF_ENC
1140Sstevel@tonic-gate #undef BF_encrypt
1150Sstevel@tonic-gate #define BF_encrypt  BF_encrypt_ptr
1160Sstevel@tonic-gate #undef HEADER_BF_LOCL_H
1170Sstevel@tonic-gate #include "bf_enc.c"
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #undef BF_PTR
1200Sstevel@tonic-gate #define BF_PTR2
1210Sstevel@tonic-gate #undef BF_ENC
1220Sstevel@tonic-gate #undef BF_encrypt
1230Sstevel@tonic-gate #define BF_encrypt  BF_encrypt_ptr2
1240Sstevel@tonic-gate #undef HEADER_BF_LOCL_H
1250Sstevel@tonic-gate #include "bf_enc.c"
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /* The following if from times(3) man page.  It may need to be changed */
1280Sstevel@tonic-gate #ifndef HZ
1290Sstevel@tonic-gate # ifndef CLK_TCK
1300Sstevel@tonic-gate #  ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
1310Sstevel@tonic-gate #   define HZ	100.0
1320Sstevel@tonic-gate #  else /* _BSD_CLK_TCK_ */
1330Sstevel@tonic-gate #   define HZ ((double)_BSD_CLK_TCK_)
1340Sstevel@tonic-gate #  endif
1350Sstevel@tonic-gate # else /* CLK_TCK */
1360Sstevel@tonic-gate #  define HZ ((double)CLK_TCK)
1370Sstevel@tonic-gate # endif
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #define BUFSIZE	((long)1024)
1410Sstevel@tonic-gate long run=0;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate double Time_F(int s);
1440Sstevel@tonic-gate #ifdef SIGALRM
1450Sstevel@tonic-gate #if defined(__STDC__) || defined(sgi)
1460Sstevel@tonic-gate #define SIGRETTYPE void
1470Sstevel@tonic-gate #else
1480Sstevel@tonic-gate #define SIGRETTYPE int
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate SIGRETTYPE sig_done(int sig);
sig_done(int sig)1520Sstevel@tonic-gate SIGRETTYPE sig_done(int sig)
1530Sstevel@tonic-gate 	{
1540Sstevel@tonic-gate 	signal(SIGALRM,sig_done);
1550Sstevel@tonic-gate 	run=0;
1560Sstevel@tonic-gate #ifdef LINT
1570Sstevel@tonic-gate 	sig=sig;
1580Sstevel@tonic-gate #endif
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate #endif
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate #define START	0
1630Sstevel@tonic-gate #define STOP	1
1640Sstevel@tonic-gate 
Time_F(int s)1650Sstevel@tonic-gate double Time_F(int s)
1660Sstevel@tonic-gate 	{
1670Sstevel@tonic-gate 	double ret;
1680Sstevel@tonic-gate #ifdef TIMES
1690Sstevel@tonic-gate 	static struct tms tstart,tend;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (s == START)
1720Sstevel@tonic-gate 		{
1730Sstevel@tonic-gate 		times(&tstart);
1740Sstevel@tonic-gate 		return(0);
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 	else
1770Sstevel@tonic-gate 		{
1780Sstevel@tonic-gate 		times(&tend);
1790Sstevel@tonic-gate 		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
1800Sstevel@tonic-gate 		return((ret == 0.0)?1e-6:ret);
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate #else /* !times() */
1830Sstevel@tonic-gate 	static struct timeb tstart,tend;
1840Sstevel@tonic-gate 	long i;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (s == START)
1870Sstevel@tonic-gate 		{
1880Sstevel@tonic-gate 		ftime(&tstart);
1890Sstevel@tonic-gate 		return(0);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	else
1920Sstevel@tonic-gate 		{
1930Sstevel@tonic-gate 		ftime(&tend);
1940Sstevel@tonic-gate 		i=(long)tend.millitm-(long)tstart.millitm;
1950Sstevel@tonic-gate 		ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
1960Sstevel@tonic-gate 		return((ret == 0.0)?1e-6:ret);
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate #endif
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate #ifdef SIGALRM
2020Sstevel@tonic-gate #define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
2030Sstevel@tonic-gate #else
2040Sstevel@tonic-gate #define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate #define time_it(func,name,index) \
2080Sstevel@tonic-gate 	print_name(name); \
2090Sstevel@tonic-gate 	Time_F(START); \
2100Sstevel@tonic-gate 	for (count=0,run=1; COND(cb); count+=4) \
2110Sstevel@tonic-gate 		{ \
2120Sstevel@tonic-gate 		unsigned long d[2]; \
2130Sstevel@tonic-gate 		func(d,&sch); \
2140Sstevel@tonic-gate 		func(d,&sch); \
2150Sstevel@tonic-gate 		func(d,&sch); \
2160Sstevel@tonic-gate 		func(d,&sch); \
2170Sstevel@tonic-gate 		} \
2180Sstevel@tonic-gate 	tm[index]=Time_F(STOP); \
2190Sstevel@tonic-gate 	fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
2200Sstevel@tonic-gate 	tm[index]=((double)COUNT(cb))/tm[index];
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #define print_it(name,index) \
2230Sstevel@tonic-gate 	fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
2240Sstevel@tonic-gate 		tm[index]*8,1.0e6/tm[index]);
2250Sstevel@tonic-gate 
main(int argc,char ** argv)2260Sstevel@tonic-gate int main(int argc, char **argv)
2270Sstevel@tonic-gate 	{
2280Sstevel@tonic-gate 	long count;
2290Sstevel@tonic-gate 	static unsigned char buf[BUFSIZE];
2300Sstevel@tonic-gate 	static char key[16]={	0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
2310Sstevel@tonic-gate 				0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
2320Sstevel@tonic-gate 	BF_KEY sch;
2330Sstevel@tonic-gate 	double d,tm[16],max=0;
2340Sstevel@tonic-gate 	int rank[16];
2350Sstevel@tonic-gate 	char *str[16];
2360Sstevel@tonic-gate 	int max_idx=0,i,num=0,j;
2370Sstevel@tonic-gate #ifndef SIGALARM
2380Sstevel@tonic-gate 	long ca,cb,cc,cd,ce;
2390Sstevel@tonic-gate #endif
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	for (i=0; i<12; i++)
2420Sstevel@tonic-gate 		{
2430Sstevel@tonic-gate 		tm[i]=0.0;
2440Sstevel@tonic-gate 		rank[i]=0;
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate #ifndef TIMES
2480Sstevel@tonic-gate 	fprintf(stderr,"To get the most accurate results, try to run this\n");
2490Sstevel@tonic-gate 	fprintf(stderr,"program when this computer is idle.\n");
2500Sstevel@tonic-gate #endif
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	BF_set_key(&sch,16,key);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #ifndef SIGALRM
2550Sstevel@tonic-gate 	fprintf(stderr,"First we calculate the approximate speed ...\n");
2560Sstevel@tonic-gate 	count=10;
2570Sstevel@tonic-gate 	do	{
2580Sstevel@tonic-gate 		long i;
2590Sstevel@tonic-gate 		unsigned long data[2];
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		count*=2;
2620Sstevel@tonic-gate 		Time_F(START);
2630Sstevel@tonic-gate 		for (i=count; i; i--)
2640Sstevel@tonic-gate 			BF_encrypt(data,&sch);
2650Sstevel@tonic-gate 		d=Time_F(STOP);
2660Sstevel@tonic-gate 		} while (d < 3.0);
2670Sstevel@tonic-gate 	ca=count;
2680Sstevel@tonic-gate 	cb=count*3;
2690Sstevel@tonic-gate 	cc=count*3*8/BUFSIZE+1;
2700Sstevel@tonic-gate 	cd=count*8/BUFSIZE+1;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	ce=count/20+1;
2730Sstevel@tonic-gate #define COND(d) (count != (d))
2740Sstevel@tonic-gate #define COUNT(d) (d)
2750Sstevel@tonic-gate #else
2760Sstevel@tonic-gate #define COND(c) (run)
2770Sstevel@tonic-gate #define COUNT(d) (count)
2780Sstevel@tonic-gate         signal(SIGALRM,sig_done);
2790Sstevel@tonic-gate         alarm(10);
2800Sstevel@tonic-gate #endif
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	time_it(BF_encrypt_normal,	"BF_encrypt_normal ", 0);
2830Sstevel@tonic-gate 	time_it(BF_encrypt_ptr,		"BF_encrypt_ptr    ", 1);
2840Sstevel@tonic-gate 	time_it(BF_encrypt_ptr2,	"BF_encrypt_ptr2   ", 2);
2850Sstevel@tonic-gate 	num+=3;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	str[0]="<nothing>";
2880Sstevel@tonic-gate 	print_it("BF_encrypt_normal ",0);
2890Sstevel@tonic-gate 	max=tm[0];
2900Sstevel@tonic-gate 	max_idx=0;
2910Sstevel@tonic-gate 	str[1]="ptr      ";
2920Sstevel@tonic-gate 	print_it("BF_encrypt_ptr ",1);
2930Sstevel@tonic-gate 	if (max < tm[1]) { max=tm[1]; max_idx=1; }
2940Sstevel@tonic-gate 	str[2]="ptr2     ";
2950Sstevel@tonic-gate 	print_it("BF_encrypt_ptr2 ",2);
2960Sstevel@tonic-gate 	if (max < tm[2]) { max=tm[2]; max_idx=2; }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	printf("options    BF ecb/s\n");
2990Sstevel@tonic-gate 	printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
3000Sstevel@tonic-gate 	d=tm[max_idx];
3010Sstevel@tonic-gate 	tm[max_idx]= -2.0;
3020Sstevel@tonic-gate 	max= -1.0;
3030Sstevel@tonic-gate 	for (;;)
3040Sstevel@tonic-gate 		{
3050Sstevel@tonic-gate 		for (i=0; i<3; i++)
3060Sstevel@tonic-gate 			{
3070Sstevel@tonic-gate 			if (max < tm[i]) { max=tm[i]; j=i; }
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 		if (max < 0.0) break;
3100Sstevel@tonic-gate 		printf("%s %12.2f  %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
3110Sstevel@tonic-gate 		tm[j]= -2.0;
3120Sstevel@tonic-gate 		max= -1.0;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	switch (max_idx)
3160Sstevel@tonic-gate 		{
3170Sstevel@tonic-gate 	case 0:
3180Sstevel@tonic-gate 		printf("-DBF_DEFAULT_OPTIONS\n");
3190Sstevel@tonic-gate 		break;
3200Sstevel@tonic-gate 	case 1:
3210Sstevel@tonic-gate 		printf("-DBF_PTR\n");
3220Sstevel@tonic-gate 		break;
3230Sstevel@tonic-gate 	case 2:
3240Sstevel@tonic-gate 		printf("-DBF_PTR2\n");
3250Sstevel@tonic-gate 		break;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 	exit(0);
3280Sstevel@tonic-gate #if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
3290Sstevel@tonic-gate 	return(0);
3300Sstevel@tonic-gate #endif
3310Sstevel@tonic-gate 	}
332