From 69786c2f006796c3237682a6ffaac3ced98ebbdf Mon Sep 17 00:00:00 2001 From: night1rider Date: Mon, 24 Aug 2026 13:48:05 -0600 Subject: [PATCH] Add unit test coverage for wc_ecc_ctx_get_rng --- tests/api/test_ecc.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/api/test_ecc.c b/tests/api/test_ecc.c index cd3710bc16..5849a6e48a 100644 --- a/tests/api/test_ecc.c +++ b/tests/api/test_ecc.c @@ -1679,8 +1679,9 @@ int test_wc_ecc_ctx_set_info(void) /* * Testing the crypto-callback context accessors wc_ecc_ctx_get_algo, - * wc_ecc_ctx_get_kdf_salt, wc_ecc_ctx_get_info, wc_ecc_ctx_get_mac_salt and - * wc_ecc_ctx_get_protocol (built only when WOLF_CRYPTO_CB is enabled). + * wc_ecc_ctx_get_kdf_salt, wc_ecc_ctx_get_info, wc_ecc_ctx_get_mac_salt, + * wc_ecc_ctx_get_protocol and wc_ecc_ctx_get_rng (built only when + * WOLF_CRYPTO_CB is enabled). */ int test_wc_ecc_ctx_getters(void) { @@ -1807,6 +1808,39 @@ int test_wc_ecc_ctx_getters(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); } + /* get_rng: the caller's RNG must come back out unchanged. The ASU ECIES + * offload uses it for the GCM nonce instead of standing up its own DRBG, + * so a getter that returned NULL or a different RNG would silently change + * where that nonce comes from. */ + { + WC_RNG rng2; + WC_RNG* gotRng = NULL; + + XMEMSET(&rng2, 0, sizeof(rng2)); + + /* Point the out-parameter somewhere else first, so the checks fail if + * the getter never writes it. */ + gotRng = &rng2; + ExpectIntEQ(wc_ecc_ctx_get_rng(ctx, &gotRng), 0); + ExpectPtrEq(gotRng, &rng); + + /* wc_ecc_ctx_reset() swaps the RNG, and the getter has to follow. */ + ExpectIntEQ(wc_InitRng(&rng2), 0); + ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng2), 0); + gotRng = NULL; + ExpectIntEQ(wc_ecc_ctx_get_rng(ctx, &gotRng), 0); + ExpectPtrEq(gotRng, &rng2); + /* Put the original RNG back so rng2 can be freed here. */ + ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0); + DoExpectIntEQ(wc_FreeRng(&rng2), 0); + + /* bad args: NULL ctx / NULL out-parameter */ + ExpectIntEQ(wc_ecc_ctx_get_rng(NULL, &gotRng), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_ecc_ctx_get_rng(ctx, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + } + wc_ecc_ctx_free(ctx); DoExpectIntEQ(wc_FreeRng(&rng), 0); #endif