Skip to content

Commit 8df621a

Browse files
committed
wolfcrypt/src/random.c: backport changes from 20251229-linuxkm-rng-wolfentropy (WC_VERBOSE_RNG).
1 parent f21da9d commit 8df621a

1 file changed

Lines changed: 60 additions & 5 deletions

File tree

wolfcrypt/src/random.c

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ static int Hash_df(DRBG_internal* drbg, byte* out, word32 outSz, byte type,
369369
XFREE(digest, drbg->heap, DYNAMIC_TYPE_DIGEST);
370370
#endif
371371

372+
#ifdef WC_VERBOSE_RNG
373+
if (ret != 0)
374+
WOLFSSL_DEBUG_PRINTF("%s failed with err = %d", __FUNCTION__, ret);
375+
#endif
376+
372377
return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
373378
}
374379

@@ -406,6 +411,12 @@ static int Hash_DRBG_Reseed(DRBG_internal* drbg, const byte* seed, word32 seedSz
406411
#ifndef WOLFSSL_SMALL_STACK_CACHE
407412
WC_FREE_VAR_EX(newV, drbg->heap, DYNAMIC_TYPE_TMP_BUFFER);
408413
#endif
414+
415+
#ifdef WC_VERBOSE_RNG
416+
if (ret != 0)
417+
WOLFSSL_DEBUG_PRINTF("Hash_DRBG_Reseed failed with err %d.", ret);
418+
#endif
419+
409420
return ret;
410421
}
411422

@@ -525,6 +536,19 @@ static int Hash_gen(DRBG_internal* drbg, byte* out, word32 outSz, const byte* V)
525536
WC_FREE_VAR_EX(data, drbg->heap, DYNAMIC_TYPE_TMP_BUFFER);
526537
#endif
527538

539+
#ifdef WC_VERBOSE_RNG
540+
if ((ret != DRBG_SUCCESS) && (ret != DRBG_FAILURE)) {
541+
/* Note, if we're just going to return DRBG_FAILURE to the caller, then
542+
* there's no point printing it out here because (1) the lower-level
543+
* code that was remapped to DRBG_FAILURE already got printed before the
544+
* remapping, so a DRBG_FAILURE message would just be spamming the log,
545+
* and (2) the caller will actually see the DRBG_FAILURE code, and is
546+
* free to (and probably will) log it itself.
547+
*/
548+
WOLFSSL_DEBUG_PRINTF("Hash_gen failed with err %d.", ret);
549+
}
550+
#endif
551+
528552
return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
529553
}
530554

@@ -635,6 +659,13 @@ static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz)
635659
#endif
636660
}
637661

662+
#ifdef WC_VERBOSE_RNG
663+
if ((ret != DRBG_SUCCESS) && (ret != DRBG_FAILURE)) {
664+
/* see note above regarding log spam reduction */
665+
WOLFSSL_DEBUG_PRINTF("Hash_DRBG_Generate failed with err %d.", ret);
666+
}
667+
#endif
668+
638669
return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
639670
}
640671

@@ -721,7 +752,6 @@ int wc_RNG_TestSeed(const byte* seed, word32 seedSz)
721752
if (ConstantCompare(seed + seedIdx,
722753
seed + seedIdx + scratchSz,
723754
(int)scratchSz) == 0) {
724-
725755
ret = DRBG_CONT_FAILURE;
726756
}
727757
seedIdx += SEED_BLOCK_SZ;
@@ -922,6 +952,9 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
922952
else {
923953
ret = seedCb(&rng->seed, seed, seedSz);
924954
if (ret != 0) {
955+
#ifdef WC_VERBOSE_RNG
956+
WOLFSSL_DEBUG_PRINTF("seedCb in _InitRng() failed with err = %d", ret);
957+
#endif
925958
ret = DRBG_FAILURE;
926959
}
927960
}
@@ -931,6 +964,8 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
931964
if (ret != 0) {
932965
#if defined(DEBUG_WOLFSSL)
933966
WOLFSSL_MSG_EX("Seed generation failed... %d", ret);
967+
#elif defined(WC_VERBOSE_RNG)
968+
WOLFSSL_DEBUG_PRINTF("wc_GenerateSeed() in _InitRng() failed with err %d", ret);
934969
#endif
935970
ret = DRBG_FAILURE;
936971
rng->status = DRBG_FAILED;
@@ -942,7 +977,12 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
942977
if (ret != 0) {
943978
WOLFSSL_MSG_EX("wc_RNG_TestSeed failed... %d", ret);
944979
}
980+
#elif defined(WC_VERBOSE_RNG)
981+
if (ret != DRBG_SUCCESS) {
982+
WOLFSSL_DEBUG_PRINTF("wc_RNG_TestSeed() in _InitRng() returned err %d.", ret);
983+
}
945984
#endif
985+
946986
if (ret == DRBG_SUCCESS)
947987
ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
948988
seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ,
@@ -1112,19 +1152,30 @@ static int PollAndReSeed(WC_RNG* rng)
11121152
else {
11131153
ret = seedCb(&rng->seed, newSeed, SEED_SZ + SEED_BLOCK_SZ);
11141154
if (ret != 0) {
1155+
#ifdef WC_VERBOSE_RNG
1156+
WOLFSSL_DEBUG_PRINTF("seedCb() in PollAndReSeed() failed with err %d", ret);
1157+
#endif
11151158
ret = DRBG_FAILURE;
11161159
}
11171160
}
11181161
#else
11191162
ret = wc_GenerateSeed(&rng->seed, newSeed,
11201163
SEED_SZ + SEED_BLOCK_SZ);
1121-
#endif
1122-
if (ret != 0)
1164+
if (ret != 0) {
1165+
#ifdef WC_VERBOSE_RNG
1166+
WOLFSSL_DEBUG_PRINTF("wc_GenerateSeed() in PollAndReSeed() failed with err %d", ret);
1167+
#endif
11231168
ret = DRBG_FAILURE;
1169+
}
1170+
#endif
11241171
}
1125-
if (ret == DRBG_SUCCESS)
1172+
if (ret == DRBG_SUCCESS) {
11261173
ret = wc_RNG_TestSeed(newSeed, SEED_SZ + SEED_BLOCK_SZ);
1127-
1174+
#ifdef WC_VERBOSE_RNG
1175+
if (ret != DRBG_SUCCESS)
1176+
WOLFSSL_DEBUG_PRINTF("wc_RNG_TestSeed() in PollAndReSeed() returned err %d.", ret);
1177+
#endif
1178+
}
11281179
if (ret == DRBG_SUCCESS)
11291180
ret = Hash_DRBG_Reseed((DRBG_internal *)rng->drbg,
11301181
newSeed + SEED_BLOCK_SZ, SEED_SZ);
@@ -1194,6 +1245,10 @@ int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
11941245
#ifdef CUSTOM_RAND_GENERATE_BLOCK
11951246
XMEMSET(output, 0, sz);
11961247
ret = (int)CUSTOM_RAND_GENERATE_BLOCK(output, sz);
1248+
#ifdef WC_VERBOSE_RNG
1249+
if (ret != 0)
1250+
WOLFSSL_DEBUG_PRINTF("CUSTOM_RAND_GENERATE_BLOCK failed with err %d.", ret);
1251+
#endif
11971252
#else
11981253

11991254
#ifdef HAVE_HASHDRBG

0 commit comments

Comments
 (0)