]>
Commit | Line | Data |
---|---|---|
2ace287d BM |
1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | * All rights reserved. | |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
2ace287d BM |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
2ace287d BM |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
2ace287d BM |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
2ace287d BM |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
2ace287d BM |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
2ace287d BM |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | /* ==================================================================== | |
58 | * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. | |
59 | * | |
60 | * Redistribution and use in source and binary forms, with or without | |
61 | * modification, are permitted provided that the following conditions | |
62 | * are met: | |
63 | * | |
64 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 65 | * notice, this list of conditions and the following disclaimer. |
2ace287d BM |
66 | * |
67 | * 2. Redistributions in binary form must reproduce the above copyright | |
68 | * notice, this list of conditions and the following disclaimer in | |
69 | * the documentation and/or other materials provided with the | |
70 | * distribution. | |
71 | * | |
72 | * 3. All advertising materials mentioning features or use of this | |
73 | * software must display the following acknowledgment: | |
74 | * "This product includes software developed by the OpenSSL Project | |
75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
76 | * | |
77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
78 | * endorse or promote products derived from this software without | |
79 | * prior written permission. For written permission, please contact | |
80 | * openssl-core@openssl.org. | |
81 | * | |
82 | * 5. Products derived from this software may not be called "OpenSSL" | |
83 | * nor may "OpenSSL" appear in their names without prior written | |
84 | * permission of the OpenSSL Project. | |
85 | * | |
86 | * 6. Redistributions of any form whatsoever must retain the following | |
87 | * acknowledgment: | |
88 | * "This product includes software developed by the OpenSSL Project | |
89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
90 | * | |
91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
102 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
103 | * ==================================================================== | |
104 | * | |
105 | * This product includes cryptographic software written by Eric Young | |
106 | * (eay@cryptsoft.com). This product includes software written by Tim | |
107 | * Hudson (tjh@cryptsoft.com). | |
108 | * | |
109 | */ | |
110 | ||
b39fc560 | 111 | #include "internal/cryptlib.h" |
c0722725 UM |
112 | #include <openssl/rand.h> |
113 | #include "rand_lcl.h" | |
2ace287d | 114 | |
7a8c7288 | 115 | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) |
0f113f3e MC |
116 | # include <windows.h> |
117 | # ifndef _WIN32_WINNT | |
118 | # define _WIN32_WINNT 0x0400 | |
119 | # endif | |
120 | # include <wincrypt.h> | |
121 | # include <tlhelp32.h> | |
c0722725 | 122 | |
0f113f3e MC |
123 | /* |
124 | * Limit the time spent walking through the heap, processes, threads and | |
125 | * modules to a maximum of 1000 miliseconds each, unless CryptoGenRandom | |
126 | * failed | |
127 | */ | |
128 | # define MAXDELAY 1000 | |
7a8c7288 | 129 | |
0f113f3e MC |
130 | /* |
131 | * Intel hardware RNG CSP -- available from | |
c0722725 UM |
132 | * http://developer.intel.com/design/security/rng/redist_license.htm |
133 | */ | |
0f113f3e MC |
134 | # define PROV_INTEL_SEC 22 |
135 | # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider" | |
c0722725 UM |
136 | |
137 | static void readtimer(void); | |
138 | static void readscreen(void); | |
139 | ||
0f113f3e MC |
140 | /* |
141 | * It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined | |
142 | * when WINVER is 0x0500 and up, which currently only happens on Win2000. | |
143 | * Unfortunately, those are typedefs, so they're a little bit difficult to | |
144 | * detect properly. On the other hand, the macro CURSOR_SHOWING is defined | |
145 | * within the same conditional, so it can be use to detect the absence of | |
146 | * said typedefs. | |
147 | */ | |
7abee0ce | 148 | |
0f113f3e | 149 | # ifndef CURSOR_SHOWING |
7abee0ce RL |
150 | /* |
151 | * Information about the global cursor. | |
152 | */ | |
0f113f3e MC |
153 | typedef struct tagCURSORINFO { |
154 | DWORD cbSize; | |
155 | DWORD flags; | |
7abee0ce | 156 | HCURSOR hCursor; |
0f113f3e | 157 | POINT ptScreenPos; |
7abee0ce RL |
158 | } CURSORINFO, *PCURSORINFO, *LPCURSORINFO; |
159 | ||
0f113f3e MC |
160 | # define CURSOR_SHOWING 0x00000001 |
161 | # endif /* CURSOR_SHOWING */ | |
162 | ||
163 | # if !defined(OPENSSL_SYS_WINCE) | |
164 | typedef BOOL(WINAPI *CRYPTACQUIRECONTEXTW) (HCRYPTPROV *, LPCWSTR, LPCWSTR, | |
165 | DWORD, DWORD); | |
166 | typedef BOOL(WINAPI *CRYPTGENRANDOM) (HCRYPTPROV, DWORD, BYTE *); | |
167 | typedef BOOL(WINAPI *CRYPTRELEASECONTEXT) (HCRYPTPROV, DWORD); | |
168 | ||
169 | typedef HWND(WINAPI *GETFOREGROUNDWINDOW) (VOID); | |
170 | typedef BOOL(WINAPI *GETCURSORINFO) (PCURSORINFO); | |
171 | typedef DWORD(WINAPI *GETQUEUESTATUS) (UINT); | |
172 | ||
173 | typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD); | |
174 | typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE); | |
175 | typedef BOOL(WINAPI *HEAP32FIRST) (LPHEAPENTRY32, DWORD, size_t); | |
176 | typedef BOOL(WINAPI *HEAP32NEXT) (LPHEAPENTRY32); | |
177 | typedef BOOL(WINAPI *HEAP32LIST) (HANDLE, LPHEAPLIST32); | |
178 | typedef BOOL(WINAPI *PROCESS32) (HANDLE, LPPROCESSENTRY32); | |
179 | typedef BOOL(WINAPI *THREAD32) (HANDLE, LPTHREADENTRY32); | |
180 | typedef BOOL(WINAPI *MODULE32) (HANDLE, LPMODULEENTRY32); | |
181 | ||
182 | # include <lmcons.h> | |
183 | # include <lmstats.h> | |
0f113f3e MC |
184 | /* |
185 | * The NET API is Unicode only. It requires the use of the UNICODE macro. | |
186 | * When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was was added to the | |
187 | * Platform SDK to allow the NET API to be used in non-Unicode applications | |
188 | * provided that Unicode strings were still used for input. LMSTR is defined | |
189 | * as LPWSTR. | |
dbd87ffc | 190 | */ |
0f113f3e MC |
191 | typedef NET_API_STATUS(NET_API_FUNCTION *NETSTATGET) |
192 | (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE *); | |
193 | typedef NET_API_STATUS(NET_API_FUNCTION *NETFREE) (LPBYTE); | |
0f113f3e | 194 | # endif /* !OPENSSL_SYS_WINCE */ |
1d95fb65 | 195 | |
c0722725 UM |
196 | int RAND_poll(void) |
197 | { | |
db40a14e | 198 | MEMORYSTATUS mst; |
0f113f3e MC |
199 | HCRYPTPROV hProvider = 0; |
200 | DWORD w; | |
201 | int good = 0; | |
202 | ||
203 | # if defined(OPENSSL_SYS_WINCE) | |
204 | # if defined(_WIN32_WCE) && _WIN32_WCE>=300 | |
205 | /* | |
206 | * Even though MSDN says _WIN32_WCE>=210, it doesn't seem to be available | |
207 | * in commonly available implementations prior 300... | |
208 | */ | |
209 | { | |
210 | BYTE buf[64]; | |
211 | /* poll the CryptoAPI PRNG */ | |
212 | /* The CryptoAPI returns sizeof(buf) bytes of randomness */ | |
213 | if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, | |
214 | CRYPT_VERIFYCONTEXT)) { | |
215 | if (CryptGenRandom(hProvider, sizeof(buf), buf)) | |
216 | RAND_add(buf, sizeof(buf), sizeof(buf)); | |
217 | CryptReleaseContext(hProvider, 0); | |
218 | } | |
219 | } | |
220 | # endif | |
221 | # else /* OPENSSL_SYS_WINCE */ | |
222 | /* | |
223 | * None of below libraries are present on Windows CE, which is | |
224 | * why we #ifndef the whole section. This also excuses us from | |
225 | * handling the GetProcAddress issue. The trouble is that in | |
226 | * real Win32 API GetProcAddress is available in ANSI flavor | |
227 | * only. In WinCE on the other hand GetProcAddress is a macro | |
228 | * most commonly defined as GetProcAddressW, which accepts | |
229 | * Unicode argument. If we were to call GetProcAddress under | |
230 | * WinCE, I'd recommend to either redefine GetProcAddress as | |
231 | * GetProcAddressA (there seem to be one in common CE spec) or | |
232 | * implement own shim routine, which would accept ANSI argument | |
233 | * and expand it to Unicode. | |
234 | */ | |
235 | { | |
236 | /* load functions dynamically - not available on all systems */ | |
237 | HMODULE advapi = LoadLibrary(TEXT("ADVAPI32.DLL")); | |
238 | HMODULE kernel = LoadLibrary(TEXT("KERNEL32.DLL")); | |
239 | HMODULE user = NULL; | |
240 | HMODULE netapi = LoadLibrary(TEXT("NETAPI32.DLL")); | |
241 | CRYPTACQUIRECONTEXTW acquire = NULL; | |
242 | CRYPTGENRANDOM gen = NULL; | |
243 | CRYPTRELEASECONTEXT release = NULL; | |
244 | NETSTATGET netstatget = NULL; | |
245 | NETFREE netfree = NULL; | |
246 | BYTE buf[64]; | |
247 | ||
248 | if (netapi) { | |
249 | netstatget = | |
250 | (NETSTATGET) GetProcAddress(netapi, "NetStatisticsGet"); | |
251 | netfree = (NETFREE) GetProcAddress(netapi, "NetApiBufferFree"); | |
252 | } | |
253 | ||
254 | if (netstatget && netfree) { | |
255 | LPBYTE outbuf; | |
256 | /* | |
257 | * NetStatisticsGet() is a Unicode only function | |
258 | * STAT_WORKSTATION_0 contains 45 fields and STAT_SERVER_0 | |
259 | * contains 17 fields. We treat each field as a source of one | |
260 | * byte of entropy. | |
261 | */ | |
262 | ||
263 | if (netstatget(NULL, L"LanmanWorkstation", 0, 0, &outbuf) == 0) { | |
264 | RAND_add(outbuf, sizeof(STAT_WORKSTATION_0), 45); | |
265 | netfree(outbuf); | |
266 | } | |
267 | if (netstatget(NULL, L"LanmanServer", 0, 0, &outbuf) == 0) { | |
268 | RAND_add(outbuf, sizeof(STAT_SERVER_0), 17); | |
269 | netfree(outbuf); | |
270 | } | |
271 | } | |
272 | ||
273 | if (netapi) | |
274 | FreeLibrary(netapi); | |
275 | ||
276 | /* | |
277 | * It appears like this can cause an exception deep within | |
278 | * ADVAPI32.DLL at random times on Windows 2000. Reported by Jeffrey | |
279 | * Altman. Only use it on NT. | |
280 | */ | |
281 | ||
282 | if (advapi) { | |
283 | /* | |
284 | * If it's available, then it's available in both ANSI | |
285 | * and UNICODE flavors even in Win9x, documentation says. | |
286 | * We favor Unicode... | |
287 | */ | |
288 | acquire = (CRYPTACQUIRECONTEXTW) GetProcAddress(advapi, | |
289 | "CryptAcquireContextW"); | |
290 | gen = (CRYPTGENRANDOM) GetProcAddress(advapi, "CryptGenRandom"); | |
291 | release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi, | |
292 | "CryptReleaseContext"); | |
293 | } | |
294 | ||
295 | if (acquire && gen && release) { | |
296 | /* poll the CryptoAPI PRNG */ | |
297 | /* The CryptoAPI returns sizeof(buf) bytes of randomness */ | |
298 | if (acquire(&hProvider, NULL, NULL, PROV_RSA_FULL, | |
299 | CRYPT_VERIFYCONTEXT)) { | |
300 | if (gen(hProvider, sizeof(buf), buf) != 0) { | |
301 | RAND_add(buf, sizeof(buf), 0); | |
302 | good = 1; | |
0f113f3e MC |
303 | } |
304 | release(hProvider, 0); | |
305 | } | |
306 | ||
307 | /* poll the Pentium PRG with CryptoAPI */ | |
308 | if (acquire(&hProvider, 0, INTEL_DEF_PROV, PROV_INTEL_SEC, 0)) { | |
309 | if (gen(hProvider, sizeof(buf), buf) != 0) { | |
310 | RAND_add(buf, sizeof(buf), sizeof(buf)); | |
311 | good = 1; | |
0f113f3e MC |
312 | } |
313 | release(hProvider, 0); | |
314 | } | |
315 | } | |
c0722725 | 316 | |
362f2b69 | 317 | if (advapi) |
0f113f3e MC |
318 | FreeLibrary(advapi); |
319 | ||
320 | if ((!check_winnt() || | |
321 | !OPENSSL_isservice()) && | |
322 | (user = LoadLibrary(TEXT("USER32.DLL")))) { | |
323 | GETCURSORINFO cursor; | |
324 | GETFOREGROUNDWINDOW win; | |
325 | GETQUEUESTATUS queue; | |
326 | ||
327 | win = | |
328 | (GETFOREGROUNDWINDOW) GetProcAddress(user, | |
329 | "GetForegroundWindow"); | |
330 | cursor = (GETCURSORINFO) GetProcAddress(user, "GetCursorInfo"); | |
331 | queue = (GETQUEUESTATUS) GetProcAddress(user, "GetQueueStatus"); | |
332 | ||
333 | if (win) { | |
334 | /* window handle */ | |
335 | HWND h = win(); | |
336 | RAND_add(&h, sizeof(h), 0); | |
337 | } | |
338 | if (cursor) { | |
339 | /* | |
340 | * unfortunately, its not safe to call GetCursorInfo() on NT4 | |
341 | * even though it exists in SP3 (or SP6) and higher. | |
342 | */ | |
343 | if (check_winnt() && !check_win_minplat(5)) | |
344 | cursor = 0; | |
345 | } | |
346 | if (cursor) { | |
347 | /* cursor position */ | |
348 | /* assume 2 bytes of entropy */ | |
349 | CURSORINFO ci; | |
350 | ci.cbSize = sizeof(CURSORINFO); | |
351 | if (cursor(&ci)) | |
352 | RAND_add(&ci, ci.cbSize, 2); | |
353 | } | |
354 | ||
355 | if (queue) { | |
356 | /* message queue status */ | |
357 | /* assume 1 byte of entropy */ | |
358 | w = queue(QS_ALLEVENTS); | |
359 | RAND_add(&w, sizeof(w), 1); | |
360 | } | |
361 | ||
362 | FreeLibrary(user); | |
363 | } | |
364 | ||
365 | /*- | |
366 | * Toolhelp32 snapshot: enumerate processes, threads, modules and heap | |
367 | * http://msdn.microsoft.com/library/psdk/winbase/toolhelp_5pfd.htm | |
368 | * (Win 9x and 2000 only, not available on NT) | |
369 | * | |
370 | * This seeding method was proposed in Peter Gutmann, Software | |
371 | * Generation of Practically Strong Random Numbers, | |
372 | * http://www.usenix.org/publications/library/proceedings/sec98/gutmann.html | |
373 | * revised version at http://www.cryptoengines.com/~peter/06_random.pdf | |
374 | * (The assignment of entropy estimates below is arbitrary, but based | |
375 | * on Peter's analysis the full poll appears to be safe. Additional | |
376 | * interactive seeding is encouraged.) | |
377 | */ | |
378 | ||
379 | if (kernel) { | |
380 | CREATETOOLHELP32SNAPSHOT snap; | |
381 | CLOSETOOLHELP32SNAPSHOT close_snap; | |
382 | HANDLE handle; | |
383 | ||
384 | HEAP32FIRST heap_first; | |
385 | HEAP32NEXT heap_next; | |
386 | HEAP32LIST heaplist_first, heaplist_next; | |
387 | PROCESS32 process_first, process_next; | |
388 | THREAD32 thread_first, thread_next; | |
389 | MODULE32 module_first, module_next; | |
390 | ||
391 | HEAPLIST32 hlist; | |
392 | HEAPENTRY32 hentry; | |
393 | PROCESSENTRY32 p; | |
394 | THREADENTRY32 t; | |
395 | MODULEENTRY32 m; | |
396 | DWORD starttime = 0; | |
397 | ||
398 | snap = (CREATETOOLHELP32SNAPSHOT) | |
399 | GetProcAddress(kernel, "CreateToolhelp32Snapshot"); | |
400 | close_snap = (CLOSETOOLHELP32SNAPSHOT) | |
401 | GetProcAddress(kernel, "CloseToolhelp32Snapshot"); | |
402 | heap_first = (HEAP32FIRST) GetProcAddress(kernel, "Heap32First"); | |
403 | heap_next = (HEAP32NEXT) GetProcAddress(kernel, "Heap32Next"); | |
404 | heaplist_first = | |
405 | (HEAP32LIST) GetProcAddress(kernel, "Heap32ListFirst"); | |
406 | heaplist_next = | |
407 | (HEAP32LIST) GetProcAddress(kernel, "Heap32ListNext"); | |
408 | process_first = | |
409 | (PROCESS32) GetProcAddress(kernel, "Process32First"); | |
410 | process_next = | |
411 | (PROCESS32) GetProcAddress(kernel, "Process32Next"); | |
412 | thread_first = (THREAD32) GetProcAddress(kernel, "Thread32First"); | |
413 | thread_next = (THREAD32) GetProcAddress(kernel, "Thread32Next"); | |
414 | module_first = (MODULE32) GetProcAddress(kernel, "Module32First"); | |
415 | module_next = (MODULE32) GetProcAddress(kernel, "Module32Next"); | |
416 | ||
417 | if (snap && heap_first && heap_next && heaplist_first && | |
418 | heaplist_next && process_first && process_next && | |
419 | thread_first && thread_next && module_first && | |
420 | module_next && (handle = snap(TH32CS_SNAPALL, 0)) | |
421 | != INVALID_HANDLE_VALUE) { | |
422 | /* heap list and heap walking */ | |
423 | /* | |
424 | * HEAPLIST32 contains 3 fields that will change with each | |
425 | * entry. Consider each field a source of 1 byte of entropy. | |
426 | * HEAPENTRY32 contains 5 fields that will change with each | |
427 | * entry. Consider each field a source of 1 byte of entropy. | |
428 | */ | |
429 | ZeroMemory(&hlist, sizeof(HEAPLIST32)); | |
430 | hlist.dwSize = sizeof(HEAPLIST32); | |
431 | if (good) | |
432 | starttime = GetTickCount(); | |
433 | # ifdef _MSC_VER | |
434 | if (heaplist_first(handle, &hlist)) { | |
435 | /* | |
436 | * following discussion on dev ML, exception on WinCE (or | |
437 | * other Win platform) is theoretically of unknown | |
438 | * origin; prevent infinite loop here when this | |
439 | * theoretical case occurs; otherwise cope with the | |
440 | * expected (MSDN documented) exception-throwing | |
441 | * behaviour of Heap32Next() on WinCE. | |
442 | * | |
443 | * based on patch in original message by Tanguy Fautré | |
444 | * (2009/03/02) Subject: RAND_poll() and | |
445 | * CreateToolhelp32Snapshot() stability | |
446 | */ | |
447 | int ex_cnt_limit = 42; | |
448 | do { | |
449 | RAND_add(&hlist, hlist.dwSize, 3); | |
450 | __try { | |
451 | ZeroMemory(&hentry, sizeof(HEAPENTRY32)); | |
452 | hentry.dwSize = sizeof(HEAPENTRY32); | |
453 | if (heap_first(&hentry, | |
454 | hlist.th32ProcessID, | |
455 | hlist.th32HeapID)) { | |
456 | int entrycnt = 80; | |
457 | do | |
458 | RAND_add(&hentry, hentry.dwSize, 5); | |
459 | while (heap_next(&hentry) | |
460 | && (!good | |
461 | || (GetTickCount() - starttime) < | |
462 | MAXDELAY) | |
463 | && --entrycnt > 0); | |
464 | } | |
2ace287d | 465 | } |
0f113f3e MC |
466 | __except(EXCEPTION_EXECUTE_HANDLER) { |
467 | /* | |
468 | * ignore access violations when walking the heap | |
469 | * list | |
470 | */ | |
471 | ex_cnt_limit--; | |
2ace287d | 472 | } |
0f113f3e MC |
473 | } while (heaplist_next(handle, &hlist) |
474 | && (!good | |
475 | || (GetTickCount() - starttime) < MAXDELAY) | |
476 | && ex_cnt_limit > 0); | |
477 | } | |
478 | # else | |
479 | if (heaplist_first(handle, &hlist)) { | |
480 | do { | |
481 | RAND_add(&hlist, hlist.dwSize, 3); | |
482 | hentry.dwSize = sizeof(HEAPENTRY32); | |
483 | if (heap_first(&hentry, | |
484 | hlist.th32ProcessID, | |
485 | hlist.th32HeapID)) { | |
486 | int entrycnt = 80; | |
487 | do | |
488 | RAND_add(&hentry, hentry.dwSize, 5); | |
489 | while (heap_next(&hentry) | |
490 | && --entrycnt > 0); | |
491 | } | |
492 | } while (heaplist_next(handle, &hlist) | |
493 | && (!good | |
494 | || (GetTickCount() - starttime) < MAXDELAY)); | |
495 | } | |
496 | # endif | |
497 | ||
498 | /* process walking */ | |
499 | /* | |
500 | * PROCESSENTRY32 contains 9 fields that will change with | |
501 | * each entry. Consider each field a source of 1 byte of | |
502 | * entropy. | |
503 | */ | |
504 | p.dwSize = sizeof(PROCESSENTRY32); | |
505 | ||
506 | if (good) | |
507 | starttime = GetTickCount(); | |
508 | if (process_first(handle, &p)) | |
509 | do | |
510 | RAND_add(&p, p.dwSize, 9); | |
511 | while (process_next(handle, &p) | |
512 | && (!good | |
513 | || (GetTickCount() - starttime) < MAXDELAY)); | |
514 | ||
515 | /* thread walking */ | |
516 | /* | |
517 | * THREADENTRY32 contains 6 fields that will change with each | |
518 | * entry. Consider each field a source of 1 byte of entropy. | |
519 | */ | |
520 | t.dwSize = sizeof(THREADENTRY32); | |
521 | if (good) | |
522 | starttime = GetTickCount(); | |
523 | if (thread_first(handle, &t)) | |
524 | do | |
525 | RAND_add(&t, t.dwSize, 6); | |
526 | while (thread_next(handle, &t) | |
527 | && (!good | |
528 | || (GetTickCount() - starttime) < MAXDELAY)); | |
529 | ||
530 | /* module walking */ | |
531 | /* | |
532 | * MODULEENTRY32 contains 9 fields that will change with each | |
533 | * entry. Consider each field a source of 1 byte of entropy. | |
534 | */ | |
535 | m.dwSize = sizeof(MODULEENTRY32); | |
536 | if (good) | |
537 | starttime = GetTickCount(); | |
538 | if (module_first(handle, &m)) | |
539 | do | |
540 | RAND_add(&m, m.dwSize, 9); | |
541 | while (module_next(handle, &m) | |
542 | && (!good | |
543 | || (GetTickCount() - starttime) < MAXDELAY)); | |
544 | if (close_snap) | |
545 | close_snap(handle); | |
546 | else | |
547 | CloseHandle(handle); | |
548 | ||
549 | } | |
550 | ||
551 | FreeLibrary(kernel); | |
552 | } | |
553 | } | |
554 | # endif /* !OPENSSL_SYS_WINCE */ | |
555 | ||
556 | /* timer data */ | |
557 | readtimer(); | |
558 | ||
559 | /* memory usage statistics */ | |
db40a14e AP |
560 | GlobalMemoryStatus(&mst); |
561 | RAND_add(&mst, sizeof(mst), 1); | |
0f113f3e MC |
562 | |
563 | /* process ID */ | |
564 | w = GetCurrentProcessId(); | |
565 | RAND_add(&w, sizeof(w), 1); | |
566 | ||
0f113f3e MC |
567 | return (1); |
568 | } | |
2ace287d | 569 | |
0f113f3e | 570 | int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam) |
c0722725 | 571 | { |
0f113f3e MC |
572 | double add_entropy = 0; |
573 | ||
574 | switch (iMsg) { | |
575 | case WM_KEYDOWN: | |
576 | { | |
577 | static WPARAM key; | |
578 | if (key != wParam) | |
579 | add_entropy = 0.05; | |
580 | key = wParam; | |
581 | } | |
582 | break; | |
583 | case WM_MOUSEMOVE: | |
584 | { | |
585 | static int lastx, lasty, lastdx, lastdy; | |
586 | int x, y, dx, dy; | |
587 | ||
588 | x = LOWORD(lParam); | |
589 | y = HIWORD(lParam); | |
590 | dx = lastx - x; | |
591 | dy = lasty - y; | |
592 | if (dx != 0 && dy != 0 && dx - lastdx != 0 && dy - lastdy != 0) | |
593 | add_entropy = .2; | |
594 | lastx = x, lasty = y; | |
595 | lastdx = dx, lastdy = dy; | |
596 | } | |
597 | break; | |
598 | } | |
599 | ||
600 | readtimer(); | |
601 | RAND_add(&iMsg, sizeof(iMsg), add_entropy); | |
602 | RAND_add(&wParam, sizeof(wParam), 0); | |
603 | RAND_add(&lParam, sizeof(lParam), 0); | |
604 | ||
605 | return (RAND_status()); | |
c0722725 UM |
606 | } |
607 | ||
0f113f3e MC |
608 | void RAND_screen(void) |
609 | { /* function available for backward | |
610 | * compatibility */ | |
611 | RAND_poll(); | |
612 | readscreen(); | |
613 | } | |
c0722725 UM |
614 | |
615 | /* feed timing information to the PRNG */ | |
616 | static void readtimer(void) | |
617 | { | |
0f113f3e MC |
618 | DWORD w; |
619 | LARGE_INTEGER l; | |
620 | static int have_perfc = 1; | |
621 | # if defined(_MSC_VER) && defined(_M_X86) | |
622 | static int have_tsc = 1; | |
623 | DWORD cyclecount; | |
624 | ||
625 | if (have_tsc) { | |
626 | __try { | |
627 | __asm { | |
628 | _emit 0x0f _emit 0x31 mov cyclecount, eax} | |
629 | RAND_add(&cyclecount, sizeof(cyclecount), 1); | |
630 | } | |
631 | __except(EXCEPTION_EXECUTE_HANDLER) { | |
632 | have_tsc = 0; | |
633 | } | |
634 | } | |
635 | # else | |
636 | # define have_tsc 0 | |
637 | # endif | |
c0722725 | 638 | |
0f113f3e MC |
639 | if (have_perfc) { |
640 | if (QueryPerformanceCounter(&l) == 0) | |
641 | have_perfc = 0; | |
642 | else | |
643 | RAND_add(&l, sizeof(l), 0); | |
644 | } | |
645 | ||
646 | if (!have_tsc && !have_perfc) { | |
647 | w = GetTickCount(); | |
648 | RAND_add(&w, sizeof(w), 0); | |
649 | } | |
c0722725 UM |
650 | } |
651 | ||
652 | /* feed screen contents to PRNG */ | |
2ace287d | 653 | /***************************************************************************** |
2ace287d BM |
654 | * |
655 | * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V. | |
656 | * | |
657 | * Code adapted from | |
c0017a5a | 658 | * <URL:http://support.microsoft.com/default.aspx?scid=kb;[LN];97193>; |
2ace287d BM |
659 | * the original copyright message is: |
660 | * | |
661 | * (C) Copyright Microsoft Corp. 1993. All rights reserved. | |
662 | * | |
663 | * You have a royalty-free right to use, modify, reproduce and | |
664 | * distribute the Sample Files (and/or any modified version) in | |
665 | * any way you find useful, provided that you agree that | |
666 | * Microsoft has no warranty obligations or liability for any | |
667 | * Sample Application Files which are modified. | |
668 | */ | |
c0722725 UM |
669 | |
670 | static void readscreen(void) | |
2ace287d | 671 | { |
0f113f3e MC |
672 | # if !defined(OPENSSL_SYS_WINCE) && !defined(OPENSSL_SYS_WIN32_CYGWIN) |
673 | HDC hScrDC; /* screen DC */ | |
674 | HBITMAP hBitmap; /* handle for our bitmap */ | |
675 | BITMAP bm; /* bitmap properties */ | |
676 | unsigned int size; /* size of bitmap */ | |
677 | char *bmbits; /* contents of bitmap */ | |
678 | int w; /* screen width */ | |
679 | int h; /* screen height */ | |
680 | int y; /* y-coordinate of screen lines to grab */ | |
681 | int n = 16; /* number of screen lines to grab at a time */ | |
682 | BITMAPINFOHEADER bi; /* info about the bitmap */ | |
683 | ||
684 | if (check_winnt() && OPENSSL_isservice() > 0) | |
685 | return; | |
686 | ||
687 | /* Get a reference to the screen DC */ | |
688 | hScrDC = GetDC(NULL); | |
689 | ||
690 | /* Get screen resolution */ | |
691 | w = GetDeviceCaps(hScrDC, HORZRES); | |
692 | h = GetDeviceCaps(hScrDC, VERTRES); | |
693 | ||
694 | /* Create a bitmap compatible with the screen DC */ | |
695 | hBitmap = CreateCompatibleBitmap(hScrDC, w, n); | |
696 | ||
697 | /* Get bitmap properties */ | |
698 | GetObject(hBitmap, sizeof(BITMAP), (LPSTR) & bm); | |
699 | size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes; | |
700 | ||
701 | bi.biSize = sizeof(BITMAPINFOHEADER); | |
702 | bi.biWidth = bm.bmWidth; | |
703 | bi.biHeight = bm.bmHeight; | |
704 | bi.biPlanes = bm.bmPlanes; | |
705 | bi.biBitCount = bm.bmBitsPixel; | |
706 | bi.biCompression = BI_RGB; | |
707 | bi.biSizeImage = 0; | |
708 | bi.biXPelsPerMeter = 0; | |
709 | bi.biYPelsPerMeter = 0; | |
710 | bi.biClrUsed = 0; | |
711 | bi.biClrImportant = 0; | |
712 | ||
713 | bmbits = OPENSSL_malloc(size); | |
90945fa3 | 714 | if (bmbits != NULL) { |
0f113f3e MC |
715 | /* Now go through the whole screen, repeatedly grabbing n lines */ |
716 | for (y = 0; y < h - n; y += n) { | |
717 | unsigned char md[MD_DIGEST_LENGTH]; | |
718 | ||
719 | /* Copy the bits of the current line range into the buffer */ | |
720 | GetDIBits(hScrDC, hBitmap, y, n, | |
721 | bmbits, (BITMAPINFO *) & bi, DIB_RGB_COLORS); | |
722 | ||
723 | /* Get the hash of the bitmap */ | |
724 | MD(bmbits, size, md); | |
725 | ||
726 | /* Seed the random generator with the hash value */ | |
727 | RAND_add(md, MD_DIGEST_LENGTH, 0); | |
728 | } | |
729 | ||
730 | OPENSSL_free(bmbits); | |
731 | } | |
732 | ||
733 | /* Clean up */ | |
734 | DeleteObject(hBitmap); | |
735 | ReleaseDC(NULL, hScrDC); | |
736 | # endif /* !OPENSSL_SYS_WINCE */ | |
2ace287d BM |
737 | } |
738 | ||
2ace287d | 739 | #endif |