Update copyright year
[openssl.git] / providers / implementations / rands / seeding / rand_win.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "prov/rand_pool.h"
13 #include "crypto/rand.h"
14 #include "prov/seeding.h"
15
16 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
17
18 # ifndef OPENSSL_RAND_SEED_OS
19 #  error "Unsupported seeding method configured; must be os"
20 # endif
21
22 # include <windows.h>
23 /* On Windows Vista or higher use BCrypt instead of the legacy CryptoAPI */
24 # if defined(_MSC_VER) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
25      && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
26 #  define USE_BCRYPTGENRANDOM
27 # endif
28
29 # ifdef USE_BCRYPTGENRANDOM
30 #  include <bcrypt.h>
31 #  pragma comment(lib, "bcrypt.lib")
32 #  ifndef STATUS_SUCCESS
33 #   define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
34 #  endif
35 # else
36 #  include <wincrypt.h>
37 /*
38  * Intel hardware RNG CSP -- available from
39  * http://developer.intel.com/design/security/rng/redist_license.htm
40  */
41 #  define PROV_INTEL_SEC 22
42 #  define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
43 # endif
44
45 size_t prov_pool_acquire_entropy(RAND_POOL *pool)
46 {
47 # ifndef USE_BCRYPTGENRANDOM
48     HCRYPTPROV hProvider;
49 # endif
50     unsigned char *buffer;
51     size_t bytes_needed;
52     size_t entropy_available = 0;
53
54
55 # ifdef OPENSSL_RAND_SEED_RDTSC
56     entropy_available = rand_acquire_entropy_from_tsc(pool);
57     if (entropy_available > 0)
58         return entropy_available;
59 # endif
60
61 # ifdef OPENSSL_RAND_SEED_RDCPU
62     entropy_available = rand_acquire_entropy_from_cpu(pool);
63     if (entropy_available > 0)
64         return entropy_available;
65 # endif
66
67 # ifdef USE_BCRYPTGENRANDOM
68     bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
69     buffer = rand_pool_add_begin(pool, bytes_needed);
70     if (buffer != NULL) {
71         size_t bytes = 0;
72         if (BCryptGenRandom(NULL, buffer, bytes_needed,
73                             BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
74             bytes = bytes_needed;
75
76         rand_pool_add_end(pool, bytes, 8 * bytes);
77         entropy_available = rand_pool_entropy_available(pool);
78     }
79     if (entropy_available > 0)
80         return entropy_available;
81 # else
82     bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
83     buffer = rand_pool_add_begin(pool, bytes_needed);
84     if (buffer != NULL) {
85         size_t bytes = 0;
86         /* poll the CryptoAPI PRNG */
87         if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
88                                  CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
89             if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
90                 bytes = bytes_needed;
91
92             CryptReleaseContext(hProvider, 0);
93         }
94
95         rand_pool_add_end(pool, bytes, 8 * bytes);
96         entropy_available = rand_pool_entropy_available(pool);
97     }
98     if (entropy_available > 0)
99         return entropy_available;
100
101     bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
102     buffer = rand_pool_add_begin(pool, bytes_needed);
103     if (buffer != NULL) {
104         size_t bytes = 0;
105         /* poll the Pentium PRG with CryptoAPI */
106         if (CryptAcquireContextW(&hProvider, NULL,
107                                  INTEL_DEF_PROV, PROV_INTEL_SEC,
108                                  CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
109             if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
110                 bytes = bytes_needed;
111
112             CryptReleaseContext(hProvider, 0);
113         }
114         rand_pool_add_end(pool, bytes, 8 * bytes);
115         entropy_available = rand_pool_entropy_available(pool);
116     }
117     if (entropy_available > 0)
118         return entropy_available;
119 # endif
120
121     return rand_pool_entropy_available(pool);
122 }
123
124
125 int prov_pool_add_nonce_data(RAND_POOL *pool)
126 {
127     struct {
128         DWORD pid;
129         DWORD tid;
130         FILETIME time;
131     } data;
132
133     /* Erase the entire structure including any padding */
134     memset(&data, 0, sizeof(data));
135
136     /*
137      * Add process id, thread id, and a high resolution timestamp to
138      * ensure that the nonce is unique with high probability for
139      * different process instances.
140      */
141     data.pid = GetCurrentProcessId();
142     data.tid = GetCurrentThreadId();
143     GetSystemTimeAsFileTime(&data.time);
144
145     return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
146 }
147
148 int rand_pool_add_additional_data(RAND_POOL *pool)
149 {
150     struct {
151         DWORD tid;
152         LARGE_INTEGER time;
153     } data;
154
155     /* Erase the entire structure including any padding */
156     memset(&data, 0, sizeof(data));
157
158     /*
159      * Add some noise from the thread id and a high resolution timer.
160      * The thread id adds a little randomness if the drbg is accessed
161      * concurrently (which is the case for the <master> drbg).
162      */
163     data.tid = GetCurrentThreadId();
164     QueryPerformanceCounter(&data.time);
165     return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
166 }
167
168 int rand_pool_init(void)
169 {
170     return 1;
171 }
172
173 void rand_pool_cleanup(void)
174 {
175 }
176
177 void rand_pool_keep_random_devices_open(int keep)
178 {
179 }
180
181 #endif