From 83669d910e47e0f1a47add448542434fc6bae63d Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 11 Apr 2023 15:42:18 +0200 Subject: [PATCH 1/8] Add a testable function for ecp_mod_p192k1 Signed-off-by: Gabor Mezei --- library/ecp_curves.c | 8 ++++++++ library/ecp_invasive.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/library/ecp_curves.c b/library/ecp_curves.c index 30ae79e277..071ac61d3a 100644 --- a/library/ecp_curves.c +++ b/library/ecp_curves.c @@ -4608,6 +4608,8 @@ static int ecp_mod_p448(mbedtls_mpi *); #endif #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) static int ecp_mod_p192k1(mbedtls_mpi *); +MBEDTLS_TEST_HOOKS +int mbedtls_ecp_mod_p192k1(mbedtls_mpi *); #endif #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) static int ecp_mod_p224k1(mbedtls_mpi *); @@ -5604,6 +5606,12 @@ cleanup: * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119 */ static int ecp_mod_p192k1(mbedtls_mpi *N) +{ + return mbedtls_ecp_mod_p192k1(N); +} + +MBEDTLS_STATIC_TESTABLE +int mbedtls_ecp_mod_p192k1(mbedtls_mpi *N) { static mbedtls_mpi_uint Rp[] = { MBEDTLS_BYTES_TO_T_UINT_8(0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, diff --git a/library/ecp_invasive.h b/library/ecp_invasive.h index 8b8ac8ada0..f1cfa530cc 100644 --- a/library/ecp_invasive.h +++ b/library/ecp_invasive.h @@ -191,6 +191,9 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_ecp_group_id id, const mbedtls_ecp_curve_type ctype); +MBEDTLS_STATIC_TESTABLE +int mbedtls_ecp_mod_p192k1(mbedtls_mpi *N); + #endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */ #endif /* MBEDTLS_ECP_INVASIVE_H */ From b86ead3cb2f772084ecd3b41450925e6a87e24aa Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 11 Apr 2023 15:43:12 +0200 Subject: [PATCH 2/8] Add generated tests for ecp_mod_p192k1 Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp.py | 63 ++++++++++++++++++++++++++++ tests/suites/test_suite_ecp.function | 42 +++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py index d1d23c1307..b211c98eb9 100644 --- a/scripts/mbedtls_dev/ecp.py +++ b/scripts/mbedtls_dev/ecp.py @@ -488,3 +488,66 @@ class EcpP521R1Raw(bignum_common.ModOperationCommon, def arguments(self): args = super().arguments() return ["MBEDTLS_ECP_DP_SECP521R1"] + args + + +class EcpP192K1Raw(bignum_common.ModOperationCommon, + EcpTarget): + """Test cases for ECP P192K1 fast reduction.""" + symbol = "-" + test_function = "ecp_mod_p192k1" + test_name = "ecp_mod_p192k1" + input_style = "fixed" + arity = 1 + + moduli = ["fffffffffffffffffffffffffffffffffffffffeffffee37"] # type: List[str] + + input_values = [ + "0", "1", + + # Modulus - 1 + "fffffffffffffffffffffffffffffffffffffffeffffee36", + + # Modulus + 1 + "fffffffffffffffffffffffffffffffffffffffeffffee38", + + # 2^192 - 1 + "ffffffffffffffffffffffffffffffffffffffffffffffff", + + # Maximum canonical P192K1 multiplication result + ("fffffffffffffffffffffffffffffffffffffffdffffdc6c" + "0000000000000000000000000000000100002394013c7364"), + + # First 8 number generated by random.getrandbits(384) - seed(2,2) + ("cf1822ffbc6887782b491044d5e341245c6e433715ba2bdd" + "177219d30e7a269fd95bafc8f2a4d27bdcf4bb99f4bea973"), + ("ffed9235288bc781ae66267594c9c9500925e4749b575bd1" + "3653f8dd9b1f282e4067c3584ee207f8da94e3e8ab73738f"), + ("ef8acd128b4f2fc15f3f57ebf30b94fa82523e86feac7eb7" + "dc38f519b91751dacdbd47d364be8049a372db8f6e405d93"), + ("e8624fab5186ee32ee8d7ee9770348a05d300cb90706a045" + "defc044a09325626e6b58de744ab6cce80877b6f71e1f6d2"), + ("2d3d854e061b90303b08c6e33c7295782d6c797f8f7d9b78" + "2a1be9cd8697bbd0e2520e33e44c50556c71c4a66148a86f"), + ("fec3f6b32e8d4b8a8f54f8ceacaab39e83844b40ffa9b9f1" + "5c14bc4a829e07b0829a48d422fe99a22c70501e533c9135"), + ("97eeab64ca2ce6bc5d3fd983c34c769fe89204e2e8168561" + "867e5e15bc01bfce6a27e0dfcbf8754472154e76e4c11ab2"), + ("bd143fa9b714210c665d7435c1066932f4767f26294365b2" + "721dea3bf63f23d0dbe53fcafb2147df5ca495fa5a91c89b"), + + # Next 2 number generated by random.getrandbits(192) + "47733e847d718d733ff98ff387c56473a7a83ee0761ebfd2", + "cbd4d3e2d4dec9ef83f0be4e80371eb97f81375eecc1cb63" + ] + + @property + def arg_a(self) -> str: + return super().format_arg('{:x}'.format(self.int_a)).zfill(2 * self.hex_digits) + + def result(self) -> List[str]: + result = self.int_a % self.int_n + return [self.format_result(result)] + + @property + def is_valid(self) -> bool: + return True diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 0326f1f665..39fdefa34d 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -1355,6 +1355,48 @@ exit: } /* END_CASE */ +/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_DP_SECP192K1_ENABLED */ +void ecp_mod_p192k1(char *input_N, + char *input_X, + char *result) +{ + mbedtls_mpi X; + mbedtls_mpi N; + mbedtls_mpi res; + size_t limbs_X; + size_t limbs_N; + size_t limbs_res; + + mbedtls_mpi_init(&X); + mbedtls_mpi_init(&N); + mbedtls_mpi_init(&res); + + TEST_EQUAL(mbedtls_test_read_mpi(&X, input_X), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&res, result), 0); + + limbs_X = X.n; + limbs_N = N.n; + limbs_res = res.n; + + size_t limbs = limbs_N; + size_t bytes = limbs * sizeof(mbedtls_mpi_uint); + + TEST_EQUAL(limbs_X, 2 * limbs); + TEST_EQUAL(limbs_res, limbs); + + TEST_EQUAL(mbedtls_ecp_mod_p192k1(&X), 0); + TEST_EQUAL(mbedtls_mpi_mod_mpi(&X, &X, &N), 0); + TEST_LE_U(mbedtls_mpi_core_bitlen(X.p, limbs_X), 192); + ASSERT_COMPARE(X.p, bytes, res.p, bytes); + +exit: + mbedtls_mpi_free(&X); + mbedtls_mpi_free(&N); + mbedtls_mpi_free(&res); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ void ecp_mod_setup(char *input_A, int id, int ctype, int iret) { From 1237a349ede01aee01a192d496a8778704dd06a5 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 11 Apr 2023 16:22:35 +0200 Subject: [PATCH 3/8] Use macro guard for function declaration Signed-off-by: Gabor Mezei --- library/ecp_invasive.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/library/ecp_invasive.h b/library/ecp_invasive.h index f1cfa530cc..f654108694 100644 --- a/library/ecp_invasive.h +++ b/library/ecp_invasive.h @@ -169,6 +169,17 @@ int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs); #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ +#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) + +/* + * Fast quasi-reduction modulo p192k1 = 2^192 - R, + * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119 + */ +MBEDTLS_STATIC_TESTABLE +int mbedtls_ecp_mod_p192k1(mbedtls_mpi *N); + +#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ + /** Initialise a modulus with hard-coded const curve data. * * \note The caller is responsible for the \p N modulus' memory. @@ -191,9 +202,6 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_ecp_group_id id, const mbedtls_ecp_curve_type ctype); -MBEDTLS_STATIC_TESTABLE -int mbedtls_ecp_mod_p192k1(mbedtls_mpi *N); - #endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */ #endif /* MBEDTLS_ECP_INVASIVE_H */ From 0a11ee6da83555f55480cde26a184837768f8bc1 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 11 Apr 2023 18:02:33 +0200 Subject: [PATCH 4/8] Fix function declaration Signed-off-by: Gabor Mezei --- library/ecp_curves.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ecp_curves.c b/library/ecp_curves.c index 071ac61d3a..2fcb5c16e4 100644 --- a/library/ecp_curves.c +++ b/library/ecp_curves.c @@ -4608,7 +4608,7 @@ static int ecp_mod_p448(mbedtls_mpi *); #endif #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) static int ecp_mod_p192k1(mbedtls_mpi *); -MBEDTLS_TEST_HOOKS +MBEDTLS_STATIC_TESTABLE int mbedtls_ecp_mod_p192k1(mbedtls_mpi *); #endif #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) From bda7f87e2a21a56367ebc71212113e1ac76e35e9 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 13 Apr 2023 13:11:05 +0200 Subject: [PATCH 5/8] Add dependency for P192K1 tests Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py index b211c98eb9..636ff20a96 100644 --- a/scripts/mbedtls_dev/ecp.py +++ b/scripts/mbedtls_dev/ecp.py @@ -498,6 +498,7 @@ class EcpP192K1Raw(bignum_common.ModOperationCommon, test_name = "ecp_mod_p192k1" input_style = "fixed" arity = 1 + dependencies = ["MBEDTLS_ECP_DP_SECP119K1_ENABLED"] moduli = ["fffffffffffffffffffffffffffffffffffffffeffffee37"] # type: List[str] From b70f5f1881eb76374415e50dd4dbb28eb87a8549 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 13 Apr 2023 13:12:00 +0200 Subject: [PATCH 6/8] Add checks to guarantee positive input parameters Signed-off-by: Gabor Mezei --- tests/suites/test_suite_ecp.function | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 39fdefa34d..59dcb6fc43 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -1378,6 +1378,9 @@ void ecp_mod_p192k1(char *input_N, limbs_X = X.n; limbs_N = N.n; limbs_res = res.n; + TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, X.p, X.n)); + TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, N.p, N.n)); + TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, res.p, res.n)); size_t limbs = limbs_N; size_t bytes = limbs * sizeof(mbedtls_mpi_uint); From 00c9c7a81b1aa8607a724a2609f29102837ecbd6 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 13 Apr 2023 13:13:14 +0200 Subject: [PATCH 7/8] Remove unneeded limb variables Signed-off-by: Gabor Mezei --- tests/suites/test_suite_ecp.function | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 59dcb6fc43..ccdacb95a8 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -1363,9 +1363,6 @@ void ecp_mod_p192k1(char *input_N, mbedtls_mpi X; mbedtls_mpi N; mbedtls_mpi res; - size_t limbs_X; - size_t limbs_N; - size_t limbs_res; mbedtls_mpi_init(&X); mbedtls_mpi_init(&N); @@ -1375,22 +1372,19 @@ void ecp_mod_p192k1(char *input_N, TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0); TEST_EQUAL(mbedtls_test_read_mpi(&res, result), 0); - limbs_X = X.n; - limbs_N = N.n; - limbs_res = res.n; TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, X.p, X.n)); TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, N.p, N.n)); TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, res.p, res.n)); - size_t limbs = limbs_N; + size_t limbs = N.n; size_t bytes = limbs * sizeof(mbedtls_mpi_uint); - TEST_EQUAL(limbs_X, 2 * limbs); - TEST_EQUAL(limbs_res, limbs); + TEST_EQUAL(X.n, 2 * limbs); + TEST_EQUAL(res.n, limbs); TEST_EQUAL(mbedtls_ecp_mod_p192k1(&X), 0); TEST_EQUAL(mbedtls_mpi_mod_mpi(&X, &X, &N), 0); - TEST_LE_U(mbedtls_mpi_core_bitlen(X.p, limbs_X), 192); + TEST_LE_U(mbedtls_mpi_core_bitlen(X.p, X.n), 192); ASSERT_COMPARE(X.p, bytes, res.p, bytes); exit: From cec506083b3dbddd52bdf6d22d5b18c4277f5aae Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 17 Apr 2023 14:56:03 +0200 Subject: [PATCH 8/8] Fix test case dependency Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py index 636ff20a96..8a711ce1f5 100644 --- a/scripts/mbedtls_dev/ecp.py +++ b/scripts/mbedtls_dev/ecp.py @@ -498,7 +498,7 @@ class EcpP192K1Raw(bignum_common.ModOperationCommon, test_name = "ecp_mod_p192k1" input_style = "fixed" arity = 1 - dependencies = ["MBEDTLS_ECP_DP_SECP119K1_ENABLED"] + dependencies = ["MBEDTLS_ECP_DP_SECP192K1_ENABLED"] moduli = ["fffffffffffffffffffffffffffffffffffffffeffffee37"] # type: List[str]