Skip to content

Commit 44de734

Browse files
add sanity check on keysize found with ECC point import
1 parent 96661a5 commit 44de734

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9487,6 +9487,13 @@ int wc_ecc_import_point_der_ex(const byte* in, word32 inLen,
94879487
keysize = (int)(inLen>>1);
94889488
#endif
94899489

9490+
/* sanity check that x coordinate is expected size */
9491+
if (err == MP_OKAY) {
9492+
if (keysize != ecc_sets[curve_idx].size) {
9493+
err = ECC_BAD_ARG_E;
9494+
}
9495+
}
9496+
94909497
/* read data */
94919498
if (err == MP_OKAY)
94929499
err = mp_read_unsigned_bin(point->x, in, (word32)keysize);

wolfcrypt/test/test.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35072,6 +35072,32 @@ static wc_test_ret_t ecc_point_test(void)
3507235072

3507335073
#if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
3507435074
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
35075+
/* Test compressed point with missing x coordinate bytes */
35076+
ret = wc_ecc_import_point_der(derComp0, 1, curve_idx, point3);
35077+
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) {
35078+
ret = WC_TEST_RET_ENC_EC(ret);
35079+
goto done;
35080+
}
35081+
35082+
ret = wc_ecc_import_point_der(derComp1, 1, curve_idx, point3);
35083+
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) {
35084+
ret = WC_TEST_RET_ENC_EC(ret);
35085+
goto done;
35086+
}
35087+
35088+
/* Full uncompressed P-256 length (65 bytes) but invalid prefix byte */
35089+
{
35090+
byte invalidType[65];
35091+
XMEMSET(invalidType, 0x42, sizeof(invalidType));
35092+
invalidType[0] = 0x01;
35093+
ret = wc_ecc_import_point_der_ex(invalidType, sizeof(invalidType),
35094+
curve_idx, point3, 0);
35095+
if (ret != WC_NO_ERR_TRACE(ASN_PARSE_E)) {
35096+
ret = WC_TEST_RET_ENC_EC(ret);
35097+
goto done;
35098+
}
35099+
}
35100+
3507535101
ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
3507635102
if (ret != 0)
3507735103
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);

0 commit comments

Comments
 (0)