From 36754683d66c0472200e323563839c4c8d900da5 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Wed, 15 May 2024 08:51:45 +1000 Subject: [PATCH] ECC: handle zero in wc_ecc_mulmod() Public API needs to handle multiplying by zero as the underlying code doesn't and needn't. --- wolfcrypt/src/ecc.c | 6 ++++++ wolfcrypt/test/test.c | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index a58c239a37..29f7bfcc9d 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -4058,6 +4058,12 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a, int wc_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, mp_int* modulus, int map) { + if ((k != NULL) && (G != NULL) && (mp_iszero(k))) { + mp_zero(G->x); + mp_zero(G->y); + mp_zero(G->z); + return MP_OKAY; + } return wc_ecc_mulmod_ex(k, G, R, a, modulus, map, NULL); } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index e1bde7b1bd..5f22f3b7f9 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -29328,6 +29328,9 @@ static wc_test_ret_t ecc_mulmod_test(ecc_key* key1) ecc_key key2[1]; ecc_key key3[1]; #endif +#ifdef WOLFSSL_PUBLIC_MP + mp_int* priv; +#endif #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) if ((key2 == NULL) || (key3 == NULL)) @@ -29362,6 +29365,22 @@ static wc_test_ret_t ecc_mulmod_test(ecc_key* key1) goto done; } +#ifdef WOLFSSL_PUBLIC_MP + priv = wc_ecc_key_get_priv(key1); + mp_zero(priv); + ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey, + wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3), + 1); + if (ret != 0) { + ret = WC_TEST_RET_ENC_EC(ret); + goto done; + } + if (!wc_ecc_point_is_at_infinity(&key2->pubkey)) { + ret = WC_TEST_RET_ENC_EC(ret); + goto done; + } +#endif + done: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)