sl@0
|
1 |
/* unused */
|
sl@0
|
2 |
|
sl@0
|
3 |
/* crypto/bn/bnspeed.c */
|
sl@0
|
4 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
5 |
* All rights reserved.
|
sl@0
|
6 |
*
|
sl@0
|
7 |
* This package is an SSL implementation written
|
sl@0
|
8 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
9 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
12 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
13 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
14 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
15 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
16 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
17 |
*
|
sl@0
|
18 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
19 |
* the code are not to be removed.
|
sl@0
|
20 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
21 |
* as the author of the parts of the library used.
|
sl@0
|
22 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
23 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
24 |
*
|
sl@0
|
25 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
26 |
* modification, are permitted provided that the following conditions
|
sl@0
|
27 |
* are met:
|
sl@0
|
28 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
30 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
31 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
32 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
33 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
34 |
* must display the following acknowledgement:
|
sl@0
|
35 |
* "This product includes cryptographic software written by
|
sl@0
|
36 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
37 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
38 |
* being used are not cryptographic related :-).
|
sl@0
|
39 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
40 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
41 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
42 |
*
|
sl@0
|
43 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
44 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
45 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
46 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
47 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
48 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
49 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
50 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
51 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
52 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
53 |
* SUCH DAMAGE.
|
sl@0
|
54 |
*
|
sl@0
|
55 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
56 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
57 |
* copied and put under another distribution licence
|
sl@0
|
58 |
* [including the GNU Public Licence.]
|
sl@0
|
59 |
*/
|
sl@0
|
60 |
|
sl@0
|
61 |
/* most of this code has been pilfered from my libdes speed.c program */
|
sl@0
|
62 |
|
sl@0
|
63 |
#define BASENUM 1000000
|
sl@0
|
64 |
#undef PROG
|
sl@0
|
65 |
#define PROG bnspeed_main
|
sl@0
|
66 |
|
sl@0
|
67 |
#include <stdio.h>
|
sl@0
|
68 |
#include <stdlib.h>
|
sl@0
|
69 |
#include <signal.h>
|
sl@0
|
70 |
#include <string.h>
|
sl@0
|
71 |
#include <openssl/crypto.h>
|
sl@0
|
72 |
#include <openssl/err.h>
|
sl@0
|
73 |
|
sl@0
|
74 |
#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
|
sl@0
|
75 |
#define TIMES
|
sl@0
|
76 |
#endif
|
sl@0
|
77 |
|
sl@0
|
78 |
#ifndef _IRIX
|
sl@0
|
79 |
#include <time.h>
|
sl@0
|
80 |
#endif
|
sl@0
|
81 |
#ifdef TIMES
|
sl@0
|
82 |
#include <sys/types.h>
|
sl@0
|
83 |
#include <sys/times.h>
|
sl@0
|
84 |
#endif
|
sl@0
|
85 |
|
sl@0
|
86 |
/* Depending on the VMS version, the tms structure is perhaps defined.
|
sl@0
|
87 |
The __TMS macro will show if it was. If it wasn't defined, we should
|
sl@0
|
88 |
undefine TIMES, since that tells the rest of the program how things
|
sl@0
|
89 |
should be handled. -- Richard Levitte */
|
sl@0
|
90 |
#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
|
sl@0
|
91 |
#undef TIMES
|
sl@0
|
92 |
#endif
|
sl@0
|
93 |
|
sl@0
|
94 |
#ifndef TIMES
|
sl@0
|
95 |
#include <sys/timeb.h>
|
sl@0
|
96 |
#endif
|
sl@0
|
97 |
|
sl@0
|
98 |
#if defined(sun) || defined(__ultrix)
|
sl@0
|
99 |
#define _POSIX_SOURCE
|
sl@0
|
100 |
#include <limits.h>
|
sl@0
|
101 |
#include <sys/param.h>
|
sl@0
|
102 |
#endif
|
sl@0
|
103 |
|
sl@0
|
104 |
#include <openssl/bn.h>
|
sl@0
|
105 |
#include <openssl/x509.h>
|
sl@0
|
106 |
|
sl@0
|
107 |
/* The following if from times(3) man page. It may need to be changed */
|
sl@0
|
108 |
#ifndef HZ
|
sl@0
|
109 |
# ifndef CLK_TCK
|
sl@0
|
110 |
# ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
|
sl@0
|
111 |
# define HZ 100.0
|
sl@0
|
112 |
# else /* _BSD_CLK_TCK_ */
|
sl@0
|
113 |
# define HZ ((double)_BSD_CLK_TCK_)
|
sl@0
|
114 |
# endif
|
sl@0
|
115 |
# else /* CLK_TCK */
|
sl@0
|
116 |
# define HZ ((double)CLK_TCK)
|
sl@0
|
117 |
# endif
|
sl@0
|
118 |
#endif
|
sl@0
|
119 |
|
sl@0
|
120 |
#undef BUFSIZE
|
sl@0
|
121 |
#define BUFSIZE ((long)1024*8)
|
sl@0
|
122 |
int run=0;
|
sl@0
|
123 |
|
sl@0
|
124 |
static double Time_F(int s);
|
sl@0
|
125 |
#define START 0
|
sl@0
|
126 |
#define STOP 1
|
sl@0
|
127 |
|
sl@0
|
128 |
static double Time_F(int s)
|
sl@0
|
129 |
{
|
sl@0
|
130 |
double ret;
|
sl@0
|
131 |
#ifdef TIMES
|
sl@0
|
132 |
static struct tms tstart,tend;
|
sl@0
|
133 |
|
sl@0
|
134 |
if (s == START)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
times(&tstart);
|
sl@0
|
137 |
return(0);
|
sl@0
|
138 |
}
|
sl@0
|
139 |
else
|
sl@0
|
140 |
{
|
sl@0
|
141 |
times(&tend);
|
sl@0
|
142 |
ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
|
sl@0
|
143 |
return((ret < 1e-3)?1e-3:ret);
|
sl@0
|
144 |
}
|
sl@0
|
145 |
#else /* !times() */
|
sl@0
|
146 |
static struct timeb tstart,tend;
|
sl@0
|
147 |
long i;
|
sl@0
|
148 |
|
sl@0
|
149 |
if (s == START)
|
sl@0
|
150 |
{
|
sl@0
|
151 |
ftime(&tstart);
|
sl@0
|
152 |
return(0);
|
sl@0
|
153 |
}
|
sl@0
|
154 |
else
|
sl@0
|
155 |
{
|
sl@0
|
156 |
ftime(&tend);
|
sl@0
|
157 |
i=(long)tend.millitm-(long)tstart.millitm;
|
sl@0
|
158 |
ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
|
sl@0
|
159 |
return((ret < 0.001)?0.001:ret);
|
sl@0
|
160 |
}
|
sl@0
|
161 |
#endif
|
sl@0
|
162 |
}
|
sl@0
|
163 |
|
sl@0
|
164 |
#define NUM_SIZES 5
|
sl@0
|
165 |
static int sizes[NUM_SIZES]={128,256,512,1024,2048};
|
sl@0
|
166 |
/*static int sizes[NUM_SIZES]={59,179,299,419,539}; */
|
sl@0
|
167 |
|
sl@0
|
168 |
void do_mul(BIGNUM *r,BIGNUM *a,BIGNUM *b,BN_CTX *ctx);
|
sl@0
|
169 |
|
sl@0
|
170 |
int main(int argc, char **argv)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
BN_CTX *ctx;
|
sl@0
|
173 |
BIGNUM a,b,c;
|
sl@0
|
174 |
|
sl@0
|
175 |
ctx=BN_CTX_new();
|
sl@0
|
176 |
BN_init(&a);
|
sl@0
|
177 |
BN_init(&b);
|
sl@0
|
178 |
BN_init(&c);
|
sl@0
|
179 |
|
sl@0
|
180 |
do_mul(&a,&b,&c,ctx);
|
sl@0
|
181 |
}
|
sl@0
|
182 |
|
sl@0
|
183 |
void do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
|
sl@0
|
184 |
{
|
sl@0
|
185 |
int i,j,k;
|
sl@0
|
186 |
double tm;
|
sl@0
|
187 |
long num;
|
sl@0
|
188 |
|
sl@0
|
189 |
for (i=0; i<NUM_SIZES; i++)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
num=BASENUM;
|
sl@0
|
192 |
if (i) num/=(i*3);
|
sl@0
|
193 |
BN_rand(a,sizes[i],1,0);
|
sl@0
|
194 |
for (j=i; j<NUM_SIZES; j++)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
BN_rand(b,sizes[j],1,0);
|
sl@0
|
197 |
Time_F(START);
|
sl@0
|
198 |
for (k=0; k<num; k++)
|
sl@0
|
199 |
BN_mul(r,b,a,ctx);
|
sl@0
|
200 |
tm=Time_F(STOP);
|
sl@0
|
201 |
printf("mul %4d x %4d -> %8.3fms\n",sizes[i],sizes[j],tm*1000.0/num);
|
sl@0
|
202 |
}
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
for (i=0; i<NUM_SIZES; i++)
|
sl@0
|
206 |
{
|
sl@0
|
207 |
num=BASENUM;
|
sl@0
|
208 |
if (i) num/=(i*3);
|
sl@0
|
209 |
BN_rand(a,sizes[i],1,0);
|
sl@0
|
210 |
Time_F(START);
|
sl@0
|
211 |
for (k=0; k<num; k++)
|
sl@0
|
212 |
BN_sqr(r,a,ctx);
|
sl@0
|
213 |
tm=Time_F(STOP);
|
sl@0
|
214 |
printf("sqr %4d x %4d -> %8.3fms\n",sizes[i],sizes[i],tm*1000.0/num);
|
sl@0
|
215 |
}
|
sl@0
|
216 |
|
sl@0
|
217 |
for (i=0; i<NUM_SIZES; i++)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
num=BASENUM/10;
|
sl@0
|
220 |
if (i) num/=(i*3);
|
sl@0
|
221 |
BN_rand(a,sizes[i]-1,1,0);
|
sl@0
|
222 |
for (j=i; j<NUM_SIZES; j++)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
BN_rand(b,sizes[j],1,0);
|
sl@0
|
225 |
Time_F(START);
|
sl@0
|
226 |
for (k=0; k<100000; k++)
|
sl@0
|
227 |
BN_div(r, NULL, b, a,ctx);
|
sl@0
|
228 |
tm=Time_F(STOP);
|
sl@0
|
229 |
printf("div %4d / %4d -> %8.3fms\n",sizes[j],sizes[i]-1,tm*1000.0/num);
|
sl@0
|
230 |
}
|
sl@0
|
231 |
}
|
sl@0
|
232 |
}
|
sl@0
|
233 |
|