1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
111 #include "internal/cryptlib.h"
112 #include <openssl/rand.h>
113 #include "rand_lcl.h"
115 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
116 # include <windows.h>
117 # ifndef _WIN32_WINNT
118 # define _WIN32_WINNT 0x0400
120 # include <wincrypt.h>
121 # include <tlhelp32.h>
124 * Limit the time spent walking through the heap, processes, threads and
125 * modules to a maximum of 1000 miliseconds each, unless CryptoGenRandom
128 # define MAXDELAY 1000
131 * Intel hardware RNG CSP -- available from
132 * http://developer.intel.com/design/security/rng/redist_license.htm
134 # define PROV_INTEL_SEC 22
135 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
137 static void readtimer(void);
138 static void readscreen(void);
141 * It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined
142 * when WINVER is 0x0500 and up, which currently only happens on Win2000.
143 * Unfortunately, those are typedefs, so they're a little bit difficult to
144 * detect properly. On the other hand, the macro CURSOR_SHOWING is defined
145 * within the same conditional, so it can be use to detect the absence of
149 # ifndef CURSOR_SHOWING
151 * Information about the global cursor.
153 typedef struct tagCURSORINFO
{
158 } CURSORINFO
, *PCURSORINFO
, *LPCURSORINFO
;
160 # define CURSOR_SHOWING 0x00000001
161 # endif /* CURSOR_SHOWING */
163 # if !defined(OPENSSL_SYS_WINCE)
164 typedef BOOL(WINAPI
*CRYPTACQUIRECONTEXTW
) (HCRYPTPROV
*, LPCWSTR
, LPCWSTR
,
166 typedef BOOL(WINAPI
*CRYPTGENRANDOM
) (HCRYPTPROV
, DWORD
, BYTE
*);
167 typedef BOOL(WINAPI
*CRYPTRELEASECONTEXT
) (HCRYPTPROV
, DWORD
);
169 typedef HWND(WINAPI
*GETFOREGROUNDWINDOW
) (VOID
);
170 typedef BOOL(WINAPI
*GETCURSORINFO
) (PCURSORINFO
);
171 typedef DWORD(WINAPI
*GETQUEUESTATUS
) (UINT
);
173 typedef HANDLE(WINAPI
*CREATETOOLHELP32SNAPSHOT
) (DWORD
, DWORD
);
174 typedef BOOL(WINAPI
*CLOSETOOLHELP32SNAPSHOT
) (HANDLE
);
175 typedef BOOL(WINAPI
*HEAP32FIRST
) (LPHEAPENTRY32
, DWORD
, size_t);
176 typedef BOOL(WINAPI
*HEAP32NEXT
) (LPHEAPENTRY32
);
177 typedef BOOL(WINAPI
*HEAP32LIST
) (HANDLE
, LPHEAPLIST32
);
178 typedef BOOL(WINAPI
*PROCESS32
) (HANDLE
, LPPROCESSENTRY32
);
179 typedef BOOL(WINAPI
*THREAD32
) (HANDLE
, LPTHREADENTRY32
);
180 typedef BOOL(WINAPI
*MODULE32
) (HANDLE
, LPMODULEENTRY32
);
183 # include <lmstats.h>
185 * The NET API is Unicode only. It requires the use of the UNICODE macro.
186 * When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was was added to the
187 * Platform SDK to allow the NET API to be used in non-Unicode applications
188 * provided that Unicode strings were still used for input. LMSTR is defined
191 typedef NET_API_STATUS(NET_API_FUNCTION
*NETSTATGET
)
192 (LPWSTR
, LPWSTR
, DWORD
, DWORD
, LPBYTE
*);
193 typedef NET_API_STATUS(NET_API_FUNCTION
*NETFREE
) (LPBYTE
);
194 # endif /* !OPENSSL_SYS_WINCE */
199 HCRYPTPROV hProvider
= 0;
203 # if defined(OPENSSL_SYS_WINCE)
204 # if defined(_WIN32_WCE) && _WIN32_WCE>=300
206 * Even though MSDN says _WIN32_WCE>=210, it doesn't seem to be available
207 * in commonly available implementations prior 300...
211 /* poll the CryptoAPI PRNG */
212 /* The CryptoAPI returns sizeof(buf) bytes of randomness */
213 if (CryptAcquireContextW(&hProvider
, NULL
, NULL
, PROV_RSA_FULL
,
214 CRYPT_VERIFYCONTEXT
)) {
215 if (CryptGenRandom(hProvider
, sizeof(buf
), buf
))
216 RAND_add(buf
, sizeof(buf
), sizeof(buf
));
217 CryptReleaseContext(hProvider
, 0);
221 # else /* OPENSSL_SYS_WINCE */
223 * None of below libraries are present on Windows CE, which is
224 * why we #ifndef the whole section. This also excuses us from
225 * handling the GetProcAddress issue. The trouble is that in
226 * real Win32 API GetProcAddress is available in ANSI flavor
227 * only. In WinCE on the other hand GetProcAddress is a macro
228 * most commonly defined as GetProcAddressW, which accepts
229 * Unicode argument. If we were to call GetProcAddress under
230 * WinCE, I'd recommend to either redefine GetProcAddress as
231 * GetProcAddressA (there seem to be one in common CE spec) or
232 * implement own shim routine, which would accept ANSI argument
233 * and expand it to Unicode.
236 /* load functions dynamically - not available on all systems */
237 HMODULE advapi
= LoadLibrary(TEXT("ADVAPI32.DLL"));
238 HMODULE kernel
= LoadLibrary(TEXT("KERNEL32.DLL"));
240 HMODULE netapi
= LoadLibrary(TEXT("NETAPI32.DLL"));
241 CRYPTACQUIRECONTEXTW acquire
= NULL
;
242 CRYPTGENRANDOM gen
= NULL
;
243 CRYPTRELEASECONTEXT release
= NULL
;
244 NETSTATGET netstatget
= NULL
;
245 NETFREE netfree
= NULL
;
250 (NETSTATGET
) GetProcAddress(netapi
, "NetStatisticsGet");
251 netfree
= (NETFREE
) GetProcAddress(netapi
, "NetApiBufferFree");
254 if (netstatget
&& netfree
) {
257 * NetStatisticsGet() is a Unicode only function
258 * STAT_WORKSTATION_0 contains 45 fields and STAT_SERVER_0
259 * contains 17 fields. We treat each field as a source of one
263 if (netstatget(NULL
, L
"LanmanWorkstation", 0, 0, &outbuf
) == 0) {
264 RAND_add(outbuf
, sizeof(STAT_WORKSTATION_0
), 45);
267 if (netstatget(NULL
, L
"LanmanServer", 0, 0, &outbuf
) == 0) {
268 RAND_add(outbuf
, sizeof(STAT_SERVER_0
), 17);
277 * It appears like this can cause an exception deep within
278 * ADVAPI32.DLL at random times on Windows 2000. Reported by Jeffrey
279 * Altman. Only use it on NT.
284 * If it's available, then it's available in both ANSI
285 * and UNICODE flavors even in Win9x, documentation says.
286 * We favor Unicode...
288 acquire
= (CRYPTACQUIRECONTEXTW
) GetProcAddress(advapi
,
289 "CryptAcquireContextW");
290 gen
= (CRYPTGENRANDOM
) GetProcAddress(advapi
, "CryptGenRandom");
291 release
= (CRYPTRELEASECONTEXT
) GetProcAddress(advapi
,
292 "CryptReleaseContext");
295 if (acquire
&& gen
&& release
) {
296 /* poll the CryptoAPI PRNG */
297 /* The CryptoAPI returns sizeof(buf) bytes of randomness */
298 if (acquire(&hProvider
, NULL
, NULL
, PROV_RSA_FULL
,
299 CRYPT_VERIFYCONTEXT
)) {
300 if (gen(hProvider
, sizeof(buf
), buf
) != 0) {
301 RAND_add(buf
, sizeof(buf
), 0);
304 release(hProvider
, 0);
307 /* poll the Pentium PRG with CryptoAPI */
308 if (acquire(&hProvider
, 0, INTEL_DEF_PROV
, PROV_INTEL_SEC
, 0)) {
309 if (gen(hProvider
, sizeof(buf
), buf
) != 0) {
310 RAND_add(buf
, sizeof(buf
), sizeof(buf
));
313 release(hProvider
, 0);
320 if ((!check_winnt() ||
321 !OPENSSL_isservice()) &&
322 (user
= LoadLibrary(TEXT("USER32.DLL")))) {
323 GETCURSORINFO cursor
;
324 GETFOREGROUNDWINDOW win
;
325 GETQUEUESTATUS queue
;
328 (GETFOREGROUNDWINDOW
) GetProcAddress(user
,
329 "GetForegroundWindow");
330 cursor
= (GETCURSORINFO
) GetProcAddress(user
, "GetCursorInfo");
331 queue
= (GETQUEUESTATUS
) GetProcAddress(user
, "GetQueueStatus");
336 RAND_add(&h
, sizeof(h
), 0);
340 * unfortunately, its not safe to call GetCursorInfo() on NT4
341 * even though it exists in SP3 (or SP6) and higher.
343 if (check_winnt() && !check_win_minplat(5))
347 /* cursor position */
348 /* assume 2 bytes of entropy */
350 ci
.cbSize
= sizeof(CURSORINFO
);
352 RAND_add(&ci
, ci
.cbSize
, 2);
356 /* message queue status */
357 /* assume 1 byte of entropy */
358 w
= queue(QS_ALLEVENTS
);
359 RAND_add(&w
, sizeof(w
), 1);
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)
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.)
380 CREATETOOLHELP32SNAPSHOT snap
;
381 CLOSETOOLHELP32SNAPSHOT close_snap
;
384 HEAP32FIRST heap_first
;
385 HEAP32NEXT heap_next
;
386 HEAP32LIST heaplist_first
, heaplist_next
;
387 PROCESS32 process_first
, process_next
;
388 THREAD32 thread_first
, thread_next
;
389 MODULE32 module_first
, module_next
;
398 snap
= (CREATETOOLHELP32SNAPSHOT
)
399 GetProcAddress(kernel
, "CreateToolhelp32Snapshot");
400 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
401 GetProcAddress(kernel
, "CloseToolhelp32Snapshot");
402 heap_first
= (HEAP32FIRST
) GetProcAddress(kernel
, "Heap32First");
403 heap_next
= (HEAP32NEXT
) GetProcAddress(kernel
, "Heap32Next");
405 (HEAP32LIST
) GetProcAddress(kernel
, "Heap32ListFirst");
407 (HEAP32LIST
) GetProcAddress(kernel
, "Heap32ListNext");
409 (PROCESS32
) GetProcAddress(kernel
, "Process32First");
411 (PROCESS32
) GetProcAddress(kernel
, "Process32Next");
412 thread_first
= (THREAD32
) GetProcAddress(kernel
, "Thread32First");
413 thread_next
= (THREAD32
) GetProcAddress(kernel
, "Thread32Next");
414 module_first
= (MODULE32
) GetProcAddress(kernel
, "Module32First");
415 module_next
= (MODULE32
) GetProcAddress(kernel
, "Module32Next");
417 if (snap
&& heap_first
&& heap_next
&& heaplist_first
&&
418 heaplist_next
&& process_first
&& process_next
&&
419 thread_first
&& thread_next
&& module_first
&&
420 module_next
&& (handle
= snap(TH32CS_SNAPALL
, 0))
421 != INVALID_HANDLE_VALUE
) {
422 /* heap list and heap walking */
424 * HEAPLIST32 contains 3 fields that will change with each
425 * entry. Consider each field a source of 1 byte of entropy.
426 * HEAPENTRY32 contains 5 fields that will change with each
427 * entry. Consider each field a source of 1 byte of entropy.
429 ZeroMemory(&hlist
, sizeof(HEAPLIST32
));
430 hlist
.dwSize
= sizeof(HEAPLIST32
);
432 starttime
= GetTickCount();
434 if (heaplist_first(handle
, &hlist
)) {
436 * following discussion on dev ML, exception on WinCE (or
437 * other Win platform) is theoretically of unknown
438 * origin; prevent infinite loop here when this
439 * theoretical case occurs; otherwise cope with the
440 * expected (MSDN documented) exception-throwing
441 * behaviour of Heap32Next() on WinCE.
443 * based on patch in original message by Tanguy Fautré
444 * (2009/03/02) Subject: RAND_poll() and
445 * CreateToolhelp32Snapshot() stability
447 int ex_cnt_limit
= 42;
449 RAND_add(&hlist
, hlist
.dwSize
, 3);
451 ZeroMemory(&hentry
, sizeof(HEAPENTRY32
));
452 hentry
.dwSize
= sizeof(HEAPENTRY32
);
453 if (heap_first(&hentry
,
458 RAND_add(&hentry
, hentry
.dwSize
, 5);
459 while (heap_next(&hentry
)
461 || (GetTickCount() - starttime
) <
466 __except(EXCEPTION_EXECUTE_HANDLER
) {
468 * ignore access violations when walking the heap
473 } while (heaplist_next(handle
, &hlist
)
475 || (GetTickCount() - starttime
) < MAXDELAY
)
476 && ex_cnt_limit
> 0);
479 if (heaplist_first(handle
, &hlist
)) {
481 RAND_add(&hlist
, hlist
.dwSize
, 3);
482 hentry
.dwSize
= sizeof(HEAPENTRY32
);
483 if (heap_first(&hentry
,
488 RAND_add(&hentry
, hentry
.dwSize
, 5);
489 while (heap_next(&hentry
)
492 } while (heaplist_next(handle
, &hlist
)
494 || (GetTickCount() - starttime
) < MAXDELAY
));
498 /* process walking */
500 * PROCESSENTRY32 contains 9 fields that will change with
501 * each entry. Consider each field a source of 1 byte of
504 p
.dwSize
= sizeof(PROCESSENTRY32
);
507 starttime
= GetTickCount();
508 if (process_first(handle
, &p
))
510 RAND_add(&p
, p
.dwSize
, 9);
511 while (process_next(handle
, &p
)
513 || (GetTickCount() - starttime
) < MAXDELAY
));
517 * THREADENTRY32 contains 6 fields that will change with each
518 * entry. Consider each field a source of 1 byte of entropy.
520 t
.dwSize
= sizeof(THREADENTRY32
);
522 starttime
= GetTickCount();
523 if (thread_first(handle
, &t
))
525 RAND_add(&t
, t
.dwSize
, 6);
526 while (thread_next(handle
, &t
)
528 || (GetTickCount() - starttime
) < MAXDELAY
));
532 * MODULEENTRY32 contains 9 fields that will change with each
533 * entry. Consider each field a source of 1 byte of entropy.
535 m
.dwSize
= sizeof(MODULEENTRY32
);
537 starttime
= GetTickCount();
538 if (module_first(handle
, &m
))
540 RAND_add(&m
, m
.dwSize
, 9);
541 while (module_next(handle
, &m
)
543 || (GetTickCount() - starttime
) < MAXDELAY
));
554 # endif /* !OPENSSL_SYS_WINCE */
559 /* memory usage statistics */
560 GlobalMemoryStatus(&mst
);
561 RAND_add(&mst
, sizeof(mst
), 1);
564 w
= GetCurrentProcessId();
565 RAND_add(&w
, sizeof(w
), 1);
570 int RAND_event(UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
572 double add_entropy
= 0;
585 static int lastx
, lasty
, lastdx
, lastdy
;
592 if (dx
!= 0 && dy
!= 0 && dx
- lastdx
!= 0 && dy
- lastdy
!= 0)
594 lastx
= x
, lasty
= y
;
595 lastdx
= dx
, lastdy
= dy
;
601 RAND_add(&iMsg
, sizeof(iMsg
), add_entropy
);
602 RAND_add(&wParam
, sizeof(wParam
), 0);
603 RAND_add(&lParam
, sizeof(lParam
), 0);
605 return (RAND_status());
608 void RAND_screen(void)
609 { /* function available for backward
615 /* feed timing information to the PRNG */
616 static void readtimer(void)
620 static int have_perfc
= 1;
621 # if defined(_MSC_VER) && defined(_M_X86)
622 static int have_tsc
= 1;
628 _emit
0x0f _emit
0x31 mov cyclecount
, eax
}
629 RAND_add(&cyclecount
, sizeof(cyclecount
), 1);
631 __except(EXCEPTION_EXECUTE_HANDLER
) {
640 if (QueryPerformanceCounter(&l
) == 0)
643 RAND_add(&l
, sizeof(l
), 0);
646 if (!have_tsc
&& !have_perfc
) {
648 RAND_add(&w
, sizeof(w
), 0);
652 /* feed screen contents to PRNG */
653 /*****************************************************************************
655 * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
658 * <URL:http://support.microsoft.com/default.aspx?scid=kb;[LN];97193>;
659 * the original copyright message is:
661 * (C) Copyright Microsoft Corp. 1993. All rights reserved.
663 * You have a royalty-free right to use, modify, reproduce and
664 * distribute the Sample Files (and/or any modified version) in
665 * any way you find useful, provided that you agree that
666 * Microsoft has no warranty obligations or liability for any
667 * Sample Application Files which are modified.
670 static void readscreen(void)
672 # if !defined(OPENSSL_SYS_WINCE) && !defined(OPENSSL_SYS_WIN32_CYGWIN)
673 HDC hScrDC
; /* screen DC */
674 HBITMAP hBitmap
; /* handle for our bitmap */
675 BITMAP bm
; /* bitmap properties */
676 unsigned int size
; /* size of bitmap */
677 char *bmbits
; /* contents of bitmap */
678 int w
; /* screen width */
679 int h
; /* screen height */
680 int y
; /* y-coordinate of screen lines to grab */
681 int n
= 16; /* number of screen lines to grab at a time */
682 BITMAPINFOHEADER bi
; /* info about the bitmap */
684 if (check_winnt() && OPENSSL_isservice() > 0)
687 /* Get a reference to the screen DC */
688 hScrDC
= GetDC(NULL
);
690 /* Get screen resolution */
691 w
= GetDeviceCaps(hScrDC
, HORZRES
);
692 h
= GetDeviceCaps(hScrDC
, VERTRES
);
694 /* Create a bitmap compatible with the screen DC */
695 hBitmap
= CreateCompatibleBitmap(hScrDC
, w
, n
);
697 /* Get bitmap properties */
698 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
) & bm
);
699 size
= (unsigned int)bm
.bmWidthBytes
* bm
.bmHeight
* bm
.bmPlanes
;
701 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
702 bi
.biWidth
= bm
.bmWidth
;
703 bi
.biHeight
= bm
.bmHeight
;
704 bi
.biPlanes
= bm
.bmPlanes
;
705 bi
.biBitCount
= bm
.bmBitsPixel
;
706 bi
.biCompression
= BI_RGB
;
708 bi
.biXPelsPerMeter
= 0;
709 bi
.biYPelsPerMeter
= 0;
711 bi
.biClrImportant
= 0;
713 bmbits
= OPENSSL_malloc(size
);
714 if (bmbits
!= NULL
) {
715 /* Now go through the whole screen, repeatedly grabbing n lines */
716 for (y
= 0; y
< h
- n
; y
+= n
) {
717 unsigned char md
[MD_DIGEST_LENGTH
];
719 /* Copy the bits of the current line range into the buffer */
720 GetDIBits(hScrDC
, hBitmap
, y
, n
,
721 bmbits
, (BITMAPINFO
*) & bi
, DIB_RGB_COLORS
);
723 /* Get the hash of the bitmap */
724 MD(bmbits
, size
, md
);
726 /* Seed the random generator with the hash value */
727 RAND_add(md
, MD_DIGEST_LENGTH
, 0);
730 OPENSSL_free(bmbits
);
734 DeleteObject(hBitmap
);
735 ReleaseDC(NULL
, hScrDC
);
736 # endif /* !OPENSSL_SYS_WINCE */