1// Copyright 2014 The BoringSSL Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#include <assert.h> 16#include <limits.h> 17#include <string.h> 18 19#if defined(BORINGSSL_FIPS) 20#include <unistd.h> 21#endif 22 23#include <openssl/chacha.h> 24#include <openssl/ctrdrbg.h> 25#include <openssl/mem.h> 26 27#include "../../bcm_support.h" 28#include "../bcm_interface.h" 29#include "../delocate.h" 30#include "internal.h" 31 32 33// It's assumed that the operating system always has an unfailing source of 34// entropy which is accessed via |CRYPTO_sysrand[_for_seed]|. (If the operating 35// system entropy source fails, it's up to |CRYPTO_sysrand| to abort the 36// process—we don't try to handle it.) 37// 38// In addition, the hardware may provide a low-latency RNG. Intel's rdrand 39// instruction is the canonical example of this. When a hardware RNG is 40// available we don't need to worry about an RNG failure arising from fork()ing 41// the process or moving a VM, so we can keep thread-local RNG state and use it 42// as an additional-data input to CTR-DRBG. 43// 44// (We assume that the OS entropy is safe from fork()ing and VM duplication. 45// This might be a bit of a leap of faith, esp on Windows, but there's nothing 46// that we can do about it.) 47 48// kReseedInterval is the number of generate calls made to CTR-DRBG before 49// reseeding. 50static const unsigned kReseedInterval = 4096; 51 52// CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the 53// continuous random number generator test in FIPS 140-2, section 4.9.2. 54#define CRNGT_BLOCK_SIZE 16 55 56namespace { 57// rand_thread_state contains the per-thread state for the RNG. 58struct rand_thread_state { 59 CTR_DRBG_STATE drbg; 60 uint64_t fork_generation; 61 // calls is the number of generate calls made on |drbg| since it was last 62 // (re)seeded. This is bound by |kReseedInterval|. 63 unsigned calls; 64 // last_block_valid is non-zero iff |last_block| contains data from 65 // |get_seed_entropy|. 66 int last_block_valid; 67 // fork_unsafe_buffering is non-zero iff, when |drbg| was last (re)seeded, 68 // fork-unsafe buffering was enabled. 69 int fork_unsafe_buffering; 70 71#if defined(BORINGSSL_FIPS) 72 // last_block contains the previous block from |get_seed_entropy|. 73 uint8_t last_block[CRNGT_BLOCK_SIZE]; 74 // next and prev form a NULL-terminated, double-linked list of all states in 75 // a process. 76 struct rand_thread_state *next, *prev; 77 // clear_drbg_lock synchronizes between uses of |drbg| and 78 // |rand_thread_state_clear_all| clearing it. This lock should be uncontended 79 // in the common case, except on shutdown. 80 CRYPTO_MUTEX clear_drbg_lock; 81#endif 82}; 83} // namespace 84 85#if defined(BORINGSSL_FIPS) 86// thread_states_list is the head of a linked-list of all |rand_thread_state| 87// objects in the process, one per thread. This is needed because FIPS requires 88// that they be zeroed on process exit, but thread-local destructors aren't 89// called when the whole process is exiting. 90DEFINE_BSS_GET(struct rand_thread_state *, thread_states_list, nullptr) 91DEFINE_STATIC_MUTEX(thread_states_list_lock) 92 93static void rand_thread_state_clear_all(void) __attribute__((destructor)); 94static void rand_thread_state_clear_all(void) { 95 CRYPTO_MUTEX_lock_write(thread_states_list_lock_bss_get()); 96 for (struct rand_thread_state *cur = *thread_states_list_bss_get(); 97 cur != NULL; cur = cur->next) { 98 CRYPTO_MUTEX_lock_write(&cur->clear_drbg_lock); 99 CTR_DRBG_clear(&cur->drbg); 100 } 101 // The locks are deliberately left locked so that any threads that are still 102 // running will hang if they try to call |BCM_rand_bytes|. It also ensures 103 // |rand_thread_state_free| cannot free any thread state while we've taken the 104 // lock. 105} 106#endif 107 108// rand_thread_state_free frees a |rand_thread_state|. This is called when a 109// thread exits. 110static void rand_thread_state_free(void *state_in) { 111 struct rand_thread_state *state = 112 reinterpret_cast<rand_thread_state *>(state_in); 113 114 if (state_in == NULL) { 115 return; 116 } 117 118#if defined(BORINGSSL_FIPS) 119 CRYPTO_MUTEX_lock_write(thread_states_list_lock_bss_get()); 120 121 if (state->prev != NULL) { 122 state->prev->next = state->next; 123 } else if (*thread_states_list_bss_get() == state) { 124 // |state->prev| may be NULL either if it is the head of the list, 125 // or if |state| is freed before it was added to the list at all. 126 // Compare against the head of the list to distinguish these cases. 127 *thread_states_list_bss_get() = state->next; 128 } 129 130 if (state->next != NULL) { 131 state->next->prev = state->prev; 132 } 133 134 CRYPTO_MUTEX_unlock_write(thread_states_list_lock_bss_get()); 135 136 CTR_DRBG_clear(&state->drbg); 137#endif 138 139 OPENSSL_free(state); 140} 141 142#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ 143 !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) 144// rdrand should only be called if either |have_rdrand| or |have_fast_rdrand| 145// returned true. 146static int rdrand(uint8_t *buf, const size_t len) { 147 const size_t len_multiple8 = len & ~7; 148 if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) { 149 return 0; 150 } 151 const size_t remainder = len - len_multiple8; 152 153 if (remainder != 0) { 154 assert(remainder < 8); 155 156 uint8_t rand_buf[8]; 157 if (!CRYPTO_rdrand(rand_buf)) { 158 return 0; 159 } 160 OPENSSL_memcpy(buf + len_multiple8, rand_buf, remainder); 161 } 162 163 return 1; 164} 165 166#else 167 168static int rdrand(uint8_t *buf, size_t len) { return 0; } 169 170#endif 171 172bcm_status BCM_rand_bytes_hwrng(uint8_t *buf, const size_t len) { 173 if (!have_rdrand()) { 174 return bcm_status::failure; 175 } 176 if (rdrand(buf, len)) { 177 return bcm_status::not_approved; 178 } 179 return bcm_status::failure; 180} 181 182#if defined(BORINGSSL_FIPS) 183 184// In passive entropy mode, entropy is supplied from outside of the module via 185// |BCM_rand_load_entropy| and is stored in global instance of the following 186// structure. 187 188struct entropy_buffer { 189 // bytes contains entropy suitable for seeding a DRBG. 190 uint8_t bytes[CRNGT_BLOCK_SIZE + CTR_DRBG_SEED_LEN * BORINGSSL_FIPS_OVERREAD]; 191 // bytes_valid indicates the number of bytes of |bytes| that contain valid 192 // data. 193 size_t bytes_valid; 194 // want_additional_input is true if any of the contents of |bytes| were 195 // obtained via a method other than from the kernel. In these cases entropy 196 // from the kernel is also provided via an additional input to the DRBG. 197 int want_additional_input; 198}; 199 200DEFINE_BSS_GET(struct entropy_buffer, entropy_buffer, {}) 201DEFINE_STATIC_MUTEX(entropy_buffer_lock) 202 203bcm_infallible BCM_rand_load_entropy(const uint8_t *entropy, size_t entropy_len, 204 int want_additional_input) { 205 struct entropy_buffer *const buffer = entropy_buffer_bss_get(); 206 207 CRYPTO_MUTEX_lock_write(entropy_buffer_lock_bss_get()); 208 const size_t space = sizeof(buffer->bytes) - buffer->bytes_valid; 209 if (entropy_len > space) { 210 entropy_len = space; 211 } 212 213 OPENSSL_memcpy(&buffer->bytes[buffer->bytes_valid], entropy, entropy_len); 214 buffer->bytes_valid += entropy_len; 215 buffer->want_additional_input |= want_additional_input && (entropy_len != 0); 216 CRYPTO_MUTEX_unlock_write(entropy_buffer_lock_bss_get()); 217 return bcm_infallible::not_approved; 218} 219 220// get_seed_entropy fills |out_entropy_len| bytes of |out_entropy| from the 221// global |entropy_buffer|. 222static void get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len, 223 int *out_want_additional_input) { 224 struct entropy_buffer *const buffer = entropy_buffer_bss_get(); 225 if (out_entropy_len > sizeof(buffer->bytes)) { 226 abort(); 227 } 228 229 CRYPTO_MUTEX_lock_write(entropy_buffer_lock_bss_get()); 230 while (buffer->bytes_valid < out_entropy_len) { 231 CRYPTO_MUTEX_unlock_write(entropy_buffer_lock_bss_get()); 232 RAND_need_entropy(out_entropy_len - buffer->bytes_valid); 233 CRYPTO_MUTEX_lock_write(entropy_buffer_lock_bss_get()); 234 } 235 236 *out_want_additional_input = buffer->want_additional_input; 237 OPENSSL_memcpy(out_entropy, buffer->bytes, out_entropy_len); 238 OPENSSL_memmove(buffer->bytes, &buffer->bytes[out_entropy_len], 239 buffer->bytes_valid - out_entropy_len); 240 buffer->bytes_valid -= out_entropy_len; 241 if (buffer->bytes_valid == 0) { 242 buffer->want_additional_input = 0; 243 } 244 245 CRYPTO_MUTEX_unlock_write(entropy_buffer_lock_bss_get()); 246} 247 248// rand_get_seed fills |seed| with entropy. In some cases, it will additionally 249// fill |additional_input| with entropy to supplement |seed|. It sets 250// |*out_additional_input_len| to the number of extra bytes. 251static void rand_get_seed(struct rand_thread_state *state, 252 uint8_t seed[CTR_DRBG_SEED_LEN], 253 uint8_t additional_input[CTR_DRBG_SEED_LEN], 254 size_t *out_additional_input_len) { 255 uint8_t entropy_bytes[sizeof(state->last_block) + 256 CTR_DRBG_SEED_LEN * BORINGSSL_FIPS_OVERREAD]; 257 uint8_t *entropy = entropy_bytes; 258 size_t entropy_len = sizeof(entropy_bytes); 259 260 if (state->last_block_valid) { 261 // No need to fill |state->last_block| with entropy from the read. 262 entropy += sizeof(state->last_block); 263 entropy_len -= sizeof(state->last_block); 264 } 265 266 int want_additional_input; 267 get_seed_entropy(entropy, entropy_len, &want_additional_input); 268 269 if (!state->last_block_valid) { 270 OPENSSL_memcpy(state->last_block, entropy, sizeof(state->last_block)); 271 entropy += sizeof(state->last_block); 272 entropy_len -= sizeof(state->last_block); 273 } 274 275 // See FIPS 140-2, section 4.9.2. This is the “continuous random number 276 // generator test” which causes the program to randomly abort. Hopefully the 277 // rate of failure is small enough not to be a problem in practice. 278 if (CRYPTO_memcmp(state->last_block, entropy, sizeof(state->last_block)) == 279 0) { 280 fprintf(CRYPTO_get_stderr(), "CRNGT failed.\n"); 281 BORINGSSL_FIPS_abort(); 282 } 283 284 assert(entropy_len % CRNGT_BLOCK_SIZE == 0); 285 for (size_t i = CRNGT_BLOCK_SIZE; i < entropy_len; i += CRNGT_BLOCK_SIZE) { 286 if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i, 287 CRNGT_BLOCK_SIZE) == 0) { 288 fprintf(CRYPTO_get_stderr(), "CRNGT failed.\n"); 289 BORINGSSL_FIPS_abort(); 290 } 291 } 292 OPENSSL_memcpy(state->last_block, entropy + entropy_len - CRNGT_BLOCK_SIZE, 293 CRNGT_BLOCK_SIZE); 294 295 assert(entropy_len == BORINGSSL_FIPS_OVERREAD * CTR_DRBG_SEED_LEN); 296 OPENSSL_memcpy(seed, entropy, CTR_DRBG_SEED_LEN); 297 298 for (size_t i = 1; i < BORINGSSL_FIPS_OVERREAD; i++) { 299 for (size_t j = 0; j < CTR_DRBG_SEED_LEN; j++) { 300 seed[j] ^= entropy[CTR_DRBG_SEED_LEN * i + j]; 301 } 302 } 303 304 // If we used something other than system entropy then also 305 // opportunistically read from the system. This avoids solely relying on the 306 // hardware once the entropy pool has been initialized. 307 *out_additional_input_len = 0; 308 if (want_additional_input && 309 CRYPTO_sysrand_if_available(additional_input, CTR_DRBG_SEED_LEN)) { 310 *out_additional_input_len = CTR_DRBG_SEED_LEN; 311 } 312} 313 314#else 315 316// rand_get_seed fills |seed| with entropy. In some cases, it will additionally 317// fill |additional_input| with entropy to supplement |seed|. It sets 318// |*out_additional_input_len| to the number of extra bytes. 319static void rand_get_seed(struct rand_thread_state *state, 320 uint8_t seed[CTR_DRBG_SEED_LEN], 321 uint8_t additional_input[CTR_DRBG_SEED_LEN], 322 size_t *out_additional_input_len) { 323 // If not in FIPS mode, we don't overread from the system entropy source and 324 // we don't depend only on the hardware RDRAND. 325 CRYPTO_sysrand_for_seed(seed, CTR_DRBG_SEED_LEN); 326 *out_additional_input_len = 0; 327} 328 329#endif 330 331bcm_infallible BCM_rand_bytes_with_additional_data( 332 uint8_t *out, size_t out_len, const uint8_t user_additional_data[32]) { 333 if (out_len == 0) { 334 return bcm_infallible::approved; 335 } 336 337 const uint64_t fork_generation = CRYPTO_get_fork_generation(); 338 const int fork_unsafe_buffering = rand_fork_unsafe_buffering_enabled(); 339 340 // Additional data is mixed into every CTR-DRBG call to protect, as best we 341 // can, against forks & VM clones. We do not over-read this information and 342 // don't reseed with it so, from the point of view of FIPS, this doesn't 343 // provide “prediction resistance”. But, in practice, it does. 344 uint8_t additional_data[32]; 345 // Intel chips have fast RDRAND instructions while, in other cases, RDRAND can 346 // be _slower_ than a system call. 347 if (!have_fast_rdrand() || 348 !rdrand(additional_data, sizeof(additional_data))) { 349 // Without a hardware RNG to save us from address-space duplication, the OS 350 // entropy is used. This can be expensive (one read per |RAND_bytes| call) 351 // and so is disabled when we have fork detection, or if the application has 352 // promised not to fork. 353 if (fork_generation != 0 || fork_unsafe_buffering) { 354 OPENSSL_memset(additional_data, 0, sizeof(additional_data)); 355 } else if (!have_rdrand()) { 356 // No alternative so block for OS entropy. 357 CRYPTO_sysrand(additional_data, sizeof(additional_data)); 358 } else if (!CRYPTO_sysrand_if_available(additional_data, 359 sizeof(additional_data)) && 360 !rdrand(additional_data, sizeof(additional_data))) { 361 // RDRAND failed: block for OS entropy. 362 CRYPTO_sysrand(additional_data, sizeof(additional_data)); 363 } 364 } 365 366 for (size_t i = 0; i < sizeof(additional_data); i++) { 367 additional_data[i] ^= user_additional_data[i]; 368 } 369 370 struct rand_thread_state stack_state; 371 struct rand_thread_state *state = reinterpret_cast<rand_thread_state *>( 372 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND)); 373 374 if (state == NULL) { 375 state = reinterpret_cast<rand_thread_state *>( 376 OPENSSL_zalloc(sizeof(struct rand_thread_state))); 377 if (state == NULL || 378 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, 379 rand_thread_state_free)) { 380 // If the system is out of memory, use an ephemeral state on the 381 // stack. 382 state = &stack_state; 383 } 384 385 state->last_block_valid = 0; 386 uint8_t seed[CTR_DRBG_SEED_LEN]; 387 uint8_t personalization[CTR_DRBG_SEED_LEN] = {0}; 388 size_t personalization_len = 0; 389 rand_get_seed(state, seed, personalization, &personalization_len); 390 391 if (!CTR_DRBG_init(&state->drbg, /*df=*/true, seed, 32u, seed + 32, 392 personalization, personalization_len)) { 393 abort(); 394 } 395 state->calls = 0; 396 state->fork_generation = fork_generation; 397 state->fork_unsafe_buffering = fork_unsafe_buffering; 398 399#if defined(BORINGSSL_FIPS) 400 CRYPTO_MUTEX_init(&state->clear_drbg_lock); 401 if (state != &stack_state) { 402 CRYPTO_MUTEX_lock_write(thread_states_list_lock_bss_get()); 403 struct rand_thread_state **states_list = thread_states_list_bss_get(); 404 state->next = *states_list; 405 if (state->next != NULL) { 406 state->next->prev = state; 407 } 408 state->prev = NULL; 409 *states_list = state; 410 CRYPTO_MUTEX_unlock_write(thread_states_list_lock_bss_get()); 411 } 412#endif 413 } 414 415 if (state->calls >= kReseedInterval || 416 // If we've forked since |state| was last seeded, reseed. 417 state->fork_generation != fork_generation || 418 // If |state| was seeded from a state with different fork-safety 419 // preferences, reseed. Suppose |state| was fork-safe, then forked into 420 // two children, but each of the children never fork and disable fork 421 // safety. The children must reseed to avoid working from the same PRNG 422 // state. 423 state->fork_unsafe_buffering != fork_unsafe_buffering) { 424 uint8_t seed[CTR_DRBG_SEED_LEN]; 425 uint8_t reseed_additional_data[CTR_DRBG_SEED_LEN] = {0}; 426 size_t reseed_additional_data_len = 0; 427 rand_get_seed(state, seed, reseed_additional_data, 428 &reseed_additional_data_len); 429#if defined(BORINGSSL_FIPS) 430 // Take a read lock around accesses to |state->drbg|. This is needed to 431 // avoid returning bad entropy if we race with 432 // |rand_thread_state_clear_all|. 433 CRYPTO_MUTEX_lock_read(&state->clear_drbg_lock); 434#endif 435 if (!CTR_DRBG_reseed_ex(&state->drbg, seed, sizeof(seed), 436 reseed_additional_data, 437 reseed_additional_data_len)) { 438 abort(); 439 } 440 state->calls = 0; 441 state->fork_generation = fork_generation; 442 state->fork_unsafe_buffering = fork_unsafe_buffering; 443 } else { 444#if defined(BORINGSSL_FIPS) 445 CRYPTO_MUTEX_lock_read(&state->clear_drbg_lock); 446#endif 447 } 448 449 int first_call = 1; 450 while (out_len > 0) { 451 size_t todo = out_len; 452 if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) { 453 todo = CTR_DRBG_MAX_GENERATE_LENGTH; 454 } 455 456 if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data, 457 first_call ? sizeof(additional_data) : 0)) { 458 abort(); 459 } 460 461 out += todo; 462 out_len -= todo; 463 // Though we only check before entering the loop, this cannot add enough to 464 // overflow a |size_t|. 465 state->calls++; 466 first_call = 0; 467 } 468 469 if (state == &stack_state) { 470 CTR_DRBG_clear(&state->drbg); 471 } 472 473#if defined(BORINGSSL_FIPS) 474 CRYPTO_MUTEX_unlock_read(&state->clear_drbg_lock); 475#endif 476 return bcm_infallible::approved; 477} 478 479bcm_infallible BCM_rand_bytes(uint8_t *out, size_t out_len) { 480 static const uint8_t kZeroAdditionalData[32] = {0}; 481 BCM_rand_bytes_with_additional_data(out, out_len, kZeroAdditionalData); 482 return bcm_infallible::approved; 483} 484