Skip to content

Commit 3675468

Browse files
committed
ECC: handle zero in wc_ecc_mulmod()
Public API needs to handle multiplying by zero as the underlying code doesn't and needn't.
1 parent 28bd4eb commit 3675468

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,6 +4058,12 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
40584058
int wc_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
40594059
mp_int* modulus, int map)
40604060
{
4061+
if ((k != NULL) && (G != NULL) && (mp_iszero(k))) {
4062+
mp_zero(G->x);
4063+
mp_zero(G->y);
4064+
mp_zero(G->z);
4065+
return MP_OKAY;
4066+
}
40614067
return wc_ecc_mulmod_ex(k, G, R, a, modulus, map, NULL);
40624068
}
40634069

wolfcrypt/test/test.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29328,6 +29328,9 @@ static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
2932829328
ecc_key key2[1];
2932929329
ecc_key key3[1];
2933029330
#endif
29331+
#ifdef WOLFSSL_PUBLIC_MP
29332+
mp_int* priv;
29333+
#endif
2933129334

2933229335
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
2933329336
if ((key2 == NULL) || (key3 == NULL))
@@ -29362,6 +29365,22 @@ static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
2936229365
goto done;
2936329366
}
2936429367

29368+
#ifdef WOLFSSL_PUBLIC_MP
29369+
priv = wc_ecc_key_get_priv(key1);
29370+
mp_zero(priv);
29371+
ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
29372+
wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
29373+
1);
29374+
if (ret != 0) {
29375+
ret = WC_TEST_RET_ENC_EC(ret);
29376+
goto done;
29377+
}
29378+
if (!wc_ecc_point_is_at_infinity(&key2->pubkey)) {
29379+
ret = WC_TEST_RET_ENC_EC(ret);
29380+
goto done;
29381+
}
29382+
#endif
29383+
2936529384
done:
2936629385

2936729386
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)

0 commit comments

Comments
 (0)