Skip to contentSkip to conversation

ecc: moved ecc from IWD into ell

PATCH Superseded James Prestwood 2 participants 13 messages Nov 29, 2018 – Dec 3, 2018 Kbuild

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/9 ecc: moved ecc from IWD into ell

IWD had its own implementation for ECC and math operations. Eventually TLS will indirectly require this (via ECDH) so it made sense to move everything into ell. Some changes had to be made to the original file to accomidate the ell style.

The original file (ecc.c) required some changes. The ecc_point type is now exposed as l_ecc_point. Several vli_ functions were prefixed with "_" so they could be exposed privately. All ecc functions that needed to be exposed as library API's were prefixed with l_ecc, and changed to take a l_ecc_curve object, explained below.

A new type was also added, l_ecc_curve, which holds all the information for a given curve. Users will call l_curve_get(int group) to get a supported curve object. This curve object can then be passed to all l_ecc API's which avoids the need for the user to know any of the curve parameters (prime, g, n, b, etc.) or vli lengths. Some of these parameters may still need to be obtained, so several getters were added as well.

When using any of the l_ecc API's, any vli integers should be created as a uint64_t array of size L_ECC_MAX_DIGITS. This ensures that the integer will support all supported curves. L_ECC_MAX_DIGITS should also be updated if any larger bit size curves are added.

---
 Makefile.am       |    8 +-
 ell/ecc-private.h |   50 ++
 ell/ecc.c         | 1288 +++++++++++++++++++++++++++++++++++++++++++++
 ell/ecc.h         |   84 +++
 ell/ell.h         |    1 +
 5 files changed, 1429 insertions(+), 2 deletions(-)
 create mode 100644 ell/ecc-private.h
 create mode 100644 ell/ecc.c
 create mode 100644 ell/ecc.h
Makefile.am +6 -2
index d115360..4e5f01b 100644
@@ -49,7 +49,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/dir.h \
 			ell/net.h \
 			ell/dhcp.h \
-			ell/cert.h
+			ell/cert.h \
+			ell/ecc.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -112,7 +113,10 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/dhcp-transport.c \
 			ell/dhcp-lease.c \
 			ell/cert.c \
-			ell/cert-private.h
+			ell/cert-private.h \
+			ell/ecc-private.h \
+			ell/ecc.h \
+			ell/ecc.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/ecc-private.h new file +50 -0

50 lines not on this page · raw message

ell/ecc.c new file +1288 -0

1,288 lines not on this page · raw message

ell/ecc.h new file +84 -0

84 lines not on this page · raw message

ell/ell.h +1 -0
index 4b0eac4..7bc26bc 100644
@@ -57,3 +57,4 @@
 #include <ell/dbus-client.h>
 #include <ell/dhcp.h>
 #include <ell/cert.h>
+#include <ell/ecc.h>
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/9 unit: moved ECC unit tests from IWD

The test was just modified to use the new l_ecc API's

---
 Makefile.am     |   5 +-
 unit/test-ecc.c | 345 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 349 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-ecc.c
Makefile.am +4 -1
index 4e5f01b..f33f992 100644
@@ -161,7 +161,8 @@ unit_tests = unit/test-unit \
 			unit/test-uuid \
 			unit/test-pbkdf2 \
 			unit/test-dhcp \
-			unit/test-dir-watch
+			unit/test-dir-watch \
+			unit/test-ecc
 
 dbus_tests = unit/test-hwdb \
 			unit/test-dbus \
@@ -280,6 +281,8 @@ unit_test_dhcp_LDADD = ell/libell-private.la
 
 unit_test_dir_watch_LDADD = ell/libell-private.la
 
+unit_test_ecc_LDADD = ell/libell-private.la
+
 if MAINTAINER_MODE
 noinst_LTLIBRARIES += unit/example-plugin.la
 endif
unit/test-ecc.c new file +345 -0

345 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

PATCH 3/9 ecdh: moved ECDH from IWD

In reply to James Prestwood

Moved the ECDH implementation from IWD. Minor API changes were made, e.g. to take an l_ecc_curve object. Some sanity checks were also added to ensure random keys were within the allowable range.

---
 Makefile.am |   7 +-
 ell/ecdh.c  | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/ecdh.h  |  51 ++++++++++++++
 ell/ell.h   |   1 +
 4 files changed, 257 insertions(+), 2 deletions(-)
 create mode 100644 ell/ecdh.c
 create mode 100644 ell/ecdh.h
Makefile.am +5 -2
index f33f992..69c3cea 100644
@@ -50,7 +50,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/net.h \
 			ell/dhcp.h \
 			ell/cert.h \
-			ell/ecc.h
+			ell/ecc.h \
+			ell/ecdh.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -116,7 +117,9 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/cert-private.h \
 			ell/ecc-private.h \
 			ell/ecc.h \
-			ell/ecc.c
+			ell/ecc.c \
+			ell/ecdh.h \
+			ell/ecdh.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/ecdh.c new file +200 -0

200 lines not on this page · raw message

ell/ecdh.h new file +51 -0

51 lines not on this page · raw message

ell/ell.h +1 -0
index 7bc26bc..aab6417 100644
@@ -58,3 +58,4 @@
 #include <ell/dhcp.h>
 #include <ell/cert.h>
 #include <ell/ecc.h>
+#include <ell/ecdh.h>
Signature · 1 line
2.17.1
#6 lore raw

PATCH 4/9 unit: added ECDH unit tests

In reply to James Prestwood

This test was moved from IWD, with required changes to the API

---
 Makefile.am      |   6 +-
 unit/test-ecdh.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-ecdh.c
Makefile.am +5 -1
index 69c3cea..dc16a2c 100644
@@ -165,7 +165,8 @@ unit_tests = unit/test-unit \
 			unit/test-pbkdf2 \
 			unit/test-dhcp \
 			unit/test-dir-watch \
-			unit/test-ecc
+			unit/test-ecc \
+			unit/test-ecdh
 
 dbus_tests = unit/test-hwdb \
 			unit/test-dbus \
@@ -286,6 +287,9 @@ unit_test_dir_watch_LDADD = ell/libell-private.la
 
 unit_test_ecc_LDADD = ell/libell-private.la
 
+unit_test_ecdh_LDADD = ell/libell-private.la
+unit_test_ecdh_LDFLAGS = -Wl,-wrap,l_getrandom
+
 if MAINTAINER_MODE
 noinst_LTLIBRARIES += unit/example-plugin.la
 endif
unit/test-ecdh.c new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.17.1
#7 lore raw

PATCH 5/9 ecc: implement P384 fast modulus

In reply to James Prestwood

This algorithm is defined in an IEICE paper:

"Simple Power Analysis on Fast Modular Reduction with Generalized Mersenne
Prime for Elliptic Curve Cryptosystems"

Unlike the existing P192/P256 algorithms, a macro was written to deal with converting from 32 bit chunks into 64 bits. This also makes the algorithm much easier to read since the C values are used directly.

---
 ell/ecc.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
ell/ecc.c +129 -0
index b3ddf30..5d5e948 100644
@@ -455,6 +455,132 @@ static void vli_mmod_fast_256(uint64_t *result, const uint64_t *product,
 	}
 }
 
+/*
+ * The NIST algorithms define S values, which are comprised of 32 bit C values
+ * of the original product we are trying to reduce. Since we are working with
+ * 64 bit 'digits', we need to convert these C values into 64 bit chunks. This
+ * macro mainly makes code readability easier since we can directly pass the
+ * two C indexes (h and l). Some of these C values are zero, which is also a
+ * value C index. In this case -1 should be passed to indicate zero.
+ */
+#define ECC_SET_S(prod, h, l) ({ 				\
+	uint64_t r = 0;						\
+	if (h == -1) {						\
+		/* zero, don't do anything */			\
+	} else if (h & 1)					\
+		r |= (prod[h / 2] & 0xffffffff00000000ull);	\
+	else							\
+		r |= (prod[h / 2] << 32);			\
+	if (l == -1) {						\
+		/* zero, don't do anything */			\
+	} else if (l & 1)					\
+		r |= (prod[l / 2] >> 32);			\
+	else							\
+		r |= (prod[l / 2] & 0xffffffff);		\
+	r;							\
+})
+
+static void vli_mmod_fast_384(uint64_t *result, const uint64_t *product,
+				const uint64_t *curve_prime, uint64_t *tmp)
+{
+	int carry;
+	const unsigned int ndigits = 6;
+
+	/* t */
+	vli_set(result, product, ndigits);
+
+	/* s1 */
+	tmp[0] = 0;
+	tmp[1] = 0;
+	tmp[2] = ECC_SET_S(product, 22, 21);
+	tmp[3] = ECC_SET_S(product, -1, 23);
+	tmp[4] = 0;
+	tmp[5] = 0;
+	carry = vli_lshift(tmp, tmp, 1, ndigits);
+	carry += vli_add(result, result, tmp, ndigits);
+
+	/* s2 */
+	tmp[0] = product[6];
+	tmp[1] = product[7];
+	tmp[2] = product[8];
+	tmp[3] = product[9];
+	tmp[4] = product[10];
+	tmp[5] = product[11];
+	carry += vli_add(result, result, tmp, ndigits);
+
+	/* s3 */
+	tmp[0] = ECC_SET_S(product, 22, 21);
+	tmp[1] = ECC_SET_S(product, 12, 23);
+	tmp[2] = ECC_SET_S(product, 14, 13);
+	tmp[3] = ECC_SET_S(product, 16, 15);
+	tmp[4] = ECC_SET_S(product, 18, 17);
+	tmp[5] = ECC_SET_S(product, 20, 19);
+	carry += vli_add(result, result, tmp, ndigits);
+
+	/* s4 */
+	tmp[0] = ECC_SET_S(product, 23, -1);
+	tmp[1] = ECC_SET_S(product, 20, -1);
+	tmp[2] = ECC_SET_S(product, 13, 12);
+	tmp[3] = ECC_SET_S(product, 15, 14);
+	tmp[4] = ECC_SET_S(product, 17, 16);
+	tmp[5] = ECC_SET_S(product, 19, 18);
+	carry += vli_add(result, result, tmp, ndigits);
+
+	/* s5 */
+	tmp[0] = 0;
+	tmp[1] = 0;
+	tmp[2] = ECC_SET_S(product, 21, 20);
+	tmp[3] = ECC_SET_S(product, 23, 22);
+	tmp[4] = 0;
+	tmp[5] = 0;
+	carry += vli_add(result, result, tmp, ndigits);
+
+	/* s6 */
+	tmp[0] = ECC_SET_S(product, -1, 20);
+	tmp[1] = ECC_SET_S(product, 21, -1);
+	tmp[2] = ECC_SET_S(product, 23, 22);
+	tmp[3] = 0;
+	tmp[4] = 0;
+	tmp[5] = 0;
+	carry += vli_add(result, result, tmp, ndigits);
+
+	/* s7 */
+	tmp[0] = ECC_SET_S(product, 12, 23);
+	tmp[1] = ECC_SET_S(product, 14, 13);
+	tmp[2] = ECC_SET_S(product, 16, 15);
+	tmp[3] = ECC_SET_S(product, 18, 17);
+	tmp[4] = ECC_SET_S(product, 20, 19);
+	tmp[5] = ECC_SET_S(product, 22, 21);
+	carry -= vli_sub(result, result, tmp, ndigits);
+
+	/* s8 */
+	tmp[0] = ECC_SET_S(product, 20, -1);
+	tmp[1] = ECC_SET_S(product, 22, 21);
+	tmp[2] = ECC_SET_S(product, -1, 23);
+	tmp[3] = 0;
+	tmp[4] = 0;
+	tmp[5] = 0;
+	carry -= vli_sub(result, result, tmp, ndigits);
+
+	/* s9 */
+	tmp[0] = 0;
+	tmp[1] = ECC_SET_S(product, 23, -1);
+	tmp[2] = ECC_SET_S(product, -1, 23);
+	tmp[3] = 0;
+	tmp[4] = 0;
+	tmp[5] = 0;
+	carry -= vli_sub(result, result, tmp, ndigits);
+
+	if (carry < 0) {
+		do {
+			carry += vli_add(result, result, curve_prime, ndigits);
+		} while (carry < 0);
+	} else {
+		while (carry || _vli_cmp(curve_prime, result, ndigits) != 1)
+			carry -= vli_sub(result, result, curve_prime, ndigits);
+	}
+}
+
 /* Computes result = product % curve_prime
  *  from http://www.nsa.gov/ia/_files/nist-routines.pdf
 */
@@ -471,6 +597,9 @@ static bool vli_mmod_fast(uint64_t *result, uint64_t *product,
 	case 4:
 		vli_mmod_fast_256(result, product, curve_prime, tmp);
 		break;
+	case 6:
+		vli_mmod_fast_384(result, product, curve_prime, tmp);
+		break;
 	default:
 		return false;
 	}
Signature · 1 line
2.17.1
#8 lore raw

PATCH 6/9 ecc: added P384 curve definition

In reply to James Prestwood
---
 ell/ecc.c | 30 ++++++++++++++++++++++++++++++
 ell/ecc.h |  2 +-
 2 files changed, 31 insertions(+), 1 deletion(-)
ell/ecc.c +30 -0
index 5d5e948..03a9610 100644
@@ -1007,8 +1007,38 @@ static struct l_ecc_curve p256 = {
 	.b = P256_CURVE_B,
 };
 
+#define P384_CURVE_P {	0x00000000FFFFFFFFull, 0xFFFFFFFF00000000ull, \
+			0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull, \
+			0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull }
+#define P384_CURVE_GX {	0x3A545E3872760AB7ull, 0x5502F25DBF55296Cull, \
+			0x59F741E082542A38ull, 0x6E1D3B628BA79B98ull, \
+			0x8EB1C71EF320AD74ull, 0xAA87CA22BE8B0537ull }
+#define P384_CURVE_GY {	0x7A431D7C90EA0E5Full, 0x0A60B1CE1D7E819Dull, \
+			0xE9DA3113B5F0B8C0ull, 0xF8F41DBD289A147Cull, \
+			0x5D9E98BF9292DC29ull, 0x3617DE4A96262C6Full }
+#define P384_CURVE_N {	0xECEC196ACCC52973ull, 0x581A0DB248B0A77Aull, \
+			0xC7634D81F4372DDFull, 0xFFFFFFFFFFFFFFFFull, \
+			0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull }
+#define P384_CURVE_B {	0x2A85C8EDD3EC2AEFull, 0xC656398D8A2ED19Dull, \
+			0x0314088F5013875Aull, 0x181D9C6EFE814112ull, \
+			0x988E056BE3F82D19ull, 0xB3312FA7E23EE7E4ull }
+
+static struct l_ecc_curve p384 = {
+	.group = 20,
+	.ndigits = 6,
+	.g = {
+		.x = P384_CURVE_GX,
+		.y = P384_CURVE_GY,
+		.ndigits = 6
+	},
+	.p = P384_CURVE_P,
+	.n = P384_CURVE_N,
+	.b = P384_CURVE_B
+};
+
 static struct l_ecc_curve *curves[] = {
 	&p256,
+	&p384,
 };
 
 LIB_EXPORT struct l_ecc_curve *l_ecc_curve_get(unsigned int group)
ell/ecc.h +1 -1
index 0601780..e1c137f 100644
@@ -27,7 +27,7 @@
 extern "C" {
 #endif
 
-#define L_ECC_MAX_DIGITS 4
+#define L_ECC_MAX_DIGITS 6
 
 struct l_ecc_curve;
 
Signature · 1 line
2.17.1
#9 lore raw

PATCH 7/9 unit: update test-ecdh with more random bytes

In reply to James Prestwood

With the addition of P384, our wrapped l_getrandom needs enough random bytes to fill a P384 integer, 48 bytes.

---
 unit/test-ecdh.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
unit/test-ecdh.c +17 -7
index efc47de..e44c4b6 100644
@@ -35,13 +35,23 @@ bool __real_l_getrandom(void *buf, size_t len);
 
 bool __wrap_l_getrandom(void *buf, size_t len)
 {
-	static const uint8_t random_buf[] = { 0x75, 0xc5, 0xfe, 0x3e, 0x53,
-						0xcc, 0x33, 0x33, 0x64, 0xea,
-						0xdd, 0xa1, 0xe6, 0x62, 0x7a,
-						0xb1, 0x98, 0xa7, 0xa0, 0x1e,
-						0xac, 0x4b, 0x1d, 0xb8, 0x71,
-						0x5b, 0x1d, 0x00, 0x36, 0xd0,
-						0x0f, 0xde };
+	/*
+	 * Note: we at least need enough bytes for the largest curve group,
+	 * currently P384
+	 */
+	static const uint8_t random_buf[] = { 0x0b, 0x34, 0x0f, 0x16, 0x02,
+						0x31, 0x88, 0xe1, 0x0e, 0x08,
+						0xb2, 0x5f, 0x03, 0x4c, 0x3c,
+						0xe8, 0x3d, 0xf4, 0xaa, 0xc5,
+						0x8d, 0x2b, 0x1f, 0x43, 0xb8,
+						0x0e, 0xf3, 0xed, 0x73, 0xd0,
+						0xd7, 0x8d, 0x77, 0x62, 0xaf,
+						0xa7, 0x37, 0xca, 0x20, 0x3f,
+						0xd8, 0xf5, 0x32, 0x64, 0x7e,
+						0xf6, 0x45, 0x46 };
+
+	if (len > sizeof(random_buf))
+		return false;
 
 	if (use_real_getrandom)
 		return __real_l_getrandom(buf, len);
Signature · 1 line
2.17.1
#10 lore raw

PATCH 8/9 unit: added P384 unit tests

In reply to James Prestwood

(also renamed 'test_vectors' to 'test_vector_p256' for consistency)

---
 unit/test-ecdh.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
unit/test-ecdh.c +47 -2
index e44c4b6..10cb47a 100644
@@ -120,7 +120,7 @@ static void test_compliant_key(const void *data)
 /*
  * Test vector from RFC 5114 - 256-bit Random ECP Group
  */
-static void test_vectors(const void *data)
+static void test_vector_p256(const void *data)
 {
 	struct l_ecc_curve *curve = l_ecc_curve_get(19);
 
@@ -161,6 +161,50 @@ static void test_vectors(const void *data)
 	use_real_getrandom = true;
 }
 
+static void test_vector_p384(const void *data)
+{
+	struct l_ecc_curve *curve = l_ecc_curve_get(20);
+
+	uint64_t a_secret[6] = { 0x86F05FEADB9376F1ull, 0xD706A90CBCB5DF29ull,
+				0xD709EE7A7962A156ull, 0x5DFD8A7965571C48ull,
+				0x44DD14E9FD126071ull, 0xD27335EA71664AF2ull };
+	uint64_t a_public[12] = { 0x7D016FE27A8B8C66ull, 0x7E6A8EA9D1FD7742ull,
+				0x0EE6B0403D627954ull, 0xE057AB62F82054D1ull,
+				0xDA4C6D9074417D05ull, 0x793148F1787634D5ull,
+				0xBACED214A1A1D128ull, 0x8F7A685923DE3B67ull,
+				0x6B8F398BB29E4236ull, 0xC947392E94F4C3F0ull,
+				0xF480F4FB4CD40504ull, 0xC6C41294331D23E6ull };
+	uint64_t b_secret[6] = { 0x2C4A6C768BCD94D2ull, 0x9BE52E00C194A413ull,
+				0x1F80231121CCE3D3ull, 0x3B6125262C36A7DFull,
+				0x9C0F00D456C2F702ull, 0x52D1791FDB4B70F8ull };
+	uint64_t b_public[12] = { 0x223F12B5A1ABC120ull, 0x789D72A84865AE2Full,
+				0x4ABC17647B6B9999ull, 0x5B36DB65915359B4ull,
+				0xF74B8D4EFB708B3Dull, 0x5CD42AB9C41B5347ull,
+				0xE035B0EDF36755DEull, 0x40BDE8723415A8ECull,
+				0x0CECA16356CA9332ull, 0x8F6D5B348C0FA4D8ull,
+				0xA3A8BFAC46B404BDull, 0xE171458FEAA939AAull };
+	uint64_t shared_secret[6] = {
+				0xDE159A58028ABC0Eull, 0x27AA8A4540884C37ull,
+				0x59D926EB1B8456E4ull, 0xCAE53160137D904Cull,
+				0x55981B110575E0A8ull, 0x5EA1FC4AF7256D20ull };
+	uint64_t a_shared[6];
+	uint64_t b_shared[6];
+
+	use_real_getrandom = false;
+
+	assert(l_ecdh_generate_shared_secret(curve, a_secret,
+						(const void *) b_public,
+						96, a_shared, 48));
+	assert(l_ecdh_generate_shared_secret(curve, b_secret,
+						(const void *) a_public,
+						96, b_shared, 48));
+
+	assert(!memcmp(a_shared, shared_secret, 48));
+	assert(!memcmp(b_shared, shared_secret, 48));
+
+	use_real_getrandom = true;
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -170,7 +214,8 @@ int main(int argc, char *argv[])
 		l_test_add("ECDH Compliant key", test_compliant_key, NULL);
 	}
 
-	l_test_add("ECDH test vector", test_vectors, NULL);
+	l_test_add("ECDH test vector P256", test_vector_p256, NULL);
+	l_test_add("ECDH test vector P384", test_vector_p384, NULL);
 
 	return l_test_run();
 }
Signature · 1 line
2.17.1
#11 lore raw

PATCH 9/9 gitignore: added test-ecc/test-ecdh

In reply to James Prestwood
---
 .gitignore | 2 ++
 1 file changed, 2 insertions(+)
.gitignore +2 -0
index 16725f5..75ab7b2 100644
@@ -56,6 +56,8 @@ unit/test-key
 unit/test-pbkdf2
 unit/test-dhcp
 unit/test-dir-watch
+unit/test-ecc
+unit/test-ecdh
 unit/cert-*.pem
 unit/cert-*.csr
 unit/cert-*.srl
Signature · 1 line
2.17.1
#12 lore raw
In reply to James Prestwood

Hi James,

Quoted 30 lines · James Prestwood

IWD had its own implementation for ECC and math operations. Eventually TLS will indirectly require this (via ECDH) so it made sense to move everything into ell. Some changes had to be made to the original file to accomidate the ell style.

The original file (ecc.c) required some changes. The ecc_point type is now exposed as l_ecc_point. Several vli_ functions were prefixed with "_" so they could be exposed privately. All ecc functions that needed to be exposed as library API's were prefixed with l_ecc, and changed to take a l_ecc_curve object, explained below.

A new type was also added, l_ecc_curve, which holds all the information for a given curve. Users will call l_curve_get(int group) to get a supported curve object. This curve object can then be passed to all l_ecc API's which avoids the need for the user to know any of the curve parameters (prime, g, n, b, etc.) or vli lengths. Some of these parameters may still need to be obtained, so several getters were added as well.

When using any of the l_ecc API's, any vli integers should be created as a uint64_t array of size L_ECC_MAX_DIGITS. This ensures that the integer will support all supported curves. L_ECC_MAX_DIGITS should also be updated if any larger bit size curves are added.

---
  Makefile.am       |    8 +-
  ell/ecc-private.h |   50 ++
  ell/ecc.c         | 1288 +++++++++++++++++++++++++++++++++++++++++++++
  ell/ecc.h         |   84 +++
  ell/ell.h         |    1 +
  5 files changed, 1429 insertions(+), 2 deletions(-)
Quoted 4 lines

create mode 100644 ell/ecc-private.h
create mode 100644 ell/ecc.c
create mode 100644 ell/ecc.h

So just commenting on the high-level API for now. Overall my impression is that it is still exposing too much low-level details. Can we get away with exposing less?

Quoted 33 lines
ell/ecc.h new file +27 -0
new file mode 100644
index 0000000..0601780
@@ -0,0 +1,84 @@
+/*
+ *
+ *  Embedded Linux library
+ *
+ *  Copyright (C) 2018 Intel Corporation. All rights reserved.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __ELL_ECC_H
+#define __ELL_ECC_H
+
+#ifdef __cplusplus
+extern "C" {

ell/ecc.h line 37

Quoted 10 lines
ell/ecc.h new file +10 -0
+#endif
+
+#define L_ECC_MAX_DIGITS 4
+
+struct l_ecc_curve;
+
+struct l_ecc_point {
+	uint64_t x[L_ECC_MAX_DIGITS];
+	uint64_t y[L_ECC_MAX_DIGITS];
+	unsigned int ndigits;

You might want to just store the pointer to the ecc_curve here and make your life easier in the rest of the API.

ell/ecc.h line 38

Quoted 1 line
ell/ecc.h new file +1 -0
+};

Do you really want to expose this?

ell/ecc.h line 40

Quoted 2 lines
ell/ecc.h new file +2 -0
+
+struct l_ecc_curve *l_ecc_curve_get(unsigned int group);

const struct l_ecc_curve *?

ell/ecc.h line 42

Quoted 2 lines
ell/ecc.h new file +2 -0
+
+unsigned int l_ecc_curve_get_ndigits(struct l_ecc_curve *curve);

const struct?

Is this one really needed, or can it be kept private?

ell/ecc.h line 45

Quoted 3 lines
ell/ecc.h new file +3 -0
+
+const uint64_t *l_ecc_curve_get_prime(struct l_ecc_curve *curve);
+

As above

ell/ecc.h line 46

Quoted 1 line
ell/ecc.h new file +1 -0
+const uint64_t *l_ecc_curve_get_b(struct l_ecc_curve *curve);

And here

ell/ecc.h line 47

Quoted 1 line
ell/ecc.h new file +1 -0
+struct l_ecc_point *l_ecc_curve_get_generator(struct l_ecc_curve *curve);

And here?

ell/ecc.h line 49

Quoted 2 lines
ell/ecc.h new file +2 -0
+void l_ecc_point_set(struct l_ecc_curve *curve, struct l_ecc_point *p,
+			void *x, void *y);

Should this be something like:

struct l_ecc_point *l_ecc_point_new(const struct l_ecc_curve *curve, const void *data, size_t len);

Where I would assume data is in big endian or whatever canonical exchange format.

ell/ecc.h line 60

Quoted 11 lines
ell/ecc.h new file +11 -0
+bool l_ecc_point_multiply(struct l_ecc_curve *curve, struct l_ecc_point *ret,
+				struct l_ecc_point *p, uint64_t *scalar,
+				uint64_t *initial_z);
+bool l_ecc_point_add(struct l_ecc_curve *curve, struct l_ecc_point *ret,
+			struct l_ecc_point *p, struct l_ecc_point *q);
+bool l_ecc_valid_point(struct l_ecc_curve *curve, struct l_ecc_point *point);
+void l_ecc_be2native(struct l_ecc_curve *curve, uint64_t *dest,
+			uint64_t *bytes);
+
+void l_ecc_native2be(struct l_ecc_curve *curve, uint64_t *dest,
+			uint64_t *native);

So this may be just size_t l_ecc_point_to_be(struct l_ecc_point *point, void *dest, size_t dest_len);

Quoted 9 lines
ell/ecc.h new file +9 -0
+void l_ecc_compute_y_sqr(struct l_ecc_curve *curve, uint64_t *y_sqr,
+				uint64_t *x);
+bool l_ecc_compute_y(struct l_ecc_curve *curve, uint64_t *y, uint64_t *x,
+			uint8_t y_bit);
+
+bool l_ecc_compute_scalar(struct l_ecc_curve *curve, uint64_t *scalar,
+				uint64_t *rand, uint64_t *mask);
+
+bool l_ecc_point_inverse(struct l_ecc_curve *curve, struct l_ecc_point *point);

ell/ecc.h line 79

Quoted 10 lines
ell/ecc.h new file +10 -0
+
+bool l_ecc_compute_residue(struct l_ecc_curve *curve,
+					uint64_t *qr_or_qnr, bool residue);
+
+bool l_ecc_is_quadradic_residue(struct l_ecc_curve *curve,
+					uint64_t *value, uint64_t *qr,
+					uint64_t *qnr);
+bool l_ecc_integer_add(struct l_ecc_curve *curve, uint64_t *ret, uint64_t *a,
+			uint64_t *b);
+

All these uint64_t * things scare me. Can we not expose these operations and expose something a bit more higher level?

Regards,
-Denis

#3 lore raw

Re: [PATCH 3/9] ecdh: moved ECDH from IWD

In reply to James Prestwood

Hi James,

ell/ecdh.h line 36

Quoted 5 lines

+ * Generate a private/public key pair. All inputs are expected in little-endian.
+ */

ell/ecdh.h new file +3 -0
+bool l_ecdh_generate_key_pair(struct l_ecc_curve *curve, void *private,
+					size_t priv_len, void *public,
+					size_t pub_len);

Public is just a point on the curve right? So should this be something like:

bool l_ecdh_generate_key_pair(const struct l_ecc_curve *curve,

				struct l_ecc_point *out_public,
				struct l_ecc_constant *out_private);

?

ell/ecdh.h line 45

Quoted 9 lines
ell/ecdh.h new file +9 -0
+/*
+ * Generate a shared secret from a private/public key. All inputs are expected
+ * in little-endian.
+ */
+bool l_ecdh_generate_shared_secret(struct l_ecc_curve *curve,
+						const void *private,
+						const void *other_public,
+						size_t pub_len, void *secret,
+						size_t secret_len);

Similarly here maybe:

struct l_ecc_point *other_public = l_ecc_point_new(curve, data, len);

bool l_ecdh_generate_shared_secret(struct l_ecc_constant *private,

				struct l_ecc_point *other_public,
				void *out_secret,
				size_t out_secret_len);

?

Regards,
-Denis

#4 lore raw

Re: [PATCH 3/9] ecdh: moved ECDH from IWD

Quoted 14 lines · Denis Kenzior

Hi James,

ell/ecdh.h line 36

Quoted 7 lines

+ * Generate a private/public key pair. All inputs are expected in little-endian.
+ */
+bool l_ecdh_generate_key_pair(struct l_ecc_curve *curve, void *private,

ell/ecdh.h new file +2 -0
+					size_t priv_len, void *public,
+					size_t pub_len);

Public is just a point on the curve right? So should this be something like:

Quoted 4 lines

bool l_ecdh_generate_key_pair(const struct l_ecc_curve *curve,

				struct l_ecc_point *out_public,
				struct l_ecc_constant *out_private);

?

Yes it is just a point on the curve, but I don't think that most users of this API need or care to know to that level of detail. Also, generating the shared secret supports compressed coordinates where only the X coordinate is given. In this case the half public key is coming from the network, so its much more convenient just feeding it that raw data that converting it to a point.

Quoted 27 lines

ell/ecdh.h line 45

Quoted 13 lines

+/*
+ * Generate a shared secret from a private/public key. All inputs are expected
+ * in little-endian.
+ */

ell/ecdh.h new file +3 -0
+bool l_ecdh_generate_shared_secret(struct l_ecc_curve *curve,
+						const void *private,
+						const void

*other_public,

(quoted diff) +1 -0
+						size_t pub_len, void

*secret,

ell/ecdh.h new file +1 -0
+						size_t secret_len);

Similarly here maybe:

struct l_ecc_point *other_public = l_ecc_point_new(curve, data, len);

bool l_ecdh_generate_shared_secret(struct l_ecc_constant *private,

				struct l_ecc_point *other_public,
				void *out_secret,
				size_t out_secret_len);

?

Regards,
-Denis

#5 lore raw

Re: [PATCH 3/9] ecdh: moved ECDH from IWD

Hi James,

Quoted 6 lines
Quoted 4 lines

bool l_ecdh_generate_key_pair(const struct l_ecc_curve *curve,

				struct l_ecc_point *out_public,
				struct l_ecc_constant *out_private);

?

Yes it is just a point on the curve, but I don't think that most users of this API need or care to know to that level of detail. Also,

But if we pass in void * and a length, don't they have to care even to a greater degree? I mean they need to know how many bytes they should pass in for example. And then they must somehow *know* that they want a compressed point or uncompressed and pass in the appropriate number of bytes. And then do the conversion to network order.

You could just make ecc_point take care of all these details.

Quoted 4 lines

generating the shared secret supports compressed coordinates where only the X coordinate is given. In this case the half public key is coming from the network, so its much more convenient just feeding it that raw data that converting it to a point.

You still need to validate what is coming over the network and convert it from network byte order. The constructor for ecc_point could do this for you.

Regards,
-Denis