155714Skris/* crypto/bf/bf_opts.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59280304Sjkim/*
60280304Sjkim * define PART1, PART2, PART3 or PART4 to build only with a few of the
61280304Sjkim * options. This is for machines with 64k code segment size restrictions.
62280304Sjkim */
6355714Skris
64109998Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
65280304Sjkim# define TIMES
6655714Skris#endif
6755714Skris
6855714Skris#include <stdio.h>
6955714Skris
7055714Skris#include <openssl/e_os2.h>
7155714Skris#include OPENSSL_UNISTD_IO
7255714SkrisOPENSSL_DECLARE_EXIT
73160814Ssimon#ifndef OPENSSL_SYS_NETWARE
74280304Sjkim# include <signal.h>
75160814Ssimon#endif
7655714Skris#ifndef _IRIX
77280304Sjkim# include <time.h>
7855714Skris#endif
7955714Skris#ifdef TIMES
80280304Sjkim# include <sys/types.h>
81280304Sjkim# include <sys/times.h>
8255714Skris#endif
83280304Sjkim    /*
84280304Sjkim     * Depending on the VMS version, the tms structure is perhaps defined.
85280304Sjkim     * The __TMS macro will show if it was.  If it wasn't defined, we should
86280304Sjkim     * undefine TIMES, since that tells the rest of the program how things
87280304Sjkim     * should be handled.  -- Richard Levitte
88280304Sjkim     */
89109998Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
90280304Sjkim# undef TIMES
9155714Skris#endif
9255714Skris#ifndef TIMES
93280304Sjkim# include <sys/timeb.h>
9455714Skris#endif
9555714Skris#if defined(sun) || defined(__ultrix)
96280304Sjkim# define _POSIX_SOURCE
97280304Sjkim# include <limits.h>
98280304Sjkim# include <sys/param.h>
9955714Skris#endif
10055714Skris#include <openssl/blowfish.h>
10155714Skris#define BF_DEFAULT_OPTIONS
10255714Skris#undef BF_ENC
10355714Skris#define BF_encrypt  BF_encrypt_normal
10455714Skris#undef HEADER_BF_LOCL_H
10555714Skris#include "bf_enc.c"
10655714Skris#define BF_PTR
10755714Skris#undef BF_PTR2
10855714Skris#undef BF_ENC
10955714Skris#undef BF_encrypt
11055714Skris#define BF_encrypt  BF_encrypt_ptr
11155714Skris#undef HEADER_BF_LOCL_H
11255714Skris#include "bf_enc.c"
11355714Skris#undef BF_PTR
11455714Skris#define BF_PTR2
11555714Skris#undef BF_ENC
11655714Skris#undef BF_encrypt
11755714Skris#define BF_encrypt  BF_encrypt_ptr2
11855714Skris#undef HEADER_BF_LOCL_H
11955714Skris#include "bf_enc.c"
12055714Skris/* The following if from times(3) man page.  It may need to be changed */
12155714Skris#ifndef HZ
12255714Skris# ifndef CLK_TCK
123280304Sjkim#  ifndef _BSD_CLK_TCK_         /* FreeBSD fix */
124280304Sjkim#   define HZ   100.0
125280304Sjkim#  else                         /* _BSD_CLK_TCK_ */
12655714Skris#   define HZ ((double)_BSD_CLK_TCK_)
12755714Skris#  endif
128280304Sjkim# else                          /* CLK_TCK */
12955714Skris#  define HZ ((double)CLK_TCK)
13055714Skris# endif
13155714Skris#endif
132280304Sjkim#define BUFSIZE ((long)1024)
133280304Sjkimlong run = 0;
13455714Skris
13555714Skrisdouble Time_F(int s);
13655714Skris#ifdef SIGALRM
137280304Sjkim# if defined(__STDC__) || defined(sgi)
138280304Sjkim#  define SIGRETTYPE void
139280304Sjkim# else
140280304Sjkim#  define SIGRETTYPE int
141280304Sjkim# endif
14255714Skris
14355714SkrisSIGRETTYPE sig_done(int sig);
14455714SkrisSIGRETTYPE sig_done(int sig)
145280304Sjkim{
146280304Sjkim    signal(SIGALRM, sig_done);
147280304Sjkim    run = 0;
148280304Sjkim# ifdef LINT
149280304Sjkim    sig = sig;
150280304Sjkim# endif
151280304Sjkim}
15255714Skris#endif
15355714Skris
154280304Sjkim#define START   0
155280304Sjkim#define STOP    1
15655714Skris
15755714Skrisdouble Time_F(int s)
158280304Sjkim{
159280304Sjkim    double ret;
16055714Skris#ifdef TIMES
161280304Sjkim    static struct tms tstart, tend;
16255714Skris
163280304Sjkim    if (s == START) {
164280304Sjkim        times(&tstart);
165280304Sjkim        return (0);
166280304Sjkim    } else {
167280304Sjkim        times(&tend);
168280304Sjkim        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
169280304Sjkim        return ((ret == 0.0) ? 1e-6 : ret);
170280304Sjkim    }
171280304Sjkim#else                           /* !times() */
172280304Sjkim    static struct timeb tstart, tend;
173280304Sjkim    long i;
17455714Skris
175280304Sjkim    if (s == START) {
176280304Sjkim        ftime(&tstart);
177280304Sjkim        return (0);
178280304Sjkim    } else {
179280304Sjkim        ftime(&tend);
180280304Sjkim        i = (long)tend.millitm - (long)tstart.millitm;
181280304Sjkim        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1000.0;
182280304Sjkim        return ((ret == 0.0) ? 1e-6 : ret);
183280304Sjkim    }
18455714Skris#endif
185280304Sjkim}
18655714Skris
18755714Skris#ifdef SIGALRM
188280304Sjkim# define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
18955714Skris#else
190280304Sjkim# define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
19155714Skris#endif
192280304Sjkim
19355714Skris#define time_it(func,name,index) \
194280304Sjkim        print_name(name); \
195280304Sjkim        Time_F(START); \
196280304Sjkim        for (count=0,run=1; COND(cb); count+=4) \
197280304Sjkim                { \
198280304Sjkim                unsigned long d[2]; \
199280304Sjkim                func(d,&sch); \
200280304Sjkim                func(d,&sch); \
201280304Sjkim                func(d,&sch); \
202280304Sjkim                func(d,&sch); \
203280304Sjkim                } \
204280304Sjkim        tm[index]=Time_F(STOP); \
205280304Sjkim        fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
206280304Sjkim        tm[index]=((double)COUNT(cb))/tm[index];
20755714Skris
20855714Skris#define print_it(name,index) \
209280304Sjkim        fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
210280304Sjkim                tm[index]*8,1.0e6/tm[index]);
21155714Skris
21255714Skrisint main(int argc, char **argv)
213280304Sjkim{
214280304Sjkim    long count;
215280304Sjkim    static unsigned char buf[BUFSIZE];
216280304Sjkim    static char key[16] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
217280304Sjkim        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
218280304Sjkim    };
219280304Sjkim    BF_KEY sch;
220280304Sjkim    double d, tm[16], max = 0;
221280304Sjkim    int rank[16];
222280304Sjkim    char *str[16];
223280304Sjkim    int max_idx = 0, i, num = 0, j;
22455714Skris#ifndef SIGALARM
225280304Sjkim    long ca, cb, cc, cd, ce;
22655714Skris#endif
22755714Skris
228280304Sjkim    for (i = 0; i < 12; i++) {
229280304Sjkim        tm[i] = 0.0;
230280304Sjkim        rank[i] = 0;
231280304Sjkim    }
23255714Skris
23355714Skris#ifndef TIMES
234280304Sjkim    fprintf(stderr, "To get the most accurate results, try to run this\n");
235280304Sjkim    fprintf(stderr, "program when this computer is idle.\n");
23655714Skris#endif
23755714Skris
238280304Sjkim    BF_set_key(&sch, 16, key);
23955714Skris
24055714Skris#ifndef SIGALRM
241280304Sjkim    fprintf(stderr, "First we calculate the approximate speed ...\n");
242280304Sjkim    count = 10;
243280304Sjkim    do {
244280304Sjkim        long i;
245280304Sjkim        unsigned long data[2];
24655714Skris
247280304Sjkim        count *= 2;
248280304Sjkim        Time_F(START);
249280304Sjkim        for (i = count; i; i--)
250280304Sjkim            BF_encrypt(data, &sch);
251280304Sjkim        d = Time_F(STOP);
252280304Sjkim    } while (d < 3.0);
253280304Sjkim    ca = count;
254280304Sjkim    cb = count * 3;
255280304Sjkim    cc = count * 3 * 8 / BUFSIZE + 1;
256280304Sjkim    cd = count * 8 / BUFSIZE + 1;
25755714Skris
258280304Sjkim    ce = count / 20 + 1;
259280304Sjkim# define COND(d) (count != (d))
260280304Sjkim# define COUNT(d) (d)
26155714Skris#else
262280304Sjkim# define COND(c) (run)
263280304Sjkim# define COUNT(d) (count)
264280304Sjkim    signal(SIGALRM, sig_done);
265280304Sjkim    alarm(10);
26655714Skris#endif
26755714Skris
268280304Sjkim    time_it(BF_encrypt_normal, "BF_encrypt_normal ", 0);
269280304Sjkim    time_it(BF_encrypt_ptr, "BF_encrypt_ptr    ", 1);
270280304Sjkim    time_it(BF_encrypt_ptr2, "BF_encrypt_ptr2   ", 2);
271280304Sjkim    num += 3;
27255714Skris
273280304Sjkim    str[0] = "<nothing>";
274280304Sjkim    print_it("BF_encrypt_normal ", 0);
275280304Sjkim    max = tm[0];
276280304Sjkim    max_idx = 0;
277280304Sjkim    str[1] = "ptr      ";
278280304Sjkim    print_it("BF_encrypt_ptr ", 1);
279280304Sjkim    if (max < tm[1]) {
280280304Sjkim        max = tm[1];
281280304Sjkim        max_idx = 1;
282280304Sjkim    }
283280304Sjkim    str[2] = "ptr2     ";
284280304Sjkim    print_it("BF_encrypt_ptr2 ", 2);
285280304Sjkim    if (max < tm[2]) {
286280304Sjkim        max = tm[2];
287280304Sjkim        max_idx = 2;
288280304Sjkim    }
28955714Skris
290280304Sjkim    printf("options    BF ecb/s\n");
291280304Sjkim    printf("%s %12.2f 100.0%%\n", str[max_idx], tm[max_idx]);
292280304Sjkim    d = tm[max_idx];
293280304Sjkim    tm[max_idx] = -2.0;
294280304Sjkim    max = -1.0;
295280304Sjkim    for (;;) {
296280304Sjkim        for (i = 0; i < 3; i++) {
297280304Sjkim            if (max < tm[i]) {
298280304Sjkim                max = tm[i];
299280304Sjkim                j = i;
300280304Sjkim            }
301280304Sjkim        }
302280304Sjkim        if (max < 0.0)
303280304Sjkim            break;
304280304Sjkim        printf("%s %12.2f  %4.1f%%\n", str[j], tm[j], tm[j] / d * 100.0);
305280304Sjkim        tm[j] = -2.0;
306280304Sjkim        max = -1.0;
307280304Sjkim    }
30855714Skris
309280304Sjkim    switch (max_idx) {
310280304Sjkim    case 0:
311280304Sjkim        printf("-DBF_DEFAULT_OPTIONS\n");
312280304Sjkim        break;
313280304Sjkim    case 1:
314280304Sjkim        printf("-DBF_PTR\n");
315280304Sjkim        break;
316280304Sjkim    case 2:
317280304Sjkim        printf("-DBF_PTR2\n");
318280304Sjkim        break;
319280304Sjkim    }
320280304Sjkim    exit(0);
321109998Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
322280304Sjkim    return (0);
32355714Skris#endif
324280304Sjkim}
325