f63fa8e168ea8b3b8bd54d5ce2cbb2635ea50dfe
[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 #if defined(sun) || defined(__ultrix)
96 #define _POSIX_SOURCE
97 #include <limits.h>
98 #include <sys/param.h>
99 #endif
100
101 #include <openssl/blowfish.h>
102
103 #define BF_DEFAULT_OPTIONS
104
105 #undef BF_ENC
106 #define BF_encrypt  BF_encrypt_normal
107 #undef HEADER_BF_LOCL_H
108 #include "bf_enc.c"
109
110 #define BF_PTR
111 #undef BF_PTR2
112 #undef BF_ENC
113 #undef BF_encrypt
114 #define BF_encrypt  BF_encrypt_ptr
115 #undef HEADER_BF_LOCL_H
116 #include "bf_enc.c"
117
118 #undef BF_PTR
119 #define BF_PTR2
120 #undef BF_ENC
121 #undef BF_encrypt
122 #define BF_encrypt  BF_encrypt_ptr2
123 #undef HEADER_BF_LOCL_H
124 #include "bf_enc.c"
125
126 /* The following if from times(3) man page.  It may need to be changed */
127 #ifndef HZ
128 # ifndef CLK_TCK
129 #  ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
130 #   ifndef VMS
131 #    define HZ  100.0
132 #   else /* VMS */
133 #    define HZ  100.0
134 #   endif
135 #  else /* _BSD_CLK_TCK_ */
136 #   define HZ ((double)_BSD_CLK_TCK_)
137 #  endif
138 # else /* CLK_TCK */
139 #  define HZ ((double)CLK_TCK)
140 # endif
141 #endif
142
143 #define BUFSIZE ((long)1024)
144 long run=0;
145
146 #ifndef NOPROTO
147 double Time_F(int s);
148 #else
149 double Time_F();
150 #endif
151
152 #ifdef SIGALRM
153 #if defined(__STDC__) || defined(sgi)
154 #define SIGRETTYPE void
155 #else
156 #define SIGRETTYPE int
157 #endif
158
159 #ifndef NOPROTO
160 SIGRETTYPE sig_done(int sig);
161 #else
162 SIGRETTYPE sig_done();
163 #endif
164
165 SIGRETTYPE sig_done(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(int s)
179         {
180         double ret;
181 #ifdef TIMES
182         static struct tms tstart,tend;
183
184         if (s == START)
185                 {
186                 times(&tstart);
187                 return(0);
188                 }
189         else
190                 {
191                 times(&tend);
192                 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
193                 return((ret == 0.0)?1e-6:ret);
194                 }
195 #else /* !times() */
196         static struct timeb tstart,tend;
197         long i;
198
199         if (s == START)
200                 {
201                 ftime(&tstart);
202                 return(0);
203                 }
204         else
205                 {
206                 ftime(&tend);
207                 i=(long)tend.millitm-(long)tstart.millitm;
208                 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
209                 return((ret == 0.0)?1e-6:ret);
210                 }
211 #endif
212         }
213
214 #ifdef SIGALRM
215 #define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
216 #else
217 #define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
218 #endif
219         
220 #define time_it(func,name,index) \
221         print_name(name); \
222         Time_F(START); \
223         for (count=0,run=1; COND(cb); count+=4) \
224                 { \
225                 unsigned long d[2]; \
226                 func(d,&sch); \
227                 func(d,&sch); \
228                 func(d,&sch); \
229                 func(d,&sch); \
230                 } \
231         tm[index]=Time_F(STOP); \
232         fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
233         tm[index]=((double)COUNT(cb))/tm[index];
234
235 #define print_it(name,index) \
236         fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
237                 tm[index]*8,1.0e6/tm[index]);
238
239 int main(int argc, char **argv)
240         {
241         long count;
242         static unsigned char buf[BUFSIZE];
243         static char key[16]={   0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
244                                 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
245         BF_KEY sch;
246         double d,tm[16],max=0;
247         int rank[16];
248         char *str[16];
249         int max_idx=0,i,num=0,j;
250 #ifndef SIGALARM
251         long ca,cb,cc,cd,ce;
252 #endif
253
254         for (i=0; i<12; i++)
255                 {
256                 tm[i]=0.0;
257                 rank[i]=0;
258                 }
259
260 #ifndef TIMES
261         fprintf(stderr,"To get the most acurate results, try to run this\n");
262         fprintf(stderr,"program when this computer is idle.\n");
263 #endif
264
265         BF_set_key(&sch,16,key);
266
267 #ifndef SIGALRM
268         fprintf(stderr,"First we calculate the approximate speed ...\n");
269         count=10;
270         do      {
271                 long i;
272                 unsigned long data[2];
273
274                 count*=2;
275                 Time_F(START);
276                 for (i=count; i; i--)
277                         BF_encrypt(data,&sch);
278                 d=Time_F(STOP);
279                 } while (d < 3.0);
280         ca=count;
281         cb=count*3;
282         cc=count*3*8/BUFSIZE+1;
283         cd=count*8/BUFSIZE+1;
284
285         ce=count/20+1;
286 #define COND(d) (count != (d))
287 #define COUNT(d) (d)
288 #else
289 #define COND(c) (run)
290 #define COUNT(d) (count)
291         signal(SIGALRM,sig_done);
292         alarm(10);
293 #endif
294
295         time_it(BF_encrypt_normal,      "BF_encrypt_normal ", 0);
296         time_it(BF_encrypt_ptr,         "BF_encrypt_ptr    ", 1);
297         time_it(BF_encrypt_ptr2,        "BF_encrypt_ptr2   ", 2);
298         num+=3;
299
300         str[0]="<nothing>";
301         print_it("BF_encrypt_normal ",0);
302         max=tm[0];
303         max_idx=0;
304         str[1]="ptr      ";
305         print_it("BF_encrypt_ptr ",1);
306         if (max < tm[1]) { max=tm[1]; max_idx=1; }
307         str[2]="ptr2     ";
308         print_it("BF_encrypt_ptr2 ",2);
309         if (max < tm[2]) { max=tm[2]; max_idx=2; }
310
311         printf("options    BF ecb/s\n");
312         printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
313         d=tm[max_idx];
314         tm[max_idx]= -2.0;
315         max= -1.0;
316         for (;;)
317                 {
318                 for (i=0; i<3; i++)
319                         {
320                         if (max < tm[i]) { max=tm[i]; j=i; }
321                         }
322                 if (max < 0.0) break;
323                 printf("%s %12.2f  %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
324                 tm[j]= -2.0;
325                 max= -1.0;
326                 }
327
328         switch (max_idx)
329                 {
330         case 0:
331                 printf("-DBF_DEFAULT_OPTIONS\n");
332                 break;
333         case 1:
334                 printf("-DBF_PTR\n");
335                 break;
336         case 2:
337                 printf("-DBF_PTR2\n");
338                 break;
339                 }
340         exit(0);
341 #if defined(LINT) || defined(MSDOS)
342         return(0);
343 #endif
344         }