X-Git-Url: https://git.openssl.org/?p=openssl.git;a=blobdiff_plain;f=crypto%2Frand%2Fmd_rand.c;h=d727fff9243d6efc57b74994ab3fc5b458fe1690;hp=080581afcdc028d5421ad978876b39fa9a77c8cd;hb=eb952088f0d5da59e569ae2aa33e9b96bc3b586d;hpb=d6847aed108ca305559dea11241198351730490c diff --git a/crypto/rand/md_rand.c b/crypto/rand/md_rand.c index 080581afcd..d727fff924 100644 --- a/crypto/rand/md_rand.c +++ b/crypto/rand/md_rand.c @@ -56,18 +56,23 @@ * [including the GNU Public Licence.] */ +#define ENTROPY_NEEDED 32 /* require 128 bits of randomness */ + +#ifndef MD_RAND_DEBUG +# ifndef NDEBUG +# define NDEBUG +# endif +#endif + +#include #include -#include #include #include -#ifndef FLAT_INC -# include "../../e_os.h" -#else -# include "e_os.h" -#endif +#include "openssl/e_os.h" #include +#include #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND) #if !defined(NO_SHA) && !defined(NO_SHA1) @@ -133,17 +138,20 @@ static int state_num=0,state_index=0; static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH]; static unsigned char md[MD_DIGEST_LENGTH]; static long md_count[2]={0,0}; +static int entropy=0; const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT; static void ssleay_rand_cleanup(void); static void ssleay_rand_seed(const void *buf, int num); -static void ssleay_rand_bytes(unsigned char *buf, int num); +static void ssleay_rand_add(const void *buf, int num, int entropy); +static int ssleay_rand_bytes(unsigned char *buf, int num); RAND_METHOD rand_ssleay_meth={ ssleay_rand_seed, ssleay_rand_bytes, ssleay_rand_cleanup, + ssleay_rand_add, }; RAND_METHOD *RAND_SSLeay(void) @@ -159,22 +167,48 @@ static void ssleay_rand_cleanup(void) memset(md,0,MD_DIGEST_LENGTH); md_count[0]=0; md_count[1]=0; + entropy=0; } -static void ssleay_rand_seed(const void *buf, int num) +static void ssleay_rand_add(const void *buf, int num, int add) { - int i,j,k,st_idx,st_num; + int i,j,k,st_idx; + long md_c[2]; + unsigned char local_md[MD_DIGEST_LENGTH]; MD_CTX m; #ifdef NORAND return; #endif + /* + * (Based on doc/ssleay.txt, section rand.doc:) + * + * The input is chopped up into units of 16 bytes (or less for + * the last block). Each of these blocks is run through the MD5 + * message digest as follow: The data passed to the MD5 digest + * is the current 'md', the same number of bytes from the 'state' + * (the location determined by in incremented looping index) as + * the current 'block', the new key data 'block', and 'count' + * (which is incremented after each use). + * The result of this is kept in 'md' and also xored into the + * 'state' at the same locations that were used as input into the MD5. + */ + CRYPTO_w_lock(CRYPTO_LOCK_RAND); st_idx=state_index; - st_num=state_num; - state_index=(state_index+num); + /* use our own copies of the counters so that even + * if a concurrent thread seeds with exactly the + * same data and uses the same subarray there's _some_ + * difference */ + md_c[0] = md_count[0]; + md_c[1] = md_count[1]; + + memcpy(local_md, md, sizeof md); + + /* state_index <= state_num <= STATE_SIZE */ + state_index += num; if (state_index >= STATE_SIZE) { state_index%=STATE_SIZE; @@ -185,6 +219,14 @@ static void ssleay_rand_seed(const void *buf, int num) if (state_index > state_num) state_num=state_index; } + /* state_index <= state_num <= STATE_SIZE */ + + /* state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE] + * are what we will use now, but other threads may use them + * as well */ + + md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0); + CRYPTO_w_unlock(CRYPTO_LOCK_RAND); for (i=0; i MD_DIGEST_LENGTH)?MD_DIGEST_LENGTH:j; MD_Init(&m); - MD_Update(&m,md,MD_DIGEST_LENGTH); + MD_Update(&m,local_md,MD_DIGEST_LENGTH); k=(st_idx+j)-STATE_SIZE; if (k > 0) { @@ -204,31 +246,63 @@ static void ssleay_rand_seed(const void *buf, int num) MD_Update(&m,&(state[st_idx]),j); MD_Update(&m,buf,j); - MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count)); - MD_Final(md,&m); - md_count[1]++; + MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c)); + MD_Final(local_md,&m); + md_c[1]++; buf=(const char *)buf + j; for (k=0; k= STATE_SIZE) - { st_idx=0; - st_num=STATE_SIZE; - } } } memset((char *)&m,0,sizeof(m)); + + CRYPTO_w_lock(CRYPTO_LOCK_RAND); + /* Don't just copy back local_md into md -- this could mean that + * other thread's seeding remains without effect (except for + * the incremented counter). By XORing it we keep at least as + * much entropy as fits into md. */ + for (k = 0; k < sizeof md; k++) + { + md[k] ^= local_md[k]; + } + CRYPTO_w_unlock(CRYPTO_LOCK_RAND); + +#ifndef THREADS + assert(md_c[1] == md_count[1]); +#endif + entropy += add; } -static void ssleay_rand_bytes(unsigned char *buf, int num) +static void ssleay_rand_seed(const void *buf, int num) + { + ssleay_rand_add(buf, num, num); + } + +static int ssleay_rand_bytes(unsigned char *buf, int num) { int i,j,k,st_num,st_idx; + int ok; + long md_c[2]; + unsigned char local_md[MD_DIGEST_LENGTH]; MD_CTX m; static int init=1; unsigned long l; +#ifndef GETPID_IS_MEANINGLESS + pid_t curr_pid = getpid(); +#endif #ifdef DEVRANDOM FILE *fh; #endif @@ -239,10 +313,26 @@ static void ssleay_rand_bytes(unsigned char *buf, int num) for (i=0; i= ENTROPY_NEEDED); + st_idx=state_index; st_num=state_num; + md_c[0] = md_count[0]; + md_c[1] = md_count[1]; + memcpy(local_md, md, sizeof md); + state_index+=num; if (state_index > state_num) - state_index=(state_index%state_num); + state_index %= state_num; + /* state[st_idx], ..., state[(st_idx + num - 1) % st_num] + * are now ours (but other threads may use them too) */ + + md_count[0] += 1; CRYPTO_w_unlock(CRYPTO_LOCK_RAND); while (num > 0) @@ -302,8 +402,15 @@ static void ssleay_rand_bytes(unsigned char *buf, int num) j=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num; num-=j; MD_Init(&m); - MD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2); - MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count)); +#ifndef GETPID_IS_MEANINGLESS + if (curr_pid) /* just in the first iteration to save time */ + { + MD_Update(&m,(unsigned char*)&curr_pid,sizeof curr_pid); + curr_pid = 0; + } +#endif + MD_Update(&m,&(local_md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2); + MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c)); #ifndef PURIFY MD_Update(&m,buf,j); /* purify complains */ #endif @@ -315,23 +422,33 @@ static void ssleay_rand_bytes(unsigned char *buf, int num) } else MD_Update(&m,&(state[st_idx]),j); - MD_Final(md,&m); + MD_Final(local_md,&m); for (i=0; i= st_num) st_idx=0; - state[st_idx++]^=md[i]; - *(buf++)=md[i+MD_DIGEST_LENGTH/2]; } } MD_Init(&m); - MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count)); - md_count[0]++; + MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c)); + MD_Update(&m,local_md,MD_DIGEST_LENGTH); + CRYPTO_w_lock(CRYPTO_LOCK_RAND); MD_Update(&m,md,MD_DIGEST_LENGTH); MD_Final(md,&m); + CRYPTO_w_unlock(CRYPTO_LOCK_RAND); + memset(&m,0,sizeof(m)); + if (ok) + return(1); + else + { + RANDerr(RAND_F_SSLEAY_RAND_BYTES,RAND_R_PRNG_NOT_SEEDED); + return(0); + } } #ifdef WINDOWS