sl@0
|
1 |
/* crypto/bn/exptest.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
/*
|
sl@0
|
59 |
© Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
#include <stdio.h>
|
sl@0
|
62 |
#include <stdlib.h>
|
sl@0
|
63 |
#include <string.h>
|
sl@0
|
64 |
#ifndef SYMBIAN
|
sl@0
|
65 |
#include "../e_os.h"
|
sl@0
|
66 |
#else
|
sl@0
|
67 |
#include "e_os.h"
|
sl@0
|
68 |
#endif
|
sl@0
|
69 |
|
sl@0
|
70 |
#include <openssl/bio.h>
|
sl@0
|
71 |
#include <openssl/bn.h>
|
sl@0
|
72 |
#include <openssl/rand.h>
|
sl@0
|
73 |
#include <openssl/err.h>
|
sl@0
|
74 |
#ifdef SYMBIAN
|
sl@0
|
75 |
#ifdef stdin
|
sl@0
|
76 |
#undef stdin
|
sl@0
|
77 |
#endif
|
sl@0
|
78 |
#ifdef stdout
|
sl@0
|
79 |
#undef stdout
|
sl@0
|
80 |
#endif
|
sl@0
|
81 |
#ifdef stderr
|
sl@0
|
82 |
#undef stderr
|
sl@0
|
83 |
#endif
|
sl@0
|
84 |
|
sl@0
|
85 |
#define stdin fp_stdin
|
sl@0
|
86 |
#define stdout fp_stdout
|
sl@0
|
87 |
#define stderr fp_stderr
|
sl@0
|
88 |
|
sl@0
|
89 |
extern FILE *fp_stdout;
|
sl@0
|
90 |
extern FILE *fp_stderr;
|
sl@0
|
91 |
#endif
|
sl@0
|
92 |
|
sl@0
|
93 |
|
sl@0
|
94 |
#define NUM_BITS (BN_BITS*2)
|
sl@0
|
95 |
|
sl@0
|
96 |
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
|
sl@0
|
97 |
#ifndef SYMBIAN
|
sl@0
|
98 |
int main(int argc, char *argv[])
|
sl@0
|
99 |
#else
|
sl@0
|
100 |
int exp_main(int argc, char *argv[])
|
sl@0
|
101 |
#endif
|
sl@0
|
102 |
{
|
sl@0
|
103 |
BN_CTX *ctx;
|
sl@0
|
104 |
BIO *out=NULL;
|
sl@0
|
105 |
int i,ret;
|
sl@0
|
106 |
unsigned char c;
|
sl@0
|
107 |
BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
|
sl@0
|
108 |
|
sl@0
|
109 |
|
sl@0
|
110 |
RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
|
sl@0
|
111 |
* even check its return value
|
sl@0
|
112 |
* (which we should) */
|
sl@0
|
113 |
if(errno==ENOMEM)
|
sl@0
|
114 |
{
|
sl@0
|
115 |
return 1;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
ERR_load_BN_strings();
|
sl@0
|
119 |
if(errno==ENOMEM)
|
sl@0
|
120 |
{
|
sl@0
|
121 |
return 1;
|
sl@0
|
122 |
}
|
sl@0
|
123 |
|
sl@0
|
124 |
ctx=BN_CTX_new();
|
sl@0
|
125 |
if (ctx == NULL)
|
sl@0
|
126 |
{
|
sl@0
|
127 |
if(errno==ENOMEM)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
return 1;
|
sl@0
|
130 |
}
|
sl@0
|
131 |
return 1;
|
sl@0
|
132 |
}
|
sl@0
|
133 |
r_mont=BN_new();
|
sl@0
|
134 |
if(r_mont==NULL&&errno==ENOMEM)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
return 1;
|
sl@0
|
137 |
}
|
sl@0
|
138 |
r_mont_const=BN_new();
|
sl@0
|
139 |
if(r_mont_const==NULL&&errno==ENOMEM)
|
sl@0
|
140 |
{
|
sl@0
|
141 |
return 1;
|
sl@0
|
142 |
}
|
sl@0
|
143 |
|
sl@0
|
144 |
r_recp=BN_new();
|
sl@0
|
145 |
if(r_recp==NULL&&errno==ENOMEM)
|
sl@0
|
146 |
{
|
sl@0
|
147 |
return 1;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
r_simple=BN_new();
|
sl@0
|
151 |
if(r_simple==NULL&&errno==ENOMEM)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
return 1;
|
sl@0
|
154 |
}
|
sl@0
|
155 |
|
sl@0
|
156 |
a=BN_new();
|
sl@0
|
157 |
if(a==NULL&&errno==ENOMEM)
|
sl@0
|
158 |
{
|
sl@0
|
159 |
return 1;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
b=BN_new();
|
sl@0
|
163 |
if(b==NULL&&errno==ENOMEM)
|
sl@0
|
164 |
{
|
sl@0
|
165 |
return 1;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
m=BN_new();
|
sl@0
|
169 |
if(m==NULL&&errno==ENOMEM)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
return 1;
|
sl@0
|
172 |
}
|
sl@0
|
173 |
|
sl@0
|
174 |
if ( (r_mont == NULL) || (r_recp == NULL) ||
|
sl@0
|
175 |
(a == NULL) || (b == NULL))
|
sl@0
|
176 |
goto err;
|
sl@0
|
177 |
|
sl@0
|
178 |
out=BIO_new(BIO_s_file());
|
sl@0
|
179 |
if(out==NULL&&errno==ENOMEM)
|
sl@0
|
180 |
{
|
sl@0
|
181 |
return 1;
|
sl@0
|
182 |
}
|
sl@0
|
183 |
if (out == NULL)
|
sl@0
|
184 |
return 1;
|
sl@0
|
185 |
BIO_set_fp(out,stdout,BIO_NOCLOSE);
|
sl@0
|
186 |
if(errno==ENOMEM)
|
sl@0
|
187 |
{
|
sl@0
|
188 |
return 1;
|
sl@0
|
189 |
}
|
sl@0
|
190 |
|
sl@0
|
191 |
for (i=0; i<200; i++)
|
sl@0
|
192 |
{
|
sl@0
|
193 |
RAND_bytes(&c,1);
|
sl@0
|
194 |
if(errno==ENOMEM)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
return 1;
|
sl@0
|
197 |
}
|
sl@0
|
198 |
|
sl@0
|
199 |
c=(c%BN_BITS)-BN_BITS2;
|
sl@0
|
200 |
BN_rand(a,NUM_BITS+c,0,0);
|
sl@0
|
201 |
if(errno==ENOMEM)
|
sl@0
|
202 |
{
|
sl@0
|
203 |
return 1;
|
sl@0
|
204 |
}
|
sl@0
|
205 |
RAND_bytes(&c,1);
|
sl@0
|
206 |
if(errno==ENOMEM)
|
sl@0
|
207 |
{
|
sl@0
|
208 |
return 1;
|
sl@0
|
209 |
}
|
sl@0
|
210 |
c=(c%BN_BITS)-BN_BITS2;
|
sl@0
|
211 |
BN_rand(b,NUM_BITS+c,0,0);
|
sl@0
|
212 |
if(errno==ENOMEM)
|
sl@0
|
213 |
{
|
sl@0
|
214 |
return 1;
|
sl@0
|
215 |
}
|
sl@0
|
216 |
RAND_bytes(&c,1);
|
sl@0
|
217 |
if(errno==ENOMEM)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
return 1;
|
sl@0
|
220 |
}
|
sl@0
|
221 |
|
sl@0
|
222 |
c=(c%BN_BITS)-BN_BITS2;
|
sl@0
|
223 |
BN_rand(m,NUM_BITS+c,0,1);
|
sl@0
|
224 |
if(errno==ENOMEM)
|
sl@0
|
225 |
{
|
sl@0
|
226 |
return 1;
|
sl@0
|
227 |
}
|
sl@0
|
228 |
|
sl@0
|
229 |
BN_mod(a,a,m,ctx);
|
sl@0
|
230 |
if(errno==ENOMEM)
|
sl@0
|
231 |
{
|
sl@0
|
232 |
return 1;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
BN_mod(b,b,m,ctx);
|
sl@0
|
236 |
if(errno==ENOMEM)
|
sl@0
|
237 |
{
|
sl@0
|
238 |
return 1;
|
sl@0
|
239 |
}
|
sl@0
|
240 |
|
sl@0
|
241 |
ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
|
sl@0
|
242 |
if (ret <= 0)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
if(errno==ENOMEM)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
return 1;
|
sl@0
|
247 |
}
|
sl@0
|
248 |
fprintf(stdout,"BN_mod_exp_mont() problems\n");
|
sl@0
|
249 |
ERR_print_errors(out);
|
sl@0
|
250 |
if(errno==ENOMEM)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
return 1;
|
sl@0
|
253 |
}
|
sl@0
|
254 |
return 1;
|
sl@0
|
255 |
}
|
sl@0
|
256 |
|
sl@0
|
257 |
ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
|
sl@0
|
258 |
if (ret <= 0)
|
sl@0
|
259 |
{
|
sl@0
|
260 |
if(errno==ENOMEM)
|
sl@0
|
261 |
{
|
sl@0
|
262 |
return 1;
|
sl@0
|
263 |
}
|
sl@0
|
264 |
fprintf(stdout,"BN_mod_exp_recp() problems\n");
|
sl@0
|
265 |
ERR_print_errors(out);
|
sl@0
|
266 |
if(errno==ENOMEM)
|
sl@0
|
267 |
{
|
sl@0
|
268 |
return 1;
|
sl@0
|
269 |
}
|
sl@0
|
270 |
return 1;
|
sl@0
|
271 |
}
|
sl@0
|
272 |
|
sl@0
|
273 |
ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
|
sl@0
|
274 |
if (ret <= 0)
|
sl@0
|
275 |
{
|
sl@0
|
276 |
if(errno==ENOMEM)
|
sl@0
|
277 |
{
|
sl@0
|
278 |
return 1;
|
sl@0
|
279 |
}
|
sl@0
|
280 |
fprintf(stdout,"BN_mod_exp_simple() problems\n");
|
sl@0
|
281 |
ERR_print_errors(out);
|
sl@0
|
282 |
if(errno==ENOMEM)
|
sl@0
|
283 |
{
|
sl@0
|
284 |
return 1;
|
sl@0
|
285 |
}
|
sl@0
|
286 |
return 1;
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
|
sl@0
|
290 |
if (ret <= 0)
|
sl@0
|
291 |
{
|
sl@0
|
292 |
if(errno==ENOMEM)
|
sl@0
|
293 |
{
|
sl@0
|
294 |
return 1;
|
sl@0
|
295 |
}
|
sl@0
|
296 |
fprintf(stdout,"BN_mod_exp_mont_consttime() problems\n");
|
sl@0
|
297 |
ERR_print_errors(out);
|
sl@0
|
298 |
if(errno==ENOMEM)
|
sl@0
|
299 |
{
|
sl@0
|
300 |
return 1;
|
sl@0
|
301 |
}
|
sl@0
|
302 |
return 1;
|
sl@0
|
303 |
}
|
sl@0
|
304 |
|
sl@0
|
305 |
if (BN_cmp(r_simple, r_mont) == 0
|
sl@0
|
306 |
&& BN_cmp(r_simple,r_recp) == 0
|
sl@0
|
307 |
&& BN_cmp(r_simple,r_mont_const) == 0)
|
sl@0
|
308 |
{
|
sl@0
|
309 |
if(errno==ENOMEM)
|
sl@0
|
310 |
{
|
sl@0
|
311 |
return 1;
|
sl@0
|
312 |
}
|
sl@0
|
313 |
fprintf(stdout,".");
|
sl@0
|
314 |
fflush(stdout);
|
sl@0
|
315 |
}
|
sl@0
|
316 |
else
|
sl@0
|
317 |
{
|
sl@0
|
318 |
if (BN_cmp(r_simple,r_mont) != 0)
|
sl@0
|
319 |
{
|
sl@0
|
320 |
if(errno==ENOMEM)
|
sl@0
|
321 |
{
|
sl@0
|
322 |
return 1;
|
sl@0
|
323 |
}
|
sl@0
|
324 |
fprintf(stdout,"\nsimple and mont results differ\n");
|
sl@0
|
325 |
}
|
sl@0
|
326 |
if (BN_cmp(r_simple,r_mont) != 0)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
if(errno==ENOMEM)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
return 1;
|
sl@0
|
331 |
}
|
sl@0
|
332 |
fprintf(stdout,"\nsimple and mont const time results differ\n");
|
sl@0
|
333 |
}
|
sl@0
|
334 |
if (BN_cmp(r_simple,r_recp) != 0)
|
sl@0
|
335 |
{
|
sl@0
|
336 |
if(errno==ENOMEM)
|
sl@0
|
337 |
{
|
sl@0
|
338 |
return 1;
|
sl@0
|
339 |
}
|
sl@0
|
340 |
fprintf(stdout,"\nsimple and recp results differ\n");
|
sl@0
|
341 |
}
|
sl@0
|
342 |
fprintf(stdout,"a (%3d) = ",BN_num_bits(a)); BN_print(out,a);
|
sl@0
|
343 |
if(errno==ENOMEM)
|
sl@0
|
344 |
{
|
sl@0
|
345 |
return 1;
|
sl@0
|
346 |
}
|
sl@0
|
347 |
fprintf(stdout,"\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
|
sl@0
|
348 |
if(errno==ENOMEM)
|
sl@0
|
349 |
{
|
sl@0
|
350 |
return 1;
|
sl@0
|
351 |
}
|
sl@0
|
352 |
fprintf(stdout,"\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
|
sl@0
|
353 |
if(errno==ENOMEM)
|
sl@0
|
354 |
{
|
sl@0
|
355 |
return 1;
|
sl@0
|
356 |
}
|
sl@0
|
357 |
fprintf(stdout,"\nsimple ="); BN_print(out,r_simple);
|
sl@0
|
358 |
if(errno==ENOMEM)
|
sl@0
|
359 |
{
|
sl@0
|
360 |
return 1;
|
sl@0
|
361 |
}
|
sl@0
|
362 |
fprintf(stdout,"\nrecp ="); BN_print(out,r_recp);
|
sl@0
|
363 |
if(errno==ENOMEM)
|
sl@0
|
364 |
{
|
sl@0
|
365 |
return 1;
|
sl@0
|
366 |
}
|
sl@0
|
367 |
fprintf(stdout,"\nmont ="); BN_print(out,r_mont);
|
sl@0
|
368 |
if(errno==ENOMEM)
|
sl@0
|
369 |
{
|
sl@0
|
370 |
return 1;
|
sl@0
|
371 |
}
|
sl@0
|
372 |
fprintf(stdout,"\nmont_ct ="); BN_print(out,r_mont_const);
|
sl@0
|
373 |
if(errno==ENOMEM)
|
sl@0
|
374 |
{
|
sl@0
|
375 |
return 1;
|
sl@0
|
376 |
}
|
sl@0
|
377 |
fprintf(stdout,"\n");
|
sl@0
|
378 |
return 1;
|
sl@0
|
379 |
}
|
sl@0
|
380 |
}
|
sl@0
|
381 |
BN_free(r_mont);
|
sl@0
|
382 |
BN_free(r_mont_const);
|
sl@0
|
383 |
BN_free(r_recp);
|
sl@0
|
384 |
BN_free(r_simple);
|
sl@0
|
385 |
BN_free(a);
|
sl@0
|
386 |
BN_free(b);
|
sl@0
|
387 |
BN_free(m);
|
sl@0
|
388 |
BN_CTX_free(ctx);
|
sl@0
|
389 |
ERR_remove_state(0);
|
sl@0
|
390 |
if(errno==ENOMEM)
|
sl@0
|
391 |
{
|
sl@0
|
392 |
return 1;
|
sl@0
|
393 |
}
|
sl@0
|
394 |
CRYPTO_mem_leaks(out);
|
sl@0
|
395 |
if(errno==ENOMEM)
|
sl@0
|
396 |
{
|
sl@0
|
397 |
return 1;
|
sl@0
|
398 |
}
|
sl@0
|
399 |
BIO_free(out);
|
sl@0
|
400 |
if(errno==ENOMEM)
|
sl@0
|
401 |
{
|
sl@0
|
402 |
return 1;
|
sl@0
|
403 |
}
|
sl@0
|
404 |
|
sl@0
|
405 |
CRYPTO_cleanup_all_ex_data();
|
sl@0
|
406 |
if(errno==ENOMEM)
|
sl@0
|
407 |
{
|
sl@0
|
408 |
return 1;
|
sl@0
|
409 |
}
|
sl@0
|
410 |
|
sl@0
|
411 |
fprintf(stdout," done\n");
|
sl@0
|
412 |
fprintf(stdout," Test case passed\n");
|
sl@0
|
413 |
return 0;
|
sl@0
|
414 |
err:
|
sl@0
|
415 |
ERR_load_crypto_strings();
|
sl@0
|
416 |
if(errno==ENOMEM)
|
sl@0
|
417 |
{
|
sl@0
|
418 |
return 1;
|
sl@0
|
419 |
}
|
sl@0
|
420 |
|
sl@0
|
421 |
ERR_print_errors(out);
|
sl@0
|
422 |
if(errno==ENOMEM)
|
sl@0
|
423 |
{
|
sl@0
|
424 |
return 1;
|
sl@0
|
425 |
}
|
sl@0
|
426 |
|
sl@0
|
427 |
|
sl@0
|
428 |
#ifdef OPENSSL_SYS_NETWARE
|
sl@0
|
429 |
fprintf(stdout,"ERROR\n");
|
sl@0
|
430 |
#endif
|
sl@0
|
431 |
return(1);
|
sl@0
|
432 |
}
|
sl@0
|
433 |
|