1/* crypto/des/speed.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@mincom.oz.au).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@mincom.oz.au).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@mincom.oz.au)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
60/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
61
62#ifdef HAVE_CONFIG_H
63#include <config.h>
64#endif
65
66#if !defined(MSDOS) && !defined(WIN32)
67#define TIMES
68#endif
69
70#include <stdio.h>
71#ifdef HAVE_UNISTD_H
72#include <unistd.h>
73#endif
74#include <signal.h>
75#ifdef HAVE_TIME_H
76#include <time.h>
77#endif
78#ifdef HAVE_SYS_TYPES_H
79#include <sys/types.h>
80#endif
81#ifdef HAVE_SYS_TIMES_H
82#include <sys/times.h>
83#endif
84
85#ifdef VMS
86#include <types.h>
87struct tms {
88	time_t tms_utime;
89	time_t tms_stime;
90	time_t tms_uchild;	/* I dunno...  */
91	time_t tms_uchildsys;	/* so these names are a guess :-) */
92	}
93#endif
94
95#ifdef HAVE_SYS_TIMEB_H
96#include <sys/timeb.h>
97#endif
98
99#include <limits.h>
100#ifdef HAVE_SYS_PARAM_H
101#include <sys/param.h>
102#endif
103
104#include "des.h"
105
106/* The following if from times(3) man page.  It may need to be changed */
107#ifndef HZ
108#ifndef CLK_TCK
109#ifndef VMS
110#define HZ	100.0
111#else /* VMS */
112#define HZ	100.0
113#endif
114#else /* CLK_TCK */
115#define HZ ((double)CLK_TCK)
116#endif
117#endif
118
119#define BUFSIZE	((long)1024)
120long run=0;
121
122#ifndef NOPROTO
123double Time_F(int s);
124#else
125double Time_F();
126#endif
127
128#ifdef SIGALRM
129#if defined(__STDC__) || defined(sgi)
130#define SIGRETTYPE void
131#else
132#define SIGRETTYPE int
133#endif
134
135#ifndef NOPROTO
136SIGRETTYPE sig_done(int sig);
137#else
138SIGRETTYPE sig_done();
139#endif
140
141SIGRETTYPE sig_done(sig)
142int sig;
143	{
144	signal(SIGALRM,sig_done);
145	run=0;
146#ifdef LINT
147	sig=sig;
148#endif
149	}
150#endif
151
152#define START	0
153#define STOP	1
154
155double Time_F(s)
156int s;
157	{
158	double ret;
159#ifdef TIMES
160	static struct tms tstart,tend;
161
162	if (s == START)
163		{
164		times(&tstart);
165		return(0);
166		}
167	else
168		{
169		times(&tend);
170		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
171		return((ret == 0.0)?1e-6:ret);
172		}
173#else /* !times() */
174	static struct timeb tstart,tend;
175	long i;
176
177	if (s == START)
178		{
179		ftime(&tstart);
180		return(0);
181		}
182	else
183		{
184		ftime(&tend);
185		i=(long)tend.millitm-(long)tstart.millitm;
186		ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
187		return((ret == 0.0)?1e-6:ret);
188		}
189#endif
190	}
191
192int main(argc,argv)
193int argc;
194char **argv;
195	{
196	long count;
197	static unsigned char buf[BUFSIZE];
198	static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
199	static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
200	static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
201	des_key_schedule sch,sch2,sch3;
202	double a,b,c,d,e;
203#ifndef SIGALRM
204	long ca,cb,cc,cd,ce;
205#endif
206
207#ifndef TIMES
208	printf("To get the most acurate results, try to run this\n");
209	printf("program when this computer is idle.\n");
210#endif
211
212	des_set_key((C_Block *)key2,sch2);
213	des_set_key((C_Block *)key3,sch3);
214
215#ifndef SIGALRM
216	printf("First we calculate the approximate speed ...\n");
217	des_set_key((C_Block *)key,sch);
218	count=10;
219	do	{
220		long i;
221		DES_LONG data[2];
222
223		count*=2;
224		Time_F(START);
225		for (i=count; i; i--)
226			des_encrypt(data,&(sch[0]),DES_ENCRYPT);
227		d=Time_F(STOP);
228		} while (d < 3.0);
229	ca=count;
230	cb=count*3;
231	cc=count*3*8/BUFSIZE+1;
232	cd=count*8/BUFSIZE+1;
233	ce=count/20+1;
234	printf("Doing set_key %ld times\n",ca);
235#define COND(d)	(count != (d))
236#define COUNT(d) (d)
237#else
238#define COND(c)	(run)
239#define COUNT(d) (count)
240	signal(SIGALRM,sig_done);
241	printf("Doing set_key for 10 seconds\n");
242	alarm(10);
243#endif
244
245	Time_F(START);
246	for (count=0,run=1; COND(ca); count++)
247		des_set_key((C_Block *)key,sch);
248	d=Time_F(STOP);
249	printf("%ld set_key's in %.2f seconds\n",count,d);
250	a=((double)COUNT(ca))/d;
251
252#ifdef SIGALRM
253	printf("Doing des_encrypt's for 10 seconds\n");
254	alarm(10);
255#else
256	printf("Doing des_encrypt %ld times\n",cb);
257#endif
258	Time_F(START);
259	for (count=0,run=1; COND(cb); count++)
260		{
261		DES_LONG data[2];
262
263		des_encrypt(data,&(sch[0]),DES_ENCRYPT);
264		}
265	d=Time_F(STOP);
266	printf("%ld des_encrypt's in %.2f second\n",count,d);
267	b=((double)COUNT(cb)*8)/d;
268
269#ifdef SIGALRM
270	printf("Doing des_cbc_encrypt on %ld byte blocks for 10 seconds\n",
271		BUFSIZE);
272	alarm(10);
273#else
274	printf("Doing des_cbc_encrypt %ld times on %ld byte blocks\n",cc,
275		BUFSIZE);
276#endif
277	Time_F(START);
278	for (count=0,run=1; COND(cc); count++)
279		des_ncbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,&(sch[0]),
280			(C_Block *)&(key[0]),DES_ENCRYPT);
281	d=Time_F(STOP);
282	printf("%ld des_cbc_encrypt's of %ld byte blocks in %.2f second\n",
283		count,BUFSIZE,d);
284	c=((double)COUNT(cc)*BUFSIZE)/d;
285
286#ifdef SIGALRM
287	printf("Doing des_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
288		BUFSIZE);
289	alarm(10);
290#else
291	printf("Doing des_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,
292		BUFSIZE);
293#endif
294	Time_F(START);
295	for (count=0,run=1; COND(cd); count++)
296		des_ede3_cbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,
297			&(sch[0]),
298			&(sch2[0]),
299			&(sch3[0]),
300			(C_Block *)&(key[0]),
301			DES_ENCRYPT);
302	d=Time_F(STOP);
303	printf("%ld des_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
304		count,BUFSIZE,d);
305	d=((double)COUNT(cd)*BUFSIZE)/d;
306
307#ifdef SIGALRM
308	printf("Doing crypt for 10 seconds\n");
309	alarm(10);
310#else
311	printf("Doing crypt %ld times\n",ce);
312#endif
313	Time_F(START);
314	for (count=0,run=1; COND(ce); count++)
315		crypt("testing1","ef");
316	e=Time_F(STOP);
317	printf("%ld crypts in %.2f second\n",count,e);
318	e=((double)COUNT(ce))/e;
319
320	printf("set_key            per sec = %12.2f (%5.1fuS)\n",a,1.0e6/a);
321	printf("DES raw ecb bytes  per sec = %12.2f (%5.1fuS)\n",b,8.0e6/b);
322	printf("DES cbc bytes      per sec = %12.2f (%5.1fuS)\n",c,8.0e6/c);
323	printf("DES ede cbc bytes  per sec = %12.2f (%5.1fuS)\n",d,8.0e6/d);
324	printf("crypt              per sec = %12.2f (%5.1fuS)\n",e,1.0e6/e);
325	exit(0);
326#if defined(LINT) || defined(MSDOS)
327	return(0);
328#endif
329	}
330