feat(hmac): use RCC atomic block to enable/reset the HMAC peripheral

This commit is contained in:
harshal.patil 2023-09-15 16:58:40 +05:30
parent 1c6ff8ce9f
commit 18b93e9564
No known key found for this signature in database
GPG Key ID: 5B5EC97C35B9A2E5
10 changed files with 181 additions and 10 deletions

View File

@ -14,6 +14,7 @@
#include "esp_timer.h" #include "esp_timer.h"
#include "esp_ds.h" #include "esp_ds.h"
#include "esp_crypto_lock.h" #include "esp_crypto_lock.h"
#include "esp_private/esp_crypto_lock_internal.h"
#include "esp_hmac.h" #include "esp_hmac.h"
#include "esp_memory_utils.h" #include "esp_memory_utils.h"
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
@ -26,6 +27,7 @@
#include "hal/ds_hal.h" #include "hal/ds_hal.h"
#include "hal/ds_ll.h" #include "hal/ds_ll.h"
#include "hal/hmac_hal.h" #include "hal/hmac_hal.h"
#include "hal/hmac_ll.h"
#endif /* !CONFIG_IDF_TARGET_ESP32S2 */ #endif /* !CONFIG_IDF_TARGET_ESP32S2 */
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
@ -258,7 +260,11 @@ static void ds_acquire_enable(void)
esp_crypto_mpi_lock_acquire(); esp_crypto_mpi_lock_acquire();
#endif #endif
// We also enable SHA and HMAC here. SHA is used by HMAC, HMAC is used by DS. // 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_SHA_MODULE);
periph_module_enable(PERIPH_DS_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_DS_MODULE);
periph_module_disable(PERIPH_SHA_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 #if CONFIG_IDF_TARGET_ESP32S3
esp_crypto_mpi_lock_release(); esp_crypto_mpi_lock_release();

View File

@ -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 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -13,11 +13,13 @@
#include "esp_hmac.h" #include "esp_hmac.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_crypto_lock.h" #include "esp_crypto_lock.h"
#include "esp_private/esp_crypto_lock_internal.h"
#include "soc/hwcrypto_reg.h" #include "soc/hwcrypto_reg.h"
#include "soc/system_reg.h" #include "soc/system_reg.h"
#if !CONFIG_IDF_TARGET_ESP32S2 #if !CONFIG_IDF_TARGET_ESP32S2
#include "hal/hmac_hal.h" #include "hal/hmac_hal.h"
#include "hal/hmac_ll.h"
#include "esp_private/periph_ctrl.h" #include "esp_private/periph_ctrl.h"
#endif #endif
@ -67,7 +69,11 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
esp_crypto_hmac_lock_acquire(); 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. // 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_SHA_MODULE);
periph_module_enable(PERIPH_DS_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_DS_MODULE);
periph_module_disable(PERIPH_SHA_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(); esp_crypto_hmac_lock_release();

View File

@ -16,9 +16,11 @@ extern "C" {
#if SOC_RCC_IS_INDEPENDENT #if SOC_RCC_IS_INDEPENDENT
#define MPI_RCC_ATOMIC() #define MPI_RCC_ATOMIC()
#define ECC_RCC_ATOMIC() #define ECC_RCC_ATOMIC()
#define HMAC_RCC_ATOMIC()
#else /* !SOC_RCC_IS_INDEPENDENT */ #else /* !SOC_RCC_IS_INDEPENDENT */
#define MPI_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #define MPI_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
#define ECC_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #define ECC_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
#define HMAC_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
#endif /* SOC_RCC_IS_INDEPENDENT */ #endif /* SOC_RCC_IS_INDEPENDENT */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -12,9 +12,11 @@
#pragma once #pragma once
#include <stdbool.h>
#include <string.h> #include <string.h>
#include "soc/system_reg.h" #include "soc/system_reg.h"
#include "soc/system_struct.h"
#include "soc/hwcrypto_reg.h" #include "soc/hwcrypto_reg.h"
#include "hal/hmac_types.h" #include "hal/hmac_types.h"
@ -30,6 +32,33 @@
extern "C" { extern "C" {
#endif #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. * Makes the peripheral ready for use, after enabling it.
*/ */

View File

@ -13,9 +13,11 @@
#pragma once #pragma once
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "soc/system_reg.h" #include "soc/system_reg.h"
#include "soc/hwcrypto_reg.h" #include "soc/hwcrypto_reg.h"
#include "soc/pcr_struct.h"
#include "hal/hmac_types.h" #include "hal/hmac_types.h"
#define SHA256_BLOCK_SZ 64 #define SHA256_BLOCK_SZ 64
@ -30,6 +32,25 @@
extern "C" { extern "C" {
#endif #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. * Makes the peripheral ready for use, after enabling it.
*/ */

View File

@ -13,9 +13,11 @@
#pragma once #pragma once
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "soc/system_reg.h" #include "soc/system_reg.h"
#include "soc/hwcrypto_reg.h" #include "soc/hwcrypto_reg.h"
#include "soc/pcr_struct.h"
#include "hal/hmac_types.h" #include "hal/hmac_types.h"
#define SHA256_BLOCK_SZ 64 #define SHA256_BLOCK_SZ 64
@ -30,6 +32,25 @@
extern "C" { extern "C" {
#endif #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. * Makes the peripheral ready for use, after enabling it.
*/ */

View File

@ -13,9 +13,11 @@
#pragma once #pragma once
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "soc/system_reg.h" #include "soc/system_reg.h"
#include "soc/hwcrypto_reg.h" #include "soc/hwcrypto_reg.h"
#include "soc/hp_sys_clkrst_struct.h"
#include "hal/hmac_hal.h" #include "hal/hmac_hal.h"
#define SHA256_BLOCK_SZ 64 #define SHA256_BLOCK_SZ 64
@ -30,6 +32,35 @@
extern "C" { extern "C" {
#endif #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. * Makes the peripheral ready for use, after enabling it.
*/ */

View File

@ -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 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -13,7 +13,10 @@
#pragma once #pragma once
#include <stddef.h> /* For size_t type */ #include <stddef.h> /* For size_t type */
#include <stdbool.h>
#include "soc/hwcrypto_reg.h" #include "soc/hwcrypto_reg.h"
#include "soc/system_struct.h"
#include "hal/hmac_types.h" #include "hal/hmac_types.h"
#define SHA256_BLOCK_SZ 64 #define SHA256_BLOCK_SZ 64
@ -28,6 +31,33 @@
extern "C" { extern "C" {
#endif #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. * Makes the peripheral ready for use, after enabling it.
*/ */

View File

@ -7,6 +7,7 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "esp_private/esp_crypto_lock_internal.h"
#include "memory_checks.h" #include "memory_checks.h"
#include "unity_fixture.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_hal.h"
#include "hal/ds_ll.h" #include "hal/ds_ll.h"
#include "hal/hmac_hal.h" #include "hal/hmac_hal.h"
#include "hal/hmac_ll.h"
static void ds_acquire_enable(void) 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_SHA_MODULE);
periph_module_enable(PERIPH_DS_MODULE); periph_module_enable(PERIPH_DS_MODULE);
hmac_hal_start(); hmac_hal_start();
@ -142,7 +148,11 @@ static void ds_disable_release(void)
ds_hal_finish(); ds_hal_finish();
periph_module_disable(PERIPH_DS_MODULE); periph_module_disable(PERIPH_DS_MODULE);
periph_module_disable(PERIPH_SHA_MODULE); periph_module_disable(PERIPH_SHA_MODULE);
periph_module_disable(PERIPH_HMAC_MODULE);
HMAC_RCC_ATOMIC() {
hmac_ll_enable_bus_clock(false);
}
} }

View File

@ -5,6 +5,7 @@
*/ */
#include <string.h> #include <string.h>
#include "esp_private/esp_crypto_lock_internal.h"
#include "esp_log.h" #include "esp_log.h"
#include "memory_checks.h" #include "memory_checks.h"
#include "unity_fixture.h" #include "unity_fixture.h"
@ -39,6 +40,7 @@ static esp_err_t hmac_jtag_disable(void)
#if !CONFIG_IDF_TARGET_ESP32S2 #if !CONFIG_IDF_TARGET_ESP32S2
#include "hal/hmac_hal.h" #include "hal/hmac_hal.h"
#include "hal/hmac_ll.h"
#include "esp_private/periph_ctrl.h" #include "esp_private/periph_ctrl.h"
#define SHA256_BLOCK_SZ 64 #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; 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_SHA_MODULE);
periph_module_enable(PERIPH_DS_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_DS_MODULE);
periph_module_disable(PERIPH_SHA_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; return ESP_OK;
} }