#include <openssl/base.h>
#include <openssl/target.h>
#include "internal.h"
#include "../internal.h"
#include "../../delocate.h"
#include "../../../rand_extra/internal.h"
#include "../../../ube/snapsafe_detect.h"
DEFINE_BSS_GET(const struct entropy_source_methods *, entropy_source_methods_override)
DEFINE_BSS_GET(int, allow_entropy_source_methods_override)
DEFINE_STATIC_MUTEX(global_entropy_source_lock)
static int entropy_get_prediction_resistance(
const struct entropy_source_t *entropy_source,
uint8_t pred_resistance[RAND_PRED_RESISTANCE_LEN]) {
#if defined(OPENSSL_X86_64)
if (rdrand_multiple8(pred_resistance, RAND_PRED_RESISTANCE_LEN) == 1) {
return 1;
}
#elif defined(OPENSSL_AARCH64)
if (rndr_multiple8(pred_resistance, RAND_PRED_RESISTANCE_LEN) == 1) {
return 1;
}
#endif
return 0;
}
static int entropy_get_extra_entropy(
const struct entropy_source_t *entropy_source,
uint8_t extra_entropy[CTR_DRBG_ENTROPY_LEN]) {
CRYPTO_sysrand(extra_entropy, CTR_DRBG_ENTROPY_LEN);
return 1;
}
DEFINE_LOCAL_DATA(struct entropy_source_methods, tree_jitter_entropy_source_methods) {
out->initialize = tree_jitter_initialize;
out->zeroize_thread = tree_jitter_zeroize_thread_drbg;
out->free_thread = tree_jitter_free_thread_drbg;
out->get_seed = tree_jitter_get_seed;
out->get_extra_entropy = entropy_get_extra_entropy;
if (have_hw_rng_x86_64() == 1 ||
have_hw_rng_aarch64() == 1) {
out->get_prediction_resistance = entropy_get_prediction_resistance;
} else {
out->get_prediction_resistance = NULL;
}
out->id = TREE_DRBG_JITTER_ENTROPY_SOURCE;
}
static int snapsafe_fallback_initialize(
struct entropy_source_t *entropy_source) {
return 1;
}
static void snapsafe_fallback_zeroize_thread(struct entropy_source_t *entropy_source) {}
static void snapsafe_fallback_free_thread(struct entropy_source_t *entropy_source) {}
static int snapsafe_fallback_get_seed_wrap(
const struct entropy_source_t *entropy_source, uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
return snapsafe_fallback_get_seed(seed);
}
static int use_snapsafe_fallback_entropy(void) {
return CRYPTO_get_snapsafe_supported();
}
DEFINE_LOCAL_DATA(struct entropy_source_methods, snapsafe_fallback_entropy_source_methods) {
out->initialize = snapsafe_fallback_initialize;
out->zeroize_thread = snapsafe_fallback_zeroize_thread;
out->free_thread = snapsafe_fallback_free_thread;
out->get_seed = snapsafe_fallback_get_seed_wrap;
if (have_hw_rng_x86_64() == 1 ||
have_hw_rng_aarch64() == 1) {
out->get_extra_entropy = entropy_get_prediction_resistance;
} else {
out->get_extra_entropy = snapsafe_fallback_get_seed_wrap;
}
out->get_prediction_resistance = NULL;
out->id = SNAPSAFE_FALLBACK_ENTROPY_SOURCE;
}
static const struct entropy_source_methods * get_entropy_source_methods(void) {
if (*allow_entropy_source_methods_override_bss_get() == 1) {
return *entropy_source_methods_override_bss_get();
}
if (use_snapsafe_fallback_entropy()) {
return snapsafe_fallback_entropy_source_methods();
}
return tree_jitter_entropy_source_methods();
}
struct entropy_source_t * get_entropy_source(void) {
struct entropy_source_t *entropy_source = OPENSSL_zalloc(sizeof(struct entropy_source_t));
if (entropy_source == NULL) {
return NULL;
}
entropy_source->methods = get_entropy_source_methods();
if (entropy_source->methods == NULL ||
entropy_source->methods->zeroize_thread == NULL ||
entropy_source->methods->free_thread == NULL ||
entropy_source->methods->get_seed == NULL ||
entropy_source->methods->initialize == NULL ||
entropy_source->methods->initialize(entropy_source) != 1) {
OPENSSL_free(entropy_source);
return NULL;
}
return entropy_source;
}
int rndr_multiple8(uint8_t *buf, const size_t len) {
if (len == 0 || ((len & 0x7) != 0)) {
return 0;
}
return CRYPTO_rndr_multiple8(buf, len);
}
int have_hw_rng_aarch64_for_testing(void) {
return have_hw_rng_aarch64();
}
#define RDRAND_MAX_RETRIES 10
OPENSSL_STATIC_ASSERT(RDRAND_MAX_RETRIES > 0, rdrand_max_retries_must_be_positive)
int rdrand_multiple8(uint8_t *buf, size_t len) {
if (len == 0 || ((len & 0x7) != 0)) {
return 0;
}
for (size_t tries = 0; tries < RDRAND_MAX_RETRIES; tries++) {
if (CRYPTO_rdrand_multiple8(buf, len) == 1) {
return 1;
}
}
return 0;
}
int have_hw_rng_x86_64_for_testing(void) {
return have_hw_rng_x86_64();
}
void override_entropy_source_method_FOR_TESTING(
const struct entropy_source_methods *override_entropy_source_methods) {
CRYPTO_STATIC_MUTEX_lock_write(global_entropy_source_lock_bss_get());
*allow_entropy_source_methods_override_bss_get() = 1;
*entropy_source_methods_override_bss_get() = override_entropy_source_methods;
CRYPTO_STATIC_MUTEX_unlock_write(global_entropy_source_lock_bss_get());
}
int get_entropy_source_method_id_FOR_TESTING(void) {
int id;
CRYPTO_STATIC_MUTEX_lock_read(global_entropy_source_lock_bss_get());
const struct entropy_source_methods *entropy_source_method = get_entropy_source_methods();
id = entropy_source_method->id;
CRYPTO_STATIC_MUTEX_unlock_read(global_entropy_source_lock_bss_get());
return id;
}