Skip to content

Commit 688bc16

Browse files
committed
wolfcrypt/src/random.c: small stack refactor of noise[] in wc_Entropy_Get().
1 parent 478bfaf commit 688bc16

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

wolfcrypt/src/random.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,13 +1494,23 @@ static wolfSSL_Mutex entropy_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(entropy_mute
14941494
int wc_Entropy_Get(int bits, unsigned char* entropy, word32 len)
14951495
{
14961496
int ret = 0;
1497+
#ifdef WOLFSSL_SMALL_STACK
1498+
byte *noise = NULL;
1499+
#else
14971500
byte noise[MAX_NOISE_CNT];
1501+
#endif
14981502
/* Noise length is the number of 8 byte samples required to get the bits of
14991503
* entropy requested. */
15001504
int noise_len = (bits + ENTROPY_EXTRA) / ENTROPY_MIN;
15011505

1506+
#ifdef WOLFSSL_SMALL_STACK
1507+
noise = (byte *)XMALLOC(MAX_NOISE_CNT, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1508+
if (noise == NULL)
1509+
return MEMORY_E;
1510+
#endif
1511+
15021512
/* Lock the mutex as collection uses globals. */
1503-
if (wc_LockMutex(&entropy_mutex) != 0) {
1513+
if ((ret == 0) && (wc_LockMutex(&entropy_mutex) != 0)) {
15041514
ret = BAD_MUTEX_E;
15051515
}
15061516

@@ -1558,6 +1568,10 @@ int wc_Entropy_Get(int bits, unsigned char* entropy, word32 len)
15581568
wc_UnLockMutex(&entropy_mutex);
15591569
}
15601570

1571+
#ifdef WOLFSSL_SMALL_STACK
1572+
XFREE(noise, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1573+
#endif
1574+
15611575
return ret;
15621576
}
15631577

0 commit comments

Comments
 (0)