Skip to content

Commit a34284e

Browse files
committed
Entropy MemUse: support for custom hi res time
Call the custom high resolution time function when CUSTOM_ENTROPY_TIMEHIRES is defined with the function name.
1 parent b1aa11d commit a34284e

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ CRYP_KEYSIZE_192B
164164
CSM_UNSUPPORTED_ALGS
165165
CTYPE_USER
166166
CURVED448_SMALL
167+
CUSTOM_ENTROPY_TIMEHIRES
167168
CY_USING_HAL
168169
DCP_USE_DCACHE
169170
DILITHIUM_MUL_11_SLOW

wolfcrypt/src/random.c

Lines changed: 37 additions & 5 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. */

0 commit comments

Comments
 (0)