os/ossrv/ssl/libcrypto/src/crypto/des/speed.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/des/speed.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,314 @@
     1.4 +/* crypto/des/speed.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
    1.63 +/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
    1.64 +
    1.65 +#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
    1.66 +#define TIMES
    1.67 +#endif
    1.68 +
    1.69 +#include <stdio.h>
    1.70 +
    1.71 +#include <openssl/e_os2.h>
    1.72 +#include OPENSSL_UNISTD_IO
    1.73 +OPENSSL_DECLARE_EXIT
    1.74 +
    1.75 +#ifndef OPENSSL_SYS_NETWARE
    1.76 +#include <signal.h>
    1.77 +#define crypt(c,s) (des_crypt((c),(s)))
    1.78 +#endif
    1.79 +
    1.80 +#ifndef _IRIX
    1.81 +#include <time.h>
    1.82 +#endif
    1.83 +#ifdef TIMES
    1.84 +#include <sys/types.h>
    1.85 +#include <sys/times.h>
    1.86 +#endif
    1.87 +
    1.88 +/* Depending on the VMS version, the tms structure is perhaps defined.
    1.89 +   The __TMS macro will show if it was.  If it wasn't defined, we should
    1.90 +   undefine TIMES, since that tells the rest of the program how things
    1.91 +   should be handled.				-- Richard Levitte */
    1.92 +#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
    1.93 +#undef TIMES
    1.94 +#endif
    1.95 +
    1.96 +#ifndef TIMES
    1.97 +#include <sys/timeb.h>
    1.98 +#endif
    1.99 +
   1.100 +#if defined(sun) || defined(__ultrix)
   1.101 +#define _POSIX_SOURCE
   1.102 +#include <limits.h>
   1.103 +#include <sys/param.h>
   1.104 +#endif
   1.105 +
   1.106 +#include <openssl/des.h>
   1.107 +
   1.108 +/* The following if from times(3) man page.  It may need to be changed */
   1.109 +#ifndef HZ
   1.110 +# ifndef CLK_TCK
   1.111 +#  ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
   1.112 +#   define HZ	100.0
   1.113 +#  else /* _BSD_CLK_TCK_ */
   1.114 +#   define HZ ((double)_BSD_CLK_TCK_)
   1.115 +#  endif
   1.116 +# else /* CLK_TCK */
   1.117 +#  define HZ ((double)CLK_TCK)
   1.118 +# endif
   1.119 +#endif
   1.120 +
   1.121 +#define BUFSIZE	((long)1024)
   1.122 +long run=0;
   1.123 +
   1.124 +double Time_F(int s);
   1.125 +#ifdef SIGALRM
   1.126 +#if defined(__STDC__) || defined(sgi) || defined(_AIX)
   1.127 +#define SIGRETTYPE void
   1.128 +#else
   1.129 +#define SIGRETTYPE int
   1.130 +#endif
   1.131 +
   1.132 +SIGRETTYPE sig_done(int sig);
   1.133 +SIGRETTYPE sig_done(int sig)
   1.134 +	{
   1.135 +	signal(SIGALRM,sig_done);
   1.136 +	run=0;
   1.137 +#ifdef LINT
   1.138 +	sig=sig;
   1.139 +#endif
   1.140 +	}
   1.141 +#endif
   1.142 +
   1.143 +#define START	0
   1.144 +#define STOP	1
   1.145 +
   1.146 +double Time_F(int s)
   1.147 +	{
   1.148 +	double ret;
   1.149 +#ifdef TIMES
   1.150 +	static struct tms tstart,tend;
   1.151 +
   1.152 +	if (s == START)
   1.153 +		{
   1.154 +		times(&tstart);
   1.155 +		return(0);
   1.156 +		}
   1.157 +	else
   1.158 +		{
   1.159 +		times(&tend);
   1.160 +		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
   1.161 +		return((ret == 0.0)?1e-6:ret);
   1.162 +		}
   1.163 +#else /* !times() */
   1.164 +	static struct timeb tstart,tend;
   1.165 +	long i;
   1.166 +
   1.167 +	if (s == START)
   1.168 +		{
   1.169 +		ftime(&tstart);
   1.170 +		return(0);
   1.171 +		}
   1.172 +	else
   1.173 +		{
   1.174 +		ftime(&tend);
   1.175 +		i=(long)tend.millitm-(long)tstart.millitm;
   1.176 +		ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
   1.177 +		return((ret == 0.0)?1e-6:ret);
   1.178 +		}
   1.179 +#endif
   1.180 +	}
   1.181 +
   1.182 +int main(int argc, char **argv)
   1.183 +	{
   1.184 +	long count;
   1.185 +	static unsigned char buf[BUFSIZE];
   1.186 +	static DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
   1.187 +	static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
   1.188 +	static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
   1.189 +	DES_key_schedule sch,sch2,sch3;
   1.190 +	double a,b,c,d,e;
   1.191 +#ifndef SIGALRM
   1.192 +	long ca,cb,cc,cd,ce;
   1.193 +#endif
   1.194 +
   1.195 +#ifndef TIMES
   1.196 +	printf("To get the most accurate results, try to run this\n");
   1.197 +	printf("program when this computer is idle.\n");
   1.198 +#endif
   1.199 +
   1.200 +	DES_set_key_unchecked(&key2,&sch2);
   1.201 +	DES_set_key_unchecked(&key3,&sch3);
   1.202 +
   1.203 +#ifndef SIGALRM
   1.204 +	printf("First we calculate the approximate speed ...\n");
   1.205 +	DES_set_key_unchecked(&key,&sch);
   1.206 +	count=10;
   1.207 +	do	{
   1.208 +		long i;
   1.209 +		DES_LONG data[2];
   1.210 +
   1.211 +		count*=2;
   1.212 +		Time_F(START);
   1.213 +		for (i=count; i; i--)
   1.214 +			DES_encrypt1(data,&sch,DES_ENCRYPT);
   1.215 +		d=Time_F(STOP);
   1.216 +		} while (d < 3.0);
   1.217 +	ca=count;
   1.218 +	cb=count*3;
   1.219 +	cc=count*3*8/BUFSIZE+1;
   1.220 +	cd=count*8/BUFSIZE+1;
   1.221 +	ce=count/20+1;
   1.222 +	printf("Doing set_key %ld times\n",ca);
   1.223 +#define COND(d)	(count != (d))
   1.224 +#define COUNT(d) (d)
   1.225 +#else
   1.226 +#define COND(c)	(run)
   1.227 +#define COUNT(d) (count)
   1.228 +	signal(SIGALRM,sig_done);
   1.229 +	printf("Doing set_key for 10 seconds\n");
   1.230 +	alarm(10);
   1.231 +#endif
   1.232 +
   1.233 +	Time_F(START);
   1.234 +	for (count=0,run=1; COND(ca); count++)
   1.235 +		DES_set_key_unchecked(&key,&sch);
   1.236 +	d=Time_F(STOP);
   1.237 +	printf("%ld set_key's in %.2f seconds\n",count,d);
   1.238 +	a=((double)COUNT(ca))/d;
   1.239 +
   1.240 +#ifdef SIGALRM
   1.241 +	printf("Doing DES_encrypt's for 10 seconds\n");
   1.242 +	alarm(10);
   1.243 +#else
   1.244 +	printf("Doing DES_encrypt %ld times\n",cb);
   1.245 +#endif
   1.246 +	Time_F(START);
   1.247 +	for (count=0,run=1; COND(cb); count++)
   1.248 +		{
   1.249 +		DES_LONG data[2];
   1.250 +
   1.251 +		DES_encrypt1(data,&sch,DES_ENCRYPT);
   1.252 +		}
   1.253 +	d=Time_F(STOP);
   1.254 +	printf("%ld DES_encrypt's in %.2f second\n",count,d);
   1.255 +	b=((double)COUNT(cb)*8)/d;
   1.256 +
   1.257 +#ifdef SIGALRM
   1.258 +	printf("Doing DES_cbc_encrypt on %ld byte blocks for 10 seconds\n",
   1.259 +		BUFSIZE);
   1.260 +	alarm(10);
   1.261 +#else
   1.262 +	printf("Doing DES_cbc_encrypt %ld times on %ld byte blocks\n",cc,
   1.263 +		BUFSIZE);
   1.264 +#endif
   1.265 +	Time_F(START);
   1.266 +	for (count=0,run=1; COND(cc); count++)
   1.267 +		DES_ncbc_encrypt(buf,buf,BUFSIZE,&sch,
   1.268 +			&key,DES_ENCRYPT);
   1.269 +	d=Time_F(STOP);
   1.270 +	printf("%ld DES_cbc_encrypt's of %ld byte blocks in %.2f second\n",
   1.271 +		count,BUFSIZE,d);
   1.272 +	c=((double)COUNT(cc)*BUFSIZE)/d;
   1.273 +
   1.274 +#ifdef SIGALRM
   1.275 +	printf("Doing DES_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
   1.276 +		BUFSIZE);
   1.277 +	alarm(10);
   1.278 +#else
   1.279 +	printf("Doing DES_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,
   1.280 +		BUFSIZE);
   1.281 +#endif
   1.282 +	Time_F(START);
   1.283 +	for (count=0,run=1; COND(cd); count++)
   1.284 +		DES_ede3_cbc_encrypt(buf,buf,BUFSIZE,
   1.285 +			&sch,
   1.286 +			&sch2,
   1.287 +			&sch3,
   1.288 +			&key,
   1.289 +			DES_ENCRYPT);
   1.290 +	d=Time_F(STOP);
   1.291 +	printf("%ld DES_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
   1.292 +		count,BUFSIZE,d);
   1.293 +	d=((double)COUNT(cd)*BUFSIZE)/d;
   1.294 +
   1.295 +#ifdef SIGALRM
   1.296 +	printf("Doing crypt for 10 seconds\n");
   1.297 +	alarm(10);
   1.298 +#else
   1.299 +	printf("Doing crypt %ld times\n",ce);
   1.300 +#endif
   1.301 +	Time_F(START);
   1.302 +	for (count=0,run=1; COND(ce); count++)
   1.303 +		crypt("testing1","ef");
   1.304 +	e=Time_F(STOP);
   1.305 +	printf("%ld crypts in %.2f second\n",count,e);
   1.306 +	e=((double)COUNT(ce))/e;
   1.307 +
   1.308 +	printf("set_key            per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
   1.309 +	printf("DES raw ecb bytes  per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
   1.310 +	printf("DES cbc bytes      per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
   1.311 +	printf("DES ede cbc bytes  per sec = %12.2f (%9.3fuS)\n",d,8.0e6/d);
   1.312 +	printf("crypt              per sec = %12.2f (%9.3fuS)\n",e,1.0e6/e);
   1.313 +	exit(0);
   1.314 +#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
   1.315 +	return(0);
   1.316 +#endif
   1.317 +	}