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 ab3a845107377c6cdf148f86015cad94434a1f2e)
This commit is contained in:
Mahavir Jain 2021-11-18 15:39:30 +05:30 committed by harshal.patil
parent 5e106db3a1
commit bd44e815e3

View File

@ -49,19 +49,12 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
<<<<<<< HEAD #if !defined(MBEDTLS_BIGNUM_ALT)
#define MPI_VALIDATE_RET(cond) \ #define MPI_VALIDATE_RET(cond) \
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA) MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
#define MPI_VALIDATE(cond) \ #define MPI_VALIDATE(cond) \
MBEDTLS_INTERNAL_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond)
=======
#if !defined(MBEDTLS_BIGNUM_ALT)
#define MPI_VALIDATE_RET( cond ) \
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
#define MPI_VALIDATE( cond ) \
MBEDTLS_INTERNAL_VALIDATE( cond )
>>>>>>> f859b9bc7 (mbedtls: Re-apply MBEDTLS_BIGNUM_ALT & related macros for custom bignum functions)
#define MPI_SIZE_T_MAX ((size_t) -1) /* SIZE_T_MAX is not standard */ #define MPI_SIZE_T_MAX ((size_t) -1) /* SIZE_T_MAX is not standard */
@ -1661,9 +1654,15 @@ cleanup:
/* /*
* Sliding-window exponentiation: X = A^E mod N (HAC 14.85) * 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, int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
const mbedtls_mpi *E, const mbedtls_mpi *N, const mbedtls_mpi *E, const mbedtls_mpi *N,
mbedtls_mpi *prec_RR) 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; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t window_bitsize; size_t window_bitsize;