Skip to content

Commit 8dca484

Browse files
committed
backport 6f95a9c and a944575, and fix in 20251226-fixes, to wolfcrypt/src/random.c:
* in _InitRng(), remove "drbg_instantiated" conditional cleanup logic (Coverity true-benign-positive: DEADCODE because drbg_instantiated is always false when ret != DRBG_SUCCESS); * fix clang-diagnostic-unreachable-code in wc_GenerateSeed(); * in wc_GenerateSeed(), move the gate closures for !FORCE_FAILURE_RDSEED and !ENTROPY_MEMUSE_FORCE_FAILURE to follow the /dev/urandom fallback method.
1 parent 57d2391 commit 8dca484

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

wolfcrypt/src/random.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
741741
#ifdef HAVE_HASHDRBG
742742
word32 seedSz = SEED_SZ + SEED_BLOCK_SZ;
743743
WC_DECLARE_VAR(seed, byte, MAX_SEED_SZ, rng->heap);
744-
int drbg_instantiated = 0;
745744
#ifdef WOLFSSL_SMALL_STACK_CACHE
746745
int drbg_scratch_instantiated = 0;
747746
#endif
@@ -948,8 +947,6 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
948947
ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
949948
seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ,
950949
nonce, nonceSz, rng->heap, devId);
951-
if (ret == 0)
952-
drbg_instantiated = 1;
953950
} /* ret == 0 */
954951

955952
#ifdef WOLFSSL_SMALL_STACK
@@ -961,8 +958,6 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
961958
WC_FREE_VAR_EX(seed, rng->heap, DYNAMIC_TYPE_SEED);
962959

963960
if (ret != DRBG_SUCCESS) {
964-
if (drbg_instantiated)
965-
(void)Hash_DRBG_Uninstantiate((DRBG_internal *)rng->drbg);
966961
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
967962
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
968963
#endif
@@ -3417,14 +3412,20 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
34173412
#ifdef HAVE_ENTROPY_MEMUSE
34183413
ret = wc_Entropy_Get(MAX_ENTROPY_BITS, output, sz);
34193414
if (ret == 0) {
3420-
return 0;
3415+
/* success, we're done */
3416+
return ret;
34213417
}
3422-
#ifdef ENTROPY_MEMUSE_FORCE_FAILURE
3423-
/* Don't fallback to /dev/urandom. */
3418+
#ifdef ENTROPY_MEMUSE_FORCE_FAILURE
3419+
/* Don't fall back to /dev/urandom. */
34243420
return ret;
3421+
#else
3422+
/* Reset error and fall back to using /dev/urandom. */
3423+
ret = 0;
34253424
#endif
34263425
#endif
34273426

3427+
#if !defined(HAVE_ENTROPY_MEMUSE) || !defined(ENTROPY_MEMUSE_FORCE_FAILURE)
3428+
34283429
#if defined(HAVE_INTEL_RDSEED) || defined(HAVE_AMD_RDSEED)
34293430
if (IS_INTEL_RDSEED(intel_flags)) {
34303431
ret = wc_GenerateSeed_IntelRD(NULL, output, sz);
@@ -3433,15 +3434,24 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
34333434
return ret;
34343435
}
34353436
#ifdef FORCE_FAILURE_RDSEED
3436-
/* don't fallback to /dev/urandom */
3437+
/* Don't fall back to /dev/urandom. */
34373438
return ret;
34383439
#else
3439-
/* reset error and fallback to using /dev/urandom */
3440+
/* Reset error and fall back to using /dev/urandom. */
34403441
ret = 0;
34413442
#endif
34423443
}
3444+
#ifdef FORCE_FAILURE_RDSEED
3445+
else {
3446+
/* Don't fall back to /dev/urandom */
3447+
return MISSING_RNG_E;
3448+
}
3449+
#endif
34433450
#endif /* HAVE_INTEL_RDSEED || HAVE_AMD_RDSEED */
34443451

3452+
#if (!defined(HAVE_INTEL_RDSEED) && !defined(HAVE_AMD_RDSEED)) || \
3453+
!defined(FORCE_FAILURE_RDSEED)
3454+
34453455
#if defined(WOLFSSL_GETRANDOM) || defined(HAVE_GETRANDOM)
34463456
{
34473457
word32 grSz = sz;
@@ -3469,10 +3479,10 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
34693479
if (ret == 0)
34703480
return ret;
34713481
#ifdef FORCE_FAILURE_GETRANDOM
3472-
/* don't fallback to /dev/urandom */
3482+
/* don't fall back to /dev/urandom */
34733483
return ret;
34743484
#elif !defined(NO_FILESYSTEM)
3475-
/* reset error and fallback to using /dev/urandom if filesystem
3485+
/* reset error and fall back to using /dev/urandom if filesystem
34763486
* support is compiled in */
34773487
ret = 0;
34783488
#endif
@@ -3525,6 +3535,10 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
35253535
ret = NOT_COMPILED_IN;
35263536
#endif /* NO_FILESYSTEM */
35273537

3538+
#endif /* (!HAVE_INTEL_RDSEED && !HAVE_AMD_RDSEED) || !FORCE_FAILURE_RDSEED */
3539+
3540+
#endif /*!HAVE_ENTROPY_MEMUSE || !ENTROPY_MEMUSE_FORCE_FAILURE */
3541+
35283542
return ret;
35293543
}
35303544

0 commit comments

Comments
 (0)