#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef HAVE_ENTROPY_MEMUSE
#include <wolfssl/wolfcrypt/wolfentropy.h>
#include <wolfssl/wolfcrypt/sha3.h>
#if defined(__APPLE__) || defined(__MACH__)
#include <mach/mach_time.h>
#endif
#define MAX_ENTROPY_BYTES (MAX_ENTROPY_BITS / 8)
#define ENTROPY_BITS_USED 8
#define ENTROPY_MIN 1
#define ENTROPY_EXTRA 64
#define MAX_NOISE_CNT (MAX_ENTROPY_BITS * 8 + ENTROPY_EXTRA)
static volatile int entropy_memuse_initialized = 0;
static wc_Sha3 entropyHash;
static void Entropy_HealthTest_Reset(void);
#ifdef CUSTOM_ENTROPY_TIMEHIRES
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
return CUSTOM_ENTROPY_TIMEHIRES();
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && \
(defined(__x86_64__) || defined(__i386__))
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
unsigned int lo_c, hi_c;
__asm__ __volatile__ (
"rdtsc"
: "=a"(lo_c), "=d"(hi_c)
: "a"(0)
: "%ebx", "%ecx");
return ((word64)lo_c) | (((word64)hi_c) << 32);
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && \
(defined(__APPLE__) || defined(__MACH__))
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && defined(__aarch64__)
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
word64 cnt;
__asm__ __volatile__ (
"mrs %[cnt], cntvct_el0"
: [cnt] "=r"(cnt)
:
:
);
return cnt;
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && defined(__MICROBLAZE__)
#define LPD_SCNTR_BASE_ADDRESS 0xFF250000
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
word64 cnt;
word32 *ptr;
ptr = (word32*)LPD_SCNTR_BASE_ADDRESS;
cnt = *(ptr+1);
cnt = cnt << 32;
cnt |= *ptr;
return cnt;
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && defined(_POSIX_C_SOURCE) && \
(_POSIX_C_SOURCE >= 199309L)
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
return now.tv_nsec;
}
#elif defined(_WIN32)
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return (word64)(count.QuadPart);
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && defined(__arm__)
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
word32 lo, hi;
__asm__ __volatile__ (
"mrrc p15, 1, %[lo], %[hi], c14"
: [lo] "=r"(lo), [hi] "=r"(hi)
);
return ((word64)hi << 32) | lo;
}
#elif defined(WOLFSSL_THREAD_NO_JOIN)
#define ENTROPY_MEMUSE_THREADED
typedef struct ENTROPY_THREAD_DATA {
word64 counter;
int stop;
} ENTROPY_THREAD_DATA;
static int entropy_thread_started = 0;
static volatile ENTROPY_THREAD_DATA entropy_thread_data = { 0, 0 };
static WC_INLINE word64 Entropy_TimeHiRes(void)
{
return entropy_thread_data.counter;
}
static THREAD_RETURN_NOJOIN WOLFSSL_THREAD_NO_JOIN
Entropy_IncCounter(void* args)
{
(void)args;
while (!entropy_thread_data.stop) {
entropy_thread_data.counter++;
}
#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
fprintf(stderr, "EXITING ENTROPY COUNTER THREAD\n");
#endif
RETURN_FROM_THREAD_NOJOIN(0);
}
static int Entropy_StartThread(void)
{
int ret = 0;
if (!entropy_thread_started) {
word64 start_counter = entropy_thread_data.counter;
entropy_thread_data.stop = 0;
#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
fprintf(stderr, "STARTING ENTROPY COUNTER THREAD\n");
#endif
ret = wolfSSL_NewThreadNoJoin(Entropy_IncCounter, NULL);
if (ret == 0) {
while (entropy_thread_data.counter == start_counter) {
sched_yield();
}
}
entropy_thread_started = (ret == 0);
}
return ret;
}
static void Entropy_StopThread(void)
{
if (entropy_thread_started) {
entropy_thread_data.stop = 1;
entropy_thread_started = 0;
}
}
#else
#error "No high precision time available for MemUse Entropy."
#endif
#ifndef ENTROPY_NUM_WORDS_BITS
#define ENTROPY_NUM_WORDS_BITS 14
#endif
#if ENTROPY_NUM_WORDS_BITS < 8
#error "ENTROPY_NUM_WORDS_BITS must be 8 or more"
#elif ENTROPY_NUM_WORDS_BITS > 30
#error "ENTROPY_NUM_WORDS_BITS must be less than 31"
#endif
#define ENTROPY_NUM_WORDS (1 << ENTROPY_NUM_WORDS_BITS)
#define ENTROPY_BLOCK_SZ (ENTROPY_NUM_WORDS_BITS - 8)
#ifndef ENTROPY_NUM_UPDATES
#define ENTROPY_NUM_UPDATES 18
#define ENTROPY_NUM_UPDATES_BITS 5
#elif !defined(ENTROPY_NUM_UPDATES_BITS)
#define ENTROPY_NUM_UPDATES_BITS ENTROPY_BLOCK_SZ
#endif
#ifndef ENTROPY_NUM_UPDATES_BITS
#error "ENTROPY_NUM_UPDATES_BITS must be defined - " \
"upper(log2(ENTROPY_NUM_UPDATES))"
#endif
#if ENTROPY_NUM_UPDATES_BITS != 0
#define ENTROPY_OFFSET_SHIFTING \
(ENTROPY_BLOCK_SZ / ENTROPY_NUM_UPDATES_BITS)
#else
#define ENTROPY_OFFSET_SHIFTING ENTROPY_BLOCK_SZ
#endif
#ifndef ENTROPY_NUM_64BIT_WORDS
#define ENTROPY_NUM_64BIT_WORDS WC_SHA3_256_DIGEST_SIZE
#elif ENTROPY_NUM_64BIT_WORDS > WC_SHA3_256_DIGEST_SIZE
#error "ENTROPY_NUM_64BIT_WORDS must be <= SHA3-256 digest size in bytes"
#endif
#if ENTROPY_BLOCK_SZ < ENTROPY_NUM_UPDATES_BITS
#define EXTRA_ENTROPY_WORDS ENTROPY_NUM_UPDATES
#else
#define EXTRA_ENTROPY_WORDS 0
#endif
static word64 entropy_state[ENTROPY_NUM_WORDS + EXTRA_ENTROPY_WORDS] = {0};
static void Entropy_MemUse(void)
{
int i;
static byte d[WC_SHA3_256_DIGEST_SIZE];
int j;
for (j = 0; j < ENTROPY_NUM_UPDATES; j++) {
wc_Sha3_256_Update(&entropyHash, (byte*)entropy_state,
sizeof(*entropy_state) * ENTROPY_NUM_64BIT_WORDS);
wc_Sha3_256_Final(&entropyHash, d);
for (i = 0; i < ENTROPY_NUM_64BIT_WORDS; i++) {
int idx = ((int)d[i] << ENTROPY_BLOCK_SZ) +
(j << ENTROPY_OFFSET_SHIFTING);
entropy_state[idx] += Entropy_TimeHiRes();
entropy_state[i] += entropy_state[idx];
}
}
}
static word64 entropy_last_time = 0;
static word64 Entropy_GetSample(void)
{
word64 now;
word64 ret;
#ifdef HAVE_FIPS
if (entropy_last_time == 0) {
Entropy_MemUse();
entropy_last_time = Entropy_TimeHiRes();
}
#endif
Entropy_MemUse();
now = Entropy_TimeHiRes();
ret = now - entropy_last_time;
entropy_last_time = now;
return ret;
}
static void Entropy_GetNoise(unsigned char* noise, int samples)
{
int i;
Entropy_MemUse();
for (i = 0; i < samples; i++) {
noise[i] = (byte)Entropy_GetSample();
}
}
int wc_Entropy_GetRawEntropy(unsigned char* raw, int cnt)
{
int ret = 0;
#ifdef ENTROPY_MEMUSE_THREADED
ret = Entropy_StartThread();
if (ret == 0)
#endif
{
Entropy_GetNoise(raw, cnt);
}
#ifdef ENTROPY_MEMUSE_THREADED
Entropy_StopThread();
#endif
return ret;
}
#if ENTROPY_MIN == 1
#define REP_CUTOFF 31
#else
#error "Minimum entropy not defined to a recognized value."
#endif
static int rep_have_prev = 0;
static byte rep_prev_noise;
static void Entropy_HealthTest_Repetition_Reset(void)
{
rep_have_prev = 0;
rep_prev_noise = 0;
}
static int Entropy_HealthTest_Repetition(byte noise)
{
int ret = 0;
static int rep_cnt = 0;
if (!rep_have_prev) {
rep_prev_noise = noise;
rep_have_prev = 1;
rep_cnt = 1;
}
else if (noise == rep_prev_noise) {
rep_cnt++;
if (rep_cnt >= REP_CUTOFF) {
#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
fprintf(stderr, "REPETITION FAILED: %d\n", noise);
#endif
Entropy_HealthTest_Repetition_Reset();
ret = ENTROPY_RT_E;
}
}
else {
rep_prev_noise = noise;
rep_cnt = 1;
}
return ret;
}
#define PROP_WINDOW_SIZE 512
#if ENTROPY_MIN == 1
#define PROP_CUTOFF 325
#else
#error "Minimum entropy not defined to a recognized value."
#endif
static word16 prop_total = 0;
static word16 prop_first = 0;
static word16 prop_last = 0;
static word16 prop_cnt[1 << ENTROPY_BITS_USED] = { 0 };
static word16 prop_samples[PROP_WINDOW_SIZE];
static void Entropy_HealthTest_Proportion_Reset(void)
{
XMEMSET(prop_samples, 0, sizeof(prop_samples));
XMEMSET(prop_cnt, 0, sizeof(prop_cnt));
prop_total = 0;
prop_first = 0;
prop_last = 0;
}
static int Entropy_HealthTest_Proportion(byte noise)
{
int ret = 0;
if (prop_total < PROP_CUTOFF - 1) {
prop_samples[prop_last++] = noise;
prop_cnt[noise]++;
prop_total++;
}
else {
prop_samples[prop_last] = noise;
prop_last = (prop_last + 1) % PROP_WINDOW_SIZE;
prop_cnt[noise]++;
prop_total++;
if (prop_cnt[noise] >= PROP_CUTOFF) {
#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
fprintf(stderr, "PROPORTION FAILED: %d %d\n", val, prop_cnt[noise]);
#endif
Entropy_HealthTest_Proportion_Reset();
ret = ENTROPY_APT_E;
}
else if (prop_total == PROP_WINDOW_SIZE) {
byte val = (byte)prop_samples[prop_first];
prop_first = (prop_first + 1) % PROP_WINDOW_SIZE;
prop_cnt[val]--;
prop_total--;
}
}
return ret;
}
#define ENTROPY_INITIAL_COUNT (1024 + PROP_WINDOW_SIZE)
static int Entropy_HealthTest_Startup(void)
{
int ret = 0;
byte initial[ENTROPY_INITIAL_COUNT];
int i;
#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
fprintf(stderr, "STARTUP HEALTH TEST\n");
#endif
Entropy_HealthTest_Reset();
Entropy_GetNoise(initial, ENTROPY_INITIAL_COUNT);
for (i = 0; (ret == 0) && (i < ENTROPY_INITIAL_COUNT); i++) {
ret = Entropy_HealthTest_Repetition(initial[i]);
if (ret == 0) {
ret = Entropy_HealthTest_Proportion(initial[i]);
}
}
if (ret != 0) {
Entropy_HealthTest_Reset();
}
return ret;
}
static int Entropy_Condition(byte* output, word32 len, byte* noise,
word32 noise_len)
{
int ret;
ret = wc_Sha3_256_Update(&entropyHash, noise, noise_len);
if (ret == 0) {
word64 now = Entropy_TimeHiRes();
ret = wc_Sha3_256_Update(&entropyHash, (byte*)&now, sizeof(now));
}
if (ret == 0) {
if (len == WC_SHA3_256_DIGEST_SIZE) {
ret = wc_Sha3_256_Final(&entropyHash, output);
}
else {
byte hash[WC_SHA3_256_DIGEST_SIZE];
ret = wc_Sha3_256_Final(&entropyHash, hash);
if (ret == 0) {
XMEMCPY(output, hash, len);
}
}
}
return ret;
}
static wolfSSL_Mutex entropy_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(entropy_mutex);
int wc_Entropy_Get(int bits, unsigned char* entropy, word32 len)
{
int ret = 0;
int noise_len = (bits + ENTROPY_EXTRA) / ENTROPY_MIN;
static byte noise[MAX_NOISE_CNT];
#ifdef HAVE_FIPS
if (!entropy_memuse_initialized) {
ret = Entropy_Init();
}
#endif
if ((ret == 0) && (wc_LockMutex(&entropy_mutex) != 0)) {
ret = BAD_MUTEX_E;
}
#ifdef ENTROPY_MEMUSE_THREADED
if (ret == 0) {
ret = Entropy_StartThread();
}
#endif
if ((ret == 0) && ((prop_total == 0) || (!rep_have_prev))) {
ret = Entropy_HealthTest_Startup();
}
while ((ret == 0) && (len > 0)) {
int i;
word32 entropy_len = WC_SHA3_256_DIGEST_SIZE;
if (len < entropy_len) {
entropy_len = len;
}
Entropy_GetNoise(noise, noise_len);
for (i = 0; (ret == 0) && (i < noise_len); i++) {
ret = Entropy_HealthTest_Repetition(noise[i]);
if (ret == 0) {
ret = Entropy_HealthTest_Proportion(noise[i]);
}
}
if (ret == 0) {
ret = Entropy_Condition(entropy, entropy_len, noise, noise_len);
}
if (ret == 0) {
entropy += entropy_len;
len -= entropy_len;
}
if (ret == 0) {
ret = WC_CHECK_FOR_INTR_SIGNALS();
}
if (ret == 0) {
WC_RELAX_LONG_LOOP();
}
}
#ifdef ENTROPY_MEMUSE_THREADED
Entropy_StopThread();
#endif
if (ret != WC_NO_ERR_TRACE(BAD_MUTEX_E)) {
wc_UnLockMutex(&entropy_mutex);
}
return ret;
}
int wc_Entropy_OnDemandTest(void)
{
int ret = 0;
if (wc_LockMutex(&entropy_mutex) != 0) {
ret = BAD_MUTEX_E;
}
if (ret == 0) {
ret = Entropy_HealthTest_Startup();
}
if (ret != WC_NO_ERR_TRACE(BAD_MUTEX_E)) {
wc_UnLockMutex(&entropy_mutex);
}
return ret;
}
int Entropy_Init(void)
{
int ret = 0;
if (!entropy_memuse_initialized) {
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER)
ret = wc_InitMutex(&entropy_mutex);
#endif
if (ret == 0)
ret = wc_LockMutex(&entropy_mutex);
if (entropy_memuse_initialized) {
if (ret == 0)
wc_UnLockMutex(&entropy_mutex);
return 0;
}
if (ret == 0) {
ret = wc_InitSha3_256(&entropyHash, NULL, INVALID_DEVID);
}
entropy_memuse_initialized = (ret == 0);
if (ret == 0) {
#ifdef ENTROPY_MEMUSE_THREADED
ret = Entropy_StartThread();
if (ret == 0)
#endif
{
ret = Entropy_HealthTest_Startup();
}
#ifdef ENTROPY_MEMUSE_THREADED
Entropy_StopThread();
#endif
}
if (ret != WC_NO_ERR_TRACE(BAD_MUTEX_E)) {
wc_UnLockMutex(&entropy_mutex);
}
}
return ret;
}
void Entropy_Final(void)
{
if (entropy_memuse_initialized) {
wc_Sha3_256_Free(&entropyHash);
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER)
wc_FreeMutex(&entropy_mutex);
#endif
Entropy_HealthTest_Reset();
entropy_memuse_initialized = 0;
}
}
static void Entropy_HealthTest_Reset(void)
{
Entropy_HealthTest_Repetition_Reset();
Entropy_HealthTest_Proportion_Reset();
}
#endif