Skip to content

Commit a9e2146

Browse files
authored
Merge pull request #8675 from SparkiDev/entropy_memuse_fix
Entropy MemUse: fix for when block size less than update bits
2 parents 2ce415c + a34284e commit a9e2146

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ CRYP_KEYSIZE_192B
165165
CSM_UNSUPPORTED_ALGS
166166
CTYPE_USER
167167
CURVED448_SMALL
168+
CUSTOM_ENTROPY_TIMEHIRES
168169
CY_USING_HAL
169170
DCP_USE_DCACHE
170171
DILITHIUM_MUL_11_SLOW

wolfcrypt/src/random.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ This library contains implementation for the random number generator.
2626
2727
*/
2828

29+
/* Possible defines:
30+
* ENTROPY_NUM_UPDATE default: 18
31+
* Number of updates to perform. A hash is created and memory accessed
32+
* based on the hash values in each update of a sample.
33+
* More updates will result in better entropy quality but longer sample
34+
* times.
35+
* ENTROPY_NUM_UPDATES_BITS default: 5
36+
* Number of bits needed to represent ENTROPY_NUM_UPDATE.
37+
* = upper(log2(ENTROPY_NUM_UPDATE))
38+
* ENTROPY_NUM_WORDS_BITS default: 14
39+
* State has 2^ENTROPY_NUMN_WORDS_BITS entries. Range: 8-30
40+
* The value should be based on the cache sizes.
41+
* Use a value that is at least as large as the L1 cache if possible.
42+
* The higher the value, the more likely there will be cache misses and
43+
* better the entropy quality.
44+
* A larger value will use more static memory.
45+
*/
46+
2947
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
3048

3149
/* on HPUX 11 you may need to install /dev/random see
@@ -788,8 +806,13 @@ static wc_Sha3 entropyHash;
788806
/* Reset the health tests. */
789807
static void Entropy_HealthTest_Reset(void);
790808

791-
#if !defined(ENTROPY_MEMUSE_THREAD) && \
792-
(defined(__x86_64__) || defined(__i386__))
809+
#ifdef CUSTOM_ENTROPY_TIMEHIRES
810+
static WC_INLINE word64 Entropy_TimeHiRes(void)
811+
{
812+
return CUSTOM_ENTROPY_TIMEHIRES();
813+
}
814+
#elif !defined(ENTROPY_MEMUSE_THREAD) && \
815+
(defined(__x86_64__) || defined(__i386__))
793816
/* Get the high resolution time counter.
794817
*
795818
* @return 64-bit count of CPU cycles.
@@ -1027,9 +1050,18 @@ static void Entropy_StopThread(void)
10271050
#elif !defined(ENTROPY_NUM_UPDATES_BITS)
10281051
#define ENTROPY_NUM_UPDATES_BITS ENTROPY_BLOCK_SZ
10291052
#endif
1030-
/* Amount to shift offset to get better coverage of a block */
1031-
#define ENTROPY_OFFSET_SHIFTING \
1032-
(ENTROPY_BLOCK_SZ / ENTROPY_NUM_UPDATES_BITS)
1053+
#ifndef ENTROPY_NUM_UPDATES_BITS
1054+
#error "ENTROPY_NUM_UPDATES_BITS must be defined - " \
1055+
"upper(log2(ENTROPY_NUM_UPDATES))"
1056+
#endif
1057+
#if ENTROPY_NUM_UPDATES_BITS != 0
1058+
/* Amount to shift offset to get better coverage of a block */
1059+
#define ENTROPY_OFFSET_SHIFTING \
1060+
(ENTROPY_BLOCK_SZ / ENTROPY_NUM_UPDATES_BITS)
1061+
#else
1062+
/* Amount to shift offset to get better coverage of a block */
1063+
#define ENTROPY_OFFSET_SHIFTING ENTROPY_BLOCK_SZ
1064+
#endif
10331065

10341066
#ifndef ENTROPY_NUM_64BIT_WORDS
10351067
/* Number of 64-bit words to update - 32. */
@@ -1038,8 +1070,14 @@ static void Entropy_StopThread(void)
10381070
#error "ENTROPY_NUM_64BIT_WORDS must be <= SHA3-256 digest size in bytes"
10391071
#endif
10401072

1073+
#if ENTROPY_BLOCK_SZ < ENTROPY_NUM_UPDATES_BITS
1074+
#define EXTRA_ENTROPY_WORDS ENTROPY_NUM_UPDATES
1075+
#else
1076+
#define EXTRA_ENTROPY_WORDS 0
1077+
#endif
1078+
10411079
/* State to update that is multiple cache lines long. */
1042-
static word64 entropy_state[ENTROPY_NUM_WORDS] = {0};
1080+
static word64 entropy_state[ENTROPY_NUM_WORDS + EXTRA_ENTROPY_WORDS] = {0};
10431081

10441082
/* Using memory will take different amount of times depending on the CPU's
10451083
* caches and business.

0 commit comments

Comments
 (0)