Remove use of BN_init, BN_RECP_CTX_init from bntest

BN_init and BN_RECP_CTX_init are deprecated and are not exported
from shared libraries on some platforms (e.g. Windows) convert
bntest to use BN_new and BN_RECP_CTX_new instead.
Reviewed-by: Matt Caswell <matt@openssl.org>
This commit is contained in:
Dr. Stephen Henson 2015-01-13 15:21:28 +00:00
parent 98b3b116ab
commit a5a412350d

View File

@ -329,160 +329,160 @@ err:
int test_add(BIO *bp) int test_add(BIO *bp)
{ {
BIGNUM a,b,c; BIGNUM *a,*b,*c;
int i; int i;
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
BN_bntest_rand(&a,512,0,0); BN_bntest_rand(a,512,0,0);
for (i=0; i<num0; i++) for (i=0; i<num0; i++)
{ {
BN_bntest_rand(&b,450+i,0,0); BN_bntest_rand(b,450+i,0,0);
a.neg=rand_neg(); a->neg=rand_neg();
b.neg=rand_neg(); b->neg=rand_neg();
BN_add(&c,&a,&b); BN_add(c,a,b);
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," + "); BIO_puts(bp," + ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&c); BN_print(bp,c);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
a.neg=!a.neg; a->neg=!a->neg;
b.neg=!b.neg; b->neg=!b->neg;
BN_add(&c,&c,&b); BN_add(c,c,b);
BN_add(&c,&c,&a); BN_add(c,c,a);
if(!BN_is_zero(&c)) if(!BN_is_zero(c))
{ {
fprintf(stderr,"Add test failed!\n"); fprintf(stderr,"Add test failed!\n");
return 0; return 0;
} }
} }
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
return(1); return(1);
} }
int test_sub(BIO *bp) int test_sub(BIO *bp)
{ {
BIGNUM a,b,c; BIGNUM *a,*b,*c;
int i; int i;
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
for (i=0; i<num0+num1; i++) for (i=0; i<num0+num1; i++)
{ {
if (i < num1) if (i < num1)
{ {
BN_bntest_rand(&a,512,0,0); BN_bntest_rand(a,512,0,0);
BN_copy(&b,&a); BN_copy(b,a);
if (BN_set_bit(&a,i)==0) return(0); if (BN_set_bit(a,i)==0) return(0);
BN_add_word(&b,i); BN_add_word(b,i);
} }
else else
{ {
BN_bntest_rand(&b,400+i-num1,0,0); BN_bntest_rand(b,400+i-num1,0,0);
a.neg=rand_neg(); a->neg=rand_neg();
b.neg=rand_neg(); b->neg=rand_neg();
} }
BN_sub(&c,&a,&b); BN_sub(c,a,b);
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," - "); BIO_puts(bp," - ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&c); BN_print(bp,c);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
BN_add(&c,&c,&b); BN_add(c,c,b);
BN_sub(&c,&c,&a); BN_sub(c,c,a);
if(!BN_is_zero(&c)) if(!BN_is_zero(c))
{ {
fprintf(stderr,"Subtract test failed!\n"); fprintf(stderr,"Subtract test failed!\n");
return 0; return 0;
} }
} }
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
return(1); return(1);
} }
int test_div(BIO *bp, BN_CTX *ctx) int test_div(BIO *bp, BN_CTX *ctx)
{ {
BIGNUM a,b,c,d,e; BIGNUM *a,*b,*c,*d,*e;
int i; int i;
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
BN_init(&d); d = BN_new();
BN_init(&e); e = BN_new();
for (i=0; i<num0+num1; i++) for (i=0; i<num0+num1; i++)
{ {
if (i < num1) if (i < num1)
{ {
BN_bntest_rand(&a,400,0,0); BN_bntest_rand(a,400,0,0);
BN_copy(&b,&a); BN_copy(b,a);
BN_lshift(&a,&a,i); BN_lshift(a,a,i);
BN_add_word(&a,i); BN_add_word(a,i);
} }
else else
BN_bntest_rand(&b,50+3*(i-num1),0,0); BN_bntest_rand(b,50+3*(i-num1),0,0);
a.neg=rand_neg(); a->neg=rand_neg();
b.neg=rand_neg(); b->neg=rand_neg();
BN_div(&d,&c,&a,&b,ctx); BN_div(d,c,a,b,ctx);
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," / "); BIO_puts(bp," / ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&d); BN_print(bp,d);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," % "); BIO_puts(bp," % ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&c); BN_print(bp,c);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
BN_mul(&e,&d,&b,ctx); BN_mul(e,d,b,ctx);
BN_add(&d,&e,&c); BN_add(d,e,c);
BN_sub(&d,&d,&a); BN_sub(d,d,a);
if(!BN_is_zero(&d)) if(!BN_is_zero(d))
{ {
fprintf(stderr,"Division test failed!\n"); fprintf(stderr,"Division test failed!\n");
return 0; return 0;
} }
} }
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
BN_free(&d); BN_free(d);
BN_free(&e); BN_free(e);
return(1); return(1);
} }
@ -504,39 +504,39 @@ static void print_word(BIO *bp,BN_ULONG w)
int test_div_word(BIO *bp) int test_div_word(BIO *bp)
{ {
BIGNUM a,b; BIGNUM *a,*b;
BN_ULONG r,s; BN_ULONG r,s;
int i; int i;
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
for (i=0; i<num0; i++) for (i=0; i<num0; i++)
{ {
do { do {
BN_bntest_rand(&a,512,-1,0); BN_bntest_rand(a,512,-1,0);
BN_bntest_rand(&b,BN_BITS2,-1,0); BN_bntest_rand(b,BN_BITS2,-1,0);
s = b.d[0]; s = b->d[0];
} while (!s); } while (!s);
BN_copy(&b, &a); BN_copy(b, a);
r = BN_div_word(&b, s); r = BN_div_word(b, s);
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," / "); BIO_puts(bp," / ");
print_word(bp,s); print_word(bp,s);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," % "); BIO_puts(bp," % ");
print_word(bp,s); print_word(bp,s);
BIO_puts(bp," - "); BIO_puts(bp," - ");
@ -544,145 +544,145 @@ int test_div_word(BIO *bp)
print_word(bp,r); print_word(bp,r);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
BN_mul_word(&b,s); BN_mul_word(b,s);
BN_add_word(&b,r); BN_add_word(b,r);
BN_sub(&b,&a,&b); BN_sub(b,a,b);
if(!BN_is_zero(&b)) if(!BN_is_zero(b))
{ {
fprintf(stderr,"Division (word) test failed!\n"); fprintf(stderr,"Division (word) test failed!\n");
return 0; return 0;
} }
} }
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
return(1); return(1);
} }
int test_div_recp(BIO *bp, BN_CTX *ctx) int test_div_recp(BIO *bp, BN_CTX *ctx)
{ {
BIGNUM a,b,c,d,e; BIGNUM *a,*b,*c,*d,*e;
BN_RECP_CTX recp; BN_RECP_CTX *recp;
int i; int i;
BN_RECP_CTX_init(&recp); recp = BN_RECP_CTX_new();
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
BN_init(&d); d = BN_new();
BN_init(&e); e = BN_new();
for (i=0; i<num0+num1; i++) for (i=0; i<num0+num1; i++)
{ {
if (i < num1) if (i < num1)
{ {
BN_bntest_rand(&a,400,0,0); BN_bntest_rand(a,400,0,0);
BN_copy(&b,&a); BN_copy(b,a);
BN_lshift(&a,&a,i); BN_lshift(a,a,i);
BN_add_word(&a,i); BN_add_word(a,i);
} }
else else
BN_bntest_rand(&b,50+3*(i-num1),0,0); BN_bntest_rand(b,50+3*(i-num1),0,0);
a.neg=rand_neg(); a->neg=rand_neg();
b.neg=rand_neg(); b->neg=rand_neg();
BN_RECP_CTX_set(&recp,&b,ctx); BN_RECP_CTX_set(recp,b,ctx);
BN_div_recp(&d,&c,&a,&recp,ctx); BN_div_recp(d,c,a,recp,ctx);
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," / "); BIO_puts(bp," / ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&d); BN_print(bp,d);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," % "); BIO_puts(bp," % ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&c); BN_print(bp,c);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
BN_mul(&e,&d,&b,ctx); BN_mul(e,d,b,ctx);
BN_add(&d,&e,&c); BN_add(d,e,c);
BN_sub(&d,&d,&a); BN_sub(d,d,a);
if(!BN_is_zero(&d)) if(!BN_is_zero(d))
{ {
fprintf(stderr,"Reciprocal division test failed!\n"); fprintf(stderr,"Reciprocal division test failed!\n");
fprintf(stderr,"a="); fprintf(stderr,"a=");
BN_print_fp(stderr,&a); BN_print_fp(stderr,a);
fprintf(stderr,"\nb="); fprintf(stderr,"\nb=");
BN_print_fp(stderr,&b); BN_print_fp(stderr,b);
fprintf(stderr,"\n"); fprintf(stderr,"\n");
return 0; return 0;
} }
} }
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
BN_free(&d); BN_free(d);
BN_free(&e); BN_free(e);
BN_RECP_CTX_free(&recp); BN_RECP_CTX_free(recp);
return(1); return(1);
} }
int test_mul(BIO *bp) int test_mul(BIO *bp)
{ {
BIGNUM a,b,c,d,e; BIGNUM *a,*b,*c,*d,*e;
int i; int i;
BN_CTX *ctx; BN_CTX *ctx;
ctx = BN_CTX_new(); ctx = BN_CTX_new();
if (ctx == NULL) EXIT(1); if (ctx == NULL) EXIT(1);
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
BN_init(&d); d = BN_new();
BN_init(&e); e = BN_new();
for (i=0; i<num0+num1; i++) for (i=0; i<num0+num1; i++)
{ {
if (i <= num1) if (i <= num1)
{ {
BN_bntest_rand(&a,100,0,0); BN_bntest_rand(a,100,0,0);
BN_bntest_rand(&b,100,0,0); BN_bntest_rand(b,100,0,0);
} }
else else
BN_bntest_rand(&b,i-num1,0,0); BN_bntest_rand(b,i-num1,0,0);
a.neg=rand_neg(); a->neg=rand_neg();
b.neg=rand_neg(); b->neg=rand_neg();
BN_mul(&c,&a,&b,ctx); BN_mul(c,a,b,ctx);
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," * "); BIO_puts(bp," * ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&c); BN_print(bp,c);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
BN_div(&d,&e,&c,&a,ctx); BN_div(d,e,c,a,ctx);
BN_sub(&d,&d,&b); BN_sub(d,d,b);
if(!BN_is_zero(&d) || !BN_is_zero(&e)) if(!BN_is_zero(d) || !BN_is_zero(e))
{ {
fprintf(stderr,"Multiplication test failed!\n"); fprintf(stderr,"Multiplication test failed!\n");
return 0; return 0;
} }
} }
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
BN_free(&d); BN_free(d);
BN_free(&e); BN_free(e);
BN_CTX_free(ctx); BN_CTX_free(ctx);
return(1); return(1);
} }
@ -785,78 +785,78 @@ err:
int test_mont(BIO *bp, BN_CTX *ctx) int test_mont(BIO *bp, BN_CTX *ctx)
{ {
BIGNUM a,b,c,d,A,B; BIGNUM *a,*b,*c,*d,*A,*B;
BIGNUM n; BIGNUM *n;
int i; int i;
BN_MONT_CTX *mont; BN_MONT_CTX *mont;
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
BN_init(&d); d = BN_new();
BN_init(&A); A = BN_new();
BN_init(&B); B = BN_new();
BN_init(&n); n = BN_new();
mont=BN_MONT_CTX_new(); mont=BN_MONT_CTX_new();
if (mont == NULL) if (mont == NULL)
return 0; return 0;
BN_bntest_rand(&a,100,0,0); /**/ BN_bntest_rand(a,100,0,0); /**/
BN_bntest_rand(&b,100,0,0); /**/ BN_bntest_rand(b,100,0,0); /**/
for (i=0; i<num2; i++) for (i=0; i<num2; i++)
{ {
int bits = (200*(i+1))/num2; int bits = (200*(i+1))/num2;
if (bits == 0) if (bits == 0)
continue; continue;
BN_bntest_rand(&n,bits,0,1); BN_bntest_rand(n,bits,0,1);
BN_MONT_CTX_set(mont,&n,ctx); BN_MONT_CTX_set(mont,n,ctx);
BN_nnmod(&a,&a,&n,ctx); BN_nnmod(a,a,n,ctx);
BN_nnmod(&b,&b,&n,ctx); BN_nnmod(b,b,n,ctx);
BN_to_montgomery(&A,&a,mont,ctx); BN_to_montgomery(A,a,mont,ctx);
BN_to_montgomery(&B,&b,mont,ctx); BN_to_montgomery(B,b,mont,ctx);
BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/ BN_mod_mul_montgomery(c,A,B,mont,ctx);/**/
BN_from_montgomery(&A,&c,mont,ctx);/**/ BN_from_montgomery(A,c,mont,ctx);/**/
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
#ifdef undef #ifdef undef
fprintf(stderr,"%d * %d %% %d\n", fprintf(stderr,"%d * %d %% %d\n",
BN_num_bits(&a), BN_num_bits(a),
BN_num_bits(&b), BN_num_bits(b),
BN_num_bits(mont->N)); BN_num_bits(&mont->N);
#endif #endif
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," * "); BIO_puts(bp," * ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," % "); BIO_puts(bp," % ");
BN_print(bp,&(mont->N)); BN_print(bp,&mont->N);
BIO_puts(bp," - "); BIO_puts(bp," - ");
} }
BN_print(bp,&A); BN_print(bp,A);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
BN_mod_mul(&d,&a,&b,&n,ctx); BN_mod_mul(d,a,b,n,ctx);
BN_sub(&d,&d,&A); BN_sub(d,d,A);
if(!BN_is_zero(&d)) if(!BN_is_zero(d))
{ {
fprintf(stderr,"Montgomery multiplication test failed!\n"); fprintf(stderr,"Montgomery multiplication test failed!\n");
return 0; return 0;
} }
} }
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
BN_free(&d); BN_free(d);
BN_free(&A); BN_free(A);
BN_free(&B); BN_free(B);
BN_free(&n); BN_free(n);
return(1); return(1);
} }
@ -1205,43 +1205,43 @@ int test_exp(BIO *bp, BN_CTX *ctx)
#ifndef OPENSSL_NO_EC2M #ifndef OPENSSL_NO_EC2M
int test_gf2m_add(BIO *bp) int test_gf2m_add(BIO *bp)
{ {
BIGNUM a,b,c; BIGNUM *a,*b,*c;
int i, ret = 0; int i, ret = 0;
BN_init(&a); a = BN_new();
BN_init(&b); b = BN_new();
BN_init(&c); c = BN_new();
for (i=0; i<num0; i++) for (i=0; i<num0; i++)
{ {
BN_rand(&a,512,0,0); BN_rand(a,512,0,0);
BN_copy(&b, BN_value_one()); BN_copy(b, BN_value_one());
a.neg=rand_neg(); a->neg=rand_neg();
b.neg=rand_neg(); b->neg=rand_neg();
BN_GF2m_add(&c,&a,&b); BN_GF2m_add(c,a,b);
#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
if (bp != NULL) if (bp != NULL)
{ {
if (!results) if (!results)
{ {
BN_print(bp,&a); BN_print(bp,a);
BIO_puts(bp," ^ "); BIO_puts(bp," ^ ");
BN_print(bp,&b); BN_print(bp,b);
BIO_puts(bp," = "); BIO_puts(bp," = ");
} }
BN_print(bp,&c); BN_print(bp,c);
BIO_puts(bp,"\n"); BIO_puts(bp,"\n");
} }
#endif #endif
/* Test that two added values have the correct parity. */ /* Test that two added values have the correct parity. */
if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c))) if((BN_is_odd(a) && BN_is_odd(c)) || (!BN_is_odd(a) && !BN_is_odd(c)))
{ {
fprintf(stderr,"GF(2^m) addition test (a) failed!\n"); fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
goto err; goto err;
} }
BN_GF2m_add(&c,&c,&c); BN_GF2m_add(c,c,c);
/* Test that c + c = 0. */ /* Test that c + c = 0. */
if(!BN_is_zero(&c)) if(!BN_is_zero(c))
{ {
fprintf(stderr,"GF(2^m) addition test (b) failed!\n"); fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
goto err; goto err;
@ -1249,9 +1249,9 @@ int test_gf2m_add(BIO *bp)
} }
ret = 1; ret = 1;
err: err:
BN_free(&a); BN_free(a);
BN_free(&b); BN_free(b);
BN_free(&c); BN_free(c);
return ret; return ret;
} }
@ -1964,42 +1964,42 @@ int test_small_prime(BIO *bp,BN_CTX *ctx)
{ {
static const int bits = 10; static const int bits = 10;
int ret = 0; int ret = 0;
BIGNUM r; BIGNUM *r;
BN_init(&r); r = BN_new();
if (!BN_generate_prime_ex(&r, bits, 0, NULL, NULL, NULL)) if (!BN_generate_prime_ex(r, bits, 0, NULL, NULL, NULL))
goto err; goto err;
if (BN_num_bits(&r) != bits) if (BN_num_bits(r) != bits)
{ {
BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits, BN_num_bits(&r)); BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits, BN_num_bits(r));
goto err; goto err;
} }
ret = 1; ret = 1;
err: err:
BN_clear(&r); BN_clear_free(r);
return ret; return ret;
} }
#ifndef OPENSSL_SYS_WIN32 #ifndef OPENSSL_SYS_WIN32
int test_probable_prime_coprime(BIO *bp, BN_CTX *ctx) int test_probable_prime_coprime(BIO *bp, BN_CTX *ctx)
{ {
int i, j, ret = 0; int i, j, ret = 0;
BIGNUM r; BIGNUM *r;
BN_ULONG primes[5] = { 2, 3, 5, 7, 11 }; BN_ULONG primes[5] = { 2, 3, 5, 7, 11 };
BN_init(&r); r = BN_new();
for (i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
{ {
if (!bn_probable_prime_dh_coprime(&r, 1024, ctx)) goto err; if (!bn_probable_prime_dh_coprime(r, 1024, ctx)) goto err;
for (j = 0; j < 5; j++) for (j = 0; j < 5; j++)
{ {
if (BN_mod_word(&r, primes[j]) == 0) if (BN_mod_word(r, primes[j]) == 0)
{ {
BIO_printf(bp, "Number generated is not coprime to %ld:\n", primes[j]); BIO_printf(bp, "Number generated is not coprime to %ld:\n", primes[j]);
BN_print_fp(stdout, &r); BN_print_fp(stdout, r);
BIO_printf(bp, "\n"); BIO_printf(bp, "\n");
goto err; goto err;
} }
@ -2009,7 +2009,7 @@ int test_probable_prime_coprime(BIO *bp, BN_CTX *ctx)
ret = 1; ret = 1;
err: err:
BN_clear(&r); BN_clear_free(r);
return ret; return ret;
} }
#endif #endif