Skip to content

Commit 5fa0681

Browse files
authored
Merge pull request wolfSSL#9595 from douzzer/20251229-linuxkm-rng-wolfentropy
20251229-linuxkm-rng-wolfentropy
2 parents 7a2e1c1 + 0621615 commit 5fa0681

File tree

5 files changed

+105
-15
lines changed

5 files changed

+105
-15
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ WC_LMS_FULL_HASH
622622
WC_NO_ASYNC_SLEEP
623623
WC_NO_RNG_SIMPLE
624624
WC_NO_STATIC_ASSERT
625+
WC_NO_VERBOSE_RNG
625626
WC_PKCS11_FIND_WITH_ID_ONLY
626627
WC_PROTECT_ENCRYPTED_MEM
627628
WC_RNG_BLOCKING

linuxkm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
290290
# if the above make didn't build a fresh libwolfssl.ko, then the module is already up to date and we leave it untouched, assuring stability for purposes of module-update-fips-hash.
291291
@if [[ ! "$@" -nt "$$RELOC_TMP" ]]; then echo ' Module already up-to-date.'; exit 0; fi
292292
@SECTION_MAP=$$(mktemp)
293-
@trap 'rm "$$SECTION_MAP"' EXIT
293+
@trap 'rm "$$RELOC_TMP" "$$SECTION_MAP"' EXIT
294294
@export SECTION_MAP
295295
@$(READELF) --wide --sections --symbols "$@" | $(GENERATE_SECTION_MAP)
296296
@$(READELF) --wide --relocs "$@" | $(GENERATE_RELOC_TAB) >| '$(MODULE_TOP)/linuxkm/wc_linuxkm_pie_reloc_tab.c'

linuxkm/lkcapi_sha_glue.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,10 @@ static inline void wc_linuxkm_drbg_ctx_clear(struct wc_linuxkm_drbg_ctx * ctx)
990990

991991
static volatile int wc_linuxkm_drbg_init_tfm_disable_vector_registers = 0;
992992

993+
#ifndef WC_LINUXKM_INITRNG_TIMEOUT_SEC
994+
#define WC_LINUXKM_INITRNG_TIMEOUT_SEC 30
995+
#endif
996+
993997
static int wc_linuxkm_drbg_init_tfm(struct crypto_tfm *tfm)
994998
{
995999
struct wc_linuxkm_drbg_ctx *ctx = (struct wc_linuxkm_drbg_ctx *)crypto_tfm_ctx(tfm);
@@ -1007,19 +1011,44 @@ static int wc_linuxkm_drbg_init_tfm(struct crypto_tfm *tfm)
10071011
XMEMSET(ctx->rngs, 0, sizeof(*ctx->rngs) * ctx->n_rngs);
10081012

10091013
for (i = 0; i < ctx->n_rngs; ++i) {
1010-
ctx->rngs[i].lock = 0;
1011-
if (wc_linuxkm_drbg_init_tfm_disable_vector_registers)
1012-
need_reenable_vec = (DISABLE_VECTOR_REGISTERS() == 0);
1013-
ret = wc_InitRng(&ctx->rngs[i].rng);
1014-
if (need_reenable_vec)
1015-
REENABLE_VECTOR_REGISTERS();
1014+
int nretries = 0;
1015+
u64 ts1 = ktime_get_ns();
1016+
for (;;) {
1017+
u64 ts2;
1018+
if (wc_linuxkm_drbg_init_tfm_disable_vector_registers)
1019+
need_reenable_vec = (DISABLE_VECTOR_REGISTERS() == 0);
1020+
ret = wc_InitRng(&ctx->rngs[i].rng);
1021+
if (need_reenable_vec)
1022+
REENABLE_VECTOR_REGISTERS();
1023+
if (can_sleep) {
1024+
/* if we're allowed to sleep, relax the loop between each inner
1025+
* iteration even on success, assuring relaxation of the outer
1026+
* iterations.
1027+
*/
1028+
cond_resched();
1029+
}
1030+
if (ret == 0)
1031+
break;
1032+
if (can_sleep) {
1033+
/* Allow interrupt only if we're stuck spinning retries -- i.e.,
1034+
* don't allow an untimely user signal to derail an
1035+
* initialization that is proceeding expeditiously.
1036+
*/
1037+
if (WC_CHECK_FOR_INTR_SIGNALS() == WC_NO_ERR_TRACE(INTERRUPTED_E)) {
1038+
ret = -EINTR;
1039+
break;
1040+
}
1041+
}
1042+
ts2 = ktime_get_ns();
1043+
if (ts2 - ts1 > 1000000000L * WC_LINUXKM_INITRNG_TIMEOUT_SEC)
1044+
break;
1045+
++nretries;
1046+
}
10161047
if (ret != 0) {
1017-
pr_warn_once("WARNING: wc_InitRng returned %d\n",ret);
1048+
pr_warn("WARNING: wc_InitRng returned %d after %d retries.\n", ret, nretries);
10181049
ret = -EINVAL;
10191050
break;
10201051
}
1021-
if (can_sleep)
1022-
cond_resched();
10231052
}
10241053

10251054
if (ret != 0) {

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;
@@ -926,6 +956,9 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
926956
else {
927957
ret = seedCb(&rng->seed, seed, seedSz);
928958
if (ret != 0) {
959+
#ifdef WC_VERBOSE_RNG
960+
WOLFSSL_DEBUG_PRINTF("seedCb in _InitRng() failed with err = %d", ret);
961+
#endif
929962
ret = DRBG_FAILURE;
930963
}
931964
}
@@ -935,6 +968,8 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
935968
if (ret != 0) {
936969
#if defined(DEBUG_WOLFSSL)
937970
WOLFSSL_MSG_EX("Seed generation failed... %d", ret);
971+
#elif defined(WC_VERBOSE_RNG)
972+
WOLFSSL_DEBUG_PRINTF("wc_GenerateSeed() in _InitRng() failed with err %d", ret);
938973
#endif
939974
ret = DRBG_FAILURE;
940975
rng->status = DRBG_FAILED;
@@ -946,7 +981,12 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
946981
if (ret != 0) {
947982
WOLFSSL_MSG_EX("wc_RNG_TestSeed failed... %d", ret);
948983
}
984+
#elif defined(WC_VERBOSE_RNG)
985+
if (ret != DRBG_SUCCESS) {
986+
WOLFSSL_DEBUG_PRINTF("wc_RNG_TestSeed() in _InitRng() returned err %d.", ret);
987+
}
949988
#endif
989+
950990
if (ret == DRBG_SUCCESS)
951991
ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
952992
#if defined(HAVE_FIPS) || !defined(WOLFSSL_RNG_USE_FULL_SEED)
@@ -1120,19 +1160,30 @@ static int PollAndReSeed(WC_RNG* rng)
11201160
else {
11211161
ret = seedCb(&rng->seed, newSeed, SEED_SZ + SEED_BLOCK_SZ);
11221162
if (ret != 0) {
1163+
#ifdef WC_VERBOSE_RNG
1164+
WOLFSSL_DEBUG_PRINTF("seedCb() in PollAndReSeed() failed with err %d", ret);
1165+
#endif
11231166
ret = DRBG_FAILURE;
11241167
}
11251168
}
11261169
#else
11271170
ret = wc_GenerateSeed(&rng->seed, newSeed,
11281171
SEED_SZ + SEED_BLOCK_SZ);
1129-
#endif
1130-
if (ret != 0)
1172+
if (ret != 0) {
1173+
#ifdef WC_VERBOSE_RNG
1174+
WOLFSSL_DEBUG_PRINTF("wc_GenerateSeed() in PollAndReSeed() failed with err %d", ret);
1175+
#endif
11311176
ret = DRBG_FAILURE;
1177+
}
1178+
#endif
11321179
}
1133-
if (ret == DRBG_SUCCESS)
1180+
if (ret == DRBG_SUCCESS) {
11341181
ret = wc_RNG_TestSeed(newSeed, SEED_SZ + SEED_BLOCK_SZ);
1135-
1182+
#ifdef WC_VERBOSE_RNG
1183+
if (ret != DRBG_SUCCESS)
1184+
WOLFSSL_DEBUG_PRINTF("wc_RNG_TestSeed() in PollAndReSeed() returned err %d.", ret);
1185+
#endif
1186+
}
11361187
if (ret == DRBG_SUCCESS)
11371188
ret = Hash_DRBG_Reseed((DRBG_internal *)rng->drbg,
11381189
newSeed + SEED_BLOCK_SZ, SEED_SZ);
@@ -1202,6 +1253,10 @@ int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
12021253
#ifdef CUSTOM_RAND_GENERATE_BLOCK
12031254
XMEMSET(output, 0, sz);
12041255
ret = (int)CUSTOM_RAND_GENERATE_BLOCK(output, sz);
1256+
#ifdef WC_VERBOSE_RNG
1257+
if (ret != 0)
1258+
WOLFSSL_DEBUG_PRINTF("CUSTOM_RAND_GENERATE_BLOCK failed with err %d.", ret);
1259+
#endif
12051260
#else
12061261

12071262
#ifdef HAVE_HASHDRBG

wolfssl/wolfcrypt/settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,11 @@ extern void uITRON4_free(void *p) ;
39343934
#define WOLFSSL_HAVE_MAX
39353935
#endif
39363936

3937+
#if defined(WOLFSSL_KERNEL_MODE) && !defined(WC_NO_VERBOSE_RNG) && \
3938+
!defined(WC_VERBOSE_RNG)
3939+
#define WC_VERBOSE_RNG
3940+
#endif
3941+
39373942
#if defined(WC_SYM_RELOC_TABLES) && defined(HAVE_FIPS) && \
39383943
!defined(WC_PIE_RELOC_TABLES)
39393944
/* backward compat */

0 commit comments

Comments
 (0)