From a7af364112ec73f77baf13d4ff5dfd1d9b9d7ca8 Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Thu, 12 Jun 2025 13:18:57 +0530 Subject: [PATCH] fix(esp_security): Power up MPI memory registers when enabling MPI Co-authored-by: Li HongXi --- .../esp_security/src/esp_crypto_periph_clk.c | 3 +++ components/esp_security/src/esp_ds.c | 2 ++ components/hal/mpi_hal.c | 3 +-- .../hal/test_apps/crypto/main/mpi/test_mpi.c | 16 ++++------------ components/mbedtls/port/bignum/bignum_alt.c | 2 -- 5 files changed, 10 insertions(+), 16 deletions(-) diff --git a/components/esp_security/src/esp_crypto_periph_clk.c b/components/esp_security/src/esp_crypto_periph_clk.c index 0af93ee6c5..a62c804b35 100644 --- a/components/esp_security/src/esp_crypto_periph_clk.c +++ b/components/esp_security/src/esp_crypto_periph_clk.c @@ -80,7 +80,10 @@ void esp_crypto_mpi_enable_periph_clk(bool enable) MPI_RCC_ATOMIC() { mpi_ll_enable_bus_clock(enable); if (enable) { + mpi_ll_power_up(); mpi_ll_reset_register(); + } else { + mpi_ll_power_down(); } } } diff --git a/components/esp_security/src/esp_ds.c b/components/esp_security/src/esp_ds.c index ae941f4369..0ca22898ee 100644 --- a/components/esp_security/src/esp_ds.c +++ b/components/esp_security/src/esp_ds.c @@ -253,12 +253,14 @@ static void ds_acquire_enable(void) // We also enable SHA and HMAC here. SHA is used by HMAC, HMAC is used by DS. esp_crypto_hmac_enable_periph_clk(true); esp_crypto_sha_enable_periph_clk(true); + esp_crypto_mpi_enable_periph_clk(true); esp_crypto_ds_enable_periph_clk(true); } static void ds_disable_release(void) { esp_crypto_ds_enable_periph_clk(false); + esp_crypto_mpi_enable_periph_clk(false); esp_crypto_sha_enable_periph_clk(false); esp_crypto_hmac_enable_periph_clk(false); diff --git a/components/hal/mpi_hal.c b/components/hal/mpi_hal.c index 8302ae847a..014ab14655 100644 --- a/components/hal/mpi_hal.c +++ b/components/hal/mpi_hal.c @@ -15,7 +15,6 @@ size_t mpi_hal_calc_hardware_words(size_t words) void mpi_hal_enable_hardware_hw_op(void) { - mpi_ll_power_up(); while (mpi_ll_check_memory_init_complete()) { } // Note: from enabling RSA clock to here takes about 1.3us @@ -27,7 +26,7 @@ void mpi_hal_enable_hardware_hw_op(void) void mpi_hal_disable_hardware_hw_op(void) { - mpi_ll_power_down(); + } void mpi_hal_interrupt_enable(bool enable) diff --git a/components/hal/test_apps/crypto/main/mpi/test_mpi.c b/components/hal/test_apps/crypto/main/mpi/test_mpi.c index 029abe5343..d0c971e9f1 100644 --- a/components/hal/test_apps/crypto/main/mpi/test_mpi.c +++ b/components/hal/test_apps/crypto/main/mpi/test_mpi.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -19,17 +19,13 @@ #include "hal/mpi_hal.h" #include "hal/mpi_ll.h" #include "mpi_params.h" +#include "esp_crypto_periph_clk.h" #define _DEBUG_ 0 static void esp_mpi_enable_hardware_hw_op( void ) { - /* Enable RSA hardware */ - MPI_RCC_ATOMIC() { - mpi_ll_enable_bus_clock(true); - mpi_ll_reset_register(); - } - + esp_crypto_mpi_enable_periph_clk(true); mpi_hal_enable_hardware_hw_op(); } @@ -37,11 +33,7 @@ static void esp_mpi_enable_hardware_hw_op( void ) static void esp_mpi_disable_hardware_hw_op( void ) { mpi_hal_disable_hardware_hw_op(); - - /* Disable RSA hardware */ - MPI_RCC_ATOMIC() { - mpi_ll_enable_bus_clock(false); - } + esp_crypto_mpi_enable_periph_clk(false); } diff --git a/components/mbedtls/port/bignum/bignum_alt.c b/components/mbedtls/port/bignum/bignum_alt.c index ceb263b8d5..5717faf3b4 100644 --- a/components/mbedtls/port/bignum/bignum_alt.c +++ b/components/mbedtls/port/bignum/bignum_alt.c @@ -24,8 +24,6 @@ void esp_mpi_enable_hardware_hw_op( void ) void esp_mpi_disable_hardware_hw_op( void ) { - mpi_hal_disable_hardware_hw_op(); - /* Disable RSA hardware */ esp_crypto_mpi_enable_periph_clk(false);