Skip to content

Commit 11cf7bb

Browse files
committed
ECC: import point, always do some checks when untrusted
Always check for infinity and, when B param available, whether the point is on the curve when point is untrusted. Change TLS code to treat points from peer as untrusted on import.
1 parent 9ca379f commit 11cf7bb

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

src/internal.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32367,8 +32367,15 @@ static int GetEcDiffieHellmanKea(WOLFSSL *ssl,
3236732367
}
3236832368

3236932369
curveId = wc_ecc_get_oid((word32) curveOid, NULL, NULL);
32370+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
32371+
if (wc_ecc_import_x963_ex2(input + args->idx, length,
32372+
ssl->peerEccKey, curveId, 1) != 0)
32373+
#else
32374+
/* FIPS has validation define on. */
3237032375
if (wc_ecc_import_x963_ex(input + args->idx, length,
32371-
ssl->peerEccKey, curveId) != 0) {
32376+
ssl->peerEccKey, curveId) != 0)
32377+
#endif
32378+
{
3237232379
#ifdef WOLFSSL_EXTRA_ALERTS
3237332380
SendAlert(ssl, alert_fatal, illegal_parameter);
3237432381
#endif
@@ -40651,9 +40658,17 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ],
4065140658
if (ret != 0)
4065240659
return ret;
4065340660
}
40661+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
40662+
if (wc_ecc_import_x963_ex2(input + args->idx, args->length,
40663+
ssl->peerEccKey, kea == ecdhe_psk_kea ? ssl->eccTempKey->dp->id
40664+
: private_key->dp->id, 1))
40665+
#else
40666+
/* FIPS has validation define on. */
4065440667
if (wc_ecc_import_x963_ex(input + args->idx, args->length,
40655-
ssl->peerEccKey, kea == ecdhe_psk_kea ? ssl->eccTempKey->dp->id
40656-
: private_key->dp->id)) {
40668+
ssl->peerEccKey, kea == ecdhe_psk_kea ? ssl->eccTempKey->dp->id
40669+
: private_key->dp->id))
40670+
#endif
40671+
{
4065740672
#ifdef WOLFSSL_EXTRA_ALERTS
4065840673
SendAlert(ssl, alert_fatal, illegal_parameter);
4065940674
#endif

src/tls.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9399,8 +9399,14 @@ static int TLSX_KeyShare_ProcessEcc_ex(WOLFSSL* ssl,
93999399

94009400
/* Point is validated by import function. */
94019401
if (ret == 0) {
9402-
ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
9403-
ssl->peerEccKey, curveId);
9402+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
9403+
ret = wc_ecc_import_x963_ex2(keyShareEntry->ke,
9404+
keyShareEntry->keLen, ssl->peerEccKey, curveId, 1);
9405+
#else
9406+
/* FIPS has validation define on. */
9407+
ret = wc_ecc_import_x963_ex(keyShareEntry->ke,
9408+
keyShareEntry->keLen, ssl->peerEccKey, curveId);
9409+
#endif
94049410
if (ret != 0) {
94059411
ret = ECC_PEERKEY_ERROR;
94069412
WOLFSSL_ERROR_VERBOSE(ret);

wolfcrypt/src/ecc.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10636,8 +10636,8 @@ int wc_ecc_check_key(ecc_key* key)
1063610636

1063710637
#ifdef HAVE_ECC_KEY_IMPORT
1063810638
/* import public ECC key in ANSI X9.63 format */
10639-
int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
10640-
int curve_id)
10639+
int wc_ecc_import_x963_ex2(const byte* in, word32 inLen, ecc_key* key,
10640+
int curve_id, int untrusted)
1064110641
{
1064210642
int err = MP_OKAY;
1064310643
#ifdef HAVE_COMP_KEY
@@ -10922,6 +10922,25 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
1092210922
if (err == MP_OKAY)
1092310923
err = wc_ecc_check_key(key);
1092410924
#endif
10925+
#if (!defined(WOLFSSL_VALIDATE_ECC_IMPORT) || \
10926+
!defined(HAVE_ECC_CHECK_PUBKEY_ORDER)) && \
10927+
!defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
10928+
!defined(WOLFSSL_CRYPTOCELL) && \
10929+
(!defined(WOLF_CRYPTO_CB_ONLY_ECC) || defined(WOLFSSL_QNX_CAAM) || \
10930+
defined(WOLFSSL_IMXRT1170_CAAM))
10931+
if (untrusted) {
10932+
/* Only do quick checks. */
10933+
if ((err == MP_OKAY) && wc_ecc_point_is_at_infinity(&key->pubkey)) {
10934+
err = ECC_INF_E;
10935+
}
10936+
#ifdef USE_ECC_B_PARAM
10937+
if ((err == MP_OKAY) && (key->idx != ECC_CUSTOM_IDX)) {
10938+
err = wc_ecc_point_is_on_curve(&key->pubkey, key->idx);
10939+
}
10940+
#endif /* USE_ECC_B_PARAM */
10941+
}
10942+
#endif
10943+
(void)untrusted;
1092510944

1092610945
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
1092710946
if (err == MP_OKAY) {
@@ -10941,6 +10960,13 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
1094110960
return err;
1094210961
}
1094310962

10963+
/* import public ECC key in ANSI X9.63 format */
10964+
int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
10965+
int curve_id)
10966+
{
10967+
return wc_ecc_import_x963_ex2(in, inLen, key, curve_id, 0);
10968+
}
10969+
1094410970
WOLFSSL_ABI
1094510971
int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key)
1094610972
{

wolfssl/wolfcrypt/ecc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
866866
WOLFSSL_API
867867
int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
868868
int curve_id);
869+
int wc_ecc_import_x963_ex2(const byte* in, word32 inLen, ecc_key* key,
870+
int curve_id, int untrusted);
869871
WOLFSSL_ABI WOLFSSL_API
870872
int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
871873
word32 pubSz, ecc_key* key);

0 commit comments

Comments
 (0)