Fix for possible leaks with wc_ecc_sign_set_k when building with WOLFSSL_CUSTOM_CURVES enabled. ZD11416.

This commit is contained in:
David Garske
2020-12-21 11:11:47 -08:00
parent 1c0a6b92ad
commit b4111e2f65

View File

@ -5576,31 +5576,31 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
#ifdef WOLFSSL_ECDSA_SET_K #ifdef WOLFSSL_ECDSA_SET_K
int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key) int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key)
{ {
int ret = 0; int ret;
DECLARE_CURVE_SPECS(curve, 1); DECLARE_CURVE_SPECS(curve, 1);
if (k == NULL || klen == 0 || key == NULL) { if (k == NULL || klen == 0 || key == NULL) {
ret = BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
if (ret == 0) { ALLOC_CURVE_SPECS(1);
ALLOC_CURVE_SPECS(1); ret = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
ret = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER); if (ret != 0) {
FREE_CURVE_SPECS();
return ret;
} }
if (ret == 0) { if (key->sign_k == NULL) {
if (key->sign_k == NULL) { key->sign_k = (mp_int*)XMALLOC(sizeof(mp_int), key->heap,
key->sign_k = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_ECC);
DYNAMIC_TYPE_ECC); if (key->sign_k) {
if (key->sign_k == NULL) { ret = mp_init(key->sign_k);
ret = MEMORY_E; }
} else {
ret = MEMORY_E;
} }
} }
if (ret == 0) {
ret = mp_init(key->sign_k);
}
if (ret == 0) { if (ret == 0) {
ret = mp_read_unsigned_bin(key->sign_k, k, klen); ret = mp_read_unsigned_bin(key->sign_k, k, klen);
} }
@ -5608,11 +5608,12 @@ int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key)
ret = MP_VAL; ret = MP_VAL;
} }
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS(); FREE_CURVE_SPECS();
return ret; return ret;
} }
#endif /* WOLFSSL_ECDSA_SET_K */ #endif /* WOLFSSL_ECDSA_SET_K */
#endif /* WOLFSSL_ATECC508A && WOLFSSL_CRYPTOCELL*/ #endif /* WOLFSSL_ATECC508A && WOLFSSL_CRYPTOCELL */
#endif /* !HAVE_ECC_SIGN */ #endif /* !HAVE_ECC_SIGN */