summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bntest.c
diff options
context:
space:
mode:
authorbeck <>1999-09-29 04:37:45 +0000
committerbeck <>1999-09-29 04:37:45 +0000
commitde8f24ea083384bb66b32ec105dc4743c5663cdf (patch)
tree1412176ae62a3cab2cf2b0b92150fcbceaac6092 /src/lib/libcrypto/bn/bntest.c
parentcb929d29896bcb87c2a97417fbd03e50078fc178 (diff)
downloadopenbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.gz
openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.bz2
openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.zip
OpenSSL 0.9.4 merge
Diffstat (limited to 'src/lib/libcrypto/bn/bntest.c')
-rw-r--r--src/lib/libcrypto/bn/bntest.c621
1 files changed, 430 insertions, 191 deletions
diff --git a/src/lib/libcrypto/bn/bntest.c b/src/lib/libcrypto/bn/bntest.c
index 9ebd68b429..df4b81f5b2 100644
--- a/src/lib/libcrypto/bn/bntest.c
+++ b/src/lib/libcrypto/bn/bntest.c
@@ -59,49 +59,35 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <stdlib.h> 60#include <stdlib.h>
61#include <string.h> 61#include <string.h>
62#include "e_os.h" 62
63#include "bio.h" 63#include "openssl/e_os.h"
64#include "bn.h" 64
65#include "rand.h" 65#include <openssl/bio.h>
66#include "x509.h" 66#include <openssl/bn.h>
67#include "err.h" 67#include <openssl/rand.h>
68#include <openssl/x509.h>
69#include <openssl/err.h>
68 70
69#ifdef WINDOWS 71#ifdef WINDOWS
70#include "../bio/bss_file.c" 72#include "../bio/bss_file.c"
71#endif 73#endif
72 74
73#ifndef NOPROTO 75int test_add(BIO *bp);
74int test_add (BIO *bp); 76int test_sub(BIO *bp);
75int test_sub (BIO *bp); 77int test_lshift1(BIO *bp);
76int test_lshift1 (BIO *bp); 78int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
77int test_lshift (BIO *bp); 79int test_rshift1(BIO *bp);
78int test_rshift1 (BIO *bp); 80int test_rshift(BIO *bp,BN_CTX *ctx);
79int test_rshift (BIO *bp); 81int test_div(BIO *bp,BN_CTX *ctx);
80int test_div (BIO *bp,BN_CTX *ctx); 82int test_div_recp(BIO *bp,BN_CTX *ctx);
81int test_mul (BIO *bp); 83int test_mul(BIO *bp);
82int test_sqr (BIO *bp,BN_CTX *ctx); 84int test_sqr(BIO *bp,BN_CTX *ctx);
83int test_mont (BIO *bp,BN_CTX *ctx); 85int test_mont(BIO *bp,BN_CTX *ctx);
84int test_mod (BIO *bp,BN_CTX *ctx); 86int test_mod(BIO *bp,BN_CTX *ctx);
85int test_mod_mul (BIO *bp,BN_CTX *ctx); 87int test_mod_mul(BIO *bp,BN_CTX *ctx);
86int test_mod_exp (BIO *bp,BN_CTX *ctx); 88int test_mod_exp(BIO *bp,BN_CTX *ctx);
89int test_exp(BIO *bp,BN_CTX *ctx);
87int rand_neg(void); 90int rand_neg(void);
88#else
89int test_add ();
90int test_sub ();
91int test_lshift1 ();
92int test_lshift ();
93int test_rshift1 ();
94int test_rshift ();
95int test_div ();
96int test_mul ();
97int test_sqr ();
98int test_mont ();
99int test_mod ();
100int test_mod_mul ();
101int test_mod_exp ();
102int rand_neg();
103#endif
104
105static int results=0; 91static int results=0;
106 92
107#ifdef NO_STDIO 93#ifdef NO_STDIO
@@ -109,16 +95,15 @@ static int results=0;
109#include "bss_file.c" 95#include "bss_file.c"
110#endif 96#endif
111 97
112int main(argc,argv) 98static unsigned char lst1[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
113int argc; 99"\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
114char *argv[]; 100
101int main(int argc, char *argv[])
115 { 102 {
116 BN_CTX *ctx; 103 BN_CTX *ctx;
117 BIO *out; 104 BIO *out;
118 char *outfile=NULL; 105 char *outfile=NULL;
119 106
120 srand((unsigned int)time(NULL));
121
122 argc--; 107 argc--;
123 argv++; 108 argv++;
124 while (argc >= 1) 109 while (argc >= 1)
@@ -168,8 +153,13 @@ char *argv[];
168 if (!test_lshift1(out)) goto err; 153 if (!test_lshift1(out)) goto err;
169 fflush(stdout); 154 fflush(stdout);
170 155
156 fprintf(stderr,"test BN_lshift (fixed)\n");
157 if (!test_lshift(out,ctx,BN_bin2bn(lst1,sizeof(lst1)-1,NULL)))
158 goto err;
159 fflush(stdout);
160
171 fprintf(stderr,"test BN_lshift\n"); 161 fprintf(stderr,"test BN_lshift\n");
172 if (!test_lshift(out)) goto err; 162 if (!test_lshift(out,ctx,NULL)) goto err;
173 fflush(stdout); 163 fflush(stdout);
174 164
175 fprintf(stderr,"test BN_rshift1\n"); 165 fprintf(stderr,"test BN_rshift1\n");
@@ -177,7 +167,7 @@ char *argv[];
177 fflush(stdout); 167 fflush(stdout);
178 168
179 fprintf(stderr,"test BN_rshift\n"); 169 fprintf(stderr,"test BN_rshift\n");
180 if (!test_rshift(out)) goto err; 170 if (!test_rshift(out,ctx)) goto err;
181 fflush(stdout); 171 fflush(stdout);
182 172
183 fprintf(stderr,"test BN_sqr\n"); 173 fprintf(stderr,"test BN_sqr\n");
@@ -192,6 +182,10 @@ char *argv[];
192 if (!test_div(out,ctx)) goto err; 182 if (!test_div(out,ctx)) goto err;
193 fflush(stdout); 183 fflush(stdout);
194 184
185 fprintf(stderr,"test BN_div_recp\n");
186 if (!test_div_recp(out,ctx)) goto err;
187 fflush(stdout);
188
195 fprintf(stderr,"test BN_mod\n"); 189 fprintf(stderr,"test BN_mod\n");
196 if (!test_mod(out,ctx)) goto err; 190 if (!test_mod(out,ctx)) goto err;
197 fflush(stdout); 191 fflush(stdout);
@@ -209,298 +203,423 @@ char *argv[];
209 if (!test_mod_exp(out,ctx)) goto err; 203 if (!test_mod_exp(out,ctx)) goto err;
210 fflush(stdout); 204 fflush(stdout);
211 205
206 fprintf(stderr,"test BN_exp\n");
207 if (!test_exp(out,ctx)) goto err;
208 fflush(stdout);
209
212/**/ 210/**/
213 exit(0); 211 exit(0);
214err: 212err:
213 BIO_puts(out,"1\n"); /* make sure bc fails if we are piping to it */
215 ERR_load_crypto_strings(); 214 ERR_load_crypto_strings();
216 ERR_print_errors(out); 215 ERR_print_errors(out);
217 exit(1); 216 exit(1);
218 return(1); 217 return(1);
219 } 218 }
220 219
221int test_add(bp) 220int test_add(BIO *bp)
222BIO *bp;
223 { 221 {
224 BIGNUM *a,*b,*c; 222 BIGNUM a,b,c;
225 int i; 223 int i;
226 int j; 224 int j;
227 225
228 a=BN_new(); 226 BN_init(&a);
229 b=BN_new(); 227 BN_init(&b);
230 c=BN_new(); 228 BN_init(&c);
231 229
232 BN_rand(a,512,0,0); 230 BN_rand(&a,512,0,0);
233 for (i=0; i<100; i++) 231 for (i=0; i<100; i++)
234 { 232 {
235 BN_rand(b,450+i,0,0); 233 BN_rand(&b,450+i,0,0);
236 a->neg=rand_neg(); 234 a.neg=rand_neg();
237 b->neg=rand_neg(); 235 b.neg=rand_neg();
238 if (bp == NULL) 236 if (bp == NULL)
239 for (j=0; j<10000; j++) 237 for (j=0; j<10000; j++)
240 BN_add(c,a,b); 238 BN_add(&c,&a,&b);
241 BN_add(c,a,b); 239 BN_add(&c,&a,&b);
242 if (bp != NULL) 240 if (bp != NULL)
243 { 241 {
244 if (!results) 242 if (!results)
245 { 243 {
246 BN_print(bp,a); 244 BN_print(bp,&a);
247 BIO_puts(bp," + "); 245 BIO_puts(bp," + ");
248 BN_print(bp,b); 246 BN_print(bp,&b);
249 BIO_puts(bp," - "); 247 BIO_puts(bp," - ");
250 } 248 }
251 BN_print(bp,c); 249 BN_print(bp,&c);
252 BIO_puts(bp,"\n"); 250 BIO_puts(bp,"\n");
253 } 251 }
252 a.neg=!a.neg;
253 b.neg=!b.neg;
254 BN_add(&c,&c,&b);
255 BN_add(&c,&c,&a);
256 if(!BN_is_zero(&c))
257 {
258 BIO_puts(bp,"Add test failed!\n");
259 return 0;
260 }
254 } 261 }
255 BN_free(a); 262 BN_free(&a);
256 BN_free(b); 263 BN_free(&b);
257 BN_free(c); 264 BN_free(&c);
258 return(1); 265 return(1);
259 } 266 }
260 267
261int test_sub(bp) 268int test_sub(BIO *bp)
262BIO *bp;
263 { 269 {
264 BIGNUM *a,*b,*c; 270 BIGNUM a,b,c;
265 int i; 271 int i;
266 int j; 272 int j;
267 273
268 a=BN_new(); 274 BN_init(&a);
269 b=BN_new(); 275 BN_init(&b);
270 c=BN_new(); 276 BN_init(&c);
271 277
272 BN_rand(a,512,0,0); 278 BN_rand(&a,512,0,0);
273 for (i=0; i<100; i++) 279 for (i=0; i<100; i++)
274 { 280 {
275 BN_rand(b,400+i,0,0); 281 BN_rand(&b,400+i,0,0);
276 a->neg=rand_neg(); 282 a.neg=rand_neg();
277 b->neg=rand_neg(); 283 b.neg=rand_neg();
278 if (bp == NULL) 284 if (bp == NULL)
279 for (j=0; j<10000; j++) 285 for (j=0; j<10000; j++)
280 BN_sub(c,a,b); 286 BN_sub(&c,&a,&b);
281 BN_sub(c,a,b); 287 BN_sub(&c,&a,&b);
282 if (bp != NULL) 288 if (bp != NULL)
283 { 289 {
284 if (!results) 290 if (!results)
285 { 291 {
286 BN_print(bp,a); 292 BN_print(bp,&a);
287 BIO_puts(bp," - "); 293 BIO_puts(bp," - ");
288 BN_print(bp,b); 294 BN_print(bp,&b);
289 BIO_puts(bp," - "); 295 BIO_puts(bp," - ");
290 } 296 }
291 BN_print(bp,c); 297 BN_print(bp,&c);
292 BIO_puts(bp,"\n"); 298 BIO_puts(bp,"\n");
293 } 299 }
300 BN_add(&c,&c,&b);
301 BN_sub(&c,&c,&a);
302 if(!BN_is_zero(&c))
303 {
304 BIO_puts(bp,"Subtract test failed!\n");
305 return 0;
306 }
294 } 307 }
295 BN_free(a); 308 BN_free(&a);
296 BN_free(b); 309 BN_free(&b);
297 BN_free(c); 310 BN_free(&c);
298 return(1); 311 return(1);
299 } 312 }
300 313
301int test_div(bp,ctx) 314int test_div(BIO *bp, BN_CTX *ctx)
302BIO *bp;
303BN_CTX *ctx;
304 { 315 {
305 BIGNUM *a,*b,*c,*d; 316 BIGNUM a,b,c,d,e;
306 int i; 317 int i;
307 int j; 318 int j;
308 319
309 a=BN_new(); 320 BN_init(&a);
310 b=BN_new(); 321 BN_init(&b);
311 c=BN_new(); 322 BN_init(&c);
312 d=BN_new(); 323 BN_init(&d);
324 BN_init(&e);
313 325
314 BN_rand(a,400,0,0); 326 BN_rand(&a,400,0,0);
315 for (i=0; i<100; i++) 327 for (i=0; i<100; i++)
316 { 328 {
317 BN_rand(b,50+i,0,0); 329 BN_rand(&b,50+i,0,0);
318 a->neg=rand_neg(); 330 a.neg=rand_neg();
319 b->neg=rand_neg(); 331 b.neg=rand_neg();
320 if (bp == NULL) 332 if (bp == NULL)
321 for (j=0; j<100; j++) 333 for (j=0; j<100; j++)
322 BN_div(d,c,a,b,ctx); 334 BN_div(&d,&c,&a,&b,ctx);
323 BN_div(d,c,a,b,ctx); 335 BN_div(&d,&c,&a,&b,ctx);
324 if (bp != NULL) 336 if (bp != NULL)
325 { 337 {
326 if (!results) 338 if (!results)
327 { 339 {
328 BN_print(bp,a); 340 BN_print(bp,&a);
329 BIO_puts(bp," / "); 341 BIO_puts(bp," / ");
330 BN_print(bp,b); 342 BN_print(bp,&b);
331 BIO_puts(bp," - "); 343 BIO_puts(bp," - ");
332 } 344 }
333 BN_print(bp,d); 345 BN_print(bp,&d);
334 BIO_puts(bp,"\n"); 346 BIO_puts(bp,"\n");
335 347
336 if (!results) 348 if (!results)
337 { 349 {
338 BN_print(bp,a); 350 BN_print(bp,&a);
339 BIO_puts(bp," % "); 351 BIO_puts(bp," % ");
340 BN_print(bp,b); 352 BN_print(bp,&b);
341 BIO_puts(bp," - "); 353 BIO_puts(bp," - ");
342 } 354 }
343 BN_print(bp,c); 355 BN_print(bp,&c);
344 BIO_puts(bp,"\n"); 356 BIO_puts(bp,"\n");
345 } 357 }
358 BN_mul(&e,&d,&b,ctx);
359 BN_add(&d,&e,&c);
360 BN_sub(&d,&d,&a);
361 if(!BN_is_zero(&d))
362 {
363 BIO_puts(bp,"Division test failed!\n");
364 return 0;
365 }
346 } 366 }
347 BN_free(a); 367 BN_free(&a);
348 BN_free(b); 368 BN_free(&b);
349 BN_free(c); 369 BN_free(&c);
350 BN_free(d); 370 BN_free(&d);
371 BN_free(&e);
351 return(1); 372 return(1);
352 } 373 }
353 374
354int test_mul(bp) 375int test_div_recp(BIO *bp, BN_CTX *ctx)
355BIO *bp;
356 { 376 {
357 BIGNUM *a,*b,*c; 377 BIGNUM a,b,c,d,e;
378 BN_RECP_CTX recp;
358 int i; 379 int i;
359 int j; 380 int j;
360 381
361 a=BN_new(); 382 BN_RECP_CTX_init(&recp);
362 b=BN_new(); 383 BN_init(&a);
363 c=BN_new(); 384 BN_init(&b);
385 BN_init(&c);
386 BN_init(&d);
387 BN_init(&e);
364 388
365 BN_rand(a,200,0,0); 389 BN_rand(&a,400,0,0);
366 for (i=0; i<100; i++) 390 for (i=0; i<100; i++)
367 { 391 {
368 BN_rand(b,250+i,0,0); 392 BN_rand(&b,50+i,0,0);
369 a->neg=rand_neg(); 393 a.neg=rand_neg();
370 b->neg=rand_neg(); 394 b.neg=rand_neg();
395 BN_RECP_CTX_set(&recp,&b,ctx);
371 if (bp == NULL) 396 if (bp == NULL)
372 for (j=0; j<100; j++) 397 for (j=0; j<100; j++)
373 BN_mul(c,a,b); 398 BN_div_recp(&d,&c,&a,&recp,ctx);
374 BN_mul(c,a,b); 399 BN_div_recp(&d,&c,&a,&recp,ctx);
375 if (bp != NULL) 400 if (bp != NULL)
376 { 401 {
377 if (!results) 402 if (!results)
378 { 403 {
379 BN_print(bp,a); 404 BN_print(bp,&a);
405 BIO_puts(bp," / ");
406 BN_print(bp,&b);
407 BIO_puts(bp," - ");
408 }
409 BN_print(bp,&d);
410 BIO_puts(bp,"\n");
411
412 if (!results)
413 {
414 BN_print(bp,&a);
415 BIO_puts(bp," % ");
416 BN_print(bp,&b);
417 BIO_puts(bp," - ");
418 }
419 BN_print(bp,&c);
420 BIO_puts(bp,"\n");
421 }
422 BN_mul(&e,&d,&b,ctx);
423 BN_add(&d,&e,&c);
424 BN_sub(&d,&d,&a);
425 if(!BN_is_zero(&d))
426 {
427 BIO_puts(bp,"Reciprocal division test failed!\n");
428 return 0;
429 }
430 }
431 BN_free(&a);
432 BN_free(&b);
433 BN_free(&c);
434 BN_free(&d);
435 BN_free(&e);
436 BN_RECP_CTX_free(&recp);
437 return(1);
438 }
439
440int test_mul(BIO *bp)
441 {
442 BIGNUM a,b,c,d,e;
443 int i;
444 int j;
445 BN_CTX ctx;
446
447 BN_CTX_init(&ctx);
448 BN_init(&a);
449 BN_init(&b);
450 BN_init(&c);
451 BN_init(&d);
452 BN_init(&e);
453
454 BN_rand(&a,200,0,0);
455 for (i=0; i<100; i++)
456 {
457 BN_rand(&b,250+i,0,0);
458 BN_rand(&b,200,0,0);
459 a.neg=rand_neg();
460 b.neg=rand_neg();
461 if (bp == NULL)
462 for (j=0; j<100; j++)
463 BN_mul(&c,&a,&b,&ctx);
464 BN_mul(&c,&a,&b,&ctx);
465 if (bp != NULL)
466 {
467 if (!results)
468 {
469 BN_print(bp,&a);
380 BIO_puts(bp," * "); 470 BIO_puts(bp," * ");
381 BN_print(bp,b); 471 BN_print(bp,&b);
382 BIO_puts(bp," - "); 472 BIO_puts(bp," - ");
383 } 473 }
384 BN_print(bp,c); 474 BN_print(bp,&c);
385 BIO_puts(bp,"\n"); 475 BIO_puts(bp,"\n");
386 } 476 }
477 BN_div(&d,&e,&c,&a,&ctx);
478 BN_sub(&d,&d,&b);
479 if(!BN_is_zero(&d) || !BN_is_zero(&e))
480 {
481 BIO_puts(bp,"Multiplication test failed!\n");
482 return 0;
483 }
387 } 484 }
388 BN_free(a); 485 BN_free(&a);
389 BN_free(b); 486 BN_free(&b);
390 BN_free(c); 487 BN_free(&c);
488 BN_free(&d);
489 BN_free(&e);
490 BN_CTX_free(&ctx);
391 return(1); 491 return(1);
392 } 492 }
393 493
394int test_sqr(bp,ctx) 494int test_sqr(BIO *bp, BN_CTX *ctx)
395BIO *bp;
396BN_CTX *ctx;
397 { 495 {
398 BIGNUM *a,*c; 496 BIGNUM a,c,d,e;
399 int i; 497 int i;
400 int j; 498 int j;
401 499
402 a=BN_new(); 500 BN_init(&a);
403 c=BN_new(); 501 BN_init(&c);
502 BN_init(&d);
503 BN_init(&e);
404 504
405 for (i=0; i<40; i++) 505 for (i=0; i<40; i++)
406 { 506 {
407 BN_rand(a,40+i*10,0,0); 507 BN_rand(&a,40+i*10,0,0);
408 a->neg=rand_neg(); 508 a.neg=rand_neg();
409 if (bp == NULL) 509 if (bp == NULL)
410 for (j=0; j<100; j++) 510 for (j=0; j<100; j++)
411 BN_sqr(c,a,ctx); 511 BN_sqr(&c,&a,ctx);
412 BN_sqr(c,a,ctx); 512 BN_sqr(&c,&a,ctx);
413 if (bp != NULL) 513 if (bp != NULL)
414 { 514 {
415 if (!results) 515 if (!results)
416 { 516 {
417 BN_print(bp,a); 517 BN_print(bp,&a);
418 BIO_puts(bp," * "); 518 BIO_puts(bp," * ");
419 BN_print(bp,a); 519 BN_print(bp,&a);
420 BIO_puts(bp," - "); 520 BIO_puts(bp," - ");
421 } 521 }
422 BN_print(bp,c); 522 BN_print(bp,&c);
423 BIO_puts(bp,"\n"); 523 BIO_puts(bp,"\n");
424 } 524 }
525 BN_div(&d,&e,&c,&a,ctx);
526 BN_sub(&d,&d,&a);
527 if(!BN_is_zero(&d) || !BN_is_zero(&e))
528 {
529 BIO_puts(bp,"Square test failed!\n");
530 return 0;
531 }
425 } 532 }
426 BN_free(a); 533 BN_free(&a);
427 BN_free(c); 534 BN_free(&c);
535 BN_free(&d);
536 BN_free(&e);
428 return(1); 537 return(1);
429 } 538 }
430 539
431int test_mont(bp,ctx) 540int test_mont(BIO *bp, BN_CTX *ctx)
432BIO *bp;
433BN_CTX *ctx;
434 { 541 {
435 BIGNUM *a,*b,*c,*A,*B; 542 BIGNUM a,b,c,d,A,B;
436 BIGNUM *n; 543 BIGNUM n;
437 int i; 544 int i;
438 int j; 545 int j;
439 BN_MONT_CTX *mont; 546 BN_MONT_CTX *mont;
440 547
441 a=BN_new(); 548 BN_init(&a);
442 b=BN_new(); 549 BN_init(&b);
443 c=BN_new(); 550 BN_init(&c);
444 A=BN_new(); 551 BN_init(&d);
445 B=BN_new(); 552 BN_init(&A);
446 n=BN_new(); 553 BN_init(&B);
554 BN_init(&n);
447 555
448 mont=BN_MONT_CTX_new(); 556 mont=BN_MONT_CTX_new();
449 557
450 BN_rand(a,100,0,0); /**/ 558 BN_rand(&a,100,0,0); /**/
451 BN_rand(b,100,0,0); /**/ 559 BN_rand(&b,100,0,0); /**/
452 for (i=0; i<10; i++) 560 for (i=0; i<10; i++)
453 { 561 {
454 BN_rand(n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/ 562 BN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
455 BN_MONT_CTX_set(mont,n,ctx); 563 BN_MONT_CTX_set(mont,&n,ctx);
456 564
457 BN_to_montgomery(A,a,mont,ctx); 565 BN_to_montgomery(&A,&a,mont,ctx);
458 BN_to_montgomery(B,b,mont,ctx); 566 BN_to_montgomery(&B,&b,mont,ctx);
459 567
460 if (bp == NULL) 568 if (bp == NULL)
461 for (j=0; j<100; j++) 569 for (j=0; j<100; j++)
462 BN_mod_mul_montgomery(c,A,B,mont,ctx);/**/ 570 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
463 BN_mod_mul_montgomery(c,A,B,mont,ctx);/**/ 571 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
464 BN_from_montgomery(A,c,mont,ctx);/**/ 572 BN_from_montgomery(&A,&c,mont,ctx);/**/
465 if (bp != NULL) 573 if (bp != NULL)
466 { 574 {
467 if (!results) 575 if (!results)
468 { 576 {
469#ifdef undef 577#ifdef undef
470fprintf(stderr,"%d * %d %% %d\n", 578fprintf(stderr,"%d * %d %% %d\n",
471BN_num_bits(a), 579BN_num_bits(&a),
472BN_num_bits(b), 580BN_num_bits(&b),
473BN_num_bits(mont->N)); 581BN_num_bits(mont->N));
474#endif 582#endif
475 BN_print(bp,a); 583 BN_print(bp,&a);
476 BIO_puts(bp," * "); 584 BIO_puts(bp," * ");
477 BN_print(bp,b); 585 BN_print(bp,&b);
478 BIO_puts(bp," % "); 586 BIO_puts(bp," % ");
479 BN_print(bp,mont->N); 587 BN_print(bp,&(mont->N));
480 BIO_puts(bp," - "); 588 BIO_puts(bp," - ");
481 } 589 }
482 BN_print(bp,A); 590 BN_print(bp,&A);
483 BIO_puts(bp,"\n"); 591 BIO_puts(bp,"\n");
484 } 592 }
593 BN_mod_mul(&d,&a,&b,&n,ctx);
594 BN_sub(&d,&d,&A);
595 if(!BN_is_zero(&d))
596 {
597 BIO_puts(bp,"Montgomery multiplication test failed!\n");
598 return 0;
599 }
485 } 600 }
486 BN_MONT_CTX_free(mont); 601 BN_MONT_CTX_free(mont);
487 BN_free(a); 602 BN_free(&a);
488 BN_free(b); 603 BN_free(&b);
489 BN_free(c); 604 BN_free(&c);
605 BN_free(&d);
606 BN_free(&A);
607 BN_free(&B);
608 BN_free(&n);
490 return(1); 609 return(1);
491 } 610 }
492 611
493int test_mod(bp,ctx) 612int test_mod(BIO *bp, BN_CTX *ctx)
494BIO *bp;
495BN_CTX *ctx;
496 { 613 {
497 BIGNUM *a,*b,*c; 614 BIGNUM *a,*b,*c,*d,*e;
498 int i; 615 int i;
499 int j; 616 int j;
500 617
501 a=BN_new(); 618 a=BN_new();
502 b=BN_new(); 619 b=BN_new();
503 c=BN_new(); 620 c=BN_new();
621 d=BN_new();
622 e=BN_new();
504 623
505 BN_rand(a,1024,0,0); /**/ 624 BN_rand(a,1024,0,0); /**/
506 for (i=0; i<20; i++) 625 for (i=0; i<20; i++)
@@ -524,16 +643,23 @@ BN_CTX *ctx;
524 BN_print(bp,c); 643 BN_print(bp,c);
525 BIO_puts(bp,"\n"); 644 BIO_puts(bp,"\n");
526 } 645 }
646 BN_div(d,e,a,b,ctx);
647 BN_sub(e,e,c);
648 if(!BN_is_zero(e))
649 {
650 BIO_puts(bp,"Modulo test failed!\n");
651 return 0;
652 }
527 } 653 }
528 BN_free(a); 654 BN_free(a);
529 BN_free(b); 655 BN_free(b);
530 BN_free(c); 656 BN_free(c);
657 BN_free(d);
658 BN_free(e);
531 return(1); 659 return(1);
532 } 660 }
533 661
534int test_mod_mul(bp,ctx) 662int test_mod_mul(BIO *bp, BN_CTX *ctx)
535BIO *bp;
536BN_CTX *ctx;
537 { 663 {
538 BIGNUM *a,*b,*c,*d,*e; 664 BIGNUM *a,*b,*c,*d,*e;
539 int i; 665 int i;
@@ -578,6 +704,14 @@ BN_CTX *ctx;
578 BN_print(bp,e); 704 BN_print(bp,e);
579 BIO_puts(bp,"\n"); 705 BIO_puts(bp,"\n");
580 } 706 }
707 BN_mul(d,a,b,ctx);
708 BN_sub(d,d,e);
709 BN_div(a,b,d,c,ctx);
710 if(!BN_is_zero(b))
711 {
712 BIO_puts(bp,"Modulo multiply test failed!\n");
713 return 0;
714 }
581 } 715 }
582 BN_free(a); 716 BN_free(a);
583 BN_free(b); 717 BN_free(b);
@@ -587,9 +721,7 @@ BN_CTX *ctx;
587 return(1); 721 return(1);
588 } 722 }
589 723
590int test_mod_exp(bp,ctx) 724int test_mod_exp(BIO *bp, BN_CTX *ctx)
591BIO *bp;
592BN_CTX *ctx;
593 { 725 {
594 BIGNUM *a,*b,*c,*d,*e; 726 BIGNUM *a,*b,*c,*d,*e;
595 int i; 727 int i;
@@ -623,6 +755,14 @@ BN_CTX *ctx;
623 BN_print(bp,d); 755 BN_print(bp,d);
624 BIO_puts(bp,"\n"); 756 BIO_puts(bp,"\n");
625 } 757 }
758 BN_exp(e,a,b,ctx);
759 BN_sub(e,e,d);
760 BN_div(a,b,e,c,ctx);
761 if(!BN_is_zero(b))
762 {
763 BIO_puts(bp,"Modulo exponentiation test failed!\n");
764 return 0;
765 }
626 } 766 }
627 BN_free(a); 767 BN_free(a);
628 BN_free(b); 768 BN_free(b);
@@ -632,19 +772,74 @@ BN_CTX *ctx;
632 return(1); 772 return(1);
633 } 773 }
634 774
635int test_lshift(bp) 775int test_exp(BIO *bp, BN_CTX *ctx)
636BIO *bp;
637 { 776 {
638 BIGNUM *a,*b,*c; 777 BIGNUM *a,*b,*d,*e,*one;
639 int i; 778 int i;
640 779
641 a=BN_new(); 780 a=BN_new();
642 b=BN_new(); 781 b=BN_new();
782 d=BN_new();
783 e=BN_new();
784 one=BN_new();
785 BN_one(one);
786
787 for (i=0; i<6; i++)
788 {
789 BN_rand(a,20+i*5,0,0); /**/
790 BN_rand(b,2+i,0,0); /**/
791
792 if (!BN_exp(d,a,b,ctx))
793 return(00);
794
795 if (bp != NULL)
796 {
797 if (!results)
798 {
799 BN_print(bp,a);
800 BIO_puts(bp," ^ ");
801 BN_print(bp,b);
802 BIO_puts(bp," - ");
803 }
804 BN_print(bp,d);
805 BIO_puts(bp,"\n");
806 }
807 BN_one(e);
808 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
809 BN_mul(e,e,a,ctx);
810 BN_sub(e,e,d);
811 if(!BN_is_zero(e))
812 {
813 BIO_puts(bp,"Exponentiation test failed!\n");
814 return 0;
815 }
816 }
817 BN_free(a);
818 BN_free(b);
819 BN_free(d);
820 BN_free(e);
821 BN_free(one);
822 return(1);
823 }
824
825int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
826 {
827 BIGNUM *a,*b,*c,*d;
828 int i;
829
830 b=BN_new();
643 c=BN_new(); 831 c=BN_new();
832 d=BN_new();
644 BN_one(c); 833 BN_one(c);
645 834
646 BN_rand(a,200,0,0); /**/ 835 if(a_)
647 a->neg=rand_neg(); 836 a=a_;
837 else
838 {
839 a=BN_new();
840 BN_rand(a,200,0,0); /**/
841 a->neg=rand_neg();
842 }
648 for (i=0; i<70; i++) 843 for (i=0; i<70; i++)
649 { 844 {
650 BN_lshift(b,a,i+1); 845 BN_lshift(b,a,i+1);
@@ -661,21 +856,38 @@ BIO *bp;
661 BN_print(bp,b); 856 BN_print(bp,b);
662 BIO_puts(bp,"\n"); 857 BIO_puts(bp,"\n");
663 } 858 }
859 BN_mul(d,a,c,ctx);
860 BN_sub(d,d,b);
861 if(!BN_is_zero(d))
862 {
863 BIO_puts(bp,"Left shift test failed!\n");
864 BIO_puts(bp,"a=");
865 BN_print(bp,a);
866 BIO_puts(bp,"\nb=");
867 BN_print(bp,b);
868 BIO_puts(bp,"\nc=");
869 BN_print(bp,c);
870 BIO_puts(bp,"\nd=");
871 BN_print(bp,d);
872 BIO_puts(bp,"\n");
873 return 0;
874 }
664 } 875 }
665 BN_free(a); 876 BN_free(a);
666 BN_free(b); 877 BN_free(b);
667 BN_free(c); 878 BN_free(c);
879 BN_free(d);
668 return(1); 880 return(1);
669 } 881 }
670 882
671int test_lshift1(bp) 883int test_lshift1(BIO *bp)
672BIO *bp;
673 { 884 {
674 BIGNUM *a,*b; 885 BIGNUM *a,*b,*c;
675 int i; 886 int i;
676 887
677 a=BN_new(); 888 a=BN_new();
678 b=BN_new(); 889 b=BN_new();
890 c=BN_new();
679 891
680 BN_rand(a,200,0,0); /**/ 892 BN_rand(a,200,0,0); /**/
681 a->neg=rand_neg(); 893 a->neg=rand_neg();
@@ -693,22 +905,32 @@ BIO *bp;
693 BN_print(bp,b); 905 BN_print(bp,b);
694 BIO_puts(bp,"\n"); 906 BIO_puts(bp,"\n");
695 } 907 }
908 BN_add(c,a,a);
909 BN_sub(a,b,c);
910 if(!BN_is_zero(a))
911 {
912 BIO_puts(bp,"Left shift one test failed!\n");
913 return 0;
914 }
915
696 BN_copy(a,b); 916 BN_copy(a,b);
697 } 917 }
698 BN_free(a); 918 BN_free(a);
699 BN_free(b); 919 BN_free(b);
920 BN_free(c);
700 return(1); 921 return(1);
701 } 922 }
702 923
703int test_rshift(bp) 924int test_rshift(BIO *bp,BN_CTX *ctx)
704BIO *bp;
705 { 925 {
706 BIGNUM *a,*b,*c; 926 BIGNUM *a,*b,*c,*d,*e;
707 int i; 927 int i;
708 928
709 a=BN_new(); 929 a=BN_new();
710 b=BN_new(); 930 b=BN_new();
711 c=BN_new(); 931 c=BN_new();
932 d=BN_new();
933 e=BN_new();
712 BN_one(c); 934 BN_one(c);
713 935
714 BN_rand(a,200,0,0); /**/ 936 BN_rand(a,200,0,0); /**/
@@ -729,21 +951,30 @@ BIO *bp;
729 BN_print(bp,b); 951 BN_print(bp,b);
730 BIO_puts(bp,"\n"); 952 BIO_puts(bp,"\n");
731 } 953 }
954 BN_div(d,e,a,c,ctx);
955 BN_sub(d,d,b);
956 if(!BN_is_zero(d))
957 {
958 BIO_puts(bp,"Right shift test failed!\n");
959 return 0;
960 }
732 } 961 }
733 BN_free(a); 962 BN_free(a);
734 BN_free(b); 963 BN_free(b);
735 BN_free(c); 964 BN_free(c);
965 BN_free(d);
966 BN_free(e);
736 return(1); 967 return(1);
737 } 968 }
738 969
739int test_rshift1(bp) 970int test_rshift1(BIO *bp)
740BIO *bp;
741 { 971 {
742 BIGNUM *a,*b; 972 BIGNUM *a,*b,*c;
743 int i; 973 int i;
744 974
745 a=BN_new(); 975 a=BN_new();
746 b=BN_new(); 976 b=BN_new();
977 c=BN_new();
747 978
748 BN_rand(a,200,0,0); /**/ 979 BN_rand(a,200,0,0); /**/
749 a->neg=rand_neg(); 980 a->neg=rand_neg();
@@ -761,14 +992,22 @@ BIO *bp;
761 BN_print(bp,b); 992 BN_print(bp,b);
762 BIO_puts(bp,"\n"); 993 BIO_puts(bp,"\n");
763 } 994 }
995 BN_sub(c,a,b);
996 BN_sub(c,c,b);
997 if(!BN_is_zero(c) && !BN_is_one(c))
998 {
999 BIO_puts(bp,"Right shift one test failed!\n");
1000 return 0;
1001 }
764 BN_copy(a,b); 1002 BN_copy(a,b);
765 } 1003 }
766 BN_free(a); 1004 BN_free(a);
767 BN_free(b); 1005 BN_free(b);
1006 BN_free(c);
768 return(1); 1007 return(1);
769 } 1008 }
770 1009
771int rand_neg() 1010int rand_neg(void)
772 { 1011 {
773 static unsigned int neg=0; 1012 static unsigned int neg=0;
774 static int sign[8]={0,0,0,1,1,0,1,1}; 1013 static int sign[8]={0,0,0,1,1,0,1,1};