From 111482cf2e1eda0d433f939d23509094da706dc0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulf=20M=C3=B6ller?= Date: Sat, 2 Dec 2000 07:32:57 +0000 Subject: [PATCH] New function BN_bntest_rand() to detect more BN library bugs. The bn_cmp_part_words bug was only caught in the BN_mod_mul() test, not in the BN_mul() test, so apparently the choice of parameters in some cases is bad. --- crypto/bn/bn.h | 2 ++ crypto/bn/bn_rand.c | 28 +++++++++++++++++++ crypto/bn/bntest.c | 68 ++++++++++++++++++++++----------------------- 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/crypto/bn/bn.h b/crypto/bn/bn.h index b55ac95642..3d5c91adb9 100644 --- a/crypto/bn/bn.h +++ b/crypto/bn/bn.h @@ -495,6 +495,8 @@ void bn_dump1(FILE *o, const char *a, const BN_ULONG *b,int n); # 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 21ecbc04ed..bab4510345 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) { if (bit == 0) @@ -140,3 +161,10 @@ 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 diff --git a/crypto/bn/bntest.c b/crypto/bn/bntest.c index 2df0d29b1a..7c9e5b4196 100644 --- a/crypto/bn/bntest.c +++ b/crypto/bn/bntest.c @@ -124,9 +124,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++; @@ -263,10 +261,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) @@ -745,11 +743,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) @@ -818,11 +816,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(); /* t := (b-1)/2 (note that b is odd) */ @@ -1034,10 +1032,10 @@ int test_sqrt(BIO *bp, BN_CTX *ctx) { /* construct 'a' such that it is a square modulo p, * but in general not a proper square and not reduced modulo p */ - if (!BN_rand(r, 256, 0, 3)) goto err; + if (!BN_bntest_rand(r, 256, 0, 3)) goto err; if (!BN_nnmod(r, r, p, ctx)) goto err; if (!BN_mod_sqr(r, r, p, ctx)) goto err; - if (!BN_rand(a, 256, 0, 3)) goto err; + if (!BN_bntest_rand(a, 256, 0, 3)) goto err; if (!BN_nnmod(a, a, p, ctx)) goto err; if (!BN_mod_sqr(a, a, p, ctx)) goto err; if (!BN_mul(a, a, r, ctx)) goto err; @@ -1089,7 +1087,7 @@ int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) else { a=BN_new(); - BN_rand(a,200,0,0); /**/ + BN_bntest_rand(a,200,0,0); /**/ a->neg=rand_neg(); } for (i=0; ineg=rand_neg(); for (i=0; ineg=rand_neg(); for (i=0; ineg=rand_neg(); for (i=0; i