mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
MbedTLS: Add software fallback implementation for exp mod
Add configuration option to fallback to software implementation for exponential mod incase of hardware is not supporting it for larger MPI value. Usecase: ESP32C3 only supports till RSA3072 in hardware. This config option will help to support 4k certificates for WPA enterprise connection.
This commit is contained in:
parent
c65a24063d
commit
de22f3a4e5
@ -149,6 +149,10 @@ if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
|
|||||||
endforeach()
|
endforeach()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(CONFIG_MBEDTLS_HARDWARE_MPI)
|
||||||
|
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_mpi_exp_mod")
|
||||||
|
endif()
|
||||||
|
|
||||||
set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_LIBRARIES mbedtls)
|
set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_LIBRARIES mbedtls)
|
||||||
|
|
||||||
# Link mbedtls libraries to component library
|
# Link mbedtls libraries to component library
|
||||||
|
@ -827,6 +827,17 @@ menu "mbedTLS"
|
|||||||
help
|
help
|
||||||
Enable the pthread wrapper layer for the threading layer.
|
Enable the pthread wrapper layer for the threading layer.
|
||||||
|
|
||||||
|
config MBEDTLS_LARGE_KEY_SOFTWARE_MPI
|
||||||
|
bool "Fallback to software implementation for larger MPI values"
|
||||||
|
depends on MBEDTLS_HARDWARE_MPI
|
||||||
|
default y if IDF_TARGET_ESP32C3 # HW max 3072 bits
|
||||||
|
default n
|
||||||
|
help
|
||||||
|
Fallback to software implementation for RSA key lengths
|
||||||
|
larger than SOC_RSA_MAX_BIT_LEN. If this is not active
|
||||||
|
then the ESP will be unable to process keys greater
|
||||||
|
than SOC_RSA_MAX_BIT_LEN.
|
||||||
|
|
||||||
menuconfig MBEDTLS_SECURITY_RISKS
|
menuconfig MBEDTLS_SECURITY_RISKS
|
||||||
bool "Show configurations with potential security risks"
|
bool "Show configurations with potential security risks"
|
||||||
default n
|
default n
|
||||||
|
@ -62,7 +62,6 @@ COMPONENT_EMBED_FILES := $(X509_CERTIFICATE_BUNDLE)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
ifdef CONFIG_MBEDTLS_DYNAMIC_BUFFER
|
ifdef CONFIG_MBEDTLS_DYNAMIC_BUFFER
|
||||||
|
|
||||||
WRAP_FUNCTIONS = mbedtls_ssl_handshake_client_step \
|
WRAP_FUNCTIONS = mbedtls_ssl_handshake_client_step \
|
||||||
mbedtls_ssl_handshake_server_step \
|
mbedtls_ssl_handshake_server_step \
|
||||||
mbedtls_ssl_read \
|
mbedtls_ssl_read \
|
||||||
@ -73,10 +72,14 @@ WRAP_FUNCTIONS = mbedtls_ssl_handshake_client_step \
|
|||||||
mbedtls_ssl_send_alert_message \
|
mbedtls_ssl_send_alert_message \
|
||||||
mbedtls_ssl_close_notify
|
mbedtls_ssl_close_notify
|
||||||
|
|
||||||
WRAP_ARGUMENT := -Wl,--wrap=
|
|
||||||
|
|
||||||
COMPONENT_ADD_LDFLAGS = -l$(COMPONENT_NAME) $(addprefix $(WRAP_ARGUMENT),$(WRAP_FUNCTIONS))
|
|
||||||
|
|
||||||
COMPONENT_SRCDIRS += port/dynamic
|
COMPONENT_SRCDIRS += port/dynamic
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifdef CONFIG_MBEDTLS_HARDWARE_MPI
|
||||||
|
WRAP_FUNCTIONS += mbedtls_mpi_exp_mod
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifneq ($(origin WRAP_FUNCTIONS),undefined)
|
||||||
|
WRAP_ARGUMENT := -Wl,--wrap=
|
||||||
|
COMPONENT_ADD_LDFLAGS = -l$(COMPONENT_NAME) $(addprefix $(WRAP_ARGUMENT),$(WRAP_FUNCTIONS))
|
||||||
endif
|
endif
|
||||||
|
@ -67,7 +67,9 @@ static inline size_t bits_to_words(size_t bits)
|
|||||||
/* Return the number of words actually used to represent an mpi
|
/* Return the number of words actually used to represent an mpi
|
||||||
number.
|
number.
|
||||||
*/
|
*/
|
||||||
#if defined(MBEDTLS_MPI_EXP_MOD_ALT)
|
int __wrap_mbedtls_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, mbedtls_mpi *_Rinv );
|
||||||
|
extern int __real_mbedtls_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, mbedtls_mpi *_Rinv );
|
||||||
|
|
||||||
static size_t mpi_words(const mbedtls_mpi *mpi)
|
static size_t mpi_words(const mbedtls_mpi *mpi)
|
||||||
{
|
{
|
||||||
for (size_t i = mpi->n; i > 0; i--) {
|
for (size_t i = mpi->n; i > 0; i--) {
|
||||||
@ -78,7 +80,6 @@ static size_t mpi_words(const mbedtls_mpi *mpi)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //MBEDTLS_MPI_EXP_MOD_ALT
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -181,8 +182,6 @@ cleanup:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(MBEDTLS_MPI_EXP_MOD_ALT)
|
|
||||||
|
|
||||||
#ifdef ESP_MPI_USE_MONT_EXP
|
#ifdef ESP_MPI_USE_MONT_EXP
|
||||||
/*
|
/*
|
||||||
* Return the most significant one-bit.
|
* Return the most significant one-bit.
|
||||||
@ -273,7 +272,7 @@ cleanup2:
|
|||||||
* (See RSA Accelerator section in Technical Reference for more about Mprime, Rinv)
|
* (See RSA Accelerator section in Technical Reference for more about Mprime, Rinv)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int mbedtls_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, mbedtls_mpi *_Rinv )
|
int __wrap_mbedtls_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;
|
||||||
size_t x_words = mpi_words(X);
|
size_t x_words = mpi_words(X);
|
||||||
@ -303,7 +302,11 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (num_words * 32 > SOC_RSA_MAX_BIT_LEN) {
|
if (num_words * 32 > SOC_RSA_MAX_BIT_LEN) {
|
||||||
|
#ifdef CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI
|
||||||
|
return __real_mbedtls_mpi_exp_mod(Z, X, Y, M, _Rinv);
|
||||||
|
#else
|
||||||
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Determine RR pointer, either _RR for cached value
|
/* Determine RR pointer, either _RR for cached value
|
||||||
@ -352,10 +355,6 @@ cleanup:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MBEDTLS_MPI_EXP_MOD_ALT */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_MPI_MUL_MPI_ALT) /* MBEDTLS_MPI_MUL_MPI_ALT */
|
#if defined(MBEDTLS_MPI_MUL_MPI_ALT) /* MBEDTLS_MPI_MUL_MPI_ALT */
|
||||||
|
|
||||||
static int mpi_mult_mpi_failover_mod_mult( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t z_words);
|
static int mpi_mult_mpi_failover_mod_mult( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t z_words);
|
||||||
|
@ -144,15 +144,15 @@
|
|||||||
#undef MBEDTLS_SHA512_ALT
|
#undef MBEDTLS_SHA512_ALT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* The following MPI (bignum) functions have ESP32 hardware support,
|
/* The following MPI (bignum) functions have ESP32 hardware support.
|
||||||
Uncommenting these macros will use the hardware-accelerated
|
For exponential mod, both software and hardware implementation
|
||||||
implementations.
|
will be compiled. If CONFIG_MBEDTLS_HARDWARE_MPI is enabled, mod APIs
|
||||||
|
will be wrapped to use hardware implementation.
|
||||||
*/
|
*/
|
||||||
|
#undef MBEDTLS_MPI_EXP_MOD_ALT
|
||||||
#ifdef CONFIG_MBEDTLS_HARDWARE_MPI
|
#ifdef CONFIG_MBEDTLS_HARDWARE_MPI
|
||||||
#define MBEDTLS_MPI_EXP_MOD_ALT
|
|
||||||
#define MBEDTLS_MPI_MUL_MPI_ALT
|
#define MBEDTLS_MPI_MUL_MPI_ALT
|
||||||
#else
|
#else
|
||||||
#undef MBEDTLS_MPI_EXP_MOD_ALT
|
|
||||||
#undef MBEDTLS_MPI_MUL_MPI_ALT
|
#undef MBEDTLS_MPI_MUL_MPI_ALT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user