esp_bignum: move check for supported MPI bits at start of API

This can allow hardware MPI API to return as soon as it identifies
that it can handle require bitlength operation.
This commit is contained in:
Mahavir Jain 2021-11-26 14:24:30 +05:30 committed by bot
parent 5d9392d999
commit 12fee76695

View File

@ -276,19 +276,23 @@ cleanup2:
static int esp_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, mbedtls_mpi *_Rinv ) static int esp_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, mbedtls_mpi *_Rinv )
{ {
int ret = 0; int ret = 0;
mbedtls_mpi Rinv_new; /* used if _Rinv == NULL */
mbedtls_mpi *Rinv; /* points to _Rinv (if not NULL) othwerwise &RR_new */
mbedtls_mpi_uint Mprime;
size_t x_words = mpi_words(X); size_t x_words = mpi_words(X);
size_t y_words = mpi_words(Y); size_t y_words = mpi_words(Y);
size_t m_words = mpi_words(M); size_t m_words = mpi_words(M);
/* "all numbers must be the same length", so choose longest number /* "all numbers must be the same length", so choose longest number
as cardinal length of operation... as cardinal length of operation...
*/ */
size_t num_words = esp_mpi_hardware_words(MAX(m_words, MAX(x_words, y_words))); size_t num_words = esp_mpi_hardware_words(MAX(m_words, MAX(x_words, y_words)));
mbedtls_mpi Rinv_new; /* used if _Rinv == NULL */ if (num_words * 32 > SOC_RSA_MAX_BIT_LEN) {
mbedtls_mpi *Rinv; /* points to _Rinv (if not NULL) othwerwise &RR_new */ return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
mbedtls_mpi_uint Mprime; }
if (mbedtls_mpi_cmp_int(M, 0) <= 0 || (M->p[0] & 1) == 0) { if (mbedtls_mpi_cmp_int(M, 0) <= 0 || (M->p[0] & 1) == 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
@ -302,10 +306,6 @@ static int esp_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_
return mbedtls_mpi_lset(Z, 1); return mbedtls_mpi_lset(Z, 1);
} }
if (num_words * 32 > SOC_RSA_MAX_BIT_LEN) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}
/* Determine RR pointer, either _RR for cached value /* Determine RR pointer, either _RR for cached value
or local RR_new */ or local RR_new */
if (_Rinv == NULL) { if (_Rinv == NULL) {