#include <openssl/rand.h>
#include <openssl/mem.h>
#include <openssl/ctrdrbg.h>
#include <openssl/type_check.h>
#include "entropy/internal.h"
#include "internal.h"
#include "../../internal.h"
#include "../../ube/internal.h"
#include "../delocate.h"
#include "../service_indicator/internal.h"
struct rand_thread_local_state {
CTR_DRBG_STATE drbg;
uint64_t generate_calls_since_seed;
uint64_t reseed_calls_since_initialization;
uint64_t generation_number;
struct entropy_source_t *entropy_source;
struct rand_thread_local_state *next;
struct rand_thread_local_state *previous;
CRYPTO_MUTEX state_clear_lock;
};
OPENSSL_STATIC_ASSERT((sizeof((struct rand_thread_local_state*)0)->generate_calls_since_seed) * 8 >= 48, value_can_overflow)
DEFINE_BSS_GET(struct rand_thread_local_state *, thread_states_list_head)
DEFINE_STATIC_MUTEX(thread_local_states_list_lock)
#if defined(_MSC_VER)
#pragma section(".CRT$XCU", read)
static void rand_thread_local_state_clear_all(void);
static void windows_install_rand_thread_local_state_clear_all(void) {
atexit(&rand_thread_local_state_clear_all);
}
__declspec(allocate(".CRT$XCU")) void(*rand_fips_library_destructor)(void) =
windows_install_rand_thread_local_state_clear_all;
#else
static void rand_thread_local_state_clear_all(void) __attribute__ ((destructor));
#endif
static void rand_thread_local_state_clear_all(void) {
CRYPTO_STATIC_MUTEX_lock_write(thread_local_states_list_lock_bss_get());
for (struct rand_thread_local_state *state = *thread_states_list_head_bss_get();
state != NULL; state = state->next) {
CRYPTO_MUTEX_lock_write(&state->state_clear_lock);
CTR_DRBG_clear(&state->drbg);
}
for (struct rand_thread_local_state *state = *thread_states_list_head_bss_get();
state != NULL; state = state->next) {
state->entropy_source->methods->zeroize_thread(state->entropy_source);
}
}
static void thread_local_list_delete_node(
struct rand_thread_local_state *node_delete) {
CRYPTO_STATIC_MUTEX_lock_write(thread_local_states_list_lock_bss_get());
struct rand_thread_local_state *node_head = *thread_states_list_head_bss_get();
if (node_head == node_delete) {
*thread_states_list_head_bss_get() = node_delete->next;
} else {
if (node_delete->previous != NULL) {
(node_delete->previous)->next = node_delete->next;
}
}
if (node_delete->next != NULL) {
(node_delete->next)->previous = node_delete->previous;
}
CRYPTO_STATIC_MUTEX_unlock_write(thread_local_states_list_lock_bss_get());
}
static void thread_local_list_add_node(
struct rand_thread_local_state *node_add) {
node_add->previous = NULL;
CRYPTO_STATIC_MUTEX_lock_write(thread_local_states_list_lock_bss_get());
struct rand_thread_local_state **thread_states_head = thread_states_list_head_bss_get();
node_add->next = *thread_states_head;
if (*thread_states_head != NULL) {
(*thread_states_head)->previous = node_add;
}
*thread_states_head = node_add;
CRYPTO_STATIC_MUTEX_unlock_write(thread_local_states_list_lock_bss_get());
}
static void rand_thread_local_state_free(void *state_in) {
struct rand_thread_local_state *state = state_in;
if (state_in == NULL) {
return;
}
thread_local_list_delete_node(state);
if (state->entropy_source != NULL) {
state->entropy_source->methods->free_thread(state->entropy_source);
}
OPENSSL_free(state->entropy_source);
OPENSSL_free(state);
}
static int rand_ensure_valid_state(const struct rand_thread_local_state *state) {
#if !defined(AWSLC_SNAPSAFE_TESTING)
uint64_t current_generation_number = 0;
if (CRYPTO_get_ube_generation_number(¤t_generation_number) == 1 &&
current_generation_number != state->generation_number) {
return 0;
}
#endif
return 1;
}
static int rand_check_ctr_drbg_uniqueness(struct rand_thread_local_state *state) {
uint64_t current_generation_number = 0;
if (CRYPTO_get_ube_generation_number(¤t_generation_number) != 1) {
return 0;
}
if (current_generation_number != state->generation_number) {
state->generation_number = current_generation_number;
return 0;
}
return 1;
}
static void rand_maybe_get_ctr_drbg_pred_resistance(
const struct entropy_source_t *entropy_source,
uint8_t pred_resistance[RAND_PRED_RESISTANCE_LEN],
size_t *pred_resistance_len) {
GUARD_PTR_ABORT(entropy_source);
GUARD_PTR_ABORT(pred_resistance_len);
*pred_resistance_len = 0;
if (entropy_source->methods->get_prediction_resistance != NULL) {
if (entropy_source->methods->get_prediction_resistance(
entropy_source, pred_resistance) != 1) {
abort();
}
*pred_resistance_len = RAND_PRED_RESISTANCE_LEN;
}
}
static void rand_get_ctr_drbg_seed_entropy(
const struct entropy_source_t *entropy_source,
uint8_t seed[CTR_DRBG_ENTROPY_LEN],
uint8_t extra_entropy[CTR_DRBG_ENTROPY_LEN],
size_t *extra_entropy_len) {
GUARD_PTR_ABORT(entropy_source);
GUARD_PTR_ABORT(extra_entropy_len);
*extra_entropy_len = 0;
if (entropy_source->methods->get_seed(entropy_source, seed) != 1) {
abort();
}
if (entropy_source->methods->get_extra_entropy != NULL) {
if(entropy_source->methods->get_extra_entropy(
entropy_source, extra_entropy) != 1) {
abort();
}
*extra_entropy_len = CTR_DRBG_ENTROPY_LEN;
}
}
static void rand_ctr_drbg_reseed(struct rand_thread_local_state *state,
const uint8_t seed[CTR_DRBG_ENTROPY_LEN],
const uint8_t additional_data[CTR_DRBG_ENTROPY_LEN],
size_t additional_data_len) {
GUARD_PTR_ABORT(state);
if (CTR_DRBG_reseed(&(state->drbg), seed, additional_data,
additional_data_len) != 1) {
abort();
}
state->reseed_calls_since_initialization++;
state->generate_calls_since_seed = 0;
}
static void rand_state_initialize(struct rand_thread_local_state *state) {
GUARD_PTR_ABORT(state);
state->entropy_source = get_entropy_source();
if (state->entropy_source == NULL) {
abort();
}
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
uint8_t personalization_string[CTR_DRBG_ENTROPY_LEN];
size_t personalization_string_len = 0;
rand_get_ctr_drbg_seed_entropy(state->entropy_source, seed,
personalization_string, &personalization_string_len);
assert(personalization_string_len == 0 ||
personalization_string_len == CTR_DRBG_ENTROPY_LEN);
if (!CTR_DRBG_init(&(state->drbg), seed, personalization_string,
personalization_string_len)) {
abort();
}
state->reseed_calls_since_initialization = 0;
state->generate_calls_since_seed = 0;
uint64_t current_generation_number = 0;
if (CRYPTO_get_ube_generation_number(¤t_generation_number) != 1) {
state->generation_number = 0;
} else {
state->generation_number = current_generation_number;
}
CRYPTO_MUTEX_init(&state->state_clear_lock);
OPENSSL_cleanse(seed, CTR_DRBG_ENTROPY_LEN);
OPENSSL_cleanse(personalization_string, CTR_DRBG_ENTROPY_LEN);
}
static void rand_bytes_core(
struct rand_thread_local_state *state,
uint8_t *out, size_t out_len,
const uint8_t user_pred_resistance[RAND_PRED_RESISTANCE_LEN],
int use_user_pred_resistance, int ctr_drbg_state_is_fresh) {
GUARD_PTR_ABORT(state);
GUARD_PTR_ABORT(out);
int must_reseed_before_generate = 0;
if (rand_check_ctr_drbg_uniqueness(state) != 1 &&
ctr_drbg_state_is_fresh != 1) {
must_reseed_before_generate = 1;
}
size_t first_pred_resistance_len = 0;
uint8_t pred_resistance[RAND_PRED_RESISTANCE_LEN] = {0};
rand_maybe_get_ctr_drbg_pred_resistance(state->entropy_source,
pred_resistance, &first_pred_resistance_len);
if (use_user_pred_resistance == RAND_USE_USER_PRED_RESISTANCE) {
for (size_t i = 0; i < RAND_PRED_RESISTANCE_LEN; i++) {
pred_resistance[i] ^= user_pred_resistance[i];
}
first_pred_resistance_len = RAND_PRED_RESISTANCE_LEN;
}
assert(first_pred_resistance_len == 0 ||
first_pred_resistance_len == RAND_PRED_RESISTANCE_LEN);
CRYPTO_MUTEX_lock_read(&state->state_clear_lock);
while (out_len > 0) {
size_t todo = out_len;
if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
todo = CTR_DRBG_MAX_GENERATE_LENGTH;
}
if (must_reseed_before_generate == 1 ||
(state->generate_calls_since_seed + 1) > kCtrDrbgReseedInterval) {
CRYPTO_MUTEX_unlock_read(&state->state_clear_lock);
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
uint8_t additional_data[CTR_DRBG_ENTROPY_LEN];
size_t additional_data_len = 0;
rand_get_ctr_drbg_seed_entropy(state->entropy_source, seed,
additional_data, &additional_data_len);
CRYPTO_MUTEX_lock_read(&state->state_clear_lock);
rand_ctr_drbg_reseed(state, seed, additional_data,
additional_data_len);
must_reseed_before_generate = 0;
OPENSSL_cleanse(seed, CTR_DRBG_ENTROPY_LEN);
OPENSSL_cleanse(additional_data, CTR_DRBG_ENTROPY_LEN);
}
if (!CTR_DRBG_generate(&(state->drbg), out, todo, pred_resistance,
first_pred_resistance_len)) {
abort();
}
out += todo;
out_len -= todo;
state->generate_calls_since_seed++;
first_pred_resistance_len = 0;
}
OPENSSL_cleanse(pred_resistance, RAND_PRED_RESISTANCE_LEN);
if (rand_ensure_valid_state(state) != 1) {
abort();
}
CRYPTO_MUTEX_unlock_read(&state->state_clear_lock);
}
static void rand_bytes_private(uint8_t *out, size_t out_len,
const uint8_t user_pred_resistance[RAND_PRED_RESISTANCE_LEN],
int use_user_pred_resistance) {
if (out_len == 0) {
return;
}
FIPS_service_indicator_lock_state();
struct rand_thread_local_state *state =
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_PRIVATE_RAND);
int ctr_drbg_state_is_fresh = 0;
if (state == NULL) {
state = OPENSSL_zalloc(sizeof(struct rand_thread_local_state));
if (state == NULL ||
CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_PRIVATE_RAND, state,
rand_thread_local_state_free) != 1) {
abort();
}
rand_state_initialize(state);
thread_local_list_add_node(state);
ctr_drbg_state_is_fresh = 1;
}
rand_bytes_core(state, out, out_len, user_pred_resistance,
use_user_pred_resistance, ctr_drbg_state_is_fresh);
FIPS_service_indicator_unlock_state();
FIPS_service_indicator_update_state();
}
int RAND_bytes_with_user_prediction_resistance(uint8_t *out, size_t out_len,
const uint8_t user_pred_resistance[RAND_PRED_RESISTANCE_LEN]) {
GUARD_PTR_ABORT(user_pred_resistance);
rand_bytes_private(out, out_len, user_pred_resistance,
RAND_USE_USER_PRED_RESISTANCE);
return 1;
}
int RAND_bytes(uint8_t *out, size_t out_len) {
static const uint8_t kZeroPredResistance[RAND_PRED_RESISTANCE_LEN] = {0};
rand_bytes_private(out, out_len, kZeroPredResistance,
RAND_NO_USER_PRED_RESISTANCE);
return 1;
}
int RAND_priv_bytes(uint8_t *out, size_t out_len) {
return RAND_bytes(out, out_len);
}
int RAND_pseudo_bytes(uint8_t *out, size_t out_len) {
return RAND_bytes(out, out_len);
}
uint64_t get_thread_generate_calls_since_seed(void) {
struct rand_thread_local_state *state =
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_PRIVATE_RAND);
if (state == NULL) {
return 0;
}
return state->generate_calls_since_seed;
}
uint64_t get_thread_reseed_calls_since_initialization(void) {
struct rand_thread_local_state *state =
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_PRIVATE_RAND);
if (state == NULL) {
return 0;
}
return state->reseed_calls_since_initialization;
}