Merge from vendor branch NTPD:
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / des / speed.c
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 #include <stdlib.h>
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75 #include <signal.h>
76 #ifdef HAVE_TIME_H
77 #include <time.h>
78 #endif
79 #ifdef HAVE_SYS_TYPES_H
80 #include <sys/types.h>
81 #endif
82 #ifdef HAVE_SYS_TIMES_H
83 #include <sys/times.h>
84 #endif
85
86 #ifdef VMS
87 #include <types.h>
88 struct tms {
89         time_t tms_utime;
90         time_t tms_stime;
91         time_t tms_uchild;      /* I dunno...  */
92         time_t tms_uchildsys;   /* so these names are a guess :-) */
93         }
94 #endif
95
96 #ifdef HAVE_SYS_TIMEB_H
97 #include <sys/timeb.h>
98 #endif
99
100 #include <limits.h>
101 #ifdef HAVE_SYS_PARAM_H
102 #include <sys/param.h>
103 #endif
104
105 #include "des.h"
106
107 /* The following if from times(3) man page.  It may need to be changed */
108 #ifndef HZ
109 #ifndef CLK_TCK
110 #ifndef VMS
111 #define HZ      100.0
112 #else /* VMS */
113 #define HZ      100.0
114 #endif
115 #else /* CLK_TCK */
116 #define HZ ((double)CLK_TCK)
117 #endif
118 #endif
119
120 #define BUFSIZE ((long)1024)
121 long run=0;
122
123 #ifndef NOPROTO
124 double Time_F(int s);
125 #else
126 double Time_F();
127 #endif
128
129 #ifdef SIGALRM
130 #if defined(__STDC__) || defined(sgi)
131 #define SIGRETTYPE void
132 #else
133 #define SIGRETTYPE int
134 #endif
135
136 #ifndef NOPROTO
137 SIGRETTYPE sig_done(int sig);
138 #else
139 SIGRETTYPE sig_done();
140 #endif
141
142 SIGRETTYPE sig_done(sig)
143 int sig;
144         {
145         signal(SIGALRM,sig_done);
146         run=0;
147 #ifdef LINT
148         sig=sig;
149 #endif
150         }
151 #endif
152
153 #define START   0
154 #define STOP    1
155
156 double Time_F(s)
157 int s;
158         {
159         double ret;
160 #ifdef TIMES
161         static struct tms tstart,tend;
162
163         if (s == START)
164                 {
165                 times(&tstart);
166                 return(0);
167                 }
168         else
169                 {
170                 times(&tend);
171                 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
172                 return((ret == 0.0)?1e-6:ret);
173                 }
174 #else /* !times() */
175         static struct timeb tstart,tend;
176         long i;
177
178         if (s == START)
179                 {
180                 ftime(&tstart);
181                 return(0);
182                 }
183         else
184                 {
185                 ftime(&tend);
186                 i=(long)tend.millitm-(long)tstart.millitm;
187                 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
188                 return((ret == 0.0)?1e-6:ret);
189                 }
190 #endif
191         }
192
193 int main(argc,argv)
194 int argc;
195 char **argv;
196         {
197         long count;
198         static unsigned char buf[BUFSIZE];
199         static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
200         static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
201         static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
202         des_key_schedule sch,sch2,sch3;
203         double a,b,c,d,e;
204 #ifndef SIGALRM
205         long ca,cb,cc,cd,ce;
206 #endif
207
208 #ifndef TIMES
209         printf("To get the most acurate results, try to run this\n");
210         printf("program when this computer is idle.\n");
211 #endif
212
213         des_set_key((C_Block *)key2,sch2);
214         des_set_key((C_Block *)key3,sch3);
215
216 #ifndef SIGALRM
217         printf("First we calculate the approximate speed ...\n");
218         des_set_key((C_Block *)key,sch);
219         count=10;
220         do      {
221                 long i;
222                 DES_LONG data[2];
223
224                 count*=2;
225                 Time_F(START);
226                 for (i=count; i; i--)
227                         des_encrypt(data,&(sch[0]),DES_ENCRYPT);
228                 d=Time_F(STOP);
229                 } while (d < 3.0);
230         ca=count;
231         cb=count*3;
232         cc=count*3*8/BUFSIZE+1;
233         cd=count*8/BUFSIZE+1;
234         ce=count/20+1;
235         printf("Doing set_key %ld times\n",ca);
236 #define COND(d) (count != (d))
237 #define COUNT(d) (d)
238 #else
239 #define COND(c) (run)
240 #define COUNT(d) (count)
241         signal(SIGALRM,sig_done);
242         printf("Doing set_key for 10 seconds\n");
243         alarm(10);
244 #endif
245
246         Time_F(START);
247         for (count=0,run=1; COND(ca); count++)
248                 des_set_key((C_Block *)key,sch);
249         d=Time_F(STOP);
250         printf("%ld set_key's in %.2f seconds\n",count,d);
251         a=((double)COUNT(ca))/d;
252
253 #ifdef SIGALRM
254         printf("Doing des_encrypt's for 10 seconds\n");
255         alarm(10);
256 #else
257         printf("Doing des_encrypt %ld times\n",cb);
258 #endif
259         Time_F(START);
260         for (count=0,run=1; COND(cb); count++)
261                 {
262                 DES_LONG data[2];
263
264                 des_encrypt(data,&(sch[0]),DES_ENCRYPT);
265                 }
266         d=Time_F(STOP);
267         printf("%ld des_encrypt's in %.2f second\n",count,d);
268         b=((double)COUNT(cb)*8)/d;
269
270 #ifdef SIGALRM
271         printf("Doing des_cbc_encrypt on %ld byte blocks for 10 seconds\n",
272                 BUFSIZE);
273         alarm(10);
274 #else
275         printf("Doing des_cbc_encrypt %ld times on %ld byte blocks\n",cc,
276                 BUFSIZE);
277 #endif
278         Time_F(START);
279         for (count=0,run=1; COND(cc); count++)
280                 des_ncbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,&(sch[0]),
281                         (C_Block *)&(key[0]),DES_ENCRYPT);
282         d=Time_F(STOP);
283         printf("%ld des_cbc_encrypt's of %ld byte blocks in %.2f second\n",
284                 count,BUFSIZE,d);
285         c=((double)COUNT(cc)*BUFSIZE)/d;
286
287 #ifdef SIGALRM
288         printf("Doing des_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
289                 BUFSIZE);
290         alarm(10);
291 #else
292         printf("Doing des_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,
293                 BUFSIZE);
294 #endif
295         Time_F(START);
296         for (count=0,run=1; COND(cd); count++)
297                 des_ede3_cbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,
298                         &(sch[0]),
299                         &(sch2[0]),
300                         &(sch3[0]),
301                         (C_Block *)&(key[0]),
302                         DES_ENCRYPT);
303         d=Time_F(STOP);
304         printf("%ld des_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
305                 count,BUFSIZE,d);
306         d=((double)COUNT(cd)*BUFSIZE)/d;
307
308 #ifdef SIGALRM
309         printf("Doing crypt for 10 seconds\n");
310         alarm(10);
311 #else
312         printf("Doing crypt %ld times\n",ce);
313 #endif
314         Time_F(START);
315         for (count=0,run=1; COND(ce); count++)
316                 crypt("testing1","ef");
317         e=Time_F(STOP);
318         printf("%ld crypts in %.2f second\n",count,e);
319         e=((double)COUNT(ce))/e;
320
321         printf("set_key            per sec = %12.2f (%5.1fuS)\n",a,1.0e6/a);
322         printf("DES raw ecb bytes  per sec = %12.2f (%5.1fuS)\n",b,8.0e6/b);
323         printf("DES cbc bytes      per sec = %12.2f (%5.1fuS)\n",c,8.0e6/c);
324         printf("DES ede cbc bytes  per sec = %12.2f (%5.1fuS)\n",d,8.0e6/d);
325         printf("crypt              per sec = %12.2f (%5.1fuS)\n",e,1.0e6/e);
326         exit(0);
327 #if defined(LINT) || defined(MSDOS)
328         return(0);
329 #endif
330         }