2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/rand.h>
14 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
17 # define _WIN32_WINNT 0x0400
19 # include <wincrypt.h>
20 # include <tlhelp32.h>
23 * Limit the time spent walking through the heap, processes, threads and
24 * modules to a maximum of 1000 milliseconds each, unless CryptoGenRandom
27 # define MAXDELAY 1000
30 * Intel hardware RNG CSP -- available from
31 * http://developer.intel.com/design/security/rng/redist_license.htm
33 # define PROV_INTEL_SEC 22
34 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
36 static void readtimer(void);
39 * It appears like CURSORINFO, PCURSORINFO and LPCURSORINFO are only defined
40 * when WINVER is 0x0500 and up, which currently only happens on Win2000.
41 * Unfortunately, those are typedefs, so they're a little bit difficult to
42 * detect properly. On the other hand, the macro CURSOR_SHOWING is defined
43 * within the same conditional, so it can be use to detect the absence of
47 # ifndef CURSOR_SHOWING
49 * Information about the global cursor.
51 typedef struct tagCURSORINFO {
56 } CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
58 # define CURSOR_SHOWING 0x00000001
59 # endif /* CURSOR_SHOWING */
64 HCRYPTPROV hProvider = 0;
68 /* poll the CryptoAPI PRNG */
69 /* The CryptoAPI returns sizeof(buf) bytes of randomness */
70 if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
71 if (CryptGenRandom(hProvider, sizeof(buf), buf) != 0) {
72 RAND_add(buf, sizeof(buf), sizeof(buf));
74 CryptReleaseContext(hProvider, 0);
77 /* poll the Pentium PRG with CryptoAPI */
78 if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
79 if (CryptGenRandom(hProvider, sizeof(buf), buf) != 0) {
80 RAND_add(buf, sizeof(buf), sizeof(buf));
82 CryptReleaseContext(hProvider, 0);
88 /* memory usage statistics */
89 GlobalMemoryStatus(&mst);
90 RAND_add(&mst, sizeof(mst), 1);
93 w = GetCurrentProcessId();
94 RAND_add(&w, sizeof(w), 1);
99 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
101 double add_entropy = 0;
114 static int lastx, lasty, lastdx, lastdy;
121 if (dx != 0 && dy != 0 && dx - lastdx != 0 && dy - lastdy != 0)
123 lastx = x, lasty = y;
124 lastdx = dx, lastdy = dy;
130 RAND_add(&iMsg, sizeof(iMsg), add_entropy);
131 RAND_add(&wParam, sizeof(wParam), 0);
132 RAND_add(&lParam, sizeof(lParam), 0);
134 return (RAND_status());
137 /* feed timing information to the PRNG */
138 static void readtimer(void)
142 static int have_perfc = 1;
143 # if defined(_MSC_VER) && defined(_M_X86)
144 static int have_tsc = 1;
150 _emit 0x0f _emit 0x31 mov cyclecount, eax}
151 RAND_add(&cyclecount, sizeof(cyclecount), 1);
153 __except(EXCEPTION_EXECUTE_HANDLER) {
162 if (QueryPerformanceCounter(&l) == 0)
165 RAND_add(&l, sizeof(l), 0);
168 if (!have_tsc && !have_perfc) {
170 RAND_add(&w, sizeof(w), 0);