@@ -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. */
789807static 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