Skip to content

Commit 0a0c430

Browse files
Merge pull request #9564 from douzzer/20251219-fixes
20251219-fixes
2 parents d5723d0 + a755034 commit 0a0c430

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

wolfcrypt/src/random.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,6 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
813813
#ifdef HAVE_HASHDRBG
814814
word32 seedSz = SEED_SZ + SEED_BLOCK_SZ;
815815
WC_DECLARE_VAR(seed, byte, MAX_SEED_SZ, rng->heap);
816-
int drbg_instantiated = 0;
817816
#ifdef WOLFSSL_SMALL_STACK_CACHE
818817
int drbg_scratch_instantiated = 0;
819818
#endif
@@ -1020,8 +1019,6 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
10201019
ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
10211020
seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ,
10221021
nonce, nonceSz, rng->heap, devId);
1023-
if (ret == 0)
1024-
drbg_instantiated = 1;
10251022
} /* ret == 0 */
10261023

10271024
#ifdef WOLFSSL_SMALL_STACK
@@ -1033,8 +1030,6 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
10331030
WC_FREE_VAR_EX(seed, rng->heap, DYNAMIC_TYPE_SEED);
10341031

10351032
if (ret != DRBG_SUCCESS) {
1036-
if (drbg_instantiated)
1037-
(void)Hash_DRBG_Uninstantiate((DRBG_internal *)rng->drbg);
10381033
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
10391034
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
10401035
#endif

wolfcrypt/src/wolfentropy.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ data, use this implementation to seed and re-seed the DRBG.
5858
#define MAX_NOISE_CNT (MAX_ENTROPY_BITS * 8 + ENTROPY_EXTRA)
5959

6060
/* MemUse entropy global state initialized. */
61-
static int entropy_memuse_initialized = 0;
61+
static volatile int entropy_memuse_initialized = 0;
6262
/* Global SHA-3 object used for conditioning entropy and creating noise. */
6363
static wc_Sha3 entropyHash;
6464
/* Reset the health tests. */
@@ -740,6 +740,21 @@ int wc_Entropy_Get(int bits, unsigned char* entropy, word32 len)
740740
int noise_len = (bits + ENTROPY_EXTRA) / ENTROPY_MIN;
741741
static byte noise[MAX_NOISE_CNT];
742742

743+
#ifdef HAVE_FIPS
744+
/* FIPS KATs, e.g. EccPrimitiveZ_KnownAnswerTest(), call wc_Entropy_Get()
745+
* incidental to wc_InitRng(), without first calling Entropy_Init(), neither
746+
* directly, nor indirectly via wolfCrypt_Init(). This matters, because
747+
* KATs must be usable before wolfCrypt_Init() (indeed, in the library
748+
* embodiment, the HMAC KAT always runs before wolfCrypt_Init(), incidental
749+
* to fipsEntry()). Without the InitSha3() under Entropy_Init(), the
750+
* SHA3_BLOCK function pointer is null when Sha3Update() is called by
751+
* Entropy_MemUse(), which ends badly.
752+
*/
753+
if (!entropy_memuse_initialized) {
754+
ret = Entropy_Init();
755+
}
756+
#endif
757+
743758
/* Lock the mutex as collection uses globals. */
744759
if ((ret == 0) && (wc_LockMutex(&entropy_mutex) != 0)) {
745760
ret = BAD_MUTEX_E;
@@ -851,6 +866,19 @@ int Entropy_Init(void)
851866
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER)
852867
ret = wc_InitMutex(&entropy_mutex);
853868
#endif
869+
if (ret == 0)
870+
ret = wc_LockMutex(&entropy_mutex);
871+
872+
if (entropy_memuse_initialized) {
873+
/* Short circuit return -- a competing thread initialized the state
874+
* while we were waiting. Note, this is only threadsafe when
875+
* WOLFSSL_MUTEX_INITIALIZER is defined.
876+
*/
877+
if (ret == 0)
878+
wc_UnLockMutex(&entropy_mutex);
879+
return 0;
880+
}
881+
854882
if (ret == 0) {
855883
/* Initialize a SHA3-256 object for use in entropy operations. */
856884
ret = wc_InitSha3_256(&entropyHash, NULL, INVALID_DEVID);
@@ -872,6 +900,10 @@ int Entropy_Init(void)
872900
Entropy_StopThread();
873901
#endif
874902
}
903+
904+
if (ret != WC_NO_ERR_TRACE(BAD_MUTEX_E)) {
905+
wc_UnLockMutex(&entropy_mutex);
906+
}
875907
}
876908

877909
return ret;

wolfcrypt/test/test.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7402,7 +7402,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
74027402
testVector a, b, c, d;
74037403
testVector test_hmac[4];
74047404

7405-
wc_test_ret_t ret;
7405+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
74067406
int times = sizeof(test_hmac) / sizeof(testVector), i;
74077407
WOLFSSL_ENTER("hmac_md5_test");
74087408

@@ -7543,7 +7543,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
75437543
testVector a, b, c, d;
75447544
testVector test_hmac[4];
75457545

7546-
wc_test_ret_t ret;
7546+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
75477547
int times = sizeof(test_hmac) / sizeof(testVector), i;
75487548

75497549
#if FIPS_VERSION3_GE(6,0,0)
@@ -7700,7 +7700,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
77007700
testVector a, b, c, d;
77017701
testVector test_hmac[4];
77027702

7703-
wc_test_ret_t ret;
7703+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
77047704
int times = sizeof(test_hmac) / sizeof(testVector), i;
77057705
WOLFSSL_ENTER("hmac_sha224_test");
77067706

@@ -7844,7 +7844,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
78447844
testVector a, b, c, d, e;
78457845
testVector test_hmac[5];
78467846

7847-
wc_test_ret_t ret;
7847+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
78487848
int times = sizeof(test_hmac) / sizeof(testVector), i;
78497849
WOLFSSL_ENTER("hmac_sha256_test");
78507850

@@ -8014,7 +8014,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
80148014
testVector a, b, c, d;
80158015
testVector test_hmac[4];
80168016

8017-
wc_test_ret_t ret;
8017+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
80188018
int times = sizeof(test_hmac) / sizeof(testVector), i;
80198019
WOLFSSL_ENTER("hmac_sha384_test");
80208020

@@ -8165,7 +8165,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
81658165
testVector a, b, c, d;
81668166
testVector test_hmac[4];
81678167

8168-
wc_test_ret_t ret;
8168+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
81698169
int times = sizeof(test_hmac) / sizeof(testVector), i;
81708170
WOLFSSL_ENTER("hmac_sha512_test");
81718171

@@ -8423,7 +8423,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
84238423

84248424
int i = 0, iMax = sizeof(input) / sizeof(input[0]),
84258425
j, jMax = sizeof(hashType) / sizeof(hashType[0]);
8426-
int ret;
8426+
wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
84278427
WOLFSSL_ENTER("hmac_sha3_test");
84288428

84298429
XMEMSET(&hmac, 0, sizeof(hmac));
@@ -19748,14 +19748,23 @@ static wc_test_ret_t _rng_test(WC_RNG* rng)
1974819748
!defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(5,0,0))
1974919749
/* Test periodic reseed dynamics. */
1975019750

19751-
((struct DRBG_internal *)rng->drbg)->reseedCtr = WC_RESEED_INTERVAL;
19751+
#ifdef WOLF_CRYPTO_CB
19752+
if (wc_CryptoCb_RandomBlock(rng, block, sizeof(block)) ==
19753+
WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
19754+
{
19755+
#endif
19756+
((struct DRBG_internal *)rng->drbg)->reseedCtr = WC_RESEED_INTERVAL;
1975219757

19753-
ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
19754-
if (ret != 0)
19755-
return WC_TEST_RET_ENC_EC(ret);
19758+
ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
19759+
if (ret != 0)
19760+
return WC_TEST_RET_ENC_EC(ret);
19761+
19762+
if (((struct DRBG_internal *)rng->drbg)->reseedCtr == WC_RESEED_INTERVAL)
19763+
return WC_TEST_RET_ENC_NC;
19764+
#ifdef WOLF_CRYPTO_CB
19765+
}
19766+
#endif
1975619767

19757-
if (((struct DRBG_internal *)rng->drbg)->reseedCtr == WC_RESEED_INTERVAL)
19758-
return WC_TEST_RET_ENC_NC;
1975919768
#endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK && !HAVE_SELFTEST */
1976019769

1976119770
#if defined(WOLFSSL_TRACK_MEMORY) && defined(WOLFSSL_SMALL_STACK_CACHE)
@@ -19870,7 +19879,7 @@ static wc_test_ret_t rng_seed_test(void)
1987019879
* SEED_BLOCK_SZ, which depend on which seed back end is configured.
1987119880
*/
1987219881
#if defined(HAVE_ENTROPY_MEMUSE) && defined(HAVE_AMD_RDSEED) && \
19873-
!(defined(HAVE_FIPS) && FIPS_VERSION_LT(6,0))
19882+
!(defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0) && FIPS_VERSION3_NE(5,2,4))
1987419883
#ifdef HAVE_FIPS
1987519884
WOLFSSL_SMALL_STACK_STATIC const byte check[] =
1987619885
{
@@ -19908,7 +19917,7 @@ static wc_test_ret_t rng_seed_test(void)
1990819917
};
1990919918
#endif
1991019919
#elif defined(HAVE_AMD_RDSEED) && \
19911-
!(defined(HAVE_FIPS) && FIPS_VERSION_LT(6,0))
19920+
!(defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0) && FIPS_VERSION3_NE(5,2,4))
1991219921
WOLFSSL_SMALL_STACK_STATIC const byte check[] =
1991319922
{
1991419923
0x2c, 0xd4, 0x9b, 0x1e, 0x1e, 0xe7, 0xb0, 0xb0,
@@ -19917,7 +19926,7 @@ static wc_test_ret_t rng_seed_test(void)
1991719926
0xa2, 0xe7, 0xe5, 0x90, 0x6d, 0x1f, 0x88, 0x98
1991819927
};
1991919928
#elif (defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)) && \
19920-
!(defined(HAVE_FIPS) && FIPS_VERSION_LT(6,0))
19929+
!(defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0) && FIPS_VERSION3_NE(5,2,4))
1992119930
#ifdef HAVE_FIPS
1992219931
WOLFSSL_SMALL_STACK_STATIC const byte check[] =
1992319932
{
@@ -19936,7 +19945,7 @@ static wc_test_ret_t rng_seed_test(void)
1993619945
};
1993719946
#endif
1993819947
#elif defined(HAVE_INTEL_RDSEED) && \
19939-
defined(HAVE_FIPS) && FIPS_VERSION_LT(6,0)
19948+
defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0) && FIPS_VERSION3_NE(5,2,4)
1994019949
WOLFSSL_SMALL_STACK_STATIC const byte check[] =
1994119950
{
1994219951
0x27, 0xdd, 0xff, 0x5b, 0x21, 0x26, 0x0a, 0x48,

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ WOLFSSL_LOCAL int wc_CryptoCb_Kdf_TwostepCmac(const byte * salt, word32 saltSz,
737737
#endif /* HAVE_CMAC_KDF */
738738

739739
#ifndef WC_NO_RNG
740-
WOLFSSL_LOCAL int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz);
740+
WOLFSSL_TEST_VIS int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz);
741741
WOLFSSL_LOCAL int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz);
742742
#endif
743743

0 commit comments

Comments
 (0)