summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/bn/bn_test.c661
1 files changed, 297 insertions, 364 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_test.c b/src/regress/lib/libcrypto/bn/bn_test.c
index ce26a42c96..24a9fb8198 100644
--- a/src/regress/lib/libcrypto/bn/bn_test.c
+++ b/src/regress/lib/libcrypto/bn/bn_test.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_test.c,v 1.11 2023/04/07 22:30:31 tb Exp $ */ 1/* $OpenBSD: bn_test.c,v 1.12 2023/04/07 22:32:59 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 *
@@ -89,16 +89,16 @@ const int num0 = 100; /* number of tests */
89const int num1 = 50; /* additional tests for some functions */ 89const int num1 = 50; /* additional tests for some functions */
90const int num2 = 5; /* number of tests for slow functions */ 90const int num2 = 5; /* number of tests for slow functions */
91 91
92int test_add(BIO *bp); 92int test_add(BIO *bp, BN_CTX *ctx);
93int test_sub(BIO *bp); 93int test_sub(BIO *bp, BN_CTX *ctx);
94int test_lshift1(BIO *bp); 94int test_lshift1(BIO *bp, BN_CTX *ctx);
95int test_lshift(BIO *bp, BN_CTX *ctx, int use_lst); 95int test_lshift(BIO *bp, BN_CTX *ctx, int use_lst);
96int test_rshift1(BIO *bp); 96int test_rshift1(BIO *bp, BN_CTX *ctx);
97int test_rshift(BIO *bp, BN_CTX *ctx); 97int test_rshift(BIO *bp, BN_CTX *ctx);
98int test_div(BIO *bp, BN_CTX *ctx); 98int test_div(BIO *bp, BN_CTX *ctx);
99int test_div_word(BIO *bp); 99int test_div_word(BIO *bp, BN_CTX *ctx);
100int test_div_recp(BIO *bp, BN_CTX *ctx); 100int test_div_recp(BIO *bp, BN_CTX *ctx);
101int test_mul(BIO *bp); 101int test_mul(BIO *bp, BN_CTX *ctx);
102int test_sqr(BIO *bp, BN_CTX *ctx); 102int test_sqr(BIO *bp, BN_CTX *ctx);
103int test_mont(BIO *bp, BN_CTX *ctx); 103int test_mont(BIO *bp, BN_CTX *ctx);
104int test_mod(BIO *bp, BN_CTX *ctx); 104int test_mod(BIO *bp, BN_CTX *ctx);
@@ -108,8 +108,8 @@ int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
108int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx); 108int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
109int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx); 109int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx);
110int test_exp(BIO *bp, BN_CTX *ctx); 110int test_exp(BIO *bp, BN_CTX *ctx);
111int test_gf2m_add(BIO *bp); 111int test_gf2m_add(BIO *bp, BN_CTX *ctx);
112int test_gf2m_mod(BIO *bp); 112int test_gf2m_mod(BIO *bp, BN_CTX *ctx);
113int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx); 113int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
114int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx); 114int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
115int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx); 115int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
@@ -188,32 +188,32 @@ main(int argc, char *argv[])
188 BIO_puts(out, "obase=16\nibase=16\n"); 188 BIO_puts(out, "obase=16\nibase=16\n");
189 189
190 message(out, "BN_add"); 190 message(out, "BN_add");
191 if (!test_add(out)) 191 if (!test_add(out, ctx))
192 goto err; 192 goto err;
193 (void)BIO_flush(out); 193 (void)BIO_flush(out);
194 194
195 message(out, "BN_sub"); 195 message(out, "BN_sub");
196 if (!test_sub(out)) 196 if (!test_sub(out, ctx))
197 goto err; 197 goto err;
198 (void)BIO_flush(out); 198 (void)BIO_flush(out);
199 199
200 message(out, "BN_lshift1"); 200 message(out, "BN_lshift1");
201 if (!test_lshift1(out)) 201 if (!test_lshift1(out, ctx))
202 goto err; 202 goto err;
203 (void)BIO_flush(out); 203 (void)BIO_flush(out);
204 204
205 message(out, "BN_lshift (fixed)"); 205 message(out, "BN_lshift (fixed)");
206 if (!test_lshift(out, ctx, 1)) 206 if (!test_lshift(out, ctx, 0))
207 goto err; 207 goto err;
208 (void)BIO_flush(out); 208 (void)BIO_flush(out);
209 209
210 message(out, "BN_lshift"); 210 message(out, "BN_lshift");
211 if (!test_lshift(out, ctx, 0)) 211 if (!test_lshift(out, ctx, 1))
212 goto err; 212 goto err;
213 (void)BIO_flush(out); 213 (void)BIO_flush(out);
214 214
215 message(out, "BN_rshift1"); 215 message(out, "BN_rshift1");
216 if (!test_rshift1(out)) 216 if (!test_rshift1(out, ctx))
217 goto err; 217 goto err;
218 (void)BIO_flush(out); 218 (void)BIO_flush(out);
219 219
@@ -228,7 +228,7 @@ main(int argc, char *argv[])
228 (void)BIO_flush(out); 228 (void)BIO_flush(out);
229 229
230 message(out, "BN_mul"); 230 message(out, "BN_mul");
231 if (!test_mul(out)) 231 if (!test_mul(out, ctx))
232 goto err; 232 goto err;
233 (void)BIO_flush(out); 233 (void)BIO_flush(out);
234 234
@@ -238,7 +238,7 @@ main(int argc, char *argv[])
238 (void)BIO_flush(out); 238 (void)BIO_flush(out);
239 239
240 message(out, "BN_div_word"); 240 message(out, "BN_div_word");
241 if (!test_div_word(out)) 241 if (!test_div_word(out, ctx))
242 goto err; 242 goto err;
243 (void)BIO_flush(out); 243 (void)BIO_flush(out);
244 244
@@ -299,12 +299,12 @@ main(int argc, char *argv[])
299 299
300#ifndef OPENSSL_NO_EC2M 300#ifndef OPENSSL_NO_EC2M
301 message(out, "BN_GF2m_add"); 301 message(out, "BN_GF2m_add");
302 if (!test_gf2m_add(out)) 302 if (!test_gf2m_add(out, ctx))
303 goto err; 303 goto err;
304 (void)BIO_flush(out); 304 (void)BIO_flush(out);
305 305
306 message(out, "BN_GF2m_mod"); 306 message(out, "BN_GF2m_mod");
307 if (!test_gf2m_mod(out)) 307 if (!test_gf2m_mod(out, ctx))
308 goto err; 308 goto err;
309 (void)BIO_flush(out); 309 (void)BIO_flush(out);
310 310
@@ -343,6 +343,7 @@ main(int argc, char *argv[])
343 goto err; 343 goto err;
344 (void)BIO_flush(out); 344 (void)BIO_flush(out);
345#endif 345#endif
346
346 BN_CTX_free(ctx); 347 BN_CTX_free(ctx);
347 BIO_free(out); 348 BIO_free(out);
348 349
@@ -358,17 +359,19 @@ main(int argc, char *argv[])
358} 359}
359 360
360int 361int
361test_add(BIO *bp) 362test_add(BIO *bp, BN_CTX *ctx)
362{ 363{
363 BIGNUM *a = NULL, *b = NULL, *c = NULL; 364 BIGNUM *a, *b, *c;
364 int i; 365 int i;
365 int rc = 0; 366 int rc = 0;
366 367
367 if ((a = BN_new()) == NULL) 368 BN_CTX_start(ctx);
369
370 if ((a = BN_CTX_get(ctx)) == NULL)
368 goto err; 371 goto err;
369 if ((b = BN_new()) == NULL) 372 if ((b = BN_CTX_get(ctx)) == NULL)
370 goto err; 373 goto err;
371 if ((c = BN_new()) == NULL) 374 if ((c = BN_CTX_get(ctx)) == NULL)
372 goto err; 375 goto err;
373 376
374 CHECK_GOTO(BN_bntest_rand(a, 512, 0, 0)); 377 CHECK_GOTO(BN_bntest_rand(a, 512, 0, 0));
@@ -399,25 +402,25 @@ test_add(BIO *bp)
399 402
400 rc = 1; 403 rc = 1;
401 err: 404 err:
402 BN_free(a); 405 BN_CTX_end(ctx);
403 BN_free(b);
404 BN_free(c);
405 406
406 return rc; 407 return rc;
407} 408}
408 409
409int 410int
410test_sub(BIO *bp) 411test_sub(BIO *bp, BN_CTX *ctx)
411{ 412{
412 BIGNUM *a = NULL, *b = NULL, *c = NULL; 413 BIGNUM *a, *b, *c;
413 int i; 414 int i;
414 int rc = 0; 415 int rc = 0;
415 416
416 if ((a = BN_new()) == NULL) 417 BN_CTX_start(ctx);
418
419 if ((a = BN_CTX_get(ctx)) == NULL)
417 goto err; 420 goto err;
418 if ((b = BN_new()) == NULL) 421 if ((b = BN_CTX_get(ctx)) == NULL)
419 goto err; 422 goto err;
420 if ((c = BN_new()) == NULL) 423 if ((c = BN_CTX_get(ctx)) == NULL)
421 goto err; 424 goto err;
422 425
423 for (i = 0; i < num0 + num1; i++) { 426 for (i = 0; i < num0 + num1; i++) {
@@ -453,9 +456,7 @@ test_sub(BIO *bp)
453 456
454 rc = 1; 457 rc = 1;
455 err: 458 err:
456 BN_free(a); 459 BN_CTX_end(ctx);
457 BN_free(b);
458 BN_free(c);
459 460
460 return rc; 461 return rc;
461} 462}
@@ -463,19 +464,21 @@ test_sub(BIO *bp)
463int 464int
464test_div(BIO *bp, BN_CTX *ctx) 465test_div(BIO *bp, BN_CTX *ctx)
465{ 466{
466 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 467 BIGNUM *a, *b, *c, *d, *e;
467 int i; 468 int i;
468 int rc = 0; 469 int rc = 0;
469 470
470 if ((a = BN_new()) == NULL) 471 BN_CTX_start(ctx);
472
473 if ((a = BN_CTX_get(ctx)) == NULL)
471 goto err; 474 goto err;
472 if ((b = BN_new()) == NULL) 475 if ((b = BN_CTX_get(ctx)) == NULL)
473 goto err; 476 goto err;
474 if ((c = BN_new()) == NULL) 477 if ((c = BN_CTX_get(ctx)) == NULL)
475 goto err; 478 goto err;
476 if ((d = BN_new()) == NULL) 479 if ((d = BN_CTX_get(ctx)) == NULL)
477 goto err; 480 goto err;
478 if ((e = BN_new()) == NULL) 481 if ((e = BN_CTX_get(ctx)) == NULL)
479 goto err; 482 goto err;
480 483
481 CHECK_GOTO(BN_one(a)); 484 CHECK_GOTO(BN_one(a));
@@ -528,11 +531,7 @@ test_div(BIO *bp, BN_CTX *ctx)
528 531
529 rc = 1; 532 rc = 1;
530 err: 533 err:
531 BN_free(a); 534 BN_CTX_end(ctx);
532 BN_free(b);
533 BN_free(c);
534 BN_free(d);
535 BN_free(e);
536 535
537 return rc; 536 return rc;
538} 537}
@@ -555,16 +554,18 @@ print_word(BIO *bp, BN_ULONG w)
555} 554}
556 555
557int 556int
558test_div_word(BIO *bp) 557test_div_word(BIO *bp, BN_CTX *ctx)
559{ 558{
560 BIGNUM *a = NULL, *b = NULL; 559 BIGNUM *a, *b;
561 BN_ULONG r, rmod, s = 0; 560 BN_ULONG r, rmod, s = 0;
562 int i; 561 int i;
563 int rc = 0; 562 int rc = 0;
564 563
565 if ((a = BN_new()) == NULL) 564 BN_CTX_start(ctx);
565
566 if ((a = BN_CTX_get(ctx)) == NULL)
566 goto err; 567 goto err;
567 if ((b = BN_new()) == NULL) 568 if ((b = BN_CTX_get(ctx)) == NULL)
568 goto err; 569 goto err;
569 570
570 for (i = 0; i < num0; i++) { 571 for (i = 0; i < num0; i++) {
@@ -619,8 +620,7 @@ test_div_word(BIO *bp)
619 620
620 rc = 1; 621 rc = 1;
621 err: 622 err:
622 BN_free(a); 623 BN_CTX_end(ctx);
623 BN_free(b);
624 624
625 return rc; 625 return rc;
626} 626}
@@ -628,20 +628,22 @@ test_div_word(BIO *bp)
628int 628int
629test_div_recp(BIO *bp, BN_CTX *ctx) 629test_div_recp(BIO *bp, BN_CTX *ctx)
630{ 630{
631 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
632 BN_RECP_CTX *recp = NULL; 631 BN_RECP_CTX *recp = NULL;
632 BIGNUM *a, *b, *c, *d, *e;
633 int i; 633 int i;
634 int rc = 0; 634 int rc = 0;
635 635
636 if ((a = BN_new()) == NULL) 636 BN_CTX_start(ctx);
637
638 if ((a = BN_CTX_get(ctx)) == NULL)
637 goto err; 639 goto err;
638 if ((b = BN_new()) == NULL) 640 if ((b = BN_CTX_get(ctx)) == NULL)
639 goto err; 641 goto err;
640 if ((c = BN_new()) == NULL) 642 if ((c = BN_CTX_get(ctx)) == NULL)
641 goto err; 643 goto err;
642 if ((d = BN_new()) == NULL) 644 if ((d = BN_CTX_get(ctx)) == NULL)
643 goto err; 645 goto err;
644 if ((e = BN_new()) == NULL) 646 if ((e = BN_CTX_get(ctx)) == NULL)
645 goto err; 647 goto err;
646 648
647 if ((recp = BN_RECP_CTX_new()) == NULL) 649 if ((recp = BN_RECP_CTX_new()) == NULL)
@@ -694,37 +696,30 @@ test_div_recp(BIO *bp, BN_CTX *ctx)
694 696
695 rc = 1; 697 rc = 1;
696 err: 698 err:
697 BN_free(a); 699 BN_CTX_end(ctx);
698 BN_free(b);
699 BN_free(c);
700 BN_free(d);
701 BN_free(e);
702 BN_RECP_CTX_free(recp); 700 BN_RECP_CTX_free(recp);
703 701
704 return rc; 702 return rc;
705} 703}
706 704
707int 705int
708test_mul(BIO *bp) 706test_mul(BIO *bp, BN_CTX *ctx)
709{ 707{
710 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 708 BIGNUM *a, *b, *c, *d, *e;
711 int i; 709 int i;
712 int rc = 0; 710 int rc = 0;
713 BN_CTX *ctx;
714 711
715 ctx = BN_CTX_new(); 712 BN_CTX_start(ctx);
716 if (ctx == NULL)
717 exit(1);
718 713
719 if ((a = BN_new()) == NULL) 714 if ((a = BN_CTX_get(ctx)) == NULL)
720 goto err; 715 goto err;
721 if ((b = BN_new()) == NULL) 716 if ((b = BN_CTX_get(ctx)) == NULL)
722 goto err; 717 goto err;
723 if ((c = BN_new()) == NULL) 718 if ((c = BN_CTX_get(ctx)) == NULL)
724 goto err; 719 goto err;
725 if ((d = BN_new()) == NULL) 720 if ((d = BN_CTX_get(ctx)) == NULL)
726 goto err; 721 goto err;
727 if ((e = BN_new()) == NULL) 722 if ((e = BN_CTX_get(ctx)) == NULL)
728 goto err; 723 goto err;
729 724
730 for (i = 0; i < num0 + num1; i++) { 725 for (i = 0; i < num0 + num1; i++) {
@@ -756,12 +751,7 @@ test_mul(BIO *bp)
756 751
757 rc = 1; 752 rc = 1;
758 err: 753 err:
759 BN_free(a); 754 BN_CTX_end(ctx);
760 BN_free(b);
761 BN_free(c);
762 BN_free(d);
763 BN_free(e);
764 BN_CTX_free(ctx);
765 755
766 return rc; 756 return rc;
767} 757}
@@ -769,17 +759,19 @@ test_mul(BIO *bp)
769int 759int
770test_sqr(BIO *bp, BN_CTX *ctx) 760test_sqr(BIO *bp, BN_CTX *ctx)
771{ 761{
772 BIGNUM *a = NULL, *c = NULL, *d = NULL, *e = NULL; 762 BIGNUM *a, *c, *d, *e;
773 int i; 763 int i;
774 int rc = 0; 764 int rc = 0;
775 765
776 if ((a = BN_new()) == NULL) 766 BN_CTX_start(ctx);
767
768 if ((a = BN_CTX_get(ctx)) == NULL)
777 goto err; 769 goto err;
778 if ((c = BN_new()) == NULL) 770 if ((c = BN_CTX_get(ctx)) == NULL)
779 goto err; 771 goto err;
780 if ((d = BN_new()) == NULL) 772 if ((d = BN_CTX_get(ctx)) == NULL)
781 goto err; 773 goto err;
782 if ((e = BN_new()) == NULL) 774 if ((e = BN_CTX_get(ctx)) == NULL)
783 goto err; 775 goto err;
784 776
785 for (i = 0; i < num0; i++) { 777 for (i = 0; i < num0; i++) {
@@ -855,10 +847,7 @@ test_sqr(BIO *bp, BN_CTX *ctx)
855 847
856 rc = 1; 848 rc = 1;
857 err: 849 err:
858 BN_free(a); 850 BN_CTX_end(ctx);
859 BN_free(c);
860 BN_free(d);
861 BN_free(e);
862 851
863 return rc; 852 return rc;
864} 853}
@@ -866,29 +855,29 @@ test_sqr(BIO *bp, BN_CTX *ctx)
866int 855int
867test_mont(BIO *bp, BN_CTX *ctx) 856test_mont(BIO *bp, BN_CTX *ctx)
868{ 857{
869 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *A = NULL, *B = NULL; 858 BN_MONT_CTX *mont = NULL;
870 BIGNUM *n = NULL; 859 BIGNUM *a, *b, *c, *d, *A, *B, *n;
871 int i; 860 int i;
872 int rc = 0; 861 int rc = 0;
873 BN_MONT_CTX *mont;
874 862
875 mont = BN_MONT_CTX_new(); 863 BN_CTX_start(ctx);
876 if (mont == NULL)
877 return 0;
878 864
879 if ((a = BN_new()) == NULL) 865 if ((a = BN_CTX_get(ctx)) == NULL)
866 goto err;
867 if ((b = BN_CTX_get(ctx)) == NULL)
880 goto err; 868 goto err;
881 if ((b = BN_new()) == NULL) 869 if ((c = BN_CTX_get(ctx)) == NULL)
882 goto err; 870 goto err;
883 if ((c = BN_new()) == NULL) 871 if ((d = BN_CTX_get(ctx)) == NULL)
884 goto err; 872 goto err;
885 if ((d = BN_new()) == NULL) 873 if ((A = BN_CTX_get(ctx)) == NULL)
886 goto err; 874 goto err;
887 if ((A = BN_new()) == NULL) 875 if ((B = BN_CTX_get(ctx)) == NULL)
888 goto err; 876 goto err;
889 if ((B = BN_new()) == NULL) 877 if ((n = BN_CTX_get(ctx)) == NULL)
890 goto err; 878 goto err;
891 if ((n = BN_new()) == NULL) 879
880 if ((mont = BN_MONT_CTX_new()) == NULL)
892 goto err; 881 goto err;
893 882
894 CHECK_GOTO(BN_zero(n)); 883 CHECK_GOTO(BN_zero(n));
@@ -946,14 +935,8 @@ test_mont(BIO *bp, BN_CTX *ctx)
946 935
947 rc = 1; 936 rc = 1;
948 err: 937 err:
938 BN_CTX_end(ctx);
949 BN_MONT_CTX_free(mont); 939 BN_MONT_CTX_free(mont);
950 BN_free(a);
951 BN_free(b);
952 BN_free(c);
953 BN_free(d);
954 BN_free(A);
955 BN_free(B);
956 BN_free(n);
957 940
958 return rc; 941 return rc;
959} 942}
@@ -961,19 +944,21 @@ test_mont(BIO *bp, BN_CTX *ctx)
961int 944int
962test_mod(BIO *bp, BN_CTX *ctx) 945test_mod(BIO *bp, BN_CTX *ctx)
963{ 946{
964 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 947 BIGNUM *a, *b, *c, *d, *e;
965 int i; 948 int i;
966 int rc = 0; 949 int rc = 0;
967 950
968 if ((a = BN_new()) == NULL) 951 BN_CTX_start(ctx);
952
953 if ((a = BN_CTX_get(ctx)) == NULL)
969 goto err; 954 goto err;
970 if ((b = BN_new()) == NULL) 955 if ((b = BN_CTX_get(ctx)) == NULL)
971 goto err; 956 goto err;
972 if ((c = BN_new()) == NULL) 957 if ((c = BN_CTX_get(ctx)) == NULL)
973 goto err; 958 goto err;
974 if ((d = BN_new()) == NULL) 959 if ((d = BN_CTX_get(ctx)) == NULL)
975 goto err; 960 goto err;
976 if ((e = BN_new()) == NULL) 961 if ((e = BN_CTX_get(ctx)) == NULL)
977 goto err; 962 goto err;
978 963
979 CHECK_GOTO(BN_bntest_rand(a, 1024, 0, 0)); 964 CHECK_GOTO(BN_bntest_rand(a, 1024, 0, 0));
@@ -1002,11 +987,7 @@ test_mod(BIO *bp, BN_CTX *ctx)
1002 987
1003 rc = 1; 988 rc = 1;
1004 err: 989 err:
1005 BN_free(a); 990 BN_CTX_end(ctx);
1006 BN_free(b);
1007 BN_free(c);
1008 BN_free(d);
1009 BN_free(e);
1010 991
1011 return rc; 992 return rc;
1012} 993}
@@ -1014,19 +995,21 @@ test_mod(BIO *bp, BN_CTX *ctx)
1014int 995int
1015test_mod_mul(BIO *bp, BN_CTX *ctx) 996test_mod_mul(BIO *bp, BN_CTX *ctx)
1016{ 997{
1017 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 998 BIGNUM *a, *b, *c, *d, *e;
1018 int i, j; 999 int i, j;
1019 int rc = 0; 1000 int rc = 0;
1020 1001
1021 if ((a = BN_new()) == NULL) 1002 BN_CTX_start(ctx);
1003
1004 if ((a = BN_CTX_get(ctx)) == NULL)
1022 goto err; 1005 goto err;
1023 if ((b = BN_new()) == NULL) 1006 if ((b = BN_CTX_get(ctx)) == NULL)
1024 goto err; 1007 goto err;
1025 if ((c = BN_new()) == NULL) 1008 if ((c = BN_CTX_get(ctx)) == NULL)
1026 goto err; 1009 goto err;
1027 if ((d = BN_new()) == NULL) 1010 if ((d = BN_CTX_get(ctx)) == NULL)
1028 goto err; 1011 goto err;
1029 if ((e = BN_new()) == NULL) 1012 if ((e = BN_CTX_get(ctx)) == NULL)
1030 goto err; 1013 goto err;
1031 1014
1032 CHECK_GOTO(BN_one(a)); 1015 CHECK_GOTO(BN_one(a));
@@ -1088,11 +1071,7 @@ test_mod_mul(BIO *bp, BN_CTX *ctx)
1088 1071
1089 rc = 1; 1072 rc = 1;
1090 err: 1073 err:
1091 BN_free(a); 1074 BN_CTX_end(ctx);
1092 BN_free(b);
1093 BN_free(c);
1094 BN_free(d);
1095 BN_free(e);
1096 1075
1097 return rc; 1076 return rc;
1098} 1077}
@@ -1100,19 +1079,21 @@ test_mod_mul(BIO *bp, BN_CTX *ctx)
1100int 1079int
1101test_mod_exp(BIO *bp, BN_CTX *ctx) 1080test_mod_exp(BIO *bp, BN_CTX *ctx)
1102{ 1081{
1103 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 1082 BIGNUM *a, *b, *c, *d, *e;
1104 int i; 1083 int i;
1105 int rc = 0; 1084 int rc = 0;
1106 1085
1107 if ((a = BN_new()) == NULL) 1086 BN_CTX_start(ctx);
1087
1088 if ((a = BN_CTX_get(ctx)) == NULL)
1108 goto err; 1089 goto err;
1109 if ((b = BN_new()) == NULL) 1090 if ((b = BN_CTX_get(ctx)) == NULL)
1110 goto err; 1091 goto err;
1111 if ((c = BN_new()) == NULL) 1092 if ((c = BN_CTX_get(ctx)) == NULL)
1112 goto err; 1093 goto err;
1113 if ((d = BN_new()) == NULL) 1094 if ((d = BN_CTX_get(ctx)) == NULL)
1114 goto err; 1095 goto err;
1115 if ((e = BN_new()) == NULL) 1096 if ((e = BN_CTX_get(ctx)) == NULL)
1116 goto err; 1097 goto err;
1117 1098
1118 CHECK_GOTO(BN_one(a)); 1099 CHECK_GOTO(BN_one(a));
@@ -1223,11 +1204,7 @@ test_mod_exp(BIO *bp, BN_CTX *ctx)
1223 1204
1224 rc = 1; 1205 rc = 1;
1225 err: 1206 err:
1226 BN_free(a); 1207 BN_CTX_end(ctx);
1227 BN_free(b);
1228 BN_free(c);
1229 BN_free(d);
1230 BN_free(e);
1231 1208
1232 return rc; 1209 return rc;
1233} 1210}
@@ -1235,19 +1212,21 @@ test_mod_exp(BIO *bp, BN_CTX *ctx)
1235int 1212int
1236test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) 1213test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1237{ 1214{
1238 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 1215 BIGNUM *a, *b, *c, *d, *e;
1239 int i; 1216 int i;
1240 int rc = 0; 1217 int rc = 0;
1241 1218
1242 if ((a = BN_new()) == NULL) 1219 BN_CTX_start(ctx);
1220
1221 if ((a = BN_CTX_get(ctx)) == NULL)
1243 goto err; 1222 goto err;
1244 if ((b = BN_new()) == NULL) 1223 if ((b = BN_CTX_get(ctx)) == NULL)
1245 goto err; 1224 goto err;
1246 if ((c = BN_new()) == NULL) 1225 if ((c = BN_CTX_get(ctx)) == NULL)
1247 goto err; 1226 goto err;
1248 if ((d = BN_new()) == NULL) 1227 if ((d = BN_CTX_get(ctx)) == NULL)
1249 goto err; 1228 goto err;
1250 if ((e = BN_new()) == NULL) 1229 if ((e = BN_CTX_get(ctx)) == NULL)
1251 goto err; 1230 goto err;
1252 1231
1253 CHECK_GOTO(BN_one(a)); 1232 CHECK_GOTO(BN_one(a));
@@ -1299,11 +1278,7 @@ test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1299 1278
1300 rc = 1; 1279 rc = 1;
1301 err: 1280 err:
1302 BN_free(a); 1281 BN_CTX_end(ctx);
1303 BN_free(b);
1304 BN_free(c);
1305 BN_free(d);
1306 BN_free(e);
1307 1282
1308 return rc; 1283 return rc;
1309} 1284}
@@ -1315,27 +1290,29 @@ test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1315int 1290int
1316test_mod_exp_mont5(BIO *bp, BN_CTX *ctx) 1291test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1317{ 1292{
1318 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL; 1293 BIGNUM *a, *p, *m, *d, *e;
1319 BIGNUM *b = NULL, *n = NULL, *c = NULL; 1294 BIGNUM *b, *n, *c;
1320 BN_MONT_CTX *mont = NULL; 1295 BN_MONT_CTX *mont = NULL;
1321 int len; 1296 int len;
1322 int rc = 0; 1297 int rc = 0;
1323 1298
1324 if ((a = BN_new()) == NULL) 1299 BN_CTX_start(ctx);
1300
1301 if ((a = BN_CTX_get(ctx)) == NULL)
1325 goto err; 1302 goto err;
1326 if ((p = BN_new()) == NULL) 1303 if ((p = BN_CTX_get(ctx)) == NULL)
1327 goto err; 1304 goto err;
1328 if ((m = BN_new()) == NULL) 1305 if ((m = BN_CTX_get(ctx)) == NULL)
1329 goto err; 1306 goto err;
1330 if ((d = BN_new()) == NULL) 1307 if ((d = BN_CTX_get(ctx)) == NULL)
1331 goto err; 1308 goto err;
1332 if ((e = BN_new()) == NULL) 1309 if ((e = BN_CTX_get(ctx)) == NULL)
1333 goto err; 1310 goto err;
1334 if ((b = BN_new()) == NULL) 1311 if ((b = BN_CTX_get(ctx)) == NULL)
1335 goto err; 1312 goto err;
1336 if ((n = BN_new()) == NULL) 1313 if ((n = BN_CTX_get(ctx)) == NULL)
1337 goto err; 1314 goto err;
1338 if ((c = BN_new()) == NULL) 1315 if ((c = BN_CTX_get(ctx)) == NULL)
1339 goto err; 1316 goto err;
1340 1317
1341 CHECK_GOTO(mont = BN_MONT_CTX_new()); 1318 CHECK_GOTO(mont = BN_MONT_CTX_new());
@@ -1462,14 +1439,7 @@ test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1462 1439
1463 rc = 1; 1440 rc = 1;
1464 err: 1441 err:
1465 BN_free(a); 1442 BN_CTX_end(ctx);
1466 BN_free(p);
1467 BN_free(m);
1468 BN_free(d);
1469 BN_free(e);
1470 BN_free(b);
1471 BN_free(n);
1472 BN_free(c);
1473 BN_MONT_CTX_free(mont); 1443 BN_MONT_CTX_free(mont);
1474 1444
1475 return rc; 1445 return rc;
@@ -1478,21 +1448,20 @@ test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1478int 1448int
1479test_exp(BIO *bp, BN_CTX *ctx) 1449test_exp(BIO *bp, BN_CTX *ctx)
1480{ 1450{
1481 BIGNUM *a = NULL, *b = NULL, *d = NULL, *e = NULL, *one = NULL; 1451 BIGNUM *a, *b, *d, *e;
1482 int i; 1452 int i;
1483 int rc = 0; 1453 int rc = 0;
1484 1454
1485 if ((a = BN_new()) == NULL) 1455 BN_CTX_start(ctx);
1486 goto err; 1456
1487 if ((b = BN_new()) == NULL) 1457 if ((a = BN_CTX_get(ctx)) == NULL)
1488 goto err; 1458 goto err;
1489 if ((d = BN_new()) == NULL) 1459 if ((b = BN_CTX_get(ctx)) == NULL)
1490 goto err; 1460 goto err;
1491 if ((e = BN_new()) == NULL) 1461 if ((d = BN_CTX_get(ctx)) == NULL)
1492 goto err; 1462 goto err;
1493 if ((one = BN_new()) == NULL) 1463 if ((e = BN_CTX_get(ctx)) == NULL)
1494 goto err; 1464 goto err;
1495 CHECK_GOTO(BN_one(one));
1496 1465
1497 for (i = 0; i < num2; i++) { 1466 for (i = 0; i < num2; i++) {
1498 CHECK_GOTO(BN_bntest_rand(a, 20 + i * 5, 0, 0)); 1467 CHECK_GOTO(BN_bntest_rand(a, 20 + i * 5, 0, 0));
@@ -1512,7 +1481,7 @@ test_exp(BIO *bp, BN_CTX *ctx)
1512 BIO_puts(bp, "\n"); 1481 BIO_puts(bp, "\n");
1513 } 1482 }
1514 CHECK_GOTO(BN_one(e)); 1483 CHECK_GOTO(BN_one(e));
1515 for (; !BN_is_zero(b); BN_sub(b, b, one)) 1484 for (; !BN_is_zero(b); BN_sub_word(b, 1))
1516 CHECK_GOTO(BN_mul(e, e, a, ctx)); 1485 CHECK_GOTO(BN_mul(e, e, a, ctx));
1517 CHECK_GOTO(BN_sub(e, e, d)); 1486 CHECK_GOTO(BN_sub(e, e, d));
1518 if (!BN_is_zero(e)) { 1487 if (!BN_is_zero(e)) {
@@ -1523,28 +1492,26 @@ test_exp(BIO *bp, BN_CTX *ctx)
1523 1492
1524 rc = 1; 1493 rc = 1;
1525 err: 1494 err:
1526 BN_free(a); 1495 BN_CTX_end(ctx);
1527 BN_free(b);
1528 BN_free(d);
1529 BN_free(e);
1530 BN_free(one);
1531 1496
1532 return rc; 1497 return rc;
1533} 1498}
1534 1499
1535#ifndef OPENSSL_NO_EC2M 1500#ifndef OPENSSL_NO_EC2M
1536int 1501int
1537test_gf2m_add(BIO *bp) 1502test_gf2m_add(BIO *bp, BN_CTX *ctx)
1538{ 1503{
1539 BIGNUM *a = NULL, *b = NULL, *c = NULL; 1504 BIGNUM *a, *b, *c;
1540 int i; 1505 int i;
1541 int rc = 0; 1506 int rc = 0;
1542 1507
1543 if ((a = BN_new()) == NULL) 1508 BN_CTX_start(ctx);
1509
1510 if ((a = BN_CTX_get(ctx)) == NULL)
1544 goto err; 1511 goto err;
1545 if ((b = BN_new()) == NULL) 1512 if ((b = BN_CTX_get(ctx)) == NULL)
1546 goto err; 1513 goto err;
1547 if ((c = BN_new()) == NULL) 1514 if ((c = BN_CTX_get(ctx)) == NULL)
1548 goto err; 1515 goto err;
1549 1516
1550 for (i = 0; i < num0; i++) { 1517 for (i = 0; i < num0; i++) {
@@ -1581,33 +1548,33 @@ test_gf2m_add(BIO *bp)
1581 1548
1582 rc = 1; 1549 rc = 1;
1583 err: 1550 err:
1584 BN_free(a); 1551 BN_CTX_end(ctx);
1585 BN_free(b);
1586 BN_free(c);
1587 1552
1588 return rc; 1553 return rc;
1589} 1554}
1590 1555
1591int 1556int
1592test_gf2m_mod(BIO *bp) 1557test_gf2m_mod(BIO *bp, BN_CTX *ctx)
1593{ 1558{
1594 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL; 1559 BIGNUM *a, *b[2] = { 0 }, *c, *d, *e;
1595 int i, j;
1596 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1560 int p0[] = { 163, 7, 6, 3, 0, -1 };
1597 int p1[] = { 193, 15, 0, -1 }; 1561 int p1[] = { 193, 15, 0, -1, 0 };
1562 int i, j;
1598 int rc = 0; 1563 int rc = 0;
1599 1564
1600 if ((a = BN_new()) == NULL) 1565 BN_CTX_start(ctx);
1566
1567 if ((a = BN_CTX_get(ctx)) == NULL)
1601 goto err; 1568 goto err;
1602 if ((b[0] = BN_new()) == NULL) 1569 if ((b[0] = BN_CTX_get(ctx)) == NULL)
1603 goto err; 1570 goto err;
1604 if ((b[1] = BN_new()) == NULL) 1571 if ((b[1] = BN_CTX_get(ctx)) == NULL)
1605 goto err; 1572 goto err;
1606 if ((c = BN_new()) == NULL) 1573 if ((c = BN_CTX_get(ctx)) == NULL)
1607 goto err; 1574 goto err;
1608 if ((d = BN_new()) == NULL) 1575 if ((d = BN_CTX_get(ctx)) == NULL)
1609 goto err; 1576 goto err;
1610 if ((e = BN_new()) == NULL) 1577 if ((e = BN_CTX_get(ctx)) == NULL)
1611 goto err; 1578 goto err;
1612 1579
1613 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1580 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -1641,12 +1608,7 @@ test_gf2m_mod(BIO *bp)
1641 1608
1642 rc = 1; 1609 rc = 1;
1643 err: 1610 err:
1644 BN_free(a); 1611 BN_CTX_end(ctx);
1645 BN_free(b[0]);
1646 BN_free(b[1]);
1647 BN_free(c);
1648 BN_free(d);
1649 BN_free(e);
1650 1612
1651 return rc; 1613 return rc;
1652} 1614}
@@ -1654,30 +1616,31 @@ test_gf2m_mod(BIO *bp)
1654int 1616int
1655test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx) 1617test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1656{ 1618{
1657 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL; 1619 BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f, *g, *h;
1658 BIGNUM *g = NULL, *h = NULL;
1659 int i, j;
1660 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1620 int p0[] = { 163, 7, 6, 3, 0, -1 };
1661 int p1[] = { 193, 15, 0, -1 }; 1621 int p1[] = { 193, 15, 0, -1 };
1622 int i, j;
1662 int rc = 0; 1623 int rc = 0;
1663 1624
1664 if ((a = BN_new()) == NULL) 1625 BN_CTX_start(ctx);
1626
1627 if ((a = BN_CTX_get(ctx)) == NULL)
1665 goto err; 1628 goto err;
1666 if ((b[0] = BN_new()) == NULL) 1629 if ((b[0] = BN_CTX_get(ctx)) == NULL)
1667 goto err; 1630 goto err;
1668 if ((b[1] = BN_new()) == NULL) 1631 if ((b[1] = BN_CTX_get(ctx)) == NULL)
1669 goto err; 1632 goto err;
1670 if ((c = BN_new()) == NULL) 1633 if ((c = BN_CTX_get(ctx)) == NULL)
1671 goto err; 1634 goto err;
1672 if ((d = BN_new()) == NULL) 1635 if ((d = BN_CTX_get(ctx)) == NULL)
1673 goto err; 1636 goto err;
1674 if ((e = BN_new()) == NULL) 1637 if ((e = BN_CTX_get(ctx)) == NULL)
1675 goto err; 1638 goto err;
1676 if ((f = BN_new()) == NULL) 1639 if ((f = BN_CTX_get(ctx)) == NULL)
1677 goto err; 1640 goto err;
1678 if ((g = BN_new()) == NULL) 1641 if ((g = BN_CTX_get(ctx)) == NULL)
1679 goto err; 1642 goto err;
1680 if ((h = BN_new()) == NULL) 1643 if ((h = BN_CTX_get(ctx)) == NULL)
1681 goto err; 1644 goto err;
1682 1645
1683 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1646 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -1718,15 +1681,7 @@ test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1718 1681
1719 rc = 1; 1682 rc = 1;
1720 err: 1683 err:
1721 BN_free(a); 1684 BN_CTX_end(ctx);
1722 BN_free(b[0]);
1723 BN_free(b[1]);
1724 BN_free(c);
1725 BN_free(d);
1726 BN_free(e);
1727 BN_free(f);
1728 BN_free(g);
1729 BN_free(h);
1730 1685
1731 return rc; 1686 return rc;
1732} 1687}
@@ -1734,21 +1689,23 @@ test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1734int 1689int
1735test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx) 1690test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1736{ 1691{
1737 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL; 1692 BIGNUM *a, *b[2] = { 0 }, *c, *d;
1738 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1693 int p0[] = { 163, 7, 6, 3, 0, -1 };
1739 int p1[] = { 193, 15, 0, -1 }; 1694 int p1[] = { 193, 15, 0, -1 };
1740 int i, j; 1695 int i, j;
1741 int rc = 0; 1696 int rc = 0;
1742 1697
1743 if ((a = BN_new()) == NULL) 1698 BN_CTX_start(ctx);
1699
1700 if ((a = BN_CTX_get(ctx)) == NULL)
1744 goto err; 1701 goto err;
1745 if ((b[0] = BN_new()) == NULL) 1702 if ((b[0] = BN_CTX_get(ctx)) == NULL)
1746 goto err; 1703 goto err;
1747 if ((b[1] = BN_new()) == NULL) 1704 if ((b[1] = BN_CTX_get(ctx)) == NULL)
1748 goto err; 1705 goto err;
1749 if ((c = BN_new()) == NULL) 1706 if ((c = BN_CTX_get(ctx)) == NULL)
1750 goto err; 1707 goto err;
1751 if ((d = BN_new()) == NULL) 1708 if ((d = BN_CTX_get(ctx)) == NULL)
1752 goto err; 1709 goto err;
1753 1710
1754 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1711 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -1785,11 +1742,7 @@ test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1785 1742
1786 rc = 1; 1743 rc = 1;
1787 err: 1744 err:
1788 BN_free(a); 1745 BN_CTX_end(ctx);
1789 BN_free(b[0]);
1790 BN_free(b[1]);
1791 BN_free(c);
1792 BN_free(d);
1793 1746
1794 return rc; 1747 return rc;
1795} 1748}
@@ -1797,21 +1750,23 @@ test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1797int 1750int
1798test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx) 1751test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1799{ 1752{
1800 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL; 1753 BIGNUM *a, *b[2] = { 0 }, *c, *d;
1801 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1754 int p0[] = { 163, 7, 6, 3, 0, -1 };
1802 int p1[] = { 193, 15, 0, -1 }; 1755 int p1[] = { 193, 15, 0, -1 };
1803 int i, j; 1756 int i, j;
1804 int rc = 0; 1757 int rc = 0;
1805 1758
1806 if ((a = BN_new()) == NULL) 1759 BN_CTX_start(ctx);
1760
1761 if ((a = BN_CTX_get(ctx)) == NULL)
1807 goto err; 1762 goto err;
1808 if ((b[0] = BN_new()) == NULL) 1763 if ((b[0] = BN_CTX_get(ctx)) == NULL)
1809 goto err; 1764 goto err;
1810 if ((b[1] = BN_new()) == NULL) 1765 if ((b[1] = BN_CTX_get(ctx)) == NULL)
1811 goto err; 1766 goto err;
1812 if ((c = BN_new()) == NULL) 1767 if ((c = BN_CTX_get(ctx)) == NULL)
1813 goto err; 1768 goto err;
1814 if ((d = BN_new()) == NULL) 1769 if ((d = BN_CTX_get(ctx)) == NULL)
1815 goto err; 1770 goto err;
1816 1771
1817 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1772 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -1844,11 +1799,7 @@ test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1844 1799
1845 rc = 1; 1800 rc = 1;
1846 err: 1801 err:
1847 BN_free(a); 1802 BN_CTX_end(ctx);
1848 BN_free(b[0]);
1849 BN_free(b[1]);
1850 BN_free(c);
1851 BN_free(d);
1852 1803
1853 return rc; 1804 return rc;
1854} 1805}
@@ -1856,25 +1807,27 @@ test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1856int 1807int
1857test_gf2m_mod_div(BIO *bp, BN_CTX *ctx) 1808test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1858{ 1809{
1859 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL; 1810 BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f;
1860 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1811 int p0[] = { 163, 7, 6, 3, 0, -1 };
1861 int p1[] = { 193, 15, 0, -1 }; 1812 int p1[] = { 193, 15, 0, -1 };
1862 int i, j; 1813 int i, j;
1863 int rc = 0; 1814 int rc = 0;
1864 1815
1865 if ((a = BN_new()) == NULL) 1816 BN_CTX_start(ctx);
1817
1818 if ((a = BN_CTX_get(ctx)) == NULL)
1866 goto err; 1819 goto err;
1867 if ((b[0] = BN_new()) == NULL) 1820 if ((b[0] = BN_CTX_get(ctx)) == NULL)
1868 goto err; 1821 goto err;
1869 if ((b[1] = BN_new()) == NULL) 1822 if ((b[1] = BN_CTX_get(ctx)) == NULL)
1870 goto err; 1823 goto err;
1871 if ((c = BN_new()) == NULL) 1824 if ((c = BN_CTX_get(ctx)) == NULL)
1872 goto err; 1825 goto err;
1873 if ((d = BN_new()) == NULL) 1826 if ((d = BN_CTX_get(ctx)) == NULL)
1874 goto err; 1827 goto err;
1875 if ((e = BN_new()) == NULL) 1828 if ((e = BN_CTX_get(ctx)) == NULL)
1876 goto err; 1829 goto err;
1877 if ((f = BN_new()) == NULL) 1830 if ((f = BN_CTX_get(ctx)) == NULL)
1878 goto err; 1831 goto err;
1879 1832
1880 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1833 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -1911,13 +1864,7 @@ test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1911 1864
1912 rc = 1; 1865 rc = 1;
1913 err: 1866 err:
1914 BN_free(a); 1867 BN_CTX_end(ctx);
1915 BN_free(b[0]);
1916 BN_free(b[1]);
1917 BN_free(c);
1918 BN_free(d);
1919 BN_free(e);
1920 BN_free(f);
1921 1868
1922 return rc; 1869 return rc;
1923} 1870}
@@ -1925,25 +1872,27 @@ test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1925int 1872int
1926test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx) 1873test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1927{ 1874{
1928 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL; 1875 BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f;
1929 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1876 int p0[] = { 163, 7, 6, 3, 0, -1 };
1930 int p1[] = { 193, 15, 0, -1 }; 1877 int p1[] = { 193, 15, 0, -1 };
1931 int i, j; 1878 int i, j;
1932 int rc = 0; 1879 int rc = 0;
1933 1880
1934 if ((a = BN_new()) == NULL) 1881 BN_CTX_start(ctx);
1882
1883 if ((a = BN_CTX_get(ctx)) == NULL)
1935 goto err; 1884 goto err;
1936 if ((b[0] = BN_new()) == NULL) 1885 if ((b[0] = BN_CTX_get(ctx)) == NULL)
1937 goto err; 1886 goto err;
1938 if ((b[1] = BN_new()) == NULL) 1887 if ((b[1] = BN_CTX_get(ctx)) == NULL)
1939 goto err; 1888 goto err;
1940 if ((c = BN_new()) == NULL) 1889 if ((c = BN_CTX_get(ctx)) == NULL)
1941 goto err; 1890 goto err;
1942 if ((d = BN_new()) == NULL) 1891 if ((d = BN_CTX_get(ctx)) == NULL)
1943 goto err; 1892 goto err;
1944 if ((e = BN_new()) == NULL) 1893 if ((e = BN_CTX_get(ctx)) == NULL)
1945 goto err; 1894 goto err;
1946 if ((f = BN_new()) == NULL) 1895 if ((f = BN_CTX_get(ctx)) == NULL)
1947 goto err; 1896 goto err;
1948 1897
1949 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1898 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -1988,13 +1937,7 @@ test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1988 1937
1989 rc = 1; 1938 rc = 1;
1990 err: 1939 err:
1991 BN_free(a); 1940 BN_CTX_end(ctx);
1992 BN_free(b[0]);
1993 BN_free(b[1]);
1994 BN_free(c);
1995 BN_free(d);
1996 BN_free(e);
1997 BN_free(f);
1998 1941
1999 return rc; 1942 return rc;
2000} 1943}
@@ -2002,25 +1945,27 @@ test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
2002int 1945int
2003test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx) 1946test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
2004{ 1947{
2005 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL; 1948 BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f;
2006 int p0[] = { 163, 7, 6, 3, 0, -1 }; 1949 int p0[] = { 163, 7, 6, 3, 0, -1 };
2007 int p1[] = { 193, 15, 0, -1 }; 1950 int p1[] = { 193, 15, 0, -1 };
2008 int i, j; 1951 int i, j;
2009 int rc = 0; 1952 int rc = 0;
2010 1953
2011 if ((a = BN_new()) == NULL) 1954 BN_CTX_start(ctx);
1955
1956 if ((a = BN_CTX_get(ctx)) == NULL)
2012 goto err; 1957 goto err;
2013 if ((b[0] = BN_new()) == NULL) 1958 if ((b[0] = BN_CTX_get(ctx)) == NULL)
2014 goto err; 1959 goto err;
2015 if ((b[1] = BN_new()) == NULL) 1960 if ((b[1] = BN_CTX_get(ctx)) == NULL)
2016 goto err; 1961 goto err;
2017 if ((c = BN_new()) == NULL) 1962 if ((c = BN_CTX_get(ctx)) == NULL)
2018 goto err; 1963 goto err;
2019 if ((d = BN_new()) == NULL) 1964 if ((d = BN_CTX_get(ctx)) == NULL)
2020 goto err; 1965 goto err;
2021 if ((e = BN_new()) == NULL) 1966 if ((e = BN_CTX_get(ctx)) == NULL)
2022 goto err; 1967 goto err;
2023 if ((f = BN_new()) == NULL) 1968 if ((f = BN_CTX_get(ctx)) == NULL)
2024 goto err; 1969 goto err;
2025 1970
2026 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 1971 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -2053,13 +1998,7 @@ test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
2053 1998
2054 rc = 1; 1999 rc = 1;
2055 err: 2000 err:
2056 BN_free(a); 2001 BN_CTX_end(ctx);
2057 BN_free(b[0]);
2058 BN_free(b[1]);
2059 BN_free(c);
2060 BN_free(d);
2061 BN_free(e);
2062 BN_free(f);
2063 2002
2064 return rc; 2003 return rc;
2065} 2004}
@@ -2067,23 +2006,25 @@ test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
2067int 2006int
2068test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx) 2007test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
2069{ 2008{
2070 BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL; 2009 BIGNUM *a, *b[2] = { 0 }, *c, *d, *e;
2071 int p0[] = { 163, 7, 6, 3, 0, -1 }; 2010 int p0[] = { 163, 7, 6, 3, 0, -1 };
2072 int p1[] = { 193, 15, 0, -1 }; 2011 int p1[] = { 193, 15, 0, -1 };
2073 int i, j, s = 0, t; 2012 int i, j, s = 0, t;
2074 int rc = 0; 2013 int rc = 0;
2075 2014
2076 if ((a = BN_new()) == NULL) 2015 BN_CTX_start(ctx);
2016
2017 if ((a = BN_CTX_get(ctx)) == NULL)
2077 goto err; 2018 goto err;
2078 if ((b[0] = BN_new()) == NULL) 2019 if ((b[0] = BN_CTX_get(ctx)) == NULL)
2079 goto err; 2020 goto err;
2080 if ((b[1] = BN_new()) == NULL) 2021 if ((b[1] = BN_CTX_get(ctx)) == NULL)
2081 goto err; 2022 goto err;
2082 if ((c = BN_new()) == NULL) 2023 if ((c = BN_CTX_get(ctx)) == NULL)
2083 goto err; 2024 goto err;
2084 if ((d = BN_new()) == NULL) 2025 if ((d = BN_CTX_get(ctx)) == NULL)
2085 goto err; 2026 goto err;
2086 if ((e = BN_new()) == NULL) 2027 if ((e = BN_CTX_get(ctx)) == NULL)
2087 goto err; 2028 goto err;
2088 2029
2089 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0])); 2030 CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
@@ -2140,16 +2081,12 @@ test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
2140 2081
2141 rc = 1; 2082 rc = 1;
2142 err: 2083 err:
2143 BN_free(a); 2084 BN_CTX_end(ctx);
2144 BN_free(b[0]);
2145 BN_free(b[1]);
2146 BN_free(c);
2147 BN_free(d);
2148 BN_free(e);
2149 2085
2150 return rc; 2086 return rc;
2151} 2087}
2152#endif 2088#endif
2089
2153static int 2090static int
2154genprime_cb(int p, int n, BN_GENCB *arg) 2091genprime_cb(int p, int n, BN_GENCB *arg)
2155{ 2092{
@@ -2170,19 +2107,21 @@ genprime_cb(int p, int n, BN_GENCB *arg)
2170int 2107int
2171test_kron(BIO *bp, BN_CTX *ctx) 2108test_kron(BIO *bp, BN_CTX *ctx)
2172{ 2109{
2173 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL; 2110 BIGNUM *a, *b, *r, *t;
2174 BN_GENCB *cb = NULL; 2111 BN_GENCB *cb = NULL;
2175 int i; 2112 int i;
2176 int legendre, kronecker; 2113 int legendre, kronecker;
2177 int rc = 0; 2114 int rc = 0;
2178 2115
2179 if ((a = BN_new()) == NULL) 2116 BN_CTX_start(ctx);
2117
2118 if ((a = BN_CTX_get(ctx)) == NULL)
2180 goto err; 2119 goto err;
2181 if ((b = BN_new()) == NULL) 2120 if ((b = BN_CTX_get(ctx)) == NULL)
2182 goto err; 2121 goto err;
2183 if ((r = BN_new()) == NULL) 2122 if ((r = BN_CTX_get(ctx)) == NULL)
2184 goto err; 2123 goto err;
2185 if ((t = BN_new()) == NULL) 2124 if ((t = BN_CTX_get(ctx)) == NULL)
2186 goto err; 2125 goto err;
2187 2126
2188 if ((cb = BN_GENCB_new()) == NULL) 2127 if ((cb = BN_GENCB_new()) == NULL)
@@ -2263,10 +2202,7 @@ test_kron(BIO *bp, BN_CTX *ctx)
2263 rc = 1; 2202 rc = 1;
2264 err: 2203 err:
2265 BN_GENCB_free(cb); 2204 BN_GENCB_free(cb);
2266 BN_free(a); 2205 BN_CTX_end(ctx);
2267 BN_free(b);
2268 BN_free(r);
2269 BN_free(t);
2270 2206
2271 return rc; 2207 return rc;
2272} 2208}
@@ -2274,16 +2210,18 @@ test_kron(BIO *bp, BN_CTX *ctx)
2274int 2210int
2275test_sqrt(BIO *bp, BN_CTX *ctx) 2211test_sqrt(BIO *bp, BN_CTX *ctx)
2276{ 2212{
2277 BIGNUM *a = NULL, *p = NULL, *r = NULL; 2213 BIGNUM *a, *p, *r;
2278 BN_GENCB *cb = NULL; 2214 BN_GENCB *cb = NULL;
2279 int i, j; 2215 int i, j;
2280 int rc = 0; 2216 int rc = 0;
2281 2217
2282 if ((a = BN_new()) == NULL) 2218 BN_CTX_start(ctx);
2219
2220 if ((a = BN_CTX_get(ctx)) == NULL)
2283 goto err; 2221 goto err;
2284 if ((p = BN_new()) == NULL) 2222 if ((p = BN_CTX_get(ctx)) == NULL)
2285 goto err; 2223 goto err;
2286 if ((r = BN_new()) == NULL) 2224 if ((r = BN_CTX_get(ctx)) == NULL)
2287 goto err; 2225 goto err;
2288 2226
2289 if ((cb = BN_GENCB_new()) == NULL) 2227 if ((cb = BN_GENCB_new()) == NULL)
@@ -2360,9 +2298,7 @@ test_sqrt(BIO *bp, BN_CTX *ctx)
2360 rc = 1; 2298 rc = 1;
2361 err: 2299 err:
2362 BN_GENCB_free(cb); 2300 BN_GENCB_free(cb);
2363 BN_free(a); 2301 BN_CTX_end(ctx);
2364 BN_free(p);
2365 BN_free(r);
2366 2302
2367 return rc; 2303 return rc;
2368} 2304}
@@ -2370,17 +2306,19 @@ test_sqrt(BIO *bp, BN_CTX *ctx)
2370int 2306int
2371test_lshift(BIO *bp, BN_CTX *ctx, int use_lst) 2307test_lshift(BIO *bp, BN_CTX *ctx, int use_lst)
2372{ 2308{
2373 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL; 2309 BIGNUM *a, *b, *c, *d;
2374 int i; 2310 int i;
2375 int rc = 0; 2311 int rc = 0;
2376 2312
2377 if ((a = BN_new()) == NULL) 2313 BN_CTX_start(ctx);
2314
2315 if ((a = BN_CTX_get(ctx)) == NULL)
2378 goto err; 2316 goto err;
2379 if ((b = BN_new()) == NULL) 2317 if ((b = BN_CTX_get(ctx)) == NULL)
2380 goto err; 2318 goto err;
2381 if ((c = BN_new()) == NULL) 2319 if ((c = BN_CTX_get(ctx)) == NULL)
2382 goto err; 2320 goto err;
2383 if ((d = BN_new()) == NULL) 2321 if ((d = BN_CTX_get(ctx)) == NULL)
2384 goto err; 2322 goto err;
2385 CHECK_GOTO(BN_one(c)); 2323 CHECK_GOTO(BN_one(c));
2386 2324
@@ -2424,26 +2362,25 @@ test_lshift(BIO *bp, BN_CTX *ctx, int use_lst)
2424 2362
2425 rc = 1; 2363 rc = 1;
2426 err: 2364 err:
2427 BN_free(a); 2365 BN_CTX_end(ctx);
2428 BN_free(b);
2429 BN_free(c);
2430 BN_free(d);
2431 2366
2432 return rc; 2367 return rc;
2433} 2368}
2434 2369
2435int 2370int
2436test_lshift1(BIO *bp) 2371test_lshift1(BIO *bp, BN_CTX *ctx)
2437{ 2372{
2438 BIGNUM *a = NULL, *b = NULL, *c = NULL; 2373 BIGNUM *a, *b, *c;
2439 int i; 2374 int i;
2440 int rc = 0; 2375 int rc = 0;
2441 2376
2442 if ((a = BN_new()) == NULL) 2377 BN_CTX_start(ctx);
2378
2379 if ((a = BN_CTX_get(ctx)) == NULL)
2443 goto err; 2380 goto err;
2444 if ((b = BN_new()) == NULL) 2381 if ((b = BN_CTX_get(ctx)) == NULL)
2445 goto err; 2382 goto err;
2446 if ((c = BN_new()) == NULL) 2383 if ((c = BN_CTX_get(ctx)) == NULL)
2447 goto err; 2384 goto err;
2448 2385
2449 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0)); 2386 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
@@ -2471,9 +2408,7 @@ test_lshift1(BIO *bp)
2471 2408
2472 rc = 1; 2409 rc = 1;
2473 err: 2410 err:
2474 BN_free(a); 2411 BN_CTX_end(ctx);
2475 BN_free(b);
2476 BN_free(c);
2477 2412
2478 return rc; 2413 return rc;
2479} 2414}
@@ -2481,19 +2416,21 @@ test_lshift1(BIO *bp)
2481int 2416int
2482test_rshift(BIO *bp, BN_CTX *ctx) 2417test_rshift(BIO *bp, BN_CTX *ctx)
2483{ 2418{
2484 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; 2419 BIGNUM *a, *b, *c, *d, *e;
2485 int i; 2420 int i;
2486 int rc = 0; 2421 int rc = 0;
2487 2422
2488 if ((a = BN_new()) == NULL) 2423 BN_CTX_start(ctx);
2424
2425 if ((a = BN_CTX_get(ctx)) == NULL)
2489 goto err; 2426 goto err;
2490 if ((b = BN_new()) == NULL) 2427 if ((b = BN_CTX_get(ctx)) == NULL)
2491 goto err; 2428 goto err;
2492 if ((c = BN_new()) == NULL) 2429 if ((c = BN_CTX_get(ctx)) == NULL)
2493 goto err; 2430 goto err;
2494 if ((d = BN_new()) == NULL) 2431 if ((d = BN_CTX_get(ctx)) == NULL)
2495 goto err; 2432 goto err;
2496 if ((e = BN_new()) == NULL) 2433 if ((e = BN_CTX_get(ctx)) == NULL)
2497 goto err; 2434 goto err;
2498 CHECK_GOTO(BN_one(c)); 2435 CHECK_GOTO(BN_one(c));
2499 2436
@@ -2522,27 +2459,25 @@ test_rshift(BIO *bp, BN_CTX *ctx)
2522 2459
2523 rc = 1; 2460 rc = 1;
2524 err: 2461 err:
2525 BN_free(a); 2462 BN_CTX_end(ctx);
2526 BN_free(b);
2527 BN_free(c);
2528 BN_free(d);
2529 BN_free(e);
2530 2463
2531 return rc; 2464 return rc;
2532} 2465}
2533 2466
2534int 2467int
2535test_rshift1(BIO *bp) 2468test_rshift1(BIO *bp, BN_CTX *ctx)
2536{ 2469{
2537 BIGNUM *a = NULL, *b = NULL, *c = NULL; 2470 BIGNUM *a, *b, *c;
2538 int i; 2471 int i;
2539 int rc = 0; 2472 int rc = 0;
2540 2473
2541 if ((a = BN_new()) == NULL) 2474 BN_CTX_start(ctx);
2475
2476 if ((a = BN_CTX_get(ctx)) == NULL)
2542 goto err; 2477 goto err;
2543 if ((b = BN_new()) == NULL) 2478 if ((b = BN_CTX_get(ctx)) == NULL)
2544 goto err; 2479 goto err;
2545 if ((c = BN_new()) == NULL) 2480 if ((c = BN_CTX_get(ctx)) == NULL)
2546 goto err; 2481 goto err;
2547 2482
2548 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0)); 2483 CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
@@ -2569,9 +2504,7 @@ test_rshift1(BIO *bp)
2569 2504
2570 rc = 1; 2505 rc = 1;
2571 err: 2506 err:
2572 BN_free(a); 2507 BN_CTX_end(ctx);
2573 BN_free(b);
2574 BN_free(c);
2575 2508
2576 return rc; 2509 return rc;
2577} 2510}
@@ -2589,7 +2522,7 @@ int
2589test_mod_exp_sizes(BIO *bp, BN_CTX *ctx) 2522test_mod_exp_sizes(BIO *bp, BN_CTX *ctx)
2590{ 2523{
2591 BN_MONT_CTX *mont_ctx = NULL; 2524 BN_MONT_CTX *mont_ctx = NULL;
2592 BIGNUM *p = NULL, *x = NULL, *y = NULL, *r = NULL, *r2 = NULL; 2525 BIGNUM *p, *x, *y, *r, *r2;
2593 int size; 2526 int size;
2594 int rc = 0; 2527 int rc = 0;
2595 2528
@@ -2631,8 +2564,8 @@ test_mod_exp_sizes(BIO *bp, BN_CTX *ctx)
2631 2564
2632 rc = 1; 2565 rc = 1;
2633 err: 2566 err:
2634 BN_MONT_CTX_free(mont_ctx);
2635 BN_CTX_end(ctx); 2567 BN_CTX_end(ctx);
2568 BN_MONT_CTX_free(mont_ctx);
2636 2569
2637 return rc; 2570 return rc;
2638} 2571}