rand_win.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. #include "cryptlib.h"
  112. #include <openssl/rand.h>
  113. #include "rand_lcl.h"
  114. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  115. #include <windows.h>
  116. #ifndef _WIN32_WINNT
  117. # define _WIN32_WINNT 0x0400
  118. #endif
  119. #include <wincrypt.h>
  120. #include <tlhelp32.h>
  121. /* Intel hardware RNG CSP -- available from
  122. * http://developer.intel.com/design/security/rng/redist_license.htm
  123. */
  124. #define PROV_INTEL_SEC 22
  125. #define INTEL_DEF_PROV "Intel Hardware Cryptographic Service Provider"
  126. static void readtimer(void);
  127. static void readscreen(void);
  128. /* It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined
  129. when WINVER is 0x0500 and up, which currently only happens on Win2000.
  130. Unfortunately, those are typedefs, so they're a little bit difficult to
  131. detect properly. On the other hand, the macro CURSOR_SHOWING is defined
  132. within the same conditional, so it can be use to detect the absence of said
  133. typedefs. */
  134. #ifndef CURSOR_SHOWING
  135. /*
  136. * Information about the global cursor.
  137. */
  138. typedef struct tagCURSORINFO
  139. {
  140. DWORD cbSize;
  141. DWORD flags;
  142. HCURSOR hCursor;
  143. POINT ptScreenPos;
  144. } CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
  145. #define CURSOR_SHOWING 0x00000001
  146. #endif /* CURSOR_SHOWING */
  147. typedef BOOL (WINAPI *CRYPTACQUIRECONTEXT)(HCRYPTPROV *, LPCTSTR, LPCTSTR,
  148. DWORD, DWORD);
  149. typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *);
  150. typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD);
  151. typedef HWND (WINAPI *GETFOREGROUNDWINDOW)(VOID);
  152. typedef BOOL (WINAPI *GETCURSORINFO)(PCURSORINFO);
  153. typedef DWORD (WINAPI *GETQUEUESTATUS)(UINT);
  154. typedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
  155. typedef BOOL (WINAPI *HEAP32FIRST)(LPHEAPENTRY32, DWORD, DWORD);
  156. typedef BOOL (WINAPI *HEAP32NEXT)(LPHEAPENTRY32);
  157. typedef BOOL (WINAPI *HEAP32LIST)(HANDLE, LPHEAPLIST32);
  158. typedef BOOL (WINAPI *PROCESS32)(HANDLE, LPPROCESSENTRY32);
  159. typedef BOOL (WINAPI *THREAD32)(HANDLE, LPTHREADENTRY32);
  160. typedef BOOL (WINAPI *MODULE32)(HANDLE, LPMODULEENTRY32);
  161. #include <lmcons.h>
  162. #include <lmstats.h>
  163. #if 1 /* The NET API is Unicode only. It requires the use of the UNICODE
  164. * macro. When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was
  165. * was added to the Platform SDK to allow the NET API to be used in
  166. * non-Unicode applications provided that Unicode strings were still
  167. * used for input. LMSTR is defined as LPWSTR.
  168. */
  169. typedef NET_API_STATUS (NET_API_FUNCTION * NETSTATGET)
  170. (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE*);
  171. typedef NET_API_STATUS (NET_API_FUNCTION * NETFREE)(LPBYTE);
  172. #endif /* 1 */
  173. int RAND_poll(void)
  174. {
  175. MEMORYSTATUS m;
  176. HCRYPTPROV hProvider = 0;
  177. BYTE buf[64];
  178. DWORD w;
  179. HWND h;
  180. HMODULE advapi, kernel, user, netapi;
  181. CRYPTACQUIRECONTEXT acquire = 0;
  182. CRYPTGENRANDOM gen = 0;
  183. CRYPTRELEASECONTEXT release = 0;
  184. #if 1 /* There was previously a problem with NETSTATGET. Currently, this
  185. * section is still experimental, but if all goes well, this conditional
  186. * will be removed
  187. */
  188. NETSTATGET netstatget = 0;
  189. NETFREE netfree = 0;
  190. #endif /* 1 */
  191. /* Determine the OS version we are on so we can turn off things
  192. * that do not work properly.
  193. */
  194. OSVERSIONINFO osverinfo ;
  195. osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
  196. GetVersionEx( &osverinfo ) ;
  197. /* load functions dynamically - not available on all systems */
  198. advapi = LoadLibrary("ADVAPI32.DLL");
  199. kernel = LoadLibrary("KERNEL32.DLL");
  200. user = LoadLibrary("USER32.DLL");
  201. netapi = LoadLibrary("NETAPI32.DLL");
  202. #if 1 /* There was previously a problem with NETSTATGET. Currently, this
  203. * section is still experimental, but if all goes well, this conditional
  204. * will be removed
  205. */
  206. if (netapi)
  207. {
  208. netstatget = (NETSTATGET) GetProcAddress(netapi,"NetStatisticsGet");
  209. netfree = (NETFREE) GetProcAddress(netapi,"NetApiBufferFree");
  210. }
  211. if (netstatget && netfree)
  212. {
  213. LPBYTE outbuf;
  214. /* NetStatisticsGet() is a Unicode only function
  215. * STAT_WORKSTATION_0 contains 45 fields and STAT_SERVER_0
  216. * contains 17 fields. We treat each field as a source of
  217. * one byte of entropy.
  218. */
  219. if (netstatget(NULL, L"LanmanWorkstation", 0, 0, &outbuf) == 0)
  220. {
  221. RAND_add(outbuf, sizeof(STAT_WORKSTATION_0), 45);
  222. netfree(outbuf);
  223. }
  224. if (netstatget(NULL, L"LanmanServer", 0, 0, &outbuf) == 0)
  225. {
  226. RAND_add(outbuf, sizeof(STAT_SERVER_0), 17);
  227. netfree(outbuf);
  228. }
  229. }
  230. if (netapi)
  231. FreeLibrary(netapi);
  232. #endif /* 1 */
  233. /* It appears like this can cause an exception deep within ADVAPI32.DLL
  234. * at random times on Windows 2000. Reported by Jeffrey Altman.
  235. * Only use it on NT.
  236. */
  237. /* Wolfgang Marczy <WMarczy@topcall.co.at> reports that
  238. * the RegQueryValueEx call below can hang on NT4.0 (SP6).
  239. * So we don't use this at all for now. */
  240. #if 0
  241. if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  242. osverinfo.dwMajorVersion < 5)
  243. {
  244. /* Read Performance Statistics from NT/2000 registry
  245. * The size of the performance data can vary from call
  246. * to call so we must guess the size of the buffer to use
  247. * and increase its size if we get an ERROR_MORE_DATA
  248. * return instead of ERROR_SUCCESS.
  249. */
  250. LONG rc=ERROR_MORE_DATA;
  251. char * buf=NULL;
  252. DWORD bufsz=0;
  253. DWORD length;
  254. while (rc == ERROR_MORE_DATA)
  255. {
  256. buf = realloc(buf,bufsz+8192);
  257. if (!buf)
  258. break;
  259. bufsz += 8192;
  260. length = bufsz;
  261. rc = RegQueryValueEx(HKEY_PERFORMANCE_DATA, "Global",
  262. NULL, NULL, buf, &length);
  263. }
  264. if (rc == ERROR_SUCCESS)
  265. {
  266. /* For entropy count assume only least significant
  267. * byte of each DWORD is random.
  268. */
  269. RAND_add(&length, sizeof(length), 0);
  270. RAND_add(buf, length, length / 4.0);
  271. }
  272. if (buf)
  273. free(buf);
  274. }
  275. #endif
  276. if (advapi)
  277. {
  278. acquire = (CRYPTACQUIRECONTEXT) GetProcAddress(advapi,
  279. "CryptAcquireContextA");
  280. gen = (CRYPTGENRANDOM) GetProcAddress(advapi,
  281. "CryptGenRandom");
  282. release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
  283. "CryptReleaseContext");
  284. }
  285. if (acquire && gen && release)
  286. {
  287. /* poll the CryptoAPI PRNG */
  288. /* The CryptoAPI returns sizeof(buf) bytes of randomness */
  289. if (acquire(&hProvider, 0, 0, PROV_RSA_FULL,
  290. CRYPT_VERIFYCONTEXT))
  291. {
  292. if (gen(hProvider, sizeof(buf), buf) != 0)
  293. {
  294. RAND_add(buf, sizeof(buf), 0);
  295. #ifdef DEBUG
  296. printf("randomness from PROV_RSA_FULL\n");
  297. #endif
  298. }
  299. release(hProvider, 0);
  300. }
  301. /* poll the Pentium PRG with CryptoAPI */
  302. if (acquire(&hProvider, 0, INTEL_DEF_PROV, PROV_INTEL_SEC, 0))
  303. {
  304. if (gen(hProvider, sizeof(buf), buf) != 0)
  305. {
  306. RAND_add(buf, sizeof(buf), sizeof(buf));
  307. #ifdef DEBUG
  308. printf("randomness from PROV_INTEL_SEC\n");
  309. #endif
  310. }
  311. release(hProvider, 0);
  312. }
  313. }
  314. if (advapi)
  315. FreeLibrary(advapi);
  316. /* timer data */
  317. readtimer();
  318. /* memory usage statistics */
  319. GlobalMemoryStatus(&m);
  320. RAND_add(&m, sizeof(m), 1);
  321. /* process ID */
  322. w = GetCurrentProcessId();
  323. RAND_add(&w, sizeof(w), 1);
  324. if (user)
  325. {
  326. GETCURSORINFO cursor;
  327. GETFOREGROUNDWINDOW win;
  328. GETQUEUESTATUS queue;
  329. win = (GETFOREGROUNDWINDOW) GetProcAddress(user, "GetForegroundWindow");
  330. cursor = (GETCURSORINFO) GetProcAddress(user, "GetCursorInfo");
  331. queue = (GETQUEUESTATUS) GetProcAddress(user, "GetQueueStatus");
  332. if (win)
  333. {
  334. /* window handle */
  335. h = win();
  336. RAND_add(&h, sizeof(h), 0);
  337. }
  338. if (cursor)
  339. {
  340. /* unfortunately, its not safe to call GetCursorInfo()
  341. * on NT4 even though it exists in SP3 (or SP6) and
  342. * higher.
  343. */
  344. if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  345. osverinfo.dwMajorVersion < 5)
  346. cursor = 0;
  347. }
  348. if (cursor)
  349. {
  350. /* cursor position */
  351. /* assume 2 bytes of entropy */
  352. CURSORINFO ci;
  353. ci.cbSize = sizeof(CURSORINFO);
  354. if (cursor(&ci))
  355. RAND_add(&ci, ci.cbSize, 2);
  356. }
  357. if (queue)
  358. {
  359. /* message queue status */
  360. /* assume 1 byte of entropy */
  361. w = queue(QS_ALLEVENTS);
  362. RAND_add(&w, sizeof(w), 1);
  363. }
  364. FreeLibrary(user);
  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. if (kernel)
  379. {
  380. CREATETOOLHELP32SNAPSHOT snap;
  381. HANDLE handle;
  382. HEAP32FIRST heap_first;
  383. HEAP32NEXT heap_next;
  384. HEAP32LIST heaplist_first, heaplist_next;
  385. PROCESS32 process_first, process_next;
  386. THREAD32 thread_first, thread_next;
  387. MODULE32 module_first, module_next;
  388. HEAPLIST32 hlist;
  389. HEAPENTRY32 hentry;
  390. PROCESSENTRY32 p;
  391. THREADENTRY32 t;
  392. MODULEENTRY32 m;
  393. snap = (CREATETOOLHELP32SNAPSHOT)
  394. GetProcAddress(kernel, "CreateToolhelp32Snapshot");
  395. heap_first = (HEAP32FIRST) GetProcAddress(kernel, "Heap32First");
  396. heap_next = (HEAP32NEXT) GetProcAddress(kernel, "Heap32Next");
  397. heaplist_first = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListFirst");
  398. heaplist_next = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListNext");
  399. process_first = (PROCESS32) GetProcAddress(kernel, "Process32First");
  400. process_next = (PROCESS32) GetProcAddress(kernel, "Process32Next");
  401. thread_first = (THREAD32) GetProcAddress(kernel, "Thread32First");
  402. thread_next = (THREAD32) GetProcAddress(kernel, "Thread32Next");
  403. module_first = (MODULE32) GetProcAddress(kernel, "Module32First");
  404. module_next = (MODULE32) GetProcAddress(kernel, "Module32Next");
  405. if (snap && heap_first && heap_next && heaplist_first &&
  406. heaplist_next && process_first && process_next &&
  407. thread_first && thread_next && module_first &&
  408. module_next && (handle = snap(TH32CS_SNAPALL,0))
  409. != NULL)
  410. {
  411. /* heap list and heap walking */
  412. /* HEAPLIST32 contains 3 fields that will change with
  413. * each entry. Consider each field a source of 1 byte
  414. * of entropy.
  415. * HEAPENTRY32 contains 5 fields that will change with
  416. * each entry. Consider each field a source of 1 byte
  417. * of entropy.
  418. */
  419. hlist.dwSize = sizeof(HEAPLIST32);
  420. if (heaplist_first(handle, &hlist))
  421. do
  422. {
  423. RAND_add(&hlist, hlist.dwSize, 3);
  424. hentry.dwSize = sizeof(HEAPENTRY32);
  425. if (heap_first(&hentry,
  426. hlist.th32ProcessID,
  427. hlist.th32HeapID))
  428. {
  429. int entrycnt = 80;
  430. do
  431. RAND_add(&hentry,
  432. hentry.dwSize, 5);
  433. while (heap_next(&hentry)
  434. && --entrycnt > 0);
  435. }
  436. } while (heaplist_next(handle,
  437. &hlist));
  438. /* process walking */
  439. /* PROCESSENTRY32 contains 9 fields that will change
  440. * with each entry. Consider each field a source of
  441. * 1 byte of entropy.
  442. */
  443. p.dwSize = sizeof(PROCESSENTRY32);
  444. if (process_first(handle, &p))
  445. do
  446. RAND_add(&p, p.dwSize, 9);
  447. while (process_next(handle, &p));
  448. /* thread walking */
  449. /* THREADENTRY32 contains 6 fields that will change
  450. * with each entry. Consider each field a source of
  451. * 1 byte of entropy.
  452. */
  453. t.dwSize = sizeof(THREADENTRY32);
  454. if (thread_first(handle, &t))
  455. do
  456. RAND_add(&t, t.dwSize, 6);
  457. while (thread_next(handle, &t));
  458. /* module walking */
  459. /* MODULEENTRY32 contains 9 fields that will change
  460. * with each entry. Consider each field a source of
  461. * 1 byte of entropy.
  462. */
  463. m.dwSize = sizeof(MODULEENTRY32);
  464. if (module_first(handle, &m))
  465. do
  466. RAND_add(&m, m.dwSize, 9);
  467. while (module_next(handle, &m));
  468. CloseHandle(handle);
  469. }
  470. FreeLibrary(kernel);
  471. }
  472. #ifdef DEBUG
  473. printf("Exiting RAND_poll\n");
  474. #endif
  475. return(1);
  476. }
  477. int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
  478. {
  479. double add_entropy=0;
  480. switch (iMsg)
  481. {
  482. case WM_KEYDOWN:
  483. {
  484. static WPARAM key;
  485. if (key != wParam)
  486. add_entropy = 0.05;
  487. key = wParam;
  488. }
  489. break;
  490. case WM_MOUSEMOVE:
  491. {
  492. static int lastx,lasty,lastdx,lastdy;
  493. int x,y,dx,dy;
  494. x=LOWORD(lParam);
  495. y=HIWORD(lParam);
  496. dx=lastx-x;
  497. dy=lasty-y;
  498. if (dx != 0 && dy != 0 && dx-lastdx != 0 && dy-lastdy != 0)
  499. add_entropy=.2;
  500. lastx=x, lasty=y;
  501. lastdx=dx, lastdy=dy;
  502. }
  503. break;
  504. }
  505. readtimer();
  506. RAND_add(&iMsg, sizeof(iMsg), add_entropy);
  507. RAND_add(&wParam, sizeof(wParam), 0);
  508. RAND_add(&lParam, sizeof(lParam), 0);
  509. return (RAND_status());
  510. }
  511. void RAND_screen(void) /* function available for backward compatibility */
  512. {
  513. RAND_poll();
  514. readscreen();
  515. }
  516. /* feed timing information to the PRNG */
  517. static void readtimer(void)
  518. {
  519. DWORD w;
  520. LARGE_INTEGER l;
  521. static int have_perfc = 1;
  522. #ifdef _MSC_VER
  523. static int have_tsc = 1;
  524. DWORD cyclecount;
  525. if (have_tsc) {
  526. __try {
  527. __asm {
  528. _emit 0x0f
  529. _emit 0x31
  530. mov cyclecount, eax
  531. }
  532. RAND_add(&cyclecount, sizeof(cyclecount), 1);
  533. } __except(EXCEPTION_EXECUTE_HANDLER) {
  534. have_tsc = 0;
  535. }
  536. }
  537. #else
  538. # define have_tsc 0
  539. #endif
  540. if (have_perfc) {
  541. if (QueryPerformanceCounter(&l) == 0)
  542. have_perfc = 0;
  543. else
  544. RAND_add(&l, sizeof(l), 0);
  545. }
  546. if (!have_tsc && !have_perfc) {
  547. w = GetTickCount();
  548. RAND_add(&w, sizeof(w), 0);
  549. }
  550. }
  551. /* feed screen contents to PRNG */
  552. /*****************************************************************************
  553. *
  554. * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
  555. *
  556. * Code adapted from
  557. * <URL:http://www.microsoft.com/kb/developr/win_dk/q97193.htm>;
  558. * the original copyright message is:
  559. *
  560. * (C) Copyright Microsoft Corp. 1993. All rights reserved.
  561. *
  562. * You have a royalty-free right to use, modify, reproduce and
  563. * distribute the Sample Files (and/or any modified version) in
  564. * any way you find useful, provided that you agree that
  565. * Microsoft has no warranty obligations or liability for any
  566. * Sample Application Files which are modified.
  567. */
  568. static void readscreen(void)
  569. {
  570. HDC hScrDC; /* screen DC */
  571. HDC hMemDC; /* memory DC */
  572. HBITMAP hBitmap; /* handle for our bitmap */
  573. HBITMAP hOldBitmap; /* handle for previous bitmap */
  574. BITMAP bm; /* bitmap properties */
  575. unsigned int size; /* size of bitmap */
  576. char *bmbits; /* contents of bitmap */
  577. int w; /* screen width */
  578. int h; /* screen height */
  579. int y; /* y-coordinate of screen lines to grab */
  580. int n = 16; /* number of screen lines to grab at a time */
  581. /* Create a screen DC and a memory DC compatible to screen DC */
  582. hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  583. hMemDC = CreateCompatibleDC(hScrDC);
  584. /* Get screen resolution */
  585. w = GetDeviceCaps(hScrDC, HORZRES);
  586. h = GetDeviceCaps(hScrDC, VERTRES);
  587. /* Create a bitmap compatible with the screen DC */
  588. hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
  589. /* Select new bitmap into memory DC */
  590. hOldBitmap = SelectObject(hMemDC, hBitmap);
  591. /* Get bitmap properties */
  592. GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  593. size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
  594. bmbits = OPENSSL_malloc(size);
  595. if (bmbits) {
  596. /* Now go through the whole screen, repeatedly grabbing n lines */
  597. for (y = 0; y < h-n; y += n)
  598. {
  599. unsigned char md[MD_DIGEST_LENGTH];
  600. /* Bitblt screen DC to memory DC */
  601. BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
  602. /* Copy bitmap bits from memory DC to bmbits */
  603. GetBitmapBits(hBitmap, size, bmbits);
  604. /* Get the hash of the bitmap */
  605. MD(bmbits,size,md);
  606. /* Seed the random generator with the hash value */
  607. RAND_add(md, MD_DIGEST_LENGTH, 0);
  608. }
  609. OPENSSL_free(bmbits);
  610. }
  611. /* Select old bitmap back into memory DC */
  612. hBitmap = SelectObject(hMemDC, hOldBitmap);
  613. /* Clean up */
  614. DeleteObject(hBitmap);
  615. DeleteDC(hMemDC);
  616. DeleteDC(hScrDC);
  617. }
  618. #endif