From eca59f3de3f65bfd576ea3d70e7e408a4ac759ac Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 18 Nov 2026 19:45:03 +0100 Subject: [PATCH 1/5] tests: random.c MC/DC 138/301 -> 181/338 (extras API tests + uninst/recovery fault whitebox) --- tests/api/test_random.c | 448 +++++++++++++++++++ tests/api/test_random.h | 6 +- tests/unit-mcdc/test_random_fault_whitebox.c | 92 ++++ 3 files changed, 545 insertions(+), 1 deletion(-) diff --git a/tests/api/test_random.c b/tests/api/test_random.c index c4b2c16afdf..819bc53cf8c 100644 --- a/tests/api/test_random.c +++ b/tests/api/test_random.c @@ -1964,3 +1964,451 @@ int test_wc_RngInitFlagsDecisionCoverage(void) #endif return EXPECT_RESULT(); } + +/* + * MC/DC decision coverage for the WC_RNG_WANT_* "extras" machinery added on + * master (2026-11): the entropy-epoch handler pair, the post-init flag + * policy, the next-seed aperture state machine, the RBGC stratum entry + * points, and the lock CAS front doors. Each section is guarded by the + * feature macro it needs, so the function compiles (as a no-op body) in the + * plain variants and only the matching sections run in the full_mutex / + * rbgc campaign variants. New post-v4.1.0 APIs, hence the frozen-module + * guard. + */ +#if defined(WC_RNG_HAVE_ENTROPY_EPOCH) +static wc_RNG_entropy_epoch_t test_random_epoch_handler(void *arg) +{ + (void)arg; + return 1; +} + +static void test_random_epoch_inv_handler(word64 epoch, void *arg) +{ + (void)epoch; + (void)arg; +} +#endif + +int test_wc_RngExtrasDecisionCoverage(void) +{ + EXPECT_DECLS; +#if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) + WC_RNG rng; + byte out[16]; + word32 flags; + + XMEMSET(&rng, 0, sizeof(rng)); + XMEMSET(out, 0, sizeof(out)); + +#if defined(WC_RNG_HAVE_ENTROPY_EPOCH) + { + wc_RNG_extra_entropy_epoch_get_handler_t h; + wc_RNG_entropy_epoch_extra_inv_handler_t ih; + void *arg; + void *iarg; + + /* extra_entropy_epoch_init: NULL-handler clause, the happy install, + * and the already-installed clause. */ + ExpectIntEQ(wc_RNG_extra_entropy_epoch_init(NULL, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_extra_entropy_epoch_init(test_random_epoch_handler, + NULL), 0); + ExpectIntEQ(wc_RNG_extra_entropy_epoch_init(test_random_epoch_handler, + NULL), + WC_NO_ERR_TRACE(ALREADY_E)); + + /* extra_entropy_epoch_reset: the (handler == NULL) / (arg == NULL) + * pair, then a clean restore to the no-handler state. */ + ExpectIntEQ(wc_RNG_extra_entropy_epoch_reset(NULL, &arg), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_extra_entropy_epoch_reset(&h, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_extra_entropy_epoch_reset(&h, &arg), 0); + ExpectIntEQ(wc_RNG_extra_entropy_epoch_reset(&h, &arg), + WC_NO_ERR_TRACE(BAD_STATE_E)); + + /* entropy_epoch_extra_inv_init/reset: NULL clauses + install + + * already-installed, then a clean restore. */ + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_init(NULL, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_init( + test_random_epoch_inv_handler, NULL), 0); + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_init( + test_random_epoch_inv_handler, NULL), + WC_NO_ERR_TRACE(ALREADY_E)); + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_reset(NULL, &iarg), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_reset(&ih, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_reset(&ih, &iarg), 0); + ExpectIntEQ(wc_RNG_entropy_epoch_extra_inv_reset(&ih, &iarg), + WC_NO_ERR_TRACE(BAD_STATE_E)); + } +#endif + + /* wc_RNG_GetFlags: the (rng == NULL) / (flags == NULL) pair, then a + * clean read on a live instance. */ + XMEMSET(&rng, 0, sizeof(rng)); + ExpectIntEQ(wc_RNG_GetFlags(NULL, &flags), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID), 0); + ExpectIntEQ(wc_RNG_GetFlags(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_GetFlags(&rng, &flags), 0); + + /* wc_RNG_SetFlags / wc_RNG_ClearFlags: the (flags == 0) and + * (flags & ~allowed) halves of the shared policy check. */ + ExpectIntEQ(wc_RNG_SetFlags(&rng, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_SetFlags(&rng, (word32)~WC_RNG_FLAGS_SETTABLE), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_ClearFlags(&rng, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_ClearFlags(&rng, (word32)~WC_RNG_FLAGS_CLEARABLE), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + +#ifdef WC_RNG_HAVE_LOCK + /* The policy check's lock precondition: a LOCK_REQUIRED instance that is + * not held answers OBJECT_NOT_LOCKED_E from both Set and Clear. */ + { + WC_RNG locked; + XMEMSET(&locked, 0, sizeof(locked)); + ExpectIntEQ(wc_InitRng_ex2(&locked, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + ExpectIntEQ(wc_RNG_SetFlags(&locked, WC_RNG_FLAG_FAIL_FAST), + WC_NO_ERR_TRACE(OBJECT_NOT_LOCKED_E)); + ExpectIntEQ(wc_RNG_ClearFlags(&locked, WC_RNG_FLAG_FAIL_FAST), + WC_NO_ERR_TRACE(OBJECT_NOT_LOCKED_E)); + ExpectIntEQ(wc_FreeRng(&locked), 0); + } +#endif + +#ifdef WC_RNG_HAVE_RBGC + /* wc_RNG_SetFlags seed-source policy: the check sits behind the policy + * lock precondition, so both rows run on a LOCK_REQUIRED, held root. + * NO_PRIMARY on a root (no parent to defer to) is MISSING_RNG_E; on a + * child it is legal, and pairing it with an already-set ONLY_PRIMARY is + * rejected. */ + { + WC_RNG root; + WC_RNG *child = NULL; + XMEMSET(&root, 0, sizeof(root)); +#ifdef WC_RNG_HAVE_LOCK + ExpectIntEQ(wc_InitRng_ex2(&root, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + ExpectIntEQ(wc_RNG_lock_get(&root, 0), 0); + ExpectIntEQ(wc_RNG_SetFlags(&root, WC_RNG_FLAG_NO_PRIMARY_SEED), + WC_NO_ERR_TRACE(MISSING_RNG_E)); + ExpectIntEQ(wc_InitRngRBGC_New(&child, &root, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + if (child != NULL) { + ExpectIntEQ(wc_RNG_lock_get(child, 0), 0); + ExpectIntEQ(wc_RNG_SetFlags(child, WC_RNG_FLAG_ONLY_PRIMARY_SEED), + 0); + ExpectIntEQ(wc_RNG_SetFlags(child, WC_RNG_FLAG_NO_PRIMARY_SEED), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_lock_put(child, 0), 0); + ExpectIntEQ(wc_FreeRng(child), 0); + } + ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); +#else + (void)root; + (void)child; +#endif + ExpectIntEQ(wc_FreeRng(&root), 0); + } +#endif + + /* _InitRng() argument guards: the userSeed/nonce/perso NULL-with-size + * clauses and the userSeed+seedRng mutual exclusion, driven through the + * public UserSeed entry point. */ + { + byte seed[64]; + byte nonce[16]; + word32 i; + WC_RNG r; + + for (i = 0; i < sizeof(seed); i++) + seed[i] = (byte)i; + for (i = 0; i < sizeof(nonce); i++) + nonce[i] = (byte)(i + 1); + + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRngNonce_UserSeed(&r, NULL, 16, nonce, + sizeof(nonce), NULL, 0, + HEAP_HINT, INVALID_DEVID, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRngNonce_UserSeed(&r, seed, sizeof(seed), + NULL, 16, NULL, 0, + HEAP_HINT, INVALID_DEVID, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRngNonce_UserSeed(&r, seed, sizeof(seed), nonce, + sizeof(nonce), NULL, 16, + HEAP_HINT, INVALID_DEVID, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); +#if defined(HAVE_HASHDRBG) + /* userSeedSz <= SEED_BLOCK_SZ is rejected (the health-test block + * would be the whole seed); Hash_DRBG-only check. */ + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRngNonce_UserSeed(&r, seed, 16, nonce, + sizeof(nonce), NULL, 0, + HEAP_HINT, INVALID_DEVID, 0), + WC_NO_ERR_TRACE(BAD_LENGTH_E)); +#endif + } + + /* _InitRng() NO_PRIMARY_SEED with no credited source. */ +#ifdef WC_RNG_HAVE_RBGC + { + WC_RNG r; + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRng_ex2(&r, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_NO_PRIMARY_SEED), + WC_NO_ERR_TRACE(MISSING_RNG_E)); + } +#endif + + /* wc_RNG_DRBG_GetReseedCtr: the (rng == NULL) / (reseedCtr == NULL) + * pair, then a clean read on a live instance. WOLFSSL_LOCAL, so the + * WOLFSSL_TEST_STATIC_BUILD guard keeps shared builds linkable; the + * declaration itself needs HAVE_HASHDRBG (absent in the + * CUSTOM_RAND_GENERATE_BLOCK variant). */ +#if defined(WOLFSSL_TEST_STATIC_BUILD) && defined(HAVE_HASHDRBG) + { + WC_RNG r; + wc_drbg_reseed_ctr_t ctr; + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_RNG_DRBG_GetReseedCtr(NULL, &ctr), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_InitRng_ex(&r, HEAP_HINT, INVALID_DEVID), 0); + ExpectIntEQ(wc_RNG_DRBG_GetReseedCtr(&r, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_GetReseedCtr(&r, &ctr), 0); + ExpectIntEQ(wc_FreeRng(&r), 0); + } +#endif + + /* wc_RNG_TestSeed on a long seed: the cutoff > window/2 clause's true + * side (the short-seed false side is driven by + * test_wc_RngInitFlagsDecisionCoverage). HAVE_HASHDRBG: the declaration + * is absent where the Hash_DRBG is not compiled. */ +#if defined(HAVE_HASHDRBG) + { + byte longseed[512]; + word32 i; + for (i = 0; i < sizeof(longseed); i++) + longseed[i] = (byte)(i * 3u + 1u); + (void)wc_RNG_TestSeed(longseed, sizeof(longseed)); + } +#endif + + /* wc_RNG_invalidate_entropy / wc_RNG_entropy_needs_recovery on a live + * instance: the invalidate-then-query pair. */ + { + WC_RNG r; + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRng_ex(&r, HEAP_HINT, INVALID_DEVID), 0); + ExpectIntEQ(wc_RNG_invalidate_entropy(NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_invalidate_entropy(&r), 0); + ExpectIntEQ(wc_RNG_entropy_needs_recovery(NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + (void)wc_RNG_entropy_needs_recovery(&r); + ExpectIntEQ(wc_FreeRng(&r), 0); + } + +#ifdef WC_RNG_HAVE_POOL + /* wc_RNG_Pool_Current NULL-argument pair. */ + { + WC_RNG r; + word32 n = 0; + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_RNG_Pool_Current(NULL, &n), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_InitRng_ex(&r, HEAP_HINT, INVALID_DEVID), 0); + ExpectIntEQ(wc_RNG_Pool_Current(&r, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + (void)wc_RNG_Pool_Current(&r, &n); + ExpectIntEQ(wc_FreeRng(&r), 0); + } +#endif + + DoExpectIntEQ(wc_FreeRng(&rng), 0); +#endif + return EXPECT_RESULT(); +} + +/* + * MC/DC decision coverage for the next-seed aperture state machine, the RBGC + * stratum entry points, and the lock CAS front doors (master 2026-11). These + * are the stateful halves of the extras machinery; the argument-guard halves + * live in test_wc_RngExtrasDecisionCoverage(). Guarded by the feature macros + * so each section runs only in the campaign variant that compiles it + * (rbgc / rbgc_lock / full_mutex). New post-v4.1.0 APIs: frozen-module guard. + */ +int test_wc_RngExtrasStateMachines(void) +{ + EXPECT_DECLS; +#if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) +#if defined(WC_RNG_HAVE_NEXT_SEED) || defined(WC_RNG_HAVE_RBGC) \ + || defined(WC_RNG_HAVE_LOCK) + WC_RNG rng; + byte out[16]; + + XMEMSET(&rng, 0, sizeof(rng)); + XMEMSET(out, 0, sizeof(out)); + +#ifdef WC_RNG_HAVE_LOCK + /* wc_RNG_lock_get / wc_RNG_lock_put / wc_RNG_lock_read: the basic + * acquire-release cycle on a LOCK_REQUIRED instance, plus the + * lock_read NULL-argument pair. */ + { + WC_RNG locked; + WC_RNG_lock_arg_t state; + XMEMSET(&locked, 0, sizeof(locked)); + ExpectIntEQ(wc_InitRng_ex2(&locked, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + ExpectIntEQ(wc_RNG_lock_get(&locked, 0), 0); + ExpectIntEQ(wc_RNG_lock_get(&locked, 0), + WC_NO_ERR_TRACE(BUSY_E)); + ExpectIntEQ(wc_RNG_lock_read(&locked, &state), 0); + ExpectIntEQ(wc_RNG_lock_put(&locked, 0), 0); + ExpectIntEQ(wc_RNG_lock_read(NULL, &state), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_lock_read(&locked, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_FreeRng(&locked), 0); + } + + /* wc_RNG_lock_get_conditional: the (rng == NULL) clause, then a clean + * conditional acquire on a free lease. */ + { + WC_RNG locked; + XMEMSET(&locked, 0, sizeof(locked)); + ExpectIntEQ(wc_InitRng_ex2(&locked, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + ExpectIntEQ(wc_RNG_lock_get_conditional(NULL, 0, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_lock_get_conditional(&locked, 0, 0), 0); + ExpectIntEQ(wc_RNG_lock_put(&locked, 0), 0); + ExpectIntEQ(wc_FreeRng(&locked), 0); + } + + /* rng_lock_required_check's OBJECT_NOT_LOCKED_E arm: a LOCK_REQUIRED + * instance that is not held rejects a locked-only public call. */ + { + WC_RNG locked; + XMEMSET(&locked, 0, sizeof(locked)); + ExpectIntEQ(wc_InitRng_ex2(&locked, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + ExpectIntEQ(wc_RNG_GenerateBlock(&locked, out, sizeof(out)), + WC_NO_ERR_TRACE(OBJECT_NOT_LOCKED_E)); + ExpectIntEQ(wc_FreeRng(&locked), 0); + } +#endif + +#ifdef WC_RNG_HAVE_NEXT_SEED + /* The next-seed aperture state machine: produce, read, consume, stir, + * then invalidate-entropy to force a purge and produce again. Drives the + * NextSeedPurge state clauses and the NextSeedGenerate_local / + * NextSeedNow_AddIn_local state checks. */ + { + WC_RNG ns; + WC_ATOMIC_INT_ARG n = 0; + XMEMSET(&ns, 0, sizeof(ns)); + /* A plain (non-LOCK_REQUIRED) instance: rng_lock_required_check is a + * no-op, so the next-seed aperture is reachable without a lease. */ + ExpectIntEQ(wc_InitRng_ex(&ns, HEAP_HINT, INVALID_DEVID), 0); + ExpectIntEQ(wc_RNG_DRBG_NextSeedGenerate(NULL, 1), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + (void)wc_RNG_DRBG_NextSeedGenerate(&ns, 1); + ExpectIntEQ(wc_RNG_DRBG_NextSeedCurrent(NULL, &n), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_NextSeedCurrent(&ns, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_NextSeedCurrent(&ns, &n), 0); + /* Consume the ready bank (NOT_READY_E is the legal empty answer). */ + (void)wc_RNG_DRBG_NextSeedNow(&ns); + (void)wc_RNG_DRBG_NextSeedNow_AddIn(&ns, out, sizeof(out)); + /* Stir store: NULL-arg pair + a clean store. */ + ExpectIntEQ(wc_RNG_DRBG_NextStirStore(NULL, out, sizeof(out)), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_NextStirStore(&ns, NULL, sizeof(out)), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + (void)wc_RNG_DRBG_NextStirStore(&ns, out, sizeof(out)); + (void)wc_RNG_DRBG_NextStirNow(NULL); + (void)wc_RNG_DRBG_NextStirNow(&ns); + /* Invalidate the entropy (purges the aperture) and produce again. */ + (void)wc_RNG_invalidate_entropy(&ns); + (void)wc_RNG_DRBG_NextSeedGenerate(&ns, 1); + (void)wc_RNG_DRBG_NextSeedCurrent(&ns, &n); + ExpectIntEQ(wc_FreeRng(&ns), 0); + } +#endif + +#ifdef WC_RNG_HAVE_RBGC + /* The RBGC stratum entry points: the shared (child/root) guard clause, + * the reseed/stir guards, and a credited child reseed that drives the + * credit/stratum/pid clauses. */ + { + WC_RNG root; + WC_RNG *child = NULL; + word32 child_flags = 0; + XMEMSET(&root, 0, sizeof(root)); +#ifdef WC_RNG_HAVE_LOCK + ExpectIntEQ(wc_InitRng_ex2(&root, HEAP_HINT, INVALID_DEVID, + WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); + child_flags = WC_RNG_INIT_FLAG_LOCK_REQUIRED; + ExpectIntEQ(wc_RNG_lock_get(&root, 0), 0); +#else + ExpectIntEQ(wc_InitRng_ex(&root, HEAP_HINT, INVALID_DEVID), 0); +#endif + /* Shared guard clause: NULL child, NULL root, child == root. */ + ExpectIntEQ(wc_InitRngRBGC(NULL, &root, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + { + WC_RNG tmp; + XMEMSET(&tmp, 0, sizeof(tmp)); + ExpectIntEQ(wc_InitRngRBGC(&tmp, NULL, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_InitRngRBGC(&tmp, &tmp, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + } + ExpectIntEQ(wc_InitRngRBGC_New(&child, &root, child_flags), 0); + if (child != NULL) { + /* ReseedRBGC / StirRBGC guard clause: rng == NULL, root == NULL, + * rng == root, and nonce-NULL-with-size (all before the lock + * check, so no lease is needed). */ + ExpectIntEQ(wc_RNG_DRBG_ReseedRBGC(NULL, &root, NULL, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_ReseedRBGC(&root, &root, NULL, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_ReseedRBGC(child, &root, NULL, 16), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_RNG_DRBG_StirRBGC(NULL, &root, NULL, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); +#ifdef WC_RNG_HAVE_LOCK + ExpectIntEQ(wc_RNG_lock_get(child, 0), 0); +#endif + /* A credited child reseed drives the credit/stratum clauses. */ + (void)wc_RNG_DRBG_ReseedRBGC(child, &root, out, sizeof(out)); + (void)wc_RNG_DRBG_StirRBGC(child, &root, out, sizeof(out)); + /* NextSeedGenerate_RBGC: the root == NULL clause. */ + ExpectIntEQ(wc_RNG_DRBG_NextSeedGenerate_RBGC(child, NULL, 1), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + (void)wc_RNG_DRBG_NextSeedGenerate_RBGC(child, &root, 1); +#ifdef WC_RNG_HAVE_LOCK + ExpectIntEQ(wc_RNG_lock_put(child, 0), 0); +#endif + ExpectIntEQ(wc_FreeRng(child), 0); + } +#ifdef WC_RNG_HAVE_LOCK + ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); +#endif + ExpectIntEQ(wc_FreeRng(&root), 0); + } +#endif + + DoExpectIntEQ(wc_FreeRng(&rng), 0); +#endif +#endif + return EXPECT_RESULT(); +} diff --git a/tests/api/test_random.h b/tests/api/test_random.h index dd007fb83c4..bc5f7e12cd7 100644 --- a/tests/api/test_random.h +++ b/tests/api/test_random.h @@ -47,6 +47,8 @@ int test_wc_DrbgDecisionCoverage(void); int test_wc_DrbgFeatureCoverage(void); int test_wc_DrbgReworkDecisionCoverage(void); int test_wc_RngInitFlagsDecisionCoverage(void); +int test_wc_RngExtrasDecisionCoverage(void); +int test_wc_RngExtrasStateMachines(void); int test_wc_Entropy_Get(void); #define TEST_RANDOM_DECLS \ @@ -73,6 +75,8 @@ int test_wc_Entropy_Get(void); TEST_DECL_GROUP("random", test_wc_DrbgFeatureCoverage), \ TEST_DECL_GROUP("random", test_wc_DrbgReworkDecisionCoverage), \ TEST_DECL_GROUP("random", test_wc_Entropy_Get), \ - TEST_DECL_GROUP("random", test_wc_RngInitFlagsDecisionCoverage) + TEST_DECL_GROUP("random", test_wc_RngInitFlagsDecisionCoverage), \ + TEST_DECL_GROUP("random", test_wc_RngExtrasDecisionCoverage), \ + TEST_DECL_GROUP("random", test_wc_RngExtrasStateMachines) #endif /* WOLFCRYPT_TEST_RANDOM_H */ diff --git a/tests/unit-mcdc/test_random_fault_whitebox.c b/tests/unit-mcdc/test_random_fault_whitebox.c index 0ed8c5f5edb..efa17a99ad2 100644 --- a/tests/unit-mcdc/test_random_fault_whitebox.c +++ b/tests/unit-mcdc/test_random_fault_whitebox.c @@ -1227,6 +1227,96 @@ static void wb_healthtest512_uninstantiate_fault(void) { WB_NOTE("SHA-512 Hash_DRBG not compiled in this variant; skipped"); } #endif +/* -------------------------------------------------------------------------- + * 9b. wc_FreeRng() / RngInitWipe() uninstantiate-fault chains: + * :8139 (ret == 0) && (uninst_ret != 0) and the sibling (uninst_ret + * != 0) && (ret == 0) forms across the SHA-256 / SHA-512 DRBG + * and scratch-buffer free paths. + * + * The public boundary always succeeds on the non-DRBG steps of a free, so + * ret == 0 there; the only way uninst_ret goes nonzero is a failed + * Uninstantiate, whose single cause is the ForceZero() zero-check. The + * faultable ForceZero armed for the DRBG-struct size skips exactly that + * wipe, so Uninstantiate reports DRBG_FAILURE deterministically. The false + * side (uninst_ret == 0) is driven by every clean free elsewhere in this + * whitebox; this drives the true side. + * ----------------------------------------------------------------------- */ +static void wb_free_uninstantiate_fault(void) +{ +#if defined(HAVE_HASHDRBG) && !defined(NO_SHA256) && !defined(WC_NO_RNG) + WC_RNG rng; + XMEMSET(&rng, 0, sizeof(rng)); + if (wc_InitRng_ex(&rng, NULL, INVALID_DEVID) != 0) { + WB_NOTE("free-uninst: init failed"); + wb_fail = 1; + return; + } + mcdc_fz_arm(sizeof(DRBG_internal)); + (void)wc_FreeRng(&rng); + mcdc_fz_disarm(); + WB_NOTE("free uninstantiate-fault rows driven (SHA-256)"); +#endif +#if defined(HAVE_HASHDRBG) && defined(WOLFSSL_DRBG_SHA512) && !defined(WC_NO_RNG) + WC_RNG rng512; + XMEMSET(&rng512, 0, sizeof(rng512)); + if (wc_InitRng_ex(&rng512, NULL, INVALID_DEVID) != 0) { + WB_NOTE("free-uninst: 512 init failed"); + wb_fail = 1; + return; + } + mcdc_fz_arm(sizeof(DRBG_SHA512_internal)); + (void)wc_FreeRng(&rng512); + mcdc_fz_disarm(); + WB_NOTE("free uninstantiate-fault rows driven (SHA-512)"); +#endif + + /* _InitRng() cleanup uninst chains: a failed Instantiate (first Hash_df + * faulted) that then faulted the cleanup Uninstantiate. Drives the + * (ret != 0) && (uninst_ret != 0) / (ret == 0) && (uninst_ret != 0) + * rows in the init-failure teardown. */ +#if defined(HAVE_HASHDRBG) && !defined(NO_SHA256) && !defined(WC_NO_RNG) \ + && defined(MCDC_FH_HAVE_SHA256) + WC_RNG rnf; + XMEMSET(&rnf, 0, sizeof(rnf)); + mcdc_fh_arm(1); + mcdc_fz_arm(sizeof(DRBG_internal)); + (void)wc_InitRng_ex(&rnf, NULL, INVALID_DEVID); + mcdc_fz_disarm(); + mcdc_fh_disarm(); + WB_NOTE("init-failure uninst-fault rows driven"); +#endif +} + +/* -------------------------------------------------------------------------- + * 9c. Recovery reseed fault rows: wc_RNG_DRBG_Reseed_Now() on an + * entropy-invalidated instance with the reseed hash faulted, so the + * recovery path reports a nonzero verdict (the (ret != 0) && (ret != + * NEEDS_RECOVERY_E) / (ret != 0) && (rng->status == DRBG_OK) rows). + * ----------------------------------------------------------------------- */ +#if defined(HAVE_HASHDRBG) && !defined(NO_SHA256) && !defined(WC_NO_RNG) \ + && defined(MCDC_FH_HAVE_SHA256) +static void wb_recovery_reseed_fault(void) +{ + WC_RNG rng; + XMEMSET(&rng, 0, sizeof(rng)); + if (wc_InitRng_ex(&rng, NULL, INVALID_DEVID) != 0) { + WB_NOTE("recovery-reseed: init failed"); + wb_fail = 1; + return; + } + /* Invalidate the entropy so the next reseed takes the recovery path. */ + (void)wc_RNG_invalidate_entropy(&rng); + mcdc_fh_arm(1); + (void)wc_RNG_DRBG_Reseed_Now(&rng, NULL, 0); + mcdc_fh_disarm(); + (void)wc_FreeRng(&rng); + WB_NOTE("recovery reseed-fault rows driven"); +} +#else +static void wb_recovery_reseed_fault(void) +{ WB_NOTE("recovery reseed fault: not compiled in this variant; skipped"); } +#endif + /* -------------------------------------------------------------------------- * 10. wc_RNG_DRBG_GetReseedCtr() DRBG-type rows: * :1354 (rng->drbgType == WC_DRBG_SHA256) && (rng->drbg != NULL) @@ -1354,6 +1444,8 @@ int main(void) wb_init_flags(); wb_healthtest_uninstantiate_fault(); wb_healthtest512_uninstantiate_fault(); + wb_free_uninstantiate_fault(); + wb_recovery_reseed_fault(); wb_drbg_reseedctr_type_rows(); wb_stir_nonce_null_rng(); printf("done (%s)\n", wb_fail ? "with failures" : "ok"); From c84e3498cb1e9f37d427b1b99db6ce689e8f3836 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 18 Nov 2026 20:02:10 +0100 Subject: [PATCH 2/5] Fix RNG MC/DC leaks and guard epoch callbacks --- tests/api/test_random.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/api/test_random.c b/tests/api/test_random.c index 819bc53cf8c..dcf7038a482 100644 --- a/tests/api/test_random.c +++ b/tests/api/test_random.c @@ -1975,7 +1975,8 @@ int test_wc_RngInitFlagsDecisionCoverage(void) * rbgc campaign variants. New post-v4.1.0 APIs, hence the frozen-module * guard. */ -#if defined(WC_RNG_HAVE_ENTROPY_EPOCH) +#if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) && \ + defined(WC_RNG_HAVE_ENTROPY_EPOCH) static wc_RNG_entropy_epoch_t test_random_epoch_handler(void *arg) { (void)arg; @@ -2104,7 +2105,7 @@ int test_wc_RngExtrasDecisionCoverage(void) ExpectIntEQ(wc_RNG_SetFlags(child, WC_RNG_FLAG_NO_PRIMARY_SEED), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_RNG_lock_put(child, 0), 0); - ExpectIntEQ(wc_FreeRng(child), 0); + wc_rng_free(child); } ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); #else @@ -2398,7 +2399,7 @@ int test_wc_RngExtrasStateMachines(void) #ifdef WC_RNG_HAVE_LOCK ExpectIntEQ(wc_RNG_lock_put(child, 0), 0); #endif - ExpectIntEQ(wc_FreeRng(child), 0); + wc_rng_free(child); } #ifdef WC_RNG_HAVE_LOCK ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); From 783be376a9226331564e91fdceb3673c12300b50 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 18 Nov 2026 20:10:22 +0100 Subject: [PATCH 3/5] Address RNG MC/DC test review findings --- tests/api/test_random.c | 34 ++++++-- tests/unit-mcdc/test_random_fault_whitebox.c | 87 +++++++++++--------- 2 files changed, 73 insertions(+), 48 deletions(-) diff --git a/tests/api/test_random.c b/tests/api/test_random.c index dcf7038a482..7caa5d34416 100644 --- a/tests/api/test_random.c +++ b/tests/api/test_random.c @@ -2080,7 +2080,7 @@ int test_wc_RngExtrasDecisionCoverage(void) } #endif -#ifdef WC_RNG_HAVE_RBGC +#if defined(WC_RNG_HAVE_RBGC) && defined(WC_RNG_HAVE_LOCK) /* wc_RNG_SetFlags seed-source policy: the check sits behind the policy * lock precondition, so both rows run on a LOCK_REQUIRED, held root. * NO_PRIMARY on a root (no parent to defer to) is MISSING_RNG_E; on a @@ -2088,14 +2088,16 @@ int test_wc_RngExtrasDecisionCoverage(void) * rejected. */ { WC_RNG root; +#ifndef WC_NO_CONSTRUCTORS WC_RNG *child = NULL; +#endif XMEMSET(&root, 0, sizeof(root)); -#ifdef WC_RNG_HAVE_LOCK ExpectIntEQ(wc_InitRng_ex2(&root, HEAP_HINT, INVALID_DEVID, WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); ExpectIntEQ(wc_RNG_lock_get(&root, 0), 0); ExpectIntEQ(wc_RNG_SetFlags(&root, WC_RNG_FLAG_NO_PRIMARY_SEED), WC_NO_ERR_TRACE(MISSING_RNG_E)); +#ifndef WC_NO_CONSTRUCTORS ExpectIntEQ(wc_InitRngRBGC_New(&child, &root, WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); if (child != NULL) { @@ -2107,18 +2109,26 @@ int test_wc_RngExtrasDecisionCoverage(void) ExpectIntEQ(wc_RNG_lock_put(child, 0), 0); wc_rng_free(child); } - ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); -#else - (void)root; - (void)child; #endif + ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); ExpectIntEQ(wc_FreeRng(&root), 0); } #endif - /* _InitRng() argument guards: the userSeed/nonce/perso NULL-with-size - * clauses and the userSeed+seedRng mutual exclusion, driven through the - * public UserSeed entry point. */ + /* _InitRng() rejects a NULL nonce or personalization buffer with a + * nonzero length. wc_InitRngNonce_ex2 passes the arguments through. */ + { + WC_RNG r; + byte nonce[16] = { 0 }; + + XMEMSET(&r, 0, sizeof(r)); + ExpectIntEQ(wc_InitRngNonce_ex2(&r, NULL, 16, NULL, 0, + HEAP_HINT, INVALID_DEVID, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_InitRngNonce_ex2(&r, nonce, sizeof(nonce), NULL, 16, + HEAP_HINT, INVALID_DEVID, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + } + + /* Argument validation in the public user-seed wrapper. */ { byte seed[64]; byte nonce[16]; @@ -2351,13 +2361,17 @@ int test_wc_RngExtrasStateMachines(void) * credit/stratum/pid clauses. */ { WC_RNG root; +#ifndef WC_NO_CONSTRUCTORS WC_RNG *child = NULL; word32 child_flags = 0; +#endif XMEMSET(&root, 0, sizeof(root)); #ifdef WC_RNG_HAVE_LOCK ExpectIntEQ(wc_InitRng_ex2(&root, HEAP_HINT, INVALID_DEVID, WC_RNG_INIT_FLAG_LOCK_REQUIRED), 0); +#ifndef WC_NO_CONSTRUCTORS child_flags = WC_RNG_INIT_FLAG_LOCK_REQUIRED; +#endif ExpectIntEQ(wc_RNG_lock_get(&root, 0), 0); #else ExpectIntEQ(wc_InitRng_ex(&root, HEAP_HINT, INVALID_DEVID), 0); @@ -2373,6 +2387,7 @@ int test_wc_RngExtrasStateMachines(void) ExpectIntEQ(wc_InitRngRBGC(&tmp, &tmp, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); } +#ifndef WC_NO_CONSTRUCTORS ExpectIntEQ(wc_InitRngRBGC_New(&child, &root, child_flags), 0); if (child != NULL) { /* ReseedRBGC / StirRBGC guard clause: rng == NULL, root == NULL, @@ -2401,6 +2416,7 @@ int test_wc_RngExtrasStateMachines(void) #endif wc_rng_free(child); } +#endif #ifdef WC_RNG_HAVE_LOCK ExpectIntEQ(wc_RNG_lock_put(&root, 0), 0); #endif diff --git a/tests/unit-mcdc/test_random_fault_whitebox.c b/tests/unit-mcdc/test_random_fault_whitebox.c index efa17a99ad2..644bf6bc8f5 100644 --- a/tests/unit-mcdc/test_random_fault_whitebox.c +++ b/tests/unit-mcdc/test_random_fault_whitebox.c @@ -1243,47 +1243,49 @@ static void wb_healthtest512_uninstantiate_fault(void) * ----------------------------------------------------------------------- */ static void wb_free_uninstantiate_fault(void) { -#if defined(HAVE_HASHDRBG) && !defined(NO_SHA256) && !defined(WC_NO_RNG) - WC_RNG rng; - XMEMSET(&rng, 0, sizeof(rng)); - if (wc_InitRng_ex(&rng, NULL, INVALID_DEVID) != 0) { - WB_NOTE("free-uninst: init failed"); - wb_fail = 1; - return; +#if defined(HAVE_HASHDRBG) && !defined(WC_NO_RNG) +#ifndef NO_SHA256 + { + WC_RNG rng; + int ret; + + XMEMSET(&rng, 0, sizeof(rng)); + if (wc_InitRng_ex2(&rng, NULL, INVALID_DEVID, + WC_RNG_INIT_FLAG_DRBG_SHA256) != 0) { + WB_NOTE("free-uninst: SHA-256 init failed"); + wb_fail = 1; + return; + } + mcdc_fz_arm(sizeof(DRBG_internal)); + ret = wc_FreeRng(&rng); + mcdc_fz_disarm(); + if (ret != RNG_FAILURE_E) { + WB_NOTE("free-uninst: SHA-256 wipe fault did not return RNG_FAILURE_E"); + wb_fail = 1; + } } - mcdc_fz_arm(sizeof(DRBG_internal)); - (void)wc_FreeRng(&rng); - mcdc_fz_disarm(); - WB_NOTE("free uninstantiate-fault rows driven (SHA-256)"); #endif -#if defined(HAVE_HASHDRBG) && defined(WOLFSSL_DRBG_SHA512) && !defined(WC_NO_RNG) - WC_RNG rng512; - XMEMSET(&rng512, 0, sizeof(rng512)); - if (wc_InitRng_ex(&rng512, NULL, INVALID_DEVID) != 0) { - WB_NOTE("free-uninst: 512 init failed"); - wb_fail = 1; - return; +#ifdef WOLFSSL_DRBG_SHA512 + { + WC_RNG rng; + int ret; + + XMEMSET(&rng, 0, sizeof(rng)); + if (wc_InitRng_ex2(&rng, NULL, INVALID_DEVID, + WC_RNG_INIT_FLAG_DRBG_SHA512) != 0) { + WB_NOTE("free-uninst: SHA-512 init failed"); + wb_fail = 1; + return; + } + mcdc_fz_arm(sizeof(DRBG_SHA512_internal)); + ret = wc_FreeRng(&rng); + mcdc_fz_disarm(); + if (ret != RNG_FAILURE_E) { + WB_NOTE("free-uninst: SHA-512 wipe fault did not return RNG_FAILURE_E"); + wb_fail = 1; + } } - mcdc_fz_arm(sizeof(DRBG_SHA512_internal)); - (void)wc_FreeRng(&rng512); - mcdc_fz_disarm(); - WB_NOTE("free uninstantiate-fault rows driven (SHA-512)"); #endif - - /* _InitRng() cleanup uninst chains: a failed Instantiate (first Hash_df - * faulted) that then faulted the cleanup Uninstantiate. Drives the - * (ret != 0) && (uninst_ret != 0) / (ret == 0) && (uninst_ret != 0) - * rows in the init-failure teardown. */ -#if defined(HAVE_HASHDRBG) && !defined(NO_SHA256) && !defined(WC_NO_RNG) \ - && defined(MCDC_FH_HAVE_SHA256) - WC_RNG rnf; - XMEMSET(&rnf, 0, sizeof(rnf)); - mcdc_fh_arm(1); - mcdc_fz_arm(sizeof(DRBG_internal)); - (void)wc_InitRng_ex(&rnf, NULL, INVALID_DEVID); - mcdc_fz_disarm(); - mcdc_fh_disarm(); - WB_NOTE("init-failure uninst-fault rows driven"); #endif } @@ -1298,8 +1300,11 @@ static void wb_free_uninstantiate_fault(void) static void wb_recovery_reseed_fault(void) { WC_RNG rng; + int ret; + XMEMSET(&rng, 0, sizeof(rng)); - if (wc_InitRng_ex(&rng, NULL, INVALID_DEVID) != 0) { + if (wc_InitRng_ex2(&rng, NULL, INVALID_DEVID, + WC_RNG_INIT_FLAG_DRBG_SHA256) != 0) { WB_NOTE("recovery-reseed: init failed"); wb_fail = 1; return; @@ -1307,7 +1312,11 @@ static void wb_recovery_reseed_fault(void) /* Invalidate the entropy so the next reseed takes the recovery path. */ (void)wc_RNG_invalidate_entropy(&rng); mcdc_fh_arm(1); - (void)wc_RNG_DRBG_Reseed_Now(&rng, NULL, 0); + ret = wc_RNG_DRBG_Reseed_Now(&rng, NULL, 0); + if (ret == 0 || mcdc_fh_seen() == 0) { + WB_NOTE("recovery-reseed: expected a faulted hash to fail"); + wb_fail = 1; + } mcdc_fh_disarm(); (void)wc_FreeRng(&rng); WB_NOTE("recovery reseed-fault rows driven"); From 779a45f60930b7a94022b58a04dca6051249330b Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 19 Nov 2026 12:17:38 +0100 Subject: [PATCH 4/5] Fix PRB-multi-test CI in RNG MC/DC tests --- tests/api/test_random.c | 2 +- tests/unit-mcdc/test_random_fault_whitebox.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/api/test_random.c b/tests/api/test_random.c index 7caa5d34416..8321bdb068c 100644 --- a/tests/api/test_random.c +++ b/tests/api/test_random.c @@ -2206,7 +2206,7 @@ int test_wc_RngExtrasDecisionCoverage(void) byte longseed[512]; word32 i; for (i = 0; i < sizeof(longseed); i++) - longseed[i] = (byte)(i * 3u + 1u); + longseed[i] = (byte)(i * 3U + 1U); (void)wc_RNG_TestSeed(longseed, sizeof(longseed)); } #endif diff --git a/tests/unit-mcdc/test_random_fault_whitebox.c b/tests/unit-mcdc/test_random_fault_whitebox.c index 644bf6bc8f5..e903784c673 100644 --- a/tests/unit-mcdc/test_random_fault_whitebox.c +++ b/tests/unit-mcdc/test_random_fault_whitebox.c @@ -1259,7 +1259,7 @@ static void wb_free_uninstantiate_fault(void) mcdc_fz_arm(sizeof(DRBG_internal)); ret = wc_FreeRng(&rng); mcdc_fz_disarm(); - if (ret != RNG_FAILURE_E) { + if (ret != WC_NO_ERR_TRACE(RNG_FAILURE_E)) { WB_NOTE("free-uninst: SHA-256 wipe fault did not return RNG_FAILURE_E"); wb_fail = 1; } @@ -1280,7 +1280,7 @@ static void wb_free_uninstantiate_fault(void) mcdc_fz_arm(sizeof(DRBG_SHA512_internal)); ret = wc_FreeRng(&rng); mcdc_fz_disarm(); - if (ret != RNG_FAILURE_E) { + if (ret != WC_NO_ERR_TRACE(RNG_FAILURE_E)) { WB_NOTE("free-uninst: SHA-512 wipe fault did not return RNG_FAILURE_E"); wb_fail = 1; } From 1e1998d32d86f9a84b46c13eaf2bb4ed8371edfb Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 19 Nov 2026 17:43:20 +0100 Subject: [PATCH 5/5] Guard RBGC next-seed test calls --- tests/api/test_random.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/api/test_random.c b/tests/api/test_random.c index 8321bdb068c..f170b715314 100644 --- a/tests/api/test_random.c +++ b/tests/api/test_random.c @@ -2407,10 +2407,12 @@ int test_wc_RngExtrasStateMachines(void) /* A credited child reseed drives the credit/stratum clauses. */ (void)wc_RNG_DRBG_ReseedRBGC(child, &root, out, sizeof(out)); (void)wc_RNG_DRBG_StirRBGC(child, &root, out, sizeof(out)); +#ifdef WC_RNG_HAVE_NEXT_SEED /* NextSeedGenerate_RBGC: the root == NULL clause. */ ExpectIntEQ(wc_RNG_DRBG_NextSeedGenerate_RBGC(child, NULL, 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); (void)wc_RNG_DRBG_NextSeedGenerate_RBGC(child, &root, 1); +#endif #ifdef WC_RNG_HAVE_LOCK ExpectIntEQ(wc_RNG_lock_put(child, 0), 0); #endif