From e9247d5150fde23f24225b7f94ff7cac1a89e89f Mon Sep 17 00:00:00 2001 From: Aidan Keefe Date: Thu, 22 Oct 2026 16:33:55 -0600 Subject: [PATCH] Fenrir fixes : ecc.c Fenrir 9930: harden protocol rules Updated docs to reflect what was assumed by all tests --- doc/dox_comments/header_files/ecc.h | 10 ++++++---- tests/api/test_ecc.c | 5 +++++ wolfcrypt/src/ecc.c | 9 +++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doc/dox_comments/header_files/ecc.h b/doc/dox_comments/header_files/ecc.h index 589918da90f..ddc3b6dec65 100644 --- a/doc/dox_comments/header_files/ecc.h +++ b/doc/dox_comments/header_files/ecc.h @@ -3405,13 +3405,14 @@ int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp); \return ecEncCtx pointer on success \return NULL on failure - \param flags Context flags + \param flags Either REQ_RESP_CLIENT or REQ_RESP_SERVER; any other + value fails and NULL is returned \param rng Random number generator _Example_ \code WC_RNG rng; - ecEncCtx* ctx = wc_ecc_ctx_new(0, &rng); + ecEncCtx* ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng); \endcode \sa wc_ecc_ctx_free @@ -3425,14 +3426,15 @@ ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng); \return ecEncCtx pointer on success \return NULL on failure - \param flags Context flags + \param flags Either REQ_RESP_CLIENT or REQ_RESP_SERVER; any other + value fails and NULL is returned \param rng Random number generator \param heap Heap hint _Example_ \code WC_RNG rng; - ecEncCtx* ctx = wc_ecc_ctx_new_ex(0, &rng, NULL); + ecEncCtx* ctx = wc_ecc_ctx_new_ex(REQ_RESP_CLIENT, &rng, NULL); \endcode \sa wc_ecc_ctx_new diff --git a/tests/api/test_ecc.c b/tests/api/test_ecc.c index b3936ec0545..fe3d3d98800 100644 --- a/tests/api/test_ecc.c +++ b/tests/api/test_ecc.c @@ -2121,7 +2121,12 @@ int test_wc_ecc_ctx_new(void) /* Test bad args. */ /* wc_ecc_ctx_new_ex() will free if returned NULL. */ ExpectNull(cli = wc_ecc_ctx_new(0, &rng)); + ExpectNull(cli = wc_ecc_ctx_new(100, &rng)); + ExpectNull(cli = wc_ecc_ctx_new(-3, &rng)); + /* Must not be truncated to a valid (byte) flag value. */ + ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT + 256, &rng)); ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL)); + ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT | REQ_RESP_SERVER, &rng)); DoExpectIntEQ(wc_FreeRng(&rng), 0); wc_ecc_ctx_free(cli); diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 312753f8008..7455a1340c3 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -15710,9 +15710,14 @@ int wc_ecc_ctx_reset(ecEncCtx* ctx, WC_RNG* rng) ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap) { int ret = 0; - ecEncCtx* ctx = (ecEncCtx*)XMALLOC(sizeof(ecEncCtx), heap, - DYNAMIC_TYPE_ECC); + ecEncCtx* ctx; + if (flags != REQ_RESP_SERVER && flags != REQ_RESP_CLIENT) { + WOLFSSL_MSG("Bad flag passed to wc_ecc_ctx_new_ex"); + return NULL; + } + + ctx = (ecEncCtx*)XMALLOC(sizeof(ecEncCtx), heap, DYNAMIC_TYPE_ECC); if (ctx) { ctx->protocol = (byte)flags; ctx->heap = heap;