summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorBodo Möller <bodo@openssl.org>1999-10-26 16:26:48 +0000
committerBodo Möller <bodo@openssl.org>1999-10-26 16:26:48 +0000
commit62ac2938015939e2ef30f12295f0ee59ff79c11b (patch)
tree5920f95e7f43caa16f6e46dfb6a655a51db50cb9 /crypto
parentc1e744b9125a883450c2239ec55ea606c618a5c0 (diff)
Always hash the pid in the first iteration in ssleay_rand_bytes,
don't try to detect fork()s by looking at getpid(). The reason is that threads sharing the same memory can have different PIDs; it's inefficient to run RAND_seed each time a different thread calls RAND_bytes.
Diffstat (limited to 'crypto')
-rw-r--r--crypto/rand/md_rand.c27
1 files changed, 9 insertions, 18 deletions
diff --git a/crypto/rand/md_rand.c b/crypto/rand/md_rand.c
index 26bb1244d0..729484fe92 100644
--- a/crypto/rand/md_rand.c
+++ b/crypto/rand/md_rand.c
@@ -287,8 +287,7 @@ static void ssleay_rand_bytes(unsigned char *buf, int num)
static int init=1;
unsigned long l;
#ifndef MSDOS
- static pid_t prev_pid = 0;
- pid_t curr_pid;
+ pid_t curr_pid = getpid();
#endif
#ifdef DEVRANDOM
FILE *fh;
@@ -329,8 +328,7 @@ static void ssleay_rand_bytes(unsigned char *buf, int num)
* just this */
RAND_seed(&m,sizeof(m));
#ifndef MSDOS
- prev_pid = getpid();
- l=prev_pid;
+ l=curr_pid;
RAND_seed(&l,sizeof(l));
l=getuid();
RAND_seed(&l,sizeof(l));
@@ -367,20 +365,6 @@ static void ssleay_rand_bytes(unsigned char *buf, int num)
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
-
st_idx=state_index;
st_num=state_num;
md_c[0] = md_count[0];
@@ -402,6 +386,13 @@ 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);
+#ifndef MSDOS
+ 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