5cfa60c5373210a0a044a6603c7f38d3539c9ddb
[openssl.git] / crypto / bf / bf_opts.c
1 /* crypto/bf/bf_opts.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
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@cryptsoft.com).
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@cryptsoft.com)"
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@cryptsoft.com)"
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 /* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
60  * This is for machines with 64k code segment size restrictions. */
61
62 #ifndef MSDOS
63 #define TIMES
64 #endif
65
66 #include <stdio.h>
67 #ifndef MSDOS
68 #include <unistd.h>
69 #else
70 #include <io.h>
71 extern void exit();
72 #endif
73 #include <signal.h>
74 #ifndef VMS
75 #ifndef _IRIX
76 #include <time.h>
77 #endif
78 #ifdef TIMES
79 #include <sys/types.h>
80 #include <sys/times.h>
81 #endif
82 #else /* VMS */
83 #include <types.h>
84 struct tms {
85         time_t tms_utime;
86         time_t tms_stime;
87         time_t tms_uchild;      /* I dunno...  */
88         time_t tms_uchildsys;   /* so these names are a guess :-) */
89         }
90 #endif
91 #ifndef TIMES
92 #include <sys/timeb.h>
93 #endif
94
95 #ifdef sun
96 #include <limits.h>
97 #include <sys/param.h>
98 #endif
99
100 #include "blowfish.h"
101
102 #define BF_DEFAULT_OPTIONS
103
104 #undef BF_ENC
105 #define BF_encrypt  BF_encrypt_normal
106 #undef HEADER_BF_LOCL_H
107 #include "bf_enc.c"
108
109 #define BF_PTR
110 #undef BF_PTR2
111 #undef BF_ENC
112 #undef BF_encrypt
113 #define BF_encrypt  BF_encrypt_ptr
114 #undef HEADER_BF_LOCL_H
115 #include "bf_enc.c"
116
117 #undef BF_PTR
118 #define BF_PTR2
119 #undef BF_ENC
120 #undef BF_encrypt
121 #define BF_encrypt  BF_encrypt_ptr2
122 #undef HEADER_BF_LOCL_H
123 #include "bf_enc.c"
124
125 /* The following if from times(3) man page.  It may need to be changed */
126 #ifndef HZ
127 # ifndef CLK_TCK
128 #  ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
129 #   ifndef VMS
130 #    define HZ  100.0
131 #   else /* VMS */
132 #    define HZ  100.0
133 #   endif
134 #  else /* _BSD_CLK_TCK_ */
135 #   define HZ ((double)_BSD_CLK_TCK_)
136 #  endif
137 # else /* CLK_TCK */
138 #  define HZ ((double)CLK_TCK)
139 # endif
140 #endif
141
142 #define BUFSIZE ((long)1024)
143 long run=0;
144
145 #ifndef NOPROTO
146 double Time_F(int s);
147 #else
148 double Time_F();
149 #endif
150
151 #ifdef SIGALRM
152 #if defined(__STDC__) || defined(sgi)
153 #define SIGRETTYPE void
154 #else
155 #define SIGRETTYPE int
156 #endif
157
158 #ifndef NOPROTO
159 SIGRETTYPE sig_done(int sig);
160 #else
161 SIGRETTYPE sig_done();
162 #endif
163
164 SIGRETTYPE sig_done(sig)
165 int sig;
166         {
167         signal(SIGALRM,sig_done);
168         run=0;
169 #ifdef LINT
170         sig=sig;
171 #endif
172         }
173 #endif
174
175 #define START   0
176 #define STOP    1
177
178 double Time_F(s)
179 int s;
180         {
181         double ret;
182 #ifdef TIMES
183         static struct tms tstart,tend;
184
185         if (s == START)
186                 {
187                 times(&tstart);
188                 return(0);
189                 }
190         else
191                 {
192                 times(&tend);
193                 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
194                 return((ret == 0.0)?1e-6:ret);
195                 }
196 #else /* !times() */
197         static struct timeb tstart,tend;
198         long i;
199
200         if (s == START)
201                 {
202                 ftime(&tstart);
203                 return(0);
204                 }
205         else
206                 {
207                 ftime(&tend);
208                 i=(long)tend.millitm-(long)tstart.millitm;
209                 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
210                 return((ret == 0.0)?1e-6:ret);
211                 }
212 #endif
213         }
214
215 #ifdef SIGALRM
216 #define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
217 #else
218 #define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
219 #endif
220         
221 #define time_it(func,name,index) \
222         print_name(name); \
223         Time_F(START); \
224         for (count=0,run=1; COND(cb); count+=4) \
225                 { \
226                 unsigned long d[2]; \
227                 func(d,&sch); \
228                 func(d,&sch); \
229                 func(d,&sch); \
230                 func(d,&sch); \
231                 } \
232         tm[index]=Time_F(STOP); \
233         fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
234         tm[index]=((double)COUNT(cb))/tm[index];
235
236 #define print_it(name,index) \
237         fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
238                 tm[index]*8,1.0e6/tm[index]);
239
240 int main(argc,argv)
241 int argc;
242 char **argv;
243         {
244         long count;
245         static unsigned char buf[BUFSIZE];
246         static char key[16]={   0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
247                                 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
248         BF_KEY sch;
249         double d,tm[16],max=0;
250         int rank[16];
251         char *str[16];
252         int max_idx=0,i,num=0,j;
253 #ifndef SIGALARM
254         long ca,cb,cc,cd,ce;
255 #endif
256
257         for (i=0; i<12; i++)
258                 {
259                 tm[i]=0.0;
260                 rank[i]=0;
261                 }
262
263 #ifndef TIMES
264         fprintf(stderr,"To get the most acurate results, try to run this\n");
265         fprintf(stderr,"program when this computer is idle.\n");
266 #endif
267
268         BF_set_key(&sch,16,key);
269
270 #ifndef SIGALRM
271         fprintf(stderr,"First we calculate the approximate speed ...\n");
272         count=10;
273         do      {
274                 long i;
275                 unsigned long data[2];
276
277                 count*=2;
278                 Time_F(START);
279                 for (i=count; i; i--)
280                         BF_encrypt(data,&sch);
281                 d=Time_F(STOP);
282                 } while (d < 3.0);
283         ca=count;
284         cb=count*3;
285         cc=count*3*8/BUFSIZE+1;
286         cd=count*8/BUFSIZE+1;
287
288         ce=count/20+1;
289 #define COND(d) (count != (d))
290 #define COUNT(d) (d)
291 #else
292 #define COND(c) (run)
293 #define COUNT(d) (count)
294         signal(SIGALRM,sig_done);
295         alarm(10);
296 #endif
297
298         time_it(BF_encrypt_normal,      "BF_encrypt_normal ", 0);
299         time_it(BF_encrypt_ptr,         "BF_encrypt_ptr    ", 1);
300         time_it(BF_encrypt_ptr2,        "BF_encrypt_ptr2   ", 2);
301         num+=3;
302
303         str[0]="<nothing>";
304         print_it("BF_encrypt_normal ",0);
305         max=tm[0];
306         max_idx=0;
307         str[1]="ptr      ";
308         print_it("BF_encrypt_ptr ",1);
309         if (max < tm[1]) { max=tm[1]; max_idx=1; }
310         str[2]="ptr2     ";
311         print_it("BF_encrypt_ptr2 ",2);
312         if (max < tm[2]) { max=tm[2]; max_idx=2; }
313
314         printf("options    BF ecb/s\n");
315         printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
316         d=tm[max_idx];
317         tm[max_idx]= -2.0;
318         max= -1.0;
319         for (;;)
320                 {
321                 for (i=0; i<3; i++)
322                         {
323                         if (max < tm[i]) { max=tm[i]; j=i; }
324                         }
325                 if (max < 0.0) break;
326                 printf("%s %12.2f  %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
327                 tm[j]= -2.0;
328                 max= -1.0;
329                 }
330
331         switch (max_idx)
332                 {
333         case 0:
334                 printf("-DBF_DEFAULT_OPTIONS\n");
335                 break;
336         case 1:
337                 printf("-DBF_PTR\n");
338                 break;
339         case 2:
340                 printf("-DBF_PTR2\n");
341                 break;
342                 }
343         exit(0);
344 #if defined(LINT) || defined(MSDOS)
345         return(0);
346 #endif
347         }