Skip to content

Commit 6e8f90a

Browse files
committed
wc_Entropy_GetRawEntropy: hold entropy_mutex
1 parent 575ce49 commit 6e8f90a

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

wolfcrypt/src/wolfentropy.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ static int Entropy_GetNoise(unsigned char* noise, int samples)
477477
return 0;
478478
}
479479

480+
/* Mutex to prevent multiple callers requesting entropy operations at the
481+
* same time.
482+
*/
483+
static wolfSSL_Mutex entropy_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(entropy_mutex);
484+
480485
/* Generate raw entropy for performing assessment.
481486
*
482487
* @param [out] raw Buffer to hold raw entropy data.
@@ -488,20 +493,42 @@ static int Entropy_GetNoise(unsigned char* noise, int samples)
488493
int wc_Entropy_GetRawEntropy(unsigned char* raw, int cnt)
489494
{
490495
int ret = 0;
496+
int locked = 0;
497+
498+
#ifdef HAVE_FIPS
499+
if (!entropy_memuse_initialized) {
500+
ret = Entropy_Init();
501+
}
502+
#endif
503+
504+
/* Lock the mutex as collection uses globals. */
505+
if (ret == 0) {
506+
if (wc_LockMutex(&entropy_mutex) != 0) {
507+
ret = BAD_MUTEX_E;
508+
}
509+
else {
510+
locked = 1;
511+
}
512+
}
491513

492514
#ifdef ENTROPY_MEMUSE_THREADED
493-
/* Start the counter thread as a proxy for time counter. */
494-
ret = Entropy_StartThread();
495-
if (ret == 0)
515+
if (ret == 0) {
516+
/* Start the counter thread as a proxy for time counter. */
517+
ret = Entropy_StartThread();
518+
}
496519
#endif
497-
{
520+
if (ret == 0) {
498521
ret = Entropy_GetNoise(raw, cnt);
499522
}
500523
#ifdef ENTROPY_MEMUSE_THREADED
501524
/* Stop the counter thread to avoid thrashing the system. */
502525
Entropy_StopThread();
503526
#endif
504527

528+
if (locked) {
529+
wc_UnLockMutex(&entropy_mutex);
530+
}
531+
505532
return ret;
506533
}
507534

@@ -766,11 +793,6 @@ static int Entropy_Condition(byte* output, word32 len, byte* noise,
766793
return ret;
767794
}
768795

769-
/* Mutex to prevent multiple callers requesting entropy operations at the
770-
* same time.
771-
*/
772-
static wolfSSL_Mutex entropy_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(entropy_mutex);
773-
774796
/* Get entropy of specified strength.
775797
*
776798
* SP800-90b 2.3.1 - GetEntropy: An Interface to the Entropy Source

0 commit comments

Comments
 (0)