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.
8296341Sdelphij *
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).
15296341Sdelphij *
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.
22296341Sdelphij *
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 :-).
37296341Sdelphij * 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)"
40296341Sdelphij *
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.
52296341Sdelphij *
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
59296341Sdelphij/*
60296341Sdelphij * define PART1, PART2, PART3 or PART4 to build only with a few of the
61296341Sdelphij * options. This is for machines with 64k code segment size restrictions.
62296341Sdelphij */
6355714Skris
64109998Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC))
65296341Sdelphij# 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
74296341Sdelphij# include <signal.h>
75160814Ssimon#endif
7655714Skris#ifndef _IRIX
77296341Sdelphij# include <time.h>
7855714Skris#endif
7955714Skris#ifdef TIMES
80296341Sdelphij# include <sys/types.h>
81296341Sdelphij# include <sys/times.h>
8255714Skris#endif
83296341Sdelphij    /*
84296341Sdelphij     * Depending on the VMS version, the tms structure is perhaps defined.
85296341Sdelphij     * The __TMS macro will show if it was.  If it wasn't defined, we should
86296341Sdelphij     * undefine TIMES, since that tells the rest of the program how things
87296341Sdelphij     * should be handled.  -- Richard Levitte
88296341Sdelphij     */
89109998Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
90296341Sdelphij# undef TIMES
9155714Skris#endif
9255714Skris#ifndef TIMES
93296341Sdelphij# include <sys/timeb.h>
9455714Skris#endif
9555714Skris#if defined(sun) || defined(__ultrix)
96296341Sdelphij# define _POSIX_SOURCE
97296341Sdelphij# include <limits.h>
98296341Sdelphij# 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
133296341Sdelphij#  ifndef _BSD_CLK_TCK_         /* FreeBSD fix */
134296341Sdelphij#   define HZ   100.0
135296341Sdelphij#  else                         /* _BSD_CLK_TCK_ */
13655714Skris#   define HZ ((double)_BSD_CLK_TCK_)
13755714Skris#  endif
138296341Sdelphij# else                          /* CLK_TCK */
13955714Skris#  define HZ ((double)CLK_TCK)
14055714Skris# endif
14155714Skris#endif
142296341Sdelphij#define BUFSIZE ((long)1024)
143296341Sdelphijlong run = 0;
14455714Skris
14555714Skrisdouble Time_F(int s);
14655714Skris#ifdef SIGALRM
147296341Sdelphij# if defined(__STDC__) || defined(sgi)
148296341Sdelphij#  define SIGRETTYPE void
149296341Sdelphij# else
150296341Sdelphij#  define SIGRETTYPE int
151296341Sdelphij# endif
15255714Skris
15355714SkrisSIGRETTYPE sig_done(int sig);
15455714SkrisSIGRETTYPE sig_done(int sig)
155296341Sdelphij{
156296341Sdelphij    signal(SIGALRM, sig_done);
157296341Sdelphij    run = 0;
158296341Sdelphij# ifdef LINT
159296341Sdelphij    sig = sig;
160296341Sdelphij# endif
161296341Sdelphij}
16255714Skris#endif
16355714Skris
164296341Sdelphij#define START   0
165296341Sdelphij#define STOP    1
16655714Skris
16755714Skrisdouble Time_F(int s)
168296341Sdelphij{
169296341Sdelphij    double ret;
17055714Skris#ifdef TIMES
171296341Sdelphij    static struct tms tstart, tend;
17255714Skris
173296341Sdelphij    if (s == START) {
174296341Sdelphij        times(&tstart);
175296341Sdelphij        return (0);
176296341Sdelphij    } else {
177296341Sdelphij        times(&tend);
178296341Sdelphij        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
179296341Sdelphij        return ((ret == 0.0) ? 1e-6 : ret);
180296341Sdelphij    }
181296341Sdelphij#else                           /* !times() */
182296341Sdelphij    static struct timeb tstart, tend;
183296341Sdelphij    long i;
18455714Skris
185296341Sdelphij    if (s == START) {
186296341Sdelphij        ftime(&tstart);
187296341Sdelphij        return (0);
188296341Sdelphij    } else {
189296341Sdelphij        ftime(&tend);
190296341Sdelphij        i = (long)tend.millitm - (long)tstart.millitm;
191296341Sdelphij        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1000.0;
192296341Sdelphij        return ((ret == 0.0) ? 1e-6 : ret);
193296341Sdelphij    }
19455714Skris#endif
195296341Sdelphij}
19655714Skris
19755714Skris#ifdef SIGALRM
198296341Sdelphij# define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
19955714Skris#else
200296341Sdelphij# define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
20155714Skris#endif
202296341Sdelphij
20355714Skris#define time_it(func,name,index) \
204296341Sdelphij        print_name(name); \
205296341Sdelphij        Time_F(START); \
206296341Sdelphij        for (count=0,run=1; COND(cb); count+=4) \
207296341Sdelphij                { \
208296341Sdelphij                unsigned long d[2]; \
209296341Sdelphij                func(d,&sch); \
210296341Sdelphij                func(d,&sch); \
211296341Sdelphij                func(d,&sch); \
212296341Sdelphij                func(d,&sch); \
213296341Sdelphij                } \
214296341Sdelphij        tm[index]=Time_F(STOP); \
215296341Sdelphij        fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
216296341Sdelphij        tm[index]=((double)COUNT(cb))/tm[index];
21755714Skris
21855714Skris#define print_it(name,index) \
219296341Sdelphij        fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
220296341Sdelphij                tm[index]*8,1.0e6/tm[index]);
22155714Skris
22255714Skrisint main(int argc, char **argv)
223296341Sdelphij{
224296341Sdelphij    long count;
225296341Sdelphij    static unsigned char buf[BUFSIZE];
226296341Sdelphij    static char key[16] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
227296341Sdelphij        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
228296341Sdelphij    };
229296341Sdelphij    CAST_KEY sch;
230296341Sdelphij    double d, tm[16], max = 0;
231296341Sdelphij    int rank[16];
232296341Sdelphij    char *str[16];
233296341Sdelphij    int max_idx = 0, i, num = 0, j;
23455714Skris#ifndef SIGALARM
235296341Sdelphij    long ca, cb, cc, cd, ce;
23655714Skris#endif
23755714Skris
238296341Sdelphij    for (i = 0; i < 12; i++) {
239296341Sdelphij        tm[i] = 0.0;
240296341Sdelphij        rank[i] = 0;
241296341Sdelphij    }
24255714Skris
24355714Skris#ifndef TIMES
244296341Sdelphij    fprintf(stderr, "To get the most accurate results, try to run this\n");
245296341Sdelphij    fprintf(stderr, "program when this computer is idle.\n");
24655714Skris#endif
24755714Skris
248296341Sdelphij    CAST_set_key(&sch, 16, key);
24955714Skris
25055714Skris#ifndef SIGALRM
251296341Sdelphij    fprintf(stderr, "First we calculate the approximate speed ...\n");
252296341Sdelphij    count = 10;
253296341Sdelphij    do {
254296341Sdelphij        long i;
255296341Sdelphij        unsigned long data[2];
25655714Skris
257296341Sdelphij        count *= 2;
258296341Sdelphij        Time_F(START);
259296341Sdelphij        for (i = count; i; i--)
260296341Sdelphij            CAST_encrypt(data, &sch);
261296341Sdelphij        d = Time_F(STOP);
262296341Sdelphij    } while (d < 3.0);
263296341Sdelphij    ca = count;
264296341Sdelphij    cb = count * 3;
265296341Sdelphij    cc = count * 3 * 8 / BUFSIZE + 1;
266296341Sdelphij    cd = count * 8 / BUFSIZE + 1;
26755714Skris
268296341Sdelphij    ce = count / 20 + 1;
269296341Sdelphij# define COND(d) (count != (d))
270296341Sdelphij# define COUNT(d) (d)
27155714Skris#else
272296341Sdelphij# define COND(c) (run)
273296341Sdelphij# define COUNT(d) (count)
274296341Sdelphij    signal(SIGALRM, sig_done);
275296341Sdelphij    alarm(10);
27655714Skris#endif
27755714Skris
278296341Sdelphij    time_it(CAST_encrypt_normal, "CAST_encrypt_normal ", 0);
279296341Sdelphij    time_it(CAST_encrypt_ptr, "CAST_encrypt_ptr    ", 1);
280296341Sdelphij    time_it(CAST_encrypt_ptr2, "CAST_encrypt_ptr2   ", 2);
281296341Sdelphij    num += 3;
28255714Skris
283296341Sdelphij    str[0] = "<nothing>";
284296341Sdelphij    print_it("CAST_encrypt_normal ", 0);
285296341Sdelphij    max = tm[0];
286296341Sdelphij    max_idx = 0;
287296341Sdelphij    str[1] = "ptr      ";
288296341Sdelphij    print_it("CAST_encrypt_ptr ", 1);
289296341Sdelphij    if (max < tm[1]) {
290296341Sdelphij        max = tm[1];
291296341Sdelphij        max_idx = 1;
292296341Sdelphij    }
293296341Sdelphij    str[2] = "ptr2     ";
294296341Sdelphij    print_it("CAST_encrypt_ptr2 ", 2);
295296341Sdelphij    if (max < tm[2]) {
296296341Sdelphij        max = tm[2];
297296341Sdelphij        max_idx = 2;
298296341Sdelphij    }
29955714Skris
300296341Sdelphij    printf("options    CAST ecb/s\n");
301296341Sdelphij    printf("%s %12.2f 100.0%%\n", str[max_idx], tm[max_idx]);
302296341Sdelphij    d = tm[max_idx];
303296341Sdelphij    tm[max_idx] = -2.0;
304296341Sdelphij    max = -1.0;
305296341Sdelphij    for (;;) {
306296341Sdelphij        for (i = 0; i < 3; i++) {
307296341Sdelphij            if (max < tm[i]) {
308296341Sdelphij                max = tm[i];
309296341Sdelphij                j = i;
310296341Sdelphij            }
311296341Sdelphij        }
312296341Sdelphij        if (max < 0.0)
313296341Sdelphij            break;
314296341Sdelphij        printf("%s %12.2f  %4.1f%%\n", str[j], tm[j], tm[j] / d * 100.0);
315296341Sdelphij        tm[j] = -2.0;
316296341Sdelphij        max = -1.0;
317296341Sdelphij    }
31855714Skris
319296341Sdelphij    switch (max_idx) {
320296341Sdelphij    case 0:
321296341Sdelphij        printf("-DCAST_DEFAULT_OPTIONS\n");
322296341Sdelphij        break;
323296341Sdelphij    case 1:
324296341Sdelphij        printf("-DCAST_PTR\n");
325296341Sdelphij        break;
326296341Sdelphij    case 2:
327296341Sdelphij        printf("-DCAST_PTR2\n");
328296341Sdelphij        break;
329296341Sdelphij    }
330296341Sdelphij    exit(0);
331109998Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
332296341Sdelphij    return (0);
33355714Skris#endif
334296341Sdelphij}
335