#include <openssl/rand.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#if defined(BORINGSSL_FIPS)
#if !defined(OPENSSL_WINDOWS)
#include <unistd.h>
#else
#include <io.h>
#endif
#include "../../../third_party/jitterentropy/jitterentropy.h"
#endif
#include <openssl/chacha.h>
#include <openssl/mem.h>
#include <openssl/type_check.h>
#include "internal.h"
#include "fork_detect.h"
#include "../../internal.h"
#include "../delocate.h"
#if defined(BORINGSSL_FIPS)
static const unsigned kReseedInterval = 16777216;
#else
static const unsigned kReseedInterval = 4096;
#endif
#define CRNGT_BLOCK_SIZE 16
struct rand_thread_state {
CTR_DRBG_STATE drbg;
uint64_t fork_generation;
unsigned calls;
int last_block_valid;
#if defined(BORINGSSL_FIPS)
uint8_t last_block[CRNGT_BLOCK_SIZE];
struct rand_thread_state *next, *prev;
struct rand_data *jitter_ec;
#endif
};
#if defined(BORINGSSL_FIPS)
DEFINE_BSS_GET(struct rand_thread_state *, thread_states_list)
DEFINE_STATIC_MUTEX(thread_states_list_lock)
DEFINE_STATIC_MUTEX(state_clear_all_lock)
#if defined(_MSC_VER)
#pragma section(".CRT$XCU", read)
static void rand_thread_state_clear_all(void);
static void windows_install_rand_thread_state_clear_all(void) {
atexit(&rand_thread_state_clear_all);
}
__declspec(allocate(".CRT$XCU")) void(*fips_library_destructor)(void) =
windows_install_rand_thread_state_clear_all;
#else
static void rand_thread_state_clear_all(void) __attribute__ ((destructor));
#endif
static void rand_thread_state_clear_all(void) {
CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
CRYPTO_STATIC_MUTEX_lock_write(state_clear_all_lock_bss_get());
for (struct rand_thread_state *cur = *thread_states_list_bss_get();
cur != NULL; cur = cur->next) {
CTR_DRBG_clear(&cur->drbg);
OPENSSL_cleanse(cur->last_block, sizeof(cur->last_block));
jent_entropy_collector_free(cur->jitter_ec);
}
}
#endif
static void rand_thread_state_free(void *state_in) {
struct rand_thread_state *state = state_in;
if (state_in == NULL) {
return;
}
#if defined(BORINGSSL_FIPS)
CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
if (state->prev != NULL) {
state->prev->next = state->next;
} else {
*thread_states_list_bss_get() = state->next;
}
if (state->next != NULL) {
state->next->prev = state->prev;
}
CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
CTR_DRBG_clear(&state->drbg);
OPENSSL_cleanse(state->last_block, sizeof(state->last_block));
jent_entropy_collector_free(state->jitter_ec);
#endif
OPENSSL_free(state);
}
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
!defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
#define RDRAND_MAX_RETRIES 10
OPENSSL_STATIC_ASSERT(RDRAND_MAX_RETRIES > 0, rdrand_max_retries_must_be_positive)
#define CALL_RDRAND_WITH_RETRY(rdrand_func, fail_ret_value) \
for (size_t tries = 0; tries < RDRAND_MAX_RETRIES; tries++) { \
if ((rdrand_func) == 1) { \
break; \
} \
else if (tries >= RDRAND_MAX_RETRIES - 1) { \
return fail_ret_value; \
} \
}
static int rdrand(uint8_t *buf, const size_t len) {
const size_t len_multiple8 = len & ~7;
CALL_RDRAND_WITH_RETRY(CRYPTO_rdrand_multiple8_buf(buf, len_multiple8), 0)
const size_t remainder = len - len_multiple8;
if (remainder != 0) {
assert(remainder < 8);
uint8_t rand_buf[8];
CALL_RDRAND_WITH_RETRY(CRYPTO_rdrand(rand_buf), 0)
OPENSSL_memcpy(buf + len_multiple8, rand_buf, remainder);
}
return 1;
}
#else
static int rdrand(uint8_t *buf, size_t len) {
return 0;
}
#endif
#if defined(BORINGSSL_FIPS)
static void CRYPTO_get_fips_seed(uint8_t *out_entropy, size_t out_entropy_len,
int *out_want_additional_input) {
*out_want_additional_input = 0;
struct rand_thread_state *state =
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
if (state == NULL || state->jitter_ec == NULL) {
abort();
}
size_t num_tries;
for (num_tries = 1; num_tries <= JITTER_MAX_NUM_TRIES; num_tries++) {
if (jent_read_entropy(state->jitter_ec, (char *) out_entropy,
out_entropy_len) == (ssize_t) out_entropy_len) {
break;
}
jent_entropy_collector_free(state->jitter_ec);
state->jitter_ec = NULL;
state->jitter_ec = jent_entropy_collector_alloc(0, JENT_FORCE_FIPS);
if (state->jitter_ec == NULL) {
abort();
}
}
if (num_tries > JITTER_MAX_NUM_TRIES) {
abort();
}
if (boringssl_fips_break_test("CRNG")) {
OPENSSL_memset(out_entropy, 0, out_entropy_len);
}
}
static void rand_get_seed(struct rand_thread_state *state,
uint8_t seed[CTR_DRBG_ENTROPY_LEN],
int *out_want_additional_input) {
if (!state->last_block_valid) {
int unused;
CRYPTO_get_fips_seed(state->last_block, sizeof(state->last_block), &unused);
state->last_block_valid = 1;
}
uint8_t entropy[CTR_DRBG_ENTROPY_LEN];
CRYPTO_get_fips_seed(entropy, sizeof(entropy), out_want_additional_input);
if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
fprintf(stderr, "CRNGT failed.\n");
BORINGSSL_FIPS_abort();
}
OPENSSL_STATIC_ASSERT(sizeof(entropy) % CRNGT_BLOCK_SIZE == 0, _)
for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
i += CRNGT_BLOCK_SIZE) {
if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
CRNGT_BLOCK_SIZE) == 0) {
fprintf(stderr, "CRNGT failed.\n");
BORINGSSL_FIPS_abort();
}
}
OPENSSL_memcpy(state->last_block,
entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
CRNGT_BLOCK_SIZE);
OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
}
#else
static void rand_get_seed(struct rand_thread_state *state,
uint8_t seed[CTR_DRBG_ENTROPY_LEN],
int *out_want_additional_input) {
CRYPTO_sysrand_for_seed(seed, CTR_DRBG_ENTROPY_LEN);
*out_want_additional_input = 0;
}
#endif
void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
const uint8_t user_additional_data[32]) {
if (out_len == 0) {
return;
}
const uint64_t fork_generation = CRYPTO_get_fork_generation();
uint8_t additional_data[32];
if (!have_fast_rdrand() ||
!rdrand(additional_data, sizeof(additional_data))) {
if (fork_generation != 0 || rand_fork_unsafe_buffering_enabled()) {
OPENSSL_memset(additional_data, 0, sizeof(additional_data));
} else if (!have_rdrand()) {
CRYPTO_sysrand(additional_data, sizeof(additional_data));
} else if (!CRYPTO_sysrand_if_available(additional_data,
sizeof(additional_data)) &&
!rdrand(additional_data, sizeof(additional_data))) {
CRYPTO_sysrand(additional_data, sizeof(additional_data));
}
}
for (size_t i = 0; i < sizeof(additional_data); i++) {
additional_data[i] ^= user_additional_data[i];
}
struct rand_thread_state stack_state;
struct rand_thread_state *state =
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
if (state == NULL) {
state = OPENSSL_malloc(sizeof(struct rand_thread_state));
if (state == NULL ||
!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
rand_thread_state_free)) {
state = &stack_state;
}
#if defined(BORINGSSL_FIPS)
state->jitter_ec = NULL;
state->jitter_ec = jent_entropy_collector_alloc(0, JENT_FORCE_FIPS);
if (state->jitter_ec == NULL) {
abort();
}
#endif
state->last_block_valid = 0;
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
int want_additional_input;
rand_get_seed(state, seed, &want_additional_input);
uint8_t personalization[CTR_DRBG_ENTROPY_LEN] = {0};
size_t personalization_len = 0;
#if defined(BORINGSSL_FIPS) && defined(OPENSSL_URANDOM)
CRYPTO_sysrand(personalization, sizeof(personalization));
personalization_len = sizeof(personalization);
#endif
if (!CTR_DRBG_init(&state->drbg, seed, personalization,
personalization_len)) {
abort();
}
state->calls = 0;
state->fork_generation = fork_generation;
#if defined(BORINGSSL_FIPS)
if (state != &stack_state) {
CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
struct rand_thread_state **states_list = thread_states_list_bss_get();
state->next = *states_list;
if (state->next != NULL) {
state->next->prev = state;
}
state->prev = NULL;
*states_list = state;
CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
}
#endif
OPENSSL_cleanse(seed, CTR_DRBG_ENTROPY_LEN);
OPENSSL_cleanse(personalization, CTR_DRBG_ENTROPY_LEN);
}
if (state->calls >= kReseedInterval ||
state->fork_generation != fork_generation) {
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
int want_additional_input;
rand_get_seed(state, seed, &want_additional_input);
uint8_t add_data_for_reseed[CTR_DRBG_ENTROPY_LEN];
size_t add_data_for_reseed_len = 0;
#if defined(BORINGSSL_FIPS)
CRYPTO_STATIC_MUTEX_lock_read(state_clear_all_lock_bss_get());
CRYPTO_sysrand(add_data_for_reseed, sizeof(add_data_for_reseed));
add_data_for_reseed_len = sizeof(add_data_for_reseed);
#endif
if (!CTR_DRBG_reseed(&state->drbg, seed,
add_data_for_reseed, add_data_for_reseed_len)) {
abort();
}
state->calls = 0;
state->fork_generation = fork_generation;
OPENSSL_cleanse(seed, CTR_DRBG_ENTROPY_LEN);
OPENSSL_cleanse(add_data_for_reseed, CTR_DRBG_ENTROPY_LEN);
} else {
#if defined(BORINGSSL_FIPS)
CRYPTO_STATIC_MUTEX_lock_read(state_clear_all_lock_bss_get());
#endif
}
int first_call = 1;
while (out_len > 0) {
size_t todo = out_len;
if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
todo = CTR_DRBG_MAX_GENERATE_LENGTH;
}
if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
first_call ? sizeof(additional_data) : 0)) {
abort();
}
out += todo;
out_len -= todo;
state->calls++;
first_call = 0;
}
if (state == &stack_state) {
CTR_DRBG_clear(&state->drbg);
}
OPENSSL_cleanse(additional_data, 32);
#if defined(BORINGSSL_FIPS)
CRYPTO_STATIC_MUTEX_unlock_read(state_clear_all_lock_bss_get());
#endif
}
int RAND_bytes(uint8_t *out, size_t out_len) {
static const uint8_t kZeroAdditionalData[32] = {0};
RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData);
return 1;
}
int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
return RAND_bytes(buf, len);
}
void RAND_get_system_entropy_for_custom_prng(uint8_t *buf, size_t len) {
if (len > 256) {
abort();
}
CRYPTO_sysrand_for_seed(buf, len);
}