155714Skris/* crypto/bf/bfspeed.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
6055714Skris/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
6155714Skris
62109998Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
6355714Skris#define TIMES
6455714Skris#endif
6555714Skris
6655714Skris#include <stdio.h>
6755714Skris
6855714Skris#include <openssl/e_os2.h>
6955714Skris#include OPENSSL_UNISTD_IO
7055714SkrisOPENSSL_DECLARE_EXIT
7155714Skris
72160814Ssimon#ifndef OPENSSL_SYS_NETWARE
7355714Skris#include <signal.h>
74160814Ssimon#endif
75160814Ssimon
7655714Skris#ifndef _IRIX
7755714Skris#include <time.h>
7855714Skris#endif
7955714Skris#ifdef TIMES
8055714Skris#include <sys/types.h>
8155714Skris#include <sys/times.h>
8255714Skris#endif
8355714Skris
8455714Skris/* Depending on the VMS version, the tms structure is perhaps defined.
8555714Skris   The __TMS macro will show if it was.  If it wasn't defined, we should
8655714Skris   undefine TIMES, since that tells the rest of the program how things
8755714Skris   should be handled.				-- Richard Levitte */
88109998Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
8955714Skris#undef TIMES
9055714Skris#endif
9155714Skris
9255714Skris#ifndef TIMES
9355714Skris#include <sys/timeb.h>
9455714Skris#endif
9555714Skris
9655714Skris#if defined(sun) || defined(__ultrix)
9755714Skris#define _POSIX_SOURCE
9855714Skris#include <limits.h>
9955714Skris#include <sys/param.h>
10055714Skris#endif
10155714Skris
10255714Skris#include <openssl/blowfish.h>
10355714Skris
10455714Skris/* The following if from times(3) man page.  It may need to be changed */
10555714Skris#ifndef HZ
10655714Skris#ifndef CLK_TCK
10755714Skris#define HZ	100.0
10855714Skris#else /* CLK_TCK */
10955714Skris#define HZ ((double)CLK_TCK)
11055714Skris#endif
11155714Skris#endif
11255714Skris
11355714Skris#define BUFSIZE	((long)1024)
11455714Skrislong run=0;
11555714Skris
11655714Skrisdouble Time_F(int s);
11755714Skris#ifdef SIGALRM
11855714Skris#if defined(__STDC__) || defined(sgi) || defined(_AIX)
11955714Skris#define SIGRETTYPE void
12055714Skris#else
12155714Skris#define SIGRETTYPE int
12255714Skris#endif
12355714Skris
12455714SkrisSIGRETTYPE sig_done(int sig);
12555714SkrisSIGRETTYPE sig_done(int sig)
12655714Skris	{
12755714Skris	signal(SIGALRM,sig_done);
12855714Skris	run=0;
12955714Skris#ifdef LINT
13055714Skris	sig=sig;
13155714Skris#endif
13255714Skris	}
13355714Skris#endif
13455714Skris
13555714Skris#define START	0
13655714Skris#define STOP	1
13755714Skris
13855714Skrisdouble Time_F(int s)
13955714Skris	{
14055714Skris	double ret;
14155714Skris#ifdef TIMES
14255714Skris	static struct tms tstart,tend;
14355714Skris
14455714Skris	if (s == START)
14555714Skris		{
14655714Skris		times(&tstart);
14755714Skris		return(0);
14855714Skris		}
14955714Skris	else
15055714Skris		{
15155714Skris		times(&tend);
15255714Skris		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
15355714Skris		return((ret == 0.0)?1e-6:ret);
15455714Skris		}
15555714Skris#else /* !times() */
15655714Skris	static struct timeb tstart,tend;
15755714Skris	long i;
15855714Skris
15955714Skris	if (s == START)
16055714Skris		{
16155714Skris		ftime(&tstart);
16255714Skris		return(0);
16355714Skris		}
16455714Skris	else
16555714Skris		{
16655714Skris		ftime(&tend);
16755714Skris		i=(long)tend.millitm-(long)tstart.millitm;
16855714Skris		ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
16955714Skris		return((ret == 0.0)?1e-6:ret);
17055714Skris		}
17155714Skris#endif
17255714Skris	}
17355714Skris
17455714Skrisint main(int argc, char **argv)
17555714Skris	{
17655714Skris	long count;
17755714Skris	static unsigned char buf[BUFSIZE];
17855714Skris	static unsigned char key[] ={
17955714Skris			0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
18055714Skris			0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
18155714Skris			};
18255714Skris	BF_KEY sch;
18355714Skris	double a,b,c,d;
18455714Skris#ifndef SIGALRM
18555714Skris	long ca,cb,cc;
18655714Skris#endif
18755714Skris
18855714Skris#ifndef TIMES
18959191Skris	printf("To get the most accurate results, try to run this\n");
19055714Skris	printf("program when this computer is idle.\n");
19155714Skris#endif
19255714Skris
19355714Skris#ifndef SIGALRM
19455714Skris	printf("First we calculate the approximate speed ...\n");
19555714Skris	BF_set_key(&sch,16,key);
19655714Skris	count=10;
19755714Skris	do	{
19855714Skris		long i;
19955714Skris		BF_LONG data[2];
20055714Skris
20155714Skris		count*=2;
20255714Skris		Time_F(START);
20355714Skris		for (i=count; i; i--)
20455714Skris			BF_encrypt(data,&sch);
20555714Skris		d=Time_F(STOP);
20655714Skris		} while (d < 3.0);
20755714Skris	ca=count/512;
20855714Skris	cb=count;
20955714Skris	cc=count*8/BUFSIZE+1;
21055714Skris	printf("Doing BF_set_key %ld times\n",ca);
21155714Skris#define COND(d)	(count != (d))
21255714Skris#define COUNT(d) (d)
21355714Skris#else
21455714Skris#define COND(c)	(run)
21555714Skris#define COUNT(d) (count)
21655714Skris	signal(SIGALRM,sig_done);
21755714Skris	printf("Doing BF_set_key for 10 seconds\n");
21855714Skris	alarm(10);
21955714Skris#endif
22055714Skris
22155714Skris	Time_F(START);
22255714Skris	for (count=0,run=1; COND(ca); count+=4)
22355714Skris		{
22455714Skris		BF_set_key(&sch,16,key);
22555714Skris		BF_set_key(&sch,16,key);
22655714Skris		BF_set_key(&sch,16,key);
22755714Skris		BF_set_key(&sch,16,key);
22855714Skris		}
22955714Skris	d=Time_F(STOP);
23055714Skris	printf("%ld BF_set_key's in %.2f seconds\n",count,d);
23155714Skris	a=((double)COUNT(ca))/d;
23255714Skris
23355714Skris#ifdef SIGALRM
23455714Skris	printf("Doing BF_encrypt's for 10 seconds\n");
23555714Skris	alarm(10);
23655714Skris#else
23755714Skris	printf("Doing BF_encrypt %ld times\n",cb);
23855714Skris#endif
23955714Skris	Time_F(START);
24055714Skris	for (count=0,run=1; COND(cb); count+=4)
24155714Skris		{
24255714Skris		BF_LONG data[2];
24355714Skris
24455714Skris		BF_encrypt(data,&sch);
24555714Skris		BF_encrypt(data,&sch);
24655714Skris		BF_encrypt(data,&sch);
24755714Skris		BF_encrypt(data,&sch);
24855714Skris		}
24955714Skris	d=Time_F(STOP);
25055714Skris	printf("%ld BF_encrypt's in %.2f second\n",count,d);
25155714Skris	b=((double)COUNT(cb)*8)/d;
25255714Skris
25355714Skris#ifdef SIGALRM
25455714Skris	printf("Doing BF_cbc_encrypt on %ld byte blocks for 10 seconds\n",
25555714Skris		BUFSIZE);
25655714Skris	alarm(10);
25755714Skris#else
25855714Skris	printf("Doing BF_cbc_encrypt %ld times on %ld byte blocks\n",cc,
25955714Skris		BUFSIZE);
26055714Skris#endif
26155714Skris	Time_F(START);
26255714Skris	for (count=0,run=1; COND(cc); count++)
26355714Skris		BF_cbc_encrypt(buf,buf,BUFSIZE,&sch,
26455714Skris			&(key[0]),BF_ENCRYPT);
26555714Skris	d=Time_F(STOP);
26655714Skris	printf("%ld BF_cbc_encrypt's of %ld byte blocks in %.2f second\n",
26755714Skris		count,BUFSIZE,d);
26855714Skris	c=((double)COUNT(cc)*BUFSIZE)/d;
26955714Skris
27055714Skris	printf("Blowfish set_key       per sec = %12.3f (%9.3fuS)\n",a,1.0e6/a);
27155714Skris	printf("Blowfish raw ecb bytes per sec = %12.3f (%9.3fuS)\n",b,8.0e6/b);
27255714Skris	printf("Blowfish cbc     bytes per sec = %12.3f (%9.3fuS)\n",c,8.0e6/c);
27355714Skris	exit(0);
274109998Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
27555714Skris	return(0);
27655714Skris#endif
27755714Skris	}
278