A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

638 lines
23 KiB

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