cherry pick pr-512 changes
[openssl.git] / crypto / rand / rand_win.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "internal/cryptlib.h"
11 #include <openssl/rand.h>
12 #include "rand_lcl.h"
13
14 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
15 # include <windows.h>
16 # ifndef _WIN32_WINNT
17 #  define _WIN32_WINNT 0x0400
18 # endif
19 # include <wincrypt.h>
20 # include <tlhelp32.h>
21
22 /*
23  * Limit the time spent walking through the heap, processes, threads and
24  * modules to a maximum of 1000 milliseconds each, unless CryptoGenRandom
25  * failed
26  */
27 # define MAXDELAY 1000
28
29 /*
30  * Intel hardware RNG CSP -- available from
31  * http://developer.intel.com/design/security/rng/redist_license.htm
32  */
33 # define PROV_INTEL_SEC 22
34 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
35
36 static void readtimer(void);
37
38 /*
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
44  * said typedefs.
45  */
46
47 # ifndef CURSOR_SHOWING
48 /*
49  * Information about the global cursor.
50  */
51 typedef struct tagCURSORINFO {
52     DWORD cbSize;
53     DWORD flags;
54     HCURSOR hCursor;
55     POINT ptScreenPos;
56 } CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
57
58 #  define CURSOR_SHOWING     0x00000001
59 # endif                         /* CURSOR_SHOWING */
60
61 int RAND_poll(void)
62 {
63     MEMORYSTATUS mst;
64     HCRYPTPROV hProvider = 0;
65     DWORD w;
66     BYTE buf[64];
67
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));
73         }
74         CryptReleaseContext(hProvider, 0);
75     }
76
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));
81         }
82         CryptReleaseContext(hProvider, 0);
83     }
84
85     /* timer data */
86     readtimer();
87
88     /* memory usage statistics */
89     GlobalMemoryStatus(&mst);
90     RAND_add(&mst, sizeof(mst), 1);
91
92     /* process ID */
93     w = GetCurrentProcessId();
94     RAND_add(&w, sizeof(w), 1);
95
96     return (1);
97 }
98
99 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
100 {
101     double add_entropy = 0;
102
103     switch (iMsg) {
104     case WM_KEYDOWN:
105         {
106             static WPARAM key;
107             if (key != wParam)
108                 add_entropy = 0.05;
109             key = wParam;
110         }
111         break;
112     case WM_MOUSEMOVE:
113         {
114             static int lastx, lasty, lastdx, lastdy;
115             int x, y, dx, dy;
116
117             x = LOWORD(lParam);
118             y = HIWORD(lParam);
119             dx = lastx - x;
120             dy = lasty - y;
121             if (dx != 0 && dy != 0 && dx - lastdx != 0 && dy - lastdy != 0)
122                 add_entropy = .2;
123             lastx = x, lasty = y;
124             lastdx = dx, lastdy = dy;
125         }
126         break;
127     }
128
129     readtimer();
130     RAND_add(&iMsg, sizeof(iMsg), add_entropy);
131     RAND_add(&wParam, sizeof(wParam), 0);
132     RAND_add(&lParam, sizeof(lParam), 0);
133
134     return (RAND_status());
135 }
136
137 /* feed timing information to the PRNG */
138 static void readtimer(void)
139 {
140     DWORD w;
141     LARGE_INTEGER l;
142     static int have_perfc = 1;
143 # if defined(_MSC_VER) && defined(_M_X86)
144     static int have_tsc = 1;
145     DWORD cyclecount;
146
147     if (have_tsc) {
148         __try {
149             __asm {
150             _emit 0x0f _emit 0x31 mov cyclecount, eax}
151             RAND_add(&cyclecount, sizeof(cyclecount), 1);
152         }
153         __except(EXCEPTION_EXECUTE_HANDLER) {
154             have_tsc = 0;
155         }
156     }
157 # else
158 #  define have_tsc 0
159 # endif
160
161     if (have_perfc) {
162         if (QueryPerformanceCounter(&l) == 0)
163             have_perfc = 0;
164         else
165             RAND_add(&l, sizeof(l), 0);
166     }
167
168     if (!have_tsc && !have_perfc) {
169         w = GetTickCount();
170         RAND_add(&w, sizeof(w), 0);
171     }
172 }
173
174 #endif