From e42572c49323cf679d01ae4dc98963afc0f231c7 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 18 Nov 2021 15:39:30 +0530 Subject: [PATCH] bignum: add provision for combined software and hardware MPI approach For exponential mod (API mbedtls_mpi_exp_mod) operation, some ESP target chips needs to have ability for both hardware and software implementation. Hardware implementation provided performance advantage but it can only support upto 3072 bit operations (e.g., ESP32-C3) and hence we fallback to software implementation in such cases (e.g., 4096 bit operations). Earlier this was handled using linker "--wrap" flag but that does not work in all scenarios as API `mbedtls_mpi_exp_mod` is being used in same tranlation (compilation unit). This approach was found to be next best option with minimal changes in mbedTLS library. (cherry picked from commit 30c6840e0e85d9018f8fdcd9765c7f5102745691) --- library/bignum.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/bignum.c b/library/bignum.c index 9f39925839..d75debda8c 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -2063,9 +2063,15 @@ cleanup: /* * Sliding-window exponentiation: X = A^E mod N (HAC 14.85) */ +#if !defined(MBEDTLS_MPI_EXP_MOD_ALT_FALLBACK) int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *prec_RR) +#else +int mbedtls_mpi_exp_mod_soft( mbedtls_mpi *X, const mbedtls_mpi *A, + const mbedtls_mpi *E, const mbedtls_mpi *N, + mbedtls_mpi *prec_RR ) +#endif { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; size_t window_bitsize;