46cbe1494c24f9c39e07ceb35246d2449f0a0705
[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
21 /*
22  * Intel hardware RNG CSP -- available from
23  * http://developer.intel.com/design/security/rng/redist_license.htm
24  */
25 # define PROV_INTEL_SEC 22
26 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
27
28 static void readtimer(void);
29
30 int RAND_poll(void)
31 {
32     MEMORYSTATUS mst;
33     HCRYPTPROV hProvider = 0;
34     DWORD w;
35     BYTE buf[64];
36
37     /* poll the CryptoAPI PRNG */
38     /* The CryptoAPI returns sizeof(buf) bytes of randomness */
39     if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
40         if (CryptGenRandom(hProvider, sizeof(buf), buf) != 0) {
41             RAND_add(buf, sizeof(buf), sizeof(buf));
42         }
43         CryptReleaseContext(hProvider, 0);
44     }
45
46     /* poll the Pentium PRG with CryptoAPI */
47     if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
48         if (CryptGenRandom(hProvider, sizeof(buf), buf) != 0) {
49             RAND_add(buf, sizeof(buf), sizeof(buf));
50         }
51         CryptReleaseContext(hProvider, 0);
52     }
53
54     /* timer data */
55     readtimer();
56
57     /* memory usage statistics */
58     GlobalMemoryStatus(&mst);
59     RAND_add(&mst, sizeof(mst), 1);
60
61     /* process ID */
62     w = GetCurrentProcessId();
63     RAND_add(&w, sizeof(w), 1);
64
65     return (1);
66 }
67
68 #if OPENSSL_API_COMPAT < 0x00101000L
69 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
70 {
71     RAND_poll();
72     return RAND_status();
73 }
74
75 void RAND_screen(void)
76 {
77     RAND_poll();
78 }
79 #endif
80
81 /* feed timing information to the PRNG */
82 static void readtimer(void)
83 {
84     DWORD w;
85     LARGE_INTEGER l;
86     static int have_perfc = 1;
87 # if defined(_MSC_VER) && defined(_M_X86)
88     static int have_tsc = 1;
89     DWORD cyclecount;
90
91     if (have_tsc) {
92         __try {
93             __asm {
94             _emit 0x0f _emit 0x31 mov cyclecount, eax}
95             RAND_add(&cyclecount, sizeof(cyclecount), 1);
96         }
97         __except(EXCEPTION_EXECUTE_HANDLER) {
98             have_tsc = 0;
99         }
100     }
101 # else
102 #  define have_tsc 0
103 # endif
104
105     if (have_perfc) {
106         if (QueryPerformanceCounter(&l) == 0)
107             have_perfc = 0;
108         else
109             RAND_add(&l, sizeof(l), 0);
110     }
111
112     if (!have_tsc && !have_perfc) {
113         w = GetTickCount();
114         RAND_add(&w, sizeof(w), 0);
115     }
116 }
117
118 #endif