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/Makefile196
-rw-r--r--src/lib/libcrypto/rand/Makefile.ssl196
-rw-r--r--src/lib/libcrypto/rand/md_rand.c572
-rw-r--r--src/lib/libcrypto/rand/rand.h10
-rw-r--r--src/lib/libcrypto/rand/rand_egd.c299
-rw-r--r--src/lib/libcrypto/rand/rand_err.c8
-rw-r--r--src/lib/libcrypto/rand/rand_lcl.h158
-rw-r--r--src/lib/libcrypto/rand/rand_lib.c12
-rw-r--r--src/lib/libcrypto/rand/rand_os2.c147
-rw-r--r--src/lib/libcrypto/rand/rand_unix.c264
-rw-r--r--src/lib/libcrypto/rand/rand_vms.c135
-rw-r--r--src/lib/libcrypto/rand/rand_win.c727
-rw-r--r--src/lib/libcrypto/rand/randfile.c6
-rw-r--r--src/lib/libcrypto/rand/randtest.c216
14 files changed, 2913 insertions, 33 deletions
diff --git a/src/lib/libcrypto/rand/Makefile b/src/lib/libcrypto/rand/Makefile
new file mode 100644
index 0000000000..665eaa18e5
--- /dev/null
+++ b/src/lib/libcrypto/rand/Makefile
@@ -0,0 +1,196 @@
1#
2# SSLeay/crypto/rand/Makefile
3#
4
5DIR= rand
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALL_PREFIX=
11OPENSSLDIR= /usr/local/ssl
12INSTALLTOP=/usr/local/ssl
13MAKEDEPPROG= makedepend
14MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG)
15MAKEFILE= Makefile
16AR= ar r
17
18CFLAGS= $(INCLUDES) $(CFLAG)
19
20GENERAL=Makefile
21TEST= randtest.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC=md_rand.c randfile.c rand_lib.c rand_err.c rand_egd.c \
26 rand_win.c rand_unix.c rand_os2.c
27LIBOBJ=md_rand.o randfile.o rand_lib.o rand_err.o rand_egd.o \
28 rand_win.o rand_unix.o rand_os2.o
29
30SRC= $(LIBSRC)
31
32EXHEADER= rand.h
33HEADER= $(EXHEADER)
34
35ALL= $(GENERAL) $(SRC) $(HEADER)
36
37top:
38 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
39
40all: lib
41
42lib: $(LIBOBJ)
43 $(AR) $(LIB) $(LIBOBJ)
44 $(RANLIB) $(LIB) || echo Never mind.
45 @touch lib
46
47files:
48 $(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
49
50links:
51 @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
52 @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
53 @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
54
55install:
56 @headerlist="$(EXHEADER)"; for i in $$headerlist ; \
57 do \
58 (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
59 chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
72
73dclean:
74 $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
81
82md_rand.o: ../../e_os.h ../../include/openssl/aes.h
83md_rand.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
84md_rand.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
85md_rand.o: ../../include/openssl/cast.h ../../include/openssl/crypto.h
86md_rand.o: ../../include/openssl/des.h ../../include/openssl/des_old.h
87md_rand.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
88md_rand.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
89md_rand.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
90md_rand.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
91md_rand.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
92md_rand.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
93md_rand.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
94md_rand.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
95md_rand.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h
96md_rand.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h
97md_rand.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h
98md_rand.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
99md_rand.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
100md_rand.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h
101md_rand.o: ../../include/openssl/ui_compat.h md_rand.c rand_lcl.h
102rand_egd.o: ../../include/openssl/buffer.h ../../include/openssl/e_os2.h
103rand_egd.o: ../../include/openssl/opensslconf.h
104rand_egd.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h
105rand_egd.o: rand_egd.c
106rand_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
107rand_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
108rand_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
109rand_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
110rand_err.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
111rand_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
112rand_err.o: rand_err.c
113rand_lib.o: ../../e_os.h ../../include/openssl/asn1.h
114rand_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
115rand_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
116rand_lib.o: ../../include/openssl/des.h ../../include/openssl/des_old.h
117rand_lib.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
118rand_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h
119rand_lib.o: ../../include/openssl/err.h ../../include/openssl/fips.h
120rand_lib.o: ../../include/openssl/fips_rand.h ../../include/openssl/lhash.h
121rand_lib.o: ../../include/openssl/opensslconf.h
122rand_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
123rand_lib.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
124rand_lib.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
125rand_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h
126rand_lib.o: ../../include/openssl/ui_compat.h ../cryptlib.h rand_lib.c
127rand_os2.o: ../../e_os.h ../../include/openssl/aes.h
128rand_os2.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
129rand_os2.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
130rand_os2.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
131rand_os2.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
132rand_os2.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
133rand_os2.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
134rand_os2.o: ../../include/openssl/err.h ../../include/openssl/evp.h
135rand_os2.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
136rand_os2.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
137rand_os2.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
138rand_os2.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
139rand_os2.o: ../../include/openssl/opensslconf.h
140rand_os2.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
141rand_os2.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
142rand_os2.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
143rand_os2.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
144rand_os2.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
145rand_os2.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
146rand_os2.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
147rand_os2.o: ../cryptlib.h rand_lcl.h rand_os2.c
148rand_unix.o: ../../e_os.h ../../include/openssl/aes.h
149rand_unix.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
150rand_unix.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
151rand_unix.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
152rand_unix.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
153rand_unix.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
154rand_unix.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
155rand_unix.o: ../../include/openssl/err.h ../../include/openssl/evp.h
156rand_unix.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
157rand_unix.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
158rand_unix.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
159rand_unix.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
160rand_unix.o: ../../include/openssl/opensslconf.h
161rand_unix.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
162rand_unix.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
163rand_unix.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
164rand_unix.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
165rand_unix.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
166rand_unix.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
167rand_unix.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
168rand_unix.o: ../cryptlib.h rand_lcl.h rand_unix.c
169rand_win.o: ../../e_os.h ../../include/openssl/aes.h
170rand_win.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
171rand_win.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
172rand_win.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
173rand_win.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
174rand_win.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
175rand_win.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
176rand_win.o: ../../include/openssl/err.h ../../include/openssl/evp.h
177rand_win.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
178rand_win.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
179rand_win.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
180rand_win.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
181rand_win.o: ../../include/openssl/opensslconf.h
182rand_win.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
183rand_win.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
184rand_win.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
185rand_win.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
186rand_win.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
187rand_win.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
188rand_win.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
189rand_win.o: ../cryptlib.h rand_lcl.h rand_win.c
190randfile.o: ../../e_os.h ../../include/openssl/buffer.h
191randfile.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
192randfile.o: ../../include/openssl/opensslconf.h
193randfile.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
194randfile.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
195randfile.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
196randfile.o: randfile.c
diff --git a/src/lib/libcrypto/rand/Makefile.ssl b/src/lib/libcrypto/rand/Makefile.ssl
new file mode 100644
index 0000000000..e5cbe5319c
--- /dev/null
+++ b/src/lib/libcrypto/rand/Makefile.ssl
@@ -0,0 +1,196 @@
1#
2# SSLeay/crypto/rand/Makefile
3#
4
5DIR= rand
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALL_PREFIX=
11OPENSSLDIR= /usr/local/ssl
12INSTALLTOP=/usr/local/ssl
13MAKE= make -f Makefile.ssl
14MAKEDEPPROG= makedepend
15MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG)
16MAKEFILE= Makefile.ssl
17AR= ar r
18
19CFLAGS= $(INCLUDES) $(CFLAG)
20
21GENERAL=Makefile
22TEST= randtest.c
23APPS=
24
25LIB=$(TOP)/libcrypto.a
26LIBSRC=md_rand.c randfile.c rand_lib.c rand_err.c rand_egd.c \
27 rand_win.c rand_unix.c rand_os2.c
28LIBOBJ=md_rand.o randfile.o rand_lib.o rand_err.o rand_egd.o \
29 rand_win.o rand_unix.o rand_os2.o
30
31SRC= $(LIBSRC)
32
33EXHEADER= rand.h
34HEADER= $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: lib
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 $(RANLIB) $(LIB) || echo Never mind.
46 @touch lib
47
48files:
49 $(PERL) $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
50
51links:
52 @sh $(TOP)/util/point.sh Makefile.ssl Makefile
53 @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
54 @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
55 @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
56
57install:
58 @for i in $(EXHEADER) ; \
59 do \
60 (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
61 chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
62 done;
63
64tags:
65 ctags $(SRC)
66
67tests:
68
69lint:
70 lint -DLINT $(INCLUDES) $(SRC)>fluff
71
72depend:
73 $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
74
75dclean:
76 $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
77 mv -f Makefile.new $(MAKEFILE)
78
79clean:
80 rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
81
82# DO NOT DELETE THIS LINE -- make depend depends on it.
83
84md_rand.o: ../../e_os.h ../../include/openssl/aes.h
85md_rand.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
86md_rand.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
87md_rand.o: ../../include/openssl/cast.h ../../include/openssl/crypto.h
88md_rand.o: ../../include/openssl/des.h ../../include/openssl/des_old.h
89md_rand.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
90md_rand.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
91md_rand.o: ../../include/openssl/evp.h ../../include/openssl/idea.h
92md_rand.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h
93md_rand.o: ../../include/openssl/md4.h ../../include/openssl/md5.h
94md_rand.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h
95md_rand.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
96md_rand.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
97md_rand.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
98md_rand.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
99md_rand.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
100md_rand.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
101md_rand.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
102md_rand.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
103md_rand.o: md_rand.c rand_lcl.h
104rand_egd.o: ../../include/openssl/buffer.h ../../include/openssl/e_os2.h
105rand_egd.o: ../../include/openssl/opensslconf.h
106rand_egd.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h
107rand_egd.o: rand_egd.c
108rand_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
109rand_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
110rand_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
111rand_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
112rand_err.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
113rand_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
114rand_err.o: rand_err.c
115rand_lib.o: ../../e_os.h ../../include/openssl/asn1.h
116rand_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
117rand_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
118rand_lib.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
119rand_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h
120rand_lib.o: ../../include/openssl/err.h ../../include/openssl/lhash.h
121rand_lib.o: ../../include/openssl/opensslconf.h
122rand_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
123rand_lib.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
124rand_lib.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
125rand_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h
126rand_lib.o: ../cryptlib.h rand_lib.c
127rand_os2.o: ../../e_os.h ../../include/openssl/aes.h
128rand_os2.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
129rand_os2.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
130rand_os2.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
131rand_os2.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
132rand_os2.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
133rand_os2.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
134rand_os2.o: ../../include/openssl/err.h ../../include/openssl/evp.h
135rand_os2.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
136rand_os2.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
137rand_os2.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
138rand_os2.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
139rand_os2.o: ../../include/openssl/opensslconf.h
140rand_os2.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
141rand_os2.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
142rand_os2.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
143rand_os2.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
144rand_os2.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
145rand_os2.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
146rand_os2.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
147rand_os2.o: ../cryptlib.h rand_lcl.h rand_os2.c
148rand_unix.o: ../../e_os.h ../../include/openssl/aes.h
149rand_unix.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
150rand_unix.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
151rand_unix.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
152rand_unix.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
153rand_unix.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
154rand_unix.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
155rand_unix.o: ../../include/openssl/err.h ../../include/openssl/evp.h
156rand_unix.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
157rand_unix.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
158rand_unix.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
159rand_unix.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
160rand_unix.o: ../../include/openssl/opensslconf.h
161rand_unix.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
162rand_unix.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
163rand_unix.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
164rand_unix.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
165rand_unix.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
166rand_unix.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
167rand_unix.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
168rand_unix.o: ../cryptlib.h rand_lcl.h rand_unix.c
169rand_win.o: ../../e_os.h ../../include/openssl/aes.h
170rand_win.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
171rand_win.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
172rand_win.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
173rand_win.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
174rand_win.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
175rand_win.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
176rand_win.o: ../../include/openssl/err.h ../../include/openssl/evp.h
177rand_win.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
178rand_win.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
179rand_win.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
180rand_win.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
181rand_win.o: ../../include/openssl/opensslconf.h
182rand_win.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
183rand_win.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h
184rand_win.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
185rand_win.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
186rand_win.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
187rand_win.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
188rand_win.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
189rand_win.o: ../cryptlib.h rand_lcl.h rand_win.c
190randfile.o: ../../e_os.h ../../include/openssl/buffer.h
191randfile.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
192randfile.o: ../../include/openssl/opensslconf.h
193randfile.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
194randfile.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
195randfile.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
196randfile.o: randfile.c
diff --git a/src/lib/libcrypto/rand/md_rand.c b/src/lib/libcrypto/rand/md_rand.c
new file mode 100644
index 0000000000..eeffc0df4c
--- /dev/null
+++ b/src/lib/libcrypto/rand/md_rand.c
@@ -0,0 +1,572 @@
1/* crypto/rand/md_rand.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#ifdef MD_RAND_DEBUG
113# ifndef NDEBUG
114# define NDEBUG
115# endif
116#endif
117
118#include <assert.h>
119#include <stdio.h>
120#include <string.h>
121
122#include "e_os.h"
123
124#include <openssl/rand.h>
125#include "rand_lcl.h"
126
127#include <openssl/crypto.h>
128#include <openssl/err.h>
129
130#ifdef BN_DEBUG
131# define PREDICT
132#endif
133
134/* #define PREDICT 1 */
135
136#define STATE_SIZE 1023
137static int state_num=0,state_index=0;
138static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH];
139static unsigned char md[MD_DIGEST_LENGTH];
140static long md_count[2]={0,0};
141static double entropy=0;
142static int initialized=0;
143
144static unsigned int crypto_lock_rand = 0; /* may be set only when a thread
145 * holds CRYPTO_LOCK_RAND
146 * (to prevent double locking) */
147/* access to lockin_thread is synchronized by CRYPTO_LOCK_RAND2 */
148static unsigned long locking_thread = 0; /* valid iff crypto_lock_rand is set */
149
150
151#ifdef PREDICT
152int rand_predictable=0;
153#endif
154
155const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT;
156
157static void ssleay_rand_cleanup(void);
158static void ssleay_rand_seed(const void *buf, int num);
159static void ssleay_rand_add(const void *buf, int num, double add_entropy);
160static int ssleay_rand_bytes(unsigned char *buf, int num);
161static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num);
162static int ssleay_rand_status(void);
163
164RAND_METHOD rand_ssleay_meth={
165 ssleay_rand_seed,
166 ssleay_rand_bytes,
167 ssleay_rand_cleanup,
168 ssleay_rand_add,
169 ssleay_rand_pseudo_bytes,
170 ssleay_rand_status
171 };
172
173RAND_METHOD *RAND_SSLeay(void)
174 {
175 return(&rand_ssleay_meth);
176 }
177
178static void ssleay_rand_cleanup(void)
179 {
180 OPENSSL_cleanse(state,sizeof(state));
181 state_num=0;
182 state_index=0;
183 OPENSSL_cleanse(md,MD_DIGEST_LENGTH);
184 md_count[0]=0;
185 md_count[1]=0;
186 entropy=0;
187 initialized=0;
188 }
189
190static void ssleay_rand_add(const void *buf, int num, double add)
191 {
192 int i,j,k,st_idx;
193 long md_c[2];
194 unsigned char local_md[MD_DIGEST_LENGTH];
195 EVP_MD_CTX m;
196 int do_not_lock;
197
198 /*
199 * (Based on the rand(3) manpage)
200 *
201 * The input is chopped up into units of 20 bytes (or less for
202 * the last block). Each of these blocks is run through the hash
203 * function as follows: The data passed to the hash function
204 * is the current 'md', the same number of bytes from the 'state'
205 * (the location determined by in incremented looping index) as
206 * the current 'block', the new key data 'block', and 'count'
207 * (which is incremented after each use).
208 * The result of this is kept in 'md' and also xored into the
209 * 'state' at the same locations that were used as input into the
210 * hash function.
211 */
212
213 /* check if we already have the lock */
214 if (crypto_lock_rand)
215 {
216 CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
217 do_not_lock = (locking_thread == CRYPTO_thread_id());
218 CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
219 }
220 else
221 do_not_lock = 0;
222
223 if (!do_not_lock) CRYPTO_w_lock(CRYPTO_LOCK_RAND);
224 st_idx=state_index;
225
226 /* use our own copies of the counters so that even
227 * if a concurrent thread seeds with exactly the
228 * same data and uses the same subarray there's _some_
229 * difference */
230 md_c[0] = md_count[0];
231 md_c[1] = md_count[1];
232
233 memcpy(local_md, md, sizeof md);
234
235 /* state_index <= state_num <= STATE_SIZE */
236 state_index += num;
237 if (state_index >= STATE_SIZE)
238 {
239 state_index%=STATE_SIZE;
240 state_num=STATE_SIZE;
241 }
242 else if (state_num < STATE_SIZE)
243 {
244 if (state_index > state_num)
245 state_num=state_index;
246 }
247 /* state_index <= state_num <= STATE_SIZE */
248
249 /* state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE]
250 * are what we will use now, but other threads may use them
251 * as well */
252
253 md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0);
254
255 if (!do_not_lock) CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
256
257 EVP_MD_CTX_init(&m);
258 for (i=0; i<num; i+=MD_DIGEST_LENGTH)
259 {
260 j=(num-i);
261 j=(j > MD_DIGEST_LENGTH)?MD_DIGEST_LENGTH:j;
262
263 MD_Init(&m);
264 MD_Update(&m,local_md,MD_DIGEST_LENGTH);
265 k=(st_idx+j)-STATE_SIZE;
266 if (k > 0)
267 {
268 MD_Update(&m,&(state[st_idx]),j-k);
269 MD_Update(&m,&(state[0]),k);
270 }
271 else
272 MD_Update(&m,&(state[st_idx]),j);
273
274 MD_Update(&m,buf,j);
275 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
276 MD_Final(&m,local_md);
277 md_c[1]++;
278
279 buf=(const char *)buf + j;
280
281 for (k=0; k<j; k++)
282 {
283 /* Parallel threads may interfere with this,
284 * but always each byte of the new state is
285 * the XOR of some previous value of its
286 * and local_md (itermediate values may be lost).
287 * Alway using locking could hurt performance more
288 * than necessary given that conflicts occur only
289 * when the total seeding is longer than the random
290 * state. */
291 state[st_idx++]^=local_md[k];
292 if (st_idx >= STATE_SIZE)
293 st_idx=0;
294 }
295 }
296 EVP_MD_CTX_cleanup(&m);
297
298 if (!do_not_lock) CRYPTO_w_lock(CRYPTO_LOCK_RAND);
299 /* Don't just copy back local_md into md -- this could mean that
300 * other thread's seeding remains without effect (except for
301 * the incremented counter). By XORing it we keep at least as
302 * much entropy as fits into md. */
303 for (k = 0; k < sizeof md; k++)
304 {
305 md[k] ^= local_md[k];
306 }
307 if (entropy < ENTROPY_NEEDED) /* stop counting when we have enough */
308 entropy += add;
309 if (!do_not_lock) CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
310
311#if !defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32)
312 assert(md_c[1] == md_count[1]);
313#endif
314 }
315
316static void ssleay_rand_seed(const void *buf, int num)
317 {
318 ssleay_rand_add(buf, num, num);
319 }
320
321static int ssleay_rand_bytes(unsigned char *buf, int num)
322 {
323 static volatile int stirred_pool = 0;
324 int i,j,k,st_num,st_idx;
325 int num_ceil;
326 int ok;
327 long md_c[2];
328 unsigned char local_md[MD_DIGEST_LENGTH];
329 EVP_MD_CTX m;
330#ifndef GETPID_IS_MEANINGLESS
331 pid_t curr_pid = getpid();
332#endif
333 int do_stir_pool = 0;
334
335#ifdef PREDICT
336 if (rand_predictable)
337 {
338 static unsigned char val=0;
339
340 for (i=0; i<num; i++)
341 buf[i]=val++;
342 return(1);
343 }
344#endif
345
346 if (num <= 0)
347 return 1;
348
349 EVP_MD_CTX_init(&m);
350 /* round upwards to multiple of MD_DIGEST_LENGTH/2 */
351 num_ceil = (1 + (num-1)/(MD_DIGEST_LENGTH/2)) * (MD_DIGEST_LENGTH/2);
352
353 /*
354 * (Based on the rand(3) manpage:)
355 *
356 * For each group of 10 bytes (or less), we do the following:
357 *
358 * Input into the hash function the local 'md' (which is initialized from
359 * the global 'md' before any bytes are generated), the bytes that are to
360 * be overwritten by the random bytes, and bytes from the 'state'
361 * (incrementing looping index). From this digest output (which is kept
362 * in 'md'), the top (up to) 10 bytes are returned to the caller and the
363 * bottom 10 bytes are xored into the 'state'.
364 *
365 * Finally, after we have finished 'num' random bytes for the
366 * caller, 'count' (which is incremented) and the local and global 'md'
367 * are fed into the hash function and the results are kept in the
368 * global 'md'.
369 */
370
371 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
372
373 /* prevent ssleay_rand_bytes() from trying to obtain the lock again */
374 CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
375 locking_thread = CRYPTO_thread_id();
376 CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
377 crypto_lock_rand = 1;
378
379 if (!initialized)
380 {
381 RAND_poll();
382 initialized = 1;
383 }
384
385 if (!stirred_pool)
386 do_stir_pool = 1;
387
388 ok = (entropy >= ENTROPY_NEEDED);
389 if (!ok)
390 {
391 /* If the PRNG state is not yet unpredictable, then seeing
392 * the PRNG output may help attackers to determine the new
393 * state; thus we have to decrease the entropy estimate.
394 * Once we've had enough initial seeding we don't bother to
395 * adjust the entropy count, though, because we're not ambitious
396 * to provide *information-theoretic* randomness.
397 *
398 * NOTE: This approach fails if the program forks before
399 * we have enough entropy. Entropy should be collected
400 * in a separate input pool and be transferred to the
401 * output pool only when the entropy limit has been reached.
402 */
403 entropy -= num;
404 if (entropy < 0)
405 entropy = 0;
406 }
407
408 if (do_stir_pool)
409 {
410 /* In the output function only half of 'md' remains secret,
411 * so we better make sure that the required entropy gets
412 * 'evenly distributed' through 'state', our randomness pool.
413 * The input function (ssleay_rand_add) chains all of 'md',
414 * which makes it more suitable for this purpose.
415 */
416
417 int n = STATE_SIZE; /* so that the complete pool gets accessed */
418 while (n > 0)
419 {
420#if MD_DIGEST_LENGTH > 20
421# error "Please adjust DUMMY_SEED."
422#endif
423#define DUMMY_SEED "...................." /* at least MD_DIGEST_LENGTH */
424 /* Note that the seed does not matter, it's just that
425 * ssleay_rand_add expects to have something to hash. */
426 ssleay_rand_add(DUMMY_SEED, MD_DIGEST_LENGTH, 0.0);
427 n -= MD_DIGEST_LENGTH;
428 }
429 if (ok)
430 stirred_pool = 1;
431 }
432
433 st_idx=state_index;
434 st_num=state_num;
435 md_c[0] = md_count[0];
436 md_c[1] = md_count[1];
437 memcpy(local_md, md, sizeof md);
438
439 state_index+=num_ceil;
440 if (state_index > state_num)
441 state_index %= state_num;
442
443 /* state[st_idx], ..., state[(st_idx + num_ceil - 1) % st_num]
444 * are now ours (but other threads may use them too) */
445
446 md_count[0] += 1;
447
448 /* before unlocking, we must clear 'crypto_lock_rand' */
449 crypto_lock_rand = 0;
450 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
451
452 while (num > 0)
453 {
454 /* num_ceil -= MD_DIGEST_LENGTH/2 */
455 j=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;
456 num-=j;
457 MD_Init(&m);
458#ifndef GETPID_IS_MEANINGLESS
459 if (curr_pid) /* just in the first iteration to save time */
460 {
461 MD_Update(&m,(unsigned char*)&curr_pid,sizeof curr_pid);
462 curr_pid = 0;
463 }
464#endif
465 MD_Update(&m,local_md,MD_DIGEST_LENGTH);
466 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
467#ifndef PURIFY
468 MD_Update(&m,buf,j); /* purify complains */
469#endif
470 k=(st_idx+MD_DIGEST_LENGTH/2)-st_num;
471 if (k > 0)
472 {
473 MD_Update(&m,&(state[st_idx]),MD_DIGEST_LENGTH/2-k);
474 MD_Update(&m,&(state[0]),k);
475 }
476 else
477 MD_Update(&m,&(state[st_idx]),MD_DIGEST_LENGTH/2);
478 MD_Final(&m,local_md);
479
480 for (i=0; i<MD_DIGEST_LENGTH/2; i++)
481 {
482 state[st_idx++]^=local_md[i]; /* may compete with other threads */
483 if (st_idx >= st_num)
484 st_idx=0;
485 if (i < j)
486 *(buf++)=local_md[i+MD_DIGEST_LENGTH/2];
487 }
488 }
489
490 MD_Init(&m);
491 MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
492 MD_Update(&m,local_md,MD_DIGEST_LENGTH);
493 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
494 MD_Update(&m,md,MD_DIGEST_LENGTH);
495 MD_Final(&m,md);
496 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
497
498 EVP_MD_CTX_cleanup(&m);
499 if (ok)
500 return(1);
501 else
502 {
503 RANDerr(RAND_F_SSLEAY_RAND_BYTES,RAND_R_PRNG_NOT_SEEDED);
504 ERR_add_error_data(1, "You need to read the OpenSSL FAQ, "
505 "http://www.openssl.org/support/faq.html");
506 return(0);
507 }
508 }
509
510/* pseudo-random bytes that are guaranteed to be unique but not
511 unpredictable */
512static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num)
513 {
514 int ret;
515 unsigned long err;
516
517 ret = RAND_bytes(buf, num);
518 if (ret == 0)
519 {
520 err = ERR_peek_error();
521 if (ERR_GET_LIB(err) == ERR_LIB_RAND &&
522 ERR_GET_REASON(err) == RAND_R_PRNG_NOT_SEEDED)
523 (void)ERR_get_error();
524 }
525 return (ret);
526 }
527
528static int ssleay_rand_status(void)
529 {
530 int ret;
531 int do_not_lock;
532
533 /* check if we already have the lock
534 * (could happen if a RAND_poll() implementation calls RAND_status()) */
535 if (crypto_lock_rand)
536 {
537 CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
538 do_not_lock = (locking_thread == CRYPTO_thread_id());
539 CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
540 }
541 else
542 do_not_lock = 0;
543
544 if (!do_not_lock)
545 {
546 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
547
548 /* prevent ssleay_rand_bytes() from trying to obtain the lock again */
549 CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
550 locking_thread = CRYPTO_thread_id();
551 CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
552 crypto_lock_rand = 1;
553 }
554
555 if (!initialized)
556 {
557 RAND_poll();
558 initialized = 1;
559 }
560
561 ret = entropy >= ENTROPY_NEEDED;
562
563 if (!do_not_lock)
564 {
565 /* before unlocking, we must clear 'crypto_lock_rand' */
566 crypto_lock_rand = 0;
567
568 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
569 }
570
571 return ret;
572 }
diff --git a/src/lib/libcrypto/rand/rand.h b/src/lib/libcrypto/rand/rand.h
index 604df9be6c..606382dd21 100644
--- a/src/lib/libcrypto/rand/rand.h
+++ b/src/lib/libcrypto/rand/rand.h
@@ -71,10 +71,6 @@
71extern "C" { 71extern "C" {
72#endif 72#endif
73 73
74#if defined(OPENSSL_FIPS)
75#define FIPS_RAND_SIZE_T int
76#endif
77
78typedef struct rand_meth_st 74typedef struct rand_meth_st
79 { 75 {
80 void (*seed)(const void *buf, int num); 76 void (*seed)(const void *buf, int num);
@@ -125,17 +121,11 @@ void ERR_load_RAND_strings(void);
125/* Error codes for the RAND functions. */ 121/* Error codes for the RAND functions. */
126 122
127/* Function codes. */ 123/* Function codes. */
128#define RAND_F_FIPS_RAND_BYTES 102
129#define RAND_F_RAND_GET_RAND_METHOD 101 124#define RAND_F_RAND_GET_RAND_METHOD 101
130#define RAND_F_SSLEAY_RAND_BYTES 100 125#define RAND_F_SSLEAY_RAND_BYTES 100
131 126
132/* Reason codes. */ 127/* Reason codes. */
133#define RAND_R_NON_FIPS_METHOD 101
134#define RAND_R_PRNG_ASKING_FOR_TOO_MUCH 105
135#define RAND_R_PRNG_NOT_REKEYED 103
136#define RAND_R_PRNG_NOT_RESEEDED 104
137#define RAND_R_PRNG_NOT_SEEDED 100 128#define RAND_R_PRNG_NOT_SEEDED 100
138#define RAND_R_PRNG_STUCK 102
139 129
140#ifdef __cplusplus 130#ifdef __cplusplus
141} 131}
diff --git a/src/lib/libcrypto/rand/rand_egd.c b/src/lib/libcrypto/rand/rand_egd.c
new file mode 100644
index 0000000000..6f742900a0
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand_egd.c
@@ -0,0 +1,299 @@
1/* crypto/rand/rand_egd.c */
2/* Written by Ulf Moeller and Lutz Jaenicke for the OpenSSL project. */
3/* ====================================================================
4 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this
19 * software must display the following acknowledgment:
20 * "This product includes software developed by the OpenSSL Project
21 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 *
23 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission. For written permission, please contact
26 * openssl-core@openssl.org.
27 *
28 * 5. Products derived from this software may not be called "OpenSSL"
29 * nor may "OpenSSL" appear in their names without prior written
30 * permission of the OpenSSL Project.
31 *
32 * 6. Redistributions of any form whatsoever must retain the following
33 * acknowledgment:
34 * "This product includes software developed by the OpenSSL Project
35 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This product includes cryptographic software written by Eric Young
52 * (eay@cryptsoft.com). This product includes software written by Tim
53 * Hudson (tjh@cryptsoft.com).
54 *
55 */
56
57#include <openssl/e_os2.h>
58#include <openssl/rand.h>
59#include <openssl/buffer.h>
60
61/*
62 * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
63 *
64 * This module supplies three routines:
65 *
66 * RAND_query_egd_bytes(path, buf, bytes)
67 * will actually query "bytes" bytes of entropy form the egd-socket located
68 * at path and will write them to buf (if supplied) or will directly feed
69 * it to RAND_seed() if buf==NULL.
70 * The number of bytes is not limited by the maximum chunk size of EGD,
71 * which is 255 bytes. If more than 255 bytes are wanted, several chunks
72 * of entropy bytes are requested. The connection is left open until the
73 * query is competed.
74 * RAND_query_egd_bytes() returns with
75 * -1 if an error occured during connection or communication.
76 * num the number of bytes read from the EGD socket. This number is either
77 * the number of bytes requested or smaller, if the EGD pool is
78 * drained and the daemon signals that the pool is empty.
79 * This routine does not touch any RAND_status(). This is necessary, since
80 * PRNG functions may call it during initialization.
81 *
82 * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them
83 * used to seed the PRNG.
84 * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL.
85 * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the
86 * seed status so that the return value can reflect the seed state:
87 * -1 if an error occured during connection or communication _or_
88 * if the PRNG has still not received the required seeding.
89 * num the number of bytes read from the EGD socket. This number is either
90 * the number of bytes requested or smaller, if the EGD pool is
91 * drained and the daemon signals that the pool is empty.
92 *
93 * RAND_egd(path) will query 255 bytes and use the bytes retreived to seed
94 * the PRNG.
95 * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
96 */
97
98#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS)
99int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
100 {
101 return(-1);
102 }
103int RAND_egd(const char *path)
104 {
105 return(-1);
106 }
107
108int RAND_egd_bytes(const char *path,int bytes)
109 {
110 return(-1);
111 }
112#else
113#include <openssl/opensslconf.h>
114#include OPENSSL_UNISTD
115#include <sys/types.h>
116#include <sys/socket.h>
117#ifndef NO_SYS_UN_H
118# ifdef OPENSSL_SYS_VXWORKS
119# include <streams/un.h>
120# else
121# include <sys/un.h>
122# endif
123#else
124struct sockaddr_un {
125 short sun_family; /* AF_UNIX */
126 char sun_path[108]; /* path name (gag) */
127};
128#endif /* NO_SYS_UN_H */
129#include <string.h>
130#include <errno.h>
131
132#ifndef offsetof
133# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
134#endif
135
136int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
137 {
138 int ret = 0;
139 struct sockaddr_un addr;
140 int len, num, numbytes;
141 int fd = -1;
142 int success;
143 unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
144
145 memset(&addr, 0, sizeof(addr));
146 addr.sun_family = AF_UNIX;
147 if (strlen(path) >= sizeof(addr.sun_path))
148 return (-1);
149 BUF_strlcpy(addr.sun_path,path,sizeof addr.sun_path);
150 len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
151 fd = socket(AF_UNIX, SOCK_STREAM, 0);
152 if (fd == -1) return (-1);
153 success = 0;
154 while (!success)
155 {
156 if (connect(fd, (struct sockaddr *)&addr, len) == 0)
157 success = 1;
158 else
159 {
160 switch (errno)
161 {
162#ifdef EINTR
163 case EINTR:
164#endif
165#ifdef EAGAIN
166 case EAGAIN:
167#endif
168#ifdef EINPROGRESS
169 case EINPROGRESS:
170#endif
171#ifdef EALREADY
172 case EALREADY:
173#endif
174 /* No error, try again */
175 break;
176#ifdef EISCONN
177 case EISCONN:
178 success = 1;
179 break;
180#endif
181 default:
182 goto err; /* failure */
183 }
184 }
185 }
186
187 while(bytes > 0)
188 {
189 egdbuf[0] = 1;
190 egdbuf[1] = bytes < 255 ? bytes : 255;
191 numbytes = 0;
192 while (numbytes != 2)
193 {
194 num = write(fd, egdbuf + numbytes, 2 - numbytes);
195 if (num >= 0)
196 numbytes += num;
197 else
198 {
199 switch (errno)
200 {
201#ifdef EINTR
202 case EINTR:
203#endif
204#ifdef EAGAIN
205 case EAGAIN:
206#endif
207 /* No error, try again */
208 break;
209 default:
210 ret = -1;
211 goto err; /* failure */
212 }
213 }
214 }
215 numbytes = 0;
216 while (numbytes != 1)
217 {
218 num = read(fd, egdbuf, 1);
219 if (num >= 0)
220 numbytes += num;
221 else
222 {
223 switch (errno)
224 {
225#ifdef EINTR
226 case EINTR:
227#endif
228#ifdef EAGAIN
229 case EAGAIN:
230#endif
231 /* No error, try again */
232 break;
233 default:
234 ret = -1;
235 goto err; /* failure */
236 }
237 }
238 }
239 if(egdbuf[0] == 0)
240 goto err;
241 if (buf)
242 retrievebuf = buf + ret;
243 else
244 retrievebuf = tempbuf;
245 numbytes = 0;
246 while (numbytes != egdbuf[0])
247 {
248 num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
249 if (num >= 0)
250 numbytes += num;
251 else
252 {
253 switch (errno)
254 {
255#ifdef EINTR
256 case EINTR:
257#endif
258#ifdef EAGAIN
259 case EAGAIN:
260#endif
261 /* No error, try again */
262 break;
263 default:
264 ret = -1;
265 goto err; /* failure */
266 }
267 }
268 }
269 ret += egdbuf[0];
270 bytes -= egdbuf[0];
271 if (!buf)
272 RAND_seed(tempbuf, egdbuf[0]);
273 }
274 err:
275 if (fd != -1) close(fd);
276 return(ret);
277 }
278
279
280int RAND_egd_bytes(const char *path, int bytes)
281 {
282 int num, ret = 0;
283
284 num = RAND_query_egd_bytes(path, NULL, bytes);
285 if (num < 1) goto err;
286 if (RAND_status() == 1)
287 ret = num;
288 err:
289 return(ret);
290 }
291
292
293int RAND_egd(const char *path)
294 {
295 return (RAND_egd_bytes(path, 255));
296 }
297
298
299#endif
diff --git a/src/lib/libcrypto/rand/rand_err.c b/src/lib/libcrypto/rand/rand_err.c
index 95574659ac..b77267e213 100644
--- a/src/lib/libcrypto/rand/rand_err.c
+++ b/src/lib/libcrypto/rand/rand_err.c
@@ -1,6 +1,6 @@
1/* crypto/rand/rand_err.c */ 1/* crypto/rand/rand_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -66,7 +66,6 @@
66#ifndef OPENSSL_NO_ERR 66#ifndef OPENSSL_NO_ERR
67static ERR_STRING_DATA RAND_str_functs[]= 67static ERR_STRING_DATA RAND_str_functs[]=
68 { 68 {
69{ERR_PACK(0,RAND_F_FIPS_RAND_BYTES,0), "FIPS_RAND_BYTES"},
70{ERR_PACK(0,RAND_F_RAND_GET_RAND_METHOD,0), "RAND_get_rand_method"}, 69{ERR_PACK(0,RAND_F_RAND_GET_RAND_METHOD,0), "RAND_get_rand_method"},
71{ERR_PACK(0,RAND_F_SSLEAY_RAND_BYTES,0), "SSLEAY_RAND_BYTES"}, 70{ERR_PACK(0,RAND_F_SSLEAY_RAND_BYTES,0), "SSLEAY_RAND_BYTES"},
72{0,NULL} 71{0,NULL}
@@ -74,12 +73,7 @@ static ERR_STRING_DATA RAND_str_functs[]=
74 73
75static ERR_STRING_DATA RAND_str_reasons[]= 74static ERR_STRING_DATA RAND_str_reasons[]=
76 { 75 {
77{RAND_R_NON_FIPS_METHOD ,"non fips method"},
78{RAND_R_PRNG_ASKING_FOR_TOO_MUCH ,"prng asking for too much"},
79{RAND_R_PRNG_NOT_REKEYED ,"prng not rekeyed"},
80{RAND_R_PRNG_NOT_RESEEDED ,"prng not reseeded"},
81{RAND_R_PRNG_NOT_SEEDED ,"PRNG not seeded"}, 76{RAND_R_PRNG_NOT_SEEDED ,"PRNG not seeded"},
82{RAND_R_PRNG_STUCK ,"prng stuck"},
83{0,NULL} 77{0,NULL}
84 }; 78 };
85 79
diff --git a/src/lib/libcrypto/rand/rand_lcl.h b/src/lib/libcrypto/rand/rand_lcl.h
new file mode 100644
index 0000000000..618a8ec899
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand_lcl.h
@@ -0,0 +1,158 @@
1/* crypto/rand/rand_lcl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#ifndef HEADER_RAND_LCL_H
113#define HEADER_RAND_LCL_H
114
115#define ENTROPY_NEEDED 32 /* require 256 bits = 32 bytes of randomness */
116
117
118#if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
119#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
120#define USE_SHA1_RAND
121#elif !defined(OPENSSL_NO_MD5)
122#define USE_MD5_RAND
123#elif !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DES)
124#define USE_MDC2_RAND
125#elif !defined(OPENSSL_NO_MD2)
126#define USE_MD2_RAND
127#else
128#error No message digest algorithm available
129#endif
130#endif
131
132#include <openssl/evp.h>
133#define MD_Update(a,b,c) EVP_DigestUpdate(a,b,c)
134#define MD_Final(a,b) EVP_DigestFinal_ex(a,b,NULL)
135#if defined(USE_MD5_RAND)
136#include <openssl/md5.h>
137#define MD_DIGEST_LENGTH MD5_DIGEST_LENGTH
138#define MD_Init(a) EVP_DigestInit_ex(a,EVP_md5(), NULL)
139#define MD(a,b,c) EVP_Digest(a,b,c,NULL,EVP_md5(), NULL)
140#elif defined(USE_SHA1_RAND)
141#include <openssl/sha.h>
142#define MD_DIGEST_LENGTH SHA_DIGEST_LENGTH
143#define MD_Init(a) EVP_DigestInit_ex(a,EVP_sha1(), NULL)
144#define MD(a,b,c) EVP_Digest(a,b,c,NULL,EVP_sha1(), NULL)
145#elif defined(USE_MDC2_RAND)
146#include <openssl/mdc2.h>
147#define MD_DIGEST_LENGTH MDC2_DIGEST_LENGTH
148#define MD_Init(a) EVP_DigestInit_ex(a,EVP_mdc2(), NULL)
149#define MD(a,b,c) EVP_Digest(a,b,c,NULL,EVP_mdc2(), NULL)
150#elif defined(USE_MD2_RAND)
151#include <openssl/md2.h>
152#define MD_DIGEST_LENGTH MD2_DIGEST_LENGTH
153#define MD_Init(a) EVP_DigestInit_ex(a,EVP_md2(), NULL)
154#define MD(a,b,c) EVP_Digest(a,b,c,NULL,EVP_md2(), NULL)
155#endif
156
157
158#endif
diff --git a/src/lib/libcrypto/rand/rand_lib.c b/src/lib/libcrypto/rand/rand_lib.c
index 88f1b56d91..513e338985 100644
--- a/src/lib/libcrypto/rand/rand_lib.c
+++ b/src/lib/libcrypto/rand/rand_lib.c
@@ -63,8 +63,6 @@
63#ifndef OPENSSL_NO_ENGINE 63#ifndef OPENSSL_NO_ENGINE
64#include <openssl/engine.h> 64#include <openssl/engine.h>
65#endif 65#endif
66#include <openssl/fips.h>
67#include <openssl/fips_rand.h>
68 66
69#ifndef OPENSSL_NO_ENGINE 67#ifndef OPENSSL_NO_ENGINE
70/* non-NULL if default_RAND_meth is ENGINE-provided */ 68/* non-NULL if default_RAND_meth is ENGINE-provided */
@@ -87,16 +85,6 @@ int RAND_set_rand_method(const RAND_METHOD *meth)
87 85
88const RAND_METHOD *RAND_get_rand_method(void) 86const RAND_METHOD *RAND_get_rand_method(void)
89 { 87 {
90#ifdef OPENSSL_FIPS
91 if(FIPS_mode()
92 && default_RAND_meth != FIPS_rand_check())
93 {
94 RANDerr(RAND_F_RAND_GET_RAND_METHOD,RAND_R_NON_FIPS_METHOD);
95 return 0;
96 }
97#endif
98
99
100 if (!default_RAND_meth) 88 if (!default_RAND_meth)
101 { 89 {
102#ifndef OPENSSL_NO_ENGINE 90#ifndef OPENSSL_NO_ENGINE
diff --git a/src/lib/libcrypto/rand/rand_os2.c b/src/lib/libcrypto/rand/rand_os2.c
new file mode 100644
index 0000000000..c3e36d4e5e
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand_os2.c
@@ -0,0 +1,147 @@
1/* crypto/rand/rand_os2.c */
2/* ====================================================================
3 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56#include "cryptlib.h"
57#include <openssl/rand.h>
58#include "rand_lcl.h"
59
60#ifdef OPENSSL_SYS_OS2
61
62#define INCL_DOSPROCESS
63#define INCL_DOSPROFILE
64#define INCL_DOSMISC
65#define INCL_DOSMODULEMGR
66#include <os2.h>
67
68#define CMD_KI_RDCNT (0x63)
69
70typedef struct _CPUUTIL {
71 ULONG ulTimeLow; /* Low 32 bits of time stamp */
72 ULONG ulTimeHigh; /* High 32 bits of time stamp */
73 ULONG ulIdleLow; /* Low 32 bits of idle time */
74 ULONG ulIdleHigh; /* High 32 bits of idle time */
75 ULONG ulBusyLow; /* Low 32 bits of busy time */
76 ULONG ulBusyHigh; /* High 32 bits of busy time */
77 ULONG ulIntrLow; /* Low 32 bits of interrupt time */
78 ULONG ulIntrHigh; /* High 32 bits of interrupt time */
79} CPUUTIL;
80
81APIRET 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;
83HMODULE hDoscalls = 0;
84
85int RAND_poll(void)
86{
87 char failed_module[20];
88 QWORD qwTime;
89 ULONG SysVars[QSV_FOREGROUND_PROCESS];
90
91 if (hDoscalls == 0) {
92 ULONG rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS", &hDoscalls);
93
94 if (rc == 0) {
95 rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&DosPerfSysCall);
96
97 if (rc)
98 DosPerfSysCall = NULL;
99
100 rc = DosQueryProcAddr(hDoscalls, 368, NULL, (PFN *)&DosQuerySysState);
101
102 if (rc)
103 DosQuerySysState = NULL;
104 }
105 }
106
107 /* Sample the hi-res timer, runs at around 1.1 MHz */
108 DosTmrQueryTime(&qwTime);
109 RAND_add(&qwTime, sizeof(qwTime), 2);
110
111 /* Sample a bunch of system variables, includes various process & memory statistics */
112 DosQuerySysInfo(1, QSV_FOREGROUND_PROCESS, SysVars, sizeof(SysVars));
113 RAND_add(SysVars, sizeof(SysVars), 4);
114
115 /* If available, sample CPU registers that count at CPU MHz
116 * Only fairly new CPUs (PPro & K6 onwards) & OS/2 versions support this
117 */
118 if (DosPerfSysCall) {
119 CPUUTIL util;
120
121 if (DosPerfSysCall(CMD_KI_RDCNT, (ULONG)&util, 0, 0) == 0) {
122 RAND_add(&util, sizeof(util), 10);
123 }
124 else {
125 DosPerfSysCall = NULL;
126 }
127 }
128
129 /* DosQuerySysState() gives us a huge quantity of process, thread, memory & handle stats */
130 if (DosQuerySysState) {
131 char *buffer = OPENSSL_malloc(256 * 1024);
132
133 if (DosQuerySysState(0x1F, 0, 0, 0, buffer, 256 * 1024) == 0) {
134 /* First 4 bytes in buffer is a pointer to the thread count
135 * there should be at least 1 byte of entropy per thread
136 */
137 RAND_add(buffer, 256 * 1024, **(ULONG **)buffer);
138 }
139
140 OPENSSL_free(buffer);
141 return 1;
142 }
143
144 return 0;
145}
146
147#endif /* OPENSSL_SYS_OS2 */
diff --git a/src/lib/libcrypto/rand/rand_unix.c b/src/lib/libcrypto/rand/rand_unix.c
new file mode 100644
index 0000000000..0599719dd1
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand_unix.c
@@ -0,0 +1,264 @@
1/* crypto/rand/rand_unix.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#define USE_SOCKETS
113#include "e_os.h"
114#include "cryptlib.h"
115#include <openssl/rand.h>
116#include "rand_lcl.h"
117
118#if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_VXWORKS))
119
120#include <sys/types.h>
121#include <sys/time.h>
122#include <sys/times.h>
123#include <fcntl.h>
124#include <unistd.h>
125#include <time.h>
126
127#ifdef __OpenBSD__
128int RAND_poll(void)
129{
130 u_int32_t rnd = 0, i;
131 unsigned char buf[ENTROPY_NEEDED];
132
133 for (i = 0; i < sizeof(buf); i++) {
134 if (i % 4 == 0)
135 rnd = arc4random();
136 buf[i] = rnd;
137 rnd >>= 8;
138 }
139 RAND_add(buf, sizeof(buf), ENTROPY_NEEDED);
140 memset(buf, 0, sizeof(buf));
141
142 return 1;
143}
144#else
145int RAND_poll(void)
146{
147 unsigned long l;
148 pid_t curr_pid = getpid();
149#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
150 unsigned char tmpbuf[ENTROPY_NEEDED];
151 int n = 0;
152#endif
153#ifdef DEVRANDOM
154 static const char *randomfiles[] = { DEVRANDOM, NULL };
155 const char **randomfile = NULL;
156 int fd;
157#endif
158#ifdef DEVRANDOM_EGD
159 static const char *egdsockets[] = { DEVRANDOM_EGD, NULL };
160 const char **egdsocket = NULL;
161#endif
162
163#ifdef DEVRANDOM
164 /* Use a random entropy pool device. Linux, FreeBSD and OpenBSD
165 * have this. Use /dev/urandom if you can as /dev/random may block
166 * if it runs out of random entries. */
167
168 for (randomfile = randomfiles; *randomfile && n < ENTROPY_NEEDED; randomfile++)
169 {
170 if ((fd = open(*randomfile, O_RDONLY|O_NONBLOCK
171#ifdef O_NOCTTY /* If it happens to be a TTY (god forbid), do not make it
172 our controlling tty */
173 |O_NOCTTY
174#endif
175#ifdef O_NOFOLLOW /* Fail if the file is a symbolic link */
176 |O_NOFOLLOW
177#endif
178 )) >= 0)
179 {
180 struct timeval t = { 0, 10*1000 }; /* Spend 10ms on
181 each file. */
182 int r;
183 fd_set fset;
184
185 do
186 {
187 FD_ZERO(&fset);
188 FD_SET(fd, &fset);
189 r = -1;
190
191 if (select(fd+1,&fset,NULL,NULL,&t) < 0)
192 t.tv_usec=0;
193 else if (FD_ISSET(fd, &fset))
194 {
195 r=read(fd,(unsigned char *)tmpbuf+n,
196 ENTROPY_NEEDED-n);
197 if (r > 0)
198 n += r;
199 }
200
201 /* Some Unixen will update t, some
202 won't. For those who won't, give
203 up here, otherwise, we will do
204 this once again for the remaining
205 time. */
206 if (t.tv_usec == 10*1000)
207 t.tv_usec=0;
208 }
209 while ((r > 0 || (errno == EINTR || errno == EAGAIN))
210 && t.tv_usec != 0 && n < ENTROPY_NEEDED);
211
212 close(fd);
213 }
214 }
215#endif
216
217#ifdef DEVRANDOM_EGD
218 /* Use an EGD socket to read entropy from an EGD or PRNGD entropy
219 * collecting daemon. */
220
221 for (egdsocket = egdsockets; *egdsocket && n < ENTROPY_NEEDED; egdsocket++)
222 {
223 int r;
224
225 r = RAND_query_egd_bytes(*egdsocket, (unsigned char *)tmpbuf+n,
226 ENTROPY_NEEDED-n);
227 if (r > 0)
228 n += r;
229 }
230#endif
231
232#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
233 if (n > 0)
234 {
235 RAND_add(tmpbuf,sizeof tmpbuf,n);
236 OPENSSL_cleanse(tmpbuf,n);
237 }
238#endif
239
240 /* put in some default random data, we need more than just this */
241 l=curr_pid;
242 RAND_add(&l,sizeof(l),0);
243 l=getuid();
244 RAND_add(&l,sizeof(l),0);
245
246 l=time(NULL);
247 RAND_add(&l,sizeof(l),0);
248
249#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
250 return 1;
251#else
252 return 0;
253#endif
254}
255
256#endif
257#endif
258
259#if defined(OPENSSL_SYS_VXWORKS)
260int RAND_poll(void)
261{
262 return 0;
263}
264#endif
diff --git a/src/lib/libcrypto/rand/rand_vms.c b/src/lib/libcrypto/rand/rand_vms.c
new file mode 100644
index 0000000000..29b2d7af0b
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand_vms.c
@@ -0,0 +1,135 @@
1/* crypto/rand/rand_vms.c -*- mode:C; c-file-style: "eay" -*- */
2/* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <openssl/rand.h>
60#include "rand_lcl.h"
61
62#if defined(OPENSSL_SYS_VMS)
63
64#include <descrip.h>
65#include <jpidef.h>
66#include <ssdef.h>
67#include <starlet.h>
68#ifdef __DECC
69# pragma message disable DOLLARID
70#endif
71
72static struct items_data_st
73 {
74 short length, code; /* length is amount of bytes */
75 } items_data[] =
76 { { 4, JPI$_BUFIO },
77 { 4, JPI$_CPUTIM },
78 { 4, JPI$_DIRIO },
79 { 8, JPI$_LOGINTIM },
80 { 4, JPI$_PAGEFLTS },
81 { 4, JPI$_PID },
82 { 4, JPI$_WSSIZE },
83 { 0, 0 }
84 };
85
86int RAND_poll(void)
87 {
88 long pid, iosb[2];
89 int status = 0;
90 struct
91 {
92 short length, code;
93 long *buffer;
94 int *retlen;
95 } item[32], *pitem;
96 unsigned char data_buffer[256];
97 short total_length = 0;
98 struct items_data_st *pitems_data;
99
100 pitems_data = items_data;
101 pitem = item;
102
103 /* Setup */
104 while (pitems_data->length)
105 {
106 pitem->length = pitems_data->length;
107 pitem->code = pitems_data->code;
108 pitem->buffer = (long *)data_buffer[total_length];
109 pitem->retlen = 0;
110 total_length += pitems_data->length;
111 pitems_data++;
112 pitem++;
113 }
114 pitem->length = pitem->code = 0;
115
116 /*
117 * Scan through all the processes in the system and add entropy with
118 * results from the processes that were possible to look at.
119 * However, view the information as only half trustable.
120 */
121 pid = -1; /* search context */
122 while ((status = sys$getjpiw(0, &pid, 0, item, iosb, 0, 0))
123 != SS$_NOMOREPROC)
124 {
125 if (status == SS$_NORMAL)
126 {
127 RAND_add(data_buffer, total_length, total_length/2);
128 }
129 }
130 sys$gettim(iosb);
131 RAND_add((unsigned char *)iosb, sizeof(iosb), sizeof(iosb)/2);
132 return 1;
133}
134
135#endif
diff --git a/src/lib/libcrypto/rand/rand_win.c b/src/lib/libcrypto/rand/rand_win.c
new file mode 100644
index 0000000000..3584842224
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand_win.c
@@ -0,0 +1,727 @@
1/* crypto/rand/rand_win.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#include "cryptlib.h"
113#include <openssl/rand.h>
114#include "rand_lcl.h"
115
116#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
117#include <windows.h>
118#ifndef _WIN32_WINNT
119# define _WIN32_WINNT 0x0400
120#endif
121#include <wincrypt.h>
122#include <tlhelp32.h>
123
124/* Intel hardware RNG CSP -- available from
125 * http://developer.intel.com/design/security/rng/redist_license.htm
126 */
127#define PROV_INTEL_SEC 22
128#define INTEL_DEF_PROV TEXT("Intel Hardware Cryptographic Service Provider")
129
130static void readtimer(void);
131static void readscreen(void);
132
133/* It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined
134 when WINVER is 0x0500 and up, which currently only happens on Win2000.
135 Unfortunately, those are typedefs, so they're a little bit difficult to
136 detect properly. On the other hand, the macro CURSOR_SHOWING is defined
137 within the same conditional, so it can be use to detect the absence of said
138 typedefs. */
139
140#ifndef CURSOR_SHOWING
141/*
142 * Information about the global cursor.
143 */
144typedef struct tagCURSORINFO
145{
146 DWORD cbSize;
147 DWORD flags;
148 HCURSOR hCursor;
149 POINT ptScreenPos;
150} CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
151
152#define CURSOR_SHOWING 0x00000001
153#endif /* CURSOR_SHOWING */
154
155typedef BOOL (WINAPI *CRYPTACQUIRECONTEXT)(HCRYPTPROV *, LPCTSTR, LPCTSTR,
156 DWORD, DWORD);
157typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *);
158typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD);
159
160typedef HWND (WINAPI *GETFOREGROUNDWINDOW)(VOID);
161typedef BOOL (WINAPI *GETCURSORINFO)(PCURSORINFO);
162typedef DWORD (WINAPI *GETQUEUESTATUS)(UINT);
163
164typedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
165typedef BOOL (WINAPI *CLOSETOOLHELP32SNAPSHOT)(HANDLE);
166typedef BOOL (WINAPI *HEAP32FIRST)(LPHEAPENTRY32, DWORD, DWORD);
167typedef BOOL (WINAPI *HEAP32NEXT)(LPHEAPENTRY32);
168typedef BOOL (WINAPI *HEAP32LIST)(HANDLE, LPHEAPLIST32);
169typedef BOOL (WINAPI *PROCESS32)(HANDLE, LPPROCESSENTRY32);
170typedef BOOL (WINAPI *THREAD32)(HANDLE, LPTHREADENTRY32);
171typedef BOOL (WINAPI *MODULE32)(HANDLE, LPMODULEENTRY32);
172
173#include <lmcons.h>
174#ifndef OPENSSL_SYS_WINCE
175#include <lmstats.h>
176#endif
177#if 1 /* The NET API is Unicode only. It requires the use of the UNICODE
178 * macro. When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was
179 * was added to the Platform SDK to allow the NET API to be used in
180 * non-Unicode applications provided that Unicode strings were still
181 * used for input. LMSTR is defined as LPWSTR.
182 */
183typedef NET_API_STATUS (NET_API_FUNCTION * NETSTATGET)
184 (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE*);
185typedef NET_API_STATUS (NET_API_FUNCTION * NETFREE)(LPBYTE);
186#endif /* 1 */
187
188int RAND_poll(void)
189{
190 MEMORYSTATUS m;
191 HCRYPTPROV hProvider = 0;
192 BYTE buf[64];
193 DWORD w;
194 HWND h;
195
196 HMODULE advapi, kernel, user, netapi;
197 CRYPTACQUIRECONTEXT acquire = 0;
198 CRYPTGENRANDOM gen = 0;
199 CRYPTRELEASECONTEXT release = 0;
200#if 1 /* There was previously a problem with NETSTATGET. Currently, this
201 * section is still experimental, but if all goes well, this conditional
202 * will be removed
203 */
204 NETSTATGET netstatget = 0;
205 NETFREE netfree = 0;
206#endif /* 1 */
207
208 /* Determine the OS version we are on so we can turn off things
209 * that do not work properly.
210 */
211 OSVERSIONINFO osverinfo ;
212 osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
213 GetVersionEx( &osverinfo ) ;
214
215#if defined(OPENSSL_SYS_WINCE) && WCEPLATFORM!=MS_HPC_PRO
216 /* poll the CryptoAPI PRNG */
217 /* The CryptoAPI returns sizeof(buf) bytes of randomness */
218 if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
219 {
220 if (CryptGenRandom(hProvider, sizeof(buf), buf))
221 RAND_add(buf, sizeof(buf), sizeof(buf));
222 CryptReleaseContext(hProvider, 0);
223 }
224#endif
225
226 /* load functions dynamically - not available on all systems */
227 advapi = LoadLibrary(TEXT("ADVAPI32.DLL"));
228 kernel = LoadLibrary(TEXT("KERNEL32.DLL"));
229 user = LoadLibrary(TEXT("USER32.DLL"));
230 netapi = LoadLibrary(TEXT("NETAPI32.DLL"));
231
232#ifndef OPENSSL_SYS_WINCE
233#if 1 /* There was previously a problem with NETSTATGET. Currently, this
234 * section is still experimental, but if all goes well, this conditional
235 * will be removed
236 */
237 if (netapi)
238 {
239 netstatget = (NETSTATGET) GetProcAddress(netapi,TEXT("NetStatisticsGet"));
240 netfree = (NETFREE) GetProcAddress(netapi,TEXT("NetApiBufferFree"));
241 }
242
243 if (netstatget && netfree)
244 {
245 LPBYTE outbuf;
246 /* NetStatisticsGet() is a Unicode only function
247 * STAT_WORKSTATION_0 contains 45 fields and STAT_SERVER_0
248 * contains 17 fields. We treat each field as a source of
249 * one byte of entropy.
250 */
251
252 if (netstatget(NULL, L"LanmanWorkstation", 0, 0, &outbuf) == 0)
253 {
254 RAND_add(outbuf, sizeof(STAT_WORKSTATION_0), 45);
255 netfree(outbuf);
256 }
257 if (netstatget(NULL, L"LanmanServer", 0, 0, &outbuf) == 0)
258 {
259 RAND_add(outbuf, sizeof(STAT_SERVER_0), 17);
260 netfree(outbuf);
261 }
262 }
263
264 if (netapi)
265 FreeLibrary(netapi);
266#endif /* 1 */
267#endif /* !OPENSSL_SYS_WINCE */
268
269#ifndef OPENSSL_SYS_WINCE
270 /* It appears like this can cause an exception deep within ADVAPI32.DLL
271 * at random times on Windows 2000. Reported by Jeffrey Altman.
272 * Only use it on NT.
273 */
274 /* Wolfgang Marczy <WMarczy@topcall.co.at> reports that
275 * the RegQueryValueEx call below can hang on NT4.0 (SP6).
276 * So we don't use this at all for now. */
277#if 0
278 if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
279 osverinfo.dwMajorVersion < 5)
280 {
281 /* Read Performance Statistics from NT/2000 registry
282 * The size of the performance data can vary from call
283 * to call so we must guess the size of the buffer to use
284 * and increase its size if we get an ERROR_MORE_DATA
285 * return instead of ERROR_SUCCESS.
286 */
287 LONG rc=ERROR_MORE_DATA;
288 char * buf=NULL;
289 DWORD bufsz=0;
290 DWORD length;
291
292 while (rc == ERROR_MORE_DATA)
293 {
294 buf = realloc(buf,bufsz+8192);
295 if (!buf)
296 break;
297 bufsz += 8192;
298
299 length = bufsz;
300 rc = RegQueryValueEx(HKEY_PERFORMANCE_DATA, TEXT("Global"),
301 NULL, NULL, buf, &length);
302 }
303 if (rc == ERROR_SUCCESS)
304 {
305 /* For entropy count assume only least significant
306 * byte of each DWORD is random.
307 */
308 RAND_add(&length, sizeof(length), 0);
309 RAND_add(buf, length, length / 4.0);
310
311 /* Close the Registry Key to allow Windows to cleanup/close
312 * the open handle
313 * Note: The 'HKEY_PERFORMANCE_DATA' key is implicitly opened
314 * when the RegQueryValueEx above is done. However, if
315 * it is not explicitly closed, it can cause disk
316 * partition manipulation problems.
317 */
318 RegCloseKey(HKEY_PERFORMANCE_DATA);
319 }
320 if (buf)
321 free(buf);
322 }
323#endif
324#endif /* !OPENSSL_SYS_WINCE */
325
326 if (advapi)
327 {
328 acquire = (CRYPTACQUIRECONTEXT) GetProcAddress(advapi,
329 TEXT("CryptAcquireContextA"));
330 gen = (CRYPTGENRANDOM) GetProcAddress(advapi,
331 TEXT("CryptGenRandom"));
332 release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
333 TEXT("CryptReleaseContext"));
334 }
335
336 if (acquire && gen && release)
337 {
338 /* poll the CryptoAPI PRNG */
339 /* The CryptoAPI returns sizeof(buf) bytes of randomness */
340 if (acquire(&hProvider, 0, 0, PROV_RSA_FULL,
341 CRYPT_VERIFYCONTEXT))
342 {
343 if (gen(hProvider, sizeof(buf), buf) != 0)
344 {
345 RAND_add(buf, sizeof(buf), 0);
346#if 0
347 printf("randomness from PROV_RSA_FULL\n");
348#endif
349 }
350 release(hProvider, 0);
351 }
352
353 /* poll the Pentium PRG with CryptoAPI */
354 if (acquire(&hProvider, 0, INTEL_DEF_PROV, PROV_INTEL_SEC, 0))
355 {
356 if (gen(hProvider, sizeof(buf), buf) != 0)
357 {
358 RAND_add(buf, sizeof(buf), sizeof(buf));
359#if 0
360 printf("randomness from PROV_INTEL_SEC\n");
361#endif
362 }
363 release(hProvider, 0);
364 }
365 }
366
367 if (advapi)
368 FreeLibrary(advapi);
369
370 /* timer data */
371 readtimer();
372
373 /* memory usage statistics */
374 GlobalMemoryStatus(&m);
375 RAND_add(&m, sizeof(m), 1);
376
377 /* process ID */
378 w = GetCurrentProcessId();
379 RAND_add(&w, sizeof(w), 1);
380
381 if (user)
382 {
383 GETCURSORINFO cursor;
384 GETFOREGROUNDWINDOW win;
385 GETQUEUESTATUS queue;
386
387 win = (GETFOREGROUNDWINDOW) GetProcAddress(user, TEXT("GetForegroundWindow"));
388 cursor = (GETCURSORINFO) GetProcAddress(user, TEXT("GetCursorInfo"));
389 queue = (GETQUEUESTATUS) GetProcAddress(user, TEXT("GetQueueStatus"));
390
391 if (win)
392 {
393 /* window handle */
394 h = win();
395 RAND_add(&h, sizeof(h), 0);
396 }
397 if (cursor)
398 {
399 /* unfortunately, its not safe to call GetCursorInfo()
400 * on NT4 even though it exists in SP3 (or SP6) and
401 * higher.
402 */
403 if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
404 osverinfo.dwMajorVersion < 5)
405 cursor = 0;
406 }
407 if (cursor)
408 {
409 /* cursor position */
410 /* assume 2 bytes of entropy */
411 CURSORINFO ci;
412 ci.cbSize = sizeof(CURSORINFO);
413 if (cursor(&ci))
414 RAND_add(&ci, ci.cbSize, 2);
415 }
416
417 if (queue)
418 {
419 /* message queue status */
420 /* assume 1 byte of entropy */
421 w = queue(QS_ALLEVENTS);
422 RAND_add(&w, sizeof(w), 1);
423 }
424
425 FreeLibrary(user);
426 }
427
428 /* Toolhelp32 snapshot: enumerate processes, threads, modules and heap
429 * http://msdn.microsoft.com/library/psdk/winbase/toolhelp_5pfd.htm
430 * (Win 9x and 2000 only, not available on NT)
431 *
432 * This seeding method was proposed in Peter Gutmann, Software
433 * Generation of Practically Strong Random Numbers,
434 * http://www.usenix.org/publications/library/proceedings/sec98/gutmann.html
435 * revised version at http://www.cryptoengines.com/~peter/06_random.pdf
436 * (The assignment of entropy estimates below is arbitrary, but based
437 * on Peter's analysis the full poll appears to be safe. Additional
438 * interactive seeding is encouraged.)
439 */
440
441 if (kernel)
442 {
443 CREATETOOLHELP32SNAPSHOT snap;
444 CLOSETOOLHELP32SNAPSHOT close_snap;
445 HANDLE handle;
446
447 HEAP32FIRST heap_first;
448 HEAP32NEXT heap_next;
449 HEAP32LIST heaplist_first, heaplist_next;
450 PROCESS32 process_first, process_next;
451 THREAD32 thread_first, thread_next;
452 MODULE32 module_first, module_next;
453
454 HEAPLIST32 hlist;
455 HEAPENTRY32 hentry;
456 PROCESSENTRY32 p;
457 THREADENTRY32 t;
458 MODULEENTRY32 m;
459
460 snap = (CREATETOOLHELP32SNAPSHOT)
461 GetProcAddress(kernel, TEXT("CreateToolhelp32Snapshot"));
462 close_snap = (CLOSETOOLHELP32SNAPSHOT)
463 GetProcAddress(kernel, TEXT("CloseToolhelp32Snapshot"));
464 heap_first = (HEAP32FIRST) GetProcAddress(kernel, TEXT("Heap32First"));
465 heap_next = (HEAP32NEXT) GetProcAddress(kernel, TEXT("Heap32Next"));
466 heaplist_first = (HEAP32LIST) GetProcAddress(kernel, TEXT("Heap32ListFirst"));
467 heaplist_next = (HEAP32LIST) GetProcAddress(kernel, TEXT("Heap32ListNext"));
468 process_first = (PROCESS32) GetProcAddress(kernel, TEXT("Process32First"));
469 process_next = (PROCESS32) GetProcAddress(kernel, TEXT("Process32Next"));
470 thread_first = (THREAD32) GetProcAddress(kernel, TEXT("Thread32First"));
471 thread_next = (THREAD32) GetProcAddress(kernel, TEXT("Thread32Next"));
472 module_first = (MODULE32) GetProcAddress(kernel, TEXT("Module32First"));
473 module_next = (MODULE32) GetProcAddress(kernel, TEXT("Module32Next"));
474
475 if (snap && heap_first && heap_next && heaplist_first &&
476 heaplist_next && process_first && process_next &&
477 thread_first && thread_next && module_first &&
478 module_next && (handle = snap(TH32CS_SNAPALL,0))
479 != INVALID_HANDLE_VALUE)
480 {
481 /* heap list and heap walking */
482 /* HEAPLIST32 contains 3 fields that will change with
483 * each entry. Consider each field a source of 1 byte
484 * of entropy.
485 * HEAPENTRY32 contains 5 fields that will change with
486 * each entry. Consider each field a source of 1 byte
487 * of entropy.
488 */
489 hlist.dwSize = sizeof(HEAPLIST32);
490 if (heaplist_first(handle, &hlist))
491 do
492 {
493 RAND_add(&hlist, hlist.dwSize, 3);
494 hentry.dwSize = sizeof(HEAPENTRY32);
495 if (heap_first(&hentry,
496 hlist.th32ProcessID,
497 hlist.th32HeapID))
498 {
499 int entrycnt = 80;
500 do
501 RAND_add(&hentry,
502 hentry.dwSize, 5);
503 while (heap_next(&hentry)
504 && --entrycnt > 0);
505 }
506 } while (heaplist_next(handle,
507 &hlist));
508
509 /* process walking */
510 /* PROCESSENTRY32 contains 9 fields that will change
511 * with each entry. Consider each field a source of
512 * 1 byte of entropy.
513 */
514 p.dwSize = sizeof(PROCESSENTRY32);
515 if (process_first(handle, &p))
516 do
517 RAND_add(&p, p.dwSize, 9);
518 while (process_next(handle, &p));
519
520 /* thread walking */
521 /* THREADENTRY32 contains 6 fields that will change
522 * with each entry. Consider each field a source of
523 * 1 byte of entropy.
524 */
525 t.dwSize = sizeof(THREADENTRY32);
526 if (thread_first(handle, &t))
527 do
528 RAND_add(&t, t.dwSize, 6);
529 while (thread_next(handle, &t));
530
531 /* module walking */
532 /* MODULEENTRY32 contains 9 fields that will change
533 * with each entry. Consider each field a source of
534 * 1 byte of entropy.
535 */
536 m.dwSize = sizeof(MODULEENTRY32);
537 if (module_first(handle, &m))
538 do
539 RAND_add(&m, m.dwSize, 9);
540 while (module_next(handle, &m));
541 if (close_snap)
542 close_snap(handle);
543 else
544 CloseHandle(handle);
545 }
546
547 FreeLibrary(kernel);
548 }
549
550#if 0
551 printf("Exiting RAND_poll\n");
552#endif
553
554 return(1);
555}
556
557int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
558 {
559 double add_entropy=0;
560
561 switch (iMsg)
562 {
563 case WM_KEYDOWN:
564 {
565 static WPARAM key;
566 if (key != wParam)
567 add_entropy = 0.05;
568 key = wParam;
569 }
570 break;
571 case WM_MOUSEMOVE:
572 {
573 static int lastx,lasty,lastdx,lastdy;
574 int x,y,dx,dy;
575
576 x=LOWORD(lParam);
577 y=HIWORD(lParam);
578 dx=lastx-x;
579 dy=lasty-y;
580 if (dx != 0 && dy != 0 && dx-lastdx != 0 && dy-lastdy != 0)
581 add_entropy=.2;
582 lastx=x, lasty=y;
583 lastdx=dx, lastdy=dy;
584 }
585 break;
586 }
587
588 readtimer();
589 RAND_add(&iMsg, sizeof(iMsg), add_entropy);
590 RAND_add(&wParam, sizeof(wParam), 0);
591 RAND_add(&lParam, sizeof(lParam), 0);
592
593 return (RAND_status());
594 }
595
596
597void RAND_screen(void) /* function available for backward compatibility */
598{
599 RAND_poll();
600 readscreen();
601}
602
603
604/* feed timing information to the PRNG */
605static void readtimer(void)
606{
607 DWORD w;
608 LARGE_INTEGER l;
609 static int have_perfc = 1;
610#if defined(_MSC_VER) && !defined(OPENSSL_SYS_WINCE)
611 static int have_tsc = 1;
612 DWORD cyclecount;
613
614 if (have_tsc) {
615 __try {
616 __asm {
617 _emit 0x0f
618 _emit 0x31
619 mov cyclecount, eax
620 }
621 RAND_add(&cyclecount, sizeof(cyclecount), 1);
622 } __except(EXCEPTION_EXECUTE_HANDLER) {
623 have_tsc = 0;
624 }
625 }
626#else
627# define have_tsc 0
628#endif
629
630 if (have_perfc) {
631 if (QueryPerformanceCounter(&l) == 0)
632 have_perfc = 0;
633 else
634 RAND_add(&l, sizeof(l), 0);
635 }
636
637 if (!have_tsc && !have_perfc) {
638 w = GetTickCount();
639 RAND_add(&w, sizeof(w), 0);
640 }
641}
642
643/* feed screen contents to PRNG */
644/*****************************************************************************
645 *
646 * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
647 *
648 * Code adapted from
649 * <URL:http://support.microsoft.com/default.aspx?scid=kb;[LN];97193>;
650 * the original copyright message is:
651 *
652 * (C) Copyright Microsoft Corp. 1993. All rights reserved.
653 *
654 * You have a royalty-free right to use, modify, reproduce and
655 * distribute the Sample Files (and/or any modified version) in
656 * any way you find useful, provided that you agree that
657 * Microsoft has no warranty obligations or liability for any
658 * Sample Application Files which are modified.
659 */
660
661static void readscreen(void)
662{
663#ifndef OPENSSL_SYS_WINCE
664 HDC hScrDC; /* screen DC */
665 HDC hMemDC; /* memory DC */
666 HBITMAP hBitmap; /* handle for our bitmap */
667 HBITMAP hOldBitmap; /* handle for previous bitmap */
668 BITMAP bm; /* bitmap properties */
669 unsigned int size; /* size of bitmap */
670 char *bmbits; /* contents of bitmap */
671 int w; /* screen width */
672 int h; /* screen height */
673 int y; /* y-coordinate of screen lines to grab */
674 int n = 16; /* number of screen lines to grab at a time */
675
676 /* Create a screen DC and a memory DC compatible to screen DC */
677 hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
678 hMemDC = CreateCompatibleDC(hScrDC);
679
680 /* Get screen resolution */
681 w = GetDeviceCaps(hScrDC, HORZRES);
682 h = GetDeviceCaps(hScrDC, VERTRES);
683
684 /* Create a bitmap compatible with the screen DC */
685 hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
686
687 /* Select new bitmap into memory DC */
688 hOldBitmap = SelectObject(hMemDC, hBitmap);
689
690 /* Get bitmap properties */
691 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
692 size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
693
694 bmbits = OPENSSL_malloc(size);
695 if (bmbits) {
696 /* Now go through the whole screen, repeatedly grabbing n lines */
697 for (y = 0; y < h-n; y += n)
698 {
699 unsigned char md[MD_DIGEST_LENGTH];
700
701 /* Bitblt screen DC to memory DC */
702 BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
703
704 /* Copy bitmap bits from memory DC to bmbits */
705 GetBitmapBits(hBitmap, size, bmbits);
706
707 /* Get the hash of the bitmap */
708 MD(bmbits,size,md);
709
710 /* Seed the random generator with the hash value */
711 RAND_add(md, MD_DIGEST_LENGTH, 0);
712 }
713
714 OPENSSL_free(bmbits);
715 }
716
717 /* Select old bitmap back into memory DC */
718 hBitmap = SelectObject(hMemDC, hOldBitmap);
719
720 /* Clean up */
721 DeleteObject(hBitmap);
722 DeleteDC(hMemDC);
723 DeleteDC(hScrDC);
724#endif /* !OPENSSL_SYS_WINCE */
725}
726
727#endif
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c
index c7fba496a8..d88ee0d780 100644
--- a/src/lib/libcrypto/rand/randfile.c
+++ b/src/lib/libcrypto/rand/randfile.c
@@ -166,7 +166,6 @@ int RAND_write_file(const char *file)
166 } 166 }
167 167
168#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) 168#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
169 {
170 /* For some reason Win32 can't write to files created this way */ 169 /* For some reason Win32 can't write to files created this way */
171 170
172 /* chmod(..., 0600) is too late to protect the file, 171 /* chmod(..., 0600) is too late to protect the file,
@@ -174,7 +173,6 @@ int RAND_write_file(const char *file)
174 int fd = open(file, O_CREAT, 0600); 173 int fd = open(file, O_CREAT, 0600);
175 if (fd != -1) 174 if (fd != -1)
176 out = fdopen(fd, "wb"); 175 out = fdopen(fd, "wb");
177 }
178#endif 176#endif
179 if (out == NULL) 177 if (out == NULL)
180 out = fopen(file,"wb"); 178 out = fopen(file,"wb");
@@ -233,7 +231,7 @@ const char *RAND_file_name(char *buf, size_t size)
233 struct stat sb; 231 struct stat sb;
234#endif 232#endif
235 233
236 if (OPENSSL_issetugid() == 0) 234 if (issetugid() == 0)
237 s=getenv("RANDFILE"); 235 s=getenv("RANDFILE");
238 if (s != NULL && *s && strlen(s) + 1 < size) 236 if (s != NULL && *s && strlen(s) + 1 < size)
239 { 237 {
@@ -242,7 +240,7 @@ const char *RAND_file_name(char *buf, size_t size)
242 } 240 }
243 else 241 else
244 { 242 {
245 if (OPENSSL_issetugid() == 0) 243 if (issetugid() == 0)
246 s=getenv("HOME"); 244 s=getenv("HOME");
247#ifdef DEFAULT_HOME 245#ifdef DEFAULT_HOME
248 if (s == NULL) 246 if (s == NULL)
diff --git a/src/lib/libcrypto/rand/randtest.c b/src/lib/libcrypto/rand/randtest.c
new file mode 100644
index 0000000000..701932e6ee
--- /dev/null
+++ b/src/lib/libcrypto/rand/randtest.c
@@ -0,0 +1,216 @@
1/* crypto/rand/randtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <openssl/rand.h>
62
63#include "../e_os.h"
64
65/* some FIPS 140-1 random number test */
66/* some simple tests */
67
68int main()
69 {
70 unsigned char buf[2500];
71 int i,j,k,s,sign,nsign,err=0;
72 unsigned long n1;
73 unsigned long n2[16];
74 unsigned long runs[2][34];
75 /*double d; */
76 long d;
77
78 i = RAND_pseudo_bytes(buf,2500);
79 if (i < 0)
80 {
81 printf ("init failed, the rand method is not properly installed\n");
82 err++;
83 goto err;
84 }
85
86 n1=0;
87 for (i=0; i<16; i++) n2[i]=0;
88 for (i=0; i<34; i++) runs[0][i]=runs[1][i]=0;
89
90 /* test 1 and 2 */
91 sign=0;
92 nsign=0;
93 for (i=0; i<2500; i++)
94 {
95 j=buf[i];
96
97 n2[j&0x0f]++;
98 n2[(j>>4)&0x0f]++;
99
100 for (k=0; k<8; k++)
101 {
102 s=(j&0x01);
103 if (s == sign)
104 nsign++;
105 else
106 {
107 if (nsign > 34) nsign=34;
108 if (nsign != 0)
109 {
110 runs[sign][nsign-1]++;
111 if (nsign > 6)
112 runs[sign][5]++;
113 }
114 sign=s;
115 nsign=1;
116 }
117
118 if (s) n1++;
119 j>>=1;
120 }
121 }
122 if (nsign > 34) nsign=34;
123 if (nsign != 0) runs[sign][nsign-1]++;
124
125 /* test 1 */
126 if (!((9654 < n1) && (n1 < 10346)))
127 {
128 printf("test 1 failed, X=%lu\n",n1);
129 err++;
130 }
131 printf("test 1 done\n");
132
133 /* test 2 */
134#ifdef undef
135 d=0;
136 for (i=0; i<16; i++)
137 d+=n2[i]*n2[i];
138 d=d*16.0/5000.0-5000.0;
139 if (!((1.03 < d) && (d < 57.4)))
140 {
141 printf("test 2 failed, X=%.2f\n",d);
142 err++;
143 }
144#endif
145 d=0;
146 for (i=0; i<16; i++)
147 d+=n2[i]*n2[i];
148 d=(d*8)/25-500000;
149 if (!((103 < d) && (d < 5740)))
150 {
151 printf("test 2 failed, X=%ld.%02ld\n",d/100L,d%100L);
152 err++;
153 }
154 printf("test 2 done\n");
155
156 /* test 3 */
157 for (i=0; i<2; i++)
158 {
159 if (!((2267 < runs[i][0]) && (runs[i][0] < 2733)))
160 {
161 printf("test 3 failed, bit=%d run=%d num=%lu\n",
162 i,1,runs[i][0]);
163 err++;
164 }
165 if (!((1079 < runs[i][1]) && (runs[i][1] < 1421)))
166 {
167 printf("test 3 failed, bit=%d run=%d num=%lu\n",
168 i,2,runs[i][1]);
169 err++;
170 }
171 if (!(( 502 < runs[i][2]) && (runs[i][2] < 748)))
172 {
173 printf("test 3 failed, bit=%d run=%d num=%lu\n",
174 i,3,runs[i][2]);
175 err++;
176 }
177 if (!(( 223 < runs[i][3]) && (runs[i][3] < 402)))
178 {
179 printf("test 3 failed, bit=%d run=%d num=%lu\n",
180 i,4,runs[i][3]);
181 err++;
182 }
183 if (!(( 90 < runs[i][4]) && (runs[i][4] < 223)))
184 {
185 printf("test 3 failed, bit=%d run=%d num=%lu\n",
186 i,5,runs[i][4]);
187 err++;
188 }
189 if (!(( 90 < runs[i][5]) && (runs[i][5] < 223)))
190 {
191 printf("test 3 failed, bit=%d run=%d num=%lu\n",
192 i,6,runs[i][5]);
193 err++;
194 }
195 }
196 printf("test 3 done\n");
197
198 /* test 4 */
199 if (runs[0][33] != 0)
200 {
201 printf("test 4 failed, bit=%d run=%d num=%lu\n",
202 0,34,runs[0][33]);
203 err++;
204 }
205 if (runs[1][33] != 0)
206 {
207 printf("test 4 failed, bit=%d run=%d num=%lu\n",
208 1,34,runs[1][33]);
209 err++;
210 }
211 printf("test 4 done\n");
212 err:
213 err=((err)?1:0);
214 EXIT(err);
215 return(err);
216 }