161821Smarkm/* crypto/idea/idea_spd.c */
261821Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
361821Smarkm * All rights reserved.
461821Smarkm *
561821Smarkm * This package is an SSL implementation written
661821Smarkm * by Eric Young (eay@cryptsoft.com).
761821Smarkm * The implementation was written so as to conform with Netscapes SSL.
861821Smarkm *
961821Smarkm * This library is free for commercial and non-commercial use as long as
1061821Smarkm * the following conditions are aheared to.  The following conditions
1161821Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
1261821Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1361821Smarkm * included with this distribution is covered by the same copyright terms
1461821Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1561821Smarkm *
1661821Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
1761821Smarkm * the code are not to be removed.
1861821Smarkm * If this package is used in a product, Eric Young should be given attribution
1961821Smarkm * as the author of the parts of the library used.
2061821Smarkm * This can be in the form of a textual message at program startup or
2161821Smarkm * in documentation (online or textual) provided with the package.
2261821Smarkm *
2361821Smarkm * Redistribution and use in source and binary forms, with or without
2461821Smarkm * modification, are permitted provided that the following conditions
2561821Smarkm * are met:
2661821Smarkm * 1. Redistributions of source code must retain the copyright
2761821Smarkm *    notice, this list of conditions and the following disclaimer.
2861821Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2961821Smarkm *    notice, this list of conditions and the following disclaimer in the
3061821Smarkm *    documentation and/or other materials provided with the distribution.
3161821Smarkm * 3. All advertising materials mentioning features or use of this software
3261821Smarkm *    must display the following acknowledgement:
3361821Smarkm *    "This product includes cryptographic software written by
3461821Smarkm *     Eric Young (eay@cryptsoft.com)"
3561821Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
3661821Smarkm *    being used are not cryptographic related :-).
3761821Smarkm * 4. If you include any Windows specific code (or a derivative thereof) from
3861821Smarkm *    the apps directory (application code) you must include an acknowledgement:
3961821Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4061821Smarkm *
4161821Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4261821Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4361821Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4461821Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4561821Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4661821Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4761821Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4861821Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4961821Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5061821Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5161821Smarkm * SUCH DAMAGE.
5261821Smarkm *
5361821Smarkm * The licence and distribution terms for any publically available version or
5461821Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
5561821Smarkm * copied and put under another distribution licence
5661821Smarkm * [including the GNU Public Licence.]
5761821Smarkm */
5861821Smarkm
5961821Smarkm/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
6061821Smarkm/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
6161821Smarkm
62110007Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
6361821Smarkm#define TIMES
6461821Smarkm#endif
6561821Smarkm
6661821Smarkm#include <stdio.h>
6761821Smarkm
6861821Smarkm#include <openssl/e_os2.h>
6961821Smarkm#include OPENSSL_UNISTD_IO
7061821SmarkmOPENSSL_DECLARE_EXIT
7161821Smarkm
72160817Ssimon#ifndef OPENSSL_SYS_NETWARE
7361821Smarkm#include <signal.h>
74160817Ssimon#endif
75160817Ssimon
7661821Smarkm#ifndef _IRIX
7761821Smarkm#include <time.h>
7861821Smarkm#endif
7961821Smarkm#ifdef TIMES
8061821Smarkm#include <sys/types.h>
8161821Smarkm#include <sys/times.h>
8261821Smarkm#endif
8361821Smarkm
8461821Smarkm/* Depending on the VMS version, the tms structure is perhaps defined.
8561821Smarkm   The __TMS macro will show if it was.  If it wasn't defined, we should
8661821Smarkm   undefine TIMES, since that tells the rest of the program how things
8761821Smarkm   should be handled.				-- Richard Levitte */
88110007Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
8961821Smarkm#undef TIMES
9061821Smarkm#endif
9161821Smarkm
9261821Smarkm#ifndef TIMES
9361821Smarkm#include <sys/timeb.h>
9461821Smarkm#endif
9561821Smarkm
9661821Smarkm#if defined(sun) || defined(__ultrix)
9761821Smarkm#define _POSIX_SOURCE
9861821Smarkm#include <limits.h>
9961821Smarkm#include <sys/param.h>
10061821Smarkm#endif
10161821Smarkm
10261821Smarkm#include <openssl/idea.h>
10361821Smarkm
10461821Smarkm/* The following if from times(3) man page.  It may need to be changed */
10561821Smarkm#ifndef HZ
10661821Smarkm#ifndef CLK_TCK
10761821Smarkm#define HZ	100.0
10861821Smarkm#else /* CLK_TCK */
10961821Smarkm#define HZ ((double)CLK_TCK)
11061821Smarkm#endif
11161821Smarkm#endif
11261821Smarkm
11361821Smarkm#define BUFSIZE	((long)1024)
11461821Smarkmlong run=0;
11561821Smarkm
11661821Smarkmdouble Time_F(int s);
11761821Smarkm#ifdef SIGALRM
11861821Smarkm#if defined(__STDC__) || defined(sgi) || defined(_AIX)
11961821Smarkm#define SIGRETTYPE void
12061821Smarkm#else
12161821Smarkm#define SIGRETTYPE int
12261821Smarkm#endif
12361821Smarkm
12461821SmarkmSIGRETTYPE sig_done(int sig);
12561821SmarkmSIGRETTYPE sig_done(int sig)
12661821Smarkm	{
12761821Smarkm	signal(SIGALRM,sig_done);
12861821Smarkm	run=0;
12961821Smarkm#ifdef LINT
13061821Smarkm	sig=sig;
13161821Smarkm#endif
13261821Smarkm	}
13361821Smarkm#endif
13461821Smarkm
13561821Smarkm#define START	0
13661821Smarkm#define STOP	1
13761821Smarkm
13861821Smarkmdouble Time_F(int s)
13961821Smarkm	{
14061821Smarkm	double ret;
14161821Smarkm#ifdef TIMES
14261821Smarkm	static struct tms tstart,tend;
14361821Smarkm
14461821Smarkm	if (s == START)
14561821Smarkm		{
14661821Smarkm		times(&tstart);
14761821Smarkm		return(0);
14861821Smarkm		}
14961821Smarkm	else
15061821Smarkm		{
15161821Smarkm		times(&tend);
15261821Smarkm		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
15361821Smarkm		return((ret == 0.0)?1e-6:ret);
15461821Smarkm		}
15561821Smarkm#else /* !times() */
15661821Smarkm	static struct timeb tstart,tend;
15761821Smarkm	long i;
15861821Smarkm
15961821Smarkm	if (s == START)
16061821Smarkm		{
16161821Smarkm		ftime(&tstart);
16261821Smarkm		return(0);
16361821Smarkm		}
16461821Smarkm	else
16561821Smarkm		{
16661821Smarkm		ftime(&tend);
16761821Smarkm		i=(long)tend.millitm-(long)tstart.millitm;
16861821Smarkm		ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
16961821Smarkm		return((ret == 0.0)?1e-6:ret);
17061821Smarkm		}
17161821Smarkm#endif
17261821Smarkm	}
17361821Smarkm
17461821Smarkmint main(int argc, char **argv)
17561821Smarkm	{
17661821Smarkm	long count;
17761821Smarkm	static unsigned char buf[BUFSIZE];
17861821Smarkm	static unsigned char key[] ={
17961821Smarkm			0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
18061821Smarkm			0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
18161821Smarkm			};
18261821Smarkm	IDEA_KEY_SCHEDULE sch;
18361821Smarkm	double a,aa,b,c,d;
18461821Smarkm#ifndef SIGALRM
18561821Smarkm	long ca,cca,cb,cc;
18661821Smarkm#endif
18761821Smarkm
18861821Smarkm#ifndef TIMES
18961821Smarkm	printf("To get the most accurate results, try to run this\n");
19061821Smarkm	printf("program when this computer is idle.\n");
19161821Smarkm#endif
19261821Smarkm
19361821Smarkm#ifndef SIGALRM
19461821Smarkm	printf("First we calculate the approximate speed ...\n");
19561821Smarkm	idea_set_encrypt_key(key,&sch);
19661821Smarkm	count=10;
19761821Smarkm	do	{
19861821Smarkm		long i;
19961821Smarkm		IDEA_INT data[2];
20061821Smarkm
20161821Smarkm		count*=2;
20261821Smarkm		Time_F(START);
20361821Smarkm		for (i=count; i; i--)
20461821Smarkm			idea_encrypt(data,&sch);
20561821Smarkm		d=Time_F(STOP);
20661821Smarkm		} while (d < 3.0);
20761821Smarkm	ca=count/4;
20861821Smarkm	cca=count/200;
20961821Smarkm	cb=count;
21061821Smarkm	cc=count*8/BUFSIZE+1;
21161821Smarkm	printf("idea_set_encrypt_key %ld times\n",ca);
21261821Smarkm#define COND(d)	(count <= (d))
21361821Smarkm#define COUNT(d) (d)
21461821Smarkm#else
21561821Smarkm#define COND(c)	(run)
21661821Smarkm#define COUNT(d) (count)
21761821Smarkm	signal(SIGALRM,sig_done);
21861821Smarkm	printf("Doing idea_set_encrypt_key for 10 seconds\n");
21961821Smarkm	alarm(10);
22061821Smarkm#endif
22161821Smarkm
22261821Smarkm	Time_F(START);
22361821Smarkm	for (count=0,run=1; COND(ca); count+=4)
22461821Smarkm		{
22561821Smarkm		idea_set_encrypt_key(key,&sch);
22661821Smarkm		idea_set_encrypt_key(key,&sch);
22761821Smarkm		idea_set_encrypt_key(key,&sch);
22861821Smarkm		idea_set_encrypt_key(key,&sch);
22961821Smarkm		}
23061821Smarkm	d=Time_F(STOP);
23161821Smarkm	printf("%ld idea idea_set_encrypt_key's in %.2f seconds\n",count,d);
23261821Smarkm	a=((double)COUNT(ca))/d;
23361821Smarkm
23461821Smarkm#ifdef SIGALRM
23561821Smarkm	printf("Doing idea_set_decrypt_key for 10 seconds\n");
23661821Smarkm	alarm(10);
23761821Smarkm#else
23861821Smarkm	printf("Doing idea_set_decrypt_key %ld times\n",cca);
23961821Smarkm#endif
24061821Smarkm
24161821Smarkm	Time_F(START);
24261821Smarkm	for (count=0,run=1; COND(cca); count+=4)
24361821Smarkm		{
24461821Smarkm		idea_set_decrypt_key(&sch,&sch);
24561821Smarkm		idea_set_decrypt_key(&sch,&sch);
24661821Smarkm		idea_set_decrypt_key(&sch,&sch);
24761821Smarkm		idea_set_decrypt_key(&sch,&sch);
24861821Smarkm		}
24961821Smarkm	d=Time_F(STOP);
25061821Smarkm	printf("%ld idea idea_set_decrypt_key's in %.2f seconds\n",count,d);
25161821Smarkm	aa=((double)COUNT(cca))/d;
25261821Smarkm
25361821Smarkm#ifdef SIGALRM
25461821Smarkm	printf("Doing idea_encrypt's for 10 seconds\n");
25561821Smarkm	alarm(10);
25661821Smarkm#else
25761821Smarkm	printf("Doing idea_encrypt %ld times\n",cb);
25861821Smarkm#endif
25961821Smarkm	Time_F(START);
26061821Smarkm	for (count=0,run=1; COND(cb); count+=4)
26161821Smarkm		{
26261821Smarkm		unsigned long data[2];
26361821Smarkm
26461821Smarkm		idea_encrypt(data,&sch);
26561821Smarkm		idea_encrypt(data,&sch);
26661821Smarkm		idea_encrypt(data,&sch);
26761821Smarkm		idea_encrypt(data,&sch);
26861821Smarkm		}
26961821Smarkm	d=Time_F(STOP);
27061821Smarkm	printf("%ld idea_encrypt's in %.2f second\n",count,d);
27161821Smarkm	b=((double)COUNT(cb)*8)/d;
27261821Smarkm
27361821Smarkm#ifdef SIGALRM
27461821Smarkm	printf("Doing idea_cbc_encrypt on %ld byte blocks for 10 seconds\n",
27561821Smarkm		BUFSIZE);
27661821Smarkm	alarm(10);
27761821Smarkm#else
27861821Smarkm	printf("Doing idea_cbc_encrypt %ld times on %ld byte blocks\n",cc,
27961821Smarkm		BUFSIZE);
28061821Smarkm#endif
28161821Smarkm	Time_F(START);
28261821Smarkm	for (count=0,run=1; COND(cc); count++)
28361821Smarkm		idea_cbc_encrypt(buf,buf,BUFSIZE,&sch,
28461821Smarkm			&(key[0]),IDEA_ENCRYPT);
28561821Smarkm	d=Time_F(STOP);
28661821Smarkm	printf("%ld idea_cbc_encrypt's of %ld byte blocks in %.2f second\n",
28761821Smarkm		count,BUFSIZE,d);
28861821Smarkm	c=((double)COUNT(cc)*BUFSIZE)/d;
28961821Smarkm
29061821Smarkm	printf("IDEA set_encrypt_key per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
29161821Smarkm	printf("IDEA set_decrypt_key per sec = %12.2f (%9.3fuS)\n",aa,1.0e6/aa);
29261821Smarkm	printf("IDEA raw ecb bytes   per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
29361821Smarkm	printf("IDEA cbc     bytes   per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
29461821Smarkm	exit(0);
295110007Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
29661821Smarkm	return(0);
29761821Smarkm#endif
29861821Smarkm	}
29961821Smarkm
300