155714Skris/* crypto/rc4/rc4speed.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
5955714Skris/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
6055714Skris/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
6155714Skris
62109998Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
63280304Sjkim# define TIMES
6455714Skris#endif
6555714Skris
6655714Skris#include <stdio.h>
6755714Skris
6855714Skris#include <openssl/e_os2.h>
6955714Skris#include OPENSSL_UNISTD_IO
7055714SkrisOPENSSL_DECLARE_EXIT
71160814Ssimon#ifndef OPENSSL_SYS_NETWARE
72280304Sjkim# include <signal.h>
73160814Ssimon#endif
7455714Skris#ifndef _IRIX
75280304Sjkim# include <time.h>
7655714Skris#endif
7755714Skris#ifdef TIMES
78280304Sjkim# include <sys/types.h>
79280304Sjkim# include <sys/times.h>
8055714Skris#endif
81280304Sjkim    /*
82280304Sjkim     * Depending on the VMS version, the tms structure is perhaps defined.
83280304Sjkim     * The __TMS macro will show if it was.  If it wasn't defined, we should
84280304Sjkim     * undefine TIMES, since that tells the rest of the program how things
85280304Sjkim     * should be handled.  -- Richard Levitte
86280304Sjkim     */
87109998Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
88280304Sjkim# undef TIMES
8955714Skris#endif
9055714Skris#ifndef TIMES
91280304Sjkim# include <sys/timeb.h>
9255714Skris#endif
9355714Skris#if defined(sun) || defined(__ultrix)
94280304Sjkim# define _POSIX_SOURCE
95280304Sjkim# include <limits.h>
96280304Sjkim# include <sys/param.h>
9755714Skris#endif
9855714Skris#include <openssl/rc4.h>
9955714Skris/* The following if from times(3) man page.  It may need to be changed */
10055714Skris#ifndef HZ
101280304Sjkim# ifndef CLK_TCK
102280304Sjkim#  define HZ      100.0
103280304Sjkim# else                          /* CLK_TCK */
104280304Sjkim#  define HZ ((double)CLK_TCK)
105280304Sjkim# endif
10655714Skris#endif
107280304Sjkim#define BUFSIZE ((long)1024)
108280304Sjkimlong run = 0;
10955714Skris
11055714Skrisdouble Time_F(int s);
11155714Skris#ifdef SIGALRM
112280304Sjkim# if defined(__STDC__) || defined(sgi) || defined(_AIX)
113280304Sjkim#  define SIGRETTYPE void
114280304Sjkim# else
115280304Sjkim#  define SIGRETTYPE int
116280304Sjkim# endif
11755714Skris
11855714SkrisSIGRETTYPE sig_done(int sig);
11955714SkrisSIGRETTYPE sig_done(int sig)
120280304Sjkim{
121280304Sjkim    signal(SIGALRM, sig_done);
122280304Sjkim    run = 0;
123280304Sjkim# ifdef LINT
124280304Sjkim    sig = sig;
125280304Sjkim# endif
126280304Sjkim}
12755714Skris#endif
12855714Skris
129280304Sjkim#define START   0
130280304Sjkim#define STOP    1
13155714Skris
13255714Skrisdouble Time_F(int s)
133280304Sjkim{
134280304Sjkim    double ret;
13555714Skris#ifdef TIMES
136280304Sjkim    static struct tms tstart, tend;
13755714Skris
138280304Sjkim    if (s == START) {
139280304Sjkim        times(&tstart);
140280304Sjkim        return (0);
141280304Sjkim    } else {
142280304Sjkim        times(&tend);
143280304Sjkim        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
144280304Sjkim        return ((ret == 0.0) ? 1e-6 : ret);
145280304Sjkim    }
146280304Sjkim#else                           /* !times() */
147280304Sjkim    static struct timeb tstart, tend;
148280304Sjkim    long i;
14955714Skris
150280304Sjkim    if (s == START) {
151280304Sjkim        ftime(&tstart);
152280304Sjkim        return (0);
153280304Sjkim    } else {
154280304Sjkim        ftime(&tend);
155280304Sjkim        i = (long)tend.millitm - (long)tstart.millitm;
156280304Sjkim        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1e3;
157280304Sjkim        return ((ret == 0.0) ? 1e-6 : ret);
158280304Sjkim    }
15955714Skris#endif
160280304Sjkim}
16155714Skris
16255714Skrisint main(int argc, char **argv)
163280304Sjkim{
164280304Sjkim    long count;
165280304Sjkim    static unsigned char buf[BUFSIZE];
166280304Sjkim    static unsigned char key[] = {
167280304Sjkim        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
168280304Sjkim        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
169280304Sjkim    };
170280304Sjkim    RC4_KEY sch;
171280304Sjkim    double a, b, c, d;
17255714Skris#ifndef SIGALRM
173280304Sjkim    long ca, cb, cc;
17455714Skris#endif
17555714Skris
17655714Skris#ifndef TIMES
177280304Sjkim    printf("To get the most accurate results, try to run this\n");
178280304Sjkim    printf("program when this computer is idle.\n");
17955714Skris#endif
18055714Skris
18155714Skris#ifndef SIGALRM
182280304Sjkim    printf("First we calculate the approximate speed ...\n");
183280304Sjkim    RC4_set_key(&sch, 16, key);
184280304Sjkim    count = 10;
185280304Sjkim    do {
186280304Sjkim        long i;
187280304Sjkim        unsigned long data[2];
18855714Skris
189280304Sjkim        count *= 2;
190280304Sjkim        Time_F(START);
191280304Sjkim        for (i = count; i; i--)
192280304Sjkim            RC4(&sch, 8, buf, buf);
193280304Sjkim        d = Time_F(STOP);
194280304Sjkim    } while (d < 3.0);
195280304Sjkim    ca = count / 512;
196280304Sjkim    cc = count * 8 / BUFSIZE + 1;
197280304Sjkim    printf("Doing RC4_set_key %ld times\n", ca);
198280304Sjkim# define COND(d) (count != (d))
199280304Sjkim# define COUNT(d) (d)
20055714Skris#else
201280304Sjkim# define COND(c) (run)
202280304Sjkim# define COUNT(d) (count)
203280304Sjkim    signal(SIGALRM, sig_done);
204280304Sjkim    printf("Doing RC4_set_key for 10 seconds\n");
205280304Sjkim    alarm(10);
20655714Skris#endif
20755714Skris
208280304Sjkim    Time_F(START);
209280304Sjkim    for (count = 0, run = 1; COND(ca); count += 4) {
210280304Sjkim        RC4_set_key(&sch, 16, key);
211280304Sjkim        RC4_set_key(&sch, 16, key);
212280304Sjkim        RC4_set_key(&sch, 16, key);
213280304Sjkim        RC4_set_key(&sch, 16, key);
214280304Sjkim    }
215280304Sjkim    d = Time_F(STOP);
216280304Sjkim    printf("%ld RC4_set_key's in %.2f seconds\n", count, d);
217280304Sjkim    a = ((double)COUNT(ca)) / d;
21855714Skris
21955714Skris#ifdef SIGALRM
220280304Sjkim    printf("Doing RC4 on %ld byte blocks for 10 seconds\n", BUFSIZE);
221280304Sjkim    alarm(10);
22255714Skris#else
223280304Sjkim    printf("Doing RC4 %ld times on %ld byte blocks\n", cc, BUFSIZE);
22455714Skris#endif
225280304Sjkim    Time_F(START);
226280304Sjkim    for (count = 0, run = 1; COND(cc); count++)
227280304Sjkim        RC4(&sch, BUFSIZE, buf, buf);
228280304Sjkim    d = Time_F(STOP);
229280304Sjkim    printf("%ld RC4's of %ld byte blocks in %.2f second\n",
230280304Sjkim           count, BUFSIZE, d);
231280304Sjkim    c = ((double)COUNT(cc) * BUFSIZE) / d;
23255714Skris
233280304Sjkim    printf("RC4 set_key per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a);
234280304Sjkim    printf("RC4   bytes per sec = %12.2f (%9.3fuS)\n", c, 8.0e6 / c);
235280304Sjkim    exit(0);
236109998Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
237280304Sjkim    return (0);
23855714Skris#endif
239280304Sjkim}
240