161821Smarkm/* crypto/idea/idea_spd.c */
261821Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
361821Smarkm * All rights reserved.
461821Smarkm *
561821Smarkm * This package is an SSL implementation written
661821Smarkm * by Eric Young (eay@cryptsoft.com).
761821Smarkm * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
961821Smarkm * This library is free for commercial and non-commercial use as long as
1061821Smarkm * the following conditions are aheared to.  The following conditions
1161821Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
1261821Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1361821Smarkm * included with this distribution is covered by the same copyright terms
1461821Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1661821Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
1761821Smarkm * the code are not to be removed.
1861821Smarkm * If this package is used in a product, Eric Young should be given attribution
1961821Smarkm * as the author of the parts of the library used.
2061821Smarkm * This can be in the form of a textual message at program startup or
2161821Smarkm * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2361821Smarkm * Redistribution and use in source and binary forms, with or without
2461821Smarkm * modification, are permitted provided that the following conditions
2561821Smarkm * are met:
2661821Smarkm * 1. Redistributions of source code must retain the copyright
2761821Smarkm *    notice, this list of conditions and the following disclaimer.
2861821Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2961821Smarkm *    notice, this list of conditions and the following disclaimer in the
3061821Smarkm *    documentation and/or other materials provided with the distribution.
3161821Smarkm * 3. All advertising materials mentioning features or use of this software
3261821Smarkm *    must display the following acknowledgement:
3361821Smarkm *    "This product includes cryptographic software written by
3461821Smarkm *     Eric Young (eay@cryptsoft.com)"
3561821Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
3661821Smarkm *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3861821Smarkm *    the apps directory (application code) you must include an acknowledgement:
3961821Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4161821Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4261821Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4361821Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4461821Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4561821Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4661821Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4761821Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4861821Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4961821Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5061821Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5161821Smarkm * SUCH DAMAGE.
52296465Sdelphij *
5361821Smarkm * The licence and distribution terms for any publically available version or
5461821Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
5561821Smarkm * copied and put under another distribution licence
5661821Smarkm * [including the GNU Public Licence.]
5761821Smarkm */
5861821Smarkm
5961821Smarkm/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
6061821Smarkm/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
6161821Smarkm
62110007Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
63296465Sdelphij# define TIMES
6461821Smarkm#endif
6561821Smarkm
6661821Smarkm#include <stdio.h>
6761821Smarkm
6861821Smarkm#include <openssl/e_os2.h>
6961821Smarkm#include OPENSSL_UNISTD_IO
7061821SmarkmOPENSSL_DECLARE_EXIT
71160817Ssimon#ifndef OPENSSL_SYS_NETWARE
72296465Sdelphij# include <signal.h>
73160817Ssimon#endif
7461821Smarkm#ifndef _IRIX
75296465Sdelphij# include <time.h>
7661821Smarkm#endif
7761821Smarkm#ifdef TIMES
78296465Sdelphij# include <sys/types.h>
79296465Sdelphij# include <sys/times.h>
8061821Smarkm#endif
81296465Sdelphij    /*
82296465Sdelphij     * Depending on the VMS version, the tms structure is perhaps defined.
83296465Sdelphij     * The __TMS macro will show if it was.  If it wasn't defined, we should
84296465Sdelphij     * undefine TIMES, since that tells the rest of the program how things
85296465Sdelphij     * should be handled.  -- Richard Levitte
86296465Sdelphij     */
87110007Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
88296465Sdelphij# undef TIMES
8961821Smarkm#endif
9061821Smarkm#ifndef TIMES
91296465Sdelphij# include <sys/timeb.h>
9261821Smarkm#endif
9361821Smarkm#if defined(sun) || defined(__ultrix)
94296465Sdelphij# define _POSIX_SOURCE
95296465Sdelphij# include <limits.h>
96296465Sdelphij# include <sys/param.h>
9761821Smarkm#endif
9861821Smarkm#include <openssl/idea.h>
9961821Smarkm/* The following if from times(3) man page.  It may need to be changed */
10061821Smarkm#ifndef HZ
101296465Sdelphij# ifndef CLK_TCK
102296465Sdelphij#  define HZ      100.0
103296465Sdelphij# else                          /* CLK_TCK */
104296465Sdelphij#  define HZ ((double)CLK_TCK)
105296465Sdelphij# endif
10661821Smarkm#endif
107296465Sdelphij#define BUFSIZE ((long)1024)
108296465Sdelphijlong run = 0;
10961821Smarkm
11061821Smarkmdouble Time_F(int s);
11161821Smarkm#ifdef SIGALRM
112296465Sdelphij# if defined(__STDC__) || defined(sgi) || defined(_AIX)
113296465Sdelphij#  define SIGRETTYPE void
114296465Sdelphij# else
115296465Sdelphij#  define SIGRETTYPE int
116296465Sdelphij# endif
11761821Smarkm
11861821SmarkmSIGRETTYPE sig_done(int sig);
11961821SmarkmSIGRETTYPE sig_done(int sig)
120296465Sdelphij{
121296465Sdelphij    signal(SIGALRM, sig_done);
122296465Sdelphij    run = 0;
123296465Sdelphij# ifdef LINT
124296465Sdelphij    sig = sig;
125296465Sdelphij# endif
126296465Sdelphij}
12761821Smarkm#endif
12861821Smarkm
129296465Sdelphij#define START   0
130296465Sdelphij#define STOP    1
13161821Smarkm
13261821Smarkmdouble Time_F(int s)
133296465Sdelphij{
134296465Sdelphij    double ret;
13561821Smarkm#ifdef TIMES
136296465Sdelphij    static struct tms tstart, tend;
13761821Smarkm
138296465Sdelphij    if (s == START) {
139296465Sdelphij        times(&tstart);
140296465Sdelphij        return (0);
141296465Sdelphij    } else {
142296465Sdelphij        times(&tend);
143296465Sdelphij        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
144296465Sdelphij        return ((ret == 0.0) ? 1e-6 : ret);
145296465Sdelphij    }
146296465Sdelphij#else                           /* !times() */
147296465Sdelphij    static struct timeb tstart, tend;
148296465Sdelphij    long i;
14961821Smarkm
150296465Sdelphij    if (s == START) {
151296465Sdelphij        ftime(&tstart);
152296465Sdelphij        return (0);
153296465Sdelphij    } else {
154296465Sdelphij        ftime(&tend);
155296465Sdelphij        i = (long)tend.millitm - (long)tstart.millitm;
156296465Sdelphij        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1e3;
157296465Sdelphij        return ((ret == 0.0) ? 1e-6 : ret);
158296465Sdelphij    }
15961821Smarkm#endif
160296465Sdelphij}
16161821Smarkm
16261821Smarkmint main(int argc, char **argv)
163296465Sdelphij{
164296465Sdelphij    long count;
165296465Sdelphij    static unsigned char buf[BUFSIZE];
166296465Sdelphij    static unsigned char key[] = {
167296465Sdelphij        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
168296465Sdelphij        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
169296465Sdelphij    };
170296465Sdelphij    IDEA_KEY_SCHEDULE sch;
171296465Sdelphij    double a, aa, b, c, d;
17261821Smarkm#ifndef SIGALRM
173296465Sdelphij    long ca, cca, cb, cc;
17461821Smarkm#endif
17561821Smarkm
17661821Smarkm#ifndef TIMES
177296465Sdelphij    printf("To get the most accurate results, try to run this\n");
178296465Sdelphij    printf("program when this computer is idle.\n");
17961821Smarkm#endif
18061821Smarkm
18161821Smarkm#ifndef SIGALRM
182296465Sdelphij    printf("First we calculate the approximate speed ...\n");
183296465Sdelphij    idea_set_encrypt_key(key, &sch);
184296465Sdelphij    count = 10;
185296465Sdelphij    do {
186296465Sdelphij        long i;
187296465Sdelphij        IDEA_INT data[2];
18861821Smarkm
189296465Sdelphij        count *= 2;
190296465Sdelphij        Time_F(START);
191296465Sdelphij        for (i = count; i; i--)
192296465Sdelphij            idea_encrypt(data, &sch);
193296465Sdelphij        d = Time_F(STOP);
194296465Sdelphij    } while (d < 3.0);
195296465Sdelphij    ca = count / 4;
196296465Sdelphij    cca = count / 200;
197296465Sdelphij    cb = count;
198296465Sdelphij    cc = count * 8 / BUFSIZE + 1;
199296465Sdelphij    printf("idea_set_encrypt_key %ld times\n", ca);
200296465Sdelphij# define COND(d) (count <= (d))
201296465Sdelphij# define COUNT(d) (d)
20261821Smarkm#else
203296465Sdelphij# define COND(c) (run)
204296465Sdelphij# define COUNT(d) (count)
205296465Sdelphij    signal(SIGALRM, sig_done);
206296465Sdelphij    printf("Doing idea_set_encrypt_key for 10 seconds\n");
207296465Sdelphij    alarm(10);
20861821Smarkm#endif
20961821Smarkm
210296465Sdelphij    Time_F(START);
211296465Sdelphij    for (count = 0, run = 1; COND(ca); count += 4) {
212296465Sdelphij        idea_set_encrypt_key(key, &sch);
213296465Sdelphij        idea_set_encrypt_key(key, &sch);
214296465Sdelphij        idea_set_encrypt_key(key, &sch);
215296465Sdelphij        idea_set_encrypt_key(key, &sch);
216296465Sdelphij    }
217296465Sdelphij    d = Time_F(STOP);
218296465Sdelphij    printf("%ld idea idea_set_encrypt_key's in %.2f seconds\n", count, d);
219296465Sdelphij    a = ((double)COUNT(ca)) / d;
22061821Smarkm
22161821Smarkm#ifdef SIGALRM
222296465Sdelphij    printf("Doing idea_set_decrypt_key for 10 seconds\n");
223296465Sdelphij    alarm(10);
22461821Smarkm#else
225296465Sdelphij    printf("Doing idea_set_decrypt_key %ld times\n", cca);
22661821Smarkm#endif
22761821Smarkm
228296465Sdelphij    Time_F(START);
229296465Sdelphij    for (count = 0, run = 1; COND(cca); count += 4) {
230296465Sdelphij        idea_set_decrypt_key(&sch, &sch);
231296465Sdelphij        idea_set_decrypt_key(&sch, &sch);
232296465Sdelphij        idea_set_decrypt_key(&sch, &sch);
233296465Sdelphij        idea_set_decrypt_key(&sch, &sch);
234296465Sdelphij    }
235296465Sdelphij    d = Time_F(STOP);
236296465Sdelphij    printf("%ld idea idea_set_decrypt_key's in %.2f seconds\n", count, d);
237296465Sdelphij    aa = ((double)COUNT(cca)) / d;
23861821Smarkm
23961821Smarkm#ifdef SIGALRM
240296465Sdelphij    printf("Doing idea_encrypt's for 10 seconds\n");
241296465Sdelphij    alarm(10);
24261821Smarkm#else
243296465Sdelphij    printf("Doing idea_encrypt %ld times\n", cb);
24461821Smarkm#endif
245296465Sdelphij    Time_F(START);
246296465Sdelphij    for (count = 0, run = 1; COND(cb); count += 4) {
247296465Sdelphij        unsigned long data[2];
24861821Smarkm
249296465Sdelphij        idea_encrypt(data, &sch);
250296465Sdelphij        idea_encrypt(data, &sch);
251296465Sdelphij        idea_encrypt(data, &sch);
252296465Sdelphij        idea_encrypt(data, &sch);
253296465Sdelphij    }
254296465Sdelphij    d = Time_F(STOP);
255296465Sdelphij    printf("%ld idea_encrypt's in %.2f second\n", count, d);
256296465Sdelphij    b = ((double)COUNT(cb) * 8) / d;
25761821Smarkm
25861821Smarkm#ifdef SIGALRM
259296465Sdelphij    printf("Doing idea_cbc_encrypt on %ld byte blocks for 10 seconds\n",
260296465Sdelphij           BUFSIZE);
261296465Sdelphij    alarm(10);
26261821Smarkm#else
263296465Sdelphij    printf("Doing idea_cbc_encrypt %ld times on %ld byte blocks\n", cc,
264296465Sdelphij           BUFSIZE);
26561821Smarkm#endif
266296465Sdelphij    Time_F(START);
267296465Sdelphij    for (count = 0, run = 1; COND(cc); count++)
268296465Sdelphij        idea_cbc_encrypt(buf, buf, BUFSIZE, &sch, &(key[0]), IDEA_ENCRYPT);
269296465Sdelphij    d = Time_F(STOP);
270296465Sdelphij    printf("%ld idea_cbc_encrypt's of %ld byte blocks in %.2f second\n",
271296465Sdelphij           count, BUFSIZE, d);
272296465Sdelphij    c = ((double)COUNT(cc) * BUFSIZE) / d;
27361821Smarkm
274296465Sdelphij    printf("IDEA set_encrypt_key per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a);
275296465Sdelphij    printf("IDEA set_decrypt_key per sec = %12.2f (%9.3fuS)\n", aa,
276296465Sdelphij           1.0e6 / aa);
277296465Sdelphij    printf("IDEA raw ecb bytes   per sec = %12.2f (%9.3fuS)\n", b, 8.0e6 / b);
278296465Sdelphij    printf("IDEA cbc     bytes   per sec = %12.2f (%9.3fuS)\n", c, 8.0e6 / c);
279296465Sdelphij    exit(0);
280110007Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
281296465Sdelphij    return (0);
28261821Smarkm#endif
283296465Sdelphij}
284