Remove obsolete files from SSLeay 0.8.
[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 "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(sig)
166 int sig;
167         {
168         signal(SIGALRM,sig_done);
169         run=0;
170 #ifdef LINT
171         sig=sig;
172 #endif
173         }
174 #endif
175
176 #define START   0
177 #define STOP    1
178
179 double Time_F(s)
180 int s;
181         {
182         double ret;
183 #ifdef TIMES
184         static struct tms tstart,tend;
185
186         if (s == START)
187                 {
188                 times(&tstart);
189                 return(0);
190                 }
191         else
192                 {
193                 times(&tend);
194                 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
195                 return((ret == 0.0)?1e-6:ret);
196                 }
197 #else /* !times() */
198         static struct timeb tstart,tend;
199         long i;
200
201         if (s == START)
202                 {
203                 ftime(&tstart);
204                 return(0);
205                 }
206         else
207                 {
208                 ftime(&tend);
209                 i=(long)tend.millitm-(long)tstart.millitm;
210                 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
211                 return((ret == 0.0)?1e-6:ret);
212                 }
213 #endif
214         }
215
216 #ifdef SIGALRM
217 #define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
218 #else
219 #define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
220 #endif
221         
222 #define time_it(func,name,index) \
223         print_name(name); \
224         Time_F(START); \
225         for (count=0,run=1; COND(cb); count+=4) \
226                 { \
227                 unsigned long d[2]; \
228                 func(d,&sch); \
229                 func(d,&sch); \
230                 func(d,&sch); \
231                 func(d,&sch); \
232                 } \
233         tm[index]=Time_F(STOP); \
234         fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
235         tm[index]=((double)COUNT(cb))/tm[index];
236
237 #define print_it(name,index) \
238         fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
239                 tm[index]*8,1.0e6/tm[index]);
240
241 int main(argc,argv)
242 int argc;
243 char **argv;
244         {
245         long count;
246         static unsigned char buf[BUFSIZE];
247         static char key[16]={   0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
248                                 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
249         BF_KEY sch;
250         double d,tm[16],max=0;
251         int rank[16];
252         char *str[16];
253         int max_idx=0,i,num=0,j;
254 #ifndef SIGALARM
255         long ca,cb,cc,cd,ce;
256 #endif
257
258         for (i=0; i<12; i++)
259                 {
260                 tm[i]=0.0;
261                 rank[i]=0;
262                 }
263
264 #ifndef TIMES
265         fprintf(stderr,"To get the most acurate results, try to run this\n");
266         fprintf(stderr,"program when this computer is idle.\n");
267 #endif
268
269         BF_set_key(&sch,16,key);
270
271 #ifndef SIGALRM
272         fprintf(stderr,"First we calculate the approximate speed ...\n");
273         count=10;
274         do      {
275                 long i;
276                 unsigned long data[2];
277
278                 count*=2;
279                 Time_F(START);
280                 for (i=count; i; i--)
281                         BF_encrypt(data,&sch);
282                 d=Time_F(STOP);
283                 } while (d < 3.0);
284         ca=count;
285         cb=count*3;
286         cc=count*3*8/BUFSIZE+1;
287         cd=count*8/BUFSIZE+1;
288
289         ce=count/20+1;
290 #define COND(d) (count != (d))
291 #define COUNT(d) (d)
292 #else
293 #define COND(c) (run)
294 #define COUNT(d) (count)
295         signal(SIGALRM,sig_done);
296         alarm(10);
297 #endif
298
299         time_it(BF_encrypt_normal,      "BF_encrypt_normal ", 0);
300         time_it(BF_encrypt_ptr,         "BF_encrypt_ptr    ", 1);
301         time_it(BF_encrypt_ptr2,        "BF_encrypt_ptr2   ", 2);
302         num+=3;
303
304         str[0]="<nothing>";
305         print_it("BF_encrypt_normal ",0);
306         max=tm[0];
307         max_idx=0;
308         str[1]="ptr      ";
309         print_it("BF_encrypt_ptr ",1);
310         if (max < tm[1]) { max=tm[1]; max_idx=1; }
311         str[2]="ptr2     ";
312         print_it("BF_encrypt_ptr2 ",2);
313         if (max < tm[2]) { max=tm[2]; max_idx=2; }
314
315         printf("options    BF ecb/s\n");
316         printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
317         d=tm[max_idx];
318         tm[max_idx]= -2.0;
319         max= -1.0;
320         for (;;)
321                 {
322                 for (i=0; i<3; i++)
323                         {
324                         if (max < tm[i]) { max=tm[i]; j=i; }
325                         }
326                 if (max < 0.0) break;
327                 printf("%s %12.2f  %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
328                 tm[j]= -2.0;
329                 max= -1.0;
330                 }
331
332         switch (max_idx)
333                 {
334         case 0:
335                 printf("-DBF_DEFAULT_OPTIONS\n");
336                 break;
337         case 1:
338                 printf("-DBF_PTR\n");
339                 break;
340         case 2:
341                 printf("-DBF_PTR2\n");
342                 break;
343                 }
344         exit(0);
345 #if defined(LINT) || defined(MSDOS)
346         return(0);
347 #endif
348         }