summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rand')
-rw-r--r--src/lib/libcrypto/rand/Makefile71
-rw-r--r--src/lib/libcrypto/rand/md_rand.c46
-rw-r--r--src/lib/libcrypto/rand/rand_egd.c2
-rw-r--r--src/lib/libcrypto/rand/rand_lcl.h11
-rw-r--r--src/lib/libcrypto/rand/rand_os2.c6
-rw-r--r--src/lib/libcrypto/rand/rand_unix.c71
-rw-r--r--src/lib/libcrypto/rand/rand_win.c71
7 files changed, 183 insertions, 95 deletions
diff --git a/src/lib/libcrypto/rand/Makefile b/src/lib/libcrypto/rand/Makefile
index 30794305cb..27694aa664 100644
--- a/src/lib/libcrypto/rand/Makefile
+++ b/src/lib/libcrypto/rand/Makefile
@@ -17,9 +17,9 @@ TEST= randtest.c
17APPS= 17APPS=
18 18
19LIB=$(TOP)/libcrypto.a 19LIB=$(TOP)/libcrypto.a
20LIBSRC=md_rand.c randfile.c rand_lib.c rand_eng.c rand_err.c rand_egd.c \ 20LIBSRC=md_rand.c randfile.c rand_lib.c rand_err.c rand_egd.c \
21 rand_win.c rand_unix.c rand_os2.c rand_nw.c 21 rand_win.c rand_unix.c rand_os2.c rand_nw.c
22LIBOBJ=md_rand.o randfile.o rand_lib.o rand_eng.o rand_err.o rand_egd.o \ 22LIBOBJ=md_rand.o randfile.o rand_lib.o rand_err.o rand_egd.o \
23 rand_win.o rand_unix.o rand_os2.o rand_nw.o 23 rand_win.o rand_unix.o rand_os2.o rand_nw.o
24 24
25SRC= $(LIBSRC) 25SRC= $(LIBSRC)
@@ -35,7 +35,7 @@ top:
35all: lib 35all: lib
36 36
37lib: $(LIBOBJ) 37lib: $(LIBOBJ)
38 $(ARX) $(LIB) $(LIBOBJ) 38 $(AR) $(LIB) $(LIBOBJ)
39 $(RANLIB) $(LIB) || echo Never mind. 39 $(RANLIB) $(LIB) || echo Never mind.
40 @touch lib 40 @touch lib
41 41
@@ -79,34 +79,17 @@ clean:
79md_rand.o: ../../e_os.h ../../include/openssl/asn1.h 79md_rand.o: ../../e_os.h ../../include/openssl/asn1.h
80md_rand.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h 80md_rand.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
81md_rand.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h 81md_rand.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
82md_rand.o: ../../include/openssl/evp.h ../../include/openssl/fips.h 82md_rand.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h
83md_rand.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h 83md_rand.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
84md_rand.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h 84md_rand.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
85md_rand.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 85md_rand.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h
86md_rand.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h 86md_rand.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
87md_rand.o: ../../include/openssl/sha.h ../../include/openssl/stack.h 87md_rand.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
88md_rand.o: ../../include/openssl/symhacks.h md_rand.c rand_lcl.h 88md_rand.o: md_rand.c rand_lcl.h
89rand_egd.o: ../../include/openssl/buffer.h ../../include/openssl/e_os2.h 89rand_egd.o: ../../include/openssl/buffer.h ../../include/openssl/e_os2.h
90rand_egd.o: ../../include/openssl/opensslconf.h 90rand_egd.o: ../../include/openssl/opensslconf.h
91rand_egd.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h 91rand_egd.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h
92rand_egd.o: rand_egd.c 92rand_egd.o: rand_egd.c
93rand_eng.o: ../../e_os.h ../../include/openssl/asn1.h
94rand_eng.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
95rand_eng.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
96rand_eng.o: ../../include/openssl/des_old.h ../../include/openssl/e_os2.h
97rand_eng.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
98rand_eng.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h
99rand_eng.o: ../../include/openssl/err.h ../../include/openssl/evp.h
100rand_eng.o: ../../include/openssl/fips.h ../../include/openssl/fips_rand.h
101rand_eng.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
102rand_eng.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
103rand_eng.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
104rand_eng.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
105rand_eng.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
106rand_eng.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
107rand_eng.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
108rand_eng.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
109rand_eng.o: ../cryptlib.h rand_eng.c rand_lcl.h
110rand_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h 93rand_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
111rand_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h 94rand_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
112rand_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h 95rand_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
@@ -116,39 +99,34 @@ rand_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
116rand_err.o: rand_err.c 99rand_err.o: rand_err.c
117rand_lib.o: ../../e_os.h ../../include/openssl/asn1.h 100rand_lib.o: ../../e_os.h ../../include/openssl/asn1.h
118rand_lib.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h 101rand_lib.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
119rand_lib.o: ../../include/openssl/crypto.h ../../include/openssl/des.h 102rand_lib.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
120rand_lib.o: ../../include/openssl/des_old.h ../../include/openssl/e_os2.h
121rand_lib.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h 103rand_lib.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
122rand_lib.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h 104rand_lib.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h
123rand_lib.o: ../../include/openssl/err.h ../../include/openssl/evp.h 105rand_lib.o: ../../include/openssl/err.h ../../include/openssl/evp.h
124rand_lib.o: ../../include/openssl/fips.h ../../include/openssl/fips_rand.h
125rand_lib.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h 106rand_lib.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
126rand_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h 107rand_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
127rand_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 108rand_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
128rand_lib.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h 109rand_lib.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
129rand_lib.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h 110rand_lib.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
130rand_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h 111rand_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
131rand_lib.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
132rand_lib.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h 112rand_lib.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
133rand_lib.o: ../cryptlib.h rand_lcl.h rand_lib.c 113rand_lib.o: ../cryptlib.h rand_lib.c
134rand_nw.o: ../../e_os.h ../../include/openssl/asn1.h 114rand_nw.o: ../../e_os.h ../../include/openssl/asn1.h
135rand_nw.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h 115rand_nw.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
136rand_nw.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h 116rand_nw.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
137rand_nw.o: ../../include/openssl/err.h ../../include/openssl/evp.h 117rand_nw.o: ../../include/openssl/err.h ../../include/openssl/evp.h
138rand_nw.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h 118rand_nw.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
139rand_nw.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h 119rand_nw.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
140rand_nw.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h 120rand_nw.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
141rand_nw.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h 121rand_nw.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
142rand_nw.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h 122rand_nw.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
143rand_nw.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h 123rand_nw.o: ../../include/openssl/symhacks.h ../cryptlib.h rand_lcl.h rand_nw.c
144rand_nw.o: ../cryptlib.h rand_lcl.h rand_nw.c
145rand_os2.o: ../../e_os.h ../../include/openssl/asn1.h 124rand_os2.o: ../../e_os.h ../../include/openssl/asn1.h
146rand_os2.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h 125rand_os2.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
147rand_os2.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h 126rand_os2.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
148rand_os2.o: ../../include/openssl/err.h ../../include/openssl/evp.h 127rand_os2.o: ../../include/openssl/err.h ../../include/openssl/evp.h
149rand_os2.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h 128rand_os2.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
150rand_os2.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h 129rand_os2.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
151rand_os2.o: ../../include/openssl/opensslconf.h
152rand_os2.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 130rand_os2.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
153rand_os2.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h 131rand_os2.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
154rand_os2.o: ../../include/openssl/sha.h ../../include/openssl/stack.h 132rand_os2.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
@@ -158,8 +136,8 @@ rand_unix.o: ../../e_os.h ../../include/openssl/asn1.h
158rand_unix.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h 136rand_unix.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
159rand_unix.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h 137rand_unix.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
160rand_unix.o: ../../include/openssl/err.h ../../include/openssl/evp.h 138rand_unix.o: ../../include/openssl/err.h ../../include/openssl/evp.h
161rand_unix.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h 139rand_unix.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
162rand_unix.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h 140rand_unix.o: ../../include/openssl/objects.h
163rand_unix.o: ../../include/openssl/opensslconf.h 141rand_unix.o: ../../include/openssl/opensslconf.h
164rand_unix.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 142rand_unix.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
165rand_unix.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h 143rand_unix.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
@@ -170,9 +148,8 @@ rand_win.o: ../../e_os.h ../../include/openssl/asn1.h
170rand_win.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h 148rand_win.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
171rand_win.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h 149rand_win.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
172rand_win.o: ../../include/openssl/err.h ../../include/openssl/evp.h 150rand_win.o: ../../include/openssl/err.h ../../include/openssl/evp.h
173rand_win.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h 151rand_win.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
174rand_win.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h 152rand_win.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
175rand_win.o: ../../include/openssl/opensslconf.h
176rand_win.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 153rand_win.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
177rand_win.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h 154rand_win.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
178rand_win.o: ../../include/openssl/sha.h ../../include/openssl/stack.h 155rand_win.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
diff --git a/src/lib/libcrypto/rand/md_rand.c b/src/lib/libcrypto/rand/md_rand.c
index 0f8dd3e00f..88088ce73c 100644
--- a/src/lib/libcrypto/rand/md_rand.c
+++ b/src/lib/libcrypto/rand/md_rand.c
@@ -126,10 +126,6 @@
126 126
127#include <openssl/crypto.h> 127#include <openssl/crypto.h>
128#include <openssl/err.h> 128#include <openssl/err.h>
129#ifdef OPENSSL_FIPS
130#include <openssl/fips.h>
131#endif
132
133 129
134#ifdef BN_DEBUG 130#ifdef BN_DEBUG
135# define PREDICT 131# define PREDICT
@@ -149,7 +145,7 @@ static unsigned int crypto_lock_rand = 0; /* may be set only when a thread
149 * holds CRYPTO_LOCK_RAND 145 * holds CRYPTO_LOCK_RAND
150 * (to prevent double locking) */ 146 * (to prevent double locking) */
151/* access to lockin_thread is synchronized by CRYPTO_LOCK_RAND2 */ 147/* access to lockin_thread is synchronized by CRYPTO_LOCK_RAND2 */
152static unsigned long locking_thread = 0; /* valid iff crypto_lock_rand is set */ 148static CRYPTO_THREADID locking_threadid; /* valid iff crypto_lock_rand is set */
153 149
154 150
155#ifdef PREDICT 151#ifdef PREDICT
@@ -217,8 +213,10 @@ static void ssleay_rand_add(const void *buf, int num, double add)
217 /* check if we already have the lock */ 213 /* check if we already have the lock */
218 if (crypto_lock_rand) 214 if (crypto_lock_rand)
219 { 215 {
216 CRYPTO_THREADID cur;
217 CRYPTO_THREADID_current(&cur);
220 CRYPTO_r_lock(CRYPTO_LOCK_RAND2); 218 CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
221 do_not_lock = (locking_thread == CRYPTO_thread_id()); 219 do_not_lock = !CRYPTO_THREADID_cmp(&locking_threadid, &cur);
222 CRYPTO_r_unlock(CRYPTO_LOCK_RAND2); 220 CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
223 } 221 }
224 else 222 else
@@ -274,8 +272,16 @@ static void ssleay_rand_add(const void *buf, int num, double add)
274 } 272 }
275 else 273 else
276 MD_Update(&m,&(state[st_idx]),j); 274 MD_Update(&m,&(state[st_idx]),j);
277 275
276 /* DO NOT REMOVE THE FOLLOWING CALL TO MD_Update()! */
278 MD_Update(&m,buf,j); 277 MD_Update(&m,buf,j);
278 /* We know that line may cause programs such as
279 purify and valgrind to complain about use of
280 uninitialized data. The problem is not, it's
281 with the caller. Removing that line will make
282 sure you get really bad randomness and thereby
283 other problems such as very insecure keys. */
284
279 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c)); 285 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
280 MD_Final(&m,local_md); 286 MD_Final(&m,local_md);
281 md_c[1]++; 287 md_c[1]++;
@@ -336,14 +342,6 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
336#endif 342#endif
337 int do_stir_pool = 0; 343 int do_stir_pool = 0;
338 344
339#ifdef OPENSSL_FIPS
340 if(FIPS_mode())
341 {
342 FIPSerr(FIPS_F_SSLEAY_RAND_BYTES,FIPS_R_NON_FIPS_METHOD);
343 return 0;
344 }
345#endif
346
347#ifdef PREDICT 345#ifdef PREDICT
348 if (rand_predictable) 346 if (rand_predictable)
349 { 347 {
@@ -384,7 +382,7 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
384 382
385 /* prevent ssleay_rand_bytes() from trying to obtain the lock again */ 383 /* prevent ssleay_rand_bytes() from trying to obtain the lock again */
386 CRYPTO_w_lock(CRYPTO_LOCK_RAND2); 384 CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
387 locking_thread = CRYPTO_thread_id(); 385 CRYPTO_THREADID_current(&locking_threadid);
388 CRYPTO_w_unlock(CRYPTO_LOCK_RAND2); 386 CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
389 crypto_lock_rand = 1; 387 crypto_lock_rand = 1;
390 388
@@ -476,9 +474,15 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
476#endif 474#endif
477 MD_Update(&m,local_md,MD_DIGEST_LENGTH); 475 MD_Update(&m,local_md,MD_DIGEST_LENGTH);
478 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c)); 476 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
479#ifndef PURIFY 477
480 MD_Update(&m,buf,j); /* purify complains */ 478#ifndef PURIFY /* purify complains */
479 /* DO NOT REMOVE THE FOLLOWING CALL TO MD_Update()! */
480 MD_Update(&m,buf,j);
481 /* We know that line may cause programs such as
482 purify and valgrind to complain about use of
483 uninitialized data. */
481#endif 484#endif
485
482 k=(st_idx+MD_DIGEST_LENGTH/2)-st_num; 486 k=(st_idx+MD_DIGEST_LENGTH/2)-st_num;
483 if (k > 0) 487 if (k > 0)
484 { 488 {
@@ -539,15 +543,17 @@ static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num)
539 543
540static int ssleay_rand_status(void) 544static int ssleay_rand_status(void)
541 { 545 {
546 CRYPTO_THREADID cur;
542 int ret; 547 int ret;
543 int do_not_lock; 548 int do_not_lock;
544 549
550 CRYPTO_THREADID_current(&cur);
545 /* check if we already have the lock 551 /* check if we already have the lock
546 * (could happen if a RAND_poll() implementation calls RAND_status()) */ 552 * (could happen if a RAND_poll() implementation calls RAND_status()) */
547 if (crypto_lock_rand) 553 if (crypto_lock_rand)
548 { 554 {
549 CRYPTO_r_lock(CRYPTO_LOCK_RAND2); 555 CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
550 do_not_lock = (locking_thread == CRYPTO_thread_id()); 556 do_not_lock = !CRYPTO_THREADID_cmp(&locking_threadid, &cur);
551 CRYPTO_r_unlock(CRYPTO_LOCK_RAND2); 557 CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
552 } 558 }
553 else 559 else
@@ -559,7 +565,7 @@ static int ssleay_rand_status(void)
559 565
560 /* prevent ssleay_rand_bytes() from trying to obtain the lock again */ 566 /* prevent ssleay_rand_bytes() from trying to obtain the lock again */
561 CRYPTO_w_lock(CRYPTO_LOCK_RAND2); 567 CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
562 locking_thread = CRYPTO_thread_id(); 568 CRYPTO_THREADID_cpy(&locking_threadid, &cur);
563 CRYPTO_w_unlock(CRYPTO_LOCK_RAND2); 569 CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
564 crypto_lock_rand = 1; 570 crypto_lock_rand = 1;
565 } 571 }
diff --git a/src/lib/libcrypto/rand/rand_egd.c b/src/lib/libcrypto/rand/rand_egd.c
index 50bce6caba..d53b916ebe 100644
--- a/src/lib/libcrypto/rand/rand_egd.c
+++ b/src/lib/libcrypto/rand/rand_egd.c
@@ -95,7 +95,7 @@
95 * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255. 95 * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
96 */ 96 */
97 97
98#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_VOS) 98#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_BEOS)
99int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes) 99int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
100 { 100 {
101 return(-1); 101 return(-1);
diff --git a/src/lib/libcrypto/rand/rand_lcl.h b/src/lib/libcrypto/rand/rand_lcl.h
index 18cc9b1e4a..618a8ec899 100644
--- a/src/lib/libcrypto/rand/rand_lcl.h
+++ b/src/lib/libcrypto/rand/rand_lcl.h
@@ -154,16 +154,5 @@
154#define MD(a,b,c) EVP_Digest(a,b,c,NULL,EVP_md2(), NULL) 154#define MD(a,b,c) EVP_Digest(a,b,c,NULL,EVP_md2(), NULL)
155#endif 155#endif
156 156
157#ifndef OPENSSL_NO_ENGINE
158void int_RAND_set_callbacks(
159 int (*set_rand_func)(const RAND_METHOD *meth,
160 const RAND_METHOD **pmeth),
161 const RAND_METHOD *(*get_rand_func)
162 (const RAND_METHOD **pmeth));
163int eng_RAND_set_rand_method(const RAND_METHOD *meth,
164 const RAND_METHOD **pmeth);
165const RAND_METHOD *eng_RAND_get_rand_method(const RAND_METHOD **pmeth);
166#endif
167
168 157
169#endif 158#endif
diff --git a/src/lib/libcrypto/rand/rand_os2.c b/src/lib/libcrypto/rand/rand_os2.c
index c3e36d4e5e..fc1e78b179 100644
--- a/src/lib/libcrypto/rand/rand_os2.c
+++ b/src/lib/libcrypto/rand/rand_os2.c
@@ -78,8 +78,10 @@ typedef struct _CPUUTIL {
78 ULONG ulIntrHigh; /* High 32 bits of interrupt time */ 78 ULONG ulIntrHigh; /* High 32 bits of interrupt time */
79} CPUUTIL; 79} CPUUTIL;
80 80
81#ifndef __KLIBC__
81APIRET APIENTRY(*DosPerfSysCall) (ULONG ulCommand, ULONG ulParm1, ULONG ulParm2, ULONG ulParm3) = NULL; 82APIRET APIENTRY(*DosPerfSysCall) (ULONG ulCommand, ULONG ulParm1, ULONG ulParm2, ULONG ulParm3) = NULL;
82APIRET APIENTRY(*DosQuerySysState) (ULONG func, ULONG arg1, ULONG pid, ULONG _res_, PVOID buf, ULONG bufsz) = NULL; 83APIRET APIENTRY(*DosQuerySysState) (ULONG func, ULONG arg1, ULONG pid, ULONG _res_, PVOID buf, ULONG bufsz) = NULL;
84#endif
83HMODULE hDoscalls = 0; 85HMODULE hDoscalls = 0;
84 86
85int RAND_poll(void) 87int RAND_poll(void)
@@ -91,6 +93,7 @@ int RAND_poll(void)
91 if (hDoscalls == 0) { 93 if (hDoscalls == 0) {
92 ULONG rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS", &hDoscalls); 94 ULONG rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS", &hDoscalls);
93 95
96#ifndef __KLIBC__
94 if (rc == 0) { 97 if (rc == 0) {
95 rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&DosPerfSysCall); 98 rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&DosPerfSysCall);
96 99
@@ -102,6 +105,7 @@ int RAND_poll(void)
102 if (rc) 105 if (rc)
103 DosQuerySysState = NULL; 106 DosQuerySysState = NULL;
104 } 107 }
108#endif
105 } 109 }
106 110
107 /* Sample the hi-res timer, runs at around 1.1 MHz */ 111 /* Sample the hi-res timer, runs at around 1.1 MHz */
@@ -122,7 +126,9 @@ int RAND_poll(void)
122 RAND_add(&util, sizeof(util), 10); 126 RAND_add(&util, sizeof(util), 10);
123 } 127 }
124 else { 128 else {
129#ifndef __KLIBC__
125 DosPerfSysCall = NULL; 130 DosPerfSysCall = NULL;
131#endif
126 } 132 }
127 } 133 }
128 134
diff --git a/src/lib/libcrypto/rand/rand_unix.c b/src/lib/libcrypto/rand/rand_unix.c
index 71b98ec212..e9ead3a529 100644
--- a/src/lib/libcrypto/rand/rand_unix.c
+++ b/src/lib/libcrypto/rand/rand_unix.c
@@ -133,7 +133,50 @@
133# define FD_SETSIZE (8*sizeof(fd_set)) 133# define FD_SETSIZE (8*sizeof(fd_set))
134#endif 134#endif
135 135
136#ifdef __OpenBSD__ 136#ifdef __VOS__
137int RAND_poll(void)
138{
139 unsigned char buf[ENTROPY_NEEDED];
140 pid_t curr_pid;
141 uid_t curr_uid;
142 static int first=1;
143 int i;
144 long rnd = 0;
145 struct timespec ts;
146 unsigned seed;
147
148/* The VOS random() function starts from a static seed so its
149 initial value is predictable. If random() returns the
150 initial value, reseed it with dynamic data. The VOS
151 real-time clock has a granularity of 1 nsec so it should be
152 reasonably difficult to predict its exact value. Do not
153 gratuitously reseed the PRNG because other code in this
154 process or thread may be using it. */
155
156 if (first) {
157 first = 0;
158 rnd = random ();
159 if (rnd == 1804289383) {
160 clock_gettime (CLOCK_REALTIME, &ts);
161 curr_pid = getpid();
162 curr_uid = getuid();
163 seed = ts.tv_sec ^ ts.tv_nsec ^ curr_pid ^ curr_uid;
164 srandom (seed);
165 }
166 }
167
168 for (i = 0; i < sizeof(buf); i++) {
169 if (i % 4 == 0)
170 rnd = random();
171 buf[i] = rnd;
172 rnd >>= 8;
173 }
174 RAND_add(buf, sizeof(buf), ENTROPY_NEEDED);
175 memset(buf, 0, sizeof(buf));
176
177 return 1;
178}
179#elif defined __OpenBSD__
137int RAND_poll(void) 180int RAND_poll(void)
138{ 181{
139 u_int32_t rnd = 0, i; 182 u_int32_t rnd = 0, i;
@@ -163,7 +206,7 @@ int RAND_poll(void)
163 static const char *randomfiles[] = { DEVRANDOM }; 206 static const char *randomfiles[] = { DEVRANDOM };
164 struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; 207 struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])];
165 int fd; 208 int fd;
166 size_t i; 209 unsigned int i;
167#endif 210#endif
168#ifdef DEVRANDOM_EGD 211#ifdef DEVRANDOM_EGD
169 static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; 212 static const char *egdsockets[] = { DEVRANDOM_EGD, NULL };
@@ -176,7 +219,8 @@ int RAND_poll(void)
176 * have this. Use /dev/urandom if you can as /dev/random may block 219 * have this. Use /dev/urandom if you can as /dev/random may block
177 * if it runs out of random entries. */ 220 * if it runs out of random entries. */
178 221
179 for (i=0; i<sizeof(randomfiles)/sizeof(randomfiles[0]) && n < ENTROPY_NEEDED; i++) 222 for (i = 0; (i < sizeof(randomfiles)/sizeof(randomfiles[0])) &&
223 (n < ENTROPY_NEEDED); i++)
180 { 224 {
181 if ((fd = open(randomfiles[i], O_RDONLY 225 if ((fd = open(randomfiles[i], O_RDONLY
182#ifdef O_NONBLOCK 226#ifdef O_NONBLOCK
@@ -193,7 +237,7 @@ int RAND_poll(void)
193 { 237 {
194 int usec = 10*1000; /* spend 10ms on each file */ 238 int usec = 10*1000; /* spend 10ms on each file */
195 int r; 239 int r;
196 size_t j; 240 unsigned int j;
197 struct stat *st=&randomstats[i]; 241 struct stat *st=&randomstats[i];
198 242
199 /* Avoid using same input... Used to be O_NOFOLLOW 243 /* Avoid using same input... Used to be O_NOFOLLOW
@@ -211,7 +255,12 @@ int RAND_poll(void)
211 { 255 {
212 int try_read = 0; 256 int try_read = 0;
213 257
214#if defined(OPENSSL_SYS_LINUX) 258#if defined(OPENSSL_SYS_BEOS_R5)
259 /* select() is broken in BeOS R5, so we simply
260 * try to read something and snooze if we couldn't */
261 try_read = 1;
262
263#elif defined(OPENSSL_SYS_LINUX)
215 /* use poll() */ 264 /* use poll() */
216 struct pollfd pset; 265 struct pollfd pset;
217 266
@@ -258,6 +307,10 @@ int RAND_poll(void)
258 r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); 307 r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n);
259 if (r > 0) 308 if (r > 0)
260 n += r; 309 n += r;
310#if defined(OPENSSL_SYS_BEOS_R5)
311 if (r == 0)
312 snooze(t.tv_usec);
313#endif
261 } 314 }
262 else 315 else
263 r = -1; 316 r = -1;
@@ -311,6 +364,14 @@ int RAND_poll(void)
311 l=time(NULL); 364 l=time(NULL);
312 RAND_add(&l,sizeof(l),0.0); 365 RAND_add(&l,sizeof(l),0.0);
313 366
367#if defined(OPENSSL_SYS_BEOS)
368 {
369 system_info sysInfo;
370 get_system_info(&sysInfo);
371 RAND_add(&sysInfo,sizeof(sysInfo),0);
372 }
373#endif
374
314#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) 375#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
315 return 1; 376 return 1;
316#else 377#else
diff --git a/src/lib/libcrypto/rand/rand_win.c b/src/lib/libcrypto/rand/rand_win.c
index 00dbe4232c..5d134e186b 100644
--- a/src/lib/libcrypto/rand/rand_win.c
+++ b/src/lib/libcrypto/rand/rand_win.c
@@ -463,7 +463,7 @@ int RAND_poll(void)
463 PROCESSENTRY32 p; 463 PROCESSENTRY32 p;
464 THREADENTRY32 t; 464 THREADENTRY32 t;
465 MODULEENTRY32 m; 465 MODULEENTRY32 m;
466 DWORD stoptime = 0; 466 DWORD starttime = 0;
467 467
468 snap = (CREATETOOLHELP32SNAPSHOT) 468 snap = (CREATETOOLHELP32SNAPSHOT)
469 GetProcAddress(kernel, "CreateToolhelp32Snapshot"); 469 GetProcAddress(kernel, "CreateToolhelp32Snapshot");
@@ -494,12 +494,29 @@ int RAND_poll(void)
494 * each entry. Consider each field a source of 1 byte 494 * each entry. Consider each field a source of 1 byte
495 * of entropy. 495 * of entropy.
496 */ 496 */
497 ZeroMemory(&hlist, sizeof(HEAPLIST32));
497 hlist.dwSize = sizeof(HEAPLIST32); 498 hlist.dwSize = sizeof(HEAPLIST32);
498 if (good) stoptime = GetTickCount() + MAXDELAY; 499 if (good) starttime = GetTickCount();
500#ifdef _MSC_VER
499 if (heaplist_first(handle, &hlist)) 501 if (heaplist_first(handle, &hlist))
502 {
503 /*
504 following discussion on dev ML, exception on WinCE (or other Win
505 platform) is theoretically of unknown origin; prevent infinite
506 loop here when this theoretical case occurs; otherwise cope with
507 the expected (MSDN documented) exception-throwing behaviour of
508 Heap32Next() on WinCE.
509
510 based on patch in original message by Tanguy Fautré (2009/03/02)
511 Subject: RAND_poll() and CreateToolhelp32Snapshot() stability
512 */
513 int ex_cnt_limit = 42;
500 do 514 do
501 { 515 {
502 RAND_add(&hlist, hlist.dwSize, 3); 516 RAND_add(&hlist, hlist.dwSize, 3);
517 __try
518 {
519 ZeroMemory(&hentry, sizeof(HEAPENTRY32));
503 hentry.dwSize = sizeof(HEAPENTRY32); 520 hentry.dwSize = sizeof(HEAPENTRY32);
504 if (heap_first(&hentry, 521 if (heap_first(&hentry,
505 hlist.th32ProcessID, 522 hlist.th32ProcessID,
@@ -510,10 +527,42 @@ int RAND_poll(void)
510 RAND_add(&hentry, 527 RAND_add(&hentry,
511 hentry.dwSize, 5); 528 hentry.dwSize, 5);
512 while (heap_next(&hentry) 529 while (heap_next(&hentry)
530 && (!good || (GetTickCount()-starttime)<MAXDELAY)
513 && --entrycnt > 0); 531 && --entrycnt > 0);
514 } 532 }
515 } while (heaplist_next(handle, 533 }
516 &hlist) && GetTickCount() < stoptime); 534 __except (EXCEPTION_EXECUTE_HANDLER)
535 {
536 /* ignore access violations when walking the heap list */
537 ex_cnt_limit--;
538 }
539 } while (heaplist_next(handle, &hlist)
540 && (!good || (GetTickCount()-starttime)<MAXDELAY)
541 && ex_cnt_limit > 0);
542 }
543
544#else
545 if (heaplist_first(handle, &hlist))
546 {
547 do
548 {
549 RAND_add(&hlist, hlist.dwSize, 3);
550 hentry.dwSize = sizeof(HEAPENTRY32);
551 if (heap_first(&hentry,
552 hlist.th32ProcessID,
553 hlist.th32HeapID))
554 {
555 int entrycnt = 80;
556 do
557 RAND_add(&hentry,
558 hentry.dwSize, 5);
559 while (heap_next(&hentry)
560 && --entrycnt > 0);
561 }
562 } while (heaplist_next(handle, &hlist)
563 && (!good || (GetTickCount()-starttime)<MAXDELAY));
564 }
565#endif
517 566
518 /* process walking */ 567 /* process walking */
519 /* PROCESSENTRY32 contains 9 fields that will change 568 /* PROCESSENTRY32 contains 9 fields that will change
@@ -522,11 +571,11 @@ int RAND_poll(void)
522 */ 571 */
523 p.dwSize = sizeof(PROCESSENTRY32); 572 p.dwSize = sizeof(PROCESSENTRY32);
524 573
525 if (good) stoptime = GetTickCount() + MAXDELAY; 574 if (good) starttime = GetTickCount();
526 if (process_first(handle, &p)) 575 if (process_first(handle, &p))
527 do 576 do
528 RAND_add(&p, p.dwSize, 9); 577 RAND_add(&p, p.dwSize, 9);
529 while (process_next(handle, &p) && GetTickCount() < stoptime); 578 while (process_next(handle, &p) && (!good || (GetTickCount()-starttime)<MAXDELAY));
530 579
531 /* thread walking */ 580 /* thread walking */
532 /* THREADENTRY32 contains 6 fields that will change 581 /* THREADENTRY32 contains 6 fields that will change
@@ -534,11 +583,11 @@ int RAND_poll(void)
534 * 1 byte of entropy. 583 * 1 byte of entropy.
535 */ 584 */
536 t.dwSize = sizeof(THREADENTRY32); 585 t.dwSize = sizeof(THREADENTRY32);
537 if (good) stoptime = GetTickCount() + MAXDELAY; 586 if (good) starttime = GetTickCount();
538 if (thread_first(handle, &t)) 587 if (thread_first(handle, &t))
539 do 588 do
540 RAND_add(&t, t.dwSize, 6); 589 RAND_add(&t, t.dwSize, 6);
541 while (thread_next(handle, &t) && GetTickCount() < stoptime); 590 while (thread_next(handle, &t) && (!good || (GetTickCount()-starttime)<MAXDELAY));
542 591
543 /* module walking */ 592 /* module walking */
544 /* MODULEENTRY32 contains 9 fields that will change 593 /* MODULEENTRY32 contains 9 fields that will change
@@ -546,12 +595,12 @@ int RAND_poll(void)
546 * 1 byte of entropy. 595 * 1 byte of entropy.
547 */ 596 */
548 m.dwSize = sizeof(MODULEENTRY32); 597 m.dwSize = sizeof(MODULEENTRY32);
549 if (good) stoptime = GetTickCount() + MAXDELAY; 598 if (good) starttime = GetTickCount();
550 if (module_first(handle, &m)) 599 if (module_first(handle, &m))
551 do 600 do
552 RAND_add(&m, m.dwSize, 9); 601 RAND_add(&m, m.dwSize, 9);
553 while (module_next(handle, &m) 602 while (module_next(handle, &m)
554 && (GetTickCount() < stoptime)); 603 && (!good || (GetTickCount()-starttime)<MAXDELAY));
555 if (close_snap) 604 if (close_snap)
556 close_snap(handle); 605 close_snap(handle);
557 else 606 else
@@ -701,7 +750,7 @@ static void readscreen(void)
701 int y; /* y-coordinate of screen lines to grab */ 750 int y; /* y-coordinate of screen lines to grab */
702 int n = 16; /* number of screen lines to grab at a time */ 751 int n = 16; /* number of screen lines to grab at a time */
703 752
704 if (GetVersion() >= 0x80000000 || !OPENSSL_isservice()) 753 if (GetVersion() < 0x80000000 && OPENSSL_isservice()>0)
705 return; 754 return;
706 755
707 /* Create a screen DC and a memory DC compatible to screen DC */ 756 /* Create a screen DC and a memory DC compatible to screen DC */