blob: ce5e7aa31b1a3c438ce6c565e079882fc8acba88 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/rand.h>
16
Adam Langleyd7554562015-09-24 15:03:14 -070017#include <assert.h>
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -070018#include <limits.h>
Adam Langley310d4dd2015-04-13 11:04:21 -070019#include <string.h>
20
Adam Langley0ffc7952017-05-31 14:05:20 -070021#if defined(BORINGSSL_FIPS)
22#include <unistd.h>
23#endif
24
David Benjamin1db476e2015-06-17 00:53:09 -040025#include <openssl/chacha.h>
Adam Langleyd7554562015-09-24 15:03:14 -070026#include <openssl/cpu.h>
Adam Langley310d4dd2015-04-13 11:04:21 -070027#include <openssl/mem.h>
28
29#include "internal.h"
Adam Langley77841042017-04-14 11:16:20 -070030#include "../../internal.h"
Adam Langley0ffc7952017-05-31 14:05:20 -070031#include "../delocate.h"
Adam Langley310d4dd2015-04-13 11:04:21 -070032
33
34/* It's assumed that the operating system always has an unfailing source of
35 * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
36 * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
37 * don't try to handle it.)
38 *
39 * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
40 * instruction is the canonical example of this. When a hardware RNG is
41 * available we don't need to worry about an RNG failure arising from fork()ing
Adam Langley88bb8482017-04-10 16:53:20 -070042 * the process or moving a VM, so we can keep thread-local RNG state and use it
43 * as an additional-data input to CTR-DRBG.
Adam Langley310d4dd2015-04-13 11:04:21 -070044 *
45 * (We assume that the OS entropy is safe from fork()ing and VM duplication.
46 * This might be a bit of a leap of faith, esp on Windows, but there's nothing
47 * that we can do about it.) */
48
Adam Langley88bb8482017-04-10 16:53:20 -070049/* kReseedInterval is the number of generate calls made to CTR-DRBG before
50 * reseeding. */
51static const unsigned kReseedInterval = 4096;
52
Adam Langley90ada2f2017-04-11 16:19:27 -070053/* CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the
54 * continuous random number generator test in FIPS 140-2, section 4.9.2. */
55#define CRNGT_BLOCK_SIZE 16
56
Adam Langley88bb8482017-04-10 16:53:20 -070057/* rand_thread_state contains the per-thread state for the RNG. */
Adam Langley310d4dd2015-04-13 11:04:21 -070058struct rand_thread_state {
Adam Langley88bb8482017-04-10 16:53:20 -070059 CTR_DRBG_STATE drbg;
60 /* calls is the number of generate calls made on |drbg| since it was last
61 * (re)seeded. This is bound by |kReseedInterval|. */
62 unsigned calls;
Adam Langley90ada2f2017-04-11 16:19:27 -070063 /* last_block_valid is non-zero iff |last_block| contains data from
64 * |CRYPTO_sysrand|. */
65 int last_block_valid;
Adam Langley0ffc7952017-05-31 14:05:20 -070066
67#if defined(BORINGSSL_FIPS)
68 /* last_block contains the previous block from |CRYPTO_sysrand|. */
69 uint8_t last_block[CRNGT_BLOCK_SIZE];
70 /* next and prev form a NULL-terminated, double-linked list of all states in
71 * a process. */
72 struct rand_thread_state *next, *prev;
73#endif
Adam Langley310d4dd2015-04-13 11:04:21 -070074};
75
Adam Langley0ffc7952017-05-31 14:05:20 -070076#if defined(BORINGSSL_FIPS)
77/* thread_states_list is the head of a linked-list of all |rand_thread_state|
78 * objects in the process, one per thread. This is needed because FIPS requires
79 * that they be zeroed on process exit, but thread-local destructors aren't
80 * called when the whole process is exiting. */
81DEFINE_BSS_GET(struct rand_thread_state *, thread_states_list);
82DEFINE_STATIC_MUTEX(thread_states_list_lock);
83
84static void rand_thread_state_clear_all(void) __attribute__((destructor));
85static void rand_thread_state_clear_all(void) {
86 CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
87 for (struct rand_thread_state *cur = *thread_states_list_bss_get();
88 cur != NULL; cur = cur->next) {
89 CTR_DRBG_clear(&cur->drbg);
90 }
91 /* |thread_states_list_lock is deliberately left locked so that any threads
92 * that are still running will hang if they try to call |RAND_bytes|. */
93}
94#endif
95
Adam Langley310d4dd2015-04-13 11:04:21 -070096/* rand_thread_state_free frees a |rand_thread_state|. This is called when a
97 * thread exits. */
Adam Langley88bb8482017-04-10 16:53:20 -070098static void rand_thread_state_free(void *state_in) {
Adam Langley0ffc7952017-05-31 14:05:20 -070099 struct rand_thread_state *state = state_in;
100
Adam Langley88bb8482017-04-10 16:53:20 -0700101 if (state_in == NULL) {
Adam Langley310d4dd2015-04-13 11:04:21 -0700102 return;
103 }
104
Adam Langley0ffc7952017-05-31 14:05:20 -0700105#if defined(BORINGSSL_FIPS)
106 CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
107
108 if (state->prev != NULL) {
109 state->prev->next = state->next;
110 } else {
111 *thread_states_list_bss_get() = state->next;
112 }
113
114 if (state->next != NULL) {
115 state->next->prev = state->prev;
116 }
117
118 CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
119
Adam Langley88bb8482017-04-10 16:53:20 -0700120 CTR_DRBG_clear(&state->drbg);
Adam Langley0ffc7952017-05-31 14:05:20 -0700121#endif
122
Adam Langley310d4dd2015-04-13 11:04:21 -0700123 OPENSSL_free(state);
124}
125
David Benjaminbc5b2a22016-03-01 22:57:32 -0500126#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
David Benjaminec978dd2016-11-04 18:59:33 -0400127 !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
Adam Langleyd7554562015-09-24 15:03:14 -0700128
129/* These functions are defined in asm/rdrand-x86_64.pl */
130extern int CRYPTO_rdrand(uint8_t out[8]);
131extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
132
133static int have_rdrand(void) {
David Benjamin91871012017-04-25 15:37:53 -0400134 return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0;
Adam Langleyd7554562015-09-24 15:03:14 -0700135}
136
137static int hwrand(uint8_t *buf, size_t len) {
138 if (!have_rdrand()) {
139 return 0;
140 }
141
142 const size_t len_multiple8 = len & ~7;
143 if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
144 return 0;
145 }
146 len -= len_multiple8;
147
148 if (len != 0) {
149 assert(len < 8);
150
151 uint8_t rand_buf[8];
152 if (!CRYPTO_rdrand(rand_buf)) {
153 return 0;
154 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500155 OPENSSL_memcpy(buf + len_multiple8, rand_buf, len);
Adam Langleyd7554562015-09-24 15:03:14 -0700156 }
157
158 return 1;
159}
160
161#else
162
163static int hwrand(uint8_t *buf, size_t len) {
164 return 0;
165}
166
167#endif
168
Adam Langley88bb8482017-04-10 16:53:20 -0700169#if defined(BORINGSSL_FIPS)
170
Adam Langley90ada2f2017-04-11 16:19:27 -0700171static void rand_get_seed(struct rand_thread_state *state,
172 uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
173 if (!state->last_block_valid) {
Adam Langley5f107ce2017-05-18 11:26:36 -0700174 if (!hwrand(state->last_block, sizeof(state->last_block))) {
175 CRYPTO_sysrand(state->last_block, sizeof(state->last_block));
176 }
Adam Langley90ada2f2017-04-11 16:19:27 -0700177 state->last_block_valid = 1;
178 }
179
Adam Langley5f107ce2017-05-18 11:26:36 -0700180 /* We overread from /dev/urandom or RDRAND by a factor of 10 and XOR to
181 * whiten. */
Adam Langley88bb8482017-04-10 16:53:20 -0700182#define FIPS_OVERREAD 10
183 uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD];
Adam Langley5f107ce2017-05-18 11:26:36 -0700184
185 if (!hwrand(entropy, sizeof(entropy))) {
186 CRYPTO_sysrand(entropy, sizeof(entropy));
187 }
Adam Langley88bb8482017-04-10 16:53:20 -0700188
Adam Langley90ada2f2017-04-11 16:19:27 -0700189 /* See FIPS 140-2, section 4.9.2. This is the “continuous random number
190 * generator test” which causes the program to randomly abort. Hopefully the
191 * rate of failure is small enough not to be a problem in practice. */
192 if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
Adam Langley429e85b2017-05-18 11:37:44 -0700193 BORINGSSL_FIPS_abort();
Adam Langley90ada2f2017-04-11 16:19:27 -0700194 }
195
196 for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
197 i += CRNGT_BLOCK_SIZE) {
198 if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
199 CRNGT_BLOCK_SIZE) == 0) {
Adam Langley429e85b2017-05-18 11:37:44 -0700200 BORINGSSL_FIPS_abort();
Adam Langley90ada2f2017-04-11 16:19:27 -0700201 }
202 }
203 OPENSSL_memcpy(state->last_block,
204 entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
205 CRNGT_BLOCK_SIZE);
206
Adam Langley88bb8482017-04-10 16:53:20 -0700207 OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
208
209 for (size_t i = 1; i < FIPS_OVERREAD; i++) {
210 for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) {
211 seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
212 }
213 }
214}
215
216#else
217
Adam Langley90ada2f2017-04-11 16:19:27 -0700218static void rand_get_seed(struct rand_thread_state *state,
219 uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
Adam Langley5f107ce2017-05-18 11:26:36 -0700220 /* If not in FIPS mode, we don't overread from the system entropy source and
221 * we don't depend only on the hardware RDRAND. */
Adam Langley88bb8482017-04-10 16:53:20 -0700222 CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN);
223}
224
225#endif
226
227void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
228 const uint8_t user_additional_data[32]) {
229 if (out_len == 0) {
230 return;
Adam Langley310d4dd2015-04-13 11:04:21 -0700231 }
232
Adam Langley88bb8482017-04-10 16:53:20 -0700233 struct rand_thread_state stack_state;
Adam Langley310d4dd2015-04-13 11:04:21 -0700234 struct rand_thread_state *state =
235 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
Adam Langley88bb8482017-04-10 16:53:20 -0700236
Adam Langley310d4dd2015-04-13 11:04:21 -0700237 if (state == NULL) {
238 state = OPENSSL_malloc(sizeof(struct rand_thread_state));
239 if (state == NULL ||
240 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
241 rand_thread_state_free)) {
Adam Langley88bb8482017-04-10 16:53:20 -0700242 /* If the system is out of memory, use an ephemeral state on the
243 * stack. */
244 state = &stack_state;
Adam Langley310d4dd2015-04-13 11:04:21 -0700245 }
246
Adam Langley90ada2f2017-04-11 16:19:27 -0700247 state->last_block_valid = 0;
Adam Langley88bb8482017-04-10 16:53:20 -0700248 uint8_t seed[CTR_DRBG_ENTROPY_LEN];
Adam Langley90ada2f2017-04-11 16:19:27 -0700249 rand_get_seed(state, seed);
Adam Langley88bb8482017-04-10 16:53:20 -0700250 if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) {
251 abort();
252 }
253 state->calls = 0;
Adam Langley0ffc7952017-05-31 14:05:20 -0700254
255#if defined(BORINGSSL_FIPS)
256 if (state != &stack_state) {
257 CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
258 struct rand_thread_state **states_list = thread_states_list_bss_get();
259 state->next = *states_list;
260 if (state->next != NULL) {
261 state->next->prev = state;
262 }
263 state->prev = NULL;
264 *states_list = state;
265 CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
266 }
267#endif
Adam Langley310d4dd2015-04-13 11:04:21 -0700268 }
269
Adam Langley0ffc7952017-05-31 14:05:20 -0700270#if defined(BORINGSSL_FIPS)
271 CRYPTO_STATIC_MUTEX_lock_read(thread_states_list_lock_bss_get());
272#endif
273
Adam Langley88bb8482017-04-10 16:53:20 -0700274 if (state->calls >= kReseedInterval) {
275 uint8_t seed[CTR_DRBG_ENTROPY_LEN];
Adam Langley90ada2f2017-04-11 16:19:27 -0700276 rand_get_seed(state, seed);
Adam Langley88bb8482017-04-10 16:53:20 -0700277 if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
278 abort();
279 }
280 state->calls = 0;
Adam Langley310d4dd2015-04-13 11:04:21 -0700281 }
282
Adam Langley88bb8482017-04-10 16:53:20 -0700283 /* Additional data is mixed into every CTR-DRBG call to protect, as best we
284 * can, against forks & VM clones. We do not over-read this information and
285 * don't reseed with it so, from the point of view of FIPS, this doesn't
286 * provide “prediction resistance”. But, in practice, it does. */
287 uint8_t additional_data[32];
288 if (!hwrand(additional_data, sizeof(additional_data))) {
289 /* Without a hardware RNG to save us from address-space duplication, the OS
Adam Langley92f888e2017-04-11 15:06:47 -0700290 * entropy is used. This can be expensive (one read per |RAND_bytes| call)
291 * and so can be disabled by applications that we have ensured don't fork
292 * and aren't at risk of VM cloning. */
293 if (!rand_fork_unsafe_buffering_enabled()) {
294 CRYPTO_sysrand(additional_data, sizeof(additional_data));
295 } else {
296 OPENSSL_memset(additional_data, 0, sizeof(additional_data));
297 }
Adam Langley310d4dd2015-04-13 11:04:21 -0700298 }
Adam Langley310d4dd2015-04-13 11:04:21 -0700299
Adam Langley88bb8482017-04-10 16:53:20 -0700300 for (size_t i = 0; i < sizeof(additional_data); i++) {
301 additional_data[i] ^= user_additional_data[i];
302 }
303
304 int first_call = 1;
305 while (out_len > 0) {
306 size_t todo = out_len;
307 if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
308 todo = CTR_DRBG_MAX_GENERATE_LENGTH;
309 }
310
311 if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
312 first_call ? sizeof(additional_data) : 0)) {
313 abort();
314 }
315
316 out += todo;
317 out_len -= todo;
318 state->calls++;
319 first_call = 0;
320 }
321
322 if (state == &stack_state) {
323 CTR_DRBG_clear(&state->drbg);
324 }
325
Adam Langley0ffc7952017-05-31 14:05:20 -0700326#if defined(BORINGSSL_FIPS)
327 CRYPTO_STATIC_MUTEX_unlock_read(thread_states_list_lock_bss_get());
328#endif
329
Adam Langley88bb8482017-04-10 16:53:20 -0700330 return;
331}
332
333int RAND_bytes(uint8_t *out, size_t out_len) {
334 static const uint8_t kZeroAdditionalData[32] = {0};
335 RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData);
Adam Langley310d4dd2015-04-13 11:04:21 -0700336 return 1;
337}
Adam Langley95c29f32014-06-20 12:00:00 -0700338
339int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
340 return RAND_bytes(buf, len);
341}