mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
feat(hmac): use RCC atomic block to enable/reset the HMAC peripheral
This commit is contained in:
parent
1c6ff8ce9f
commit
18b93e9564
@ -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();
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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
|
||||
|
@ -12,9 +12,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
@ -13,9 +13,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
@ -13,9 +13,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
@ -13,9 +13,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
@ -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 <stddef.h> /* For size_t type */
|
||||
#include <stdbool.h>
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user