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

- Override ecp_mul_restartable_internal with accelerator
  - Many ECC operations use the internal API ecp_mul_restartable_internal
    instead of the public API mbedtls_ecp_mul for point multiplication.
    This will improve the performance of all those parent operations as
    well
This commit is contained in:
Sachin Parekh 2022-01-05 15:23:44 +05:30 committed by Laukik Hase
parent ab3a845107
commit 0ecb27b9d8
No known key found for this signature in database
GPG Key ID: 11C571361F51A199

View File

@ -1236,6 +1236,7 @@ cleanup:
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */ #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, * Normalize jacobian coordinates of an array of (pointers to) points,
* using Montgomery's trick to perform only one inversion mod P. * using Montgomery's trick to perform only one inversion mod P.
@ -1359,6 +1360,7 @@ cleanup:
mbedtls_mpi_free( &tmp ); mbedtls_mpi_free( &tmp );
return( ret ); return( ret );
} }
#endif /* MBEDTLS_ECP_MUL_ALT */
/* /*
* Point doubling R = 2 P, Jacobian coordinates * Point doubling R = 2 P, Jacobian coordinates
@ -1564,6 +1566,7 @@ cleanup:
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */ #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
} }
#if !defined(MBEDTLS_ECP_MUL_ALT)
/* /*
* Randomize jacobian coordinates: * Randomize jacobian coordinates:
* (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
@ -2289,6 +2292,8 @@ cleanup:
return( ret ); return( ret );
} }
#endif /* MBEDTLS_ECP_MUL_ALT */
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
@ -2300,6 +2305,7 @@ cleanup:
* For scalar multiplication, we'll use a Montgomery ladder. * 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 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
* Cost: 1M + 1I * Cost: 1M + 1I
@ -2495,18 +2501,27 @@ cleanup:
return( ret ); return( ret );
} }
#endif /* MBEDTLS_ECP_MUL_ALT */
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
#if !defined(MBEDTLS_ECP_MUL_ALT)
/* /*
* Restartable multiplication R = m * P * Restartable multiplication R = m * P
* *
* This internal function can be called without an RNG in case where we know * This internal function can be called without an RNG in case where we know
* the inputs are not sensitive. * the inputs are not sensitive.
*/ */
#if defined(MBEDTLS_ECP_MUL_ALT_SOFT_FALLBACK)
int ecp_mul_restartable_internal_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
static int ecp_mul_restartable_internal( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, static int ecp_mul_restartable_internal( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P, const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
mbedtls_ecp_restart_ctx *rs_ctx ) mbedtls_ecp_restart_ctx *rs_ctx )
#endif
{ {
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
#if defined(MBEDTLS_ECP_INTERNAL_ALT) #if defined(MBEDTLS_ECP_INTERNAL_ALT)
@ -2565,10 +2580,12 @@ cleanup:
return( ret ); return( ret );
} }
#endif /* MBEDTLS_ECP_MUL_ALT */
/* /*
* Restartable multiplication R = m * P * Restartable multiplication R = m * P
*/ */
int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P, const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
@ -2590,7 +2607,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 ) ); 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) #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
/* /*
* Check that an affine point is valid as a public key, * Check that an affine point is valid as a public key,
* short weierstrass curves (SEC1 3.2.3.1) * short weierstrass curves (SEC1 3.2.3.1)
@ -2639,6 +2659,7 @@ cleanup:
return( ret ); return( ret );
} }
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
#endif /* MBEDTLS_ECP_VERIFY_ALT */
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
/* /*
@ -2790,6 +2811,8 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
} }
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
#if !defined(MBEDTLS_ECP_VERIFY_ALT)
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
#define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)} #define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)}
@ -2903,11 +2926,19 @@ static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_
} }
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ #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 * 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, int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
const mbedtls_ecp_point *pt ) const mbedtls_ecp_point *pt )
#endif
{ {
/* Must use affine coordinates */ /* Must use affine coordinates */
if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 ) if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
@ -2923,6 +2954,7 @@ int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
#endif #endif
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
} }
#endif /* MBEDTLS_ECP_VERIFY_ALT */
/* /*
* Check that an mbedtls_mpi is valid as a private key * Check that an mbedtls_mpi is valid as a private key