From aad96b457f73b59793a94b9d0d1ccc657d0fd43a Mon Sep 17 00:00:00 2001 From: Sachin Parekh Date: Wed, 5 Jan 2022 15:23:44 +0530 Subject: [PATCH] ecp: Add support for hardware implementation of ECP routines ESP32C2 has a hardware ECC accelerator that supports NIST P-192 and NIST P-256 curves, which can increase the performance of the point multiplication and point verification operation. Provision is also added to fallback to software implementation in case the curve is not from the supported curves --- library/ecp.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/ecp.c b/library/ecp.c index 7f9e1045d4..1957d65c79 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -168,6 +168,7 @@ static inline int ecp_drbg_random( void *p_rng, return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) ); } +#if !defined(MBEDTLS_ECP_MUL_ALT) /* DRBG context seeding */ static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret, size_t secret_len ) @@ -194,6 +195,7 @@ cleanup: return( ret ); } +#endif /* MBEDTLS_ECP_MUL_ALT */ #elif defined(MBEDTLS_CTR_DRBG_C) @@ -1358,6 +1360,7 @@ cleanup: #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */ } +#if !defined(MBEDTLS_ECP_MUL_ALT) /* * Normalize jacobian coordinates of an array of (pointers to) points, * using Montgomery's trick to perform only one inversion mod P. @@ -1481,6 +1484,7 @@ cleanup: return( ret ); } +#endif /* MBEDTLS_ECP_MUL_ALT */ /* * Point doubling R = 2 P, Jacobian coordinates @@ -1681,6 +1685,7 @@ cleanup: #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */ } +#if !defined(MBEDTLS_ECP_MUL_ALT) /* * Randomize jacobian coordinates: * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l @@ -2416,6 +2421,8 @@ cleanup: return( ret ); } +#endif /* MBEDTLS_ECP_MUL_ALT */ + #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) @@ -2427,6 +2434,7 @@ cleanup: * For scalar multiplication, we'll use a Montgomery ladder. */ +#if !defined(MBEDTLS_ECP_MUL_ALT) /* * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1 * Cost: 1M + 1I @@ -2648,15 +2656,24 @@ cleanup: return( ret ); } +#endif /* MBEDTLS_ECP_MUL_ALT */ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ +#if !defined(MBEDTLS_ECP_MUL_ALT) /* * Restartable multiplication R = m * P */ +#if defined(MBEDTLS_ECP_MUL_ALT_SOFT_FALLBACK) +int mbedtls_ecp_mul_restartable_soft( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, + const mbedtls_mpi *m, const mbedtls_ecp_point *P, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, + mbedtls_ecp_restart_ctx *rs_ctx ) +#else int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_ecp_restart_ctx *rs_ctx ) +#endif { int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; #if defined(MBEDTLS_ECP_INTERNAL_ALT) @@ -2717,6 +2734,7 @@ cleanup: return( ret ); } +#endif /* MBEDTLS_ECP_MUL_ALT */ /* * Multiplication R = m * P @@ -2732,7 +2750,10 @@ int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) ); } +#if !defined(MBEDTLS_ECP_VERIFY_ALT) + #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) + /* * Check that an affine point is valid as a public key, * short weierstrass curves (SEC1 3.2.3.1) @@ -2781,6 +2802,7 @@ cleanup: return( ret ); } #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ +#endif /* MBEDTLS_ECP_VERIFY_ALT */ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) /* @@ -2934,6 +2956,8 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, } #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ +#if !defined(MBEDTLS_ECP_VERIFY_ALT) + #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) #define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)} @@ -3047,11 +3071,19 @@ static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_ } #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ +#endif /* MBEDTLS_ECP_VERIFY_ALT */ + +#if !defined(MBEDTLS_ECP_VERIFY_ALT) /* * Check that a point is valid as a public key */ +#if defined(MBEDTLS_ECP_VERIFY_ALT_SOFT_FALLBACK) +int mbedtls_ecp_check_pubkey_soft( const mbedtls_ecp_group *grp, + const mbedtls_ecp_point *pt ) +#else int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ) +#endif { ECP_VALIDATE_RET( grp != NULL ); ECP_VALIDATE_RET( pt != NULL ); @@ -3070,6 +3102,7 @@ int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, #endif return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); } +#endif /* MBEDTLS_ECP_VERIFY_ALT */ /* * Check that an mbedtls_mpi is valid as a private key