]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* crypto/bn/bntest.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
3 | * All rights reserved. |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
8 | * | |
9 | * This library is free for commercial and non-commercial use as long as | |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
15 | * | |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
22 | * | |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
38 | * the apps directory (application code) you must include an acknowledgement: | |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
52 | * | |
53 | * The licence and distribution terms for any publically available version or | |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <string.h> | |
17e3dd1c | 62 | |
41d2a336 | 63 | #include "e_os.h" |
17e3dd1c | 64 | |
ec577822 BM |
65 | #include <openssl/bio.h> |
66 | #include <openssl/bn.h> | |
67 | #include <openssl/rand.h> | |
68 | #include <openssl/x509.h> | |
69 | #include <openssl/err.h> | |
d02b48c6 | 70 | |
bc36ee62 | 71 | #ifdef OPENSSL_SYS_WINDOWS |
58964a49 RE |
72 | #include "../bio/bss_file.c" |
73 | #endif | |
74 | ||
f07fb9b2 DSH |
75 | const int num0 = 100; /* number of tests */ |
76 | const int num1 = 50; /* additional tests for some functions */ | |
77 | const int num2 = 5; /* number of tests for slow functions */ | |
cae55bfc | 78 | |
dfeab068 RE |
79 | int test_add(BIO *bp); |
80 | int test_sub(BIO *bp); | |
81 | int test_lshift1(BIO *bp); | |
4da88a8d | 82 | int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_); |
dfeab068 | 83 | int test_rshift1(BIO *bp); |
62bad771 | 84 | int test_rshift(BIO *bp,BN_CTX *ctx); |
dfeab068 RE |
85 | int test_div(BIO *bp,BN_CTX *ctx); |
86 | int test_div_recp(BIO *bp,BN_CTX *ctx); | |
87 | int test_mul(BIO *bp); | |
88 | int test_sqr(BIO *bp,BN_CTX *ctx); | |
89 | int test_mont(BIO *bp,BN_CTX *ctx); | |
90 | int test_mod(BIO *bp,BN_CTX *ctx); | |
91 | int test_mod_mul(BIO *bp,BN_CTX *ctx); | |
92 | int test_mod_exp(BIO *bp,BN_CTX *ctx); | |
a0a54079 | 93 | int test_exp(BIO *bp,BN_CTX *ctx); |
c7820896 | 94 | int test_kron(BIO *bp,BN_CTX *ctx); |
cd2eebfd | 95 | int test_sqrt(BIO *bp,BN_CTX *ctx); |
d02b48c6 | 96 | int rand_neg(void); |
d02b48c6 RE |
97 | static int results=0; |
98 | ||
cf1b7d96 | 99 | #ifdef OPENSSL_NO_STDIO |
d02b48c6 | 100 | #define APPS_WIN16 |
58964a49 | 101 | #include "bss_file.c" |
d02b48c6 RE |
102 | #endif |
103 | ||
b25c8db8 | 104 | static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9" |
4da88a8d BL |
105 | "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0"; |
106 | ||
0c50e02b BM |
107 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; |
108 | ||
667ac4ec | 109 | static void message(BIO *out, char *m) |
cae55bfc UM |
110 | { |
111 | fprintf(stderr, "test %s\n", m); | |
cae55bfc UM |
112 | BIO_puts(out, "print \"test "); |
113 | BIO_puts(out, m); | |
114 | BIO_puts(out, "\\n\"\n"); | |
cae55bfc UM |
115 | } |
116 | ||
6b691a5c | 117 | int main(int argc, char *argv[]) |
d02b48c6 RE |
118 | { |
119 | BN_CTX *ctx; | |
120 | BIO *out; | |
121 | char *outfile=NULL; | |
122 | ||
79875776 BM |
123 | results = 0; |
124 | ||
111482cf | 125 | RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */ |
0c50e02b | 126 | |
d02b48c6 RE |
127 | argc--; |
128 | argv++; | |
129 | while (argc >= 1) | |
130 | { | |
131 | if (strcmp(*argv,"-results") == 0) | |
132 | results=1; | |
133 | else if (strcmp(*argv,"-out") == 0) | |
134 | { | |
135 | if (--argc < 1) break; | |
136 | outfile= *(++argv); | |
137 | } | |
138 | argc--; | |
139 | argv++; | |
140 | } | |
141 | ||
142 | ||
143 | ctx=BN_CTX_new(); | |
144 | if (ctx == NULL) exit(1); | |
145 | ||
146 | out=BIO_new(BIO_s_file()); | |
147 | if (out == NULL) exit(1); | |
148 | if (outfile == NULL) | |
149 | { | |
150 | BIO_set_fp(out,stdout,BIO_NOCLOSE); | |
151 | } | |
152 | else | |
153 | { | |
154 | if (!BIO_write_filename(out,outfile)) | |
155 | { | |
156 | perror(outfile); | |
157 | exit(1); | |
158 | } | |
159 | } | |
160 | ||
161 | if (!results) | |
162 | BIO_puts(out,"obase=16\nibase=16\n"); | |
163 | ||
cae55bfc | 164 | message(out,"BN_add"); |
d02b48c6 | 165 | if (!test_add(out)) goto err; |
f36e02b2 | 166 | BIO_flush(out); |
d02b48c6 | 167 | |
cae55bfc | 168 | message(out,"BN_sub"); |
d02b48c6 | 169 | if (!test_sub(out)) goto err; |
f36e02b2 | 170 | BIO_flush(out); |
d02b48c6 | 171 | |
cae55bfc | 172 | message(out,"BN_lshift1"); |
d02b48c6 | 173 | if (!test_lshift1(out)) goto err; |
f36e02b2 | 174 | BIO_flush(out); |
d02b48c6 | 175 | |
cae55bfc | 176 | message(out,"BN_lshift (fixed)"); |
b25c8db8 | 177 | if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL))) |
4da88a8d | 178 | goto err; |
f36e02b2 | 179 | BIO_flush(out); |
4da88a8d | 180 | |
cae55bfc | 181 | message(out,"BN_lshift"); |
4da88a8d | 182 | if (!test_lshift(out,ctx,NULL)) goto err; |
f36e02b2 | 183 | BIO_flush(out); |
d02b48c6 | 184 | |
cae55bfc | 185 | message(out,"BN_rshift1"); |
d02b48c6 | 186 | if (!test_rshift1(out)) goto err; |
f36e02b2 | 187 | BIO_flush(out); |
d02b48c6 | 188 | |
cae55bfc | 189 | message(out,"BN_rshift"); |
62bad771 | 190 | if (!test_rshift(out,ctx)) goto err; |
f36e02b2 | 191 | BIO_flush(out); |
d02b48c6 | 192 | |
cae55bfc | 193 | message(out,"BN_sqr"); |
58964a49 | 194 | if (!test_sqr(out,ctx)) goto err; |
f36e02b2 | 195 | BIO_flush(out); |
d02b48c6 | 196 | |
cae55bfc | 197 | message(out,"BN_mul"); |
d02b48c6 | 198 | if (!test_mul(out)) goto err; |
f36e02b2 | 199 | BIO_flush(out); |
d02b48c6 | 200 | |
cae55bfc | 201 | message(out,"BN_div"); |
58964a49 | 202 | if (!test_div(out,ctx)) goto err; |
f36e02b2 | 203 | BIO_flush(out); |
58964a49 | 204 | |
cae55bfc | 205 | message(out,"BN_div_recp"); |
dfeab068 | 206 | if (!test_div_recp(out,ctx)) goto err; |
f36e02b2 | 207 | BIO_flush(out); |
dfeab068 | 208 | |
cae55bfc | 209 | message(out,"BN_mod"); |
58964a49 | 210 | if (!test_mod(out,ctx)) goto err; |
f36e02b2 | 211 | BIO_flush(out); |
d02b48c6 | 212 | |
cae55bfc | 213 | message(out,"BN_mod_mul"); |
d02b48c6 | 214 | if (!test_mod_mul(out,ctx)) goto err; |
f36e02b2 | 215 | BIO_flush(out); |
d02b48c6 | 216 | |
cae55bfc | 217 | message(out,"BN_mont"); |
d02b48c6 | 218 | if (!test_mont(out,ctx)) goto err; |
f36e02b2 | 219 | BIO_flush(out); |
6e0cad8d | 220 | |
cae55bfc | 221 | message(out,"BN_mod_exp"); |
d02b48c6 | 222 | if (!test_mod_exp(out,ctx)) goto err; |
f36e02b2 | 223 | BIO_flush(out); |
d02b48c6 | 224 | |
cae55bfc | 225 | message(out,"BN_exp"); |
a0a54079 | 226 | if (!test_exp(out,ctx)) goto err; |
f36e02b2 | 227 | BIO_flush(out); |
a0a54079 | 228 | |
c7820896 BM |
229 | message(out,"BN_kronecker"); |
230 | if (!test_kron(out,ctx)) goto err; | |
231 | BIO_flush(out); | |
232 | ||
cd2eebfd BM |
233 | message(out,"BN_mod_sqrt"); |
234 | if (!test_sqrt(out,ctx)) goto err; | |
235 | BIO_flush(out); | |
236 | ||
79875776 BM |
237 | BN_CTX_free(ctx); |
238 | BIO_free(out); | |
239 | ||
d02b48c6 RE |
240 | /**/ |
241 | exit(0); | |
242 | err: | |
df83eeb7 BM |
243 | BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices |
244 | * the failure, see test_bn in test/Makefile.ssl*/ | |
f36e02b2 | 245 | BIO_flush(out); |
d02b48c6 | 246 | ERR_load_crypto_strings(); |
cae55bfc | 247 | ERR_print_errors_fp(stderr); |
d02b48c6 RE |
248 | exit(1); |
249 | return(1); | |
250 | } | |
251 | ||
6b691a5c | 252 | int test_add(BIO *bp) |
d02b48c6 | 253 | { |
dfeab068 | 254 | BIGNUM a,b,c; |
d02b48c6 | 255 | int i; |
d02b48c6 | 256 | |
dfeab068 RE |
257 | BN_init(&a); |
258 | BN_init(&b); | |
259 | BN_init(&c); | |
d02b48c6 | 260 | |
111482cf | 261 | BN_bntest_rand(&a,512,0,0); |
cae55bfc | 262 | for (i=0; i<num0; i++) |
d02b48c6 | 263 | { |
111482cf | 264 | BN_bntest_rand(&b,450+i,0,0); |
dfeab068 RE |
265 | a.neg=rand_neg(); |
266 | b.neg=rand_neg(); | |
dfeab068 | 267 | BN_add(&c,&a,&b); |
d02b48c6 RE |
268 | if (bp != NULL) |
269 | { | |
270 | if (!results) | |
271 | { | |
dfeab068 | 272 | BN_print(bp,&a); |
d02b48c6 | 273 | BIO_puts(bp," + "); |
dfeab068 | 274 | BN_print(bp,&b); |
d02b48c6 RE |
275 | BIO_puts(bp," - "); |
276 | } | |
dfeab068 | 277 | BN_print(bp,&c); |
d02b48c6 RE |
278 | BIO_puts(bp,"\n"); |
279 | } | |
62bad771 BL |
280 | a.neg=!a.neg; |
281 | b.neg=!b.neg; | |
282 | BN_add(&c,&c,&b); | |
283 | BN_add(&c,&c,&a); | |
284 | if(!BN_is_zero(&c)) | |
285 | { | |
cae55bfc | 286 | fprintf(stderr,"Add test failed!\n"); |
62bad771 BL |
287 | return 0; |
288 | } | |
d02b48c6 | 289 | } |
dfeab068 RE |
290 | BN_free(&a); |
291 | BN_free(&b); | |
292 | BN_free(&c); | |
d02b48c6 RE |
293 | return(1); |
294 | } | |
295 | ||
6b691a5c | 296 | int test_sub(BIO *bp) |
d02b48c6 | 297 | { |
dfeab068 | 298 | BIGNUM a,b,c; |
d02b48c6 | 299 | int i; |
d02b48c6 | 300 | |
dfeab068 RE |
301 | BN_init(&a); |
302 | BN_init(&b); | |
303 | BN_init(&c); | |
d02b48c6 | 304 | |
cae55bfc | 305 | for (i=0; i<num0+num1; i++) |
d02b48c6 | 306 | { |
cae55bfc UM |
307 | if (i < num1) |
308 | { | |
111482cf | 309 | BN_bntest_rand(&a,512,0,0); |
cae55bfc UM |
310 | BN_copy(&b,&a); |
311 | if (BN_set_bit(&a,i)==0) return(0); | |
312 | BN_add_word(&b,i); | |
313 | } | |
314 | else | |
315 | { | |
111482cf | 316 | BN_bntest_rand(&b,400+i-num1,0,0); |
cae55bfc UM |
317 | a.neg=rand_neg(); |
318 | b.neg=rand_neg(); | |
319 | } | |
dfeab068 | 320 | BN_sub(&c,&a,&b); |
d02b48c6 RE |
321 | if (bp != NULL) |
322 | { | |
323 | if (!results) | |
324 | { | |
dfeab068 | 325 | BN_print(bp,&a); |
d02b48c6 | 326 | BIO_puts(bp," - "); |
dfeab068 | 327 | BN_print(bp,&b); |
d02b48c6 RE |
328 | BIO_puts(bp," - "); |
329 | } | |
dfeab068 | 330 | BN_print(bp,&c); |
d02b48c6 RE |
331 | BIO_puts(bp,"\n"); |
332 | } | |
62bad771 BL |
333 | BN_add(&c,&c,&b); |
334 | BN_sub(&c,&c,&a); | |
335 | if(!BN_is_zero(&c)) | |
336 | { | |
cae55bfc | 337 | fprintf(stderr,"Subtract test failed!\n"); |
62bad771 BL |
338 | return 0; |
339 | } | |
d02b48c6 | 340 | } |
dfeab068 RE |
341 | BN_free(&a); |
342 | BN_free(&b); | |
343 | BN_free(&c); | |
d02b48c6 RE |
344 | return(1); |
345 | } | |
346 | ||
6b691a5c | 347 | int test_div(BIO *bp, BN_CTX *ctx) |
d02b48c6 | 348 | { |
62bad771 | 349 | BIGNUM a,b,c,d,e; |
d02b48c6 | 350 | int i; |
d02b48c6 | 351 | |
dfeab068 RE |
352 | BN_init(&a); |
353 | BN_init(&b); | |
354 | BN_init(&c); | |
355 | BN_init(&d); | |
62bad771 | 356 | BN_init(&e); |
d02b48c6 | 357 | |
cae55bfc | 358 | for (i=0; i<num0+num1; i++) |
d02b48c6 | 359 | { |
cae55bfc UM |
360 | if (i < num1) |
361 | { | |
111482cf | 362 | BN_bntest_rand(&a,400,0,0); |
cae55bfc UM |
363 | BN_copy(&b,&a); |
364 | BN_lshift(&a,&a,i); | |
365 | BN_add_word(&a,i); | |
366 | } | |
367 | else | |
111482cf | 368 | BN_bntest_rand(&b,50+3*(i-num1),0,0); |
dfeab068 RE |
369 | a.neg=rand_neg(); |
370 | b.neg=rand_neg(); | |
dfeab068 | 371 | BN_div(&d,&c,&a,&b,ctx); |
d02b48c6 RE |
372 | if (bp != NULL) |
373 | { | |
374 | if (!results) | |
375 | { | |
dfeab068 | 376 | BN_print(bp,&a); |
d02b48c6 | 377 | BIO_puts(bp," / "); |
dfeab068 | 378 | BN_print(bp,&b); |
d02b48c6 RE |
379 | BIO_puts(bp," - "); |
380 | } | |
dfeab068 | 381 | BN_print(bp,&d); |
d02b48c6 RE |
382 | BIO_puts(bp,"\n"); |
383 | ||
384 | if (!results) | |
385 | { | |
dfeab068 | 386 | BN_print(bp,&a); |
d02b48c6 | 387 | BIO_puts(bp," % "); |
dfeab068 | 388 | BN_print(bp,&b); |
d02b48c6 RE |
389 | BIO_puts(bp," - "); |
390 | } | |
dfeab068 | 391 | BN_print(bp,&c); |
d02b48c6 RE |
392 | BIO_puts(bp,"\n"); |
393 | } | |
62bad771 BL |
394 | BN_mul(&e,&d,&b,ctx); |
395 | BN_add(&d,&e,&c); | |
396 | BN_sub(&d,&d,&a); | |
397 | if(!BN_is_zero(&d)) | |
398 | { | |
cae55bfc | 399 | fprintf(stderr,"Division test failed!\n"); |
62bad771 BL |
400 | return 0; |
401 | } | |
d02b48c6 | 402 | } |
dfeab068 RE |
403 | BN_free(&a); |
404 | BN_free(&b); | |
405 | BN_free(&c); | |
406 | BN_free(&d); | |
62bad771 | 407 | BN_free(&e); |
dfeab068 RE |
408 | return(1); |
409 | } | |
410 | ||
6b691a5c | 411 | int test_div_recp(BIO *bp, BN_CTX *ctx) |
dfeab068 | 412 | { |
62bad771 | 413 | BIGNUM a,b,c,d,e; |
dfeab068 RE |
414 | BN_RECP_CTX recp; |
415 | int i; | |
dfeab068 RE |
416 | |
417 | BN_RECP_CTX_init(&recp); | |
418 | BN_init(&a); | |
419 | BN_init(&b); | |
420 | BN_init(&c); | |
421 | BN_init(&d); | |
62bad771 | 422 | BN_init(&e); |
dfeab068 | 423 | |
cae55bfc | 424 | for (i=0; i<num0+num1; i++) |
dfeab068 | 425 | { |
cae55bfc UM |
426 | if (i < num1) |
427 | { | |
111482cf | 428 | BN_bntest_rand(&a,400,0,0); |
cae55bfc UM |
429 | BN_copy(&b,&a); |
430 | BN_lshift(&a,&a,i); | |
431 | BN_add_word(&a,i); | |
432 | } | |
433 | else | |
111482cf | 434 | BN_bntest_rand(&b,50+3*(i-num1),0,0); |
dfeab068 RE |
435 | a.neg=rand_neg(); |
436 | b.neg=rand_neg(); | |
437 | BN_RECP_CTX_set(&recp,&b,ctx); | |
dfeab068 RE |
438 | BN_div_recp(&d,&c,&a,&recp,ctx); |
439 | if (bp != NULL) | |
440 | { | |
441 | if (!results) | |
442 | { | |
443 | BN_print(bp,&a); | |
444 | BIO_puts(bp," / "); | |
445 | BN_print(bp,&b); | |
446 | BIO_puts(bp," - "); | |
447 | } | |
448 | BN_print(bp,&d); | |
449 | BIO_puts(bp,"\n"); | |
450 | ||
451 | if (!results) | |
452 | { | |
453 | BN_print(bp,&a); | |
454 | BIO_puts(bp," % "); | |
455 | BN_print(bp,&b); | |
456 | BIO_puts(bp," - "); | |
457 | } | |
458 | BN_print(bp,&c); | |
459 | BIO_puts(bp,"\n"); | |
460 | } | |
62bad771 BL |
461 | BN_mul(&e,&d,&b,ctx); |
462 | BN_add(&d,&e,&c); | |
463 | BN_sub(&d,&d,&a); | |
464 | if(!BN_is_zero(&d)) | |
465 | { | |
cae55bfc UM |
466 | fprintf(stderr,"Reciprocal division test failed!\n"); |
467 | fprintf(stderr,"a="); | |
468 | BN_print_fp(stderr,&a); | |
469 | fprintf(stderr,"\nb="); | |
470 | BN_print_fp(stderr,&b); | |
471 | fprintf(stderr,"\n"); | |
62bad771 BL |
472 | return 0; |
473 | } | |
dfeab068 RE |
474 | } |
475 | BN_free(&a); | |
476 | BN_free(&b); | |
477 | BN_free(&c); | |
478 | BN_free(&d); | |
62bad771 | 479 | BN_free(&e); |
dfeab068 | 480 | BN_RECP_CTX_free(&recp); |
d02b48c6 RE |
481 | return(1); |
482 | } | |
483 | ||
6b691a5c | 484 | int test_mul(BIO *bp) |
d02b48c6 | 485 | { |
62bad771 | 486 | BIGNUM a,b,c,d,e; |
d02b48c6 | 487 | int i; |
c62b26fd | 488 | BN_CTX *ctx; |
d02b48c6 | 489 | |
c62b26fd BM |
490 | ctx = BN_CTX_new(); |
491 | if (ctx == NULL) exit(1); | |
492 | ||
dfeab068 RE |
493 | BN_init(&a); |
494 | BN_init(&b); | |
495 | BN_init(&c); | |
62bad771 BL |
496 | BN_init(&d); |
497 | BN_init(&e); | |
d02b48c6 | 498 | |
cae55bfc | 499 | for (i=0; i<num0+num1; i++) |
d02b48c6 | 500 | { |
587bb0e0 | 501 | if (i <= num1) |
cae55bfc | 502 | { |
111482cf UM |
503 | BN_bntest_rand(&a,100,0,0); |
504 | BN_bntest_rand(&b,100,0,0); | |
cae55bfc UM |
505 | } |
506 | else | |
111482cf | 507 | BN_bntest_rand(&b,i-num1,0,0); |
dfeab068 RE |
508 | a.neg=rand_neg(); |
509 | b.neg=rand_neg(); | |
c62b26fd | 510 | BN_mul(&c,&a,&b,ctx); |
d02b48c6 RE |
511 | if (bp != NULL) |
512 | { | |
513 | if (!results) | |
514 | { | |
dfeab068 | 515 | BN_print(bp,&a); |
d02b48c6 | 516 | BIO_puts(bp," * "); |
dfeab068 | 517 | BN_print(bp,&b); |
d02b48c6 RE |
518 | BIO_puts(bp," - "); |
519 | } | |
dfeab068 | 520 | BN_print(bp,&c); |
d02b48c6 RE |
521 | BIO_puts(bp,"\n"); |
522 | } | |
c62b26fd | 523 | BN_div(&d,&e,&c,&a,ctx); |
62bad771 BL |
524 | BN_sub(&d,&d,&b); |
525 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) | |
526 | { | |
cae55bfc | 527 | fprintf(stderr,"Multiplication test failed!\n"); |
62bad771 BL |
528 | return 0; |
529 | } | |
d02b48c6 | 530 | } |
dfeab068 RE |
531 | BN_free(&a); |
532 | BN_free(&b); | |
533 | BN_free(&c); | |
62bad771 BL |
534 | BN_free(&d); |
535 | BN_free(&e); | |
c62b26fd | 536 | BN_CTX_free(ctx); |
d02b48c6 RE |
537 | return(1); |
538 | } | |
539 | ||
6b691a5c | 540 | int test_sqr(BIO *bp, BN_CTX *ctx) |
d02b48c6 | 541 | { |
62bad771 | 542 | BIGNUM a,c,d,e; |
d02b48c6 | 543 | int i; |
d02b48c6 | 544 | |
dfeab068 RE |
545 | BN_init(&a); |
546 | BN_init(&c); | |
62bad771 BL |
547 | BN_init(&d); |
548 | BN_init(&e); | |
d02b48c6 | 549 | |
cae55bfc | 550 | for (i=0; i<num0; i++) |
d02b48c6 | 551 | { |
111482cf | 552 | BN_bntest_rand(&a,40+i*10,0,0); |
dfeab068 | 553 | a.neg=rand_neg(); |
dfeab068 | 554 | BN_sqr(&c,&a,ctx); |
d02b48c6 RE |
555 | if (bp != NULL) |
556 | { | |
557 | if (!results) | |
558 | { | |
dfeab068 | 559 | BN_print(bp,&a); |
d02b48c6 | 560 | BIO_puts(bp," * "); |
dfeab068 | 561 | BN_print(bp,&a); |
d02b48c6 RE |
562 | BIO_puts(bp," - "); |
563 | } | |
dfeab068 | 564 | BN_print(bp,&c); |
d02b48c6 RE |
565 | BIO_puts(bp,"\n"); |
566 | } | |
62bad771 BL |
567 | BN_div(&d,&e,&c,&a,ctx); |
568 | BN_sub(&d,&d,&a); | |
569 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) | |
570 | { | |
cae55bfc | 571 | fprintf(stderr,"Square test failed!\n"); |
62bad771 BL |
572 | return 0; |
573 | } | |
d02b48c6 | 574 | } |
dfeab068 RE |
575 | BN_free(&a); |
576 | BN_free(&c); | |
62bad771 BL |
577 | BN_free(&d); |
578 | BN_free(&e); | |
d02b48c6 RE |
579 | return(1); |
580 | } | |
581 | ||
6b691a5c | 582 | int test_mont(BIO *bp, BN_CTX *ctx) |
d02b48c6 | 583 | { |
62bad771 | 584 | BIGNUM a,b,c,d,A,B; |
dfeab068 | 585 | BIGNUM n; |
d02b48c6 | 586 | int i; |
d02b48c6 RE |
587 | BN_MONT_CTX *mont; |
588 | ||
dfeab068 RE |
589 | BN_init(&a); |
590 | BN_init(&b); | |
591 | BN_init(&c); | |
62bad771 | 592 | BN_init(&d); |
dfeab068 RE |
593 | BN_init(&A); |
594 | BN_init(&B); | |
595 | BN_init(&n); | |
d02b48c6 RE |
596 | |
597 | mont=BN_MONT_CTX_new(); | |
598 | ||
111482cf UM |
599 | BN_bntest_rand(&a,100,0,0); /**/ |
600 | BN_bntest_rand(&b,100,0,0); /**/ | |
cae55bfc | 601 | for (i=0; i<num2; i++) |
d02b48c6 | 602 | { |
ea96c4bc | 603 | int bits = (200*(i+1))/num2; |
6e0cad8d BM |
604 | |
605 | if (bits == 0) | |
606 | continue; | |
111482cf | 607 | BN_bntest_rand(&n,bits,0,1); |
dfeab068 | 608 | BN_MONT_CTX_set(mont,&n,ctx); |
d02b48c6 | 609 | |
14697d9d BM |
610 | BN_nnmod(&a,&a,&n,ctx); |
611 | BN_nnmod(&b,&b,&n,ctx); | |
612 | ||
dfeab068 RE |
613 | BN_to_montgomery(&A,&a,mont,ctx); |
614 | BN_to_montgomery(&B,&b,mont,ctx); | |
d02b48c6 | 615 | |
dfeab068 RE |
616 | BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/ |
617 | BN_from_montgomery(&A,&c,mont,ctx);/**/ | |
d02b48c6 RE |
618 | if (bp != NULL) |
619 | { | |
620 | if (!results) | |
621 | { | |
622 | #ifdef undef | |
623 | fprintf(stderr,"%d * %d %% %d\n", | |
dfeab068 RE |
624 | BN_num_bits(&a), |
625 | BN_num_bits(&b), | |
d02b48c6 RE |
626 | BN_num_bits(mont->N)); |
627 | #endif | |
dfeab068 | 628 | BN_print(bp,&a); |
d02b48c6 | 629 | BIO_puts(bp," * "); |
dfeab068 | 630 | BN_print(bp,&b); |
d02b48c6 | 631 | BIO_puts(bp," % "); |
dfeab068 | 632 | BN_print(bp,&(mont->N)); |
d02b48c6 RE |
633 | BIO_puts(bp," - "); |
634 | } | |
dfeab068 | 635 | BN_print(bp,&A); |
d02b48c6 RE |
636 | BIO_puts(bp,"\n"); |
637 | } | |
62bad771 BL |
638 | BN_mod_mul(&d,&a,&b,&n,ctx); |
639 | BN_sub(&d,&d,&A); | |
640 | if(!BN_is_zero(&d)) | |
641 | { | |
cae55bfc | 642 | fprintf(stderr,"Montgomery multiplication test failed!\n"); |
62bad771 BL |
643 | return 0; |
644 | } | |
d02b48c6 RE |
645 | } |
646 | BN_MONT_CTX_free(mont); | |
dfeab068 RE |
647 | BN_free(&a); |
648 | BN_free(&b); | |
649 | BN_free(&c); | |
62bad771 BL |
650 | BN_free(&d); |
651 | BN_free(&A); | |
652 | BN_free(&B); | |
653 | BN_free(&n); | |
d02b48c6 RE |
654 | return(1); |
655 | } | |
656 | ||
6b691a5c | 657 | int test_mod(BIO *bp, BN_CTX *ctx) |
d02b48c6 | 658 | { |
62bad771 | 659 | BIGNUM *a,*b,*c,*d,*e; |
d02b48c6 | 660 | int i; |
d02b48c6 RE |
661 | |
662 | a=BN_new(); | |
663 | b=BN_new(); | |
664 | c=BN_new(); | |
62bad771 BL |
665 | d=BN_new(); |
666 | e=BN_new(); | |
d02b48c6 | 667 | |
111482cf | 668 | BN_bntest_rand(a,1024,0,0); /**/ |
cae55bfc | 669 | for (i=0; i<num0; i++) |
d02b48c6 | 670 | { |
111482cf | 671 | BN_bntest_rand(b,450+i*10,0,0); /**/ |
d02b48c6 RE |
672 | a->neg=rand_neg(); |
673 | b->neg=rand_neg(); | |
d02b48c6 RE |
674 | BN_mod(c,a,b,ctx);/**/ |
675 | if (bp != NULL) | |
676 | { | |
677 | if (!results) | |
678 | { | |
679 | BN_print(bp,a); | |
680 | BIO_puts(bp," % "); | |
681 | BN_print(bp,b); | |
682 | BIO_puts(bp," - "); | |
683 | } | |
684 | BN_print(bp,c); | |
685 | BIO_puts(bp,"\n"); | |
686 | } | |
62bad771 BL |
687 | BN_div(d,e,a,b,ctx); |
688 | BN_sub(e,e,c); | |
689 | if(!BN_is_zero(e)) | |
690 | { | |
cae55bfc | 691 | fprintf(stderr,"Modulo test failed!\n"); |
62bad771 BL |
692 | return 0; |
693 | } | |
d02b48c6 RE |
694 | } |
695 | BN_free(a); | |
696 | BN_free(b); | |
697 | BN_free(c); | |
62bad771 BL |
698 | BN_free(d); |
699 | BN_free(e); | |
d02b48c6 RE |
700 | return(1); |
701 | } | |
702 | ||
6b691a5c | 703 | int test_mod_mul(BIO *bp, BN_CTX *ctx) |
d02b48c6 RE |
704 | { |
705 | BIGNUM *a,*b,*c,*d,*e; | |
aecb0b01 | 706 | int i,j; |
d02b48c6 RE |
707 | |
708 | a=BN_new(); | |
709 | b=BN_new(); | |
710 | c=BN_new(); | |
711 | d=BN_new(); | |
712 | e=BN_new(); | |
713 | ||
aecb0b01 | 714 | for (j=0; j<3; j++) { |
111482cf | 715 | BN_bntest_rand(c,1024,0,0); /**/ |
cae55bfc | 716 | for (i=0; i<num0; i++) |
d02b48c6 | 717 | { |
111482cf UM |
718 | BN_bntest_rand(a,475+i*10,0,0); /**/ |
719 | BN_bntest_rand(b,425+i*11,0,0); /**/ | |
d02b48c6 RE |
720 | a->neg=rand_neg(); |
721 | b->neg=rand_neg(); | |
d02b48c6 RE |
722 | if (!BN_mod_mul(e,a,b,c,ctx)) |
723 | { | |
724 | unsigned long l; | |
725 | ||
726 | while ((l=ERR_get_error())) | |
727 | fprintf(stderr,"ERROR:%s\n", | |
728 | ERR_error_string(l,NULL)); | |
729 | exit(1); | |
730 | } | |
731 | if (bp != NULL) | |
732 | { | |
733 | if (!results) | |
734 | { | |
735 | BN_print(bp,a); | |
736 | BIO_puts(bp," * "); | |
737 | BN_print(bp,b); | |
738 | BIO_puts(bp," % "); | |
739 | BN_print(bp,c); | |
5acaa495 BM |
740 | if ((a->neg ^ b->neg) && !BN_is_zero(e)) |
741 | { | |
742 | /* If (a*b) % c is negative, c must be added | |
743 | * in order to obtain the normalized remainder | |
744 | * (new with OpenSSL 0.9.7, previous versions of | |
745 | * BN_mod_mul could generate negative results) | |
746 | */ | |
747 | BIO_puts(bp," + "); | |
748 | BN_print(bp,c); | |
749 | } | |
d02b48c6 RE |
750 | BIO_puts(bp," - "); |
751 | } | |
752 | BN_print(bp,e); | |
753 | BIO_puts(bp,"\n"); | |
754 | } | |
62bad771 BL |
755 | BN_mul(d,a,b,ctx); |
756 | BN_sub(d,d,e); | |
757 | BN_div(a,b,d,c,ctx); | |
758 | if(!BN_is_zero(b)) | |
759 | { | |
cae55bfc | 760 | fprintf(stderr,"Modulo multiply test failed!\n"); |
020fc820 | 761 | ERR_print_errors_fp(stderr); |
62bad771 BL |
762 | return 0; |
763 | } | |
d02b48c6 | 764 | } |
aecb0b01 | 765 | } |
d02b48c6 RE |
766 | BN_free(a); |
767 | BN_free(b); | |
768 | BN_free(c); | |
769 | BN_free(d); | |
770 | BN_free(e); | |
771 | return(1); | |
772 | } | |
773 | ||
6b691a5c | 774 | int test_mod_exp(BIO *bp, BN_CTX *ctx) |
d02b48c6 RE |
775 | { |
776 | BIGNUM *a,*b,*c,*d,*e; | |
777 | int i; | |
778 | ||
779 | a=BN_new(); | |
780 | b=BN_new(); | |
781 | c=BN_new(); | |
782 | d=BN_new(); | |
783 | e=BN_new(); | |
784 | ||
111482cf | 785 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ |
cae55bfc | 786 | for (i=0; i<num2; i++) |
d02b48c6 | 787 | { |
111482cf UM |
788 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
789 | BN_bntest_rand(b,2+i,0,0); /**/ | |
d02b48c6 RE |
790 | |
791 | if (!BN_mod_exp(d,a,b,c,ctx)) | |
792 | return(00); | |
793 | ||
794 | if (bp != NULL) | |
795 | { | |
796 | if (!results) | |
797 | { | |
798 | BN_print(bp,a); | |
799 | BIO_puts(bp," ^ "); | |
800 | BN_print(bp,b); | |
801 | BIO_puts(bp," % "); | |
802 | BN_print(bp,c); | |
803 | BIO_puts(bp," - "); | |
804 | } | |
805 | BN_print(bp,d); | |
806 | BIO_puts(bp,"\n"); | |
807 | } | |
62bad771 BL |
808 | BN_exp(e,a,b,ctx); |
809 | BN_sub(e,e,d); | |
810 | BN_div(a,b,e,c,ctx); | |
811 | if(!BN_is_zero(b)) | |
812 | { | |
cae55bfc | 813 | fprintf(stderr,"Modulo exponentiation test failed!\n"); |
62bad771 BL |
814 | return 0; |
815 | } | |
d02b48c6 RE |
816 | } |
817 | BN_free(a); | |
818 | BN_free(b); | |
819 | BN_free(c); | |
820 | BN_free(d); | |
821 | BN_free(e); | |
822 | return(1); | |
823 | } | |
824 | ||
6b691a5c | 825 | int test_exp(BIO *bp, BN_CTX *ctx) |
a0a54079 | 826 | { |
62bad771 | 827 | BIGNUM *a,*b,*d,*e,*one; |
a0a54079 MC |
828 | int i; |
829 | ||
830 | a=BN_new(); | |
831 | b=BN_new(); | |
832 | d=BN_new(); | |
833 | e=BN_new(); | |
62bad771 BL |
834 | one=BN_new(); |
835 | BN_one(one); | |
a0a54079 | 836 | |
cae55bfc | 837 | for (i=0; i<num2; i++) |
a0a54079 | 838 | { |
111482cf UM |
839 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
840 | BN_bntest_rand(b,2+i,0,0); /**/ | |
a0a54079 MC |
841 | |
842 | if (!BN_exp(d,a,b,ctx)) | |
843 | return(00); | |
844 | ||
845 | if (bp != NULL) | |
846 | { | |
847 | if (!results) | |
848 | { | |
849 | BN_print(bp,a); | |
850 | BIO_puts(bp," ^ "); | |
851 | BN_print(bp,b); | |
852 | BIO_puts(bp," - "); | |
853 | } | |
854 | BN_print(bp,d); | |
855 | BIO_puts(bp,"\n"); | |
856 | } | |
62bad771 BL |
857 | BN_one(e); |
858 | for( ; !BN_is_zero(b) ; BN_sub(b,b,one)) | |
859 | BN_mul(e,e,a,ctx); | |
860 | BN_sub(e,e,d); | |
861 | if(!BN_is_zero(e)) | |
862 | { | |
cae55bfc | 863 | fprintf(stderr,"Exponentiation test failed!\n"); |
62bad771 BL |
864 | return 0; |
865 | } | |
a0a54079 MC |
866 | } |
867 | BN_free(a); | |
868 | BN_free(b); | |
869 | BN_free(d); | |
870 | BN_free(e); | |
62bad771 | 871 | BN_free(one); |
a0a54079 MC |
872 | return(1); |
873 | } | |
874 | ||
bdec3c53 BM |
875 | static void genprime_cb(int p, int n, void *arg) |
876 | { | |
877 | char c='*'; | |
878 | ||
879 | if (p == 0) c='.'; | |
880 | if (p == 1) c='+'; | |
881 | if (p == 2) c='*'; | |
882 | if (p == 3) c='\n'; | |
883 | putc(c, stderr); | |
884 | fflush(stderr); | |
885 | (void)n; | |
886 | (void)arg; | |
887 | } | |
888 | ||
c7820896 BM |
889 | int test_kron(BIO *bp, BN_CTX *ctx) |
890 | { | |
470fa98f | 891 | BIGNUM *a,*b,*r,*t; |
bdec3c53 BM |
892 | int i; |
893 | int legendre, kronecker; | |
894 | int ret = 0; | |
895 | ||
896 | a = BN_new(); | |
897 | b = BN_new(); | |
898 | r = BN_new(); | |
470fa98f BM |
899 | t = BN_new(); |
900 | if (a == NULL || b == NULL || r == NULL || t == NULL) goto err; | |
bdec3c53 | 901 | |
d79cab27 BM |
902 | /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). |
903 | * In this case we know that if b is prime, then BN_kronecker(a, b, ctx) | |
904 | * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). | |
905 | * So we generate a random prime b and compare these values | |
906 | * for a number of random a's. (That is, we run the Solovay-Strassen | |
907 | * primality test to confirm that b is prime, except that we | |
908 | * don't want to test whether b is prime but whether BN_kronecker | |
909 | * works.) */ | |
910 | ||
bdec3c53 | 911 | if (!BN_generate_prime(b, 512, 0, NULL, NULL, genprime_cb, NULL)) goto err; |
80d89e6a | 912 | b->neg = rand_neg(); |
bdec3c53 | 913 | putc('\n', stderr); |
d79cab27 | 914 | |
bdec3c53 BM |
915 | for (i = 0; i < num0; i++) |
916 | { | |
111482cf | 917 | if (!BN_bntest_rand(a, 512, 0, 0)) goto err; |
eb1f1b0a BM |
918 | a->neg = rand_neg(); |
919 | ||
80d89e6a | 920 | /* t := (|b|-1)/2 (note that b is odd) */ |
470fa98f | 921 | if (!BN_copy(t, b)) goto err; |
80d89e6a | 922 | t->neg = 0; |
470fa98f BM |
923 | if (!BN_sub_word(t, 1)) goto err; |
924 | if (!BN_rshift1(t, t)) goto err; | |
925 | /* r := a^t mod b */ | |
8dea52fa BM |
926 | b->neg=0; |
927 | ||
5c9396e3 | 928 | if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err; |
8dea52fa | 929 | b->neg=1; |
bdec3c53 BM |
930 | |
931 | if (BN_is_word(r, 1)) | |
932 | legendre = 1; | |
b2993bde BM |
933 | else if (BN_is_zero(r)) |
934 | legendre = 0; | |
bdec3c53 BM |
935 | else |
936 | { | |
937 | if (!BN_add_word(r, 1)) goto err; | |
8dea52fa | 938 | if (0 != BN_ucmp(r, b)) |
bdec3c53 BM |
939 | { |
940 | fprintf(stderr, "Legendre symbol computation failed\n"); | |
941 | goto err; | |
942 | } | |
943 | legendre = -1; | |
944 | } | |
fc2e05c2 | 945 | |
bdec3c53 BM |
946 | kronecker = BN_kronecker(a, b, ctx); |
947 | if (kronecker < -1) goto err; | |
80d89e6a BM |
948 | /* we actually need BN_kronecker(a, |b|) */ |
949 | if (a->neg && b->neg) | |
950 | kronecker = -kronecker; | |
bdec3c53 BM |
951 | |
952 | if (legendre != kronecker) | |
953 | { | |
954 | fprintf(stderr, "legendre != kronecker; a = "); | |
955 | BN_print_fp(stderr, a); | |
fc2e05c2 | 956 | fprintf(stderr, ", b = "); |
bdec3c53 BM |
957 | BN_print_fp(stderr, b); |
958 | fprintf(stderr, "\n"); | |
959 | goto err; | |
960 | } | |
961 | ||
03a08489 BM |
962 | putc('.', stderr); |
963 | fflush(stderr); | |
bdec3c53 BM |
964 | } |
965 | ||
03a08489 BM |
966 | putc('\n', stderr); |
967 | fflush(stderr); | |
bdec3c53 BM |
968 | ret = 1; |
969 | err: | |
970 | if (a != NULL) BN_free(a); | |
971 | if (b != NULL) BN_free(b); | |
972 | if (r != NULL) BN_free(r); | |
470fa98f | 973 | if (t != NULL) BN_free(t); |
bdec3c53 | 974 | return ret; |
c7820896 BM |
975 | } |
976 | ||
cd2eebfd BM |
977 | int test_sqrt(BIO *bp, BN_CTX *ctx) |
978 | { | |
979 | BIGNUM *a,*p,*r; | |
980 | int i, j; | |
981 | int ret = 0; | |
982 | ||
983 | a = BN_new(); | |
984 | p = BN_new(); | |
985 | r = BN_new(); | |
986 | if (a == NULL || p == NULL || r == NULL) goto err; | |
987 | ||
988 | for (i = 0; i < 16; i++) | |
989 | { | |
990 | if (i < 8) | |
991 | { | |
77ac92d0 | 992 | unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 }; |
cd2eebfd BM |
993 | |
994 | if (!BN_set_word(p, primes[i])) goto err; | |
995 | } | |
996 | else | |
997 | { | |
998 | if (!BN_set_word(a, 32)) goto err; | |
999 | if (!BN_set_word(r, 2*i + 1)) goto err; | |
1000 | ||
1001 | if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err; | |
1002 | putc('\n', stderr); | |
1003 | } | |
80d89e6a | 1004 | p->neg = rand_neg(); |
cd2eebfd BM |
1005 | |
1006 | for (j = 0; j < num2; j++) | |
1007 | { | |
1008 | /* construct 'a' such that it is a square modulo p, | |
1009 | * but in general not a proper square and not reduced modulo p */ | |
111482cf | 1010 | if (!BN_bntest_rand(r, 256, 0, 3)) goto err; |
cd2eebfd BM |
1011 | if (!BN_nnmod(r, r, p, ctx)) goto err; |
1012 | if (!BN_mod_sqr(r, r, p, ctx)) goto err; | |
111482cf | 1013 | if (!BN_bntest_rand(a, 256, 0, 3)) goto err; |
cd2eebfd BM |
1014 | if (!BN_nnmod(a, a, p, ctx)) goto err; |
1015 | if (!BN_mod_sqr(a, a, p, ctx)) goto err; | |
1016 | if (!BN_mul(a, a, r, ctx)) goto err; | |
80d89e6a BM |
1017 | if (rand_neg()) |
1018 | if (!BN_sub(a, a, p)) goto err; | |
cd2eebfd BM |
1019 | |
1020 | if (!BN_mod_sqrt(r, a, p, ctx)) goto err; | |
1021 | if (!BN_mod_sqr(r, r, p, ctx)) goto err; | |
1022 | ||
1023 | if (!BN_nnmod(a, a, p, ctx)) goto err; | |
1024 | ||
1025 | if (BN_cmp(a, r) != 0) | |
1026 | { | |
1027 | fprintf(stderr, "BN_mod_sqrt failed: a = "); | |
1028 | BN_print_fp(stderr, a); | |
1029 | fprintf(stderr, ", r = "); | |
1030 | BN_print_fp(stderr, r); | |
1031 | fprintf(stderr, ", p = "); | |
1032 | BN_print_fp(stderr, p); | |
1033 | fprintf(stderr, "\n"); | |
1034 | goto err; | |
1035 | } | |
1036 | ||
1037 | putc('.', stderr); | |
1038 | fflush(stderr); | |
1039 | } | |
1040 | ||
1041 | putc('\n', stderr); | |
1042 | fflush(stderr); | |
1043 | } | |
1044 | ret = 1; | |
1045 | err: | |
1046 | if (a != NULL) BN_free(a); | |
1047 | if (p != NULL) BN_free(p); | |
1048 | if (r != NULL) BN_free(r); | |
1049 | return ret; | |
1050 | } | |
1051 | ||
4da88a8d | 1052 | int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) |
d02b48c6 | 1053 | { |
62bad771 | 1054 | BIGNUM *a,*b,*c,*d; |
d02b48c6 RE |
1055 | int i; |
1056 | ||
d02b48c6 RE |
1057 | b=BN_new(); |
1058 | c=BN_new(); | |
62bad771 | 1059 | d=BN_new(); |
d02b48c6 RE |
1060 | BN_one(c); |
1061 | ||
4da88a8d BL |
1062 | if(a_) |
1063 | a=a_; | |
1064 | else | |
1065 | { | |
1066 | a=BN_new(); | |
111482cf | 1067 | BN_bntest_rand(a,200,0,0); /**/ |
4da88a8d BL |
1068 | a->neg=rand_neg(); |
1069 | } | |
cae55bfc | 1070 | for (i=0; i<num0; i++) |
d02b48c6 RE |
1071 | { |
1072 | BN_lshift(b,a,i+1); | |
1073 | BN_add(c,c,c); | |
1074 | if (bp != NULL) | |
1075 | { | |
1076 | if (!results) | |
1077 | { | |
1078 | BN_print(bp,a); | |
1079 | BIO_puts(bp," * "); | |
1080 | BN_print(bp,c); | |
1081 | BIO_puts(bp," - "); | |
1082 | } | |
1083 | BN_print(bp,b); | |
1084 | BIO_puts(bp,"\n"); | |
1085 | } | |
62bad771 BL |
1086 | BN_mul(d,a,c,ctx); |
1087 | BN_sub(d,d,b); | |
1088 | if(!BN_is_zero(d)) | |
1089 | { | |
cae55bfc UM |
1090 | fprintf(stderr,"Left shift test failed!\n"); |
1091 | fprintf(stderr,"a="); | |
1092 | BN_print_fp(stderr,a); | |
1093 | fprintf(stderr,"\nb="); | |
1094 | BN_print_fp(stderr,b); | |
1095 | fprintf(stderr,"\nc="); | |
1096 | BN_print_fp(stderr,c); | |
1097 | fprintf(stderr,"\nd="); | |
1098 | BN_print_fp(stderr,d); | |
1099 | fprintf(stderr,"\n"); | |
62bad771 BL |
1100 | return 0; |
1101 | } | |
d02b48c6 RE |
1102 | } |
1103 | BN_free(a); | |
1104 | BN_free(b); | |
1105 | BN_free(c); | |
62bad771 | 1106 | BN_free(d); |
d02b48c6 RE |
1107 | return(1); |
1108 | } | |
1109 | ||
6b691a5c | 1110 | int test_lshift1(BIO *bp) |
d02b48c6 | 1111 | { |
62bad771 | 1112 | BIGNUM *a,*b,*c; |
d02b48c6 RE |
1113 | int i; |
1114 | ||
1115 | a=BN_new(); | |
1116 | b=BN_new(); | |
62bad771 | 1117 | c=BN_new(); |
d02b48c6 | 1118 | |
111482cf | 1119 | BN_bntest_rand(a,200,0,0); /**/ |
d02b48c6 | 1120 | a->neg=rand_neg(); |
cae55bfc | 1121 | for (i=0; i<num0; i++) |
d02b48c6 RE |
1122 | { |
1123 | BN_lshift1(b,a); | |
1124 | if (bp != NULL) | |
1125 | { | |
1126 | if (!results) | |
1127 | { | |
1128 | BN_print(bp,a); | |
1129 | BIO_puts(bp," * 2"); | |
1130 | BIO_puts(bp," - "); | |
1131 | } | |
1132 | BN_print(bp,b); | |
1133 | BIO_puts(bp,"\n"); | |
1134 | } | |
62bad771 BL |
1135 | BN_add(c,a,a); |
1136 | BN_sub(a,b,c); | |
1137 | if(!BN_is_zero(a)) | |
1138 | { | |
cae55bfc | 1139 | fprintf(stderr,"Left shift one test failed!\n"); |
62bad771 BL |
1140 | return 0; |
1141 | } | |
1142 | ||
d02b48c6 RE |
1143 | BN_copy(a,b); |
1144 | } | |
1145 | BN_free(a); | |
1146 | BN_free(b); | |
62bad771 | 1147 | BN_free(c); |
d02b48c6 RE |
1148 | return(1); |
1149 | } | |
1150 | ||
62bad771 | 1151 | int test_rshift(BIO *bp,BN_CTX *ctx) |
d02b48c6 | 1152 | { |
62bad771 | 1153 | BIGNUM *a,*b,*c,*d,*e; |
d02b48c6 RE |
1154 | int i; |
1155 | ||
1156 | a=BN_new(); | |
1157 | b=BN_new(); | |
1158 | c=BN_new(); | |
62bad771 BL |
1159 | d=BN_new(); |
1160 | e=BN_new(); | |
d02b48c6 RE |
1161 | BN_one(c); |
1162 | ||
111482cf | 1163 | BN_bntest_rand(a,200,0,0); /**/ |
d02b48c6 | 1164 | a->neg=rand_neg(); |
cae55bfc | 1165 | for (i=0; i<num0; i++) |
d02b48c6 RE |
1166 | { |
1167 | BN_rshift(b,a,i+1); | |
1168 | BN_add(c,c,c); | |
1169 | if (bp != NULL) | |
1170 | { | |
1171 | if (!results) | |
1172 | { | |
1173 | BN_print(bp,a); | |
1174 | BIO_puts(bp," / "); | |
1175 | BN_print(bp,c); | |
1176 | BIO_puts(bp," - "); | |
1177 | } | |
1178 | BN_print(bp,b); | |
1179 | BIO_puts(bp,"\n"); | |
1180 | } | |
62bad771 BL |
1181 | BN_div(d,e,a,c,ctx); |
1182 | BN_sub(d,d,b); | |
1183 | if(!BN_is_zero(d)) | |
1184 | { | |
cae55bfc | 1185 | fprintf(stderr,"Right shift test failed!\n"); |
62bad771 BL |
1186 | return 0; |
1187 | } | |
d02b48c6 RE |
1188 | } |
1189 | BN_free(a); | |
1190 | BN_free(b); | |
1191 | BN_free(c); | |
62bad771 BL |
1192 | BN_free(d); |
1193 | BN_free(e); | |
d02b48c6 RE |
1194 | return(1); |
1195 | } | |
1196 | ||
6b691a5c | 1197 | int test_rshift1(BIO *bp) |
d02b48c6 | 1198 | { |
62bad771 | 1199 | BIGNUM *a,*b,*c; |
d02b48c6 RE |
1200 | int i; |
1201 | ||
1202 | a=BN_new(); | |
1203 | b=BN_new(); | |
62bad771 | 1204 | c=BN_new(); |
d02b48c6 | 1205 | |
111482cf | 1206 | BN_bntest_rand(a,200,0,0); /**/ |
d02b48c6 | 1207 | a->neg=rand_neg(); |
cae55bfc | 1208 | for (i=0; i<num0; i++) |
d02b48c6 RE |
1209 | { |
1210 | BN_rshift1(b,a); | |
1211 | if (bp != NULL) | |
1212 | { | |
1213 | if (!results) | |
1214 | { | |
1215 | BN_print(bp,a); | |
1216 | BIO_puts(bp," / 2"); | |
1217 | BIO_puts(bp," - "); | |
1218 | } | |
1219 | BN_print(bp,b); | |
1220 | BIO_puts(bp,"\n"); | |
1221 | } | |
62bad771 BL |
1222 | BN_sub(c,a,b); |
1223 | BN_sub(c,c,b); | |
8dea52fa | 1224 | if(!BN_is_zero(c) && !BN_abs_is_word(c, 1)) |
62bad771 | 1225 | { |
cae55bfc | 1226 | fprintf(stderr,"Right shift one test failed!\n"); |
62bad771 BL |
1227 | return 0; |
1228 | } | |
d02b48c6 RE |
1229 | BN_copy(a,b); |
1230 | } | |
1231 | BN_free(a); | |
1232 | BN_free(b); | |
62bad771 | 1233 | BN_free(c); |
d02b48c6 RE |
1234 | return(1); |
1235 | } | |
1236 | ||
6b691a5c | 1237 | int rand_neg(void) |
d02b48c6 RE |
1238 | { |
1239 | static unsigned int neg=0; | |
1240 | static int sign[8]={0,0,0,1,1,0,1,1}; | |
1241 | ||
1242 | return(sign[(neg++)%8]); | |
1243 | } |