Skip to content

Commit a2224a8

Browse files
committed
wc_Entropy_GetRawEntropy: hold entropy_mutex
1 parent 2020c3f commit a2224a8

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

wolfcrypt/src/wolfentropy.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,11 @@ static void Entropy_GetNoise(unsigned char* noise, int samples)
440440
}
441441
}
442442

443+
/* Mutex to prevent multiple callers requesting entropy operations at the
444+
* same time.
445+
*/
446+
static wolfSSL_Mutex entropy_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(entropy_mutex);
447+
443448
/* Generate raw entropy for performing assessment.
444449
*
445450
* @param [out] raw Buffer to hold raw entropy data.
@@ -452,19 +457,35 @@ int wc_Entropy_GetRawEntropy(unsigned char* raw, int cnt)
452457
{
453458
int ret = 0;
454459

460+
#ifdef HAVE_FIPS
461+
if (!entropy_memuse_initialized) {
462+
ret = Entropy_Init();
463+
}
464+
#endif
465+
466+
/* Lock the mutex as collection uses globals. */
467+
if ((ret == 0) && (wc_LockMutex(&entropy_mutex) != 0)) {
468+
ret = BAD_MUTEX_E;
469+
}
470+
455471
#ifdef ENTROPY_MEMUSE_THREADED
456-
/* Start the counter thread as a proxy for time counter. */
457-
ret = Entropy_StartThread();
458-
if (ret == 0)
472+
if (ret == 0) {
473+
/* Start the counter thread as a proxy for time counter. */
474+
ret = Entropy_StartThread();
475+
}
459476
#endif
460-
{
477+
if (ret == 0) {
461478
Entropy_GetNoise(raw, cnt);
462479
}
463480
#ifdef ENTROPY_MEMUSE_THREADED
464481
/* Stop the counter thread to avoid thrashing the system. */
465482
Entropy_StopThread();
466483
#endif
467484

485+
if (ret != WC_NO_ERR_TRACE(BAD_MUTEX_E)) {
486+
wc_UnLockMutex(&entropy_mutex);
487+
}
488+
468489
return ret;
469490
}
470491

@@ -729,11 +750,6 @@ static int Entropy_Condition(byte* output, word32 len, byte* noise,
729750
return ret;
730751
}
731752

732-
/* Mutex to prevent multiple callers requesting entropy operations at the
733-
* same time.
734-
*/
735-
static wolfSSL_Mutex entropy_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(entropy_mutex);
736-
737753
/* Get entropy of specified strength.
738754
*
739755
* SP800-90b 2.3.1 - GetEntropy: An Interface to the Entropy Source

0 commit comments

Comments
 (0)