Skip to content

Commit ea5514d

Browse files
sachin0x18laukik-hase
authored andcommitted
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
1 parent 2092ac6 commit ea5514d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

library/ecp.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,7 @@ static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *p
13081308
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */
13091309
}
13101310

1311+
#if !defined(MBEDTLS_ECP_MUL_ALT)
13111312
/*
13121313
* Normalize jacobian coordinates of an array of (pointers to) points,
13131314
* using Montgomery's trick to perform only one inversion mod P.
@@ -1431,6 +1432,7 @@ static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
14311432
mbedtls_mpi_free( &tmp );
14321433
return( ret );
14331434
}
1435+
#endif /* MBEDTLS_ECP_MUL_ALT */
14341436

14351437
/*
14361438
* Point doubling R = 2 P, Jacobian coordinates
@@ -1636,6 +1638,7 @@ static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
16361638
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
16371639
}
16381640

1641+
#if !defined(MBEDTLS_ECP_MUL_ALT)
16391642
/*
16401643
* Randomize jacobian coordinates:
16411644
* (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
@@ -2359,6 +2362,8 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
23592362
return( ret );
23602363
}
23612364

2365+
#endif /* MBEDTLS_ECP_MUL_ALT */
2366+
23622367
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
23632368

23642369
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
@@ -2370,6 +2375,7 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
23702375
* For scalar multiplication, we'll use a Montgomery ladder.
23712376
*/
23722377

2378+
#if !defined(MBEDTLS_ECP_MUL_ALT)
23732379
/*
23742380
* Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
23752381
* Cost: 1M + 1I
@@ -2565,18 +2571,27 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
25652571
return( ret );
25662572
}
25672573

2574+
#endif /* MBEDTLS_ECP_MUL_ALT */
25682575
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
25692576

2577+
#if !defined(MBEDTLS_ECP_MUL_ALT)
25702578
/*
25712579
* Restartable multiplication R = m * P
25722580
*
25732581
* This internal function can be called without an RNG in case where we know
25742582
* the inputs are not sensitive.
25752583
*/
2584+
#if defined(MBEDTLS_ECP_MUL_ALT_SOFT_FALLBACK)
2585+
int ecp_mul_restartable_internal_soft( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2586+
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2587+
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2588+
mbedtls_ecp_restart_ctx *rs_ctx )
2589+
#else
25762590
static int ecp_mul_restartable_internal( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
25772591
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
25782592
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
25792593
mbedtls_ecp_restart_ctx *rs_ctx )
2594+
#endif
25802595
{
25812596
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
25822597
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
@@ -2633,10 +2648,12 @@ static int ecp_mul_restartable_internal( mbedtls_ecp_group *grp, mbedtls_ecp_poi
26332648

26342649
return( ret );
26352650
}
2651+
#endif /* MBEDTLS_ECP_MUL_ALT */
26362652

26372653
/*
26382654
* Restartable multiplication R = m * P
26392655
*/
2656+
26402657
int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
26412658
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
26422659
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
@@ -2667,7 +2684,10 @@ int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
26672684
return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
26682685
}
26692686

2687+
#if !defined(MBEDTLS_ECP_VERIFY_ALT)
2688+
26702689
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2690+
26712691
/*
26722692
* Check that an affine point is valid as a public key,
26732693
* short weierstrass curves (SEC1 3.2.3.1)
@@ -2716,6 +2736,7 @@ static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_
27162736
return( ret );
27172737
}
27182738
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2739+
#endif /* MBEDTLS_ECP_VERIFY_ALT */
27192740

27202741
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
27212742
/*
@@ -2877,6 +2898,8 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
28772898
}
28782899
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
28792900

2901+
#if !defined(MBEDTLS_ECP_VERIFY_ALT)
2902+
28802903
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
28812904
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
28822905
#define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)}
@@ -2990,11 +3013,19 @@ static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_
29903013
}
29913014
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
29923015

3016+
#endif /* MBEDTLS_ECP_VERIFY_ALT */
3017+
3018+
#if !defined(MBEDTLS_ECP_VERIFY_ALT)
29933019
/*
29943020
* Check that a point is valid as a public key
29953021
*/
3022+
#if defined(MBEDTLS_ECP_VERIFY_ALT_SOFT_FALLBACK)
3023+
int mbedtls_ecp_check_pubkey_soft( const mbedtls_ecp_group *grp,
3024+
const mbedtls_ecp_point *pt )
3025+
#else
29963026
int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
29973027
const mbedtls_ecp_point *pt )
3028+
#endif
29983029
{
29993030
ECP_VALIDATE_RET( grp != NULL );
30003031
ECP_VALIDATE_RET( pt != NULL );
@@ -3013,6 +3044,7 @@ int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
30133044
#endif
30143045
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
30153046
}
3047+
#endif /* MBEDTLS_ECP_VERIFY_ALT */
30163048

30173049
/*
30183050
* Check that an mbedtls_mpi is valid as a private key

0 commit comments

Comments
 (0)