Skip to content

Commit 49ed1fa

Browse files
authored
Merge pull request #9684 from SparkiDev/ecc_import_pub_check_fix
ECC: import point, always do some checks
2 parents 1b0b4b1 + 565ac4c commit 49ed1fa

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

src/internal.c

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

3244132441
curveId = wc_ecc_get_oid((word32) curveOid, NULL, NULL);
32442+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
32443+
if (wc_ecc_import_x963_ex2(input + args->idx, length,
32444+
ssl->peerEccKey, curveId, 1) != 0)
32445+
#else
32446+
/* FIPS has validation define on. */
3244232447
if (wc_ecc_import_x963_ex(input + args->idx, length,
32443-
ssl->peerEccKey, curveId) != 0) {
32448+
ssl->peerEccKey, curveId) != 0)
32449+
#endif
32450+
{
3244432451
#ifdef WOLFSSL_EXTRA_ALERTS
3244532452
SendAlert(ssl, alert_fatal, illegal_parameter);
3244632453
#endif
@@ -40723,9 +40730,17 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ],
4072340730
if (ret != 0)
4072440731
return ret;
4072540732
}
40733+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
40734+
if (wc_ecc_import_x963_ex2(input + args->idx, args->length,
40735+
ssl->peerEccKey, kea == ecdhe_psk_kea ? ssl->eccTempKey->dp->id
40736+
: private_key->dp->id, 1))
40737+
#else
40738+
/* FIPS has validation define on. */
4072640739
if (wc_ecc_import_x963_ex(input + args->idx, args->length,
40727-
ssl->peerEccKey, kea == ecdhe_psk_kea ? ssl->eccTempKey->dp->id
40728-
: private_key->dp->id)) {
40740+
ssl->peerEccKey, kea == ecdhe_psk_kea ? ssl->eccTempKey->dp->id
40741+
: private_key->dp->id))
40742+
#endif
40743+
{
4072940744
#ifdef WOLFSSL_EXTRA_ALERTS
4073040745
SendAlert(ssl, alert_fatal, illegal_parameter);
4073140746
#endif

src/tls.c

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

95349534
/* Point is validated by import function. */
95359535
if (ret == 0) {
9536-
ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
9537-
ssl->peerEccKey, curveId);
9536+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
9537+
ret = wc_ecc_import_x963_ex2(keyShareEntry->ke,
9538+
keyShareEntry->keLen, ssl->peerEccKey, curveId, 1);
9539+
#else
9540+
/* FIPS has validation define on. */
9541+
ret = wc_ecc_import_x963_ex(keyShareEntry->ke,
9542+
keyShareEntry->keLen, ssl->peerEccKey, curveId);
9543+
#endif
95389544
if (ret != 0) {
95399545
ret = ECC_PEERKEY_ERROR;
95409546
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,9 @@ 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+
WOLFSSL_API
870+
int wc_ecc_import_x963_ex2(const byte* in, word32 inLen, ecc_key* key,
871+
int curve_id, int untrusted);
869872
WOLFSSL_ABI WOLFSSL_API
870873
int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
871874
word32 pubSz, ecc_key* key);

0 commit comments

Comments
 (0)