diff options
author | miod <> | 2015-02-15 09:46:47 +0000 |
---|---|---|
committer | miod <> | 2015-02-15 09:46:47 +0000 |
commit | b14ee9fa51f3b9946c30db015d8cebc5cf62d1cc (patch) | |
tree | 44e80d502ceb8516e92c8833431d696df7501769 | |
parent | 746d14aa78a9eb49d6e296bb68b8c615639978d8 (diff) | |
download | openbsd-b14ee9fa51f3b9946c30db015d8cebc5cf62d1cc.tar.gz openbsd-b14ee9fa51f3b9946c30db015d8cebc5cf62d1cc.tar.bz2 openbsd-b14ee9fa51f3b9946c30db015d8cebc5cf62d1cc.zip |
Fix various memory leaks by not exiting so abruptly from failed tests.
-rw-r--r-- | src/regress/lib/libcrypto/bn/general/bntest.c | 1086 |
1 files changed, 507 insertions, 579 deletions
diff --git a/src/regress/lib/libcrypto/bn/general/bntest.c b/src/regress/lib/libcrypto/bn/general/bntest.c index a3206aba8c..ad000fcf56 100644 --- a/src/regress/lib/libcrypto/bn/general/bntest.c +++ b/src/regress/lib/libcrypto/bn/general/bntest.c | |||
@@ -120,19 +120,21 @@ int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx); | |||
120 | int rand_neg(void); | 120 | int rand_neg(void); |
121 | static int results=0; | 121 | static int results=0; |
122 | 122 | ||
123 | static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9" | 123 | static const unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9" |
124 | "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0"; | 124 | "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0"; |
125 | 125 | ||
126 | static void message(BIO *out, char *m) | 126 | static void |
127 | { | 127 | message(BIO *out, char *m) |
128 | { | ||
128 | fprintf(stderr, "test %s\n", m); | 129 | fprintf(stderr, "test %s\n", m); |
129 | BIO_puts(out, "print \"test "); | 130 | BIO_puts(out, "print \"test "); |
130 | BIO_puts(out, m); | 131 | BIO_puts(out, m); |
131 | BIO_puts(out, "\\n\"\n"); | 132 | BIO_puts(out, "\\n\"\n"); |
132 | } | 133 | } |
133 | 134 | ||
134 | int main(int argc, char *argv[]) | 135 | int |
135 | { | 136 | main(int argc, char *argv[]) |
137 | { | ||
136 | BN_CTX *ctx; | 138 | BN_CTX *ctx; |
137 | BIO *out; | 139 | BIO *out; |
138 | char *outfile=NULL; | 140 | char *outfile=NULL; |
@@ -141,18 +143,16 @@ int main(int argc, char *argv[]) | |||
141 | 143 | ||
142 | argc--; | 144 | argc--; |
143 | argv++; | 145 | argv++; |
144 | while (argc >= 1) | 146 | while (argc >= 1) { |
145 | { | ||
146 | if (strcmp(*argv,"-results") == 0) | 147 | if (strcmp(*argv,"-results") == 0) |
147 | results=1; | 148 | results=1; |
148 | else if (strcmp(*argv,"-out") == 0) | 149 | else if (strcmp(*argv,"-out") == 0) { |
149 | { | ||
150 | if (--argc < 1) break; | 150 | if (--argc < 1) break; |
151 | outfile= *(++argv); | 151 | outfile= *(++argv); |
152 | } | 152 | } |
153 | argc--; | 153 | argc--; |
154 | argv++; | 154 | argv++; |
155 | } | 155 | } |
156 | 156 | ||
157 | 157 | ||
158 | ctx=BN_CTX_new(); | 158 | ctx=BN_CTX_new(); |
@@ -160,18 +160,14 @@ int main(int argc, char *argv[]) | |||
160 | 160 | ||
161 | out=BIO_new(BIO_s_file()); | 161 | out=BIO_new(BIO_s_file()); |
162 | if (out == NULL) exit(1); | 162 | if (out == NULL) exit(1); |
163 | if (outfile == NULL) | 163 | if (outfile == NULL) { |
164 | { | ||
165 | BIO_set_fp(out,stdout,BIO_NOCLOSE); | 164 | BIO_set_fp(out,stdout,BIO_NOCLOSE); |
166 | } | 165 | } else { |
167 | else | 166 | if (!BIO_write_filename(out,outfile)) { |
168 | { | ||
169 | if (!BIO_write_filename(out,outfile)) | ||
170 | { | ||
171 | perror(outfile); | 167 | perror(outfile); |
172 | exit(1); | 168 | exit(1); |
173 | } | ||
174 | } | 169 | } |
170 | } | ||
175 | 171 | ||
176 | if (!results) | 172 | if (!results) |
177 | BIO_puts(out,"obase=16\nibase=16\n"); | 173 | BIO_puts(out,"obase=16\nibase=16\n"); |
@@ -311,107 +307,107 @@ err: | |||
311 | ERR_load_crypto_strings(); | 307 | ERR_load_crypto_strings(); |
312 | ERR_print_errors_fp(stderr); | 308 | ERR_print_errors_fp(stderr); |
313 | exit(1); | 309 | exit(1); |
314 | } | 310 | } |
315 | 311 | ||
316 | int test_add(BIO *bp) | 312 | int |
317 | { | 313 | test_add(BIO *bp) |
314 | { | ||
318 | BIGNUM a,b,c; | 315 | BIGNUM a,b,c; |
319 | int i; | 316 | int i; |
317 | int rc = 1; | ||
320 | 318 | ||
321 | BN_init(&a); | 319 | BN_init(&a); |
322 | BN_init(&b); | 320 | BN_init(&b); |
323 | BN_init(&c); | 321 | BN_init(&c); |
324 | 322 | ||
325 | BN_bntest_rand(&a,512,0,0); | 323 | BN_bntest_rand(&a,512,0,0); |
326 | for (i=0; i<num0; i++) | 324 | for (i=0; i<num0; i++) { |
327 | { | ||
328 | BN_bntest_rand(&b,450+i,0,0); | 325 | BN_bntest_rand(&b,450+i,0,0); |
329 | a.neg=rand_neg(); | 326 | a.neg=rand_neg(); |
330 | b.neg=rand_neg(); | 327 | b.neg=rand_neg(); |
331 | BN_add(&c,&a,&b); | 328 | BN_add(&c,&a,&b); |
332 | if (bp != NULL) | 329 | if (bp != NULL) { |
333 | { | 330 | if (!results) { |
334 | if (!results) | ||
335 | { | ||
336 | BN_print(bp,&a); | 331 | BN_print(bp,&a); |
337 | BIO_puts(bp," + "); | 332 | BIO_puts(bp," + "); |
338 | BN_print(bp,&b); | 333 | BN_print(bp,&b); |
339 | BIO_puts(bp," - "); | 334 | BIO_puts(bp," - "); |
340 | } | 335 | } |
341 | BN_print(bp,&c); | 336 | BN_print(bp,&c); |
342 | BIO_puts(bp,"\n"); | 337 | BIO_puts(bp,"\n"); |
343 | } | 338 | } |
344 | a.neg=!a.neg; | 339 | a.neg=!a.neg; |
345 | b.neg=!b.neg; | 340 | b.neg=!b.neg; |
346 | BN_add(&c,&c,&b); | 341 | BN_add(&c,&c,&b); |
347 | BN_add(&c,&c,&a); | 342 | BN_add(&c,&c,&a); |
348 | if(!BN_is_zero(&c)) | 343 | if(!BN_is_zero(&c)) { |
349 | { | ||
350 | fprintf(stderr,"Add test failed!\n"); | 344 | fprintf(stderr,"Add test failed!\n"); |
351 | return 0; | 345 | rc = 0; |
352 | } | 346 | break; |
353 | } | 347 | } |
348 | } | ||
354 | BN_free(&a); | 349 | BN_free(&a); |
355 | BN_free(&b); | 350 | BN_free(&b); |
356 | BN_free(&c); | 351 | BN_free(&c); |
357 | return(1); | 352 | return(rc); |
358 | } | 353 | } |
359 | 354 | ||
360 | int test_sub(BIO *bp) | 355 | int |
361 | { | 356 | test_sub(BIO *bp) |
357 | { | ||
362 | BIGNUM a,b,c; | 358 | BIGNUM a,b,c; |
363 | int i; | 359 | int i; |
360 | int rc = 1; | ||
364 | 361 | ||
365 | BN_init(&a); | 362 | BN_init(&a); |
366 | BN_init(&b); | 363 | BN_init(&b); |
367 | BN_init(&c); | 364 | BN_init(&c); |
368 | 365 | ||
369 | for (i=0; i<num0+num1; i++) | 366 | for (i=0; i<num0+num1; i++) { |
370 | { | 367 | if (i < num1) { |
371 | if (i < num1) | ||
372 | { | ||
373 | BN_bntest_rand(&a,512,0,0); | 368 | BN_bntest_rand(&a,512,0,0); |
374 | BN_copy(&b,&a); | 369 | BN_copy(&b,&a); |
375 | if (BN_set_bit(&a,i)==0) return(0); | 370 | if (BN_set_bit(&a,i)==0) { |
376 | BN_add_word(&b,i); | 371 | rc = 0; |
372 | break; | ||
377 | } | 373 | } |
378 | else | 374 | BN_add_word(&b,i); |
379 | { | 375 | } else { |
380 | BN_bntest_rand(&b,400+i-num1,0,0); | 376 | BN_bntest_rand(&b,400+i-num1,0,0); |
381 | a.neg=rand_neg(); | 377 | a.neg=rand_neg(); |
382 | b.neg=rand_neg(); | 378 | b.neg=rand_neg(); |
383 | } | 379 | } |
384 | BN_sub(&c,&a,&b); | 380 | BN_sub(&c,&a,&b); |
385 | if (bp != NULL) | 381 | if (bp != NULL) { |
386 | { | 382 | if (!results) { |
387 | if (!results) | ||
388 | { | ||
389 | BN_print(bp,&a); | 383 | BN_print(bp,&a); |
390 | BIO_puts(bp," - "); | 384 | BIO_puts(bp," - "); |
391 | BN_print(bp,&b); | 385 | BN_print(bp,&b); |
392 | BIO_puts(bp," - "); | 386 | BIO_puts(bp," - "); |
393 | } | 387 | } |
394 | BN_print(bp,&c); | 388 | BN_print(bp,&c); |
395 | BIO_puts(bp,"\n"); | 389 | BIO_puts(bp,"\n"); |
396 | } | 390 | } |
397 | BN_add(&c,&c,&b); | 391 | BN_add(&c,&c,&b); |
398 | BN_sub(&c,&c,&a); | 392 | BN_sub(&c,&c,&a); |
399 | if(!BN_is_zero(&c)) | 393 | if(!BN_is_zero(&c)) { |
400 | { | ||
401 | fprintf(stderr,"Subtract test failed!\n"); | 394 | fprintf(stderr,"Subtract test failed!\n"); |
402 | return 0; | 395 | rc = 0; |
403 | } | 396 | break; |
404 | } | 397 | } |
398 | } | ||
405 | BN_free(&a); | 399 | BN_free(&a); |
406 | BN_free(&b); | 400 | BN_free(&b); |
407 | BN_free(&c); | 401 | BN_free(&c); |
408 | return(1); | 402 | return(rc); |
409 | } | 403 | } |
410 | 404 | ||
411 | int test_div(BIO *bp, BN_CTX *ctx) | 405 | int |
412 | { | 406 | test_div(BIO *bp, BN_CTX *ctx) |
407 | { | ||
413 | BIGNUM a,b,c,d,e; | 408 | BIGNUM a,b,c,d,e; |
414 | int i; | 409 | int i; |
410 | int rc = 1; | ||
415 | 411 | ||
416 | BN_init(&a); | 412 | BN_init(&a); |
417 | BN_init(&b); | 413 | BN_init(&b); |
@@ -419,86 +415,82 @@ int test_div(BIO *bp, BN_CTX *ctx) | |||
419 | BN_init(&d); | 415 | BN_init(&d); |
420 | BN_init(&e); | 416 | BN_init(&e); |
421 | 417 | ||
422 | for (i=0; i<num0+num1; i++) | 418 | for (i=0; i<num0+num1; i++) { |
423 | { | 419 | if (i < num1) { |
424 | if (i < num1) | ||
425 | { | ||
426 | BN_bntest_rand(&a,400,0,0); | 420 | BN_bntest_rand(&a,400,0,0); |
427 | BN_copy(&b,&a); | 421 | BN_copy(&b,&a); |
428 | BN_lshift(&a,&a,i); | 422 | BN_lshift(&a,&a,i); |
429 | BN_add_word(&a,i); | 423 | BN_add_word(&a,i); |
430 | } | 424 | } else |
431 | else | ||
432 | BN_bntest_rand(&b,50+3*(i-num1),0,0); | 425 | BN_bntest_rand(&b,50+3*(i-num1),0,0); |
433 | a.neg=rand_neg(); | 426 | a.neg=rand_neg(); |
434 | b.neg=rand_neg(); | 427 | b.neg=rand_neg(); |
435 | BN_div(&d,&c,&a,&b,ctx); | 428 | BN_div(&d,&c,&a,&b,ctx); |
436 | if (bp != NULL) | 429 | if (bp != NULL) { |
437 | { | 430 | if (!results) { |
438 | if (!results) | ||
439 | { | ||
440 | BN_print(bp,&a); | 431 | BN_print(bp,&a); |
441 | BIO_puts(bp," / "); | 432 | BIO_puts(bp," / "); |
442 | BN_print(bp,&b); | 433 | BN_print(bp,&b); |
443 | BIO_puts(bp," - "); | 434 | BIO_puts(bp," - "); |
444 | } | 435 | } |
445 | BN_print(bp,&d); | 436 | BN_print(bp,&d); |
446 | BIO_puts(bp,"\n"); | 437 | BIO_puts(bp,"\n"); |
447 | 438 | ||
448 | if (!results) | 439 | if (!results) { |
449 | { | ||
450 | BN_print(bp,&a); | 440 | BN_print(bp,&a); |
451 | BIO_puts(bp," % "); | 441 | BIO_puts(bp," % "); |
452 | BN_print(bp,&b); | 442 | BN_print(bp,&b); |
453 | BIO_puts(bp," - "); | 443 | BIO_puts(bp," - "); |
454 | } | 444 | } |
455 | BN_print(bp,&c); | 445 | BN_print(bp,&c); |
456 | BIO_puts(bp,"\n"); | 446 | BIO_puts(bp,"\n"); |
457 | } | 447 | } |
458 | BN_mul(&e,&d,&b,ctx); | 448 | BN_mul(&e,&d,&b,ctx); |
459 | BN_add(&d,&e,&c); | 449 | BN_add(&d,&e,&c); |
460 | BN_sub(&d,&d,&a); | 450 | BN_sub(&d,&d,&a); |
461 | if(!BN_is_zero(&d)) | 451 | if(!BN_is_zero(&d)) { |
462 | { | ||
463 | fprintf(stderr,"Division test failed!\n"); | 452 | fprintf(stderr,"Division test failed!\n"); |
464 | return 0; | 453 | rc = 0; |
465 | } | 454 | break; |
466 | } | 455 | } |
456 | } | ||
467 | BN_free(&a); | 457 | BN_free(&a); |
468 | BN_free(&b); | 458 | BN_free(&b); |
469 | BN_free(&c); | 459 | BN_free(&c); |
470 | BN_free(&d); | 460 | BN_free(&d); |
471 | BN_free(&e); | 461 | BN_free(&e); |
472 | return(1); | 462 | return(rc); |
473 | } | 463 | } |
474 | 464 | ||
475 | static void print_word(BIO *bp,BN_ULONG w) | 465 | static void |
476 | { | 466 | print_word(BIO *bp,BN_ULONG w) |
467 | { | ||
477 | #ifdef SIXTY_FOUR_BIT | 468 | #ifdef SIXTY_FOUR_BIT |
478 | if (sizeof(w) > sizeof(unsigned long)) | 469 | if (sizeof(w) > sizeof(unsigned long)) { |
479 | { | 470 | unsigned long h=(unsigned long)(w>>32), l=(unsigned long)(w); |
480 | unsigned long h=(unsigned long)(w>>32), | ||
481 | l=(unsigned long)(w); | ||
482 | 471 | ||
483 | if (h) BIO_printf(bp,"%lX%08lX",h,l); | 472 | if (h) |
484 | else BIO_printf(bp,"%lX",l); | 473 | BIO_printf(bp,"%lX%08lX",h,l); |
474 | else | ||
475 | BIO_printf(bp,"%lX",l); | ||
485 | return; | 476 | return; |
486 | } | 477 | } |
487 | #endif | 478 | #endif |
488 | BIO_printf(bp,BN_HEX_FMT1,w); | 479 | BIO_printf(bp,BN_HEX_FMT1,w); |
489 | } | 480 | } |
490 | 481 | ||
491 | int test_div_word(BIO *bp) | 482 | int |
492 | { | 483 | test_div_word(BIO *bp) |
484 | { | ||
493 | BIGNUM a,b; | 485 | BIGNUM a,b; |
494 | BN_ULONG r,s; | 486 | BN_ULONG r,s; |
495 | int i; | 487 | int i; |
488 | int rc = 1; | ||
496 | 489 | ||
497 | BN_init(&a); | 490 | BN_init(&a); |
498 | BN_init(&b); | 491 | BN_init(&b); |
499 | 492 | ||
500 | for (i=0; i<num0; i++) | 493 | for (i=0; i<num0; i++) { |
501 | { | ||
502 | do { | 494 | do { |
503 | BN_bntest_rand(&a,512,-1,0); | 495 | BN_bntest_rand(&a,512,-1,0); |
504 | BN_bntest_rand(&b,BN_BITS2,-1,0); | 496 | BN_bntest_rand(&b,BN_BITS2,-1,0); |
@@ -508,47 +500,46 @@ int test_div_word(BIO *bp) | |||
508 | BN_copy(&b, &a); | 500 | BN_copy(&b, &a); |
509 | r = BN_div_word(&b, s); | 501 | r = BN_div_word(&b, s); |
510 | 502 | ||
511 | if (bp != NULL) | 503 | if (bp != NULL) { |
512 | { | 504 | if (!results) { |
513 | if (!results) | ||
514 | { | ||
515 | BN_print(bp,&a); | 505 | BN_print(bp,&a); |
516 | BIO_puts(bp," / "); | 506 | BIO_puts(bp," / "); |
517 | print_word(bp,s); | 507 | print_word(bp,s); |
518 | BIO_puts(bp," - "); | 508 | BIO_puts(bp," - "); |
519 | } | 509 | } |
520 | BN_print(bp,&b); | 510 | BN_print(bp,&b); |
521 | BIO_puts(bp,"\n"); | 511 | BIO_puts(bp,"\n"); |
522 | 512 | ||
523 | if (!results) | 513 | if (!results) { |
524 | { | ||
525 | BN_print(bp,&a); | 514 | BN_print(bp,&a); |
526 | BIO_puts(bp," % "); | 515 | BIO_puts(bp," % "); |
527 | print_word(bp,s); | 516 | print_word(bp,s); |
528 | BIO_puts(bp," - "); | 517 | BIO_puts(bp," - "); |
529 | } | 518 | } |
530 | print_word(bp,r); | 519 | print_word(bp,r); |
531 | BIO_puts(bp,"\n"); | 520 | BIO_puts(bp,"\n"); |
532 | } | 521 | } |
533 | BN_mul_word(&b,s); | 522 | BN_mul_word(&b,s); |
534 | BN_add_word(&b,r); | 523 | BN_add_word(&b,r); |
535 | BN_sub(&b,&a,&b); | 524 | BN_sub(&b,&a,&b); |
536 | if(!BN_is_zero(&b)) | 525 | if(!BN_is_zero(&b)) { |
537 | { | ||
538 | fprintf(stderr,"Division (word) test failed!\n"); | 526 | fprintf(stderr,"Division (word) test failed!\n"); |
539 | return 0; | 527 | rc = 0; |
540 | } | 528 | break; |
541 | } | 529 | } |
530 | } | ||
542 | BN_free(&a); | 531 | BN_free(&a); |
543 | BN_free(&b); | 532 | BN_free(&b); |
544 | return(1); | 533 | return(rc); |
545 | } | 534 | } |
546 | 535 | ||
547 | int test_div_recp(BIO *bp, BN_CTX *ctx) | 536 | int |
548 | { | 537 | test_div_recp(BIO *bp, BN_CTX *ctx) |
538 | { | ||
549 | BIGNUM a,b,c,d,e; | 539 | BIGNUM a,b,c,d,e; |
550 | BN_RECP_CTX recp; | 540 | BN_RECP_CTX recp; |
551 | int i; | 541 | int i; |
542 | int rc = 1; | ||
552 | 543 | ||
553 | BN_RECP_CTX_init(&recp); | 544 | BN_RECP_CTX_init(&recp); |
554 | BN_init(&a); | 545 | BN_init(&a); |
@@ -557,70 +548,66 @@ int test_div_recp(BIO *bp, BN_CTX *ctx) | |||
557 | BN_init(&d); | 548 | BN_init(&d); |
558 | BN_init(&e); | 549 | BN_init(&e); |
559 | 550 | ||
560 | for (i=0; i<num0+num1; i++) | 551 | for (i=0; i<num0+num1; i++) { |
561 | { | 552 | if (i < num1) { |
562 | if (i < num1) | ||
563 | { | ||
564 | BN_bntest_rand(&a,400,0,0); | 553 | BN_bntest_rand(&a,400,0,0); |
565 | BN_copy(&b,&a); | 554 | BN_copy(&b,&a); |
566 | BN_lshift(&a,&a,i); | 555 | BN_lshift(&a,&a,i); |
567 | BN_add_word(&a,i); | 556 | BN_add_word(&a,i); |
568 | } | 557 | } else |
569 | else | ||
570 | BN_bntest_rand(&b,50+3*(i-num1),0,0); | 558 | BN_bntest_rand(&b,50+3*(i-num1),0,0); |
571 | a.neg=rand_neg(); | 559 | a.neg=rand_neg(); |
572 | b.neg=rand_neg(); | 560 | b.neg=rand_neg(); |
573 | BN_RECP_CTX_set(&recp,&b,ctx); | 561 | BN_RECP_CTX_set(&recp,&b,ctx); |
574 | BN_div_recp(&d,&c,&a,&recp,ctx); | 562 | BN_div_recp(&d,&c,&a,&recp,ctx); |
575 | if (bp != NULL) | 563 | if (bp != NULL) { |
576 | { | 564 | if (!results) { |
577 | if (!results) | ||
578 | { | ||
579 | BN_print(bp,&a); | 565 | BN_print(bp,&a); |
580 | BIO_puts(bp," / "); | 566 | BIO_puts(bp," / "); |
581 | BN_print(bp,&b); | 567 | BN_print(bp,&b); |
582 | BIO_puts(bp," - "); | 568 | BIO_puts(bp," - "); |
583 | } | 569 | } |
584 | BN_print(bp,&d); | 570 | BN_print(bp,&d); |
585 | BIO_puts(bp,"\n"); | 571 | BIO_puts(bp,"\n"); |
586 | 572 | ||
587 | if (!results) | 573 | if (!results) { |
588 | { | ||
589 | BN_print(bp,&a); | 574 | BN_print(bp,&a); |
590 | BIO_puts(bp," % "); | 575 | BIO_puts(bp," % "); |
591 | BN_print(bp,&b); | 576 | BN_print(bp,&b); |
592 | BIO_puts(bp," - "); | 577 | BIO_puts(bp," - "); |
593 | } | 578 | } |
594 | BN_print(bp,&c); | 579 | BN_print(bp,&c); |
595 | BIO_puts(bp,"\n"); | 580 | BIO_puts(bp,"\n"); |
596 | } | 581 | } |
597 | BN_mul(&e,&d,&b,ctx); | 582 | BN_mul(&e,&d,&b,ctx); |
598 | BN_add(&d,&e,&c); | 583 | BN_add(&d,&e,&c); |
599 | BN_sub(&d,&d,&a); | 584 | BN_sub(&d,&d,&a); |
600 | if(!BN_is_zero(&d)) | 585 | if(!BN_is_zero(&d)) { |
601 | { | ||
602 | fprintf(stderr,"Reciprocal division test failed!\n"); | 586 | fprintf(stderr,"Reciprocal division test failed!\n"); |
603 | fprintf(stderr,"a="); | 587 | fprintf(stderr,"a="); |
604 | BN_print_fp(stderr,&a); | 588 | BN_print_fp(stderr,&a); |
605 | fprintf(stderr,"\nb="); | 589 | fprintf(stderr,"\nb="); |
606 | BN_print_fp(stderr,&b); | 590 | BN_print_fp(stderr,&b); |
607 | fprintf(stderr,"\n"); | 591 | fprintf(stderr,"\n"); |
608 | return 0; | 592 | rc = 0; |
609 | } | 593 | break; |
610 | } | 594 | } |
595 | } | ||
611 | BN_free(&a); | 596 | BN_free(&a); |
612 | BN_free(&b); | 597 | BN_free(&b); |
613 | BN_free(&c); | 598 | BN_free(&c); |
614 | BN_free(&d); | 599 | BN_free(&d); |
615 | BN_free(&e); | 600 | BN_free(&e); |
616 | BN_RECP_CTX_free(&recp); | 601 | BN_RECP_CTX_free(&recp); |
617 | return(1); | 602 | return(rc); |
618 | } | 603 | } |
619 | 604 | ||
620 | int test_mul(BIO *bp) | 605 | int |
621 | { | 606 | test_mul(BIO *bp) |
607 | { | ||
622 | BIGNUM a,b,c,d,e; | 608 | BIGNUM a,b,c,d,e; |
623 | int i; | 609 | int i; |
610 | int rc = 1; | ||
624 | BN_CTX *ctx; | 611 | BN_CTX *ctx; |
625 | 612 | ||
626 | ctx = BN_CTX_new(); | 613 | ctx = BN_CTX_new(); |
@@ -632,96 +619,96 @@ int test_mul(BIO *bp) | |||
632 | BN_init(&d); | 619 | BN_init(&d); |
633 | BN_init(&e); | 620 | BN_init(&e); |
634 | 621 | ||
635 | for (i=0; i<num0+num1; i++) | 622 | for (i=0; i<num0+num1; i++) { |
636 | { | 623 | if (i <= num1) { |
637 | if (i <= num1) | ||
638 | { | ||
639 | BN_bntest_rand(&a,100,0,0); | 624 | BN_bntest_rand(&a,100,0,0); |
640 | BN_bntest_rand(&b,100,0,0); | 625 | BN_bntest_rand(&b,100,0,0); |
641 | } | 626 | } else |
642 | else | ||
643 | BN_bntest_rand(&b,i-num1,0,0); | 627 | BN_bntest_rand(&b,i-num1,0,0); |
644 | a.neg=rand_neg(); | 628 | a.neg=rand_neg(); |
645 | b.neg=rand_neg(); | 629 | b.neg=rand_neg(); |
646 | BN_mul(&c,&a,&b,ctx); | 630 | BN_mul(&c,&a,&b,ctx); |
647 | if (bp != NULL) | 631 | if (bp != NULL) { |
648 | { | 632 | if (!results) { |
649 | if (!results) | ||
650 | { | ||
651 | BN_print(bp,&a); | 633 | BN_print(bp,&a); |
652 | BIO_puts(bp," * "); | 634 | BIO_puts(bp," * "); |
653 | BN_print(bp,&b); | 635 | BN_print(bp,&b); |
654 | BIO_puts(bp," - "); | 636 | BIO_puts(bp," - "); |
655 | } | 637 | } |
656 | BN_print(bp,&c); | 638 | BN_print(bp,&c); |
657 | BIO_puts(bp,"\n"); | 639 | BIO_puts(bp,"\n"); |
658 | } | 640 | } |
659 | BN_div(&d,&e,&c,&a,ctx); | 641 | BN_div(&d,&e,&c,&a,ctx); |
660 | BN_sub(&d,&d,&b); | 642 | BN_sub(&d,&d,&b); |
661 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) | 643 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) { |
662 | { | ||
663 | fprintf(stderr,"Multiplication test failed!\n"); | 644 | fprintf(stderr,"Multiplication test failed!\n"); |
664 | return 0; | 645 | rc = 0; |
665 | } | 646 | break; |
666 | } | 647 | } |
648 | } | ||
667 | BN_free(&a); | 649 | BN_free(&a); |
668 | BN_free(&b); | 650 | BN_free(&b); |
669 | BN_free(&c); | 651 | BN_free(&c); |
670 | BN_free(&d); | 652 | BN_free(&d); |
671 | BN_free(&e); | 653 | BN_free(&e); |
672 | BN_CTX_free(ctx); | 654 | BN_CTX_free(ctx); |
673 | return(1); | 655 | return(rc); |
674 | } | 656 | } |
675 | 657 | ||
676 | int test_sqr(BIO *bp, BN_CTX *ctx) | 658 | int |
677 | { | 659 | test_sqr(BIO *bp, BN_CTX *ctx) |
660 | { | ||
678 | BIGNUM a,c,d,e; | 661 | BIGNUM a,c,d,e; |
679 | int i; | 662 | int i; |
663 | int rc = 1; | ||
680 | 664 | ||
681 | BN_init(&a); | 665 | BN_init(&a); |
682 | BN_init(&c); | 666 | BN_init(&c); |
683 | BN_init(&d); | 667 | BN_init(&d); |
684 | BN_init(&e); | 668 | BN_init(&e); |
685 | 669 | ||
686 | for (i=0; i<num0; i++) | 670 | for (i=0; i<num0; i++) { |
687 | { | ||
688 | BN_bntest_rand(&a,40+i*10,0,0); | 671 | BN_bntest_rand(&a,40+i*10,0,0); |
689 | a.neg=rand_neg(); | 672 | a.neg=rand_neg(); |
690 | BN_sqr(&c,&a,ctx); | 673 | BN_sqr(&c,&a,ctx); |
691 | if (bp != NULL) | 674 | if (bp != NULL) { |
692 | { | 675 | if (!results) { |
693 | if (!results) | ||
694 | { | ||
695 | BN_print(bp,&a); | 676 | BN_print(bp,&a); |
696 | BIO_puts(bp," * "); | 677 | BIO_puts(bp," * "); |
697 | BN_print(bp,&a); | 678 | BN_print(bp,&a); |
698 | BIO_puts(bp," - "); | 679 | BIO_puts(bp," - "); |
699 | } | 680 | } |
700 | BN_print(bp,&c); | 681 | BN_print(bp,&c); |
701 | BIO_puts(bp,"\n"); | 682 | BIO_puts(bp,"\n"); |
702 | } | 683 | } |
703 | BN_div(&d,&e,&c,&a,ctx); | 684 | BN_div(&d,&e,&c,&a,ctx); |
704 | BN_sub(&d,&d,&a); | 685 | BN_sub(&d,&d,&a); |
705 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) | 686 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) { |
706 | { | ||
707 | fprintf(stderr,"Square test failed!\n"); | 687 | fprintf(stderr,"Square test failed!\n"); |
708 | return 0; | 688 | rc = 0; |
709 | } | 689 | break; |
710 | } | 690 | } |
691 | } | ||
711 | BN_free(&a); | 692 | BN_free(&a); |
712 | BN_free(&c); | 693 | BN_free(&c); |
713 | BN_free(&d); | 694 | BN_free(&d); |
714 | BN_free(&e); | 695 | BN_free(&e); |
715 | return(1); | 696 | return(rc); |
716 | } | 697 | } |
717 | 698 | ||
718 | int test_mont(BIO *bp, BN_CTX *ctx) | 699 | int |
719 | { | 700 | test_mont(BIO *bp, BN_CTX *ctx) |
701 | { | ||
720 | BIGNUM a,b,c,d,A,B; | 702 | BIGNUM a,b,c,d,A,B; |
721 | BIGNUM n; | 703 | BIGNUM n; |
722 | int i; | 704 | int i; |
705 | int rc = 1; | ||
723 | BN_MONT_CTX *mont; | 706 | BN_MONT_CTX *mont; |
724 | 707 | ||
708 | mont=BN_MONT_CTX_new(); | ||
709 | if (mont == NULL) | ||
710 | return 0; | ||
711 | |||
725 | BN_init(&a); | 712 | BN_init(&a); |
726 | BN_init(&b); | 713 | BN_init(&b); |
727 | BN_init(&c); | 714 | BN_init(&c); |
@@ -730,14 +717,9 @@ int test_mont(BIO *bp, BN_CTX *ctx) | |||
730 | BN_init(&B); | 717 | BN_init(&B); |
731 | BN_init(&n); | 718 | BN_init(&n); |
732 | 719 | ||
733 | mont=BN_MONT_CTX_new(); | ||
734 | if (mont == NULL) | ||
735 | return 0; | ||
736 | |||
737 | BN_bntest_rand(&a,100,0,0); /**/ | 720 | BN_bntest_rand(&a,100,0,0); /**/ |
738 | BN_bntest_rand(&b,100,0,0); /**/ | 721 | BN_bntest_rand(&b,100,0,0); /**/ |
739 | for (i=0; i<num2; i++) | 722 | for (i=0; i<num2; i++) { |
740 | { | ||
741 | int bits = (200*(i+1))/num2; | 723 | int bits = (200*(i+1))/num2; |
742 | 724 | ||
743 | if (bits == 0) | 725 | if (bits == 0) |
@@ -753,28 +735,26 @@ int test_mont(BIO *bp, BN_CTX *ctx) | |||
753 | 735 | ||
754 | BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/ | 736 | BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/ |
755 | BN_from_montgomery(&A,&c,mont,ctx);/**/ | 737 | BN_from_montgomery(&A,&c,mont,ctx);/**/ |
756 | if (bp != NULL) | 738 | if (bp != NULL) { |
757 | { | 739 | if (!results) { |
758 | if (!results) | ||
759 | { | ||
760 | BN_print(bp,&a); | 740 | BN_print(bp,&a); |
761 | BIO_puts(bp," * "); | 741 | BIO_puts(bp," * "); |
762 | BN_print(bp,&b); | 742 | BN_print(bp,&b); |
763 | BIO_puts(bp," % "); | 743 | BIO_puts(bp," % "); |
764 | BN_print(bp,&(mont->N)); | 744 | BN_print(bp,&(mont->N)); |
765 | BIO_puts(bp," - "); | 745 | BIO_puts(bp," - "); |
766 | } | 746 | } |
767 | BN_print(bp,&A); | 747 | BN_print(bp,&A); |
768 | BIO_puts(bp,"\n"); | 748 | BIO_puts(bp,"\n"); |
769 | } | 749 | } |
770 | BN_mod_mul(&d,&a,&b,&n,ctx); | 750 | BN_mod_mul(&d,&a,&b,&n,ctx); |
771 | BN_sub(&d,&d,&A); | 751 | BN_sub(&d,&d,&A); |
772 | if(!BN_is_zero(&d)) | 752 | if(!BN_is_zero(&d)) { |
773 | { | ||
774 | fprintf(stderr,"Montgomery multiplication test failed!\n"); | 753 | fprintf(stderr,"Montgomery multiplication test failed!\n"); |
775 | return 0; | 754 | rc = 0; |
776 | } | 755 | break; |
777 | } | 756 | } |
757 | } | ||
778 | BN_MONT_CTX_free(mont); | 758 | BN_MONT_CTX_free(mont); |
779 | BN_free(&a); | 759 | BN_free(&a); |
780 | BN_free(&b); | 760 | BN_free(&b); |
@@ -783,13 +763,15 @@ int test_mont(BIO *bp, BN_CTX *ctx) | |||
783 | BN_free(&A); | 763 | BN_free(&A); |
784 | BN_free(&B); | 764 | BN_free(&B); |
785 | BN_free(&n); | 765 | BN_free(&n); |
786 | return(1); | 766 | return(rc); |
787 | } | 767 | } |
788 | 768 | ||
789 | int test_mod(BIO *bp, BN_CTX *ctx) | 769 | int |
790 | { | 770 | test_mod(BIO *bp, BN_CTX *ctx) |
771 | { | ||
791 | BIGNUM *a,*b,*c,*d,*e; | 772 | BIGNUM *a,*b,*c,*d,*e; |
792 | int i; | 773 | int i; |
774 | int rc = 1; | ||
793 | 775 | ||
794 | a=BN_new(); | 776 | a=BN_new(); |
795 | b=BN_new(); | 777 | b=BN_new(); |
@@ -798,44 +780,43 @@ int test_mod(BIO *bp, BN_CTX *ctx) | |||
798 | e=BN_new(); | 780 | e=BN_new(); |
799 | 781 | ||
800 | BN_bntest_rand(a,1024,0,0); /**/ | 782 | BN_bntest_rand(a,1024,0,0); /**/ |
801 | for (i=0; i<num0; i++) | 783 | for (i=0; i<num0; i++) { |
802 | { | ||
803 | BN_bntest_rand(b,450+i*10,0,0); /**/ | 784 | BN_bntest_rand(b,450+i*10,0,0); /**/ |
804 | a->neg=rand_neg(); | 785 | a->neg=rand_neg(); |
805 | b->neg=rand_neg(); | 786 | b->neg=rand_neg(); |
806 | BN_mod(c,a,b,ctx);/**/ | 787 | BN_mod(c,a,b,ctx);/**/ |
807 | if (bp != NULL) | 788 | if (bp != NULL) { |
808 | { | 789 | if (!results) { |
809 | if (!results) | ||
810 | { | ||
811 | BN_print(bp,a); | 790 | BN_print(bp,a); |
812 | BIO_puts(bp," % "); | 791 | BIO_puts(bp," % "); |
813 | BN_print(bp,b); | 792 | BN_print(bp,b); |
814 | BIO_puts(bp," - "); | 793 | BIO_puts(bp," - "); |
815 | } | 794 | } |
816 | BN_print(bp,c); | 795 | BN_print(bp,c); |
817 | BIO_puts(bp,"\n"); | 796 | BIO_puts(bp,"\n"); |
818 | } | 797 | } |
819 | BN_div(d,e,a,b,ctx); | 798 | BN_div(d,e,a,b,ctx); |
820 | BN_sub(e,e,c); | 799 | BN_sub(e,e,c); |
821 | if(!BN_is_zero(e)) | 800 | if(!BN_is_zero(e)) { |
822 | { | ||
823 | fprintf(stderr,"Modulo test failed!\n"); | 801 | fprintf(stderr,"Modulo test failed!\n"); |
824 | return 0; | 802 | rc = 0; |
825 | } | 803 | break; |
826 | } | 804 | } |
805 | } | ||
827 | BN_free(a); | 806 | BN_free(a); |
828 | BN_free(b); | 807 | BN_free(b); |
829 | BN_free(c); | 808 | BN_free(c); |
830 | BN_free(d); | 809 | BN_free(d); |
831 | BN_free(e); | 810 | BN_free(e); |
832 | return(1); | 811 | return(rc); |
833 | } | 812 | } |
834 | 813 | ||
835 | int test_mod_mul(BIO *bp, BN_CTX *ctx) | 814 | int |
836 | { | 815 | test_mod_mul(BIO *bp, BN_CTX *ctx) |
816 | { | ||
837 | BIGNUM *a,*b,*c,*d,*e; | 817 | BIGNUM *a,*b,*c,*d,*e; |
838 | int i,j; | 818 | int i,j; |
819 | int rc = 1; | ||
839 | 820 | ||
840 | a=BN_new(); | 821 | a=BN_new(); |
841 | b=BN_new(); | 822 | b=BN_new(); |
@@ -844,69 +825,67 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx) | |||
844 | e=BN_new(); | 825 | e=BN_new(); |
845 | 826 | ||
846 | for (j=0; j<3; j++) { | 827 | for (j=0; j<3; j++) { |
847 | BN_bntest_rand(c,1024,0,0); /**/ | 828 | BN_bntest_rand(c,1024,0,0); /**/ |
848 | for (i=0; i<num0; i++) | 829 | for (i=0; i<num0; i++) { |
849 | { | 830 | BN_bntest_rand(a,475+i*10,0,0); /**/ |
850 | BN_bntest_rand(a,475+i*10,0,0); /**/ | 831 | BN_bntest_rand(b,425+i*11,0,0); /**/ |
851 | BN_bntest_rand(b,425+i*11,0,0); /**/ | 832 | a->neg=rand_neg(); |
852 | a->neg=rand_neg(); | 833 | b->neg=rand_neg(); |
853 | b->neg=rand_neg(); | 834 | if (!BN_mod_mul(e,a,b,c,ctx)) { |
854 | if (!BN_mod_mul(e,a,b,c,ctx)) | 835 | unsigned long l; |
855 | { | 836 | |
856 | unsigned long l; | 837 | while ((l=ERR_get_error())) |
857 | 838 | fprintf(stderr,"ERROR:%s\n", | |
858 | while ((l=ERR_get_error())) | 839 | ERR_error_string(l,NULL)); |
859 | fprintf(stderr,"ERROR:%s\n", | 840 | exit(1); |
860 | ERR_error_string(l,NULL)); | ||
861 | exit(1); | ||
862 | } | 841 | } |
863 | if (bp != NULL) | 842 | if (bp != NULL) { |
864 | { | 843 | if (!results) { |
865 | if (!results) | 844 | BN_print(bp,a); |
866 | { | 845 | BIO_puts(bp," * "); |
867 | BN_print(bp,a); | 846 | BN_print(bp,b); |
868 | BIO_puts(bp," * "); | 847 | BIO_puts(bp," % "); |
869 | BN_print(bp,b); | ||
870 | BIO_puts(bp," % "); | ||
871 | BN_print(bp,c); | ||
872 | if ((a->neg ^ b->neg) && !BN_is_zero(e)) | ||
873 | { | ||
874 | /* If (a*b) % c is negative, c must be added | ||
875 | * in order to obtain the normalized remainder | ||
876 | * (new with OpenSSL 0.9.7, previous versions of | ||
877 | * BN_mod_mul could generate negative results) | ||
878 | */ | ||
879 | BIO_puts(bp," + "); | ||
880 | BN_print(bp,c); | 848 | BN_print(bp,c); |
849 | if ((a->neg ^ b->neg) && !BN_is_zero(e)) { | ||
850 | /* If (a*b) % c is negative, c must be added | ||
851 | * in order to obtain the normalized remainder | ||
852 | * (new with OpenSSL 0.9.7, previous versions of | ||
853 | * BN_mod_mul could generate negative results) | ||
854 | */ | ||
855 | BIO_puts(bp," + "); | ||
856 | BN_print(bp,c); | ||
881 | } | 857 | } |
882 | BIO_puts(bp," - "); | 858 | BIO_puts(bp," - "); |
883 | } | 859 | } |
884 | BN_print(bp,e); | 860 | BN_print(bp,e); |
885 | BIO_puts(bp,"\n"); | 861 | BIO_puts(bp,"\n"); |
862 | } | ||
863 | BN_mul(d,a,b,ctx); | ||
864 | BN_sub(d,d,e); | ||
865 | BN_div(a,b,d,c,ctx); | ||
866 | if(!BN_is_zero(b)) { | ||
867 | fprintf(stderr,"Modulo multiply test failed!\n"); | ||
868 | ERR_print_errors_fp(stderr); | ||
869 | rc = 0; | ||
870 | goto done; | ||
886 | } | 871 | } |
887 | BN_mul(d,a,b,ctx); | ||
888 | BN_sub(d,d,e); | ||
889 | BN_div(a,b,d,c,ctx); | ||
890 | if(!BN_is_zero(b)) | ||
891 | { | ||
892 | fprintf(stderr,"Modulo multiply test failed!\n"); | ||
893 | ERR_print_errors_fp(stderr); | ||
894 | return 0; | ||
895 | } | ||
896 | } | 872 | } |
897 | } | 873 | } |
874 | done: | ||
898 | BN_free(a); | 875 | BN_free(a); |
899 | BN_free(b); | 876 | BN_free(b); |
900 | BN_free(c); | 877 | BN_free(c); |
901 | BN_free(d); | 878 | BN_free(d); |
902 | BN_free(e); | 879 | BN_free(e); |
903 | return(1); | 880 | return(rc); |
904 | } | 881 | } |
905 | 882 | ||
906 | int test_mod_exp(BIO *bp, BN_CTX *ctx) | 883 | int |
907 | { | 884 | test_mod_exp(BIO *bp, BN_CTX *ctx) |
885 | { | ||
908 | BIGNUM *a,*b,*c,*d,*e; | 886 | BIGNUM *a,*b,*c,*d,*e; |
909 | int i; | 887 | int i; |
888 | int rc = 1; | ||
910 | 889 | ||
911 | a=BN_new(); | 890 | a=BN_new(); |
912 | b=BN_new(); | 891 | b=BN_new(); |
@@ -915,49 +894,50 @@ int test_mod_exp(BIO *bp, BN_CTX *ctx) | |||
915 | e=BN_new(); | 894 | e=BN_new(); |
916 | 895 | ||
917 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ | 896 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ |
918 | for (i=0; i<num2; i++) | 897 | for (i=0; i<num2; i++) { |
919 | { | ||
920 | BN_bntest_rand(a,20+i*5,0,0); /**/ | 898 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
921 | BN_bntest_rand(b,2+i,0,0); /**/ | 899 | BN_bntest_rand(b,2+i,0,0); /**/ |
922 | 900 | ||
923 | if (!BN_mod_exp(d,a,b,c,ctx)) | 901 | if (!BN_mod_exp(d,a,b,c,ctx)) { |
924 | return(0); | 902 | rc = 0; |
903 | break; | ||
904 | } | ||
925 | 905 | ||
926 | if (bp != NULL) | 906 | if (bp != NULL) { |
927 | { | 907 | if (!results) { |
928 | if (!results) | ||
929 | { | ||
930 | BN_print(bp,a); | 908 | BN_print(bp,a); |
931 | BIO_puts(bp," ^ "); | 909 | BIO_puts(bp," ^ "); |
932 | BN_print(bp,b); | 910 | BN_print(bp,b); |
933 | BIO_puts(bp," % "); | 911 | BIO_puts(bp," % "); |
934 | BN_print(bp,c); | 912 | BN_print(bp,c); |
935 | BIO_puts(bp," - "); | 913 | BIO_puts(bp," - "); |
936 | } | 914 | } |
937 | BN_print(bp,d); | 915 | BN_print(bp,d); |
938 | BIO_puts(bp,"\n"); | 916 | BIO_puts(bp,"\n"); |
939 | } | 917 | } |
940 | BN_exp(e,a,b,ctx); | 918 | BN_exp(e,a,b,ctx); |
941 | BN_sub(e,e,d); | 919 | BN_sub(e,e,d); |
942 | BN_div(a,b,e,c,ctx); | 920 | BN_div(a,b,e,c,ctx); |
943 | if(!BN_is_zero(b)) | 921 | if(!BN_is_zero(b)) { |
944 | { | ||
945 | fprintf(stderr,"Modulo exponentiation test failed!\n"); | 922 | fprintf(stderr,"Modulo exponentiation test failed!\n"); |
946 | return 0; | 923 | rc = 0; |
947 | } | 924 | break; |
948 | } | 925 | } |
926 | } | ||
949 | BN_free(a); | 927 | BN_free(a); |
950 | BN_free(b); | 928 | BN_free(b); |
951 | BN_free(c); | 929 | BN_free(c); |
952 | BN_free(d); | 930 | BN_free(d); |
953 | BN_free(e); | 931 | BN_free(e); |
954 | return(1); | 932 | return(rc); |
955 | } | 933 | } |
956 | 934 | ||
957 | int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) | 935 | int |
958 | { | 936 | test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) |
937 | { | ||
959 | BIGNUM *a,*b,*c,*d,*e; | 938 | BIGNUM *a,*b,*c,*d,*e; |
960 | int i; | 939 | int i; |
940 | int rc = 1; | ||
961 | 941 | ||
962 | a=BN_new(); | 942 | a=BN_new(); |
963 | b=BN_new(); | 943 | b=BN_new(); |
@@ -966,49 +946,50 @@ int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) | |||
966 | e=BN_new(); | 946 | e=BN_new(); |
967 | 947 | ||
968 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ | 948 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ |
969 | for (i=0; i<num2; i++) | 949 | for (i=0; i<num2; i++) { |
970 | { | ||
971 | BN_bntest_rand(a,20+i*5,0,0); /**/ | 950 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
972 | BN_bntest_rand(b,2+i,0,0); /**/ | 951 | BN_bntest_rand(b,2+i,0,0); /**/ |
973 | 952 | ||
974 | if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL)) | 953 | if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL)) { |
975 | return(00); | 954 | rc = 0; |
955 | break; | ||
956 | } | ||
976 | 957 | ||
977 | if (bp != NULL) | 958 | if (bp != NULL) { |
978 | { | 959 | if (!results) { |
979 | if (!results) | ||
980 | { | ||
981 | BN_print(bp,a); | 960 | BN_print(bp,a); |
982 | BIO_puts(bp," ^ "); | 961 | BIO_puts(bp," ^ "); |
983 | BN_print(bp,b); | 962 | BN_print(bp,b); |
984 | BIO_puts(bp," % "); | 963 | BIO_puts(bp," % "); |
985 | BN_print(bp,c); | 964 | BN_print(bp,c); |
986 | BIO_puts(bp," - "); | 965 | BIO_puts(bp," - "); |
987 | } | 966 | } |
988 | BN_print(bp,d); | 967 | BN_print(bp,d); |
989 | BIO_puts(bp,"\n"); | 968 | BIO_puts(bp,"\n"); |
990 | } | 969 | } |
991 | BN_exp(e,a,b,ctx); | 970 | BN_exp(e,a,b,ctx); |
992 | BN_sub(e,e,d); | 971 | BN_sub(e,e,d); |
993 | BN_div(a,b,e,c,ctx); | 972 | BN_div(a,b,e,c,ctx); |
994 | if(!BN_is_zero(b)) | 973 | if(!BN_is_zero(b)) { |
995 | { | ||
996 | fprintf(stderr,"Modulo exponentiation test failed!\n"); | 974 | fprintf(stderr,"Modulo exponentiation test failed!\n"); |
997 | return 0; | 975 | rc = 0; |
998 | } | 976 | break; |
999 | } | 977 | } |
978 | } | ||
1000 | BN_free(a); | 979 | BN_free(a); |
1001 | BN_free(b); | 980 | BN_free(b); |
1002 | BN_free(c); | 981 | BN_free(c); |
1003 | BN_free(d); | 982 | BN_free(d); |
1004 | BN_free(e); | 983 | BN_free(e); |
1005 | return(1); | 984 | return(rc); |
1006 | } | 985 | } |
1007 | 986 | ||
1008 | int test_exp(BIO *bp, BN_CTX *ctx) | 987 | int |
1009 | { | 988 | test_exp(BIO *bp, BN_CTX *ctx) |
989 | { | ||
1010 | BIGNUM *a,*b,*d,*e,*one; | 990 | BIGNUM *a,*b,*d,*e,*one; |
1011 | int i; | 991 | int i; |
992 | int rc = 1; | ||
1012 | 993 | ||
1013 | a=BN_new(); | 994 | a=BN_new(); |
1014 | b=BN_new(); | 995 | b=BN_new(); |
@@ -1017,46 +998,47 @@ int test_exp(BIO *bp, BN_CTX *ctx) | |||
1017 | one=BN_new(); | 998 | one=BN_new(); |
1018 | BN_one(one); | 999 | BN_one(one); |
1019 | 1000 | ||
1020 | for (i=0; i<num2; i++) | 1001 | for (i=0; i<num2; i++) { |
1021 | { | ||
1022 | BN_bntest_rand(a,20+i*5,0,0); /**/ | 1002 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
1023 | BN_bntest_rand(b,2+i,0,0); /**/ | 1003 | BN_bntest_rand(b,2+i,0,0); /**/ |
1024 | 1004 | ||
1025 | if (BN_exp(d,a,b,ctx) <= 0) | 1005 | if (BN_exp(d,a,b,ctx) <= 0) { |
1026 | return(0); | 1006 | rc = 0; |
1007 | break; | ||
1008 | } | ||
1027 | 1009 | ||
1028 | if (bp != NULL) | 1010 | if (bp != NULL) { |
1029 | { | 1011 | if (!results) { |
1030 | if (!results) | ||
1031 | { | ||
1032 | BN_print(bp,a); | 1012 | BN_print(bp,a); |
1033 | BIO_puts(bp," ^ "); | 1013 | BIO_puts(bp," ^ "); |
1034 | BN_print(bp,b); | 1014 | BN_print(bp,b); |
1035 | BIO_puts(bp," - "); | 1015 | BIO_puts(bp," - "); |
1036 | } | 1016 | } |
1037 | BN_print(bp,d); | 1017 | BN_print(bp,d); |
1038 | BIO_puts(bp,"\n"); | 1018 | BIO_puts(bp,"\n"); |
1039 | } | 1019 | } |
1040 | BN_one(e); | 1020 | BN_one(e); |
1041 | for( ; !BN_is_zero(b) ; BN_sub(b,b,one)) | 1021 | for( ; !BN_is_zero(b) ; BN_sub(b,b,one)) |
1042 | BN_mul(e,e,a,ctx); | 1022 | BN_mul(e,e,a,ctx); |
1043 | BN_sub(e,e,d); | 1023 | BN_sub(e,e,d); |
1044 | if(!BN_is_zero(e)) | 1024 | if(!BN_is_zero(e)) { |
1045 | { | ||
1046 | fprintf(stderr,"Exponentiation test failed!\n"); | 1025 | fprintf(stderr,"Exponentiation test failed!\n"); |
1047 | return 0; | 1026 | rc = 0; |
1048 | } | 1027 | break; |
1049 | } | 1028 | } |
1029 | } | ||
1050 | BN_free(a); | 1030 | BN_free(a); |
1051 | BN_free(b); | 1031 | BN_free(b); |
1052 | BN_free(d); | 1032 | BN_free(d); |
1053 | BN_free(e); | 1033 | BN_free(e); |
1054 | BN_free(one); | 1034 | BN_free(one); |
1055 | return(1); | 1035 | return(rc); |
1056 | } | 1036 | } |
1037 | |||
1057 | #ifndef OPENSSL_NO_EC2M | 1038 | #ifndef OPENSSL_NO_EC2M |
1058 | int test_gf2m_add(BIO *bp) | 1039 | int |
1059 | { | 1040 | test_gf2m_add(BIO *bp) |
1041 | { | ||
1060 | BIGNUM a,b,c; | 1042 | BIGNUM a,b,c; |
1061 | int i, ret = 0; | 1043 | int i, ret = 0; |
1062 | 1044 | ||
@@ -1064,51 +1046,47 @@ int test_gf2m_add(BIO *bp) | |||
1064 | BN_init(&b); | 1046 | BN_init(&b); |
1065 | BN_init(&c); | 1047 | BN_init(&c); |
1066 | 1048 | ||
1067 | for (i=0; i<num0; i++) | 1049 | for (i=0; i<num0; i++) { |
1068 | { | ||
1069 | BN_rand(&a,512,0,0); | 1050 | BN_rand(&a,512,0,0); |
1070 | BN_copy(&b, BN_value_one()); | 1051 | BN_copy(&b, BN_value_one()); |
1071 | a.neg=rand_neg(); | 1052 | a.neg=rand_neg(); |
1072 | b.neg=rand_neg(); | 1053 | b.neg=rand_neg(); |
1073 | BN_GF2m_add(&c,&a,&b); | 1054 | BN_GF2m_add(&c,&a,&b); |
1074 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1055 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1075 | if (bp != NULL) | 1056 | if (bp != NULL) { |
1076 | { | 1057 | if (!results) { |
1077 | if (!results) | ||
1078 | { | ||
1079 | BN_print(bp,&a); | 1058 | BN_print(bp,&a); |
1080 | BIO_puts(bp," ^ "); | 1059 | BIO_puts(bp," ^ "); |
1081 | BN_print(bp,&b); | 1060 | BN_print(bp,&b); |
1082 | BIO_puts(bp," = "); | 1061 | BIO_puts(bp," = "); |
1083 | } | 1062 | } |
1084 | BN_print(bp,&c); | 1063 | BN_print(bp,&c); |
1085 | BIO_puts(bp,"\n"); | 1064 | BIO_puts(bp,"\n"); |
1086 | } | 1065 | } |
1087 | #endif | 1066 | #endif |
1088 | /* Test that two added values have the correct parity. */ | 1067 | /* Test that two added values have the correct parity. */ |
1089 | if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c))) | 1068 | if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c))) { |
1090 | { | ||
1091 | fprintf(stderr,"GF(2^m) addition test (a) failed!\n"); | 1069 | fprintf(stderr,"GF(2^m) addition test (a) failed!\n"); |
1092 | goto err; | 1070 | goto err; |
1093 | } | 1071 | } |
1094 | BN_GF2m_add(&c,&c,&c); | 1072 | BN_GF2m_add(&c,&c,&c); |
1095 | /* Test that c + c = 0. */ | 1073 | /* Test that c + c = 0. */ |
1096 | if(!BN_is_zero(&c)) | 1074 | if(!BN_is_zero(&c)) { |
1097 | { | ||
1098 | fprintf(stderr,"GF(2^m) addition test (b) failed!\n"); | 1075 | fprintf(stderr,"GF(2^m) addition test (b) failed!\n"); |
1099 | goto err; | 1076 | goto err; |
1100 | } | ||
1101 | } | 1077 | } |
1078 | } | ||
1102 | ret = 1; | 1079 | ret = 1; |
1103 | err: | 1080 | err: |
1104 | BN_free(&a); | 1081 | BN_free(&a); |
1105 | BN_free(&b); | 1082 | BN_free(&b); |
1106 | BN_free(&c); | 1083 | BN_free(&c); |
1107 | return ret; | 1084 | return ret; |
1108 | } | 1085 | } |
1109 | 1086 | ||
1110 | int test_gf2m_mod(BIO *bp) | 1087 | int |
1111 | { | 1088 | test_gf2m_mod(BIO *bp) |
1089 | { | ||
1112 | BIGNUM *a,*b[2],*c,*d,*e; | 1090 | BIGNUM *a,*b[2],*c,*d,*e; |
1113 | int i, j, ret = 0; | 1091 | int i, j, ret = 0; |
1114 | int p0[] = {163,7,6,3,0,-1}; | 1092 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1124,38 +1102,33 @@ int test_gf2m_mod(BIO *bp) | |||
1124 | BN_GF2m_arr2poly(p0, b[0]); | 1102 | BN_GF2m_arr2poly(p0, b[0]); |
1125 | BN_GF2m_arr2poly(p1, b[1]); | 1103 | BN_GF2m_arr2poly(p1, b[1]); |
1126 | 1104 | ||
1127 | for (i=0; i<num0; i++) | 1105 | for (i=0; i<num0; i++) { |
1128 | { | ||
1129 | BN_bntest_rand(a, 1024, 0, 0); | 1106 | BN_bntest_rand(a, 1024, 0, 0); |
1130 | for (j=0; j < 2; j++) | 1107 | for (j=0; j < 2; j++) { |
1131 | { | ||
1132 | BN_GF2m_mod(c, a, b[j]); | 1108 | BN_GF2m_mod(c, a, b[j]); |
1133 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1109 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1134 | if (bp != NULL) | 1110 | if (bp != NULL) { |
1135 | { | 1111 | if (!results) { |
1136 | if (!results) | ||
1137 | { | ||
1138 | BN_print(bp,a); | 1112 | BN_print(bp,a); |
1139 | BIO_puts(bp," % "); | 1113 | BIO_puts(bp," % "); |
1140 | BN_print(bp,b[j]); | 1114 | BN_print(bp,b[j]); |
1141 | BIO_puts(bp," - "); | 1115 | BIO_puts(bp," - "); |
1142 | BN_print(bp,c); | 1116 | BN_print(bp,c); |
1143 | BIO_puts(bp,"\n"); | 1117 | BIO_puts(bp,"\n"); |
1144 | } | ||
1145 | } | 1118 | } |
1119 | } | ||
1146 | #endif | 1120 | #endif |
1147 | BN_GF2m_add(d, a, c); | 1121 | BN_GF2m_add(d, a, c); |
1148 | BN_GF2m_mod(e, d, b[j]); | 1122 | BN_GF2m_mod(e, d, b[j]); |
1149 | /* Test that a + (a mod p) mod p == 0. */ | 1123 | /* Test that a + (a mod p) mod p == 0. */ |
1150 | if(!BN_is_zero(e)) | 1124 | if(!BN_is_zero(e)) { |
1151 | { | ||
1152 | fprintf(stderr,"GF(2^m) modulo test failed!\n"); | 1125 | fprintf(stderr,"GF(2^m) modulo test failed!\n"); |
1153 | goto err; | 1126 | goto err; |
1154 | } | ||
1155 | } | 1127 | } |
1156 | } | 1128 | } |
1129 | } | ||
1157 | ret = 1; | 1130 | ret = 1; |
1158 | err: | 1131 | err: |
1159 | BN_free(a); | 1132 | BN_free(a); |
1160 | BN_free(b[0]); | 1133 | BN_free(b[0]); |
1161 | BN_free(b[1]); | 1134 | BN_free(b[1]); |
@@ -1163,10 +1136,11 @@ int test_gf2m_mod(BIO *bp) | |||
1163 | BN_free(d); | 1136 | BN_free(d); |
1164 | BN_free(e); | 1137 | BN_free(e); |
1165 | return ret; | 1138 | return ret; |
1166 | } | 1139 | } |
1167 | 1140 | ||
1168 | int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | 1141 | int |
1169 | { | 1142 | test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) |
1143 | { | ||
1170 | BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h; | 1144 | BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h; |
1171 | int i, j, ret = 0; | 1145 | int i, j, ret = 0; |
1172 | int p0[] = {163,7,6,3,0,-1}; | 1146 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1185,19 +1159,15 @@ int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | |||
1185 | BN_GF2m_arr2poly(p0, b[0]); | 1159 | BN_GF2m_arr2poly(p0, b[0]); |
1186 | BN_GF2m_arr2poly(p1, b[1]); | 1160 | BN_GF2m_arr2poly(p1, b[1]); |
1187 | 1161 | ||
1188 | for (i=0; i<num0; i++) | 1162 | for (i=0; i<num0; i++) { |
1189 | { | ||
1190 | BN_bntest_rand(a, 1024, 0, 0); | 1163 | BN_bntest_rand(a, 1024, 0, 0); |
1191 | BN_bntest_rand(c, 1024, 0, 0); | 1164 | BN_bntest_rand(c, 1024, 0, 0); |
1192 | BN_bntest_rand(d, 1024, 0, 0); | 1165 | BN_bntest_rand(d, 1024, 0, 0); |
1193 | for (j=0; j < 2; j++) | 1166 | for (j=0; j < 2; j++) { |
1194 | { | ||
1195 | BN_GF2m_mod_mul(e, a, c, b[j], ctx); | 1167 | BN_GF2m_mod_mul(e, a, c, b[j], ctx); |
1196 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1168 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1197 | if (bp != NULL) | 1169 | if (bp != NULL) { |
1198 | { | 1170 | if (!results) { |
1199 | if (!results) | ||
1200 | { | ||
1201 | BN_print(bp,a); | 1171 | BN_print(bp,a); |
1202 | BIO_puts(bp," * "); | 1172 | BIO_puts(bp," * "); |
1203 | BN_print(bp,c); | 1173 | BN_print(bp,c); |
@@ -1206,8 +1176,8 @@ int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | |||
1206 | BIO_puts(bp," - "); | 1176 | BIO_puts(bp," - "); |
1207 | BN_print(bp,e); | 1177 | BN_print(bp,e); |
1208 | BIO_puts(bp,"\n"); | 1178 | BIO_puts(bp,"\n"); |
1209 | } | ||
1210 | } | 1179 | } |
1180 | } | ||
1211 | #endif | 1181 | #endif |
1212 | BN_GF2m_add(f, a, d); | 1182 | BN_GF2m_add(f, a, d); |
1213 | BN_GF2m_mod_mul(g, f, c, b[j], ctx); | 1183 | BN_GF2m_mod_mul(g, f, c, b[j], ctx); |
@@ -1215,15 +1185,14 @@ int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | |||
1215 | BN_GF2m_add(f, e, g); | 1185 | BN_GF2m_add(f, e, g); |
1216 | BN_GF2m_add(f, f, h); | 1186 | BN_GF2m_add(f, f, h); |
1217 | /* Test that (a+d)*c = a*c + d*c. */ | 1187 | /* Test that (a+d)*c = a*c + d*c. */ |
1218 | if(!BN_is_zero(f)) | 1188 | if(!BN_is_zero(f)) { |
1219 | { | ||
1220 | fprintf(stderr,"GF(2^m) modular multiplication test failed!\n"); | 1189 | fprintf(stderr,"GF(2^m) modular multiplication test failed!\n"); |
1221 | goto err; | 1190 | goto err; |
1222 | } | ||
1223 | } | 1191 | } |
1224 | } | 1192 | } |
1193 | } | ||
1225 | ret = 1; | 1194 | ret = 1; |
1226 | err: | 1195 | err: |
1227 | BN_free(a); | 1196 | BN_free(a); |
1228 | BN_free(b[0]); | 1197 | BN_free(b[0]); |
1229 | BN_free(b[1]); | 1198 | BN_free(b[1]); |
@@ -1234,10 +1203,11 @@ int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | |||
1234 | BN_free(g); | 1203 | BN_free(g); |
1235 | BN_free(h); | 1204 | BN_free(h); |
1236 | return ret; | 1205 | return ret; |
1237 | } | 1206 | } |
1238 | 1207 | ||
1239 | int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx) | 1208 | int |
1240 | { | 1209 | test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx) |
1210 | { | ||
1241 | BIGNUM *a,*b[2],*c,*d; | 1211 | BIGNUM *a,*b[2],*c,*d; |
1242 | int i, j, ret = 0; | 1212 | int i, j, ret = 0; |
1243 | int p0[] = {163,7,6,3,0,-1}; | 1213 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1252,19 +1222,15 @@ int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx) | |||
1252 | BN_GF2m_arr2poly(p0, b[0]); | 1222 | BN_GF2m_arr2poly(p0, b[0]); |
1253 | BN_GF2m_arr2poly(p1, b[1]); | 1223 | BN_GF2m_arr2poly(p1, b[1]); |
1254 | 1224 | ||
1255 | for (i=0; i<num0; i++) | 1225 | for (i=0; i<num0; i++) { |
1256 | { | ||
1257 | BN_bntest_rand(a, 1024, 0, 0); | 1226 | BN_bntest_rand(a, 1024, 0, 0); |
1258 | for (j=0; j < 2; j++) | 1227 | for (j=0; j < 2; j++) { |
1259 | { | ||
1260 | BN_GF2m_mod_sqr(c, a, b[j], ctx); | 1228 | BN_GF2m_mod_sqr(c, a, b[j], ctx); |
1261 | BN_copy(d, a); | 1229 | BN_copy(d, a); |
1262 | BN_GF2m_mod_mul(d, a, d, b[j], ctx); | 1230 | BN_GF2m_mod_mul(d, a, d, b[j], ctx); |
1263 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1231 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1264 | if (bp != NULL) | 1232 | if (bp != NULL) { |
1265 | { | 1233 | if (!results) { |
1266 | if (!results) | ||
1267 | { | ||
1268 | BN_print(bp,a); | 1234 | BN_print(bp,a); |
1269 | BIO_puts(bp," ^ 2 % "); | 1235 | BIO_puts(bp," ^ 2 % "); |
1270 | BN_print(bp,b[j]); | 1236 | BN_print(bp,b[j]); |
@@ -1273,30 +1239,30 @@ int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx) | |||
1273 | BIO_puts(bp,"; a * a = "); | 1239 | BIO_puts(bp,"; a * a = "); |
1274 | BN_print(bp,d); | 1240 | BN_print(bp,d); |
1275 | BIO_puts(bp,"\n"); | 1241 | BIO_puts(bp,"\n"); |
1276 | } | ||
1277 | } | 1242 | } |
1243 | } | ||
1278 | #endif | 1244 | #endif |
1279 | BN_GF2m_add(d, c, d); | 1245 | BN_GF2m_add(d, c, d); |
1280 | /* Test that a*a = a^2. */ | 1246 | /* Test that a*a = a^2. */ |
1281 | if(!BN_is_zero(d)) | 1247 | if(!BN_is_zero(d)) { |
1282 | { | ||
1283 | fprintf(stderr,"GF(2^m) modular squaring test failed!\n"); | 1248 | fprintf(stderr,"GF(2^m) modular squaring test failed!\n"); |
1284 | goto err; | 1249 | goto err; |
1285 | } | ||
1286 | } | 1250 | } |
1287 | } | 1251 | } |
1252 | } | ||
1288 | ret = 1; | 1253 | ret = 1; |
1289 | err: | 1254 | err: |
1290 | BN_free(a); | 1255 | BN_free(a); |
1291 | BN_free(b[0]); | 1256 | BN_free(b[0]); |
1292 | BN_free(b[1]); | 1257 | BN_free(b[1]); |
1293 | BN_free(c); | 1258 | BN_free(c); |
1294 | BN_free(d); | 1259 | BN_free(d); |
1295 | return ret; | 1260 | return ret; |
1296 | } | 1261 | } |
1297 | 1262 | ||
1298 | int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx) | 1263 | int |
1299 | { | 1264 | test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx) |
1265 | { | ||
1300 | BIGNUM *a,*b[2],*c,*d; | 1266 | BIGNUM *a,*b[2],*c,*d; |
1301 | int i, j, ret = 0; | 1267 | int i, j, ret = 0; |
1302 | int p0[] = {163,7,6,3,0,-1}; | 1268 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1311,47 +1277,43 @@ int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx) | |||
1311 | BN_GF2m_arr2poly(p0, b[0]); | 1277 | BN_GF2m_arr2poly(p0, b[0]); |
1312 | BN_GF2m_arr2poly(p1, b[1]); | 1278 | BN_GF2m_arr2poly(p1, b[1]); |
1313 | 1279 | ||
1314 | for (i=0; i<num0; i++) | 1280 | for (i=0; i<num0; i++) { |
1315 | { | ||
1316 | BN_bntest_rand(a, 512, 0, 0); | 1281 | BN_bntest_rand(a, 512, 0, 0); |
1317 | for (j=0; j < 2; j++) | 1282 | for (j=0; j < 2; j++) { |
1318 | { | ||
1319 | BN_GF2m_mod_inv(c, a, b[j], ctx); | 1283 | BN_GF2m_mod_inv(c, a, b[j], ctx); |
1320 | BN_GF2m_mod_mul(d, a, c, b[j], ctx); | 1284 | BN_GF2m_mod_mul(d, a, c, b[j], ctx); |
1321 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1285 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1322 | if (bp != NULL) | 1286 | if (bp != NULL) { |
1323 | { | 1287 | if (!results) { |
1324 | if (!results) | ||
1325 | { | ||
1326 | BN_print(bp,a); | 1288 | BN_print(bp,a); |
1327 | BIO_puts(bp, " * "); | 1289 | BIO_puts(bp, " * "); |
1328 | BN_print(bp,c); | 1290 | BN_print(bp,c); |
1329 | BIO_puts(bp," - 1 % "); | 1291 | BIO_puts(bp," - 1 % "); |
1330 | BN_print(bp,b[j]); | 1292 | BN_print(bp,b[j]); |
1331 | BIO_puts(bp,"\n"); | 1293 | BIO_puts(bp,"\n"); |
1332 | } | ||
1333 | } | 1294 | } |
1295 | } | ||
1334 | #endif | 1296 | #endif |
1335 | /* Test that ((1/a)*a) = 1. */ | 1297 | /* Test that ((1/a)*a) = 1. */ |
1336 | if(!BN_is_one(d)) | 1298 | if(!BN_is_one(d)) { |
1337 | { | ||
1338 | fprintf(stderr,"GF(2^m) modular inversion test failed!\n"); | 1299 | fprintf(stderr,"GF(2^m) modular inversion test failed!\n"); |
1339 | goto err; | 1300 | goto err; |
1340 | } | ||
1341 | } | 1301 | } |
1342 | } | 1302 | } |
1303 | } | ||
1343 | ret = 1; | 1304 | ret = 1; |
1344 | err: | 1305 | err: |
1345 | BN_free(a); | 1306 | BN_free(a); |
1346 | BN_free(b[0]); | 1307 | BN_free(b[0]); |
1347 | BN_free(b[1]); | 1308 | BN_free(b[1]); |
1348 | BN_free(c); | 1309 | BN_free(c); |
1349 | BN_free(d); | 1310 | BN_free(d); |
1350 | return ret; | 1311 | return ret; |
1351 | } | 1312 | } |
1352 | 1313 | ||
1353 | int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx) | 1314 | int |
1354 | { | 1315 | test_gf2m_mod_div(BIO *bp,BN_CTX *ctx) |
1316 | { | ||
1355 | BIGNUM *a,*b[2],*c,*d,*e,*f; | 1317 | BIGNUM *a,*b[2],*c,*d,*e,*f; |
1356 | int i, j, ret = 0; | 1318 | int i, j, ret = 0; |
1357 | int p0[] = {163,7,6,3,0,-1}; | 1319 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1368,20 +1330,16 @@ int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx) | |||
1368 | BN_GF2m_arr2poly(p0, b[0]); | 1330 | BN_GF2m_arr2poly(p0, b[0]); |
1369 | BN_GF2m_arr2poly(p1, b[1]); | 1331 | BN_GF2m_arr2poly(p1, b[1]); |
1370 | 1332 | ||
1371 | for (i=0; i<num0; i++) | 1333 | for (i=0; i<num0; i++) { |
1372 | { | ||
1373 | BN_bntest_rand(a, 512, 0, 0); | 1334 | BN_bntest_rand(a, 512, 0, 0); |
1374 | BN_bntest_rand(c, 512, 0, 0); | 1335 | BN_bntest_rand(c, 512, 0, 0); |
1375 | for (j=0; j < 2; j++) | 1336 | for (j=0; j < 2; j++) { |
1376 | { | ||
1377 | BN_GF2m_mod_div(d, a, c, b[j], ctx); | 1337 | BN_GF2m_mod_div(d, a, c, b[j], ctx); |
1378 | BN_GF2m_mod_mul(e, d, c, b[j], ctx); | 1338 | BN_GF2m_mod_mul(e, d, c, b[j], ctx); |
1379 | BN_GF2m_mod_div(f, a, e, b[j], ctx); | 1339 | BN_GF2m_mod_div(f, a, e, b[j], ctx); |
1380 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1340 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1381 | if (bp != NULL) | 1341 | if (bp != NULL) { |
1382 | { | 1342 | if (!results) { |
1383 | if (!results) | ||
1384 | { | ||
1385 | BN_print(bp,a); | 1343 | BN_print(bp,a); |
1386 | BIO_puts(bp, " = "); | 1344 | BIO_puts(bp, " = "); |
1387 | BN_print(bp,c); | 1345 | BN_print(bp,c); |
@@ -1390,19 +1348,18 @@ int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx) | |||
1390 | BIO_puts(bp, " % "); | 1348 | BIO_puts(bp, " % "); |
1391 | BN_print(bp,b[j]); | 1349 | BN_print(bp,b[j]); |
1392 | BIO_puts(bp,"\n"); | 1350 | BIO_puts(bp,"\n"); |
1393 | } | ||
1394 | } | 1351 | } |
1352 | } | ||
1395 | #endif | 1353 | #endif |
1396 | /* Test that ((a/c)*c)/a = 1. */ | 1354 | /* Test that ((a/c)*c)/a = 1. */ |
1397 | if(!BN_is_one(f)) | 1355 | if(!BN_is_one(f)) { |
1398 | { | ||
1399 | fprintf(stderr,"GF(2^m) modular division test failed!\n"); | 1356 | fprintf(stderr,"GF(2^m) modular division test failed!\n"); |
1400 | goto err; | 1357 | goto err; |
1401 | } | ||
1402 | } | 1358 | } |
1403 | } | 1359 | } |
1360 | } | ||
1404 | ret = 1; | 1361 | ret = 1; |
1405 | err: | 1362 | err: |
1406 | BN_free(a); | 1363 | BN_free(a); |
1407 | BN_free(b[0]); | 1364 | BN_free(b[0]); |
1408 | BN_free(b[1]); | 1365 | BN_free(b[1]); |
@@ -1411,10 +1368,11 @@ int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx) | |||
1411 | BN_free(e); | 1368 | BN_free(e); |
1412 | BN_free(f); | 1369 | BN_free(f); |
1413 | return ret; | 1370 | return ret; |
1414 | } | 1371 | } |
1415 | 1372 | ||
1416 | int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) | 1373 | int |
1417 | { | 1374 | test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) |
1375 | { | ||
1418 | BIGNUM *a,*b[2],*c,*d,*e,*f; | 1376 | BIGNUM *a,*b[2],*c,*d,*e,*f; |
1419 | int i, j, ret = 0; | 1377 | int i, j, ret = 0; |
1420 | int p0[] = {163,7,6,3,0,-1}; | 1378 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1431,23 +1389,19 @@ int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) | |||
1431 | BN_GF2m_arr2poly(p0, b[0]); | 1389 | BN_GF2m_arr2poly(p0, b[0]); |
1432 | BN_GF2m_arr2poly(p1, b[1]); | 1390 | BN_GF2m_arr2poly(p1, b[1]); |
1433 | 1391 | ||
1434 | for (i=0; i<num0; i++) | 1392 | for (i=0; i<num0; i++) { |
1435 | { | ||
1436 | BN_bntest_rand(a, 512, 0, 0); | 1393 | BN_bntest_rand(a, 512, 0, 0); |
1437 | BN_bntest_rand(c, 512, 0, 0); | 1394 | BN_bntest_rand(c, 512, 0, 0); |
1438 | BN_bntest_rand(d, 512, 0, 0); | 1395 | BN_bntest_rand(d, 512, 0, 0); |
1439 | for (j=0; j < 2; j++) | 1396 | for (j=0; j < 2; j++) { |
1440 | { | ||
1441 | BN_GF2m_mod_exp(e, a, c, b[j], ctx); | 1397 | BN_GF2m_mod_exp(e, a, c, b[j], ctx); |
1442 | BN_GF2m_mod_exp(f, a, d, b[j], ctx); | 1398 | BN_GF2m_mod_exp(f, a, d, b[j], ctx); |
1443 | BN_GF2m_mod_mul(e, e, f, b[j], ctx); | 1399 | BN_GF2m_mod_mul(e, e, f, b[j], ctx); |
1444 | BN_add(f, c, d); | 1400 | BN_add(f, c, d); |
1445 | BN_GF2m_mod_exp(f, a, f, b[j], ctx); | 1401 | BN_GF2m_mod_exp(f, a, f, b[j], ctx); |
1446 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1402 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1447 | if (bp != NULL) | 1403 | if (bp != NULL) { |
1448 | { | 1404 | if (!results) { |
1449 | if (!results) | ||
1450 | { | ||
1451 | BN_print(bp,a); | 1405 | BN_print(bp,a); |
1452 | BIO_puts(bp, " ^ ("); | 1406 | BIO_puts(bp, " ^ ("); |
1453 | BN_print(bp,c); | 1407 | BN_print(bp,c); |
@@ -1460,20 +1414,19 @@ int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) | |||
1460 | BIO_puts(bp, " % "); | 1414 | BIO_puts(bp, " % "); |
1461 | BN_print(bp,b[j]); | 1415 | BN_print(bp,b[j]); |
1462 | BIO_puts(bp,"\n"); | 1416 | BIO_puts(bp,"\n"); |
1463 | } | ||
1464 | } | 1417 | } |
1418 | } | ||
1465 | #endif | 1419 | #endif |
1466 | BN_GF2m_add(f, e, f); | 1420 | BN_GF2m_add(f, e, f); |
1467 | /* Test that a^(c+d)=a^c*a^d. */ | 1421 | /* Test that a^(c+d)=a^c*a^d. */ |
1468 | if(!BN_is_zero(f)) | 1422 | if(!BN_is_zero(f)) { |
1469 | { | ||
1470 | fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n"); | 1423 | fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n"); |
1471 | goto err; | 1424 | goto err; |
1472 | } | ||
1473 | } | 1425 | } |
1474 | } | 1426 | } |
1427 | } | ||
1475 | ret = 1; | 1428 | ret = 1; |
1476 | err: | 1429 | err: |
1477 | BN_free(a); | 1430 | BN_free(a); |
1478 | BN_free(b[0]); | 1431 | BN_free(b[0]); |
1479 | BN_free(b[1]); | 1432 | BN_free(b[1]); |
@@ -1482,10 +1435,11 @@ int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) | |||
1482 | BN_free(e); | 1435 | BN_free(e); |
1483 | BN_free(f); | 1436 | BN_free(f); |
1484 | return ret; | 1437 | return ret; |
1485 | } | 1438 | } |
1486 | 1439 | ||
1487 | int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx) | 1440 | int |
1488 | { | 1441 | test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx) |
1442 | { | ||
1489 | BIGNUM *a,*b[2],*c,*d,*e,*f; | 1443 | BIGNUM *a,*b[2],*c,*d,*e,*f; |
1490 | int i, j, ret = 0; | 1444 | int i, j, ret = 0; |
1491 | int p0[] = {163,7,6,3,0,-1}; | 1445 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1502,37 +1456,32 @@ int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx) | |||
1502 | BN_GF2m_arr2poly(p0, b[0]); | 1456 | BN_GF2m_arr2poly(p0, b[0]); |
1503 | BN_GF2m_arr2poly(p1, b[1]); | 1457 | BN_GF2m_arr2poly(p1, b[1]); |
1504 | 1458 | ||
1505 | for (i=0; i<num0; i++) | 1459 | for (i=0; i<num0; i++) { |
1506 | { | ||
1507 | BN_bntest_rand(a, 512, 0, 0); | 1460 | BN_bntest_rand(a, 512, 0, 0); |
1508 | for (j=0; j < 2; j++) | 1461 | for (j=0; j < 2; j++) { |
1509 | { | ||
1510 | BN_GF2m_mod(c, a, b[j]); | 1462 | BN_GF2m_mod(c, a, b[j]); |
1511 | BN_GF2m_mod_sqrt(d, a, b[j], ctx); | 1463 | BN_GF2m_mod_sqrt(d, a, b[j], ctx); |
1512 | BN_GF2m_mod_sqr(e, d, b[j], ctx); | 1464 | BN_GF2m_mod_sqr(e, d, b[j], ctx); |
1513 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1465 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1514 | if (bp != NULL) | 1466 | if (bp != NULL) { |
1515 | { | 1467 | if (!results) { |
1516 | if (!results) | ||
1517 | { | ||
1518 | BN_print(bp,d); | 1468 | BN_print(bp,d); |
1519 | BIO_puts(bp, " ^ 2 - "); | 1469 | BIO_puts(bp, " ^ 2 - "); |
1520 | BN_print(bp,a); | 1470 | BN_print(bp,a); |
1521 | BIO_puts(bp,"\n"); | 1471 | BIO_puts(bp,"\n"); |
1522 | } | ||
1523 | } | 1472 | } |
1473 | } | ||
1524 | #endif | 1474 | #endif |
1525 | BN_GF2m_add(f, c, e); | 1475 | BN_GF2m_add(f, c, e); |
1526 | /* Test that d^2 = a, where d = sqrt(a). */ | 1476 | /* Test that d^2 = a, where d = sqrt(a). */ |
1527 | if(!BN_is_zero(f)) | 1477 | if(!BN_is_zero(f)) { |
1528 | { | ||
1529 | fprintf(stderr,"GF(2^m) modular square root test failed!\n"); | 1478 | fprintf(stderr,"GF(2^m) modular square root test failed!\n"); |
1530 | goto err; | 1479 | goto err; |
1531 | } | ||
1532 | } | 1480 | } |
1533 | } | 1481 | } |
1482 | } | ||
1534 | ret = 1; | 1483 | ret = 1; |
1535 | err: | 1484 | err: |
1536 | BN_free(a); | 1485 | BN_free(a); |
1537 | BN_free(b[0]); | 1486 | BN_free(b[0]); |
1538 | BN_free(b[1]); | 1487 | BN_free(b[1]); |
@@ -1541,10 +1490,11 @@ int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx) | |||
1541 | BN_free(e); | 1490 | BN_free(e); |
1542 | BN_free(f); | 1491 | BN_free(f); |
1543 | return ret; | 1492 | return ret; |
1544 | } | 1493 | } |
1545 | 1494 | ||
1546 | int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | 1495 | int |
1547 | { | 1496 | test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) |
1497 | { | ||
1548 | BIGNUM *a,*b[2],*c,*d,*e; | 1498 | BIGNUM *a,*b[2],*c,*d,*e; |
1549 | int i, j, s = 0, t, ret = 0; | 1499 | int i, j, s = 0, t, ret = 0; |
1550 | int p0[] = {163,7,6,3,0,-1}; | 1500 | int p0[] = {163,7,6,3,0,-1}; |
@@ -1560,67 +1510,55 @@ int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | |||
1560 | BN_GF2m_arr2poly(p0, b[0]); | 1510 | BN_GF2m_arr2poly(p0, b[0]); |
1561 | BN_GF2m_arr2poly(p1, b[1]); | 1511 | BN_GF2m_arr2poly(p1, b[1]); |
1562 | 1512 | ||
1563 | for (i=0; i<num0; i++) | 1513 | for (i=0; i<num0; i++) { |
1564 | { | ||
1565 | BN_bntest_rand(a, 512, 0, 0); | 1514 | BN_bntest_rand(a, 512, 0, 0); |
1566 | for (j=0; j < 2; j++) | 1515 | for (j=0; j < 2; j++) { |
1567 | { | ||
1568 | t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx); | 1516 | t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx); |
1569 | if (t) | 1517 | if (t) { |
1570 | { | ||
1571 | s++; | 1518 | s++; |
1572 | BN_GF2m_mod_sqr(d, c, b[j], ctx); | 1519 | BN_GF2m_mod_sqr(d, c, b[j], ctx); |
1573 | BN_GF2m_add(d, c, d); | 1520 | BN_GF2m_add(d, c, d); |
1574 | BN_GF2m_mod(e, a, b[j]); | 1521 | BN_GF2m_mod(e, a, b[j]); |
1575 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1522 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1576 | if (bp != NULL) | 1523 | if (bp != NULL) { |
1577 | { | 1524 | if (!results) { |
1578 | if (!results) | ||
1579 | { | ||
1580 | BN_print(bp,c); | 1525 | BN_print(bp,c); |
1581 | BIO_puts(bp, " is root of z^2 + z = "); | 1526 | BIO_puts(bp, " is root of z^2 + z = "); |
1582 | BN_print(bp,a); | 1527 | BN_print(bp,a); |
1583 | BIO_puts(bp, " % "); | 1528 | BIO_puts(bp, " % "); |
1584 | BN_print(bp,b[j]); | 1529 | BN_print(bp,b[j]); |
1585 | BIO_puts(bp, "\n"); | 1530 | BIO_puts(bp, "\n"); |
1586 | } | ||
1587 | } | 1531 | } |
1532 | } | ||
1588 | #endif | 1533 | #endif |
1589 | BN_GF2m_add(e, e, d); | 1534 | BN_GF2m_add(e, e, d); |
1590 | /* Test that solution of quadratic c satisfies c^2 + c = a. */ | 1535 | /* Test that solution of quadratic c satisfies c^2 + c = a. */ |
1591 | if(!BN_is_zero(e)) | 1536 | if(!BN_is_zero(e)) { |
1592 | { | ||
1593 | fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n"); | 1537 | fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n"); |
1594 | goto err; | 1538 | goto err; |
1595 | } | ||
1596 | |||
1597 | } | 1539 | } |
1598 | else | 1540 | } else { |
1599 | { | ||
1600 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1541 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1601 | if (bp != NULL) | 1542 | if (bp != NULL) { |
1602 | { | 1543 | if (!results) { |
1603 | if (!results) | ||
1604 | { | ||
1605 | BIO_puts(bp, "There are no roots of z^2 + z = "); | 1544 | BIO_puts(bp, "There are no roots of z^2 + z = "); |
1606 | BN_print(bp,a); | 1545 | BN_print(bp,a); |
1607 | BIO_puts(bp, " % "); | 1546 | BIO_puts(bp, " % "); |
1608 | BN_print(bp,b[j]); | 1547 | BN_print(bp,b[j]); |
1609 | BIO_puts(bp, "\n"); | 1548 | BIO_puts(bp, "\n"); |
1610 | } | ||
1611 | } | 1549 | } |
1612 | #endif | ||
1613 | } | 1550 | } |
1551 | #endif | ||
1614 | } | 1552 | } |
1615 | } | 1553 | } |
1616 | if (s == 0) | 1554 | } |
1617 | { | 1555 | if (s == 0) { |
1618 | fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0); | 1556 | fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0); |
1619 | fprintf(stderr,"this is very unlikely and probably indicates an error.\n"); | 1557 | fprintf(stderr,"this is very unlikely and probably indicates an error.\n"); |
1620 | goto err; | 1558 | goto err; |
1621 | } | 1559 | } |
1622 | ret = 1; | 1560 | ret = 1; |
1623 | err: | 1561 | err: |
1624 | BN_free(a); | 1562 | BN_free(a); |
1625 | BN_free(b[0]); | 1563 | BN_free(b[0]); |
1626 | BN_free(b[1]); | 1564 | BN_free(b[1]); |
@@ -1628,10 +1566,12 @@ int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | |||
1628 | BN_free(d); | 1566 | BN_free(d); |
1629 | BN_free(e); | 1567 | BN_free(e); |
1630 | return ret; | 1568 | return ret; |
1631 | } | 1569 | } |
1632 | #endif | 1570 | #endif |
1633 | static int genprime_cb(int p, int n, BN_GENCB *arg) | 1571 | |
1634 | { | 1572 | static int |
1573 | genprime_cb(int p, int n, BN_GENCB *arg) | ||
1574 | { | ||
1635 | char c='*'; | 1575 | char c='*'; |
1636 | 1576 | ||
1637 | if (p == 0) c='.'; | 1577 | if (p == 0) c='.'; |
@@ -1641,10 +1581,11 @@ static int genprime_cb(int p, int n, BN_GENCB *arg) | |||
1641 | putc(c, stderr); | 1581 | putc(c, stderr); |
1642 | fflush(stderr); | 1582 | fflush(stderr); |
1643 | return 1; | 1583 | return 1; |
1644 | } | 1584 | } |
1645 | 1585 | ||
1646 | int test_kron(BIO *bp, BN_CTX *ctx) | 1586 | int |
1647 | { | 1587 | test_kron(BIO *bp, BN_CTX *ctx) |
1588 | { | ||
1648 | BN_GENCB cb; | 1589 | BN_GENCB cb; |
1649 | BIGNUM *a,*b,*r,*t; | 1590 | BIGNUM *a,*b,*r,*t; |
1650 | int i; | 1591 | int i; |
@@ -1672,8 +1613,7 @@ int test_kron(BIO *bp, BN_CTX *ctx) | |||
1672 | b->neg = rand_neg(); | 1613 | b->neg = rand_neg(); |
1673 | putc('\n', stderr); | 1614 | putc('\n', stderr); |
1674 | 1615 | ||
1675 | for (i = 0; i < num0; i++) | 1616 | for (i = 0; i < num0; i++) { |
1676 | { | ||
1677 | if (!BN_bntest_rand(a, 512, 0, 0)) goto err; | 1617 | if (!BN_bntest_rand(a, 512, 0, 0)) goto err; |
1678 | a->neg = rand_neg(); | 1618 | a->neg = rand_neg(); |
1679 | 1619 | ||
@@ -1692,16 +1632,14 @@ int test_kron(BIO *bp, BN_CTX *ctx) | |||
1692 | legendre = 1; | 1632 | legendre = 1; |
1693 | else if (BN_is_zero(r)) | 1633 | else if (BN_is_zero(r)) |
1694 | legendre = 0; | 1634 | legendre = 0; |
1695 | else | 1635 | else { |
1696 | { | ||
1697 | if (!BN_add_word(r, 1)) goto err; | 1636 | if (!BN_add_word(r, 1)) goto err; |
1698 | if (0 != BN_ucmp(r, b)) | 1637 | if (0 != BN_ucmp(r, b)) { |
1699 | { | ||
1700 | fprintf(stderr, "Legendre symbol computation failed\n"); | 1638 | fprintf(stderr, "Legendre symbol computation failed\n"); |
1701 | goto err; | 1639 | goto err; |
1702 | } | ||
1703 | legendre = -1; | ||
1704 | } | 1640 | } |
1641 | legendre = -1; | ||
1642 | } | ||
1705 | 1643 | ||
1706 | kronecker = BN_kronecker(a, b, ctx); | 1644 | kronecker = BN_kronecker(a, b, ctx); |
1707 | if (kronecker < -1) goto err; | 1645 | if (kronecker < -1) goto err; |
@@ -1709,33 +1647,33 @@ int test_kron(BIO *bp, BN_CTX *ctx) | |||
1709 | if (a->neg && b->neg) | 1647 | if (a->neg && b->neg) |
1710 | kronecker = -kronecker; | 1648 | kronecker = -kronecker; |
1711 | 1649 | ||
1712 | if (legendre != kronecker) | 1650 | if (legendre != kronecker) { |
1713 | { | ||
1714 | fprintf(stderr, "legendre != kronecker; a = "); | 1651 | fprintf(stderr, "legendre != kronecker; a = "); |
1715 | BN_print_fp(stderr, a); | 1652 | BN_print_fp(stderr, a); |
1716 | fprintf(stderr, ", b = "); | 1653 | fprintf(stderr, ", b = "); |
1717 | BN_print_fp(stderr, b); | 1654 | BN_print_fp(stderr, b); |
1718 | fprintf(stderr, "\n"); | 1655 | fprintf(stderr, "\n"); |
1719 | goto err; | 1656 | goto err; |
1720 | } | 1657 | } |
1721 | 1658 | ||
1722 | putc('.', stderr); | 1659 | putc('.', stderr); |
1723 | fflush(stderr); | 1660 | fflush(stderr); |
1724 | } | 1661 | } |
1725 | 1662 | ||
1726 | putc('\n', stderr); | 1663 | putc('\n', stderr); |
1727 | fflush(stderr); | 1664 | fflush(stderr); |
1728 | ret = 1; | 1665 | ret = 1; |
1729 | err: | 1666 | err: |
1730 | if (a != NULL) BN_free(a); | 1667 | if (a != NULL) BN_free(a); |
1731 | if (b != NULL) BN_free(b); | 1668 | if (b != NULL) BN_free(b); |
1732 | if (r != NULL) BN_free(r); | 1669 | if (r != NULL) BN_free(r); |
1733 | if (t != NULL) BN_free(t); | 1670 | if (t != NULL) BN_free(t); |
1734 | return ret; | 1671 | return ret; |
1735 | } | 1672 | } |
1736 | 1673 | ||
1737 | int test_sqrt(BIO *bp, BN_CTX *ctx) | 1674 | int |
1738 | { | 1675 | test_sqrt(BIO *bp, BN_CTX *ctx) |
1676 | { | ||
1739 | BN_GENCB cb; | 1677 | BN_GENCB cb; |
1740 | BIGNUM *a,*p,*r; | 1678 | BIGNUM *a,*p,*r; |
1741 | int i, j; | 1679 | int i, j; |
@@ -1748,26 +1686,21 @@ int test_sqrt(BIO *bp, BN_CTX *ctx) | |||
1748 | 1686 | ||
1749 | BN_GENCB_set(&cb, genprime_cb, NULL); | 1687 | BN_GENCB_set(&cb, genprime_cb, NULL); |
1750 | 1688 | ||
1751 | for (i = 0; i < 16; i++) | 1689 | for (i = 0; i < 16; i++) { |
1752 | { | 1690 | if (i < 8) { |
1753 | if (i < 8) | ||
1754 | { | ||
1755 | unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 }; | 1691 | unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 }; |
1756 | 1692 | ||
1757 | if (!BN_set_word(p, primes[i])) goto err; | 1693 | if (!BN_set_word(p, primes[i])) goto err; |
1758 | } | 1694 | } else { |
1759 | else | ||
1760 | { | ||
1761 | if (!BN_set_word(a, 32)) goto err; | 1695 | if (!BN_set_word(a, 32)) goto err; |
1762 | if (!BN_set_word(r, 2*i + 1)) goto err; | 1696 | if (!BN_set_word(r, 2*i + 1)) goto err; |
1763 | 1697 | ||
1764 | if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err; | 1698 | if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err; |
1765 | putc('\n', stderr); | 1699 | putc('\n', stderr); |
1766 | } | 1700 | } |
1767 | p->neg = rand_neg(); | 1701 | p->neg = rand_neg(); |
1768 | 1702 | ||
1769 | for (j = 0; j < num2; j++) | 1703 | for (j = 0; j < num2; j++) { |
1770 | { | ||
1771 | /* construct 'a' such that it is a square modulo p, | 1704 | /* construct 'a' such that it is a square modulo p, |
1772 | * but in general not a proper square and not reduced modulo p */ | 1705 | * but in general not a proper square and not reduced modulo p */ |
1773 | if (!BN_bntest_rand(r, 256, 0, 3)) goto err; | 1706 | if (!BN_bntest_rand(r, 256, 0, 3)) goto err; |
@@ -1785,8 +1718,7 @@ int test_sqrt(BIO *bp, BN_CTX *ctx) | |||
1785 | 1718 | ||
1786 | if (!BN_nnmod(a, a, p, ctx)) goto err; | 1719 | if (!BN_nnmod(a, a, p, ctx)) goto err; |
1787 | 1720 | ||
1788 | if (BN_cmp(a, r) != 0) | 1721 | if (BN_cmp(a, r) != 0) { |
1789 | { | ||
1790 | fprintf(stderr, "BN_mod_sqrt failed: a = "); | 1722 | fprintf(stderr, "BN_mod_sqrt failed: a = "); |
1791 | BN_print_fp(stderr, a); | 1723 | BN_print_fp(stderr, a); |
1792 | fprintf(stderr, ", r = "); | 1724 | fprintf(stderr, ", r = "); |
@@ -1795,27 +1727,29 @@ int test_sqrt(BIO *bp, BN_CTX *ctx) | |||
1795 | BN_print_fp(stderr, p); | 1727 | BN_print_fp(stderr, p); |
1796 | fprintf(stderr, "\n"); | 1728 | fprintf(stderr, "\n"); |
1797 | goto err; | 1729 | goto err; |
1798 | } | 1730 | } |
1799 | 1731 | ||
1800 | putc('.', stderr); | 1732 | putc('.', stderr); |
1801 | fflush(stderr); | 1733 | fflush(stderr); |
1802 | } | 1734 | } |
1803 | 1735 | ||
1804 | putc('\n', stderr); | 1736 | putc('\n', stderr); |
1805 | fflush(stderr); | 1737 | fflush(stderr); |
1806 | } | 1738 | } |
1807 | ret = 1; | 1739 | ret = 1; |
1808 | err: | 1740 | err: |
1809 | if (a != NULL) BN_free(a); | 1741 | if (a != NULL) BN_free(a); |
1810 | if (p != NULL) BN_free(p); | 1742 | if (p != NULL) BN_free(p); |
1811 | if (r != NULL) BN_free(r); | 1743 | if (r != NULL) BN_free(r); |
1812 | return ret; | 1744 | return ret; |
1813 | } | 1745 | } |
1814 | 1746 | ||
1815 | int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) | 1747 | int |
1816 | { | 1748 | test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) |
1749 | { | ||
1817 | BIGNUM *a,*b,*c,*d; | 1750 | BIGNUM *a,*b,*c,*d; |
1818 | int i; | 1751 | int i; |
1752 | int rc = 1; | ||
1819 | 1753 | ||
1820 | b=BN_new(); | 1754 | b=BN_new(); |
1821 | c=BN_new(); | 1755 | c=BN_new(); |
@@ -1824,32 +1758,27 @@ int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) | |||
1824 | 1758 | ||
1825 | if(a_) | 1759 | if(a_) |
1826 | a=a_; | 1760 | a=a_; |
1827 | else | 1761 | else { |
1828 | { | ||
1829 | a=BN_new(); | 1762 | a=BN_new(); |
1830 | BN_bntest_rand(a,200,0,0); /**/ | 1763 | BN_bntest_rand(a,200,0,0); /**/ |
1831 | a->neg=rand_neg(); | 1764 | a->neg=rand_neg(); |
1832 | } | 1765 | } |
1833 | for (i=0; i<num0; i++) | 1766 | for (i=0; i<num0; i++) { |
1834 | { | ||
1835 | BN_lshift(b,a,i+1); | 1767 | BN_lshift(b,a,i+1); |
1836 | BN_add(c,c,c); | 1768 | BN_add(c,c,c); |
1837 | if (bp != NULL) | 1769 | if (bp != NULL) { |
1838 | { | 1770 | if (!results) { |
1839 | if (!results) | ||
1840 | { | ||
1841 | BN_print(bp,a); | 1771 | BN_print(bp,a); |
1842 | BIO_puts(bp," * "); | 1772 | BIO_puts(bp," * "); |
1843 | BN_print(bp,c); | 1773 | BN_print(bp,c); |
1844 | BIO_puts(bp," - "); | 1774 | BIO_puts(bp," - "); |
1845 | } | 1775 | } |
1846 | BN_print(bp,b); | 1776 | BN_print(bp,b); |
1847 | BIO_puts(bp,"\n"); | 1777 | BIO_puts(bp,"\n"); |
1848 | } | 1778 | } |
1849 | BN_mul(d,a,c,ctx); | 1779 | BN_mul(d,a,c,ctx); |
1850 | BN_sub(d,d,b); | 1780 | BN_sub(d,d,b); |
1851 | if(!BN_is_zero(d)) | 1781 | if(!BN_is_zero(d)) { |
1852 | { | ||
1853 | fprintf(stderr,"Left shift test failed!\n"); | 1782 | fprintf(stderr,"Left shift test failed!\n"); |
1854 | fprintf(stderr,"a="); | 1783 | fprintf(stderr,"a="); |
1855 | BN_print_fp(stderr,a); | 1784 | BN_print_fp(stderr,a); |
@@ -1860,20 +1789,23 @@ int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) | |||
1860 | fprintf(stderr,"\nd="); | 1789 | fprintf(stderr,"\nd="); |
1861 | BN_print_fp(stderr,d); | 1790 | BN_print_fp(stderr,d); |
1862 | fprintf(stderr,"\n"); | 1791 | fprintf(stderr,"\n"); |
1863 | return 0; | 1792 | rc = 0; |
1864 | } | 1793 | break; |
1865 | } | 1794 | } |
1795 | } | ||
1866 | BN_free(a); | 1796 | BN_free(a); |
1867 | BN_free(b); | 1797 | BN_free(b); |
1868 | BN_free(c); | 1798 | BN_free(c); |
1869 | BN_free(d); | 1799 | BN_free(d); |
1870 | return(1); | 1800 | return(rc); |
1871 | } | 1801 | } |
1872 | 1802 | ||
1873 | int test_lshift1(BIO *bp) | 1803 | int |
1874 | { | 1804 | test_lshift1(BIO *bp) |
1805 | { | ||
1875 | BIGNUM *a,*b,*c; | 1806 | BIGNUM *a,*b,*c; |
1876 | int i; | 1807 | int i; |
1808 | int rc = 1; | ||
1877 | 1809 | ||
1878 | a=BN_new(); | 1810 | a=BN_new(); |
1879 | b=BN_new(); | 1811 | b=BN_new(); |
@@ -1881,40 +1813,39 @@ int test_lshift1(BIO *bp) | |||
1881 | 1813 | ||
1882 | BN_bntest_rand(a,200,0,0); /**/ | 1814 | BN_bntest_rand(a,200,0,0); /**/ |
1883 | a->neg=rand_neg(); | 1815 | a->neg=rand_neg(); |
1884 | for (i=0; i<num0; i++) | 1816 | for (i=0; i<num0; i++) { |
1885 | { | ||
1886 | BN_lshift1(b,a); | 1817 | BN_lshift1(b,a); |
1887 | if (bp != NULL) | 1818 | if (bp != NULL) { |
1888 | { | 1819 | if (!results) { |
1889 | if (!results) | ||
1890 | { | ||
1891 | BN_print(bp,a); | 1820 | BN_print(bp,a); |
1892 | BIO_puts(bp," * 2"); | 1821 | BIO_puts(bp," * 2"); |
1893 | BIO_puts(bp," - "); | 1822 | BIO_puts(bp," - "); |
1894 | } | 1823 | } |
1895 | BN_print(bp,b); | 1824 | BN_print(bp,b); |
1896 | BIO_puts(bp,"\n"); | 1825 | BIO_puts(bp,"\n"); |
1897 | } | 1826 | } |
1898 | BN_add(c,a,a); | 1827 | BN_add(c,a,a); |
1899 | BN_sub(a,b,c); | 1828 | BN_sub(a,b,c); |
1900 | if(!BN_is_zero(a)) | 1829 | if(!BN_is_zero(a)) { |
1901 | { | ||
1902 | fprintf(stderr,"Left shift one test failed!\n"); | 1830 | fprintf(stderr,"Left shift one test failed!\n"); |
1903 | return 0; | 1831 | rc = 0; |
1904 | } | 1832 | break; |
1833 | } | ||
1905 | 1834 | ||
1906 | BN_copy(a,b); | 1835 | BN_copy(a,b); |
1907 | } | 1836 | } |
1908 | BN_free(a); | 1837 | BN_free(a); |
1909 | BN_free(b); | 1838 | BN_free(b); |
1910 | BN_free(c); | 1839 | BN_free(c); |
1911 | return(1); | 1840 | return(rc); |
1912 | } | 1841 | } |
1913 | 1842 | ||
1914 | int test_rshift(BIO *bp,BN_CTX *ctx) | 1843 | int |
1915 | { | 1844 | test_rshift(BIO *bp,BN_CTX *ctx) |
1845 | { | ||
1916 | BIGNUM *a,*b,*c,*d,*e; | 1846 | BIGNUM *a,*b,*c,*d,*e; |
1917 | int i; | 1847 | int i; |
1848 | int rc = 1; | ||
1918 | 1849 | ||
1919 | a=BN_new(); | 1850 | a=BN_new(); |
1920 | b=BN_new(); | 1851 | b=BN_new(); |
@@ -1925,42 +1856,41 @@ int test_rshift(BIO *bp,BN_CTX *ctx) | |||
1925 | 1856 | ||
1926 | BN_bntest_rand(a,200,0,0); /**/ | 1857 | BN_bntest_rand(a,200,0,0); /**/ |
1927 | a->neg=rand_neg(); | 1858 | a->neg=rand_neg(); |
1928 | for (i=0; i<num0; i++) | 1859 | for (i=0; i<num0; i++) { |
1929 | { | ||
1930 | BN_rshift(b,a,i+1); | 1860 | BN_rshift(b,a,i+1); |
1931 | BN_add(c,c,c); | 1861 | BN_add(c,c,c); |
1932 | if (bp != NULL) | 1862 | if (bp != NULL) { |
1933 | { | 1863 | if (!results) { |
1934 | if (!results) | ||
1935 | { | ||
1936 | BN_print(bp,a); | 1864 | BN_print(bp,a); |
1937 | BIO_puts(bp," / "); | 1865 | BIO_puts(bp," / "); |
1938 | BN_print(bp,c); | 1866 | BN_print(bp,c); |
1939 | BIO_puts(bp," - "); | 1867 | BIO_puts(bp," - "); |
1940 | } | 1868 | } |
1941 | BN_print(bp,b); | 1869 | BN_print(bp,b); |
1942 | BIO_puts(bp,"\n"); | 1870 | BIO_puts(bp,"\n"); |
1943 | } | 1871 | } |
1944 | BN_div(d,e,a,c,ctx); | 1872 | BN_div(d,e,a,c,ctx); |
1945 | BN_sub(d,d,b); | 1873 | BN_sub(d,d,b); |
1946 | if(!BN_is_zero(d)) | 1874 | if(!BN_is_zero(d)) { |
1947 | { | ||
1948 | fprintf(stderr,"Right shift test failed!\n"); | 1875 | fprintf(stderr,"Right shift test failed!\n"); |
1949 | return 0; | 1876 | rc = 0; |
1950 | } | 1877 | break; |
1951 | } | 1878 | } |
1879 | } | ||
1952 | BN_free(a); | 1880 | BN_free(a); |
1953 | BN_free(b); | 1881 | BN_free(b); |
1954 | BN_free(c); | 1882 | BN_free(c); |
1955 | BN_free(d); | 1883 | BN_free(d); |
1956 | BN_free(e); | 1884 | BN_free(e); |
1957 | return(1); | 1885 | return(rc); |
1958 | } | 1886 | } |
1959 | 1887 | ||
1960 | int test_rshift1(BIO *bp) | 1888 | int |
1961 | { | 1889 | test_rshift1(BIO *bp) |
1890 | { | ||
1962 | BIGNUM *a,*b,*c; | 1891 | BIGNUM *a,*b,*c; |
1963 | int i; | 1892 | int i; |
1893 | int rc = 1; | ||
1964 | 1894 | ||
1965 | a=BN_new(); | 1895 | a=BN_new(); |
1966 | b=BN_new(); | 1896 | b=BN_new(); |
@@ -1968,42 +1898,40 @@ int test_rshift1(BIO *bp) | |||
1968 | 1898 | ||
1969 | BN_bntest_rand(a,200,0,0); /**/ | 1899 | BN_bntest_rand(a,200,0,0); /**/ |
1970 | a->neg=rand_neg(); | 1900 | a->neg=rand_neg(); |
1971 | for (i=0; i<num0; i++) | 1901 | for (i=0; i<num0; i++) { |
1972 | { | ||
1973 | BN_rshift1(b,a); | 1902 | BN_rshift1(b,a); |
1974 | if (bp != NULL) | 1903 | if (bp != NULL) { |
1975 | { | 1904 | if (!results) { |
1976 | if (!results) | ||
1977 | { | ||
1978 | BN_print(bp,a); | 1905 | BN_print(bp,a); |
1979 | BIO_puts(bp," / 2"); | 1906 | BIO_puts(bp," / 2"); |
1980 | BIO_puts(bp," - "); | 1907 | BIO_puts(bp," - "); |
1981 | } | 1908 | } |
1982 | BN_print(bp,b); | 1909 | BN_print(bp,b); |
1983 | BIO_puts(bp,"\n"); | 1910 | BIO_puts(bp,"\n"); |
1984 | } | 1911 | } |
1985 | BN_sub(c,a,b); | 1912 | BN_sub(c,a,b); |
1986 | BN_sub(c,c,b); | 1913 | BN_sub(c,c,b); |
1987 | if(!BN_is_zero(c) && !BN_abs_is_word(c, 1)) | 1914 | if(!BN_is_zero(c) && !BN_abs_is_word(c, 1)) { |
1988 | { | ||
1989 | fprintf(stderr,"Right shift one test failed!\n"); | 1915 | fprintf(stderr,"Right shift one test failed!\n"); |
1990 | return 0; | 1916 | rc = 0; |
1991 | } | 1917 | break; |
1992 | BN_copy(a,b); | ||
1993 | } | 1918 | } |
1919 | BN_copy(a,b); | ||
1920 | } | ||
1994 | BN_free(a); | 1921 | BN_free(a); |
1995 | BN_free(b); | 1922 | BN_free(b); |
1996 | BN_free(c); | 1923 | BN_free(c); |
1997 | return(1); | 1924 | return(rc); |
1998 | } | 1925 | } |
1999 | 1926 | ||
2000 | int rand_neg(void) | 1927 | int |
2001 | { | 1928 | rand_neg(void) |
1929 | { | ||
2002 | static unsigned int neg=0; | 1930 | static unsigned int neg=0; |
2003 | static int sign[8]={0,0,0,1,1,0,1,1}; | 1931 | static int sign[8]={0,0,0,1,1,0,1,1}; |
2004 | 1932 | ||
2005 | return(sign[(neg++)%8]); | 1933 | return(sign[(neg++)%8]); |
2006 | } | 1934 | } |
2007 | 1935 | ||
2008 | int | 1936 | int |
2009 | test_mod_exp_sizes(BIO *bp, BN_CTX *ctx) | 1937 | test_mod_exp_sizes(BIO *bp, BN_CTX *ctx) |