From 98486a93109a9e01183f11fb6f8090941e422068 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulf=20M=C3=B6ller?= Date: Tue, 27 Feb 2001 23:00:42 +0000 Subject: [PATCH] improved bignum test as in 0.9.7. We need this to find out if the bignum failures on Irix and Alpha are caused by new 0.9.7 code or just aren't triggered in the 0.9.6 test suite. --- CHANGES | 3 +++ crypto/bn/bn.h | 2 ++ crypto/bn/bn_rand.c | 28 ++++++++++++++++++++ crypto/bn/bntest.c | 62 ++++++++++++++++++++++----------------------- 4 files changed, 63 insertions(+), 32 deletions(-) diff --git a/CHANGES b/CHANGES index 215bda7a15..7707467d34 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,9 @@ Changes between 0.9.6 and 0.9.6a [xx XXX 2001] + *) Use better test patterns in bntest. + [Ulf Möller] + *) Initialise "ex_data" member of RSA/DSA/DH structures prior to calling the method-specific "init()" handler. Also clean up ex_data after calling the method-specific "finish()" handler. Previously, this was diff --git a/crypto/bn/bn.h b/crypto/bn/bn.h index ef1cfb0259..b232c2ceae 100644 --- a/crypto/bn/bn.h +++ b/crypto/bn/bn.h @@ -468,6 +468,8 @@ BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num); # define bn_dump(a,b) #endif +int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom); + /* BEGIN ERROR CODES */ /* The following lines are auto generated by the script mkerr.pl. Any changes * made after this point may be overwritten when the script is next run. diff --git a/crypto/bn/bn_rand.c b/crypto/bn/bn_rand.c index 2e45770e8f..acd0619921 100644 --- a/crypto/bn/bn_rand.c +++ b/crypto/bn/bn_rand.c @@ -100,6 +100,27 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) goto err; } +#if 1 + if (pseudorand == 2) + { + /* generate patterns that are more likely to trigger BN + library bugs */ + int i; + unsigned char c; + + for (i = 0; i < bytes; i++) + { + RAND_pseudo_bytes(&c, 1); + if (c >= 128 && i > 0) + buf[i] = buf[i-1]; + else if (c < 42) + buf[i] = 0; + else if (c < 84) + buf[i] = 255; + } + } +#endif + if (top != -1) { if (top) @@ -143,6 +164,13 @@ int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) return bnrand(1, rnd, bits, top, bottom); } +#if 1 +int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom) + { + return bnrand(2, rnd, bits, top, bottom); + } +#endif + /* random number r: 0 <= r < range */ int BN_rand_range(BIGNUM *r, BIGNUM *range) { diff --git a/crypto/bn/bntest.c b/crypto/bn/bntest.c index c9801dc016..af0c2629e8 100644 --- a/crypto/bn/bntest.c +++ b/crypto/bn/bntest.c @@ -120,9 +120,7 @@ int main(int argc, char *argv[]) results = 0; - RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't - * even check its return value - * (which we should) */ + RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */ argc--; argv++; @@ -251,10 +249,10 @@ int test_add(BIO *bp) BN_init(&b); BN_init(&c); - BN_rand(&a,512,0,0); + BN_bntest_rand(&a,512,0,0); for (i=0; ineg=rand_neg(); b->neg=rand_neg(); if (bp == NULL) @@ -730,11 +728,11 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx) d=BN_new(); e=BN_new(); - BN_rand(c,1024,0,0); /**/ + BN_bntest_rand(c,1024,0,0); /**/ for (i=0; ineg=rand_neg(); b->neg=rand_neg(); /* if (bp == NULL) @@ -792,11 +790,11 @@ int test_mod_exp(BIO *bp, BN_CTX *ctx) d=BN_new(); e=BN_new(); - BN_rand(c,30,0,1); /* must be odd for montgomery */ + BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ for (i=0; ineg=rand_neg(); } for (i=0; ineg=rand_neg(); for (i=0; ineg=rand_neg(); for (i=0; ineg=rand_neg(); for (i=0; i