From a30afe2216748997a1f184e16ddd1faf66f802b5 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Wed, 15 Feb 2023 15:36:29 +0000 Subject: [PATCH] ecp_curves: Minor refactoring. This patch introduces the following changes: * Documentation for `mbedtls_ecp_modulus_setup()` moved to `ecp_invasive.h`. * Added invalid modulus selector `MBEDTLS_ECP_MOD_NONE`. * Adjusted negative tests to use invalid selectors. * Reworded documentation. Signed-off-by: Minos Galanakis --- include/mbedtls/ecp.h | 5 +++-- library/ecp_curves.c | 18 +----------------- library/ecp_invasive.h | 17 +++++++++++++++++ tests/suites/test_suite_ecp.data | 4 ++-- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index 20b663e79..83d5b6ae6 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -142,10 +142,11 @@ typedef enum { } mbedtls_ecp_curve_type; /* - * Curve moduli types + * Curve modulus types */ typedef enum { - MBEDTLS_ECP_MOD_COORDINATE = 0, + MBEDTLS_ECP_MOD_NONE = 0, + MBEDTLS_ECP_MOD_COORDINATE, MBEDTLS_ECP_MOD_SCALAR } mbedtls_ecp_modulus_type; diff --git a/library/ecp_curves.c b/library/ecp_curves.c index d4d8c089e..b352e7633 100644 --- a/library/ecp_curves.c +++ b/library/ecp_curves.c @@ -5535,22 +5535,6 @@ static int ecp_mod_p256k1(mbedtls_mpi *N) #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ #if defined(MBEDTLS_TEST_HOOKS) - -/** Initialise a modulus with hard-coded const curve data. - * - * \param[out] N The address of the modulus structure to populate. - * Must be initialized. - * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus. - * \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P) - * or a scalar modulus (N). - * - * \return \c 0 if successful. - * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not - * have the correct number of limbs. - * - * \note The caller is responsible for the \p N moduli lifecycle. - * - */ MBEDTLS_STATIC_TESTABLE int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_ecp_group_id id, @@ -5728,7 +5712,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, if (mbedtls_mpi_mod_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY)) { - return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; } return 0; } diff --git a/library/ecp_invasive.h b/library/ecp_invasive.h index 10aa34bb1..aba7cca1c 100644 --- a/library/ecp_invasive.h +++ b/library/ecp_invasive.h @@ -118,6 +118,23 @@ int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs); #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ +/** Initialise a modulus with hard-coded const curve data. + * + * \note The caller is responsible for the \p N modulus' memory. + * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the + * end of its lifecycle. + * + * \param[in,out] N The address of the modulus structure to populate. + * Must be initialized. + * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus. + * \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P) + * or a scalar modulus (N). + * + * \return \c 0 if successful. + * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not + * have the correct number of limbs. + * + */ MBEDTLS_STATIC_TESTABLE int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_ecp_group_id id, diff --git a/tests/suites/test_suite_ecp.data b/tests/suites/test_suite_ecp.data index 8d838984b..3fbad92a5 100644 --- a/tests/suites/test_suite_ecp.data +++ b/tests/suites/test_suite_ecp.data @@ -1145,8 +1145,8 @@ ecp_mod_setup:"0000000000000003fffffffffffffffffffffffffffffffffffffffffffffffff ecp_setup_negative_test #27 Invalid Moduli Type depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED -ecp_mod_setup:"fffffffffffffffffffffffe26f2fc17f69466a74defd8d":MBEDTLS_ECP_DP_CURVE448:MBEDTLS_ECP_MOD_SCALAR+1:MBEDTLS_ERR_ECP_BAD_INPUT_DATA +ecp_mod_setup:"fffffffffffffffffffffffe26f2fc17f69466a74defd8d":MBEDTLS_ECP_DP_CURVE448:MBEDTLS_ECP_MOD_NONE:MBEDTLS_ERR_ECP_BAD_INPUT_DATA ecp_setup_negative_test #28 Invalid Curve Type depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED -ecp_mod_setup:"fffffffffffffffffffffffe26f2fc17f69466a74defd8d":MBEDTLS_ECP_DP_CURVE448+1:MBEDTLS_ECP_MOD_SCALAR:MBEDTLS_ERR_ECP_BAD_INPUT_DATA +ecp_mod_setup:"fffffffffffffffffffffffe26f2fc17f69466a74defd8d":MBEDTLS_ECP_DP_NONE:MBEDTLS_ECP_MOD_SCALAR:MBEDTLS_ERR_ECP_BAD_INPUT_DATA