From 373b575f5a7b509bbadd67b1d57eef57dd23357a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulf=20M=C3=B6ller?= Date: Sun, 16 Jan 2000 15:58:17 +0000 Subject: New function RAND_pseudo_bytes() generated pseudorandom numbers that are not guaranteed to be unpredictable. --- crypto/pkcs7/pk7_mime.c | 2 +- crypto/rand/md_rand.c | 19 +++++++++++++++++++ crypto/rand/rand.h | 2 ++ crypto/rand/rand_lib.c | 6 ++++++ crypto/rand/randtest.c | 2 +- 5 files changed, 29 insertions(+), 2 deletions(-) (limited to 'crypto') diff --git a/crypto/pkcs7/pk7_mime.c b/crypto/pkcs7/pk7_mime.c index 4282f69d0d..54d5f422ad 100644 --- a/crypto/pkcs7/pk7_mime.c +++ b/crypto/pkcs7/pk7_mime.c @@ -149,7 +149,7 @@ int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags) if((flags & PKCS7_DETACHED) && data) { /* We want multipart/signed */ /* Generate a random boundary */ - RAND_bytes((unsigned char *)bound, 32); + RAND_pseudo_bytes((unsigned char *)bound, 32); for(i = 0; i < 32; i++) { c = bound[i] & 0xf; if(c < 10) c += '0'; diff --git a/crypto/rand/md_rand.c b/crypto/rand/md_rand.c index dbed1dcde2..7b8cde9401 100644 --- a/crypto/rand/md_rand.c +++ b/crypto/rand/md_rand.c @@ -146,12 +146,14 @@ static void ssleay_rand_cleanup(void); static void ssleay_rand_seed(const void *buf, int num); static void ssleay_rand_add(const void *buf, int num, int add_entropy); static int ssleay_rand_bytes(unsigned char *buf, int num); +static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num); RAND_METHOD rand_ssleay_meth={ ssleay_rand_seed, ssleay_rand_bytes, ssleay_rand_cleanup, ssleay_rand_add, + ssleay_rand_pseudo_bytes, }; RAND_METHOD *RAND_SSLeay(void) @@ -449,6 +451,23 @@ static int ssleay_rand_bytes(unsigned char *buf, int num) } } +/* pseudo-random bytes that are guaranteed to be unique but not + unpredictable */ +static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num) + { + int ret, err; + + ret = RAND_bytes(buf, num); + if (ret == 0) + { + err = ERR_peek_error(); + if (ERR_GET_LIB(err) == ERR_LIB_RAND && + ERR_GET_REASON(err) == RAND_R_PRNG_NOT_SEEDED) + (void)ERR_get_error(); + } + return (ret); + } + #ifdef WINDOWS #include #include diff --git a/crypto/rand/rand.h b/crypto/rand/rand.h index 35a3bb6e10..5ab94a779b 100644 --- a/crypto/rand/rand.h +++ b/crypto/rand/rand.h @@ -69,6 +69,7 @@ typedef struct rand_meth_st int (*bytes)(unsigned char *buf, int num); void (*cleanup)(void); void (*add)(const void *buf, int num, int entropy); + int (*pseudorand)(unsigned char *buf, int num); } RAND_METHOD; void RAND_set_rand_method(RAND_METHOD *meth); @@ -76,6 +77,7 @@ RAND_METHOD *RAND_get_rand_method(void ); RAND_METHOD *RAND_SSLeay(void); void RAND_cleanup(void ); int RAND_bytes(unsigned char *buf,int num); +int RAND_pseudo_bytes(unsigned char *buf,int num); void RAND_seed(const void *buf,int num); void RAND_add(const void *buf,int num,int entropy); int RAND_load_file(const char *file,long max_bytes); diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c index 3cdba48ba8..9a0b804292 100644 --- a/crypto/rand/rand_lib.c +++ b/crypto/rand/rand_lib.c @@ -102,3 +102,9 @@ int RAND_bytes(unsigned char *buf, int num) return(-1); } +int RAND_pseudo_bytes(unsigned char *buf, int num) + { + if (rand_meth != NULL) + return rand_meth->pseudorand(buf,num); + return(-1); + } diff --git a/crypto/rand/randtest.c b/crypto/rand/randtest.c index f0706d779a..da96e3f695 100644 --- a/crypto/rand/randtest.c +++ b/crypto/rand/randtest.c @@ -73,7 +73,7 @@ int main() /*double d; */ long d; - RAND_bytes(buf,2500); + RAND_pseudo_bytes(buf,2500); n1=0; for (i=0; i<16; i++) n2[i]=0; -- cgit v1.2.3