155714Skris/* crypto/cast/castopts.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))
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/cast.h>
10155714Skris#define CAST_DEFAULT_OPTIONS
10255714Skris#undef E_CAST
10355714Skris#define CAST_encrypt  CAST_encrypt_normal
10455714Skris#define CAST_decrypt  CAST_decrypt_normal
10555714Skris#define CAST_cbc_encrypt  CAST_cbc_encrypt_normal
10655714Skris#undef HEADER_CAST_LOCL_H
10755714Skris#include "c_enc.c"
10855714Skris#define CAST_PTR
10955714Skris#undef CAST_PTR2
11055714Skris#undef E_CAST
11155714Skris#undef CAST_encrypt
11255714Skris#undef CAST_decrypt
11355714Skris#undef CAST_cbc_encrypt
11455714Skris#define CAST_encrypt  CAST_encrypt_ptr
11555714Skris#define CAST_decrypt  CAST_decrypt_ptr
11655714Skris#define CAST_cbc_encrypt  CAST_cbc_encrypt_ptr
11755714Skris#undef HEADER_CAST_LOCL_H
11855714Skris#include "c_enc.c"
11955714Skris#undef CAST_PTR
12055714Skris#define CAST_PTR2
12155714Skris#undef E_CAST
12255714Skris#undef CAST_encrypt
12355714Skris#undef CAST_decrypt
12455714Skris#undef CAST_cbc_encrypt
12555714Skris#define CAST_encrypt  CAST_encrypt_ptr2
12655714Skris#define CAST_decrypt  CAST_decrypt_ptr2
12755714Skris#define CAST_cbc_encrypt  CAST_cbc_encrypt_ptr2
12855714Skris#undef HEADER_CAST_LOCL_H
12955714Skris#include "c_enc.c"
13055714Skris/* The following if from times(3) man page.  It may need to be changed */
13155714Skris#ifndef HZ
13255714Skris# ifndef CLK_TCK
133280304Sjkim#  ifndef _BSD_CLK_TCK_         /* FreeBSD fix */
134280304Sjkim#   define HZ   100.0
135280304Sjkim#  else                         /* _BSD_CLK_TCK_ */
13655714Skris#   define HZ ((double)_BSD_CLK_TCK_)
13755714Skris#  endif
138280304Sjkim# else                          /* CLK_TCK */
13955714Skris#  define HZ ((double)CLK_TCK)
14055714Skris# endif
14155714Skris#endif
142280304Sjkim#define BUFSIZE ((long)1024)
143280304Sjkimlong run = 0;
14455714Skris
14555714Skrisdouble Time_F(int s);
14655714Skris#ifdef SIGALRM
147280304Sjkim# if defined(__STDC__) || defined(sgi)
148280304Sjkim#  define SIGRETTYPE void
149280304Sjkim# else
150280304Sjkim#  define SIGRETTYPE int
151280304Sjkim# endif
15255714Skris
15355714SkrisSIGRETTYPE sig_done(int sig);
15455714SkrisSIGRETTYPE sig_done(int sig)
155280304Sjkim{
156280304Sjkim    signal(SIGALRM, sig_done);
157280304Sjkim    run = 0;
158280304Sjkim# ifdef LINT
159280304Sjkim    sig = sig;
160280304Sjkim# endif
161280304Sjkim}
16255714Skris#endif
16355714Skris
164280304Sjkim#define START   0
165280304Sjkim#define STOP    1
16655714Skris
16755714Skrisdouble Time_F(int s)
168280304Sjkim{
169280304Sjkim    double ret;
17055714Skris#ifdef TIMES
171280304Sjkim    static struct tms tstart, tend;
17255714Skris
173280304Sjkim    if (s == START) {
174280304Sjkim        times(&tstart);
175280304Sjkim        return (0);
176280304Sjkim    } else {
177280304Sjkim        times(&tend);
178280304Sjkim        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
179280304Sjkim        return ((ret == 0.0) ? 1e-6 : ret);
180280304Sjkim    }
181280304Sjkim#else                           /* !times() */
182280304Sjkim    static struct timeb tstart, tend;
183280304Sjkim    long i;
18455714Skris
185280304Sjkim    if (s == START) {
186280304Sjkim        ftime(&tstart);
187280304Sjkim        return (0);
188280304Sjkim    } else {
189280304Sjkim        ftime(&tend);
190280304Sjkim        i = (long)tend.millitm - (long)tstart.millitm;
191280304Sjkim        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1000.0;
192280304Sjkim        return ((ret == 0.0) ? 1e-6 : ret);
193280304Sjkim    }
19455714Skris#endif
195280304Sjkim}
19655714Skris
19755714Skris#ifdef SIGALRM
198280304Sjkim# define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
19955714Skris#else
200280304Sjkim# define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
20155714Skris#endif
202280304Sjkim
20355714Skris#define time_it(func,name,index) \
204280304Sjkim        print_name(name); \
205280304Sjkim        Time_F(START); \
206280304Sjkim        for (count=0,run=1; COND(cb); count+=4) \
207280304Sjkim                { \
208280304Sjkim                unsigned long d[2]; \
209280304Sjkim                func(d,&sch); \
210280304Sjkim                func(d,&sch); \
211280304Sjkim                func(d,&sch); \
212280304Sjkim                func(d,&sch); \
213280304Sjkim                } \
214280304Sjkim        tm[index]=Time_F(STOP); \
215280304Sjkim        fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
216280304Sjkim        tm[index]=((double)COUNT(cb))/tm[index];
21755714Skris
21855714Skris#define print_it(name,index) \
219280304Sjkim        fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
220280304Sjkim                tm[index]*8,1.0e6/tm[index]);
22155714Skris
22255714Skrisint main(int argc, char **argv)
223280304Sjkim{
224280304Sjkim    long count;
225280304Sjkim    static unsigned char buf[BUFSIZE];
226280304Sjkim    static char key[16] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
227280304Sjkim        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
228280304Sjkim    };
229280304Sjkim    CAST_KEY sch;
230280304Sjkim    double d, tm[16], max = 0;
231280304Sjkim    int rank[16];
232280304Sjkim    char *str[16];
233280304Sjkim    int max_idx = 0, i, num = 0, j;
23455714Skris#ifndef SIGALARM
235280304Sjkim    long ca, cb, cc, cd, ce;
23655714Skris#endif
23755714Skris
238280304Sjkim    for (i = 0; i < 12; i++) {
239280304Sjkim        tm[i] = 0.0;
240280304Sjkim        rank[i] = 0;
241280304Sjkim    }
24255714Skris
24355714Skris#ifndef TIMES
244280304Sjkim    fprintf(stderr, "To get the most accurate results, try to run this\n");
245280304Sjkim    fprintf(stderr, "program when this computer is idle.\n");
24655714Skris#endif
24755714Skris
248280304Sjkim    CAST_set_key(&sch, 16, key);
24955714Skris
25055714Skris#ifndef SIGALRM
251280304Sjkim    fprintf(stderr, "First we calculate the approximate speed ...\n");
252280304Sjkim    count = 10;
253280304Sjkim    do {
254280304Sjkim        long i;
255280304Sjkim        unsigned long data[2];
25655714Skris
257280304Sjkim        count *= 2;
258280304Sjkim        Time_F(START);
259280304Sjkim        for (i = count; i; i--)
260280304Sjkim            CAST_encrypt(data, &sch);
261280304Sjkim        d = Time_F(STOP);
262280304Sjkim    } while (d < 3.0);
263280304Sjkim    ca = count;
264280304Sjkim    cb = count * 3;
265280304Sjkim    cc = count * 3 * 8 / BUFSIZE + 1;
266280304Sjkim    cd = count * 8 / BUFSIZE + 1;
26755714Skris
268280304Sjkim    ce = count / 20 + 1;
269280304Sjkim# define COND(d) (count != (d))
270280304Sjkim# define COUNT(d) (d)
27155714Skris#else
272280304Sjkim# define COND(c) (run)
273280304Sjkim# define COUNT(d) (count)
274280304Sjkim    signal(SIGALRM, sig_done);
275280304Sjkim    alarm(10);
27655714Skris#endif
27755714Skris
278280304Sjkim    time_it(CAST_encrypt_normal, "CAST_encrypt_normal ", 0);
279280304Sjkim    time_it(CAST_encrypt_ptr, "CAST_encrypt_ptr    ", 1);
280280304Sjkim    time_it(CAST_encrypt_ptr2, "CAST_encrypt_ptr2   ", 2);
281280304Sjkim    num += 3;
28255714Skris
283280304Sjkim    str[0] = "<nothing>";
284280304Sjkim    print_it("CAST_encrypt_normal ", 0);
285280304Sjkim    max = tm[0];
286280304Sjkim    max_idx = 0;
287280304Sjkim    str[1] = "ptr      ";
288280304Sjkim    print_it("CAST_encrypt_ptr ", 1);
289280304Sjkim    if (max < tm[1]) {
290280304Sjkim        max = tm[1];
291280304Sjkim        max_idx = 1;
292280304Sjkim    }
293280304Sjkim    str[2] = "ptr2     ";
294280304Sjkim    print_it("CAST_encrypt_ptr2 ", 2);
295280304Sjkim    if (max < tm[2]) {
296280304Sjkim        max = tm[2];
297280304Sjkim        max_idx = 2;
298280304Sjkim    }
29955714Skris
300280304Sjkim    printf("options    CAST ecb/s\n");
301280304Sjkim    printf("%s %12.2f 100.0%%\n", str[max_idx], tm[max_idx]);
302280304Sjkim    d = tm[max_idx];
303280304Sjkim    tm[max_idx] = -2.0;
304280304Sjkim    max = -1.0;
305280304Sjkim    for (;;) {
306280304Sjkim        for (i = 0; i < 3; i++) {
307280304Sjkim            if (max < tm[i]) {
308280304Sjkim                max = tm[i];
309280304Sjkim                j = i;
310280304Sjkim            }
311280304Sjkim        }
312280304Sjkim        if (max < 0.0)
313280304Sjkim            break;
314280304Sjkim        printf("%s %12.2f  %4.1f%%\n", str[j], tm[j], tm[j] / d * 100.0);
315280304Sjkim        tm[j] = -2.0;
316280304Sjkim        max = -1.0;
317280304Sjkim    }
31855714Skris
319280304Sjkim    switch (max_idx) {
320280304Sjkim    case 0:
321280304Sjkim        printf("-DCAST_DEFAULT_OPTIONS\n");
322280304Sjkim        break;
323280304Sjkim    case 1:
324280304Sjkim        printf("-DCAST_PTR\n");
325280304Sjkim        break;
326280304Sjkim    case 2:
327280304Sjkim        printf("-DCAST_PTR2\n");
328280304Sjkim        break;
329280304Sjkim    }
330280304Sjkim    exit(0);
331109998Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
332280304Sjkim    return (0);
33355714Skris#endif
334280304Sjkim}
335