* [including the GNU Public Licence.]
*/
+#define ENTROPY_NEEDED 32 /* require 128 bits of randomness */
+
#ifndef MD_RAND_DEBUG
# ifndef NDEBUG
# define NDEBUG
#include "openssl/e_os.h"
#include <openssl/crypto.h>
+#include <openssl/err.h>
#if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
#if !defined(NO_SHA) && !defined(NO_SHA1)
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 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)
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;
long md_c[2];
#ifndef THREADS
assert(md_c[1] == md_count[1]);
#endif
+ entropy += add;
+ }
+
+static void ssleay_rand_seed(const void *buf, int num)
+ {
+ ssleay_rand_add(buf, num, num);
}
-static void ssleay_rand_bytes(unsigned char *buf, int 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 MSDOS
- static pid_t prev_pid = 0;
- pid_t curr_pid;
+#ifndef GETPID_IS_MEANINGLESS
+ pid_t curr_pid = getpid();
#endif
#ifdef DEVRANDOM
FILE *fh;
for (i=0; i<num; i++)
buf[i]=val++;
- return;
+ return(1);
}
#endif
CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
/* put in some default random data, we need more than
* just this */
- RAND_seed(&m,sizeof(m));
-#ifndef MSDOS
- prev_pid = getpid();
- l=prev_pid;
- RAND_seed(&l,sizeof(l));
+ RAND_add(&m,sizeof(m),0);
+#ifndef GETPID_IS_MEANINGLESS
+ l=curr_pid;
+ RAND_add(&l,sizeof(l),0);
l=getuid();
- RAND_seed(&l,sizeof(l));
+ RAND_add(&l,sizeof(l),0);
#endif
l=time(NULL);
- RAND_seed(&l,sizeof(l));
+ RAND_add(&l,sizeof(l),0);
#ifdef DEVRANDOM
/*
*/
if ((fh = fopen(DEVRANDOM, "r")) != NULL)
{
- unsigned char tmpbuf[32];
+ unsigned char tmpbuf[ENTROPY_NEEDED];
+ int n;
- fread((unsigned char *)tmpbuf,1,32,fh);
- /* we don't care how many bytes we read,
- * we will just copy the 'stack' if there is
- * nothing else :-) */
+ n=fread((unsigned char *)tmpbuf,1,ENTROPY_NEEDED,fh);
fclose(fh);
- RAND_seed(tmpbuf,32);
- memset(tmpbuf,0,32);
+ RAND_add(tmpbuf,sizeof tmpbuf,n);
+ memset(tmpbuf,0,n);
}
#endif
#ifdef PURIFY
init=0;
}
-#ifndef MSDOS
- /* make sure we have unique states when a program forks
- * (new with OpenSSL 0.9.5; for earlier versions, applications
- * must take care of this) */
- curr_pid = getpid();
- if (prev_pid != curr_pid)
- {
- prev_pid = curr_pid;
- CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
- RAND_seed(&curr_pid, sizeof curr_pid);
- CRYPTO_w_lock(CRYPTO_LOCK_RAND);
- }
-#endif
+ ok = (entropy >= ENTROPY_NEEDED);
st_idx=state_index;
st_num=state_num;
j=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;
num-=j;
MD_Init(&m);
+#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
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);
+ }
+ }
+
+/* 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