Skip to contentSkip to conversation

unit: add to test-dpp to expose ASN1 point conversion bug

PATCH In review James Prestwood 2 participants 5 messages Oct 10, 2023 – Oct 11, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 unit: add to test-dpp to expose ASN1 point conversion bug

Add a test to show the incorrect ASN1 conversion to and from points. This was due to the check if Y is odd/even being inverted which incorrectly prefixes the X coordinate with the wrong byte.

The test itself was not fully correct because it was using compliant points rather than full points, and the spec contains the entire Y coordinate so the full point should be used.

This patch also adds ASN1 conversions to validate that dpp_point_from_asn1 and dpp_point_to_asn1 work properly.

---
 unit/test-dpp.c | 44 +++++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 13 deletions(-)
unit/test-dpp.c +31 -13
index 96175759..c782efba 100644
@@ -162,11 +162,14 @@ static void test_bad_channels(const void *data)
  * B.2 Test Vectors for DPP Authentication Using P-256 for
  * Responder-only Authentication
  */
-const char *i_proto_public_bytes = "50a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61a";
-const char *r_boot_public_bytes = "09c585a91b4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f";
+const char *i_proto_public_bytes = "50a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61a"
+				"e14f35a5a858bccad90d126c46594c49ef82655e78888e15a32d916ac2172491";
+const char *r_boot_public_bytes = "09c585a91b4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f"
+				"52bf05968198a2f92883e96a386d767579883302dbf292105c90a43694c2fd5c";
 const char *r_boot_private_bytes = "54ce181a98525f217216f59b245f60e9df30ac7f6b26c939418cfc3c42d1afa0";
 const char *r_proto_private_bytes = "f798ed2e19286f6a6efe210b1863badb99af2a14b497634dbfd2a97394fb5aa5";
-const char *r_proto_public_bytes = "5e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a90";
+const char *r_proto_public_bytes = "5e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a90"
+				"2d4b030669fb9eff8b0a79fa7c1a172ac2a92c626256963f9274dc90682c81e5";
 const char *k1_bytes = "3d832a02ed6d7fc1dc96d2eceab738cf01c0028eb256be33d5a21a720bfcf949";
 const char *k2_bytes = "ca08bdeeef838ddf897a5f01f20bb93dc5a895cb86788ca8c00a7664899bc310";
 const char *ke_bytes = "c8882a8ab30c878467822534138c704ede0ab1e873fe03b601a7908463fec87a";
@@ -176,6 +179,8 @@ const char *i_nonce_bytes = "13f4602a16daeb69712263b9c46cba31";
 const char *r_nonce_bytes = "3d0cfb011ca916d796f7029ff0b43393";
 const char *i_auth_bytes = "787d1189b526448d2901e7f6c22775ce514fce52fc886c1e924f2fbb8d97b210";
 const char *r_auth_bytes = "43509ef7137d8c2fbe66d802ae09dedd94d41b8cbfafb4954782014ff4a3f91c";
+const char *r_asn1 = "3039301306072a8648ce3d020106082a8648ce3d0301070322000209c585a91b"
+			"4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f";
 
 #define HEX2BUF(s, buf, _len) { \
 	unsigned char *_tmp = l_util_from_hexstring(s, NULL); \
@@ -192,7 +197,7 @@ const char *r_auth_bytes = "43509ef7137d8c2fbe66d802ae09dedd94d41b8cbfafb4954782
 
 static void test_key_derivation(const void *data)
 {
-	uint64_t tmp[L_ECC_MAX_DIGITS];
+	uint64_t tmp[L_ECC_MAX_DIGITS * 2];
 	const struct l_ecc_curve *curve = l_ecc_curve_from_ike_group(19);
 	_auto_(l_ecc_point_free) struct l_ecc_point *i_proto_public = NULL;
 	_auto_(l_ecc_point_free) struct l_ecc_point *r_boot_public = NULL;
@@ -201,6 +206,7 @@ static void test_key_derivation(const void *data)
 	_auto_(l_ecc_point_free) struct l_ecc_point *r_proto_public = NULL;
 	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *m = NULL;
 	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *n = NULL;
+	_auto_(l_ecc_point_free) struct l_ecc_point *from_asn1 = NULL;
 	uint64_t k1[L_ECC_MAX_DIGITS];
 	uint64_t k2[L_ECC_MAX_DIGITS];
 	uint64_t ke[L_ECC_MAX_DIGITS];
@@ -208,23 +214,35 @@ static void test_key_derivation(const void *data)
 	uint8_t r_nonce[16];
 	uint64_t r_auth[L_ECC_MAX_DIGITS];
 	uint64_t i_auth[L_ECC_MAX_DIGITS];
+	_auto_(l_free) uint8_t *asn1 = NULL;
+	size_t asn1_len;
 
-	HEX2BUF(i_proto_public_bytes, tmp, 32);
+	HEX2BUF(i_proto_public_bytes, tmp, 64);
 	i_proto_public = l_ecc_point_from_data(curve,
-						L_ECC_POINT_TYPE_COMPLIANT,
-						tmp, 32);
+						L_ECC_POINT_TYPE_FULL,
+						tmp, 64);
 	assert(i_proto_public);
 
-	HEX2BUF(r_boot_public_bytes, tmp, 32);
+	HEX2BUF(r_boot_public_bytes, tmp, 64);
 	r_boot_public = l_ecc_point_from_data(curve,
-						L_ECC_POINT_TYPE_COMPLIANT,
-						tmp, 32);
+						L_ECC_POINT_TYPE_FULL,
+						tmp, 64);
 	assert(r_boot_public);
 
-	HEX2BUF(r_proto_public_bytes, tmp, 32);
+	HEX2BUF(r_asn1, tmp, sizeof(tmp));
+	asn1 = dpp_point_to_asn1(r_boot_public, &asn1_len);
+
+	from_asn1 = dpp_point_from_asn1(asn1, asn1_len);
+
+	assert(l_ecc_points_are_equal(from_asn1, r_boot_public));
+
+	assert(asn1_len == 59);
+	assert(memcmp(tmp, asn1, asn1_len) == 0);
+
+	HEX2BUF(r_proto_public_bytes, tmp, 64);
 	r_proto_public = l_ecc_point_from_data(curve,
-						L_ECC_POINT_TYPE_COMPLIANT,
-						tmp, 32);
+						L_ECC_POINT_TYPE_FULL,
+						tmp, 64);
 	assert(r_proto_public);
 
 	HEX2BUF(r_boot_private_bytes, tmp, 32);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/4 dpp-util: fix incorrect ASN1 compressed public key encoding

The prefix to the X coordinate was incorrect when using compressed points. This has been modified to match the ANSI X9.62 spec.

---
 src/dpp-util.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/dpp-util.c +3 -3
index a2640076..0406a4dc 100644
@@ -767,11 +767,11 @@ uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out)
 
 	/*
 	 * Set the type to whatever avoids doing p - y when reading in the
-	 * key. Working backwards from l_ecc_point_from_data if Y is odd and
-	 * the type is BIT0 there is no subtraction. Similarly if Y is even
+	 * key. Working backwards from l_ecc_point_from_data if Y is even and
+	 * the type is BIT0 there is no subtraction. Similarly if Y is odd
 	 * and the type is BIT1.
 	 */
-	if (l_ecc_point_y_isodd(p))
+	if (!l_ecc_point_y_isodd(p))
 		point_type = L_ECC_POINT_TYPE_COMPRESSED_BIT0;
 	else
 		point_type = L_ECC_POINT_TYPE_COMPRESSED_BIT1;
Signature · 1 line
2.25.1
#2 lore raw

PATCH 3/4 eap-pwd: fix usage of compressed points (after ELL is fixed)

In reply to James Prestwood

EAP-PWD was incorrectly computing the PWE but due to the also incorrect logic in ELL the point converted correctly. This is being fixed, so both places need the reverse logic.

Also added a big comment explaining why this is, and how l_ecc_point_from_data behaves since its somewhat confusing since EAP-PWD expects the pwd-seed to be compared to the actual Y coordinate (which is handled automatically by ELL).

---
 src/eap-pwd.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
src/eap-pwd.c +21 -1
index cd6684e7..5aa51668 100644
@@ -320,7 +320,27 @@ static void eap_pwd_handle_id(struct eap_state *eap,
 				strlen("EAP-pwd Hunting And Pecking"),
 				pwd_value, nbytes);
 
-		if (!(pwd_seed[31] & 1))
+		/*
+		 * The RFC requires the point be solved unambiguously (since
+		 * solving for Y results in two solutions). The correct Y value
+		 * is chosen based on the LSB of the pwd-seed:
+		 *
+		 *     if (LSB(y) == LSB(pwd-seed))
+		 *     then
+		 *         PWE = (x, y)
+		 *     else
+		 *         PWE = (x, p-y)
+		 *
+		 * The ELL API (somewhat hidden from view here) automatically
+		 * performs a subtraction (P - Y) when:
+		 *     - Y is even and BIT1
+		 *     - Y is odd and BIT0
+		 *
+		 * So we choose the point type which matches the parity of
+		 * pwd-seed. This means a subtraction will be performed (P - Y)
+		 * if the parity of pwd-seed and the computed Y do not match.
+		 */
+		if (pwd_seed[31] & 1)
 			pwe = l_ecc_point_from_data(pwd->curve,
 					L_ECC_POINT_TYPE_COMPRESSED_BIT1,
 					pwd_value, nbytes);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 4/4 sae: fix usage of compressed points (after ELL is fixed)

In reply to James Prestwood

SAE was also relying on the ELL bug which was incorrectly performing a subtraction on the Y coordinate based on the compressed point type. Correct this and make the point type more clear (rather than something like "is_odd + 2").

---
 src/sae.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
src/sae.c +23 -1
index cfd4d650..336954b4 100644
@@ -511,7 +511,29 @@ static struct l_ecc_point *sae_compute_pwe(const struct l_ecc_curve *curve,
 		return NULL;
 	}
 
-	pwe = l_ecc_point_from_data(curve, !is_odd + 2, x, bytes);
+	/*
+	 * The 802.11 spec requires the point be solved unambiguously (since
+	 * solving for Y results in two solutions). The correct Y value
+	 * is chosen based on the LSB of the pwd-seed:
+	 *
+	 *     if (LSB(y) == LSB(pwd-seed))
+	 *     then
+	 *         PWE = (x, y)
+	 *     else
+	 *         PWE = (x, p-y)
+	 *
+	 * The ELL API (somewhat hidden from view here) automatically
+	 * performs a subtraction (P - Y) when:
+	 *     - Y is even and BIT1
+	 *     - Y is odd and BIT0
+	 *
+	 * So we choose the point type which matches the parity of
+	 * pwd-seed. This means a subtraction will be performed (P - Y)
+	 * if the parity of pwd-seed and the computed Y do not match.
+	 */
+	pwe = l_ecc_point_from_data(curve,
+				is_odd ? L_ECC_POINT_TYPE_COMPRESSED_BIT1 :
+				L_ECC_POINT_TYPE_COMPRESSED_BIT0, x, bytes);
 	if (!pwe)
 		l_error("computing y failed, was x quadratic residue?");
 
Signature · 1 line
2.25.1
#4 lore raw
In reply to James Prestwood

Hi James,

Quoted 8 lines · James Prestwood

Add a test to show the incorrect ASN1 conversion to and from points. This was due to the check if Y is odd/even being inverted which incorrectly prefixes the X coordinate with the wrong byte.

The test itself was not fully correct because it was using compliant points rather than full points, and the spec contains the entire Y coordinate so the full point should be used.

Quoted 6 lines

This patch also adds ASN1 conversions to validate that dpp_point_from_asn1 and dpp_point_to_asn1 work properly.

---
  unit/test-dpp.c | 44 +++++++++++++++++++++++++++++++-------------
  1 file changed, 31 insertions(+), 13 deletions(-)

All applied, thanks.

Regards,
-Denis