1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* crypto/rand/rand_win.c */
2656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * All rights reserved.
4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This package is an SSL implementation written
6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * by Eric Young (eay@cryptsoft.com).
7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The implementation was written so as to conform with Netscapes SSL.
8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This library is free for commercial and non-commercial use as long as
10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the following conditions are aheared to.  The following conditions
11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * apply to all code found in this distribution, be it the RC4, RSA,
12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * included with this distribution is covered by the same copyright terms
14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright remains Eric Young's, and as such any Copyright notices in
17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the code are not to be removed.
18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * If this package is used in a product, Eric Young should be given attribution
19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * as the author of the parts of the library used.
20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This can be in the form of a textual message at program startup or
21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * in documentation (online or textual) provided with the package.
22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the copyright
27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    documentation and/or other materials provided with the distribution.
31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this software
32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    must display the following acknowledgement:
33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes cryptographic software written by
34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *     Eric Young (eay@cryptsoft.com)"
35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    The word 'cryptographic' can be left out if the rouines from the library
36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    being used are not cryptographic related :-).
37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. If you include any Windows specific code (or a derivative thereof) from
38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the apps directory (application code) you must include an acknowledgement:
39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SUCH DAMAGE.
52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The licence and distribution terms for any publically available version or
54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * derivative of this code cannot be changed.  i.e. this code cannot simply be
55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * copied and put under another distribution licence
56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * [including the GNU Public Licence.]
57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* ====================================================================
59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the above copyright
66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in
70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the documentation and/or other materials provided with the
71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    distribution.
72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this
74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    software must display the following acknowledgment:
75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    endorse or promote products derived from this software without
80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    prior written permission. For written permission, please contact
81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    openssl-core@openssl.org.
82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5. Products derived from this software may not be called "OpenSSL"
84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    nor may "OpenSSL" appear in their names without prior written
85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    permission of the OpenSSL Project.
86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 6. Redistributions of any form whatsoever must retain the following
88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    acknowledgment:
89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OF THE POSSIBILITY OF SUCH DAMAGE.
104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ====================================================================
105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This product includes cryptographic software written by Eric Young
107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * (eay@cryptsoft.com).  This product includes software written by Tim
108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Hudson (tjh@cryptsoft.com).
109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "cryptlib.h"
113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/rand.h>
114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "rand_lcl.h"
115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <windows.h>
118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef _WIN32_WINNT
119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define _WIN32_WINNT 0x0400
120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <wincrypt.h>
122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <tlhelp32.h>
123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Limit the time spent walking through the heap, processes, threads and modules to
125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project   a maximum of 1000 miliseconds each, unless CryptoGenRandom failed */
126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define MAXDELAY 1000
127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Intel hardware RNG CSP -- available from
129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * http://developer.intel.com/design/security/rng/redist_license.htm
130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define PROV_INTEL_SEC 22
132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void readtimer(void);
135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void readscreen(void);
136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined
138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project   when WINVER is 0x0500 and up, which currently only happens on Win2000.
139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project   Unfortunately, those are typedefs, so they're a little bit difficult to
140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project   detect properly.  On the other hand, the macro CURSOR_SHOWING is defined
141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project   within the same conditional, so it can be use to detect the absence of said
142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project   typedefs. */
143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef CURSOR_SHOWING
145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/*
146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Information about the global cursor.
147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef struct tagCURSORINFO
149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    DWORD   cbSize;
151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    DWORD   flags;
152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    HCURSOR hCursor;
153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    POINT   ptScreenPos;
154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define CURSOR_SHOWING     0x00000001
157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif /* CURSOR_SHOWING */
158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if !defined(OPENSSL_SYS_WINCE)
160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *CRYPTACQUIRECONTEXTW)(HCRYPTPROV *, LPCWSTR, LPCWSTR,
161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				    DWORD, DWORD);
162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *);
163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD);
164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef HWND (WINAPI *GETFOREGROUNDWINDOW)(VOID);
166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *GETCURSORINFO)(PCURSORINFO);
167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef DWORD (WINAPI *GETQUEUESTATUS)(UINT);
168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *CLOSETOOLHELP32SNAPSHOT)(HANDLE);
171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *HEAP32FIRST)(LPHEAPENTRY32, DWORD, size_t);
172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *HEAP32NEXT)(LPHEAPENTRY32);
173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *HEAP32LIST)(HANDLE, LPHEAPLIST32);
174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *PROCESS32)(HANDLE, LPPROCESSENTRY32);
175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *THREAD32)(HANDLE, LPTHREADENTRY32);
176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef BOOL (WINAPI *MODULE32)(HANDLE, LPMODULEENTRY32);
177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <lmcons.h>
179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <lmstats.h>
180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 1 /* The NET API is Unicode only.  It requires the use of the UNICODE
181656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project       * macro.  When UNICODE is defined LPTSTR becomes LPWSTR.  LMSTR was
182656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project       * was added to the Platform SDK to allow the NET API to be used in
183656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project       * non-Unicode applications provided that Unicode strings were still
184656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project       * used for input.  LMSTR is defined as LPWSTR.
185656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project       */
186656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef NET_API_STATUS (NET_API_FUNCTION * NETSTATGET)
187656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE*);
188656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecttypedef NET_API_STATUS (NET_API_FUNCTION * NETFREE)(LPBYTE);
189656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif /* 1 */
190656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif /* !OPENSSL_SYS_WINCE */
191656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
192656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint RAND_poll(void)
193656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
194656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	MEMORYSTATUS m;
195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	HCRYPTPROV hProvider = 0;
196656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	DWORD w;
197656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int good = 0;
198656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
199656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Determine the OS version we are on so we can turn off things
200656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * that do not work properly.
201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 */
202656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        OSVERSIONINFO osverinfo ;
203656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
204656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        GetVersionEx( &osverinfo ) ;
205656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
206656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(OPENSSL_SYS_WINCE)
207656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# if defined(_WIN32_WCE) && _WIN32_WCE>=300
208656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Even though MSDN says _WIN32_WCE>=210, it doesn't seem to be available
209656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * in commonly available implementations prior 300... */
210656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
211656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	BYTE buf[64];
212656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* poll the CryptoAPI PRNG */
213656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* The CryptoAPI returns sizeof(buf) bytes of randomness */
214656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
215656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				CRYPT_VERIFYCONTEXT))
216656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
217656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (CryptGenRandom(hProvider, sizeof(buf), buf))
218656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(buf, sizeof(buf), sizeof(buf));
219656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		CryptReleaseContext(hProvider, 0);
220656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
221656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
222656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# endif
223656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else	/* OPENSSL_SYS_WINCE */
224656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/*
225656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * None of below libraries are present on Windows CE, which is
226656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * why we #ifndef the whole section. This also excuses us from
227656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * handling the GetProcAddress issue. The trouble is that in
228656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * real Win32 API GetProcAddress is available in ANSI flavor
229656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * only. In WinCE on the other hand GetProcAddress is a macro
230656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * most commonly defined as GetProcAddressW, which accepts
231656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * Unicode argument. If we were to call GetProcAddress under
232656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * WinCE, I'd recommend to either redefine GetProcAddress as
233656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * GetProcAddressA (there seem to be one in common CE spec) or
234656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * implement own shim routine, which would accept ANSI argument
235656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * and expand it to Unicode.
236656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 */
237656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
238656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* load functions dynamically - not available on all systems */
239656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	HMODULE advapi = LoadLibrary(TEXT("ADVAPI32.DLL"));
240656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	HMODULE kernel = LoadLibrary(TEXT("KERNEL32.DLL"));
241656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	HMODULE user = NULL;
242656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	HMODULE netapi = LoadLibrary(TEXT("NETAPI32.DLL"));
243656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	CRYPTACQUIRECONTEXTW acquire = NULL;
244656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	CRYPTGENRANDOM gen = NULL;
245656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	CRYPTRELEASECONTEXT release = NULL;
246656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	NETSTATGET netstatget = NULL;
247656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	NETFREE netfree = NULL;
248656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	BYTE buf[64];
249656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
250656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (netapi)
251656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
252656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		netstatget = (NETSTATGET) GetProcAddress(netapi,"NetStatisticsGet");
253656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		netfree = (NETFREE) GetProcAddress(netapi,"NetApiBufferFree");
254656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
255656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
256656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (netstatget && netfree)
257656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
258656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		LPBYTE outbuf;
259656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* NetStatisticsGet() is a Unicode only function
260656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 		 * STAT_WORKSTATION_0 contains 45 fields and STAT_SERVER_0
261656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * contains 17 fields.  We treat each field as a source of
262656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * one byte of entropy.
263656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                 */
264656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
265656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (netstatget(NULL, L"LanmanWorkstation", 0, 0, &outbuf) == 0)
266656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
267656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(outbuf, sizeof(STAT_WORKSTATION_0), 45);
268656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			netfree(outbuf);
269656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
270656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (netstatget(NULL, L"LanmanServer", 0, 0, &outbuf) == 0)
271656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
272656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(outbuf, sizeof(STAT_SERVER_0), 17);
273656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			netfree(outbuf);
274656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
275656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
276656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
277656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (netapi)
278656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		FreeLibrary(netapi);
279656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
280656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        /* It appears like this can cause an exception deep within ADVAPI32.DLL
281656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project         * at random times on Windows 2000.  Reported by Jeffrey Altman.
282656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project         * Only use it on NT.
283656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 */
284656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Wolfgang Marczy <WMarczy@topcall.co.at> reports that
285656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * the RegQueryValueEx call below can hang on NT4.0 (SP6).
286656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * So we don't use this at all for now. */
287656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 0
288656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
289656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		osverinfo.dwMajorVersion < 5)
290656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
291656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* Read Performance Statistics from NT/2000 registry
292656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * The size of the performance data can vary from call
293656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * to call so we must guess the size of the buffer to use
294656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * and increase its size if we get an ERROR_MORE_DATA
295656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * return instead of ERROR_SUCCESS.
296656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 */
297656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		LONG   rc=ERROR_MORE_DATA;
298656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		char * buf=NULL;
299656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		DWORD bufsz=0;
300656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		DWORD length;
301656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
302656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		while (rc == ERROR_MORE_DATA)
303656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
304656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			buf = realloc(buf,bufsz+8192);
305656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!buf)
306656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				break;
307656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			bufsz += 8192;
308656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
309656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			length = bufsz;
310656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			rc = RegQueryValueEx(HKEY_PERFORMANCE_DATA, TEXT("Global"),
311656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				NULL, NULL, buf, &length);
312656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
313656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (rc == ERROR_SUCCESS)
314656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
315656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* For entropy count assume only least significant
316656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 * byte of each DWORD is random.
317656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 */
318656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(&length, sizeof(length), 0);
319656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(buf, length, length / 4.0);
320656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
321656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* Close the Registry Key to allow Windows to cleanup/close
322656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 * the open handle
323656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 * Note: The 'HKEY_PERFORMANCE_DATA' key is implicitly opened
324656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 *       when the RegQueryValueEx above is done.  However, if
325656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 *       it is not explicitly closed, it can cause disk
326656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 *       partition manipulation problems.
327656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 */
328656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RegCloseKey(HKEY_PERFORMANCE_DATA);
329656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
330656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (buf)
331656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			free(buf);
332656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
333656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
334656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
335656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (advapi)
336656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
337656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/*
338656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * If it's available, then it's available in both ANSI
339656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * and UNICODE flavors even in Win9x, documentation says.
340656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 * We favor Unicode...
341656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		 */
342656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		acquire = (CRYPTACQUIRECONTEXTW) GetProcAddress(advapi,
343656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			"CryptAcquireContextW");
344656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		gen = (CRYPTGENRANDOM) GetProcAddress(advapi,
345656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			"CryptGenRandom");
346656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
347656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			"CryptReleaseContext");
348656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
349656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
350656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (acquire && gen && release)
351656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
352656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* poll the CryptoAPI PRNG */
353656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                /* The CryptoAPI returns sizeof(buf) bytes of randomness */
354656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (acquire(&hProvider, NULL, NULL, PROV_RSA_FULL,
355656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			CRYPT_VERIFYCONTEXT))
356656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
357656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (gen(hProvider, sizeof(buf), buf) != 0)
358656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				{
359656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				RAND_add(buf, sizeof(buf), 0);
360656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				good = 1;
361656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 0
362656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				printf("randomness from PROV_RSA_FULL\n");
363656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
364656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				}
365656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			release(hProvider, 0);
366656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
367656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
368656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* poll the Pentium PRG with CryptoAPI */
369656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (acquire(&hProvider, 0, INTEL_DEF_PROV, PROV_INTEL_SEC, 0))
370656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
371656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (gen(hProvider, sizeof(buf), buf) != 0)
372656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				{
373656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				RAND_add(buf, sizeof(buf), sizeof(buf));
374656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				good = 1;
375656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 0
376656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				printf("randomness from PROV_INTEL_SEC\n");
377656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
378656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				}
379656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			release(hProvider, 0);
380656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
381656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
382656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
383656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        if (advapi)
384656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		FreeLibrary(advapi);
385656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
386656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if ((osverinfo.dwPlatformId != VER_PLATFORM_WIN32_NT ||
387656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	     !OPENSSL_isservice()) &&
388656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	    (user = LoadLibrary(TEXT("USER32.DLL"))))
389656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
390656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		GETCURSORINFO cursor;
391656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		GETFOREGROUNDWINDOW win;
392656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		GETQUEUESTATUS queue;
393656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
394656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		win = (GETFOREGROUNDWINDOW) GetProcAddress(user, "GetForegroundWindow");
395656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		cursor = (GETCURSORINFO) GetProcAddress(user, "GetCursorInfo");
396656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		queue = (GETQUEUESTATUS) GetProcAddress(user, "GetQueueStatus");
397656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
398656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (win)
399656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
400656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* window handle */
401656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			HWND h = win();
402656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(&h, sizeof(h), 0);
403656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
404656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (cursor)
405656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
406656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* unfortunately, its not safe to call GetCursorInfo()
407656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 * on NT4 even though it exists in SP3 (or SP6) and
408656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 * higher.
409656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			 */
410656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
411656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				osverinfo.dwMajorVersion < 5)
412656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				cursor = 0;
413656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
414656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (cursor)
415656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
416656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* cursor position */
417656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* assume 2 bytes of entropy */
418656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			CURSORINFO ci;
419656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			ci.cbSize = sizeof(CURSORINFO);
420656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (cursor(&ci))
421656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				RAND_add(&ci, ci.cbSize, 2);
422656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
423656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
424656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (queue)
425656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
426656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* message queue status */
427656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* assume 1 byte of entropy */
428656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			w = queue(QS_ALLEVENTS);
429656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			RAND_add(&w, sizeof(w), 1);
430656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
431656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
432656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		FreeLibrary(user);
433656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
434656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
435656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Toolhelp32 snapshot: enumerate processes, threads, modules and heap
436656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * http://msdn.microsoft.com/library/psdk/winbase/toolhelp_5pfd.htm
437656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * (Win 9x and 2000 only, not available on NT)
438656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 *
439656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * This seeding method was proposed in Peter Gutmann, Software
440656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * Generation of Practically Strong Random Numbers,
441656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * http://www.usenix.org/publications/library/proceedings/sec98/gutmann.html
442656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * revised version at http://www.cryptoengines.com/~peter/06_random.pdf
443656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * (The assignment of entropy estimates below is arbitrary, but based
444656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * on Peter's analysis the full poll appears to be safe. Additional
445656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * interactive seeding is encouraged.)
446656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 */
447656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
448656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (kernel)
449656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
450656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		CREATETOOLHELP32SNAPSHOT snap;
451656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		CLOSETOOLHELP32SNAPSHOT close_snap;
452656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		HANDLE handle;
453656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
454656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		HEAP32FIRST heap_first;
455656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		HEAP32NEXT heap_next;
456656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		HEAP32LIST heaplist_first, heaplist_next;
457656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		PROCESS32 process_first, process_next;
458656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		THREAD32 thread_first, thread_next;
459656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		MODULE32 module_first, module_next;
460656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
461656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		HEAPLIST32 hlist;
462656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		HEAPENTRY32 hentry;
463656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		PROCESSENTRY32 p;
464656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		THREADENTRY32 t;
465656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		MODULEENTRY32 m;
46698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom		DWORD starttime = 0;
467656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
468656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		snap = (CREATETOOLHELP32SNAPSHOT)
469656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			GetProcAddress(kernel, "CreateToolhelp32Snapshot");
470656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		close_snap = (CLOSETOOLHELP32SNAPSHOT)
471656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			GetProcAddress(kernel, "CloseToolhelp32Snapshot");
472656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		heap_first = (HEAP32FIRST) GetProcAddress(kernel, "Heap32First");
473656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		heap_next = (HEAP32NEXT) GetProcAddress(kernel, "Heap32Next");
474656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		heaplist_first = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListFirst");
475656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		heaplist_next = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListNext");
476656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		process_first = (PROCESS32) GetProcAddress(kernel, "Process32First");
477656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		process_next = (PROCESS32) GetProcAddress(kernel, "Process32Next");
478656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		thread_first = (THREAD32) GetProcAddress(kernel, "Thread32First");
479656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		thread_next = (THREAD32) GetProcAddress(kernel, "Thread32Next");
480656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		module_first = (MODULE32) GetProcAddress(kernel, "Module32First");
481656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		module_next = (MODULE32) GetProcAddress(kernel, "Module32Next");
482656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
483656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (snap && heap_first && heap_next && heaplist_first &&
484656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			heaplist_next && process_first && process_next &&
485656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			thread_first && thread_next && module_first &&
486656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			module_next && (handle = snap(TH32CS_SNAPALL,0))
487656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			!= INVALID_HANDLE_VALUE)
488656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
489656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* heap list and heap walking */
490656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* HEAPLIST32 contains 3 fields that will change with
491656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * each entry.  Consider each field a source of 1 byte
492656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * of entropy.
493656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * HEAPENTRY32 contains 5 fields that will change with
494656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * each entry.  Consider each field a source of 1 byte
495656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * of entropy.
496656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         */
49798d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			ZeroMemory(&hlist, sizeof(HEAPLIST32));
498656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			hlist.dwSize = sizeof(HEAPLIST32);
49998d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			if (good) starttime = GetTickCount();
50098d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom#ifdef _MSC_VER
501656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (heaplist_first(handle, &hlist))
50298d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				{
50398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				/*
50498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				   following discussion on dev ML, exception on WinCE (or other Win
50598d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				   platform) is theoretically of unknown origin; prevent infinite
50698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				   loop here when this theoretical case occurs; otherwise cope with
50798d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				   the expected (MSDN documented) exception-throwing behaviour of
50898d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				   Heap32Next() on WinCE.
50998d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom
51098d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				   based on patch in original message by Tanguy Fautré (2009/03/02)
51198d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			           Subject: RAND_poll() and CreateToolhelp32Snapshot() stability
51298d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			     */
51398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				int ex_cnt_limit = 42;
51498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				do
51598d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					{
51698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					RAND_add(&hlist, hlist.dwSize, 3);
51798d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					__try
51898d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						{
51998d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						ZeroMemory(&hentry, sizeof(HEAPENTRY32));
52098d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					hentry.dwSize = sizeof(HEAPENTRY32);
52198d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					if (heap_first(&hentry,
52298d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						hlist.th32ProcessID,
52398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						hlist.th32HeapID))
52498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						{
52598d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						int entrycnt = 80;
52698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						do
52798d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom							RAND_add(&hentry,
52898d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom								hentry.dwSize, 5);
52998d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						while (heap_next(&hentry)
53098d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						&& (!good || (GetTickCount()-starttime)<MAXDELAY)
53198d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom							&& --entrycnt > 0);
53298d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						}
53398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						}
53498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					__except (EXCEPTION_EXECUTE_HANDLER)
53598d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						{
53698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom							/* ignore access violations when walking the heap list */
53798d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom							ex_cnt_limit--;
53898d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						}
53998d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					} while (heaplist_next(handle, &hlist)
54098d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						&& (!good || (GetTickCount()-starttime)<MAXDELAY)
54198d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						&& ex_cnt_limit > 0);
54298d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				}
54398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom
54498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom#else
54598d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			if (heaplist_first(handle, &hlist))
54698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				{
547656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				do
548656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					{
549656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					RAND_add(&hlist, hlist.dwSize, 3);
550656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					hentry.dwSize = sizeof(HEAPENTRY32);
551656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					if (heap_first(&hentry,
552656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						hlist.th32ProcessID,
553656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						hlist.th32HeapID))
554656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						{
555656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						int entrycnt = 80;
556656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						do
557656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project							RAND_add(&hentry,
558656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project								hentry.dwSize, 5);
559656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						while (heap_next(&hentry)
560656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project							&& --entrycnt > 0);
561656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project						}
56298d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					} while (heaplist_next(handle, &hlist)
56398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom						&& (!good || (GetTickCount()-starttime)<MAXDELAY));
56498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				}
56598d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom#endif
566656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
567656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* process walking */
568656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* PROCESSENTRY32 contains 9 fields that will change
569656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * with each entry.  Consider each field a source of
570656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * 1 byte of entropy.
571656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         */
572656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			p.dwSize = sizeof(PROCESSENTRY32);
573656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
57498d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			if (good) starttime = GetTickCount();
575656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (process_first(handle, &p))
576656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				do
577656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					RAND_add(&p, p.dwSize, 9);
57898d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				while (process_next(handle, &p) && (!good || (GetTickCount()-starttime)<MAXDELAY));
579656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
580656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* thread walking */
581656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* THREADENTRY32 contains 6 fields that will change
582656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * with each entry.  Consider each field a source of
583656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * 1 byte of entropy.
584656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         */
585656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t.dwSize = sizeof(THREADENTRY32);
58698d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			if (good) starttime = GetTickCount();
587656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (thread_first(handle, &t))
588656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				do
589656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					RAND_add(&t, t.dwSize, 6);
59098d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom				while (thread_next(handle, &t) && (!good || (GetTickCount()-starttime)<MAXDELAY));
591656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
592656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* module walking */
593656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        /* MODULEENTRY32 contains 9 fields that will change
594656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * with each entry.  Consider each field a source of
595656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         * 1 byte of entropy.
596656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                         */
597656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			m.dwSize = sizeof(MODULEENTRY32);
59898d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom			if (good) starttime = GetTickCount();
599656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (module_first(handle, &m))
600656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				do
601656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					RAND_add(&m, m.dwSize, 9);
602656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				while (module_next(handle, &m)
60398d58bb80c64b02a33662f0ea80351d4a1535267Brian Carlstrom					       	&& (!good || (GetTickCount()-starttime)<MAXDELAY));
604656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (close_snap)
605656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				close_snap(handle);
606656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			else
607656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				CloseHandle(handle);
608656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
609656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
610656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
611656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		FreeLibrary(kernel);
612656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
613656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
614656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif /* !OPENSSL_SYS_WINCE */
615656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
616656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* timer data */
617656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	readtimer();
618656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
619656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* memory usage statistics */
620656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	GlobalMemoryStatus(&m);
621656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	RAND_add(&m, sizeof(m), 1);
622656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
623656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* process ID */
624656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	w = GetCurrentProcessId();
625656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	RAND_add(&w, sizeof(w), 1);
626656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
627656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 0
628656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	printf("Exiting RAND_poll\n");
629656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
630656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
631656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return(1);
632656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
633656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
634656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
635656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        {
636656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        double add_entropy=0;
637656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
638656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        switch (iMsg)
639656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                {
640656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        case WM_KEYDOWN:
641656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        {
642656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        static WPARAM key;
643656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        if (key != wParam)
644656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                                add_entropy = 0.05;
645656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        key = wParam;
646656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        }
647656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        break;
648656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	case WM_MOUSEMOVE:
649656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        {
650656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        static int lastx,lasty,lastdx,lastdy;
651656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        int x,y,dx,dy;
652656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
653656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        x=LOWORD(lParam);
654656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        y=HIWORD(lParam);
655656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        dx=lastx-x;
656656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        dy=lasty-y;
657656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        if (dx != 0 && dy != 0 && dx-lastdx != 0 && dy-lastdy != 0)
658656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                                add_entropy=.2;
659656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        lastx=x, lasty=y;
660656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        lastdx=dx, lastdy=dy;
661656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project                        }
662656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		break;
663656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
664656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
665656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	readtimer();
666656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project        RAND_add(&iMsg, sizeof(iMsg), add_entropy);
667656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	RAND_add(&wParam, sizeof(wParam), 0);
668656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	RAND_add(&lParam, sizeof(lParam), 0);
669656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
670656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return (RAND_status());
671656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
672656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
673656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
674656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid RAND_screen(void) /* function available for backward compatibility */
675656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
676656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	RAND_poll();
677656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	readscreen();
678656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
679656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
680656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
681656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* feed timing information to the PRNG */
682656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void readtimer(void)
683656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
684656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	DWORD w;
685656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	LARGE_INTEGER l;
686656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	static int have_perfc = 1;
687656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(_MSC_VER) && defined(_M_X86)
688656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	static int have_tsc = 1;
689656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	DWORD cyclecount;
690656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
691656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (have_tsc) {
692656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  __try {
693656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	    __asm {
694656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	      _emit 0x0f
695656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	      _emit 0x31
696656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	      mov cyclecount, eax
697656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	      }
698656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	    RAND_add(&cyclecount, sizeof(cyclecount), 1);
699656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  } __except(EXCEPTION_EXECUTE_HANDLER) {
700656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	    have_tsc = 0;
701656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  }
702656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
703656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else
704656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define have_tsc 0
705656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
706656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
707656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (have_perfc) {
708656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  if (QueryPerformanceCounter(&l) == 0)
709656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	    have_perfc = 0;
710656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  else
711656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	    RAND_add(&l, sizeof(l), 0);
712656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
713656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
714656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (!have_tsc && !have_perfc) {
715656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  w = GetTickCount();
716656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	  RAND_add(&w, sizeof(w), 0);
717656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
718656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
719656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
720656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* feed screen contents to PRNG */
721656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/*****************************************************************************
722656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
723656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
724656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
725656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Code adapted from
726656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * <URL:http://support.microsoft.com/default.aspx?scid=kb;[LN];97193>;
727656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the original copyright message is:
728656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
729656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *   (C) Copyright Microsoft Corp. 1993.  All rights reserved.
730656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
731656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *   You have a royalty-free right to use, modify, reproduce and
732656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *   distribute the Sample Files (and/or any modified version) in
733656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *   any way you find useful, provided that you agree that
734656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *   Microsoft has no warranty obligations or liability for any
735656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *   Sample Application Files which are modified.
736656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
737656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
738656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void readscreen(void)
739656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
740656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if !defined(OPENSSL_SYS_WINCE) && !defined(OPENSSL_SYS_WIN32_CYGWIN)
741656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  HDC		hScrDC;		/* screen DC */
742656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  HDC		hMemDC;		/* memory DC */
743656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  HBITMAP	hBitmap;	/* handle for our bitmap */
744656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  HBITMAP	hOldBitmap;	/* handle for previous bitmap */
745656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  BITMAP	bm;		/* bitmap properties */
746656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  unsigned int	size;		/* size of bitmap */
747656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  char		*bmbits;	/* contents of bitmap */
748656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  int		w;		/* screen width */
749656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  int		h;		/* screen height */
750656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  int		y;		/* y-coordinate of screen lines to grab */
751656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  int		n = 16;		/* number of screen lines to grab at a time */
752656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
753221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom  if (GetVersion() < 0x80000000 && OPENSSL_isservice()>0)
754656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    return;
755656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
756656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Create a screen DC and a memory DC compatible to screen DC */
757656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
758656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  hMemDC = CreateCompatibleDC(hScrDC);
759656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
760656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Get screen resolution */
761656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  w = GetDeviceCaps(hScrDC, HORZRES);
762656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  h = GetDeviceCaps(hScrDC, VERTRES);
763656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
764656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Create a bitmap compatible with the screen DC */
765656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
766656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
767656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Select new bitmap into memory DC */
768656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  hOldBitmap = SelectObject(hMemDC, hBitmap);
769656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
770656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Get bitmap properties */
771656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
772656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
773656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
774656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  bmbits = OPENSSL_malloc(size);
775656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  if (bmbits) {
776656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    /* Now go through the whole screen, repeatedly grabbing n lines */
777656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    for (y = 0; y < h-n; y += n)
778656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    	{
779656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	unsigned char md[MD_DIGEST_LENGTH];
780656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
781656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Bitblt screen DC to memory DC */
782656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
783656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
784656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Copy bitmap bits from memory DC to bmbits */
785656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	GetBitmapBits(hBitmap, size, bmbits);
786656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
787656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Get the hash of the bitmap */
788656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	MD(bmbits,size,md);
789656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
790656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Seed the random generator with the hash value */
791656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	RAND_add(md, MD_DIGEST_LENGTH, 0);
792656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
793656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
794656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project    OPENSSL_free(bmbits);
795656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  }
796656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
797656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Select old bitmap back into memory DC */
798656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  hBitmap = SelectObject(hMemDC, hOldBitmap);
799656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
800656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  /* Clean up */
801656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  DeleteObject(hBitmap);
802656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  DeleteDC(hMemDC);
803656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project  DeleteDC(hScrDC);
804656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif /* !OPENSSL_SYS_WINCE */
805656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
806656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
807656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
808