Merge pull request #8675 from SparkiDev/entropy_memuse_fix

Entropy MemUse: fix for when block size less than update bits
pull/8687/head
David Garske 2025-04-16 20:18:22 -07:00 committed by GitHub
commit a9e2146f06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 6 deletions

View File

@ -165,6 +165,7 @@ CRYP_KEYSIZE_192B
CSM_UNSUPPORTED_ALGS CSM_UNSUPPORTED_ALGS
CTYPE_USER CTYPE_USER
CURVED448_SMALL CURVED448_SMALL
CUSTOM_ENTROPY_TIMEHIRES
CY_USING_HAL CY_USING_HAL
DCP_USE_DCACHE DCP_USE_DCACHE
DILITHIUM_MUL_11_SLOW DILITHIUM_MUL_11_SLOW

View File

@ -26,6 +26,24 @@ This library contains implementation for the random number generator.
*/ */
/* Possible defines:
* ENTROPY_NUM_UPDATE default: 18
* Number of updates to perform. A hash is created and memory accessed
* based on the hash values in each update of a sample.
* More updates will result in better entropy quality but longer sample
* times.
* ENTROPY_NUM_UPDATES_BITS default: 5
* Number of bits needed to represent ENTROPY_NUM_UPDATE.
* = upper(log2(ENTROPY_NUM_UPDATE))
* ENTROPY_NUM_WORDS_BITS default: 14
* State has 2^ENTROPY_NUMN_WORDS_BITS entries. Range: 8-30
* The value should be based on the cache sizes.
* Use a value that is at least as large as the L1 cache if possible.
* The higher the value, the more likely there will be cache misses and
* better the entropy quality.
* A larger value will use more static memory.
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h> #include <wolfssl/wolfcrypt/libwolfssl_sources.h>
/* on HPUX 11 you may need to install /dev/random see /* on HPUX 11 you may need to install /dev/random see
@ -788,8 +806,13 @@ static wc_Sha3 entropyHash;
/* Reset the health tests. */ /* Reset the health tests. */
static void Entropy_HealthTest_Reset(void); static void Entropy_HealthTest_Reset(void);
#if !defined(ENTROPY_MEMUSE_THREAD) && \ #ifdef CUSTOM_ENTROPY_TIMEHIRES
(defined(__x86_64__) || defined(__i386__)) static WC_INLINE word64 Entropy_TimeHiRes(void)
{
return CUSTOM_ENTROPY_TIMEHIRES();
}
#elif !defined(ENTROPY_MEMUSE_THREAD) && \
(defined(__x86_64__) || defined(__i386__))
/* Get the high resolution time counter. /* Get the high resolution time counter.
* *
* @return 64-bit count of CPU cycles. * @return 64-bit count of CPU cycles.
@ -1027,9 +1050,18 @@ static void Entropy_StopThread(void)
#elif !defined(ENTROPY_NUM_UPDATES_BITS) #elif !defined(ENTROPY_NUM_UPDATES_BITS)
#define ENTROPY_NUM_UPDATES_BITS ENTROPY_BLOCK_SZ #define ENTROPY_NUM_UPDATES_BITS ENTROPY_BLOCK_SZ
#endif #endif
/* Amount to shift offset to get better coverage of a block */ #ifndef ENTROPY_NUM_UPDATES_BITS
#define ENTROPY_OFFSET_SHIFTING \ #error "ENTROPY_NUM_UPDATES_BITS must be defined - " \
(ENTROPY_BLOCK_SZ / ENTROPY_NUM_UPDATES_BITS) "upper(log2(ENTROPY_NUM_UPDATES))"
#endif
#if ENTROPY_NUM_UPDATES_BITS != 0
/* Amount to shift offset to get better coverage of a block */
#define ENTROPY_OFFSET_SHIFTING \
(ENTROPY_BLOCK_SZ / ENTROPY_NUM_UPDATES_BITS)
#else
/* Amount to shift offset to get better coverage of a block */
#define ENTROPY_OFFSET_SHIFTING ENTROPY_BLOCK_SZ
#endif
#ifndef ENTROPY_NUM_64BIT_WORDS #ifndef ENTROPY_NUM_64BIT_WORDS
/* Number of 64-bit words to update - 32. */ /* Number of 64-bit words to update - 32. */
@ -1038,8 +1070,14 @@ static void Entropy_StopThread(void)
#error "ENTROPY_NUM_64BIT_WORDS must be <= SHA3-256 digest size in bytes" #error "ENTROPY_NUM_64BIT_WORDS must be <= SHA3-256 digest size in bytes"
#endif #endif
#if ENTROPY_BLOCK_SZ < ENTROPY_NUM_UPDATES_BITS
#define EXTRA_ENTROPY_WORDS ENTROPY_NUM_UPDATES
#else
#define EXTRA_ENTROPY_WORDS 0
#endif
/* State to update that is multiple cache lines long. */ /* State to update that is multiple cache lines long. */
static word64 entropy_state[ENTROPY_NUM_WORDS] = {0}; static word64 entropy_state[ENTROPY_NUM_WORDS + EXTRA_ENTROPY_WORDS] = {0};
/* Using memory will take different amount of times depending on the CPU's /* Using memory will take different amount of times depending on the CPU's
* caches and business. * caches and business.