From de87461c23080763155b98b12c9567b2d2ae8b2e Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Tue, 13 Jun 2023 16:59:26 +0100 Subject: [PATCH] ecp_curves: Updated the optimised reduction function pointer. This patch modifies the `mbedtls_mpi_opt_red_struct` to use an mpi_uint * pointer and size_t limps arguments. The methods interacting with this pointer have been updated accordingly: - mbedtls_mpi_mod_optred_modulus_setup - mbedtls_ecp_modulus_setup Signed-off-by: Minos Galanakis --- library/bignum_mod.c | 3 ++- library/bignum_mod.h | 6 ++++-- library/ecp_curves.c | 22 +++++++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 54d38bd68..60a3c306f 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -166,7 +166,8 @@ int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N, int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_mpi_uint *p, size_t p_limbs, - int (*modp)(mbedtls_mpi *)) + int (*modp)(mbedtls_mpi_uint *X, + size_t X_limbs)) { standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_OPT_RED); N->rep.ored.modp = modp; diff --git a/library/bignum_mod.h b/library/bignum_mod.h index 60966cc88..87ee01569 100644 --- a/library/bignum_mod.h +++ b/library/bignum_mod.h @@ -125,7 +125,8 @@ typedef struct { } mbedtls_mpi_mont_struct; typedef struct { - int (*modp)(mbedtls_mpi *); /* The optimised reduction function pointer */ + int (*modp)(mbedtls_mpi_uint *X, + size_t X_limbs); /* The optimised reduction function pointer */ } mbedtls_mpi_opt_red_struct; typedef struct { @@ -222,7 +223,8 @@ int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N, int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_mpi_uint *p, size_t p_limbs, - int (*modp)(mbedtls_mpi *)); + int (*modp)(mbedtls_mpi_uint *X, + size_t X_limbs)); /** Free elements of a modulus structure. * diff --git a/library/ecp_curves.c b/library/ecp_curves.c index 569277202..cb941966b 100644 --- a/library/ecp_curves.c +++ b/library/ecp_curves.c @@ -5833,7 +5833,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, const mbedtls_ecp_group_id id, const mbedtls_ecp_modulus_type ctype) { - int (*modp)(mbedtls_mpi *) = NULL; + int (*modp)(mbedtls_mpi_uint *X, size_t X_limbs) = NULL; mbedtls_mpi_uint *p = NULL; size_t p_limbs; @@ -5847,7 +5847,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, case MBEDTLS_ECP_DP_SECP192R1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { #if defined(MBEDTLS_ECP_NIST_OPTIM) - modp = &ecp_mod_p192; + modp = &mbedtls_ecp_mod_p192_raw; #endif p = (mbedtls_mpi_uint *) secp192r1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp192r1_p)); @@ -5862,7 +5862,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, case MBEDTLS_ECP_DP_SECP224R1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { #if defined(MBEDTLS_ECP_NIST_OPTIM) - modp = &ecp_mod_p224; + modp = &mbedtls_ecp_mod_p224_raw; #endif p = (mbedtls_mpi_uint *) secp224r1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp224r1_p)); @@ -5877,7 +5877,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, case MBEDTLS_ECP_DP_SECP256R1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { #if defined(MBEDTLS_ECP_NIST_OPTIM) - modp = &ecp_mod_p256; + modp = &mbedtls_ecp_mod_p256_raw; #endif p = (mbedtls_mpi_uint *) secp256r1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp256r1_p)); @@ -5892,7 +5892,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, case MBEDTLS_ECP_DP_SECP384R1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { #if defined(MBEDTLS_ECP_NIST_OPTIM) - modp = &ecp_mod_p384; + modp = &mbedtls_ecp_mod_p384_raw; #endif p = (mbedtls_mpi_uint *) secp384r1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp384r1_p)); @@ -5907,7 +5907,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, case MBEDTLS_ECP_DP_SECP521R1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { #if defined(MBEDTLS_ECP_NIST_OPTIM) - modp = &ecp_mod_p521; + modp = &mbedtls_ecp_mod_p521_raw; #endif p = (mbedtls_mpi_uint *) secp521r1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp521r1_p)); @@ -5957,7 +5957,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) case MBEDTLS_ECP_DP_CURVE25519: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { - modp = &ecp_mod_p255; + modp = &mbedtls_ecp_mod_p255_raw; p = (mbedtls_mpi_uint *) curve25519_p; p_limbs = CHARS_TO_LIMBS(sizeof(curve25519_p)); } else { @@ -5970,7 +5970,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) case MBEDTLS_ECP_DP_SECP192K1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { - modp = &ecp_mod_p192; + modp = &mbedtls_ecp_mod_p192_raw; p = (mbedtls_mpi_uint *) secp192k1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp192k1_p)); } else { @@ -5983,7 +5983,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) case MBEDTLS_ECP_DP_SECP224K1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { - modp = &ecp_mod_p224; + modp = &mbedtls_ecp_mod_p224_raw; p = (mbedtls_mpi_uint *) secp224k1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp224k1_p)); } else { @@ -5996,7 +5996,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) case MBEDTLS_ECP_DP_SECP256K1: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { - modp = &ecp_mod_p256; + modp = &mbedtls_ecp_mod_p256_raw; p = (mbedtls_mpi_uint *) secp256k1_p; p_limbs = CHARS_TO_LIMBS(sizeof(secp256k1_p)); } else { @@ -6009,7 +6009,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) case MBEDTLS_ECP_DP_CURVE448: if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) { - modp = &ecp_mod_p448; + modp = &mbedtls_ecp_mod_p448_raw; p = (mbedtls_mpi_uint *) curve448_p; p_limbs = CHARS_TO_LIMBS(sizeof(curve448_p)); } else {