#include "jitterentropy.h"
#include "jitterentropy-base.h"
#include "jitterentropy-gcd.h"
#include "jitterentropy-health.h"
#include "jitterentropy-noise.h"
#include "jitterentropy-timer.h"
#include "jitterentropy-sha3.h"
#define MAJVERSION 3
#define MINVERSION 4
#define PATCHLEVEL 0
#ifdef __OPTIMIZE__
#error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
#endif
#define JENT_POWERUP_TESTLOOPCOUNT 1024
JENT_PRIVATE_STATIC
unsigned int jent_version(void)
{
unsigned int version = 0;
version = MAJVERSION * 1000000;
version += MINVERSION * 10000;
version += PATCHLEVEL * 100;
return version;
}
static inline unsigned int jent_log2_simple(unsigned int val)
{
unsigned int idx = 0;
while (val >>= 1)
idx++;
return idx;
}
static inline unsigned int jent_update_memsize(unsigned int flags)
{
unsigned int global_max = JENT_FLAGS_TO_MAX_MEMSIZE(
JENT_MAX_MEMSIZE_MAX);
unsigned int max;
max = JENT_FLAGS_TO_MAX_MEMSIZE(flags);
if (!max) {
max = jent_log2_simple(JENT_MEMORY_SIZE);
max = (max > JENT_MAX_MEMSIZE_OFFSET) ?
max - JENT_MAX_MEMSIZE_OFFSET : 0;
} else {
max++;
}
max = (max > global_max) ? global_max : max;
flags &= ~JENT_MAX_MEMSIZE_MASK;
flags |= JENT_MAX_MEMSIZE_TO_FLAGS(max);
return flags;
}
JENT_PRIVATE_STATIC
ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len)
{
char *p = data;
size_t orig_len = len;
int ret = 0;
if (NULL == ec)
return -1;
if (jent_notime_settick(ec))
return -4;
while (len > 0) {
size_t tocopy;
unsigned int health_test_result;
jent_random_data(ec);
if ((health_test_result = jent_health_failure(ec))) {
if (health_test_result & JENT_RCT_FAILURE)
ret = -2;
else if (health_test_result & JENT_APT_FAILURE)
ret = -3;
else
ret = -5;
goto err;
}
if ((DATA_SIZE_BITS / 8) < len)
tocopy = (DATA_SIZE_BITS / 8);
else
tocopy = len;
jent_read_random_block(ec, p, tocopy);
len -= tocopy;
p += tocopy;
}
#ifndef CONFIG_CRYPTO_CPU_JITTERENTROPY_SECURE_MEMORY
jent_read_random_block(ec, NULL, 0);
#endif
err:
jent_notime_unsettick(ec);
return ret ? ret : (ssize_t)orig_len;
}
static struct rand_data *_jent_entropy_collector_alloc(unsigned int osr,
unsigned int flags);
JENT_PRIVATE_STATIC
ssize_t jent_read_entropy_safe(struct rand_data **ec, char *data, size_t len)
{
char *p = data;
size_t orig_len = len;
ssize_t ret = 0;
if (!ec)
return -1;
while (len > 0) {
unsigned int osr, flags, max_mem_set;
ret = jent_read_entropy(*ec, p, len);
switch (ret) {
case -1:
case -4:
return ret;
case -2:
case -3:
case -5:
osr = (*ec)->osr + 1;
flags = (*ec)->flags;
max_mem_set = (*ec)->max_mem_set;
if (osr > 20)
return ret;
if (!max_mem_set)
flags = jent_update_memsize(flags);
jent_entropy_collector_free(*ec);
if (jent_entropy_init_ex(osr, flags))
return -1;
*ec = _jent_entropy_collector_alloc(osr, flags);
if (!*ec)
return -1;
(*ec)->max_mem_set = !!max_mem_set;
break;
default:
len -= (size_t)ret;
p += (size_t)ret;
}
}
return (ssize_t)orig_len;
}
static inline uint32_t jent_memsize(unsigned int flags)
{
uint32_t memsize, max_memsize;
max_memsize = JENT_FLAGS_TO_MAX_MEMSIZE(flags);
if (max_memsize == 0) {
max_memsize = JENT_MEMORY_SIZE;
} else {
max_memsize = UINT32_C(1) << (max_memsize +
JENT_MAX_MEMSIZE_OFFSET);
}
memsize = jent_cache_size_roundup();
memsize = (memsize > max_memsize) ? max_memsize : memsize;
if (!memsize)
memsize = JENT_MEMORY_SIZE;
return memsize;
}
static int jent_selftest_run = 0;
static struct rand_data
*jent_entropy_collector_alloc_internal(unsigned int osr, unsigned int flags)
{
struct rand_data *entropy_collector;
uint32_t memsize = 0;
if ((flags & JENT_DISABLE_INTERNAL_TIMER) &&
(flags & JENT_FORCE_INTERNAL_TIMER))
return NULL;
if (!jent_selftest_run && jent_entropy_init_ex(osr, flags))
return NULL;
if (jent_notime_forced() && (flags & JENT_DISABLE_INTERNAL_TIMER))
return NULL;
entropy_collector = jent_zalloc(sizeof(struct rand_data));
if (NULL == entropy_collector)
return NULL;
if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
memsize = jent_memsize(flags);
entropy_collector->mem = (unsigned char *)jent_zalloc(memsize);
#ifdef JENT_RANDOM_MEMACCESS
entropy_collector->memmask = memsize - 1;
#else
entropy_collector->memblocksize = memsize / JENT_MEMORY_BLOCKS;
entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
if (entropy_collector->memblocksize *
entropy_collector->memblocks != memsize)
goto err;
#endif
if (entropy_collector->mem == NULL)
goto err;
entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
}
if (sha3_alloc(&entropy_collector->hash_state))
goto err;
sha3_256_init(entropy_collector->hash_state);
if (osr < JENT_MIN_OSR)
osr = JENT_MIN_OSR;
entropy_collector->osr = osr;
entropy_collector->flags = flags;
if ((flags & JENT_FORCE_FIPS) || jent_fips_enabled())
entropy_collector->fips_enabled = 1;
jent_apt_init(entropy_collector, osr);
jent_lag_init(entropy_collector, osr);
if (jent_gcd_get(&entropy_collector->jent_common_timer_gcd)) {
entropy_collector->jent_common_timer_gcd = 1;
}
if (!(flags & JENT_DISABLE_INTERNAL_TIMER)) {
if (jent_notime_enable(entropy_collector, flags))
goto err;
}
return entropy_collector;
err:
if (entropy_collector->mem != NULL)
jent_zfree(entropy_collector->mem, memsize);
jent_zfree(entropy_collector, sizeof(struct rand_data));
return NULL;
}
static struct rand_data *_jent_entropy_collector_alloc(unsigned int osr,
unsigned int flags)
{
struct rand_data *ec = jent_entropy_collector_alloc_internal(osr,
flags);
if (!ec)
return ec;
if (jent_notime_settick(ec)) {
jent_entropy_collector_free(ec);
return NULL;
}
jent_random_data(ec);
jent_notime_unsettick(ec);
return ec;
}
JENT_PRIVATE_STATIC
struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
unsigned int flags)
{
struct rand_data *ec = _jent_entropy_collector_alloc(osr, flags);
if (ec)
ec->max_mem_set = !!JENT_FLAGS_TO_MAX_MEMSIZE(flags);
return ec;
}
JENT_PRIVATE_STATIC
void jent_entropy_collector_free(struct rand_data *entropy_collector)
{
if (entropy_collector != NULL) {
sha3_dealloc(entropy_collector->hash_state);
jent_notime_disable(entropy_collector);
if (entropy_collector->mem != NULL) {
jent_zfree(entropy_collector->mem,
jent_memsize(entropy_collector->flags));
entropy_collector->mem = NULL;
}
jent_zfree(entropy_collector, sizeof(struct rand_data));
}
}
int jent_time_entropy_init(unsigned int osr, unsigned int flags)
{
struct rand_data *ec;
uint64_t *delta_history;
int i, time_backwards = 0, count_stuck = 0, ret = 0;
unsigned int health_test_result;
delta_history = jent_gcd_init(JENT_POWERUP_TESTLOOPCOUNT);
if (!delta_history)
return EMEM;
if (flags & JENT_FORCE_INTERNAL_TIMER)
jent_notime_force();
else
flags |= JENT_DISABLE_INTERNAL_TIMER;
flags |= JENT_FORCE_FIPS;
ec = jent_entropy_collector_alloc_internal(osr, flags);
if (!ec) {
ret = EMEM;
goto out;
}
if (jent_notime_settick(ec)) {
ret = EMEM;
goto out;
}
jent_measure_jitter(ec, 0, NULL);
#define CLEARCACHE 100
for (i = -CLEARCACHE; i < JENT_POWERUP_TESTLOOPCOUNT; i++) {
uint64_t start_time = 0, end_time = 0, delta = 0;
unsigned int stuck;
stuck = jent_measure_jitter(ec, 0, &delta);
end_time = ec->prev_time;
start_time = ec->prev_time - delta;
if (!start_time || !end_time) {
ret = ENOTIME;
goto out;
}
if (!delta || (end_time == start_time)) {
ret = ECOARSETIME;
goto out;
}
if (i < 0)
continue;
if (stuck)
count_stuck++;
if (!(end_time > start_time))
time_backwards++;
jent_gcd_add_value(delta_history, delta, i);
}
if (time_backwards > 3) {
ret = ENOMONOTONIC;
goto out;
}
if ((health_test_result = jent_health_failure(ec))) {
ret = (health_test_result & JENT_RCT_FAILURE) ? ERCT : EHEALTH;
goto out;
}
ret = jent_gcd_analyze(delta_history, JENT_POWERUP_TESTLOOPCOUNT);
if (ret)
goto out;
if (JENT_STUCK_INIT_THRES(JENT_POWERUP_TESTLOOPCOUNT) < count_stuck)
ret = ESTUCK;
out:
jent_gcd_fini(delta_history, JENT_POWERUP_TESTLOOPCOUNT);
if ((flags & JENT_FORCE_INTERNAL_TIMER) && ec)
jent_notime_unsettick(ec);
jent_entropy_collector_free(ec);
return ret;
}
static inline int jent_entropy_init_common_pre(void)
{
int ret;
jent_notime_block_switch();
jent_health_cb_block_switch();
if (sha3_tester())
return EHASH;
ret = jent_gcd_selftest();
jent_selftest_run = 1;
return ret;
}
static inline int jent_entropy_init_common_post(int ret)
{
if (ret)
jent_selftest_run = 0;
return ret;
}
JENT_PRIVATE_STATIC
int jent_entropy_init(void)
{
int ret = jent_entropy_init_common_pre();
if (ret)
return ret;
ret = jent_time_entropy_init(0, JENT_DISABLE_INTERNAL_TIMER);
#ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
if (ret)
ret = jent_time_entropy_init(0, JENT_FORCE_INTERNAL_TIMER);
#endif
return jent_entropy_init_common_post(ret);
}
JENT_PRIVATE_STATIC
int jent_entropy_init_ex(unsigned int osr, unsigned int flags)
{
int ret = jent_entropy_init_common_pre();
if (ret)
return ret;
ret = ENOTIME;
if (!(flags & JENT_FORCE_INTERNAL_TIMER))
ret = jent_time_entropy_init(osr,
flags | JENT_DISABLE_INTERNAL_TIMER);
#ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
if (ret && !(flags & JENT_DISABLE_INTERNAL_TIMER))
ret = jent_time_entropy_init(osr,
flags | JENT_FORCE_INTERNAL_TIMER);
#endif
return jent_entropy_init_common_post(ret);
}
JENT_PRIVATE_STATIC
int jent_entropy_switch_notime_impl(struct jent_notime_thread *new_thread)
{
return jent_notime_switch(new_thread);
}
JENT_PRIVATE_STATIC
int jent_set_fips_failure_callback(jent_fips_failure_cb cb)
{
return jent_set_fips_failure_callback_internal(cb);
}