From d10900e124bbd74d28b75cfba3ad6045bc35636d Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Tue, 22 Feb 2022 17:00:23 +1000 Subject: [PATCH] ECC with SP math: OOB write Don't let input points ordinates be greater than modulus in length. --- wolfcrypt/src/ecc.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index de36e8a48..067fc2810 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -3346,6 +3346,11 @@ exit: if (mp_count_bits(k) > mp_count_bits(modulus) + 1) { return ECC_OUT_OF_RANGE_E; } + if (mp_count_bits(G->x) > mp_count_bits(modulus) || + mp_count_bits(G->y) > mp_count_bits(modulus) || + mp_count_bits(G->z) > mp_count_bits(modulus)) { + return IS_POINT_E; + } #ifdef WOLFSSL_HAVE_SP_ECC #ifndef WOLFSSL_SP_NO_256 @@ -3512,6 +3517,11 @@ exit: if (k == NULL || G == NULL || R == NULL || modulus == NULL) { return ECC_BAD_ARG_E; } + if (mp_count_bits(G->x) > mp_count_bits(modulus) || + mp_count_bits(G->y) > mp_count_bits(modulus) || + mp_count_bits(G->z) > mp_count_bits(modulus)) { + return IS_POINT_E; + } (void)a; (void)order; @@ -8639,6 +8649,11 @@ static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a, if (key == NULL) return BAD_FUNC_ARG; + if (mp_count_bits(pubkey->x) > mp_count_bits(prime) || + mp_count_bits(pubkey->y) > mp_count_bits(prime) || + mp_count_bits(pubkey->z) > mp_count_bits(prime)) { + return IS_POINT_E; + } #ifdef WOLFSSL_NO_MALLOC inf = &lcl_inf; @@ -11412,6 +11427,11 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL) { return ECC_BAD_ARG_E; } + if (mp_count_bits(G->x) > mp_count_bits(modulus) || + mp_count_bits(G->y) > mp_count_bits(modulus) || + mp_count_bits(G->z) > mp_count_bits(modulus)) { + return IS_POINT_E; + } #ifndef WOLFSSL_SP_NO_256 if (mp_count_bits(modulus) == 256) { @@ -11563,10 +11583,15 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, (void)rng; - if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL || + if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL || order == NULL) { return ECC_BAD_ARG_E; } + if (mp_count_bits(G->x) > mp_count_bits(modulus) || + mp_count_bits(G->y) > mp_count_bits(modulus) || + mp_count_bits(G->z) > mp_count_bits(modulus)) { + return IS_POINT_E; + } #ifndef WOLFSSL_SP_NO_256 if (mp_count_bits(modulus) == 256) {