summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
authortb <>2021-11-18 14:59:44 +0000
committertb <>2021-11-18 14:59:44 +0000
commit76475d10a90dc31c5598bd1f9346a6d20b67d45f (patch)
tree684d099ba736e5df380e1e5c991e5aae248c13f0 /src/regress/lib
parent2a87395eaec3a79a5bc43d49a99f03caeb8235b3 (diff)
downloadopenbsd-76475d10a90dc31c5598bd1f9346a6d20b67d45f.tar.gz
openbsd-76475d10a90dc31c5598bd1f9346a6d20b67d45f.tar.bz2
openbsd-76475d10a90dc31c5598bd1f9346a6d20b67d45f.zip
bntest: Fix all but one test in this file to work with opaque BN.
The remaining test needs some thinking (or disabling once we flip the switch). It is currently marked with an XXX.
Diffstat (limited to 'src/regress/lib')
-rw-r--r--src/regress/lib/libcrypto/bn/general/bntest.c962
1 files changed, 563 insertions, 399 deletions
diff --git a/src/regress/lib/libcrypto/bn/general/bntest.c b/src/regress/lib/libcrypto/bn/general/bntest.c
index 138b7673e6..b9d2c296e6 100644
--- a/src/regress/lib/libcrypto/bn/general/bntest.c
+++ b/src/regress/lib/libcrypto/bn/general/bntest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bntest.c,v 1.21 2019/09/05 00:59:36 bluhm Exp $ */ 1/* $OpenBSD: bntest.c,v 1.22 2021/11/18 14:59:44 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -359,7 +359,7 @@ main(int argc, char *argv[])
359 BIO_free(out); 359 BIO_free(out);
360 360
361 exit(0); 361 exit(0);
362err: 362 err:
363 BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc notices 363 BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc notices
364 * the failure, see test_bn in test/Makefile.ssl*/ 364 * the failure, see test_bn in test/Makefile.ssl*/
365 365
@@ -372,164 +372,177 @@ err:
372int 372int
373test_add(BIO *bp) 373test_add(BIO *bp)
374{ 374{
375 BIGNUM a, b, c; 375 BIGNUM *a = NULL, *b = NULL, *c = NULL;
376 int i; 376 int i;
377 int rc = 1; 377 int rc = 1;
378 378
379 BN_init(&a); 379 if ((a = BN_new()) == NULL)
380 BN_init(&b); 380 goto err;
381 BN_init(&c); 381 if ((b = BN_new()) == NULL)
382 goto err;
383 if ((c = BN_new()) == NULL)
384 goto err;
382 385
383 CHECK_GOTO(BN_bntest_rand(&a, 512, 0, 0)); 386 CHECK_GOTO(BN_bntest_rand(a, 512, 0, 0));
384 for (i = 0; i < num0; i++) { 387 for (i = 0; i < num0; i++) {
385 CHECK_GOTO(BN_bntest_rand(&b, 450 + i, 0, 0)); 388 CHECK_GOTO(BN_bntest_rand(b, 450 + i, 0, 0));
386 a.neg = rand_neg(); 389 BN_set_negative(a, rand_neg());
387 b.neg = rand_neg(); 390 BN_set_negative(b, rand_neg());
388 CHECK_GOTO(BN_add(&c, &a, &b)); 391 CHECK_GOTO(BN_add(c, a, b));
389 if (bp != NULL) { 392 if (bp != NULL) {
390 if (!results) { 393 if (!results) {
391 CHECK_GOTO(BN_print(bp, &a)); 394 CHECK_GOTO(BN_print(bp, a));
392 BIO_puts(bp, " + "); 395 BIO_puts(bp, " + ");
393 CHECK_GOTO(BN_print(bp, &b)); 396 CHECK_GOTO(BN_print(bp, b));
394 BIO_puts(bp, " - "); 397 BIO_puts(bp, " - ");
395 } 398 }
396 CHECK_GOTO(BN_print(bp, &c)); 399 CHECK_GOTO(BN_print(bp, c));
397 BIO_puts(bp, "\n"); 400 BIO_puts(bp, "\n");
398 } 401 }
399 a.neg = !a.neg; 402 BN_set_negative(a, !BN_is_negative(a));
400 b.neg = !b.neg; 403 BN_set_negative(b, !BN_is_negative(b));
401 CHECK_GOTO(BN_add(&c, &c, &b)); 404 CHECK_GOTO(BN_add(c, c, b));
402 CHECK_GOTO(BN_add(&c, &c, &a)); 405 CHECK_GOTO(BN_add(c, c, a));
403 if (!BN_is_zero(&c)) { 406 if (!BN_is_zero(c)) {
404 fprintf(stderr, "Add test failed!\n"); 407 fprintf(stderr, "Add test failed!\n");
405 rc = 0; 408 rc = 0;
406 break; 409 break;
407 } 410 }
408 } 411 }
409err: 412
410 BN_free(&a); 413 err:
411 BN_free(&b); 414 BN_free(a);
412 BN_free(&c); 415 BN_free(b);
413 return (rc); 416 BN_free(c);
417
418 return rc;
414} 419}
415 420
416int 421int
417test_sub(BIO *bp) 422test_sub(BIO *bp)
418{ 423{
419 BIGNUM a, b, c; 424 BIGNUM *a = NULL, *b = NULL, *c = NULL;
420 int i; 425 int i;
421 int rc = 1; 426 int rc = 1;
422 427
423 BN_init(&a); 428 if ((a = BN_new()) == NULL)
424 BN_init(&b); 429 goto err;
425 BN_init(&c); 430 if ((b = BN_new()) == NULL)
431 goto err;
432 if ((c = BN_new()) == NULL)
433 goto err;
426 434
427 for (i = 0; i < num0 + num1; i++) { 435 for (i = 0; i < num0 + num1; i++) {
428 if (i < num1) { 436 if (i < num1) {
429 CHECK_GOTO(BN_bntest_rand(&a, 512, 0, 0)); 437 CHECK_GOTO(BN_bntest_rand(a, 512, 0, 0));
430 CHECK_GOTO(BN_copy(&b, &a)); 438 CHECK_GOTO(BN_copy(b, a));
431 if (BN_set_bit(&a, i) == 0) { 439 if (BN_set_bit(a, i) == 0) {
432 rc = 0; 440 rc = 0;
433 break; 441 break;
434 } 442 }
435 CHECK_GOTO(BN_add_word(&b, i)); 443 CHECK_GOTO(BN_add_word(b, i));
436 } else { 444 } else {
437 CHECK_GOTO(BN_bntest_rand(&b, 400 + i - num1, 0, 0)); 445 CHECK_GOTO(BN_bntest_rand(b, 400 + i - num1, 0, 0));
438 a.neg = rand_neg(); 446 BN_set_negative(a, rand_neg());
439 b.neg = rand_neg(); 447 BN_set_negative(b, rand_neg());
440 } 448 }
441 CHECK_GOTO(BN_sub(&c, &a, &b)); 449 CHECK_GOTO(BN_sub(c, a, b));
442 if (bp != NULL) { 450 if (bp != NULL) {
443 if (!results) { 451 if (!results) {
444 CHECK_GOTO(BN_print(bp, &a)); 452 CHECK_GOTO(BN_print(bp, a));
445 BIO_puts(bp, " - "); 453 BIO_puts(bp, " - ");
446 CHECK_GOTO(BN_print(bp, &b)); 454 CHECK_GOTO(BN_print(bp, b));
447 BIO_puts(bp, " - "); 455 BIO_puts(bp, " - ");
448 } 456 }
449 CHECK_GOTO(BN_print(bp, &c)); 457 CHECK_GOTO(BN_print(bp, c));
450 BIO_puts(bp, "\n"); 458 BIO_puts(bp, "\n");
451 } 459 }
452 CHECK_GOTO(BN_add(&c, &c, &b)); 460 CHECK_GOTO(BN_add(c, c, b));
453 CHECK_GOTO(BN_sub(&c, &c, &a)); 461 CHECK_GOTO(BN_sub(c, c, a));
454 if (!BN_is_zero(&c)) { 462 if (!BN_is_zero(c)) {
455 fprintf(stderr, "Subtract test failed!\n"); 463 fprintf(stderr, "Subtract test failed!\n");
456 rc = 0; 464 rc = 0;
457 break; 465 break;
458 } 466 }
459 } 467 }
460err: 468 err:
461 BN_free(&a); 469 BN_free(a);
462 BN_free(&b); 470 BN_free(b);
463 BN_free(&c); 471 BN_free(c);
464 return (rc); 472 return (rc);
465} 473}
466 474
467int 475int
468test_div(BIO *bp, BN_CTX *ctx) 476test_div(BIO *bp, BN_CTX *ctx)
469{ 477{
470 BIGNUM a, b, c, d, e; 478 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
471 int i; 479 int i;
472 int rc = 1; 480 int rc = 1;
473 481
474 BN_init(&a); 482 if ((a = BN_new()) == NULL)
475 BN_init(&b); 483 goto err;
476 BN_init(&c); 484 if ((b = BN_new()) == NULL)
477 BN_init(&d); 485 goto err;
478 BN_init(&e); 486 if ((c = BN_new()) == NULL)
487 goto err;
488 if ((d = BN_new()) == NULL)
489 goto err;
490 if ((e = BN_new()) == NULL)
491 goto err;
479 492
480 CHECK_GOTO(BN_one(&a)); 493 CHECK_GOTO(BN_one(a));
481 CHECK_GOTO(BN_zero(&b)); 494 CHECK_GOTO(BN_zero(b));
482 495
483 if (BN_div(&d, &c, &a, &b, ctx)) { 496 if (BN_div(d, c, a, b, ctx)) {
484 fprintf(stderr, "Division by zero succeeded!\n"); 497 fprintf(stderr, "Division by zero succeeded!\n");
485 return (0); 498 return (0);
486 } 499 }
487 500
488 for (i = 0; i < num0 + num1; i++) { 501 for (i = 0; i < num0 + num1; i++) {
489 if (i < num1) { 502 if (i < num1) {
490 CHECK_GOTO(BN_bntest_rand(&a, 400, 0, 0)); 503 CHECK_GOTO(BN_bntest_rand(a, 400, 0, 0));
491 CHECK_GOTO(BN_copy(&b, &a)); 504 CHECK_GOTO(BN_copy(b, a));
492 CHECK_GOTO(BN_lshift(&a, &a, i)); 505 CHECK_GOTO(BN_lshift(a, a, i));
493 CHECK_GOTO(BN_add_word(&a, i)); 506 CHECK_GOTO(BN_add_word(a, i));
494 } else 507 } else
495 CHECK_GOTO(BN_bntest_rand(&b, 50 + 3 * (i - num1), 0, 0)); 508 CHECK_GOTO(BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0));
496 a.neg = rand_neg(); 509 BN_set_negative(a, rand_neg());
497 b.neg = rand_neg(); 510 BN_set_negative(b, rand_neg());
498 CHECK_GOTO(BN_div(&d, &c, &a, &b, ctx)); 511 CHECK_GOTO(BN_div(d, c, a, b, ctx));
499 if (bp != NULL) { 512 if (bp != NULL) {
500 if (!results) { 513 if (!results) {
501 CHECK_GOTO(BN_print(bp, &a)); 514 CHECK_GOTO(BN_print(bp, a));
502 BIO_puts(bp, " / "); 515 BIO_puts(bp, " / ");
503 CHECK_GOTO(BN_print(bp, &b)); 516 CHECK_GOTO(BN_print(bp, b));
504 BIO_puts(bp, " - "); 517 BIO_puts(bp, " - ");
505 } 518 }
506 CHECK_GOTO(BN_print(bp, &d)); 519 CHECK_GOTO(BN_print(bp, d));
507 BIO_puts(bp, "\n"); 520 BIO_puts(bp, "\n");
508 521
509 if (!results) { 522 if (!results) {
510 CHECK_GOTO(BN_print(bp, &a)); 523 CHECK_GOTO(BN_print(bp, a));
511 BIO_puts(bp, " % "); 524 BIO_puts(bp, " % ");
512 CHECK_GOTO(BN_print(bp, &b)); 525 CHECK_GOTO(BN_print(bp, b));
513 BIO_puts(bp, " - "); 526 BIO_puts(bp, " - ");
514 } 527 }
515 CHECK_GOTO(BN_print(bp, &c)); 528 CHECK_GOTO(BN_print(bp, c));
516 BIO_puts(bp, "\n"); 529 BIO_puts(bp, "\n");
517 } 530 }
518 CHECK_GOTO(BN_mul(&e, &d, &b, ctx)); 531 CHECK_GOTO(BN_mul(e, d, b, ctx));
519 CHECK_GOTO(BN_add(&d, &e, &c)); 532 CHECK_GOTO(BN_add(d, e, c));
520 CHECK_GOTO(BN_sub(&d, &d, &a)); 533 CHECK_GOTO(BN_sub(d, d, a));
521 if (!BN_is_zero(&d)) { 534 if (!BN_is_zero(d)) {
522 fprintf(stderr, "Division test failed!\n"); 535 fprintf(stderr, "Division test failed!\n");
523 rc = 0; 536 rc = 0;
524 break; 537 break;
525 } 538 }
526 } 539 }
527err: 540 err:
528 BN_free(&a); 541 BN_free(a);
529 BN_free(&b); 542 BN_free(b);
530 BN_free(&c); 543 BN_free(c);
531 BN_free(&d); 544 BN_free(d);
532 BN_free(&e); 545 BN_free(e);
533 return (rc); 546 return (rc);
534} 547}
535 548
@@ -553,31 +566,33 @@ print_word(BIO *bp, BN_ULONG w)
553int 566int
554test_div_word(BIO *bp) 567test_div_word(BIO *bp)
555{ 568{
556 BIGNUM a, b; 569 BIGNUM *a = NULL, *b = NULL;
557 BN_ULONG r, rmod, s = 0; 570 BN_ULONG r, rmod, s = 0;
558 int i; 571 int i;
559 int rc = 1; 572 int rc = 1;
560 573
561 BN_init(&a); 574 if ((a = BN_new()) == NULL)
562 BN_init(&b); 575 goto err;
576 if ((b = BN_new()) == NULL)
577 goto err;
563 578
564 for (i = 0; i < num0; i++) { 579 for (i = 0; i < num0; i++) {
565 do { 580 do {
566 if (!BN_bntest_rand(&a, 512, -1, 0) || 581 if (!BN_bntest_rand(a, 512, -1, 0) ||
567 !BN_bntest_rand(&b, BN_BITS2, -1, 0)) { 582 !BN_bntest_rand(b, BN_BITS2, -1, 0)) {
568 rc = 0; 583 rc = 0;
569 break; 584 break;
570 } 585 }
571 s = b.d[0]; 586 s = BN_get_word(b);
572 } while (!s); 587 } while (!s);
573 588
574 if (!BN_copy(&b, &a)) { 589 if (!BN_copy(b, a)) {
575 rc = 0; 590 rc = 0;
576 break; 591 break;
577 } 592 }
578 593
579 rmod = BN_mod_word(&b, s); 594 rmod = BN_mod_word(b, s);
580 r = BN_div_word(&b, s); 595 r = BN_div_word(b, s);
581 596
582 if (r == (BN_ULONG)-1 || rmod == (BN_ULONG)-1) { 597 if (r == (BN_ULONG)-1 || rmod == (BN_ULONG)-1) {
583 rc = 0; 598 rc = 0;
@@ -592,16 +607,16 @@ test_div_word(BIO *bp)
592 607
593 if (bp != NULL) { 608 if (bp != NULL) {
594 if (!results) { 609 if (!results) {
595 CHECK_GOTO(BN_print(bp, &a)); 610 CHECK_GOTO(BN_print(bp, a));
596 BIO_puts(bp, " / "); 611 BIO_puts(bp, " / ");
597 print_word(bp, s); 612 print_word(bp, s);
598 BIO_puts(bp, " - "); 613 BIO_puts(bp, " - ");
599 } 614 }
600 CHECK_GOTO(BN_print(bp, &b)); 615 CHECK_GOTO(BN_print(bp, b));
601 BIO_puts(bp, "\n"); 616 BIO_puts(bp, "\n");
602 617
603 if (!results) { 618 if (!results) {
604 CHECK_GOTO(BN_print(bp, &a)); 619 CHECK_GOTO(BN_print(bp, a));
605 BIO_puts(bp, " % "); 620 BIO_puts(bp, " % ");
606 print_word(bp, s); 621 print_word(bp, s);
607 BIO_puts(bp, " - "); 622 BIO_puts(bp, " - ");
@@ -609,95 +624,103 @@ test_div_word(BIO *bp)
609 print_word(bp, r); 624 print_word(bp, r);
610 BIO_puts(bp, "\n"); 625 BIO_puts(bp, "\n");
611 } 626 }
612 CHECK_GOTO(BN_mul_word(&b, s)); 627 CHECK_GOTO(BN_mul_word(b, s));
613 CHECK_GOTO(BN_add_word(&b, r)); 628 CHECK_GOTO(BN_add_word(b, r));
614 CHECK_GOTO(BN_sub(&b, &a, &b)); 629 CHECK_GOTO(BN_sub(b, a, b));
615 if (!BN_is_zero(&b)) { 630 if (!BN_is_zero(b)) {
616 fprintf(stderr, "Division (word) test failed!\n"); 631 fprintf(stderr, "Division (word) test failed!\n");
617 rc = 0; 632 rc = 0;
618 break; 633 break;
619 } 634 }
620 } 635 }
621err: 636 err:
622 BN_free(&a); 637 BN_free(a);
623 BN_free(&b); 638 BN_free(b);
624 return (rc); 639
640 return rc;
625} 641}
626 642
627int 643int
628test_div_recp(BIO *bp, BN_CTX *ctx) 644test_div_recp(BIO *bp, BN_CTX *ctx)
629{ 645{
630 BIGNUM a, b, c, d, e; 646 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
631 BN_RECP_CTX recp; 647 BN_RECP_CTX *recp = NULL;
632 int i; 648 int i;
633 int rc = 1; 649 int rc = 1;
634 650
635 BN_RECP_CTX_init(&recp); 651 if ((a = BN_new()) == NULL)
636 BN_init(&a); 652 goto err;
637 BN_init(&b); 653 if ((b = BN_new()) == NULL)
638 BN_init(&c); 654 goto err;
639 BN_init(&d); 655 if ((c = BN_new()) == NULL)
640 BN_init(&e); 656 goto err;
657 if ((d = BN_new()) == NULL)
658 goto err;
659 if ((e = BN_new()) == NULL)
660 goto err;
661
662 if ((recp = BN_RECP_CTX_new()) == NULL)
663 goto err;
641 664
642 for (i = 0; i < num0 + num1; i++) { 665 for (i = 0; i < num0 + num1; i++) {
643 if (i < num1) { 666 if (i < num1) {
644 CHECK_GOTO(BN_bntest_rand(&a, 400, 0, 0)); 667 CHECK_GOTO(BN_bntest_rand(a, 400, 0, 0));
645 CHECK_GOTO(BN_copy(&b, &a)); 668 CHECK_GOTO(BN_copy(b, a));
646 CHECK_GOTO(BN_lshift(&a, &a, i)); 669 CHECK_GOTO(BN_lshift(a, a, i));
647 CHECK_GOTO(BN_add_word(&a, i)); 670 CHECK_GOTO(BN_add_word(a, i));
648 } else 671 } else
649 CHECK_GOTO(BN_bntest_rand(&b, 50 + 3 * (i - num1), 0, 0)); 672 CHECK_GOTO(BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0));
650 a.neg = rand_neg(); 673 BN_set_negative(a, rand_neg());
651 b.neg = rand_neg(); 674 BN_set_negative(b, rand_neg());
652 CHECK_GOTO(BN_RECP_CTX_set(&recp, &b, ctx)); 675 CHECK_GOTO(BN_RECP_CTX_set(recp, b, ctx));
653 CHECK_GOTO(BN_div_recp(&d, &c, &a, &recp, ctx)); 676 CHECK_GOTO(BN_div_recp(d, c, a, recp, ctx));
654 if (bp != NULL) { 677 if (bp != NULL) {
655 if (!results) { 678 if (!results) {
656 CHECK_GOTO(BN_print(bp, &a)); 679 CHECK_GOTO(BN_print(bp, a));
657 BIO_puts(bp, " / "); 680 BIO_puts(bp, " / ");
658 CHECK_GOTO(BN_print(bp, &b)); 681 CHECK_GOTO(BN_print(bp, b));
659 BIO_puts(bp, " - "); 682 BIO_puts(bp, " - ");
660 } 683 }
661 CHECK_GOTO(BN_print(bp, &d)); 684 CHECK_GOTO(BN_print(bp, d));
662 BIO_puts(bp, "\n"); 685 BIO_puts(bp, "\n");
663 686
664 if (!results) { 687 if (!results) {
665 CHECK_GOTO(BN_print(bp, &a)); 688 CHECK_GOTO(BN_print(bp, a));
666 BIO_puts(bp, " % "); 689 BIO_puts(bp, " % ");
667 CHECK_GOTO(BN_print(bp, &b)); 690 CHECK_GOTO(BN_print(bp, b));
668 BIO_puts(bp, " - "); 691 BIO_puts(bp, " - ");
669 } 692 }
670 CHECK_GOTO(BN_print(bp, &c)); 693 CHECK_GOTO(BN_print(bp, c));
671 BIO_puts(bp, "\n"); 694 BIO_puts(bp, "\n");
672 } 695 }
673 CHECK_GOTO(BN_mul(&e, &d, &b, ctx)); 696 CHECK_GOTO(BN_mul(e, d, b, ctx));
674 CHECK_GOTO(BN_add(&d, &e, &c)); 697 CHECK_GOTO(BN_add(d, e, c));
675 CHECK_GOTO(BN_sub(&d, &d, &a)); 698 CHECK_GOTO(BN_sub(d, d, a));
676 if (!BN_is_zero(&d)) { 699 if (!BN_is_zero(d)) {
677 fprintf(stderr, "Reciprocal division test failed!\n"); 700 fprintf(stderr, "Reciprocal division test failed!\n");
678 fprintf(stderr, "a="); 701 fprintf(stderr, "a=");
679 CHECK_GOTO(BN_print_fp(stderr, &a)); 702 CHECK_GOTO(BN_print_fp(stderr, a));
680 fprintf(stderr, "\nb="); 703 fprintf(stderr, "\nb=");
681 CHECK_GOTO(BN_print_fp(stderr, &b)); 704 CHECK_GOTO(BN_print_fp(stderr, b));
682 fprintf(stderr, "\n"); 705 fprintf(stderr, "\n");
683 rc = 0; 706 rc = 0;
684 break; 707 break;
685 } 708 }
686 } 709 }
687err: 710 err:
688 BN_free(&a); 711 BN_free(a);
689 BN_free(&b); 712 BN_free(b);
690 BN_free(&c); 713 BN_free(c);
691 BN_free(&d); 714 BN_free(d);
692 BN_free(&e); 715 BN_free(e);
693 BN_RECP_CTX_free(&recp); 716 BN_RECP_CTX_free(recp);
694 return (rc); 717 return (rc);
695} 718}
696 719
697int 720int
698test_mul(BIO *bp) 721test_mul(BIO *bp)
699{ 722{
700 BIGNUM a, b, c, d, e; 723 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
701 int i; 724 int i;
702 int rc = 1; 725 int rc = 1;
703 BN_CTX *ctx; 726 BN_CTX *ctx;
@@ -706,45 +729,50 @@ test_mul(BIO *bp)
706 if (ctx == NULL) 729 if (ctx == NULL)
707 exit(1); 730 exit(1);
708 731
709 BN_init(&a); 732 if ((a = BN_new()) == NULL)
710 BN_init(&b); 733 goto err;
711 BN_init(&c); 734 if ((b = BN_new()) == NULL)
712 BN_init(&d); 735 goto err;
713 BN_init(&e); 736 if ((c = BN_new()) == NULL)
737 goto err;
738 if ((d = BN_new()) == NULL)
739 goto err;
740 if ((e = BN_new()) == NULL)
741 goto err;
714 742
715 for (i = 0; i < num0 + num1; i++) { 743 for (i = 0; i < num0 + num1; i++) {
716 if (i <= num1) { 744 if (i <= num1) {
717 CHECK_GOTO(BN_bntest_rand(&a, 100, 0, 0)); 745 CHECK_GOTO(BN_bntest_rand(a, 100, 0, 0));
718 CHECK_GOTO(BN_bntest_rand(&b, 100, 0, 0)); 746 CHECK_GOTO(BN_bntest_rand(b, 100, 0, 0));
719 } else 747 } else
720 CHECK_GOTO(BN_bntest_rand(&b, i - num1, 0, 0)); 748 CHECK_GOTO(BN_bntest_rand(b, i - num1, 0, 0));
721 a.neg = rand_neg(); 749 BN_set_negative(a, rand_neg());
722 b.neg = rand_neg(); 750 BN_set_negative(b, rand_neg());
723 CHECK_GOTO(BN_mul(&c, &a, &b, ctx)); 751 CHECK_GOTO(BN_mul(c, a, b, ctx));
724 if (bp != NULL) { 752 if (bp != NULL) {
725 if (!results) { 753 if (!results) {
726 CHECK_GOTO(BN_print(bp, &a)); 754 CHECK_GOTO(BN_print(bp, a));
727 BIO_puts(bp, " * "); 755 BIO_puts(bp, " * ");
728 CHECK_GOTO(BN_print(bp, &b)); 756 CHECK_GOTO(BN_print(bp, b));
729 BIO_puts(bp, " - "); 757 BIO_puts(bp, " - ");
730 } 758 }
731 CHECK_GOTO(BN_print(bp, &c)); 759 CHECK_GOTO(BN_print(bp, c));
732 BIO_puts(bp, "\n"); 760 BIO_puts(bp, "\n");
733 } 761 }
734 CHECK_GOTO(BN_div(&d, &e, &c, &a, ctx)); 762 CHECK_GOTO(BN_div(d, e, c, a, ctx));
735 CHECK_GOTO(BN_sub(&d, &d, &b)); 763 CHECK_GOTO(BN_sub(d, d, b));
736 if (!BN_is_zero(&d) || !BN_is_zero(&e)) { 764 if (!BN_is_zero(d) || !BN_is_zero(e)) {
737 fprintf(stderr, "Multiplication test failed!\n"); 765 fprintf(stderr, "Multiplication test failed!\n");
738 rc = 0; 766 rc = 0;
739 break; 767 break;
740 } 768 }
741 } 769 }
742err: 770 err:
743 BN_free(&a); 771 BN_free(a);
744 BN_free(&b); 772 BN_free(b);
745 BN_free(&c); 773 BN_free(c);
746 BN_free(&d); 774 BN_free(d);
747 BN_free(&e); 775 BN_free(e);
748 BN_CTX_free(ctx); 776 BN_CTX_free(ctx);
749 return (rc); 777 return (rc);
750} 778}
@@ -752,17 +780,21 @@ err:
752int 780int
753test_sqr(BIO *bp, BN_CTX *ctx) 781test_sqr(BIO *bp, BN_CTX *ctx)
754{ 782{
755 BIGNUM *a, *c, *d, *e; 783 BIGNUM *a = NULL, *c = NULL, *d = NULL, *e = NULL;
756 int i, rc = 0; 784 int i, rc = 0;
757 785
758 a = BN_new(); 786 if ((a = BN_new()) == NULL)
759 c = BN_new(); 787 goto err;
760 d = BN_new(); 788 if ((c = BN_new()) == NULL)
761 e = BN_new(); 789 goto err;
790 if ((d = BN_new()) == NULL)
791 goto err;
792 if ((e = BN_new()) == NULL)
793 goto err;
762 794
763 for (i = 0; i < num0; i++) { 795 for (i = 0; i < num0; i++) {
764 CHECK_GOTO(BN_bntest_rand(a, 40 + i * 10, 0, 0)); 796 CHECK_GOTO(BN_bntest_rand(a, 40 + i * 10, 0, 0));
765 a->neg = rand_neg(); 797 BN_set_negative(a, rand_neg());
766 CHECK_GOTO(BN_sqr(c, a, ctx)); 798 CHECK_GOTO(BN_sqr(c, a, ctx));
767 if (bp != NULL) { 799 if (bp != NULL) {
768 if (!results) { 800 if (!results) {
@@ -831,7 +863,7 @@ test_sqr(BIO *bp, BN_CTX *ctx)
831 goto err; 863 goto err;
832 } 864 }
833 rc = 1; 865 rc = 1;
834err: 866 err:
835 BN_free(a); 867 BN_free(a);
836 BN_free(c); 868 BN_free(c);
837 BN_free(d); 869 BN_free(d);
@@ -842,8 +874,8 @@ err:
842int 874int
843test_mont(BIO *bp, BN_CTX *ctx) 875test_mont(BIO *bp, BN_CTX *ctx)
844{ 876{
845 BIGNUM a, b, c, d, A, B; 877 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *A = NULL, *B = NULL;
846 BIGNUM n; 878 BIGNUM *n = NULL;
847 int i; 879 int i;
848 int rc = 1; 880 int rc = 1;
849 BN_MONT_CTX *mont; 881 BN_MONT_CTX *mont;
@@ -852,94 +884,107 @@ test_mont(BIO *bp, BN_CTX *ctx)
852 if (mont == NULL) 884 if (mont == NULL)
853 return 0; 885 return 0;
854 886
855 BN_init(&a); 887 if ((a = BN_new()) == NULL)
856 BN_init(&b); 888 goto err;
857 BN_init(&c); 889 if ((b = BN_new()) == NULL)
858 BN_init(&d); 890 goto err;
859 BN_init(&A); 891 if ((c = BN_new()) == NULL)
860 BN_init(&B); 892 goto err;
861 BN_init(&n); 893 if ((d = BN_new()) == NULL)
894 goto err;
895 if ((A = BN_new()) == NULL)
896 goto err;
897 if ((B = BN_new()) == NULL)
898 goto err;
899 if ((n = BN_new()) == NULL)
900 goto err;
862 901
863 CHECK_GOTO(BN_zero(&n)); 902 CHECK_GOTO(BN_zero(n));
864 if (BN_MONT_CTX_set(mont, &n, ctx)) { 903 if (BN_MONT_CTX_set(mont, n, ctx)) {
865 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n"); 904 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
866 return (0); 905 return (0);
867 } 906 }
868 907
869 CHECK_GOTO(BN_set_word(&n, 16)); 908 CHECK_GOTO(BN_set_word(n, 16));
870 if (BN_MONT_CTX_set(mont, &n, ctx)) { 909 if (BN_MONT_CTX_set(mont, n, ctx)) {
871 fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n"); 910 fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
872 return (0); 911 return (0);
873 } 912 }
874 913
875 CHECK_GOTO(BN_bntest_rand(&a, 100, 0, 0)); 914 CHECK_GOTO(BN_bntest_rand(a, 100, 0, 0));
876 CHECK_GOTO(BN_bntest_rand(&b, 100, 0, 0)); 915 CHECK_GOTO(BN_bntest_rand(b, 100, 0, 0));
877 for (i = 0; i < num2; i++) { 916 for (i = 0; i < num2; i++) {
878 int bits = (200 * (i + 1)) / num2; 917 int bits = (200 * (i + 1)) / num2;
879 918
880 if (bits == 0) 919 if (bits == 0)
881 continue; 920 continue;
882 CHECK_GOTO(BN_bntest_rand(&n, bits, 0, 1)); 921 CHECK_GOTO(BN_bntest_rand(n, bits, 0, 1));
883 CHECK_GOTO(BN_MONT_CTX_set(mont, &n, ctx)); 922 CHECK_GOTO(BN_MONT_CTX_set(mont, n, ctx));
884 923
885 CHECK_GOTO(BN_nnmod(&a, &a, &n, ctx)); 924 CHECK_GOTO(BN_nnmod(a, a, n, ctx));
886 CHECK_GOTO(BN_nnmod(&b, &b, &n, ctx)); 925 CHECK_GOTO(BN_nnmod(b, b, n, ctx));
887 926
888 CHECK_GOTO(BN_to_montgomery(&A, &a, mont, ctx)); 927 CHECK_GOTO(BN_to_montgomery(A, a, mont, ctx));
889 CHECK_GOTO(BN_to_montgomery(&B, &b, mont, ctx)); 928 CHECK_GOTO(BN_to_montgomery(B, b, mont, ctx));
890 929
891 CHECK_GOTO(BN_mod_mul_montgomery(&c, &A, &B, mont, ctx)); 930 CHECK_GOTO(BN_mod_mul_montgomery(c, A, B, mont, ctx));
892 CHECK_GOTO(BN_from_montgomery(&A, &c, mont, ctx)); 931 CHECK_GOTO(BN_from_montgomery(A, c, mont, ctx));
893 if (bp != NULL) { 932 if (bp != NULL) {
894 if (!results) { 933 if (!results) {
895 CHECK_GOTO(BN_print(bp, &a)); 934 CHECK_GOTO(BN_print(bp, a));
896 BIO_puts(bp, " * "); 935 BIO_puts(bp, " * ");
897 CHECK_GOTO(BN_print(bp, &b)); 936 CHECK_GOTO(BN_print(bp, b));
898 BIO_puts(bp, " % "); 937 BIO_puts(bp, " % ");
938 /* XXX opaque BN */
899 CHECK_GOTO(BN_print(bp, &(mont->N))); 939 CHECK_GOTO(BN_print(bp, &(mont->N)));
900 BIO_puts(bp, " - "); 940 BIO_puts(bp, " - ");
901 } 941 }
902 CHECK_GOTO(BN_print(bp, &A)); 942 CHECK_GOTO(BN_print(bp, A));
903 BIO_puts(bp, "\n"); 943 BIO_puts(bp, "\n");
904 } 944 }
905 CHECK_GOTO(BN_mod_mul(&d, &a, &b, &n, ctx)); 945 CHECK_GOTO(BN_mod_mul(d, a, b, n, ctx));
906 CHECK_GOTO(BN_sub(&d, &d, &A)); 946 CHECK_GOTO(BN_sub(d, d, A));
907 if (!BN_is_zero(&d)) { 947 if (!BN_is_zero(d)) {
908 fprintf(stderr, "Montgomery multiplication test failed!\n"); 948 fprintf(stderr, "Montgomery multiplication test failed!\n");
909 rc = 0; 949 rc = 0;
910 break; 950 break;
911 } 951 }
912 } 952 }
913err: 953 err:
914 BN_MONT_CTX_free(mont); 954 BN_MONT_CTX_free(mont);
915 BN_free(&a); 955 BN_free(a);
916 BN_free(&b); 956 BN_free(b);
917 BN_free(&c); 957 BN_free(c);
918 BN_free(&d); 958 BN_free(d);
919 BN_free(&A); 959 BN_free(A);
920 BN_free(&B); 960 BN_free(B);
921 BN_free(&n); 961 BN_free(n);
922 return (rc); 962 return (rc);
923} 963}
924 964
925int 965int
926test_mod(BIO *bp, BN_CTX *ctx) 966test_mod(BIO *bp, BN_CTX *ctx)
927{ 967{
928 BIGNUM *a, *b, *c, *d, *e; 968 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
929 int i; 969 int i;
930 int rc = 1; 970 int rc = 1;
931 971
932 a = BN_new(); 972 if ((a = BN_new()) == NULL)
933 b = BN_new(); 973 goto err;
934 c = BN_new(); 974 if ((b = BN_new()) == NULL)
935 d = BN_new(); 975 goto err;
936 e = BN_new(); 976 if ((c = BN_new()) == NULL)
977 goto err;
978 if ((d = BN_new()) == NULL)
979 goto err;
980 if ((e = BN_new()) == NULL)
981 goto err;
937 982
938 CHECK_GOTO(BN_bntest_rand(a, 1024, 0, 0)); 983 CHECK_GOTO(BN_bntest_rand(a, 1024, 0, 0));
939 for (i = 0; i < num0; i++) { 984 for (i = 0; i < num0; i++) {
940 CHECK_GOTO(BN_bntest_rand(b, 450 + i * 10, 0, 0)); 985 CHECK_GOTO(BN_bntest_rand(b, 450 + i * 10, 0, 0));
941 a->neg = rand_neg(); 986 BN_set_negative(a, rand_neg());
942 b->neg = rand_neg(); 987 BN_set_negative(b, rand_neg());
943 CHECK_GOTO(BN_mod(c, a, b, ctx)); 988 CHECK_GOTO(BN_mod(c, a, b, ctx));
944 if (bp != NULL) { 989 if (bp != NULL) {
945 if (!results) { 990 if (!results) {
@@ -959,7 +1004,7 @@ test_mod(BIO *bp, BN_CTX *ctx)
959 break; 1004 break;
960 } 1005 }
961 } 1006 }
962err: 1007 err:
963 BN_free(a); 1008 BN_free(a);
964 BN_free(b); 1009 BN_free(b);
965 BN_free(c); 1010 BN_free(c);
@@ -971,15 +1016,20 @@ err:
971int 1016int
972test_mod_mul(BIO *bp, BN_CTX *ctx) 1017test_mod_mul(BIO *bp, BN_CTX *ctx)
973{ 1018{
974 BIGNUM *a, *b, *c, *d, *e; 1019 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
975 int i, j; 1020 int i, j;
976 int rc = 1; 1021 int rc = 1;
977 1022
978 a = BN_new(); 1023 if ((a = BN_new()) == NULL)
979 b = BN_new(); 1024 goto err;
980 c = BN_new(); 1025 if ((b = BN_new()) == NULL)
981 d = BN_new(); 1026 goto err;
982 e = BN_new(); 1027 if ((c = BN_new()) == NULL)
1028 goto err;
1029 if ((d = BN_new()) == NULL)
1030 goto err;
1031 if ((e = BN_new()) == NULL)
1032 goto err;
983 1033
984 CHECK_GOTO(BN_one(a)); 1034 CHECK_GOTO(BN_one(a));
985 CHECK_GOTO(BN_one(b)); 1035 CHECK_GOTO(BN_one(b));
@@ -994,8 +1044,8 @@ test_mod_mul(BIO *bp, BN_CTX *ctx)
994 for (i = 0; i < num0; i++) { 1044 for (i = 0; i < num0; i++) {
995 CHECK_GOTO(BN_bntest_rand(a, 475 + i * 10, 0, 0)); 1045 CHECK_GOTO(BN_bntest_rand(a, 475 + i * 10, 0, 0));
996 CHECK_GOTO(BN_bntest_rand(b, 425 + i * 11, 0, 0)); 1046 CHECK_GOTO(BN_bntest_rand(b, 425 + i * 11, 0, 0));
997 a->neg = rand_neg(); 1047 BN_set_negative(a, rand_neg());
998 b->neg = rand_neg(); 1048 BN_set_negative(b, rand_neg());
999 if (!BN_mod_mul(e, a, b, c, ctx)) { 1049 if (!BN_mod_mul(e, a, b, c, ctx)) {
1000 unsigned long l; 1050 unsigned long l;
1001 1051
@@ -1011,7 +1061,8 @@ test_mod_mul(BIO *bp, BN_CTX *ctx)
1011 CHECK_GOTO(BN_print(bp, b)); 1061 CHECK_GOTO(BN_print(bp, b));
1012 BIO_puts(bp, " % "); 1062 BIO_puts(bp, " % ");
1013 CHECK_GOTO(BN_print(bp, c)); 1063 CHECK_GOTO(BN_print(bp, c));
1014 if ((a->neg ^ b->neg) && !BN_is_zero(e)) { 1064 if ((BN_is_negative(a) ^ BN_is_negative(b)) &&
1065 !BN_is_zero(e)) {
1015 /* If (a*b) % c is negative, c must be added 1066 /* If (a*b) % c is negative, c must be added
1016 * in order to obtain the normalized remainder 1067 * in order to obtain the normalized remainder
1017 * (new with OpenSSL 0.9.7, previous versions of 1068 * (new with OpenSSL 0.9.7, previous versions of
@@ -1036,7 +1087,7 @@ test_mod_mul(BIO *bp, BN_CTX *ctx)
1036 } 1087 }
1037 } 1088 }
1038 } 1089 }
1039err: 1090 err:
1040 BN_free(a); 1091 BN_free(a);
1041 BN_free(b); 1092 BN_free(b);
1042 BN_free(c); 1093 BN_free(c);
@@ -1048,15 +1099,20 @@ err:
1048int 1099int
1049test_mod_exp(BIO *bp, BN_CTX *ctx) 1100test_mod_exp(BIO *bp, BN_CTX *ctx)
1050{ 1101{
1051 BIGNUM *a, *b, *c, *d, *e; 1102 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
1052 int i; 1103 int i;
1053 int rc = 1; 1104 int rc = 1;
1054 1105
1055 a = BN_new(); 1106 if ((a = BN_new()) == NULL)
1056 b = BN_new(); 1107 goto err;
1057 c = BN_new(); 1108 if ((b = BN_new()) == NULL)
1058 d = BN_new(); 1109 goto err;
1059 e = BN_new(); 1110 if ((c = BN_new()) == NULL)
1111 goto err;
1112 if ((d = BN_new()) == NULL)
1113 goto err;
1114 if ((e = BN_new()) == NULL)
1115 goto err;
1060 1116
1061 CHECK_GOTO(BN_one(a)); 1117 CHECK_GOTO(BN_one(a));
1062 CHECK_GOTO(BN_one(b)); 1118 CHECK_GOTO(BN_one(b));
@@ -1172,7 +1228,7 @@ test_mod_exp(BIO *bp, BN_CTX *ctx)
1172 break; 1228 break;
1173 } 1229 }
1174 } 1230 }
1175err: 1231 err:
1176 BN_free(a); 1232 BN_free(a);
1177 BN_free(b); 1233 BN_free(b);
1178 BN_free(c); 1234 BN_free(c);
@@ -1184,15 +1240,20 @@ err:
1184int 1240int
1185test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) 1241test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1186{ 1242{
1187 BIGNUM *a, *b, *c, *d, *e; 1243 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
1188 int i; 1244 int i;
1189 int rc = 1; 1245 int rc = 1;
1190 1246
1191 a = BN_new(); 1247 if ((a = BN_new()) == NULL)
1192 b = BN_new(); 1248 goto err;
1193 c = BN_new(); 1249 if ((b = BN_new()) == NULL)
1194 d = BN_new(); 1250 goto err;
1195 e = BN_new(); 1251 if ((c = BN_new()) == NULL)
1252 goto err;
1253 if ((d = BN_new()) == NULL)
1254 goto err;
1255 if ((e = BN_new()) == NULL)
1256 goto err;
1196 1257
1197 CHECK_GOTO(BN_one(a)); 1258 CHECK_GOTO(BN_one(a));
1198 CHECK_GOTO(BN_one(b)); 1259 CHECK_GOTO(BN_one(b));
@@ -1243,7 +1304,7 @@ test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1243 break; 1304 break;
1244 } 1305 }
1245 } 1306 }
1246err: 1307 err:
1247 BN_free(a); 1308 BN_free(a);
1248 BN_free(b); 1309 BN_free(b);
1249 BN_free(c); 1310 BN_free(c);
@@ -1259,18 +1320,28 @@ err:
1259int 1320int
1260test_mod_exp_mont5(BIO *bp, BN_CTX *ctx) 1321test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1261{ 1322{
1262 BIGNUM *a, *p, *m, *d, *e, *b, *n, *c; 1323 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
1263 int len, rc = 1; 1324 BIGNUM *b = NULL, *n = NULL, *c = NULL;
1264 BN_MONT_CTX *mont; 1325 BN_MONT_CTX *mont = NULL;
1326 int len;
1327 int rc = 1;
1265 1328
1266 a = BN_new(); 1329 if ((a = BN_new()) == NULL)
1267 p = BN_new(); 1330 goto err;
1268 m = BN_new(); 1331 if ((p = BN_new()) == NULL)
1269 d = BN_new(); 1332 goto err;
1270 e = BN_new(); 1333 if ((m = BN_new()) == NULL)
1271 b = BN_new(); 1334 goto err;
1272 n = BN_new(); 1335 if ((d = BN_new()) == NULL)
1273 c = BN_new(); 1336 goto err;
1337 if ((e = BN_new()) == NULL)
1338 goto err;
1339 if ((b = BN_new()) == NULL)
1340 goto err;
1341 if ((n = BN_new()) == NULL)
1342 goto err;
1343 if ((c = BN_new()) == NULL)
1344 goto err;
1274 1345
1275 CHECK_GOTO(mont = BN_MONT_CTX_new()); 1346 CHECK_GOTO(mont = BN_MONT_CTX_new());
1276 1347
@@ -1414,7 +1485,7 @@ test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1414 rc = 0; 1485 rc = 0;
1415 goto err; 1486 goto err;
1416 } 1487 }
1417err: 1488 err:
1418 BN_free(a); 1489 BN_free(a);
1419 BN_free(p); 1490 BN_free(p);
1420 BN_free(m); 1491 BN_free(m);
@@ -1430,15 +1501,20 @@ err:
1430int 1501int
1431test_exp(BIO *bp, BN_CTX *ctx) 1502test_exp(BIO *bp, BN_CTX *ctx)
1432{ 1503{
1433 BIGNUM *a, *b, *d, *e, *one; 1504 BIGNUM *a = NULL, *b = NULL, *d = NULL, *e = NULL, *one = NULL;
1434 int i; 1505 int i;
1435 int rc = 1; 1506 int rc = 1;
1436 1507
1437 a = BN_new(); 1508 if ((a = BN_new()) == NULL)
1438 b = BN_new(); 1509 goto err;
1439 d = BN_new(); 1510 if ((b = BN_new()) == NULL)
1440 e = BN_new(); 1511 goto err;
1441 one = BN_new(); 1512 if ((d = BN_new()) == NULL)
1513 goto err;
1514 if ((e = BN_new()) == NULL)
1515 goto err;
1516 if ((one = BN_new()) == NULL)
1517 goto err;
1442 CHECK_GOTO(BN_one(one)); 1518 CHECK_GOTO(BN_one(one));
1443 1519
1444 for (i = 0; i < num2; i++) { 1520 for (i = 0; i < num2; i++) {
@@ -1470,7 +1546,7 @@ test_exp(BIO *bp, BN_CTX *ctx)
1470 break; 1546 break;
1471 } 1547 }
1472 } 1548 }
1473err: 1549 err:
1474 BN_free(a); 1550 BN_free(a);
1475 BN_free(b); 1551 BN_free(b);
1476 BN_free(d); 1552 BN_free(d);
@@ -1483,66 +1559,76 @@ err:
1483int 1559int
1484test_gf2m_add(BIO *bp) 1560test_gf2m_add(BIO *bp)
1485{ 1561{
1486 BIGNUM a, b, c; 1562 BIGNUM *a = NULL, *b = NULL, *c = NULL;
1487 int i, rc = 0; 1563 int i, rc = 0;
1488 1564
1489 BN_init(&a); 1565 if ((a = BN_new()) == NULL)
1490 BN_init(&b); 1566 goto err;
1491 BN_init(&c); 1567 if ((b = BN_new()) == NULL)
1568 goto err;
1569 if ((c = BN_new()) == NULL)
1570 goto err;
1492 1571
1493 for (i = 0; i < num0; i++) { 1572 for (i = 0; i < num0; i++) {
1494 CHECK_GOTO(BN_rand(&a, 512, 0, 0)); 1573 CHECK_GOTO(BN_rand(a, 512, 0, 0));
1495 CHECK_GOTO(BN_copy(&b, BN_value_one())); 1574 CHECK_GOTO(BN_copy(b, BN_value_one()));
1496 a.neg = rand_neg(); 1575 BN_set_negative(a, rand_neg());
1497 b.neg = rand_neg(); 1576 BN_set_negative(b, rand_neg());
1498 CHECK_GOTO(BN_GF2m_add(&c, &a, &b)); 1577 CHECK_GOTO(BN_GF2m_add(c, a, b));
1499#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ 1578#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1500 if (bp != NULL) { 1579 if (bp != NULL) {
1501 if (!results) { 1580 if (!results) {
1502 CHECK_GOTO(BN_print(bp, &a)); 1581 CHECK_GOTO(BN_print(bp, a));
1503 BIO_puts(bp, " ^ "); 1582 BIO_puts(bp, " ^ ");
1504 CHECK_GOTO(BN_print(bp, &b)); 1583 CHECK_GOTO(BN_print(bp, b));
1505 BIO_puts(bp, " = "); 1584 BIO_puts(bp, " = ");
1506 } 1585 }
1507 CHECK_GOTO(BN_print(bp, &c)); 1586 CHECK_GOTO(BN_print(bp, c));
1508 BIO_puts(bp, "\n"); 1587 BIO_puts(bp, "\n");
1509 } 1588 }
1510#endif 1589#endif
1511 /* Test that two added values have the correct parity. */ 1590 /* Test that two added values have the correct parity. */
1512 if ((BN_is_odd(&a) && BN_is_odd(&c)) 1591 if ((BN_is_odd(a) && BN_is_odd(c))
1513 || (!BN_is_odd(&a) && !BN_is_odd(&c))) { 1592 || (!BN_is_odd(a) && !BN_is_odd(c))) {
1514 fprintf(stderr, "GF(2^m) addition test (a) failed!\n"); 1593 fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
1515 goto err; 1594 goto err;
1516 } 1595 }
1517 CHECK_GOTO(BN_GF2m_add(&c, &c, &c)); 1596 CHECK_GOTO(BN_GF2m_add(c, c, c));
1518 /* Test that c + c = 0. */ 1597 /* Test that c + c = 0. */
1519 if (!BN_is_zero(&c)) { 1598 if (!BN_is_zero(c)) {
1520 fprintf(stderr, "GF(2^m) addition test (b) failed!\n"); 1599 fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
1521 goto err; 1600 goto err;
1522 } 1601 }
1523 } 1602 }
1524 rc = 1; 1603 rc = 1;
1525err: 1604 err:
1526 BN_free(&a); 1605 BN_free(a);
1527 BN_free(&b); 1606 BN_free(b);
1528 BN_free(&c); 1607 BN_free(c);
1529 return rc; 1608 return rc;
1530} 1609}
1531 1610
1532int 1611int
1533test_gf2m_mod(BIO *bp) 1612test_gf2m_mod(BIO *bp)
1534{ 1613{
1535 BIGNUM *a, *b[2], *c, *d, *e; 1614 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL;
1536 int i, j, rc = 0; 1615 int i, j;
1537 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1616 int p0[] = { 163, 7, 6, 3, 0, -1 };
1538 int p1[] = { 193, 15, 0, -1 }; 1617 int p1[] = { 193, 15, 0, -1 };
1618 int rc = 0;
1539 1619
1540 a = BN_new(); 1620 if ((a = BN_new()) == NULL)
1541 b[0] = BN_new(); 1621 goto err;
1542 b[1] = BN_new(); 1622 if ((b[0] = BN_new()) == NULL)
1543 c = BN_new(); 1623 goto err;
1544 d = BN_new(); 1624 if ((b[1] = BN_new()) == NULL)
1545 e = BN_new(); 1625 goto err;
1626 if ((c = BN_new()) == NULL)
1627 goto err;
1628 if ((d = BN_new()) == NULL)
1629 goto err;
1630 if ((e = BN_new()) == NULL)
1631 goto err;
1546 1632
1547 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1633 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1548 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 1634 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1573,7 +1659,7 @@ test_gf2m_mod(BIO *bp)
1573 } 1659 }
1574 } 1660 }
1575 rc = 1; 1661 rc = 1;
1576err: 1662 err:
1577 BN_free(a); 1663 BN_free(a);
1578 BN_free(b[0]); 1664 BN_free(b[0]);
1579 BN_free(b[1]); 1665 BN_free(b[1]);
@@ -1586,20 +1672,31 @@ err:
1586int 1672int
1587test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx) 1673test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1588{ 1674{
1589 BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h; 1675 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
1590 int i, j, rc = 0; 1676 BIGNUM *g = NULL, *h = NULL;
1677 int i, j;
1591 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1678 int p0[] = { 163, 7, 6, 3, 0, -1 };
1592 int p1[] = { 193, 15, 0, -1 }; 1679 int p1[] = { 193, 15, 0, -1 };
1680 int rc = 0;
1593 1681
1594 a = BN_new(); 1682 if ((a = BN_new()) == NULL)
1595 b[0] = BN_new(); 1683 goto err;
1596 b[1] = BN_new(); 1684 if ((b[0] = BN_new()) == NULL)
1597 c = BN_new(); 1685 goto err;
1598 d = BN_new(); 1686 if ((b[1] = BN_new()) == NULL)
1599 e = BN_new(); 1687 goto err;
1600 f = BN_new(); 1688 if ((c = BN_new()) == NULL)
1601 g = BN_new(); 1689 goto err;
1602 h = BN_new(); 1690 if ((d = BN_new()) == NULL)
1691 goto err;
1692 if ((e = BN_new()) == NULL)
1693 goto err;
1694 if ((f = BN_new()) == NULL)
1695 goto err;
1696 if ((g = BN_new()) == NULL)
1697 goto err;
1698 if ((h = BN_new()) == NULL)
1699 goto err;
1603 1700
1604 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1701 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1605 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 1702 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1637,7 +1734,7 @@ test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1637 } 1734 }
1638 } 1735 }
1639 rc = 1; 1736 rc = 1;
1640err: 1737 err:
1641 BN_free(a); 1738 BN_free(a);
1642 BN_free(b[0]); 1739 BN_free(b[0]);
1643 BN_free(b[1]); 1740 BN_free(b[1]);
@@ -1653,16 +1750,21 @@ err:
1653int 1750int
1654test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx) 1751test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1655{ 1752{
1656 BIGNUM *a, *b[2], *c, *d; 1753 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL;
1657 int i, j, rc = 0; 1754 int i, j, rc = 0;
1658 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1755 int p0[] = { 163, 7, 6, 3, 0, -1 };
1659 int p1[] = { 193, 15, 0, -1 }; 1756 int p1[] = { 193, 15, 0, -1 };
1660 1757
1661 a = BN_new(); 1758 if ((a = BN_new()) == NULL)
1662 b[0] = BN_new(); 1759 goto err;
1663 b[1] = BN_new(); 1760 if ((b[0] = BN_new()) == NULL)
1664 c = BN_new(); 1761 goto err;
1665 d = BN_new(); 1762 if ((b[1] = BN_new()) == NULL)
1763 goto err;
1764 if ((c = BN_new()) == NULL)
1765 goto err;
1766 if ((d = BN_new()) == NULL)
1767 goto err;
1666 1768
1667 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1769 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1668 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 1770 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1696,7 +1798,7 @@ test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1696 } 1798 }
1697 } 1799 }
1698 rc = 1; 1800 rc = 1;
1699err: 1801 err:
1700 BN_free(a); 1802 BN_free(a);
1701 BN_free(b[0]); 1803 BN_free(b[0]);
1702 BN_free(b[1]); 1804 BN_free(b[1]);
@@ -1708,16 +1810,21 @@ err:
1708int 1810int
1709test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx) 1811test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1710{ 1812{
1711 BIGNUM *a, *b[2], *c, *d; 1813 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL;
1712 int i, j, rc = 0; 1814 int i, j, rc = 0;
1713 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1815 int p0[] = { 163, 7, 6, 3, 0, -1 };
1714 int p1[] = { 193, 15, 0, -1 }; 1816 int p1[] = { 193, 15, 0, -1 };
1715 1817
1716 a = BN_new(); 1818 if ((a = BN_new()) == NULL)
1717 b[0] = BN_new(); 1819 goto err;
1718 b[1] = BN_new(); 1820 if ((b[0] = BN_new()) == NULL)
1719 c = BN_new(); 1821 goto err;
1720 d = BN_new(); 1822 if ((b[1] = BN_new()) == NULL)
1823 goto err;
1824 if ((c = BN_new()) == NULL)
1825 goto err;
1826 if ((d = BN_new()) == NULL)
1827 goto err;
1721 1828
1722 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1829 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1723 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 1830 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1747,7 +1854,7 @@ test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1747 } 1854 }
1748 } 1855 }
1749 rc = 1; 1856 rc = 1;
1750err: 1857 err:
1751 BN_free(a); 1858 BN_free(a);
1752 BN_free(b[0]); 1859 BN_free(b[0]);
1753 BN_free(b[1]); 1860 BN_free(b[1]);
@@ -1759,18 +1866,25 @@ err:
1759int 1866int
1760test_gf2m_mod_div(BIO *bp, BN_CTX *ctx) 1867test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1761{ 1868{
1762 BIGNUM *a, *b[2], *c, *d, *e, *f; 1869 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
1763 int i, j, rc = 0; 1870 int i, j, rc = 0;
1764 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1871 int p0[] = { 163, 7, 6, 3, 0, -1 };
1765 int p1[] = { 193, 15, 0, -1 }; 1872 int p1[] = { 193, 15, 0, -1 };
1766 1873
1767 a = BN_new(); 1874 if ((a = BN_new()) == NULL)
1768 b[0] = BN_new(); 1875 goto err;
1769 b[1] = BN_new(); 1876 if ((b[0] = BN_new()) == NULL)
1770 c = BN_new(); 1877 goto err;
1771 d = BN_new(); 1878 if ((b[1] = BN_new()) == NULL)
1772 e = BN_new(); 1879 goto err;
1773 f = BN_new(); 1880 if ((c = BN_new()) == NULL)
1881 goto err;
1882 if ((d = BN_new()) == NULL)
1883 goto err;
1884 if ((e = BN_new()) == NULL)
1885 goto err;
1886 if ((f = BN_new()) == NULL)
1887 goto err;
1774 1888
1775 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1889 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1776 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 1890 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1804,7 +1918,7 @@ test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1804 } 1918 }
1805 } 1919 }
1806 rc = 1; 1920 rc = 1;
1807err: 1921 err:
1808 BN_free(a); 1922 BN_free(a);
1809 BN_free(b[0]); 1923 BN_free(b[0]);
1810 BN_free(b[1]); 1924 BN_free(b[1]);
@@ -1818,18 +1932,25 @@ err:
1818int 1932int
1819test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx) 1933test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1820{ 1934{
1821 BIGNUM *a, *b[2], *c, *d, *e, *f; 1935 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
1822 int i, j, rc = 0; 1936 int i, j, rc = 0;
1823 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1937 int p0[] = { 163, 7, 6, 3, 0, -1 };
1824 int p1[] = { 193, 15, 0, -1 }; 1938 int p1[] = { 193, 15, 0, -1 };
1825 1939
1826 a = BN_new(); 1940 if ((a = BN_new()) == NULL)
1827 b[0] = BN_new(); 1941 goto err;
1828 b[1] = BN_new(); 1942 if ((b[0] = BN_new()) == NULL)
1829 c = BN_new(); 1943 goto err;
1830 d = BN_new(); 1944 if ((b[1] = BN_new()) == NULL)
1831 e = BN_new(); 1945 goto err;
1832 f = BN_new(); 1946 if ((c = BN_new()) == NULL)
1947 goto err;
1948 if ((d = BN_new()) == NULL)
1949 goto err;
1950 if ((e = BN_new()) == NULL)
1951 goto err;
1952 if ((f = BN_new()) == NULL)
1953 goto err;
1833 1954
1834 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1955 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1835 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 1956 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1871,7 +1992,7 @@ test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1871 } 1992 }
1872 } 1993 }
1873 rc = 1; 1994 rc = 1;
1874err: 1995 err:
1875 BN_free(a); 1996 BN_free(a);
1876 BN_free(b[0]); 1997 BN_free(b[0]);
1877 BN_free(b[1]); 1998 BN_free(b[1]);
@@ -1885,18 +2006,25 @@ err:
1885int 2006int
1886test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx) 2007test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1887{ 2008{
1888 BIGNUM *a, *b[2], *c, *d, *e, *f; 2009 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
1889 int i, j, rc = 0; 2010 int i, j, rc = 0;
1890 int p0[] = { 163, 7, 6, 3, 0, -1 }; 2011 int p0[] = { 163, 7, 6, 3, 0, -1 };
1891 int p1[] = { 193, 15, 0, -1 }; 2012 int p1[] = { 193, 15, 0, -1 };
1892 2013
1893 a = BN_new(); 2014 if ((a = BN_new()) == NULL)
1894 b[0] = BN_new(); 2015 goto err;
1895 b[1] = BN_new(); 2016 if ((b[0] = BN_new()) == NULL)
1896 c = BN_new(); 2017 goto err;
1897 d = BN_new(); 2018 if ((b[1] = BN_new()) == NULL)
1898 e = BN_new(); 2019 goto err;
1899 f = BN_new(); 2020 if ((c = BN_new()) == NULL)
2021 goto err;
2022 if ((d = BN_new()) == NULL)
2023 goto err;
2024 if ((e = BN_new()) == NULL)
2025 goto err;
2026 if ((f = BN_new()) == NULL)
2027 goto err;
1900 2028
1901 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 2029 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1902 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 2030 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -1926,7 +2054,7 @@ test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1926 } 2054 }
1927 } 2055 }
1928 rc = 1; 2056 rc = 1;
1929err: 2057 err:
1930 BN_free(a); 2058 BN_free(a);
1931 BN_free(b[0]); 2059 BN_free(b[0]);
1932 BN_free(b[1]); 2060 BN_free(b[1]);
@@ -1940,17 +2068,23 @@ err:
1940int 2068int
1941test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx) 2069test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
1942{ 2070{
1943 BIGNUM *a, *b[2], *c, *d, *e; 2071 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL;
1944 int i, j, s = 0, t, rc = 0; 2072 int i, j, s = 0, t, rc = 0;
1945 int p0[] = { 163, 7, 6, 3, 0, -1 }; 2073 int p0[] = { 163, 7, 6, 3, 0, -1 };
1946 int p1[] = { 193, 15, 0, -1 }; 2074 int p1[] = { 193, 15, 0, -1 };
1947 2075
1948 a = BN_new(); 2076 if ((a = BN_new()) == NULL)
1949 b[0] = BN_new(); 2077 goto err;
1950 b[1] = BN_new(); 2078 if ((b[0] = BN_new()) == NULL)
1951 c = BN_new(); 2079 goto err;
1952 d = BN_new(); 2080 if ((b[1] = BN_new()) == NULL)
1953 e = BN_new(); 2081 goto err;
2082 if ((c = BN_new()) == NULL)
2083 goto err;
2084 if ((d = BN_new()) == NULL)
2085 goto err;
2086 if ((e = BN_new()) == NULL)
2087 goto err;
1954 2088
1955 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 2089 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
1956 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1])); 2090 CHECK_GOTO(BN_GF2m_arr2poly(p1, b[1]));
@@ -2004,7 +2138,7 @@ test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
2004 goto err; 2138 goto err;
2005 } 2139 }
2006 rc = 1; 2140 rc = 1;
2007err: 2141 err:
2008 BN_free(a); 2142 BN_free(a);
2009 BN_free(b[0]); 2143 BN_free(b[0]);
2010 BN_free(b[1]); 2144 BN_free(b[1]);
@@ -2034,20 +2168,25 @@ genprime_cb(int p, int n, BN_GENCB *arg)
2034int 2168int
2035test_kron(BIO *bp, BN_CTX *ctx) 2169test_kron(BIO *bp, BN_CTX *ctx)
2036{ 2170{
2037 BN_GENCB cb; 2171 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
2038 BIGNUM *a, *b, *r, *t; 2172 BN_GENCB *cb = NULL;
2039 int i; 2173 int i;
2040 int legendre, kronecker; 2174 int legendre, kronecker;
2041 int rc = 0; 2175 int rc = 0;
2042 2176
2043 a = BN_new(); 2177 if ((a = BN_new()) == NULL)
2044 b = BN_new(); 2178 goto err;
2045 r = BN_new(); 2179 if ((b = BN_new()) == NULL)
2046 t = BN_new(); 2180 goto err;
2047 if (a == NULL || b == NULL || r == NULL || t == NULL) 2181 if ((r = BN_new()) == NULL)
2182 goto err;
2183 if ((t = BN_new()) == NULL)
2184 goto err;
2185
2186 if ((cb = BN_GENCB_new()) == NULL)
2048 goto err; 2187 goto err;
2049 2188
2050 BN_GENCB_set(&cb, genprime_cb, NULL); 2189 BN_GENCB_set(cb, genprime_cb, NULL);
2051 2190
2052 /* 2191 /*
2053 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In 2192 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
@@ -2059,30 +2198,30 @@ test_kron(BIO *bp, BN_CTX *ctx)
2059 * is prime but whether BN_kronecker works.) 2198 * is prime but whether BN_kronecker works.)
2060 */ 2199 */
2061 2200
2062 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) 2201 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, cb))
2063 goto err; 2202 goto err;
2064 b->neg = rand_neg(); 2203 BN_set_negative(b, rand_neg());
2065 putc('\n', stderr); 2204 putc('\n', stderr);
2066 2205
2067 for (i = 0; i < num0; i++) { 2206 for (i = 0; i < num0; i++) {
2068 if (!BN_bntest_rand(a, 512, 0, 0)) 2207 if (!BN_bntest_rand(a, 512, 0, 0))
2069 goto err; 2208 goto err;
2070 a->neg = rand_neg(); 2209 BN_set_negative(a, rand_neg());
2071 2210
2072 /* t := (|b|-1)/2 (note that b is odd) */ 2211 /* t := (|b|-1)/2 (note that b is odd) */
2073 if (!BN_copy(t, b)) 2212 if (!BN_copy(t, b))
2074 goto err; 2213 goto err;
2075 t->neg = 0; 2214 BN_set_negative(t, 0);
2076 if (!BN_sub_word(t, 1)) 2215 if (!BN_sub_word(t, 1))
2077 goto err; 2216 goto err;
2078 if (!BN_rshift1(t, t)) 2217 if (!BN_rshift1(t, t))
2079 goto err; 2218 goto err;
2080 /* r := a^t mod b */ 2219 /* r := a^t mod b */
2081 b->neg = 0; 2220 BN_set_negative(b, 0);
2082 2221
2083 if (!BN_mod_exp_recp(r, a, t, b, ctx)) 2222 if (!BN_mod_exp_recp(r, a, t, b, ctx))
2084 goto err; 2223 goto err;
2085 b->neg = 1; 2224 BN_set_negative(b, 1);
2086 2225
2087 if (BN_is_word(r, 1)) 2226 if (BN_is_word(r, 1))
2088 legendre = 1; 2227 legendre = 1;
@@ -2102,7 +2241,7 @@ test_kron(BIO *bp, BN_CTX *ctx)
2102 if (kronecker < -1) 2241 if (kronecker < -1)
2103 goto err; 2242 goto err;
2104 /* we actually need BN_kronecker(a, |b|) */ 2243 /* we actually need BN_kronecker(a, |b|) */
2105 if (a->neg && b->neg) 2244 if (BN_is_negative(a) && BN_is_negative(b))
2106 kronecker = -kronecker; 2245 kronecker = -kronecker;
2107 2246
2108 if (legendre != kronecker) { 2247 if (legendre != kronecker) {
@@ -2119,7 +2258,9 @@ test_kron(BIO *bp, BN_CTX *ctx)
2119 2258
2120 putc('\n', stderr); 2259 putc('\n', stderr);
2121 rc = 1; 2260 rc = 1;
2122err: 2261
2262 err:
2263 BN_GENCB_free(cb);
2123 BN_free(a); 2264 BN_free(a);
2124 BN_free(b); 2265 BN_free(b);
2125 BN_free(r); 2266 BN_free(r);
@@ -2130,18 +2271,24 @@ err:
2130int 2271int
2131test_sqrt(BIO *bp, BN_CTX *ctx) 2272test_sqrt(BIO *bp, BN_CTX *ctx)
2132{ 2273{
2133 BN_GENCB cb; 2274 BIGNUM *a = NULL, *p = NULL, *r = NULL;
2134 BIGNUM *a, *p, *r; 2275 BN_GENCB *cb = NULL;
2135 int i, j; 2276 int i, j;
2136 int rc = 0; 2277 int rc = 0;
2137 2278
2138 a = BN_new(); 2279 if ((a = BN_new()) == NULL)
2139 p = BN_new(); 2280 goto err;
2140 r = BN_new(); 2281 if ((p = BN_new()) == NULL)
2282 goto err;
2283 if ((r = BN_new()) == NULL)
2284 goto err;
2141 if (a == NULL || p == NULL || r == NULL) 2285 if (a == NULL || p == NULL || r == NULL)
2142 goto err; 2286 goto err;
2143 2287
2144 BN_GENCB_set(&cb, genprime_cb, NULL); 2288 if ((cb = BN_GENCB_new()) == NULL)
2289 goto err;
2290
2291 BN_GENCB_set(cb, genprime_cb, NULL);
2145 2292
2146 for (i = 0; i < 16; i++) { 2293 for (i = 0; i < 16; i++) {
2147 if (i < 8) { 2294 if (i < 8) {
@@ -2155,11 +2302,11 @@ test_sqrt(BIO *bp, BN_CTX *ctx)
2155 if (!BN_set_word(r, 2 * i + 1)) 2302 if (!BN_set_word(r, 2 * i + 1))
2156 goto err; 2303 goto err;
2157 2304
2158 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) 2305 if (!BN_generate_prime_ex(p, 256, 0, a, r, cb))
2159 goto err; 2306 goto err;
2160 putc('\n', stderr); 2307 putc('\n', stderr);
2161 } 2308 }
2162 p->neg = rand_neg(); 2309 BN_set_negative(p, rand_neg());
2163 2310
2164 for (j = 0; j < num2; j++) { 2311 for (j = 0; j < num2; j++) {
2165 /* 2312 /*
@@ -2209,7 +2356,9 @@ test_sqrt(BIO *bp, BN_CTX *ctx)
2209 putc('\n', stderr); 2356 putc('\n', stderr);
2210 } 2357 }
2211 rc = 1; 2358 rc = 1;
2212err: 2359
2360 err:
2361 BN_GENCB_free(cb);
2213 BN_free(a); 2362 BN_free(a);
2214 BN_free(p); 2363 BN_free(p);
2215 BN_free(r); 2364 BN_free(r);
@@ -2219,21 +2368,25 @@ err:
2219int 2368int
2220test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_) 2369test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
2221{ 2370{
2222 BIGNUM *a = NULL, *b, *c, *d; 2371 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
2223 int i; 2372 int i;
2224 int rc = 1; 2373 int rc = 1;
2225 2374
2226 b = BN_new(); 2375 if ((b = BN_new()) == NULL)
2227 c = BN_new(); 2376 goto err;
2228 d = BN_new(); 2377 if ((c = BN_new()) == NULL)
2378 goto err;
2379 if ((d = BN_new()) == NULL)
2380 goto err;
2229 CHECK_GOTO(BN_one(c)); 2381 CHECK_GOTO(BN_one(c));
2230 2382
2231 if (a_) 2383 if (a_)
2232 a = a_; 2384 a = a_;
2233 else { 2385 else {
2234 a = BN_new(); 2386 if ((a = BN_new()) == NULL)
2387 goto err;
2235 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0)); 2388 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
2236 a->neg = rand_neg(); 2389 BN_set_negative(a, rand_neg());
2237 } 2390 }
2238 for (i = 0; i < num0; i++) { 2391 for (i = 0; i < num0; i++) {
2239 CHECK_GOTO(BN_lshift(b, a, i + 1)); 2392 CHECK_GOTO(BN_lshift(b, a, i + 1));
@@ -2265,7 +2418,7 @@ test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
2265 break; 2418 break;
2266 } 2419 }
2267 } 2420 }
2268err: 2421 err:
2269 BN_free(a); 2422 BN_free(a);
2270 BN_free(b); 2423 BN_free(b);
2271 BN_free(c); 2424 BN_free(c);
@@ -2276,16 +2429,19 @@ err:
2276int 2429int
2277test_lshift1(BIO *bp) 2430test_lshift1(BIO *bp)
2278{ 2431{
2279 BIGNUM *a, *b, *c; 2432 BIGNUM *a = NULL, *b = NULL, *c = NULL;
2280 int i; 2433 int i;
2281 int rc = 1; 2434 int rc = 1;
2282 2435
2283 a = BN_new(); 2436 if ((a = BN_new()) == NULL)
2284 b = BN_new(); 2437 goto err;
2285 c = BN_new(); 2438 if ((b = BN_new()) == NULL)
2439 goto err;
2440 if ((c = BN_new()) == NULL)
2441 goto err;
2286 2442
2287 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0)); 2443 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
2288 a->neg = rand_neg(); 2444 BN_set_negative(a, rand_neg());
2289 for (i = 0; i < num0; i++) { 2445 for (i = 0; i < num0; i++) {
2290 CHECK_GOTO(BN_lshift1(b, a)); 2446 CHECK_GOTO(BN_lshift1(b, a));
2291 if (bp != NULL) { 2447 if (bp != NULL) {
@@ -2307,7 +2463,7 @@ test_lshift1(BIO *bp)
2307 2463
2308 CHECK_GOTO(BN_copy(a, b)); 2464 CHECK_GOTO(BN_copy(a, b));
2309 } 2465 }
2310err: 2466 err:
2311 BN_free(a); 2467 BN_free(a);
2312 BN_free(b); 2468 BN_free(b);
2313 BN_free(c); 2469 BN_free(c);
@@ -2317,19 +2473,24 @@ err:
2317int 2473int
2318test_rshift(BIO *bp, BN_CTX *ctx) 2474test_rshift(BIO *bp, BN_CTX *ctx)
2319{ 2475{
2320 BIGNUM *a, *b, *c, *d, *e; 2476 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
2321 int i; 2477 int i;
2322 int rc = 1; 2478 int rc = 1;
2323 2479
2324 a = BN_new(); 2480 if ((a = BN_new()) == NULL)
2325 b = BN_new(); 2481 goto err;
2326 c = BN_new(); 2482 if ((b = BN_new()) == NULL)
2327 d = BN_new(); 2483 goto err;
2328 e = BN_new(); 2484 if ((c = BN_new()) == NULL)
2485 goto err;
2486 if ((d = BN_new()) == NULL)
2487 goto err;
2488 if ((e = BN_new()) == NULL)
2489 goto err;
2329 CHECK_GOTO(BN_one(c)); 2490 CHECK_GOTO(BN_one(c));
2330 2491
2331 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0)); 2492 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
2332 a->neg = rand_neg(); 2493 BN_set_negative(a, rand_neg());
2333 for (i = 0; i < num0; i++) { 2494 for (i = 0; i < num0; i++) {
2334 CHECK_GOTO(BN_rshift(b, a, i + 1)); 2495 CHECK_GOTO(BN_rshift(b, a, i + 1));
2335 CHECK_GOTO(BN_add(c, c, c)); 2496 CHECK_GOTO(BN_add(c, c, c));
@@ -2351,7 +2512,7 @@ test_rshift(BIO *bp, BN_CTX *ctx)
2351 break; 2512 break;
2352 } 2513 }
2353 } 2514 }
2354err: 2515 err:
2355 BN_free(a); 2516 BN_free(a);
2356 BN_free(b); 2517 BN_free(b);
2357 BN_free(c); 2518 BN_free(c);
@@ -2363,16 +2524,19 @@ err:
2363int 2524int
2364test_rshift1(BIO *bp) 2525test_rshift1(BIO *bp)
2365{ 2526{
2366 BIGNUM *a, *b, *c; 2527 BIGNUM *a = NULL, *b = NULL, *c = NULL;
2367 int i; 2528 int i;
2368 int rc = 1; 2529 int rc = 1;
2369 2530
2370 a = BN_new(); 2531 if ((a = BN_new()) == NULL)
2371 b = BN_new(); 2532 goto err;
2372 c = BN_new(); 2533 if ((b = BN_new()) == NULL)
2534 goto err;
2535 if ((c = BN_new()) == NULL)
2536 goto err;
2373 2537
2374 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0)); 2538 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
2375 a->neg = rand_neg(); 2539 BN_set_negative(a, rand_neg());
2376 for (i = 0; i < num0; i++) { 2540 for (i = 0; i < num0; i++) {
2377 CHECK_GOTO(BN_rshift1(b, a)); 2541 CHECK_GOTO(BN_rshift1(b, a));
2378 if (bp != NULL) { 2542 if (bp != NULL) {
@@ -2393,7 +2557,7 @@ test_rshift1(BIO *bp)
2393 } 2557 }
2394 CHECK_GOTO(BN_copy(a, b)); 2558 CHECK_GOTO(BN_copy(a, b));
2395 } 2559 }
2396err: 2560 err:
2397 BN_free(a); 2561 BN_free(a);
2398 BN_free(b); 2562 BN_free(b);
2399 BN_free(c); 2563 BN_free(c);
@@ -2413,7 +2577,7 @@ int
2413test_mod_exp_sizes(BIO *bp, BN_CTX *ctx) 2577test_mod_exp_sizes(BIO *bp, BN_CTX *ctx)
2414{ 2578{
2415 BN_MONT_CTX *mont_ctx = NULL; 2579 BN_MONT_CTX *mont_ctx = NULL;
2416 BIGNUM *p, *x, *y, *r, *r2; 2580 BIGNUM *p = NULL, *x = NULL, *y = NULL, *r = NULL, *r2 = NULL;
2417 int size; 2581 int size;
2418 int rc = 0; 2582 int rc = 0;
2419 2583
@@ -2455,7 +2619,7 @@ test_mod_exp_sizes(BIO *bp, BN_CTX *ctx)
2455 2619
2456 rc = 1; 2620 rc = 1;
2457 2621
2458err: 2622 err:
2459 BN_MONT_CTX_free(mont_ctx); 2623 BN_MONT_CTX_free(mont_ctx);
2460 BN_CTX_end(ctx); 2624 BN_CTX_end(ctx);
2461 return rc; 2625 return rc;