diff --git a/components/esp_hw_support/esp_ds.c b/components/esp_hw_support/esp_ds.c index edecb08e08..332a282b8e 100644 --- a/components/esp_hw_support/esp_ds.c +++ b/components/esp_hw_support/esp_ds.c @@ -14,6 +14,7 @@ #include "esp_timer.h" #include "esp_ds.h" #include "esp_crypto_lock.h" +#include "esp_private/esp_crypto_lock_internal.h" #include "esp_hmac.h" #include "esp_memory_utils.h" #if CONFIG_IDF_TARGET_ESP32S2 @@ -26,6 +27,7 @@ #include "hal/ds_hal.h" #include "hal/ds_ll.h" #include "hal/hmac_hal.h" +#include "hal/hmac_ll.h" #endif /* !CONFIG_IDF_TARGET_ESP32S2 */ #if CONFIG_IDF_TARGET_ESP32S2 @@ -258,7 +260,11 @@ static void ds_acquire_enable(void) esp_crypto_mpi_lock_acquire(); #endif // We also enable SHA and HMAC here. SHA is used by HMAC, HMAC is used by DS. - periph_module_enable(PERIPH_HMAC_MODULE); + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(true); + hmac_ll_reset_register(); + } + periph_module_enable(PERIPH_SHA_MODULE); periph_module_enable(PERIPH_DS_MODULE); @@ -271,7 +277,10 @@ static void ds_disable_release(void) periph_module_disable(PERIPH_DS_MODULE); periph_module_disable(PERIPH_SHA_MODULE); - periph_module_disable(PERIPH_HMAC_MODULE); + + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(false); + } #if CONFIG_IDF_TARGET_ESP32S3 esp_crypto_mpi_lock_release(); diff --git a/components/esp_hw_support/esp_hmac.c b/components/esp_hw_support/esp_hmac.c index be47806dfb..acb69d8f3c 100644 --- a/components/esp_hw_support/esp_hmac.c +++ b/components/esp_hw_support/esp_hmac.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -13,11 +13,13 @@ #include "esp_hmac.h" #include "esp_log.h" #include "esp_crypto_lock.h" +#include "esp_private/esp_crypto_lock_internal.h" #include "soc/hwcrypto_reg.h" #include "soc/system_reg.h" #if !CONFIG_IDF_TARGET_ESP32S2 #include "hal/hmac_hal.h" +#include "hal/hmac_ll.h" #include "esp_private/periph_ctrl.h" #endif @@ -67,7 +69,11 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id, esp_crypto_hmac_lock_acquire(); // We also enable SHA and DS here. SHA is used by HMAC, DS will otherwise hold SHA in reset state. - periph_module_enable(PERIPH_HMAC_MODULE); + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(true); + hmac_ll_reset_register(); + } + periph_module_enable(PERIPH_SHA_MODULE); periph_module_enable(PERIPH_DS_MODULE); @@ -133,7 +139,10 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id, periph_module_disable(PERIPH_DS_MODULE); periph_module_disable(PERIPH_SHA_MODULE); - periph_module_disable(PERIPH_HMAC_MODULE); + + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(false); + } esp_crypto_hmac_lock_release(); diff --git a/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h b/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h index 178dd48543..ceca9963d6 100644 --- a/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h +++ b/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h @@ -16,9 +16,11 @@ extern "C" { #if SOC_RCC_IS_INDEPENDENT #define MPI_RCC_ATOMIC() #define ECC_RCC_ATOMIC() +#define HMAC_RCC_ATOMIC() #else /* !SOC_RCC_IS_INDEPENDENT */ #define MPI_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #define ECC_RCC_ATOMIC() PERIPH_RCC_ATOMIC() +#define HMAC_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #endif /* SOC_RCC_IS_INDEPENDENT */ #ifdef __cplusplus diff --git a/components/hal/esp32c3/include/hal/hmac_ll.h b/components/hal/esp32c3/include/hal/hmac_ll.h index 646dd14d76..4e7fcec171 100644 --- a/components/hal/esp32c3/include/hal/hmac_ll.h +++ b/components/hal/esp32c3/include/hal/hmac_ll.h @@ -12,9 +12,11 @@ #pragma once +#include #include #include "soc/system_reg.h" +#include "soc/system_struct.h" #include "soc/hwcrypto_reg.h" #include "hal/hmac_types.h" @@ -30,6 +32,33 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for HMAC peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void hmac_ll_enable_bus_clock(bool enable) +{ + SYSTEM.perip_clk_en1.reg_crypto_hmac_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define hmac_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; hmac_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the HMAC peripheral module + */ +static inline void hmac_ll_reset_register(void) +{ + SYSTEM.perip_rst_en1.reg_crypto_hmac_rst = 1; + SYSTEM.perip_rst_en1.reg_crypto_hmac_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define hmac_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; hmac_ll_reset_register(__VA_ARGS__) + /** * Makes the peripheral ready for use, after enabling it. */ diff --git a/components/hal/esp32c6/include/hal/hmac_ll.h b/components/hal/esp32c6/include/hal/hmac_ll.h index 2911197fcc..1b690a0c6f 100644 --- a/components/hal/esp32c6/include/hal/hmac_ll.h +++ b/components/hal/esp32c6/include/hal/hmac_ll.h @@ -13,9 +13,11 @@ #pragma once #include +#include #include "soc/system_reg.h" #include "soc/hwcrypto_reg.h" +#include "soc/pcr_struct.h" #include "hal/hmac_types.h" #define SHA256_BLOCK_SZ 64 @@ -30,6 +32,25 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for HMAC peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void hmac_ll_enable_bus_clock(bool enable) +{ + PCR.hmac_conf.hmac_clk_en = enable; +} + +/** + * @brief Reset the HMAC peripheral module + */ +static inline void hmac_ll_reset_register(void) +{ + PCR.hmac_conf.hmac_rst_en = 1; + PCR.hmac_conf.hmac_rst_en = 0; +} + /** * Makes the peripheral ready for use, after enabling it. */ diff --git a/components/hal/esp32h2/include/hal/hmac_ll.h b/components/hal/esp32h2/include/hal/hmac_ll.h index 2911197fcc..1b690a0c6f 100644 --- a/components/hal/esp32h2/include/hal/hmac_ll.h +++ b/components/hal/esp32h2/include/hal/hmac_ll.h @@ -13,9 +13,11 @@ #pragma once #include +#include #include "soc/system_reg.h" #include "soc/hwcrypto_reg.h" +#include "soc/pcr_struct.h" #include "hal/hmac_types.h" #define SHA256_BLOCK_SZ 64 @@ -30,6 +32,25 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for HMAC peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void hmac_ll_enable_bus_clock(bool enable) +{ + PCR.hmac_conf.hmac_clk_en = enable; +} + +/** + * @brief Reset the HMAC peripheral module + */ +static inline void hmac_ll_reset_register(void) +{ + PCR.hmac_conf.hmac_rst_en = 1; + PCR.hmac_conf.hmac_rst_en = 0; +} + /** * Makes the peripheral ready for use, after enabling it. */ diff --git a/components/hal/esp32p4/include/hal/hmac_ll.h b/components/hal/esp32p4/include/hal/hmac_ll.h index f4f95bd836..8772385259 100644 --- a/components/hal/esp32p4/include/hal/hmac_ll.h +++ b/components/hal/esp32p4/include/hal/hmac_ll.h @@ -13,9 +13,11 @@ #pragma once #include +#include #include "soc/system_reg.h" #include "soc/hwcrypto_reg.h" +#include "soc/hp_sys_clkrst_struct.h" #include "hal/hmac_hal.h" #define SHA256_BLOCK_SZ 64 @@ -30,6 +32,35 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for HMAC peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void hmac_ll_enable_bus_clock(bool enable) +{ + HP_SYS_CLKRST.peri_clk_ctrl25.reg_crypto_hmac_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define hmac_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; hmac_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the HMAC peripheral module + */ +static inline void hmac_ll_reset_register(void) +{ + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_hmac = 1; + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_hmac = 0; + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_crypto = 1; + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_crypto = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define hmac_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; hmac_ll_reset_register(__VA_ARGS__) + /** * Makes the peripheral ready for use, after enabling it. */ diff --git a/components/hal/esp32s3/include/hal/hmac_ll.h b/components/hal/esp32s3/include/hal/hmac_ll.h index ba9cecbaa7..792d316efa 100644 --- a/components/hal/esp32s3/include/hal/hmac_ll.h +++ b/components/hal/esp32s3/include/hal/hmac_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -13,7 +13,10 @@ #pragma once #include /* For size_t type */ +#include + #include "soc/hwcrypto_reg.h" +#include "soc/system_struct.h" #include "hal/hmac_types.h" #define SHA256_BLOCK_SZ 64 @@ -28,6 +31,33 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for HMAC peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void hmac_ll_enable_bus_clock(bool enable) +{ + SYSTEM.perip_clk_en1.crypto_hmac_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define hmac_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; hmac_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the HMAC peripheral module + */ +static inline void hmac_ll_reset_register(void) +{ + SYSTEM.perip_rst_en1.crypto_hmac_rst = 1; + SYSTEM.perip_rst_en1.crypto_hmac_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define hmac_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; hmac_ll_reset_register(__VA_ARGS__) + /** * Makes the peripheral ready for use, after enabling it. */ diff --git a/components/hal/test_apps/crypto/main/ds/test_ds.c b/components/hal/test_apps/crypto/main/ds/test_ds.c index c1146916be..21c09456ad 100644 --- a/components/hal/test_apps/crypto/main/ds/test_ds.c +++ b/components/hal/test_apps/crypto/main/ds/test_ds.c @@ -7,6 +7,7 @@ #include #include +#include "esp_private/esp_crypto_lock_internal.h" #include "memory_checks.h" #include "unity_fixture.h" @@ -127,11 +128,16 @@ _Static_assert(NUM_RESULTS == NUM_MESSAGES, "expected_results size should be the #include "hal/ds_hal.h" #include "hal/ds_ll.h" #include "hal/hmac_hal.h" +#include "hal/hmac_ll.h" static void ds_acquire_enable(void) { - periph_module_enable(PERIPH_HMAC_MODULE); + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(true); + hmac_ll_reset_register(); + } + periph_module_enable(PERIPH_SHA_MODULE); periph_module_enable(PERIPH_DS_MODULE); hmac_hal_start(); @@ -142,7 +148,11 @@ static void ds_disable_release(void) ds_hal_finish(); periph_module_disable(PERIPH_DS_MODULE); periph_module_disable(PERIPH_SHA_MODULE); - periph_module_disable(PERIPH_HMAC_MODULE); + + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(false); + } + } diff --git a/components/hal/test_apps/crypto/main/hmac/test_hmac.c b/components/hal/test_apps/crypto/main/hmac/test_hmac.c index 4ac23497b1..e6c47acd73 100644 --- a/components/hal/test_apps/crypto/main/hmac/test_hmac.c +++ b/components/hal/test_apps/crypto/main/hmac/test_hmac.c @@ -5,6 +5,7 @@ */ #include +#include "esp_private/esp_crypto_lock_internal.h" #include "esp_log.h" #include "memory_checks.h" #include "unity_fixture.h" @@ -39,6 +40,7 @@ static esp_err_t hmac_jtag_disable(void) #if !CONFIG_IDF_TARGET_ESP32S2 #include "hal/hmac_hal.h" +#include "hal/hmac_ll.h" #include "esp_private/periph_ctrl.h" #define SHA256_BLOCK_SZ 64 @@ -70,7 +72,11 @@ static esp_err_t hmac_calculate(hmac_key_id_t key_id, const void *message, size_ { const uint8_t *message_bytes = (const uint8_t *)message; - periph_module_enable(PERIPH_HMAC_MODULE); + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(true); + hmac_ll_reset_register(); + } + periph_module_enable(PERIPH_SHA_MODULE); periph_module_enable(PERIPH_DS_MODULE); @@ -126,7 +132,10 @@ static esp_err_t hmac_calculate(hmac_key_id_t key_id, const void *message, size_ periph_module_disable(PERIPH_DS_MODULE); periph_module_disable(PERIPH_SHA_MODULE); - periph_module_disable(PERIPH_HMAC_MODULE); + + HMAC_RCC_ATOMIC() { + hmac_ll_enable_bus_clock(false); + } return ESP_OK; }