speed.c revision 296465
121259Swollman/* crypto/des/speed.c */
221259Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
321259Swollman * All rights reserved.
421259Swollman *
521259Swollman * This package is an SSL implementation written
621259Swollman * by Eric Young (eay@cryptsoft.com).
721259Swollman * The implementation was written so as to conform with Netscapes SSL.
821259Swollman *
921259Swollman * This library is free for commercial and non-commercial use as long as
1021259Swollman * the following conditions are aheared to.  The following conditions
1121259Swollman * apply to all code found in this distribution, be it the RC4, RSA,
1221259Swollman * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1321259Swollman * included with this distribution is covered by the same copyright terms
1421259Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1521259Swollman *
1621259Swollman * Copyright remains Eric Young's, and as such any Copyright notices in
1721259Swollman * the code are not to be removed.
1821259Swollman * If this package is used in a product, Eric Young should be given attribution
1921259Swollman * as the author of the parts of the library used.
2021259Swollman * This can be in the form of a textual message at program startup or
2121259Swollman * in documentation (online or textual) provided with the package.
2221259Swollman *
2321259Swollman * Redistribution and use in source and binary forms, with or without
2421259Swollman * modification, are permitted provided that the following conditions
2521259Swollman * are met:
2621259Swollman * 1. Redistributions of source code must retain the copyright
2721259Swollman *    notice, this list of conditions and the following disclaimer.
2821259Swollman * 2. Redistributions in binary form must reproduce the above copyright
2921259Swollman *    notice, this list of conditions and the following disclaimer in the
3021259Swollman *    documentation and/or other materials provided with the distribution.
3121259Swollman * 3. All advertising materials mentioning features or use of this software
3221259Swollman *    must display the following acknowledgement:
3321259Swollman *    "This product includes cryptographic software written by
3450477Speter *     Eric Young (eay@cryptsoft.com)"
3521259Swollman *    The word 'cryptographic' can be left out if the rouines from the library
3621259Swollman *    being used are not cryptographic related :-).
3721259Swollman * 4. If you include any Windows specific code (or a derivative thereof) from
3821259Swollman *    the apps directory (application code) you must include an acknowledgement:
3921259Swollman *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4021259Swollman *
4121259Swollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4221259Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4321259Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4421259Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4521259Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4621259Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4721259Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4821259Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4921259Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5021259Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5121259Swollman * SUCH DAMAGE.
5221259Swollman *
5321259Swollman * The licence and distribution terms for any publically available version or
5421259Swollman * derivative of this code cannot be changed.  i.e. this code cannot simply be
55108533Sschweikh * copied and put under another distribution licence
5621259Swollman * [including the GNU Public Licence.]
5721259Swollman */
5821259Swollman
5921259Swollman/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
60108533Sschweikh/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
6121259Swollman
6221259Swollman#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
6321259Swollman# define TIMES
6421259Swollman#endif
6521259Swollman
6621259Swollman#include <stdio.h>
6721259Swollman
6821259Swollman#include <openssl/e_os2.h>
6921259Swollman#include OPENSSL_UNISTD_IO
7083366SjulianOPENSSL_DECLARE_EXIT
7121259Swollman#ifndef OPENSSL_SYS_NETWARE
7285074Sru# include <signal.h>
7321259Swollman# define crypt(c,s) (des_crypt((c),(s)))
7421259Swollman#endif
7521259Swollman#ifndef _IRIX
7621259Swollman# include <time.h>
7721259Swollman#endif
7821259Swollman#ifdef TIMES
7969224Sjlemon# include <sys/types.h>
8069152Sjlemon# include <sys/times.h>
8169224Sjlemon#endif
8274914Sjhb    /*
8374914Sjhb     * Depending on the VMS version, the tms structure is perhaps defined.
8483130Sjlemon     * The __TMS macro will show if it was.  If it wasn't defined, we should
8569152Sjlemon     * undefine TIMES, since that tells the rest of the program how things
86121816Sbrooks     * should be handled.  -- Richard Levitte
87121816Sbrooks     */
8860938Sjake#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
8960938Sjake# undef TIMES
9060938Sjake#endif
9172084Sphk#ifndef TIMES
9221259Swollman# include <sys/timeb.h>
9321259Swollman#endif
9421259Swollman#if defined(sun) || defined(__ultrix)
9521259Swollman# define _POSIX_SOURCE
9621259Swollman# include <limits.h>
9721259Swollman# include <sys/param.h>
9821259Swollman#endif
9921259Swollman#include <openssl/des.h>
10021259Swollman/* The following if from times(3) man page.  It may need to be changed */
10121259Swollman#ifndef HZ
10269152Sjlemon# ifndef CLK_TCK
10321259Swollman#  ifndef _BSD_CLK_TCK_         /* FreeBSD fix */
10421259Swollman#   define HZ   100.0
10521259Swollman#  else                         /* _BSD_CLK_TCK_ */
10621259Swollman#   define HZ ((double)_BSD_CLK_TCK_)
10721259Swollman#  endif
10821259Swollman# else                          /* CLK_TCK */
10921259Swollman#  define HZ ((double)CLK_TCK)
11084380Smjacob# endif
11184380Smjacob#endif
11284380Smjacob#define BUFSIZE ((long)1024)
11384380Smjacoblong run = 0;
11486797Sluigi
11586797Sluigidouble Time_F(int s);
11686797Sluigi#ifdef SIGALRM
11786797Sluigi# if defined(__STDC__) || defined(sgi) || defined(_AIX)
11886797Sluigi#  define SIGRETTYPE void
11986797Sluigi# else
12086797Sluigi#  define SIGRETTYPE int
12186797Sluigi# endif
12286797Sluigi
12386797SluigiSIGRETTYPE sig_done(int sig);
12486797SluigiSIGRETTYPE sig_done(int sig)
12586797Sluigi{
12686797Sluigi    signal(SIGALRM, sig_done);
12786797Sluigi    run = 0;
12886797Sluigi# ifdef LINT
12984380Smjacob    sig = sig;
13021259Swollman# endif
13121259Swollman}
13260938Sjake#endif
133121816Sbrooks
134121816Sbrooks#define START   0
135121816Sbrooks#define STOP    1
13621259Swollman
13783130Sjlemondouble Time_F(int s)
13883130Sjlemon{
13921259Swollman    double ret;
14021259Swollman#ifdef TIMES
14121259Swollman    static struct tms tstart, tend;
142106931Ssam
143102052Ssobomax    if (s == START) {
14483624Sjlemon        times(&tstart);
14583624Sjlemon        return (0);
14621259Swollman    } else {
14721259Swollman        times(&tend);
14821259Swollman        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
14921259Swollman        return ((ret == 0.0) ? 1e-6 : ret);
15021404Swollman    }
15121404Swollman#else                           /* !times() */
15221259Swollman    static struct timeb tstart, tend;
15321259Swollman    long i;
15492725Salfred
15592725Salfred    if (s == START) {
156106931Ssam        ftime(&tstart);
157106931Ssam        return (0);
15821259Swollman    } else {
15992725Salfred        ftime(&tend);
16021259Swollman        i = (long)tend.millitm - (long)tstart.millitm;
16192725Salfred        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1e3;
16221259Swollman        return ((ret == 0.0) ? 1e-6 : ret);
16392725Salfred    }
16421259Swollman#endif
16592725Salfred}
16621259Swollman
16792725Salfredint main(int argc, char **argv)
16821259Swollman{
16992725Salfred    long count;
17021259Swollman    static unsigned char buf[BUFSIZE];
17192725Salfred    static DES_cblock key =
17221259Swollman        { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
17392725Salfred    static DES_cblock key2 =
17421259Swollman        { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 };
17592725Salfred    static DES_cblock key3 =
17621404Swollman        { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 };
17792725Salfred    DES_key_schedule sch, sch2, sch3;
17821259Swollman    double a, b, c, d, e;
17921259Swollman#ifndef SIGALRM
18052904Sshin    long ca, cb, cc, cd, ce;
181123220Simp#endif
182122524Srwatson
183121161Sume#ifndef TIMES
184121161Sume    printf("To get the most accurate results, try to run this\n");
185121470Sume    printf("program when this computer is idle.\n");
186121470Sume#endif
18721259Swollman
18869152Sjlemon    DES_set_key_unchecked(&key2, &sch2);
18992725Salfred    DES_set_key_unchecked(&key3, &sch3);
19021259Swollman
19121259Swollman#ifndef SIGALRM
19221259Swollman    printf("First we calculate the approximate speed ...\n");
19321259Swollman    DES_set_key_unchecked(&key, &sch);
19421259Swollman    count = 10;
19521259Swollman    do {
19621259Swollman        long i;
19721259Swollman        DES_LONG data[2];
19858698Sjlemon
19921259Swollman        count *= 2;
20021259Swollman        Time_F(START);
20121259Swollman        for (i = count; i; i--)
20221259Swollman            DES_encrypt1(data, &sch, DES_ENCRYPT);
20321259Swollman        d = Time_F(STOP);
20421259Swollman    } while (d < 3.0);
20521259Swollman    ca = count;
20621259Swollman    cb = count * 3;
20721259Swollman    cc = count * 3 * 8 / BUFSIZE + 1;
20821259Swollman    cd = count * 8 / BUFSIZE + 1;
20921259Swollman    ce = count / 20 + 1;
21021259Swollman    printf("Doing set_key %ld times\n", ca);
21121259Swollman# define COND(d) (count != (d))
21221259Swollman# define COUNT(d) (d)
21321259Swollman#else
21421259Swollman# define COND(c) (run)
21553541Sshin# define COUNT(d) (count)
21653541Sshin    signal(SIGALRM, sig_done);
21753541Sshin    printf("Doing set_key for 10 seconds\n");
21853541Sshin    alarm(10);
21921259Swollman#endif
22021259Swollman
22121259Swollman    Time_F(START);
22221259Swollman    for (count = 0, run = 1; COND(ca); count++)
22321259Swollman        DES_set_key_unchecked(&key, &sch);
22421259Swollman    d = Time_F(STOP);
22521259Swollman    printf("%ld set_key's in %.2f seconds\n", count, d);
22621259Swollman    a = ((double)COUNT(ca)) / d;
22721259Swollman
22821259Swollman#ifdef SIGALRM
22921259Swollman    printf("Doing DES_encrypt's for 10 seconds\n");
23021259Swollman    alarm(10);
23172200Sbmilekic#else
23272200Sbmilekic    printf("Doing DES_encrypt %ld times\n", cb);
23369152Sjlemon#endif
23469152Sjlemon    Time_F(START);
23569152Sjlemon    for (count = 0, run = 1; COND(cb); count++) {
23621259Swollman        DES_LONG data[2];
23769152Sjlemon
23869152Sjlemon        DES_encrypt1(data, &sch, DES_ENCRYPT);
23969152Sjlemon    }
24069152Sjlemon    d = Time_F(STOP);
24169152Sjlemon    printf("%ld DES_encrypt's in %.2f second\n", count, d);
24269152Sjlemon    b = ((double)COUNT(cb) * 8) / d;
24369152Sjlemon
24469152Sjlemon#ifdef SIGALRM
24569152Sjlemon    printf("Doing DES_cbc_encrypt on %ld byte blocks for 10 seconds\n",
24669152Sjlemon           BUFSIZE);
24769152Sjlemon    alarm(10);
24869152Sjlemon#else
24969152Sjlemon    printf("Doing DES_cbc_encrypt %ld times on %ld byte blocks\n", cc,
25069152Sjlemon           BUFSIZE);
25169152Sjlemon#endif
25269152Sjlemon    Time_F(START);
25369152Sjlemon    for (count = 0, run = 1; COND(cc); count++)
25469152Sjlemon        DES_ncbc_encrypt(buf, buf, BUFSIZE, &sch, &key, DES_ENCRYPT);
25569152Sjlemon    d = Time_F(STOP);
25669152Sjlemon    printf("%ld DES_cbc_encrypt's of %ld byte blocks in %.2f second\n",
25769152Sjlemon           count, BUFSIZE, d);
25869152Sjlemon    c = ((double)COUNT(cc) * BUFSIZE) / d;
25969152Sjlemon
26069152Sjlemon#ifdef SIGALRM
26169152Sjlemon    printf("Doing DES_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
26269152Sjlemon           BUFSIZE);
26369152Sjlemon    alarm(10);
26469152Sjlemon#else
26569152Sjlemon    printf("Doing DES_ede_cbc_encrypt %ld times on %ld byte blocks\n", cd,
26669152Sjlemon           BUFSIZE);
26769152Sjlemon#endif
26869152Sjlemon    Time_F(START);
26969152Sjlemon    for (count = 0, run = 1; COND(cd); count++)
27069152Sjlemon        DES_ede3_cbc_encrypt(buf, buf, BUFSIZE,
27169152Sjlemon                             &sch, &sch2, &sch3, &key, DES_ENCRYPT);
27269152Sjlemon    d = Time_F(STOP);
27369152Sjlemon    printf("%ld DES_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
27469152Sjlemon           count, BUFSIZE, d);
27569152Sjlemon    d = ((double)COUNT(cd) * BUFSIZE) / d;
27669152Sjlemon
27769152Sjlemon#ifdef SIGALRM
27869152Sjlemon    printf("Doing crypt for 10 seconds\n");
27969152Sjlemon    alarm(10);
28069152Sjlemon#else
28169152Sjlemon    printf("Doing crypt %ld times\n", ce);
28269152Sjlemon#endif
28369152Sjlemon    Time_F(START);
28469152Sjlemon    for (count = 0, run = 1; COND(ce); count++)
28569152Sjlemon        crypt("testing1", "ef");
28669152Sjlemon    e = Time_F(STOP);
28769152Sjlemon    printf("%ld crypts in %.2f second\n", count, e);
28869152Sjlemon    e = ((double)COUNT(ce)) / e;
28969152Sjlemon
29069152Sjlemon    printf("set_key            per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a);
29169152Sjlemon    printf("DES raw ecb bytes  per sec = %12.2f (%9.3fuS)\n", b, 8.0e6 / b);
29269152Sjlemon    printf("DES cbc bytes      per sec = %12.2f (%9.3fuS)\n", c, 8.0e6 / c);
29369152Sjlemon    printf("DES ede cbc bytes  per sec = %12.2f (%9.3fuS)\n", d, 8.0e6 / d);
29469152Sjlemon    printf("crypt              per sec = %12.2f (%9.3fuS)\n", e, 1.0e6 / e);
29555205Speter    exit(0);
296121470Sume#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
297121470Sume    return (0);
298121470Sume#endif
299121470Sume}
300121470Sume