Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/wolfengine/we_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ WOLFENGINE_LOCAL void *we_memdup(const void *data, size_t siz);
extern WC_RNG* we_rng;
#ifndef WE_SINGLE_THREADED
extern wolfSSL_Mutex* we_rng_mutex;
WOLFENGINE_LOCAL int we_rng_mutex_is_initialized(void);
#endif
WOLFENGINE_LOCAL int we_rng_is_initialized(void);
#if defined(WE_HAVE_RANDOM) && !defined(WE_STATIC_WOLFSSL)
WOLFENGINE_LOCAL void we_rand_clear_seed(void);
#endif

WOLFENGINE_LOCAL int we_pkey_get_nids(const int** nids);
Expand Down
35 changes: 33 additions & 2 deletions src/we_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ static int we_globalRngInited = 0;
wolfSSL_Mutex we_global_rng_mutex;
/* Pointer to mutex for protecting use of global random number generator.*/
wolfSSL_Mutex* we_rng_mutex = &we_global_rng_mutex;
/* Global RNG mutex has been initialized. */
static int we_globalRngMutexInited = 0;
#endif

/**
* Get whether the global random number generator is initialized.
*
* @returns 1 when initialized and 0 otherwise.
*/
int we_rng_is_initialized(void)
{
return we_globalRngInited;
}

#ifndef WE_SINGLE_THREADED
/**
* Get whether the global random number generator mutex is initialized.
*
* @returns 1 when initialized and 0 otherwise.
*/
int we_rng_mutex_is_initialized(void)
{
return we_globalRngMutexInited;
}
#endif

/**
Expand Down Expand Up @@ -189,6 +213,9 @@ static int we_init_random(void)
WOLFENGINE_ERROR_FUNC(WE_LOG_ENGINE, "wc_InitRng", rc);
ret = 0;
}
else {
we_globalRngMutexInited = 1;
}
}
#endif
if (ret == 1) {
Expand All @@ -209,11 +236,15 @@ static void we_final_random(void)
WOLFENGINE_ENTER(WE_LOG_ENGINE, "we_final_random");

if (we_globalRngInited) {
#ifndef WE_SINGLE_THREADED
wc_FreeMutex(&we_global_rng_mutex);
#if defined(WE_HAVE_RANDOM) && !defined(WE_STATIC_WOLFSSL)
we_rand_clear_seed();
#endif
wc_FreeRng(&we_globalRng);
we_globalRngInited = 0;
#ifndef WE_SINGLE_THREADED
we_globalRngMutexInited = 0;
wc_FreeMutex(&we_global_rng_mutex);
#endif
}

WOLFENGINE_LEAVE(WE_LOG_ENGINE, "we_final_random", 1);
Expand Down
101 changes: 93 additions & 8 deletions src/we_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ extern int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* seed, word32 seedSz);
unsigned char we_seed[WC_SHA256_DIGEST_SIZE] = {0,};
/* Have added to global seed. */
int haveSeed = 0;
#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
/* Tickets for serializing access to supplemental seed state. */
static wolfSSL_Atomic_Int we_seedLockNext = 0;
static wolfSSL_Atomic_Int we_seedLockServing = 0;

static void we_rand_lock_seed_state(void)
{
int ticket = wolfSSL_Atomic_Int_FetchAdd(&we_seedLockNext, 1);

while (wolfSSL_Atomic_Int_FetchAdd(&we_seedLockServing, 0) != ticket) {
}
}

static void we_rand_unlock_seed_state(void)
{
wolfSSL_Atomic_Int_FetchAdd(&we_seedLockServing, 1);
}
#endif
#endif

#ifndef WE_STATIC_WOLFSSL
Expand All @@ -53,7 +71,7 @@ static int we_rand_mix_seed(unsigned char* buf, int num,
int ret = 1;
int rc;
wc_Sha256 sha256;
unsigned char seedHash[WC_SHA256_DIGEST_SIZE];
unsigned char seedHash[WC_SHA256_DIGEST_SIZE] = {0,};
unsigned char hash[WC_SHA256_DIGEST_SIZE] = {0,};
int i;
int j;
Expand Down Expand Up @@ -140,6 +158,9 @@ static int we_rand_mix_seed(unsigned char* buf, int num,
wc_Sha256Free(&sha256);
}

OPENSSL_cleanse(seedHash, sizeof(seedHash));
OPENSSL_cleanse(hash, sizeof(hash));

WOLFENGINE_LEAVE(WE_LOG_RNG, "we_rand_mix_seed", ret);

return ret;
Expand Down Expand Up @@ -184,10 +205,24 @@ static int we_rand_seed(const void *buf, int num)
ret = 0;
}
#else
#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
we_rand_lock_seed_state();
#endif
/* Mix the seed into the global seed. */
(void)we_rand_mix_seed(we_seed, sizeof(we_seed),
ret = we_rand_mix_seed(we_seed, sizeof(we_seed),
(const unsigned char*)buf, num);
haveSeed = 1;
if (ret == 1) {
haveSeed = 1;
}
else {
OPENSSL_cleanse(we_seed, sizeof(we_seed));
haveSeed = 0;
WOLFENGINE_ERROR_MSG(WE_LOG_RNG,
"we_rand_mix_seed failed");
}
#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
we_rand_unlock_seed_state();
#endif
#endif
#ifndef WE_SINGLE_THREADED
wc_UnLockMutex(we_rng_mutex);
Expand All @@ -203,10 +238,53 @@ static int we_rand_seed(const void *buf, int num)
#endif
}

#ifndef WE_STATIC_WOLFSSL
/**
* Clear the supplemental seed state.
*
* OpenSSL can invoke the RAND cleanup callback after destroying the engine,
* so supplemental seed state uses synchronization independent of the
* engine-owned RNG mutex when atomic operations are available.
*/
void we_rand_clear_seed(void)
{
#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
we_rand_lock_seed_state();
#elif !defined(WE_SINGLE_THREADED)
int locked = 0;
int rc;

if (we_rng_mutex_is_initialized()) {
rc = wc_LockMutex(we_rng_mutex);
if (rc != 0) {
WOLFENGINE_ERROR_FUNC(WE_LOG_RNG, "wc_LockMutex", rc);
return;
}
else {
locked = 1;
}
}
#endif

OPENSSL_cleanse(we_seed, sizeof(we_seed));
haveSeed = 0;

#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
we_rand_unlock_seed_state();
#elif !defined(WE_SINGLE_THREADED)
if (locked) {
wc_UnLockMutex(we_rng_mutex);
}
#endif
}
#endif

static void we_rand_cleanup(void)
{
/* Global random cleanup done in internal.c: we_final_random(). */
WOLFENGINE_ENTER(WE_LOG_RNG, "we_rand_cleanup");
#ifndef WE_STATIC_WOLFSSL
we_rand_clear_seed();
#endif
WOLFENGINE_LEAVE(WE_LOG_RNG, "we_rand_cleanup", 1);
}

Expand All @@ -227,7 +305,7 @@ static int we_rand_add(const void *buf, int num, double entropy)

WOLFENGINE_ENTER(WE_LOG_RNG, "we_rand_add");
WOLFENGINE_MSG_VERBOSE(WE_LOG_RNG, "ARGS [buf = %p, num = %d, "
"entropy = %d]", buf, num, entropy);
"entropy = %f]", buf, num, entropy);

/* Call seed implementation - entropy not used. */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
Expand Down Expand Up @@ -386,6 +464,9 @@ static int we_rand_bytes(unsigned char *buf, int num)
ret = 0;
}
#ifndef WE_STATIC_WOLFSSL
#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
we_rand_lock_seed_state();
#endif
/* Mix global seed if RAND_add() or RAND_seed() has been called. */
if (ret == 1 && haveSeed) {
ret = we_rand_mix_seed(buf, num, we_seed, sizeof(we_seed));
Expand All @@ -394,6 +475,9 @@ static int we_rand_bytes(unsigned char *buf, int num)
"global seed failed");
}
}
#if !defined(WE_SINGLE_THREADED) && defined(WOLFSSL_ATOMIC_OPS)
we_rand_unlock_seed_state();
#endif
/* Mix in weak entropy. */
if (ret == 1) {
ret = we_rand_add_weak_entropy(buf, num);
Expand Down Expand Up @@ -422,11 +506,12 @@ static int we_rand_bytes(unsigned char *buf, int num)
*/
static int we_rand_status(void)
{
int ret = we_rng_is_initialized();

WOLFENGINE_ENTER(WE_LOG_RNG, "we_rand_status");
WOLFENGINE_LEAVE(WE_LOG_RNG, "we_rand_status", 1);
WOLFENGINE_LEAVE(WE_LOG_RNG, "we_rand_status", ret);

/* Always have enough entropy. */
return 1;
return ret;
}

/**
Expand Down
Loading