rand_win.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. #ifndef UWP_ENABLED // -- GODOT --
  120. # include <wincrypt.h>
  121. # include <tlhelp32.h>
  122. #endif // -- GODOT --
  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
  129. /*
  130. * Intel hardware RNG CSP -- available from
  131. * http://developer.intel.com/design/security/rng/redist_license.htm
  132. */
  133. # define PROV_INTEL_SEC 22
  134. # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
  135. static void readtimer(void);
  136. static void readscreen(void);
  137. /*
  138. * It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined
  139. * when WINVER is 0x0500 and up, which currently only happens on Win2000.
  140. * Unfortunately, those are typedefs, so they're a little bit difficult to
  141. * detect properly. On the other hand, the macro CURSOR_SHOWING is defined
  142. * within the same conditional, so it can be use to detect the absence of
  143. * said typedefs.
  144. */
  145. # ifndef CURSOR_SHOWING
  146. /*
  147. * Information about the global cursor.
  148. */
  149. typedef struct tagCURSORINFO {
  150. DWORD cbSize;
  151. DWORD flags;
  152. HCURSOR hCursor;
  153. POINT ptScreenPos;
  154. } CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
  155. # define CURSOR_SHOWING 0x00000001
  156. # endif /* CURSOR_SHOWING */
  157. # if !defined(OPENSSL_SYS_WINCE) && !defined(UWP_ENABLED) // -- GODOT --
  158. typedef BOOL(WINAPI *CRYPTACQUIRECONTEXTW) (HCRYPTPROV *, LPCWSTR, LPCWSTR,
  159. DWORD, DWORD);
  160. typedef BOOL(WINAPI *CRYPTGENRANDOM) (HCRYPTPROV, DWORD, BYTE *);
  161. typedef BOOL(WINAPI *CRYPTRELEASECONTEXT) (HCRYPTPROV, DWORD);
  162. typedef HWND(WINAPI *GETFOREGROUNDWINDOW) (VOID);
  163. typedef BOOL(WINAPI *GETCURSORINFO) (PCURSORINFO);
  164. typedef DWORD(WINAPI *GETQUEUESTATUS) (UINT);
  165. typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
  166. typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
  167. typedef BOOL(WINAPI *HEAP32FIRST) (LPHEAPENTRY32, DWORD, size_t);
  168. typedef BOOL(WINAPI *HEAP32NEXT) (LPHEAPENTRY32);
  169. typedef BOOL(WINAPI *HEAP32LIST) (HANDLE, LPHEAPLIST32);
  170. typedef BOOL(WINAPI *PROCESS32) (HANDLE, LPPROCESSENTRY32);
  171. typedef BOOL(WINAPI *THREAD32) (HANDLE, LPTHREADENTRY32);
  172. typedef BOOL(WINAPI *MODULE32) (HANDLE, LPMODULEENTRY32);
  173. # include <lmcons.h>
  174. # include <lmstats.h>
  175. # if 1
  176. /*
  177. * The NET API is Unicode only. It requires the use of the UNICODE macro.
  178. * When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was was added to the
  179. * Platform SDK to allow the NET API to be used in non-Unicode applications
  180. * provided that Unicode strings were still used for input. LMSTR is defined
  181. * as LPWSTR.
  182. */
  183. typedef NET_API_STATUS(NET_API_FUNCTION *NETSTATGET)
  184. (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE *);
  185. typedef NET_API_STATUS(NET_API_FUNCTION *NETFREE) (LPBYTE);
  186. # endif /* 1 */
  187. # endif /* !OPENSSL_SYS_WINCE */
  188. #if !defined(UWP_ENABLED) // -- GODOT --
  189. int RAND_poll(void)
  190. {
  191. MEMORYSTATUS m;
  192. HCRYPTPROV hProvider = 0;
  193. DWORD w;
  194. int good = 0;
  195. # if defined(OPENSSL_SYS_WINCE)
  196. # if defined(_WIN32_WCE) && _WIN32_WCE>=300
  197. /*
  198. * Even though MSDN says _WIN32_WCE>=210, it doesn't seem to be available
  199. * in commonly available implementations prior 300...
  200. */
  201. {
  202. BYTE buf[64];
  203. /* poll the CryptoAPI PRNG */
  204. /* The CryptoAPI returns sizeof(buf) bytes of randomness */
  205. if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
  206. CRYPT_VERIFYCONTEXT)) {
  207. if (CryptGenRandom(hProvider, sizeof(buf), buf))
  208. RAND_add(buf, sizeof(buf), sizeof(buf));
  209. CryptReleaseContext(hProvider, 0);
  210. }
  211. }
  212. # endif
  213. # else /* OPENSSL_SYS_WINCE */
  214. /*
  215. * None of below libraries are present on Windows CE, which is
  216. * why we #ifndef the whole section. This also excuses us from
  217. * handling the GetProcAddress issue. The trouble is that in
  218. * real Win32 API GetProcAddress is available in ANSI flavor
  219. * only. In WinCE on the other hand GetProcAddress is a macro
  220. * most commonly defined as GetProcAddressW, which accepts
  221. * Unicode argument. If we were to call GetProcAddress under
  222. * WinCE, I'd recommend to either redefine GetProcAddress as
  223. * GetProcAddressA (there seem to be one in common CE spec) or
  224. * implement own shim routine, which would accept ANSI argument
  225. * and expand it to Unicode.
  226. */
  227. {
  228. /* load functions dynamically - not available on all systems */
  229. HMODULE advapi = LoadLibrary(TEXT("ADVAPI32.DLL"));
  230. HMODULE kernel = LoadLibrary(TEXT("KERNEL32.DLL"));
  231. HMODULE user = NULL;
  232. HMODULE netapi = LoadLibrary(TEXT("NETAPI32.DLL"));
  233. CRYPTACQUIRECONTEXTW acquire = NULL;
  234. CRYPTGENRANDOM gen = NULL;
  235. CRYPTRELEASECONTEXT release = NULL;
  236. NETSTATGET netstatget = NULL;
  237. NETFREE netfree = NULL;
  238. BYTE buf[64];
  239. if (netapi) {
  240. netstatget =
  241. (NETSTATGET) GetProcAddress(netapi, "NetStatisticsGet");
  242. netfree = (NETFREE) GetProcAddress(netapi, "NetApiBufferFree");
  243. }
  244. if (netstatget && netfree) {
  245. LPBYTE outbuf;
  246. /*
  247. * NetStatisticsGet() is a Unicode only function
  248. * STAT_WORKSTATION_0 contains 45 fields and STAT_SERVER_0
  249. * contains 17 fields. We treat each field as a source of one
  250. * byte of entropy.
  251. */
  252. if (netstatget(NULL, L"LanmanWorkstation", 0, 0, &outbuf) == 0) {
  253. RAND_add(outbuf, sizeof(STAT_WORKSTATION_0), 45);
  254. netfree(outbuf);
  255. }
  256. if (netstatget(NULL, L"LanmanServer", 0, 0, &outbuf) == 0) {
  257. RAND_add(outbuf, sizeof(STAT_SERVER_0), 17);
  258. netfree(outbuf);
  259. }
  260. }
  261. if (netapi)
  262. FreeLibrary(netapi);
  263. /*
  264. * It appears like this can cause an exception deep within
  265. * ADVAPI32.DLL at random times on Windows 2000. Reported by Jeffrey
  266. * Altman. Only use it on NT.
  267. */
  268. if (advapi) {
  269. /*
  270. * If it's available, then it's available in both ANSI
  271. * and UNICODE flavors even in Win9x, documentation says.
  272. * We favor Unicode...
  273. */
  274. acquire = (CRYPTACQUIRECONTEXTW) GetProcAddress(advapi,
  275. "CryptAcquireContextW");
  276. gen = (CRYPTGENRANDOM) GetProcAddress(advapi, "CryptGenRandom");
  277. release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
  278. "CryptReleaseContext");
  279. }
  280. if (acquire && gen && release) {
  281. /* poll the CryptoAPI PRNG */
  282. /* The CryptoAPI returns sizeof(buf) bytes of randomness */
  283. if (acquire(&hProvider, NULL, NULL, PROV_RSA_FULL,
  284. CRYPT_VERIFYCONTEXT)) {
  285. if (gen(hProvider, sizeof(buf), buf) != 0) {
  286. RAND_add(buf, sizeof(buf), 0);
  287. good = 1;
  288. # if 0
  289. printf("randomness from PROV_RSA_FULL\n");
  290. # endif
  291. }
  292. release(hProvider, 0);
  293. }
  294. /* poll the Pentium PRG with CryptoAPI */
  295. if (acquire(&hProvider, 0, INTEL_DEF_PROV, PROV_INTEL_SEC, 0)) {
  296. if (gen(hProvider, sizeof(buf), buf) != 0) {
  297. RAND_add(buf, sizeof(buf), sizeof(buf));
  298. good = 1;
  299. # if 0
  300. printf("randomness from PROV_INTEL_SEC\n");
  301. # endif
  302. }
  303. release(hProvider, 0);
  304. }
  305. }
  306. if (advapi)
  307. FreeLibrary(advapi);
  308. if ((!check_winnt() ||
  309. !OPENSSL_isservice()) &&
  310. (user = LoadLibrary(TEXT("USER32.DLL")))) {
  311. GETCURSORINFO cursor;
  312. GETFOREGROUNDWINDOW win;
  313. GETQUEUESTATUS queue;
  314. win =
  315. (GETFOREGROUNDWINDOW) GetProcAddress(user,
  316. "GetForegroundWindow");
  317. cursor = (GETCURSORINFO) GetProcAddress(user, "GetCursorInfo");
  318. queue = (GETQUEUESTATUS) GetProcAddress(user, "GetQueueStatus");
  319. if (win) {
  320. /* window handle */
  321. HWND h = win();
  322. RAND_add(&h, sizeof(h), 0);
  323. }
  324. if (cursor) {
  325. /*
  326. * unfortunately, its not safe to call GetCursorInfo() on NT4
  327. * even though it exists in SP3 (or SP6) and higher.
  328. */
  329. if (check_winnt() && !check_win_minplat(5))
  330. cursor = 0;
  331. }
  332. if (cursor) {
  333. /* cursor position */
  334. /* assume 2 bytes of entropy */
  335. CURSORINFO ci;
  336. ci.cbSize = sizeof(CURSORINFO);
  337. if (cursor(&ci))
  338. RAND_add(&ci, ci.cbSize, 2);
  339. }
  340. if (queue) {
  341. /* message queue status */
  342. /* assume 1 byte of entropy */
  343. w = queue(QS_ALLEVENTS);
  344. RAND_add(&w, sizeof(w), 1);
  345. }
  346. FreeLibrary(user);
  347. }
  348. /*-
  349. * Toolhelp32 snapshot: enumerate processes, threads, modules and heap
  350. * http://msdn.microsoft.com/library/psdk/winbase/toolhelp_5pfd.htm
  351. * (Win 9x and 2000 only, not available on NT)
  352. *
  353. * This seeding method was proposed in Peter Gutmann, Software
  354. * Generation of Practically Strong Random Numbers,
  355. * http://www.usenix.org/publications/library/proceedings/sec98/gutmann.html
  356. * revised version at http://www.cryptoengines.com/~peter/06_random.pdf
  357. * (The assignment of entropy estimates below is arbitrary, but based
  358. * on Peter's analysis the full poll appears to be safe. Additional
  359. * interactive seeding is encouraged.)
  360. */
  361. if (kernel) {
  362. CREATETOOLHELP32SNAPSHOT snap;
  363. CLOSETOOLHELP32SNAPSHOT close_snap;
  364. HANDLE handle;
  365. HEAP32FIRST heap_first;
  366. HEAP32NEXT heap_next;
  367. HEAP32LIST heaplist_first, heaplist_next;
  368. PROCESS32 process_first, process_next;
  369. THREAD32 thread_first, thread_next;
  370. MODULE32 module_first, module_next;
  371. HEAPLIST32 hlist;
  372. HEAPENTRY32 hentry;
  373. PROCESSENTRY32 p;
  374. THREADENTRY32 t;
  375. MODULEENTRY32 m;
  376. DWORD starttime = 0;
  377. snap = (CREATETOOLHELP32SNAPSHOT)
  378. GetProcAddress(kernel, "CreateToolhelp32Snapshot");
  379. close_snap = (CLOSETOOLHELP32SNAPSHOT)
  380. GetProcAddress(kernel, "CloseToolhelp32Snapshot");
  381. heap_first = (HEAP32FIRST) GetProcAddress(kernel, "Heap32First");
  382. heap_next = (HEAP32NEXT) GetProcAddress(kernel, "Heap32Next");
  383. heaplist_first =
  384. (HEAP32LIST) GetProcAddress(kernel, "Heap32ListFirst");
  385. heaplist_next =
  386. (HEAP32LIST) GetProcAddress(kernel, "Heap32ListNext");
  387. process_first =
  388. (PROCESS32) GetProcAddress(kernel, "Process32First");
  389. process_next =
  390. (PROCESS32) GetProcAddress(kernel, "Process32Next");
  391. thread_first = (THREAD32) GetProcAddress(kernel, "Thread32First");
  392. thread_next = (THREAD32) GetProcAddress(kernel, "Thread32Next");
  393. module_first = (MODULE32) GetProcAddress(kernel, "Module32First");
  394. module_next = (MODULE32) GetProcAddress(kernel, "Module32Next");
  395. if (snap && heap_first && heap_next && heaplist_first &&
  396. heaplist_next && process_first && process_next &&
  397. thread_first && thread_next && module_first &&
  398. module_next && (handle = snap(TH32CS_SNAPALL, 0))
  399. != INVALID_HANDLE_VALUE) {
  400. /* heap list and heap walking */
  401. /*
  402. * HEAPLIST32 contains 3 fields that will change with each
  403. * entry. Consider each field a source of 1 byte of entropy.
  404. * HEAPENTRY32 contains 5 fields that will change with each
  405. * entry. Consider each field a source of 1 byte of entropy.
  406. */
  407. ZeroMemory(&hlist, sizeof(HEAPLIST32));
  408. hlist.dwSize = sizeof(HEAPLIST32);
  409. if (good)
  410. starttime = GetTickCount();
  411. # ifdef _MSC_VER
  412. if (heaplist_first(handle, &hlist)) {
  413. /*
  414. * following discussion on dev ML, exception on WinCE (or
  415. * other Win platform) is theoretically of unknown
  416. * origin; prevent infinite loop here when this
  417. * theoretical case occurs; otherwise cope with the
  418. * expected (MSDN documented) exception-throwing
  419. * behaviour of Heap32Next() on WinCE.
  420. *
  421. * based on patch in original message by Tanguy Fautré
  422. * (2009/03/02) Subject: RAND_poll() and
  423. * CreateToolhelp32Snapshot() stability
  424. */
  425. int ex_cnt_limit = 42;
  426. do {
  427. RAND_add(&hlist, hlist.dwSize, 3);
  428. __try {
  429. ZeroMemory(&hentry, sizeof(HEAPENTRY32));
  430. hentry.dwSize = sizeof(HEAPENTRY32);
  431. if (heap_first(&hentry,
  432. hlist.th32ProcessID,
  433. hlist.th32HeapID)) {
  434. int entrycnt = 80;
  435. do
  436. RAND_add(&hentry, hentry.dwSize, 5);
  437. while (heap_next(&hentry)
  438. && (!good
  439. || (GetTickCount() - starttime) <
  440. MAXDELAY)
  441. && --entrycnt > 0);
  442. }
  443. }
  444. __except(EXCEPTION_EXECUTE_HANDLER) {
  445. /*
  446. * ignore access violations when walking the heap
  447. * list
  448. */
  449. ex_cnt_limit--;
  450. }
  451. } while (heaplist_next(handle, &hlist)
  452. && (!good
  453. || (GetTickCount() - starttime) < MAXDELAY)
  454. && ex_cnt_limit > 0);
  455. }
  456. # else
  457. if (heaplist_first(handle, &hlist)) {
  458. do {
  459. RAND_add(&hlist, hlist.dwSize, 3);
  460. hentry.dwSize = sizeof(HEAPENTRY32);
  461. if (heap_first(&hentry,
  462. hlist.th32ProcessID,
  463. hlist.th32HeapID)) {
  464. int entrycnt = 80;
  465. do
  466. RAND_add(&hentry, hentry.dwSize, 5);
  467. while (heap_next(&hentry)
  468. && --entrycnt > 0);
  469. }
  470. } while (heaplist_next(handle, &hlist)
  471. && (!good
  472. || (GetTickCount() - starttime) < MAXDELAY));
  473. }
  474. # endif
  475. /* process walking */
  476. /*
  477. * PROCESSENTRY32 contains 9 fields that will change with
  478. * each entry. Consider each field a source of 1 byte of
  479. * entropy.
  480. */
  481. p.dwSize = sizeof(PROCESSENTRY32);
  482. if (good)
  483. starttime = GetTickCount();
  484. if (process_first(handle, &p))
  485. do
  486. RAND_add(&p, p.dwSize, 9);
  487. while (process_next(handle, &p)
  488. && (!good
  489. || (GetTickCount() - starttime) < MAXDELAY));
  490. /* thread walking */
  491. /*
  492. * THREADENTRY32 contains 6 fields that will change with each
  493. * entry. Consider each field a source of 1 byte of entropy.
  494. */
  495. t.dwSize = sizeof(THREADENTRY32);
  496. if (good)
  497. starttime = GetTickCount();
  498. if (thread_first(handle, &t))
  499. do
  500. RAND_add(&t, t.dwSize, 6);
  501. while (thread_next(handle, &t)
  502. && (!good
  503. || (GetTickCount() - starttime) < MAXDELAY));
  504. /* module walking */
  505. /*
  506. * MODULEENTRY32 contains 9 fields that will change with each
  507. * entry. Consider each field a source of 1 byte of entropy.
  508. */
  509. m.dwSize = sizeof(MODULEENTRY32);
  510. if (good)
  511. starttime = GetTickCount();
  512. if (module_first(handle, &m))
  513. do
  514. RAND_add(&m, m.dwSize, 9);
  515. while (module_next(handle, &m)
  516. && (!good
  517. || (GetTickCount() - starttime) < MAXDELAY));
  518. if (close_snap)
  519. close_snap(handle);
  520. else
  521. CloseHandle(handle);
  522. }
  523. FreeLibrary(kernel);
  524. }
  525. }
  526. # endif /* !OPENSSL_SYS_WINCE */
  527. /* timer data */
  528. readtimer();
  529. /* memory usage statistics */
  530. GlobalMemoryStatus(&m);
  531. RAND_add(&m, sizeof(m), 1);
  532. /* process ID */
  533. w = GetCurrentProcessId();
  534. RAND_add(&w, sizeof(w), 1);
  535. # if 0
  536. printf("Exiting RAND_poll\n");
  537. # endif
  538. return (1);
  539. }
  540. #endif // UWP_ENABLED // -- GODOT --
  541. int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
  542. {
  543. double add_entropy = 0;
  544. switch (iMsg) {
  545. case WM_KEYDOWN:
  546. {
  547. static WPARAM key;
  548. if (key != wParam)
  549. add_entropy = 0.05;
  550. key = wParam;
  551. }
  552. break;
  553. case WM_MOUSEMOVE:
  554. {
  555. static int lastx, lasty, lastdx, lastdy;
  556. int x, y, dx, dy;
  557. x = LOWORD(lParam);
  558. y = HIWORD(lParam);
  559. dx = lastx - x;
  560. dy = lasty - y;
  561. if (dx != 0 && dy != 0 && dx - lastdx != 0 && dy - lastdy != 0)
  562. add_entropy = .2;
  563. lastx = x, lasty = y;
  564. lastdx = dx, lastdy = dy;
  565. }
  566. break;
  567. }
  568. readtimer();
  569. RAND_add(&iMsg, sizeof(iMsg), add_entropy);
  570. RAND_add(&wParam, sizeof(wParam), 0);
  571. RAND_add(&lParam, sizeof(lParam), 0);
  572. return (RAND_status());
  573. }
  574. void RAND_screen(void)
  575. { /* function available for backward
  576. * compatibility */
  577. RAND_poll();
  578. readscreen();
  579. }
  580. /* feed timing information to the PRNG */
  581. static void readtimer(void)
  582. {
  583. DWORD w;
  584. LARGE_INTEGER l;
  585. static int have_perfc = 1;
  586. # if defined(_MSC_VER) && defined(_M_X86)
  587. static int have_tsc = 1;
  588. DWORD cyclecount;
  589. if (have_tsc) {
  590. __try {
  591. __asm {
  592. _emit 0x0f _emit 0x31 mov cyclecount, eax}
  593. RAND_add(&cyclecount, sizeof(cyclecount), 1);
  594. }
  595. __except(EXCEPTION_EXECUTE_HANDLER) {
  596. have_tsc = 0;
  597. }
  598. }
  599. # else
  600. # define have_tsc 0
  601. # endif
  602. if (have_perfc) {
  603. if (QueryPerformanceCounter(&l) == 0)
  604. have_perfc = 0;
  605. else
  606. RAND_add(&l, sizeof(l), 0);
  607. }
  608. if (!have_tsc && !have_perfc) {
  609. w = GetTickCount();
  610. RAND_add(&w, sizeof(w), 0);
  611. }
  612. }
  613. /* feed screen contents to PRNG */
  614. /*****************************************************************************
  615. *
  616. * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
  617. *
  618. * Code adapted from
  619. * <URL:http://support.microsoft.com/default.aspx?scid=kb;[LN];97193>;
  620. * the original copyright message is:
  621. *
  622. * (C) Copyright Microsoft Corp. 1993. All rights reserved.
  623. *
  624. * You have a royalty-free right to use, modify, reproduce and
  625. * distribute the Sample Files (and/or any modified version) in
  626. * any way you find useful, provided that you agree that
  627. * Microsoft has no warranty obligations or liability for any
  628. * Sample Application Files which are modified.
  629. */
  630. static void readscreen(void)
  631. {
  632. # if !defined(OPENSSL_SYS_WINCE) && !defined(OPENSSL_SYS_WIN32_CYGWIN) && !defined(UWP_ENABLED) // -- GODOT --
  633. HDC hScrDC; /* screen DC */
  634. HBITMAP hBitmap; /* handle for our bitmap */
  635. BITMAP bm; /* bitmap properties */
  636. unsigned int size; /* size of bitmap */
  637. char *bmbits; /* contents of bitmap */
  638. int w; /* screen width */
  639. int h; /* screen height */
  640. int y; /* y-coordinate of screen lines to grab */
  641. int n = 16; /* number of screen lines to grab at a time */
  642. BITMAPINFOHEADER bi; /* info about the bitmap */
  643. if (check_winnt() && OPENSSL_isservice() > 0)
  644. return;
  645. /* Get a reference to the screen DC */
  646. hScrDC = GetDC(NULL);
  647. /* Get screen resolution */
  648. w = GetDeviceCaps(hScrDC, HORZRES);
  649. h = GetDeviceCaps(hScrDC, VERTRES);
  650. /* Create a bitmap compatible with the screen DC */
  651. hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
  652. /* Get bitmap properties */
  653. GetObject(hBitmap, sizeof(BITMAP), (LPSTR) & bm);
  654. size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
  655. bi.biSize = sizeof(BITMAPINFOHEADER);
  656. bi.biWidth = bm.bmWidth;
  657. bi.biHeight = bm.bmHeight;
  658. bi.biPlanes = bm.bmPlanes;
  659. bi.biBitCount = bm.bmBitsPixel;
  660. bi.biCompression = BI_RGB;
  661. bi.biSizeImage = 0;
  662. bi.biXPelsPerMeter = 0;
  663. bi.biYPelsPerMeter = 0;
  664. bi.biClrUsed = 0;
  665. bi.biClrImportant = 0;
  666. bmbits = OPENSSL_malloc(size);
  667. if (bmbits) {
  668. /* Now go through the whole screen, repeatedly grabbing n lines */
  669. for (y = 0; y < h - n; y += n) {
  670. unsigned char md[MD_DIGEST_LENGTH];
  671. /* Copy the bits of the current line range into the buffer */
  672. GetDIBits(hScrDC, hBitmap, y, n,
  673. bmbits, (BITMAPINFO *) & bi, DIB_RGB_COLORS);
  674. /* Get the hash of the bitmap */
  675. MD(bmbits, size, md);
  676. /* Seed the random generator with the hash value */
  677. RAND_add(md, MD_DIGEST_LENGTH, 0);
  678. }
  679. OPENSSL_free(bmbits);
  680. }
  681. /* Clean up */
  682. DeleteObject(hBitmap);
  683. ReleaseDC(NULL, hScrDC);
  684. # endif /* !OPENSSL_SYS_WINCE */
  685. }
  686. #endif