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