blob: cdcd70103d98e1767217cb6888932f3504c21715 [file] [log] [blame]
xf.libfc6e712025-02-07 01:54:34 -08001/*
2 * Copyright 1995-2022 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_local.h"
13#include "crypto/rand.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) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
23 && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
24# define USE_BCRYPTGENRANDOM
25# endif
26
27# ifdef USE_BCRYPTGENRANDOM
28# include <bcrypt.h>
29# ifdef _MSC_VER
30# pragma comment(lib, "bcrypt.lib")
31# endif
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
45size_t rand_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
125int rand_pool_add_nonce_data(RAND_POOL *pool)
126{
127 struct {
128 DWORD pid;
129 DWORD tid;
130 FILETIME time;
131 } data = { 0 };
132
133 /*
134 * Add process id, thread id, and a high resolution timestamp to
135 * ensure that the nonce is unique with 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
145int rand_pool_add_additional_data(RAND_POOL *pool)
146{
147 struct {
148 DWORD tid;
149 LARGE_INTEGER time;
150 } data = { 0 };
151
152 /*
153 * Add some noise from the thread id and a high resolution timer.
154 * The thread id adds a little randomness if the drbg is accessed
155 * concurrently (which is the case for the <master> drbg).
156 */
157 data.tid = GetCurrentThreadId();
158 QueryPerformanceCounter(&data.time);
159 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
160}
161
162# if OPENSSL_API_COMPAT < 0x10100000L
163int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
164{
165 RAND_poll();
166 return RAND_status();
167}
168
169void RAND_screen(void)
170{
171 RAND_poll();
172}
173# endif
174
175int rand_pool_init(void)
176{
177 return 1;
178}
179
180void rand_pool_cleanup(void)
181{
182}
183
184void rand_pool_keep_random_devices_open(int keep)
185{
186}
187
188#endif