Skip to contentSkip to conversation

ecc: Add l_ecc_curve_get_scalar_bytes

PATCH Applied Andrew Zaborowski 2 participants 4 messages Jan 22, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ecc: Add l_ecc_curve_get_scalar_bytes

Add a function to return the number of bytes used by the *_from_data and *_get_data methods.

---
 ell/ecc.c   | 9 ++++++++-
 ell/ecc.h   | 1 +
 ell/ell.sym | 6 ++++++
 3 files changed, 15 insertions(+), 1 deletion(-)
ell/ecc.c +8 -1
index dd2e11a..eeb12dd 100644
@@ -123,6 +123,14 @@ LIB_EXPORT const char *l_ecc_curve_get_name(const struct l_ecc_curve *curve)
 	return curve->name;
 }
 
+LIB_EXPORT size_t l_ecc_curve_get_scalar_bytes(const struct l_ecc_curve *curve)
+{
+	if (unlikely(!curve))
+		return 0;
+
+	return curve->ndigits * 8;
+}
+
 LIB_EXPORT const struct l_ecc_curve *l_ecc_curve_get_ike_group(
 							unsigned int group)
 {
@@ -589,7 +597,6 @@ LIB_EXPORT struct l_ecc_scalar *l_ecc_scalar_new_random(
 }
 
 LIB_EXPORT ssize_t l_ecc_scalar_get_data(const struct l_ecc_scalar *c,
-
 						void *buf, size_t len)
 {
 	if (len < c->curve->ndigits * 8)
ell/ecc.h +1 -0
index e867ef9..26a5889 100644
@@ -44,6 +44,7 @@ enum l_ecc_point_type {
 
 const struct l_ecc_curve *l_ecc_curve_get(const char *name);
 const char *l_ecc_curve_get_name(const struct l_ecc_curve *curve);
+size_t l_ecc_curve_get_scalar_bytes(const struct l_ecc_curve *curve);
 const unsigned int *l_ecc_curve_get_supported_ike_groups(void);
 const unsigned int *l_ecc_curve_get_supported_tls_groups(void);
 const struct l_ecc_curve *l_ecc_curve_get_ike_group(unsigned int group);
ell/ell.sym +6 -0
index 7643a25..828278b 100644
@@ -460,7 +460,13 @@ global:
 	l_certchain_verify;
 	/* ecc */
 	l_ecc_curve_get;
+	l_ecc_curve_get_name;
+	l_ecc_curve_get_scalar_bytes;
 	l_ecc_curve_get_supported_groups;
+	l_ecc_curve_get_supported_ike_groups;
+	l_ecc_curve_get_supported_tls_groups;
+	l_ecc_curve_get_ike_group;
+	l_ecc_curve_get_tls_group;
 	l_ecc_curve_get_order;
 	l_ecc_curve_get_prime;
 	l_ecc_point_add;
Signature · 1 line
2.19.1
#1 lore raw

PATCH 2/2 tls: Drop unneeded ecc curve data

Drop the information on ECC curves that we can get from the l_ecc APIs except for the name and the curve ID. We keep the IDs in the tls_group_pref array so we can set the curve preference independently of the list of ecc.c supported curves and don't have to generate the array dynamically.

---
 ell/tls-extensions.c | 10 ++--------
 ell/tls-private.h    |  4 ----
 ell/tls-suites.c     | 37 +++++++++++++++++++++++--------------
 3 files changed, 25 insertions(+), 26 deletions(-)
ell/tls-extensions.c +2 -8
index 6769948..d179e67 100644
@@ -325,14 +325,8 @@ static const uint8_t tls_dh14_prime[] = {
 };
 
 static const struct tls_named_group tls_group_pref[] = {
-	{
-		"secp256r1", 23, TLS_GROUP_TYPE_EC,
-		.ec = { .l_group = 19, .point_bytes = 64 },
-	},
-	{
-		"secp384r1", 24, TLS_GROUP_TYPE_EC,
-		.ec = { .l_group = 20, .point_bytes = 96 },
-	},
+	{ "secp256r1", 23, TLS_GROUP_TYPE_EC },
+	{ "secp384r1", 24, TLS_GROUP_TYPE_EC },
 	{
 		"ffdhe2048", 256, TLS_GROUP_TYPE_FF,
 		.ff = {
ell/tls-private.h +0 -4
index b8c436a..03ad07c 100644
@@ -155,10 +155,6 @@ struct tls_named_group {
 		TLS_GROUP_TYPE_FF,
 	} type;
 	union {
-		struct {
-			unsigned int l_group;
-			size_t point_bytes;
-		} ec;
 		struct {
 			const uint8_t *prime;
 			size_t prime_len;
ell/tls-suites.c +23 -14
index d6b4caa..c11c28d 100644
@@ -396,24 +396,27 @@ static void tls_free_ecdhe_params(struct l_tls *tls)
 	l_free(params);
 }
 
-static size_t tls_write_ecpoint(uint8_t *buf,
+static size_t tls_write_ecpoint(uint8_t *buf, size_t len,
 				const struct tls_named_group *curve,
 				const struct l_ecc_point *point)
 {
+	size_t point_bytes;
+
 	/* RFC 8422, Section 5.4.1 */
-	buf[0] = 1 + curve->ec.point_bytes;	/* length */
+	point_bytes = l_ecc_point_get_data(point, buf + 2, len - 2);
+	buf[0] = 1 + point_bytes;		/* length */
 	buf[1] = 4;				/* form: uncompressed */
-	return 2 + l_ecc_point_get_data(point, buf + 2, curve->ec.point_bytes);
+	return 2 + point_bytes;
 }
 
-static size_t tls_write_server_ecdh_params(struct l_tls *tls, uint8_t *buf)
+static size_t tls_write_server_ecdh_params(struct l_tls *tls, uint8_t *buf, size_t len)
 {
 	struct tls_ecdhe_params *params = tls->pending.key_xchg_params;
 
 	/* RFC 8422, Section 5.4 */
 	buf[0] = 3;				/* curve_type: named_curve */
 	l_put_be16(tls->negotiated_curve->id, buf + 1);
-	return 3 + tls_write_ecpoint(buf + 3, tls->negotiated_curve,
+	return 3 + tls_write_ecpoint(buf + 3, len - 3, tls->negotiated_curve,
 					params->public);
 }
 
@@ -445,7 +448,7 @@ static bool tls_send_ecdhe_server_key_xchg(struct l_tls *tls)
 	}
 
 	server_ecdh_params_ptr = ptr;
-	ptr += tls_write_server_ecdh_params(tls, ptr);
+	ptr += tls_write_server_ecdh_params(tls, ptr, buf + sizeof(buf) - ptr);
 
 	if (tls->pending.cipher_suite->signature) {
 		sign_len = tls->pending.cipher_suite->signature->sign(tls, ptr,
@@ -470,6 +473,7 @@ static void tls_handle_ecdhe_server_key_xchg(struct l_tls *tls,
 	struct tls_ecdhe_params *params;
 	uint16_t namedcurve;
 	const uint8_t *server_ecdh_params_ptr = buf;
+	size_t point_bytes;
 
 	/* RFC 8422, Section 5.4 */
 
@@ -498,9 +502,11 @@ static void tls_handle_ecdhe_server_key_xchg(struct l_tls *tls,
 
 	TLS_DEBUG("Negotiated %s", tls->negotiated_curve->name);
 
-	if (*buf++ != 1 + tls->negotiated_curve->ec.point_bytes)
+	if (*buf < 1)
 		goto decode_error;
 
+	point_bytes = *buf++ - 1;
+
 	if (*buf != 4) {	/* uncompressed */
 		TLS_DISCONNECT(TLS_ALERT_ILLEGAL_PARAM, 0,
 				"Unsupported (deprecated?) PointConversionForm "
@@ -511,7 +517,7 @@ static void tls_handle_ecdhe_server_key_xchg(struct l_tls *tls,
 	buf++;
 	len -= 2;
 
-	if (len < tls->negotiated_curve->ec.point_bytes)
+	if (len < point_bytes)
 		goto decode_error;
 
 	/*
@@ -527,10 +533,11 @@ static void tls_handle_ecdhe_server_key_xchg(struct l_tls *tls,
 						L_ECC_POINT_TYPE_FULL,
 						buf, len);
 	tls->pending.key_xchg_params = params;
-	buf += tls->negotiated_curve->ec.point_bytes;
-	len -= tls->negotiated_curve->ec.point_bytes;
+	buf += point_bytes;
+	len -= point_bytes;
 
-	if (!params->public) {
+	if (!params->public || point_bytes !=
+			2 * l_ecc_curve_get_scalar_bytes(params->curve)) {
 		TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
 				"ServerKeyExchange.params.public decode error");
 		return;
@@ -575,7 +582,8 @@ static bool tls_send_ecdhe_client_key_xchg(struct l_tls *tls)
 		return false;
 	}
 
-	ptr += tls_write_ecpoint(ptr, tls->negotiated_curve, our_public);
+	ptr += tls_write_ecpoint(ptr, buf + sizeof(buf) - ptr,
+					tls->negotiated_curve, our_public);
 	l_ecc_point_free(our_public);
 
 	/*
@@ -624,13 +632,14 @@ static void tls_handle_ecdhe_client_key_xchg(struct l_tls *tls,
 	ssize_t pre_master_secret_len;
 	struct l_ecc_point *other_public;
 	struct l_ecc_scalar *secret;
+	size_t point_bytes = 2 * l_ecc_curve_get_scalar_bytes(params->curve);
 
 	/* RFC 8422, Section 5.7 */
 
 	if (len < 2)
 		goto decode_error;
 
-	if (*buf++ != 1 + tls->negotiated_curve->ec.point_bytes)
+	if (*buf++ != 1 + point_bytes)
 		goto decode_error;
 
 	if (*buf != 4) {	/* uncompressed */
@@ -643,7 +652,7 @@ static void tls_handle_ecdhe_client_key_xchg(struct l_tls *tls,
 	buf++;
 	len -= 2;
 
-	if (len != tls->negotiated_curve->ec.point_bytes)
+	if (len != point_bytes)
 		goto decode_error;
 
 	/*
Signature · 1 line
2.19.1
#3 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 8 lines · Andrew Zaborowski

Add a function to return the number of bytes used by the *_from_data and *_get_data methods.

---
  ell/ecc.c   | 9 ++++++++-
  ell/ecc.h   | 1 +
  ell/ell.sym | 6 ++++++
  3 files changed, 15 insertions(+), 1 deletion(-)

I split this up into two commits (separated out the ell.sym cleanups) and applied, thanks.

Regards,
-Denis

#2 lore raw

Re: [PATCH 2/2] tls: Drop unneeded ecc curve data

In reply to Andrew Zaborowski

Hi Andrew,

Quoted 11 lines · Andrew Zaborowski

Drop the information on ECC curves that we can get from the l_ecc APIs except for the name and the curve ID. We keep the IDs in the tls_group_pref array so we can set the curve preference independently of the list of ecc.c supported curves and don't have to generate the array dynamically.

---
  ell/tls-extensions.c | 10 ++--------
  ell/tls-private.h    |  4 ----
  ell/tls-suites.c     | 37 +++++++++++++++++++++++--------------
  3 files changed, 25 insertions(+), 26 deletions(-)

Applied, thanks.

Regards,
-Denis