155714Skris/* crypto/des/speed.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. 8280297Sjkim * 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). 15280297Sjkim * 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. 22280297Sjkim * 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 :-). 37280297Sjkim * 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)" 40280297Sjkim * 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. 52280297Sjkim * 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) 63280297Sjkim# 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 72280297Sjkim# include <signal.h> 73280297Sjkim# define crypt(c,s) (des_crypt((c),(s))) 74160814Ssimon#endif 7555714Skris#ifndef _IRIX 76280297Sjkim# include <time.h> 7755714Skris#endif 7855714Skris#ifdef TIMES 79280297Sjkim# include <sys/types.h> 80280297Sjkim# include <sys/times.h> 8155714Skris#endif 82280297Sjkim /* 83280297Sjkim * Depending on the VMS version, the tms structure is perhaps defined. 84280297Sjkim * The __TMS macro will show if it was. If it wasn't defined, we should 85280297Sjkim * undefine TIMES, since that tells the rest of the program how things 86280297Sjkim * should be handled. -- Richard Levitte 87280297Sjkim */ 88109998Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS) 89280297Sjkim# undef TIMES 9055714Skris#endif 9155714Skris#ifndef TIMES 92280297Sjkim# include <sys/timeb.h> 9355714Skris#endif 9455714Skris#if defined(sun) || defined(__ultrix) 95280297Sjkim# define _POSIX_SOURCE 96280297Sjkim# include <limits.h> 97280297Sjkim# include <sys/param.h> 9855714Skris#endif 9955714Skris#include <openssl/des.h> 10055714Skris/* The following if from times(3) man page. It may need to be changed */ 10155714Skris#ifndef HZ 10255714Skris# ifndef CLK_TCK 103280297Sjkim# ifndef _BSD_CLK_TCK_ /* FreeBSD fix */ 104280297Sjkim# define HZ 100.0 105280297Sjkim# else /* _BSD_CLK_TCK_ */ 10655714Skris# define HZ ((double)_BSD_CLK_TCK_) 10755714Skris# endif 108280297Sjkim# else /* CLK_TCK */ 10955714Skris# define HZ ((double)CLK_TCK) 11055714Skris# endif 11155714Skris#endif 112280297Sjkim#define BUFSIZE ((long)1024) 113280297Sjkimlong run = 0; 11455714Skris 11555714Skrisdouble Time_F(int s); 11655714Skris#ifdef SIGALRM 117280297Sjkim# if defined(__STDC__) || defined(sgi) || defined(_AIX) 118280297Sjkim# define SIGRETTYPE void 119280297Sjkim# else 120280297Sjkim# define SIGRETTYPE int 121280297Sjkim# endif 12255714Skris 12355714SkrisSIGRETTYPE sig_done(int sig); 12455714SkrisSIGRETTYPE sig_done(int sig) 125280297Sjkim{ 126280297Sjkim signal(SIGALRM, sig_done); 127280297Sjkim run = 0; 128280297Sjkim# ifdef LINT 129280297Sjkim sig = sig; 130280297Sjkim# endif 131280297Sjkim} 13255714Skris#endif 13355714Skris 134280297Sjkim#define START 0 135280297Sjkim#define STOP 1 13655714Skris 13755714Skrisdouble Time_F(int s) 138280297Sjkim{ 139280297Sjkim double ret; 14055714Skris#ifdef TIMES 141280297Sjkim static struct tms tstart, tend; 14255714Skris 143280297Sjkim if (s == START) { 144280297Sjkim times(&tstart); 145280297Sjkim return (0); 146280297Sjkim } else { 147280297Sjkim times(&tend); 148280297Sjkim ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ; 149280297Sjkim return ((ret == 0.0) ? 1e-6 : ret); 150280297Sjkim } 151280297Sjkim#else /* !times() */ 152280297Sjkim static struct timeb tstart, tend; 153280297Sjkim long i; 15455714Skris 155280297Sjkim if (s == START) { 156280297Sjkim ftime(&tstart); 157280297Sjkim return (0); 158280297Sjkim } else { 159280297Sjkim ftime(&tend); 160280297Sjkim i = (long)tend.millitm - (long)tstart.millitm; 161280297Sjkim ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1e3; 162280297Sjkim return ((ret == 0.0) ? 1e-6 : ret); 163280297Sjkim } 16455714Skris#endif 165280297Sjkim} 16655714Skris 16755714Skrisint main(int argc, char **argv) 168280297Sjkim{ 169280297Sjkim long count; 170280297Sjkim static unsigned char buf[BUFSIZE]; 171280297Sjkim static DES_cblock key = 172280297Sjkim { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }; 173280297Sjkim static DES_cblock key2 = 174280297Sjkim { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }; 175280297Sjkim static DES_cblock key3 = 176280297Sjkim { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 }; 177280297Sjkim DES_key_schedule sch, sch2, sch3; 178280297Sjkim double a, b, c, d, e; 17955714Skris#ifndef SIGALRM 180280297Sjkim long ca, cb, cc, cd, ce; 18155714Skris#endif 18255714Skris 18355714Skris#ifndef TIMES 184280297Sjkim printf("To get the most accurate results, try to run this\n"); 185280297Sjkim printf("program when this computer is idle.\n"); 18655714Skris#endif 18755714Skris 188280297Sjkim DES_set_key_unchecked(&key2, &sch2); 189280297Sjkim DES_set_key_unchecked(&key3, &sch3); 19055714Skris 19155714Skris#ifndef SIGALRM 192280297Sjkim printf("First we calculate the approximate speed ...\n"); 193280297Sjkim DES_set_key_unchecked(&key, &sch); 194280297Sjkim count = 10; 195280297Sjkim do { 196280297Sjkim long i; 197280297Sjkim DES_LONG data[2]; 19855714Skris 199280297Sjkim count *= 2; 200280297Sjkim Time_F(START); 201280297Sjkim for (i = count; i; i--) 202280297Sjkim DES_encrypt1(data, &sch, DES_ENCRYPT); 203280297Sjkim d = Time_F(STOP); 204280297Sjkim } while (d < 3.0); 205280297Sjkim ca = count; 206280297Sjkim cb = count * 3; 207280297Sjkim cc = count * 3 * 8 / BUFSIZE + 1; 208280297Sjkim cd = count * 8 / BUFSIZE + 1; 209280297Sjkim ce = count / 20 + 1; 210280297Sjkim printf("Doing set_key %ld times\n", ca); 211280297Sjkim# define COND(d) (count != (d)) 212280297Sjkim# define COUNT(d) (d) 21355714Skris#else 214280297Sjkim# define COND(c) (run) 215280297Sjkim# define COUNT(d) (count) 216280297Sjkim signal(SIGALRM, sig_done); 217280297Sjkim printf("Doing set_key for 10 seconds\n"); 218280297Sjkim alarm(10); 21955714Skris#endif 22055714Skris 221280297Sjkim Time_F(START); 222280297Sjkim for (count = 0, run = 1; COND(ca); count++) 223280297Sjkim DES_set_key_unchecked(&key, &sch); 224280297Sjkim d = Time_F(STOP); 225280297Sjkim printf("%ld set_key's in %.2f seconds\n", count, d); 226280297Sjkim a = ((double)COUNT(ca)) / d; 22755714Skris 22855714Skris#ifdef SIGALRM 229280297Sjkim printf("Doing DES_encrypt's for 10 seconds\n"); 230280297Sjkim alarm(10); 23155714Skris#else 232280297Sjkim printf("Doing DES_encrypt %ld times\n", cb); 23355714Skris#endif 234280297Sjkim Time_F(START); 235280297Sjkim for (count = 0, run = 1; COND(cb); count++) { 236280297Sjkim DES_LONG data[2]; 23755714Skris 238280297Sjkim DES_encrypt1(data, &sch, DES_ENCRYPT); 239280297Sjkim } 240280297Sjkim d = Time_F(STOP); 241280297Sjkim printf("%ld DES_encrypt's in %.2f second\n", count, d); 242280297Sjkim b = ((double)COUNT(cb) * 8) / d; 24355714Skris 24455714Skris#ifdef SIGALRM 245280297Sjkim printf("Doing DES_cbc_encrypt on %ld byte blocks for 10 seconds\n", 246280297Sjkim BUFSIZE); 247280297Sjkim alarm(10); 24855714Skris#else 249280297Sjkim printf("Doing DES_cbc_encrypt %ld times on %ld byte blocks\n", cc, 250280297Sjkim BUFSIZE); 25155714Skris#endif 252280297Sjkim Time_F(START); 253280297Sjkim for (count = 0, run = 1; COND(cc); count++) 254280297Sjkim DES_ncbc_encrypt(buf, buf, BUFSIZE, &sch, &key, DES_ENCRYPT); 255280297Sjkim d = Time_F(STOP); 256280297Sjkim printf("%ld DES_cbc_encrypt's of %ld byte blocks in %.2f second\n", 257280297Sjkim count, BUFSIZE, d); 258280297Sjkim c = ((double)COUNT(cc) * BUFSIZE) / d; 25955714Skris 26055714Skris#ifdef SIGALRM 261280297Sjkim printf("Doing DES_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n", 262280297Sjkim BUFSIZE); 263280297Sjkim alarm(10); 26455714Skris#else 265280297Sjkim printf("Doing DES_ede_cbc_encrypt %ld times on %ld byte blocks\n", cd, 266280297Sjkim BUFSIZE); 26755714Skris#endif 268280297Sjkim Time_F(START); 269280297Sjkim for (count = 0, run = 1; COND(cd); count++) 270280297Sjkim DES_ede3_cbc_encrypt(buf, buf, BUFSIZE, 271280297Sjkim &sch, &sch2, &sch3, &key, DES_ENCRYPT); 272280297Sjkim d = Time_F(STOP); 273280297Sjkim printf("%ld DES_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n", 274280297Sjkim count, BUFSIZE, d); 275280297Sjkim d = ((double)COUNT(cd) * BUFSIZE) / d; 27655714Skris 27755714Skris#ifdef SIGALRM 278280297Sjkim printf("Doing crypt for 10 seconds\n"); 279280297Sjkim alarm(10); 28055714Skris#else 281280297Sjkim printf("Doing crypt %ld times\n", ce); 28255714Skris#endif 283280297Sjkim Time_F(START); 284280297Sjkim for (count = 0, run = 1; COND(ce); count++) 285280297Sjkim crypt("testing1", "ef"); 286280297Sjkim e = Time_F(STOP); 287280297Sjkim printf("%ld crypts in %.2f second\n", count, e); 288280297Sjkim e = ((double)COUNT(ce)) / e; 28955714Skris 290280297Sjkim printf("set_key per sec = %12.2f (%9.3fuS)\n", a, 1.0e6 / a); 291280297Sjkim printf("DES raw ecb bytes per sec = %12.2f (%9.3fuS)\n", b, 8.0e6 / b); 292280297Sjkim printf("DES cbc bytes per sec = %12.2f (%9.3fuS)\n", c, 8.0e6 / c); 293280297Sjkim printf("DES ede cbc bytes per sec = %12.2f (%9.3fuS)\n", d, 8.0e6 / d); 294280297Sjkim printf("crypt per sec = %12.2f (%9.3fuS)\n", e, 1.0e6 / e); 295280297Sjkim exit(0); 296109998Smarkm#if defined(LINT) || defined(OPENSSL_SYS_MSDOS) 297280297Sjkim return (0); 29855714Skris#endif 299280297Sjkim} 300