bfspeed.c revision 55714
155714Skris/* crypto/bf/bfspeed.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. 855714Skris * 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). 1555714Skris * 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. 2255714Skris * 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 :-). 3755714Skris * 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)" 4055714Skris * 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. 5255714Skris * 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 6255714Skris#if !defined(MSDOS) && (!defined(VMS) || defined(__DECC)) 6355714Skris#define TIMES 6455714Skris#endif 6555714Skris 6655714Skris#include <stdio.h> 6755714Skris 6855714Skris#include <openssl/e_os2.h> 6955714Skris#include OPENSSL_UNISTD_IO 7055714SkrisOPENSSL_DECLARE_EXIT 7155714Skris 7255714Skris#include <signal.h> 7355714Skris#ifndef _IRIX 7455714Skris#include <time.h> 7555714Skris#endif 7655714Skris#ifdef TIMES 7755714Skris#include <sys/types.h> 7855714Skris#include <sys/times.h> 7955714Skris#endif 8055714Skris 8155714Skris/* Depending on the VMS version, the tms structure is perhaps defined. 8255714Skris The __TMS macro will show if it was. If it wasn't defined, we should 8355714Skris undefine TIMES, since that tells the rest of the program how things 8455714Skris should be handled. -- Richard Levitte */ 8555714Skris#if defined(VMS) && defined(__DECC) && !defined(__TMS) 8655714Skris#undef TIMES 8755714Skris#endif 8855714Skris 8955714Skris#ifndef TIMES 9055714Skris#include <sys/timeb.h> 9155714Skris#endif 9255714Skris 9355714Skris#if defined(sun) || defined(__ultrix) 9455714Skris#define _POSIX_SOURCE 9555714Skris#include <limits.h> 9655714Skris#include <sys/param.h> 9755714Skris#endif 9855714Skris 9955714Skris#include <openssl/blowfish.h> 10055714Skris 10155714Skris/* The following if from times(3) man page. It may need to be changed */ 10255714Skris#ifndef HZ 10355714Skris#ifndef CLK_TCK 10455714Skris#define HZ 100.0 10555714Skris#else /* CLK_TCK */ 10655714Skris#define HZ ((double)CLK_TCK) 10755714Skris#endif 10855714Skris#endif 10955714Skris 11055714Skris#define BUFSIZE ((long)1024) 11155714Skrislong run=0; 11255714Skris 11355714Skrisdouble Time_F(int s); 11455714Skris#ifdef SIGALRM 11555714Skris#if defined(__STDC__) || defined(sgi) || defined(_AIX) 11655714Skris#define SIGRETTYPE void 11755714Skris#else 11855714Skris#define SIGRETTYPE int 11955714Skris#endif 12055714Skris 12155714SkrisSIGRETTYPE sig_done(int sig); 12255714SkrisSIGRETTYPE sig_done(int sig) 12355714Skris { 12455714Skris signal(SIGALRM,sig_done); 12555714Skris run=0; 12655714Skris#ifdef LINT 12755714Skris sig=sig; 12855714Skris#endif 12955714Skris } 13055714Skris#endif 13155714Skris 13255714Skris#define START 0 13355714Skris#define STOP 1 13455714Skris 13555714Skrisdouble Time_F(int s) 13655714Skris { 13755714Skris double ret; 13855714Skris#ifdef TIMES 13955714Skris static struct tms tstart,tend; 14055714Skris 14155714Skris if (s == START) 14255714Skris { 14355714Skris times(&tstart); 14455714Skris return(0); 14555714Skris } 14655714Skris else 14755714Skris { 14855714Skris times(&tend); 14955714Skris ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ; 15055714Skris return((ret == 0.0)?1e-6:ret); 15155714Skris } 15255714Skris#else /* !times() */ 15355714Skris static struct timeb tstart,tend; 15455714Skris long i; 15555714Skris 15655714Skris if (s == START) 15755714Skris { 15855714Skris ftime(&tstart); 15955714Skris return(0); 16055714Skris } 16155714Skris else 16255714Skris { 16355714Skris ftime(&tend); 16455714Skris i=(long)tend.millitm-(long)tstart.millitm; 16555714Skris ret=((double)(tend.time-tstart.time))+((double)i)/1e3; 16655714Skris return((ret == 0.0)?1e-6:ret); 16755714Skris } 16855714Skris#endif 16955714Skris } 17055714Skris 17155714Skrisint main(int argc, char **argv) 17255714Skris { 17355714Skris long count; 17455714Skris static unsigned char buf[BUFSIZE]; 17555714Skris static unsigned char key[] ={ 17655714Skris 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0, 17755714Skris 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10, 17855714Skris }; 17955714Skris BF_KEY sch; 18055714Skris double a,b,c,d; 18155714Skris#ifndef SIGALRM 18255714Skris long ca,cb,cc; 18355714Skris#endif 18455714Skris 18555714Skris#ifndef TIMES 18655714Skris printf("To get the most acurate results, try to run this\n"); 18755714Skris printf("program when this computer is idle.\n"); 18855714Skris#endif 18955714Skris 19055714Skris#ifndef SIGALRM 19155714Skris printf("First we calculate the approximate speed ...\n"); 19255714Skris BF_set_key(&sch,16,key); 19355714Skris count=10; 19455714Skris do { 19555714Skris long i; 19655714Skris BF_LONG data[2]; 19755714Skris 19855714Skris count*=2; 19955714Skris Time_F(START); 20055714Skris for (i=count; i; i--) 20155714Skris BF_encrypt(data,&sch); 20255714Skris d=Time_F(STOP); 20355714Skris } while (d < 3.0); 20455714Skris ca=count/512; 20555714Skris cb=count; 20655714Skris cc=count*8/BUFSIZE+1; 20755714Skris printf("Doing BF_set_key %ld times\n",ca); 20855714Skris#define COND(d) (count != (d)) 20955714Skris#define COUNT(d) (d) 21055714Skris#else 21155714Skris#define COND(c) (run) 21255714Skris#define COUNT(d) (count) 21355714Skris signal(SIGALRM,sig_done); 21455714Skris printf("Doing BF_set_key for 10 seconds\n"); 21555714Skris alarm(10); 21655714Skris#endif 21755714Skris 21855714Skris Time_F(START); 21955714Skris for (count=0,run=1; COND(ca); count+=4) 22055714Skris { 22155714Skris BF_set_key(&sch,16,key); 22255714Skris BF_set_key(&sch,16,key); 22355714Skris BF_set_key(&sch,16,key); 22455714Skris BF_set_key(&sch,16,key); 22555714Skris } 22655714Skris d=Time_F(STOP); 22755714Skris printf("%ld BF_set_key's in %.2f seconds\n",count,d); 22855714Skris a=((double)COUNT(ca))/d; 22955714Skris 23055714Skris#ifdef SIGALRM 23155714Skris printf("Doing BF_encrypt's for 10 seconds\n"); 23255714Skris alarm(10); 23355714Skris#else 23455714Skris printf("Doing BF_encrypt %ld times\n",cb); 23555714Skris#endif 23655714Skris Time_F(START); 23755714Skris for (count=0,run=1; COND(cb); count+=4) 23855714Skris { 23955714Skris BF_LONG data[2]; 24055714Skris 24155714Skris BF_encrypt(data,&sch); 24255714Skris BF_encrypt(data,&sch); 24355714Skris BF_encrypt(data,&sch); 24455714Skris BF_encrypt(data,&sch); 24555714Skris } 24655714Skris d=Time_F(STOP); 24755714Skris printf("%ld BF_encrypt's in %.2f second\n",count,d); 24855714Skris b=((double)COUNT(cb)*8)/d; 24955714Skris 25055714Skris#ifdef SIGALRM 25155714Skris printf("Doing BF_cbc_encrypt on %ld byte blocks for 10 seconds\n", 25255714Skris BUFSIZE); 25355714Skris alarm(10); 25455714Skris#else 25555714Skris printf("Doing BF_cbc_encrypt %ld times on %ld byte blocks\n",cc, 25655714Skris BUFSIZE); 25755714Skris#endif 25855714Skris Time_F(START); 25955714Skris for (count=0,run=1; COND(cc); count++) 26055714Skris BF_cbc_encrypt(buf,buf,BUFSIZE,&sch, 26155714Skris &(key[0]),BF_ENCRYPT); 26255714Skris d=Time_F(STOP); 26355714Skris printf("%ld BF_cbc_encrypt's of %ld byte blocks in %.2f second\n", 26455714Skris count,BUFSIZE,d); 26555714Skris c=((double)COUNT(cc)*BUFSIZE)/d; 26655714Skris 26755714Skris printf("Blowfish set_key per sec = %12.3f (%9.3fuS)\n",a,1.0e6/a); 26855714Skris printf("Blowfish raw ecb bytes per sec = %12.3f (%9.3fuS)\n",b,8.0e6/b); 26955714Skris printf("Blowfish cbc bytes per sec = %12.3f (%9.3fuS)\n",c,8.0e6/c); 27055714Skris exit(0); 27155714Skris#if defined(LINT) || defined(MSDOS) 27255714Skris return(0); 27355714Skris#endif 27455714Skris } 275