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