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