mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
refactor(hal): Created esp_hal_security for security code
This commit is contained in:
@@ -1,658 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "soc/apm_defs.h"
|
||||
#include "soc/tee_reg.h"
|
||||
#include "soc/hp_apm_reg.h"
|
||||
#include "soc/hp_apm_struct.h"
|
||||
#include "soc/lp_apm_reg.h"
|
||||
#include "soc/lp_apm_struct.h"
|
||||
#include "soc/cpu_apm_reg.h"
|
||||
#include "soc/cpu_apm_struct.h"
|
||||
|
||||
#include "soc/pcr_reg.h"
|
||||
#include "soc/interrupts.h"
|
||||
#include "hal/apm_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Helper macros for getting pms attributes field mask for given security mode */
|
||||
#define APM_REGION_PMS_SHIFT(mode) (4U * ((mode) - 1))
|
||||
#define APM_REGION_PMS_MASK(mode) (0x07U << APM_REGION_PMS_SHIFT(mode))
|
||||
#define APM_REGION_PMS_FIELD(mode, pms) ((pms) << APM_REGION_PMS_SHIFT(mode))
|
||||
|
||||
/**
|
||||
* @brief Set security mode for a specific master in HP-TEE
|
||||
*
|
||||
* @param id Master ID
|
||||
* @param mode Security mode to set
|
||||
*/
|
||||
static inline void apm_ll_hp_tee_set_master_sec_mode(apm_master_id_t id, apm_security_mode_t mode)
|
||||
{
|
||||
REG_WRITE(TEE_M0_MODE_CTRL_REG + APM_TEE_MODE_CTRL_OFFSET * id, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Lock master security mode for HP-TEE
|
||||
*
|
||||
* @param id Master ID
|
||||
*/
|
||||
static inline void apm_ll_hp_tee_lock_master_sec_mode(apm_master_id_t id)
|
||||
{
|
||||
REG_SET_BIT(TEE_M0_MODE_CTRL_REG + APM_TEE_MODE_CTRL_OFFSET * id, APM_TEE_MODE_LOCK_BIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable clock gating for HP-TEE
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_hp_tee_enable_clk_gating(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_CLR_BIT(TEE_CLOCK_GATE_REG, TEE_CLK_EN);
|
||||
} else {
|
||||
REG_SET_BIT(TEE_CLOCK_GATE_REG, TEE_CLK_EN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller filter for specific path in HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_enable_ctrl_filter(apm_ctrl_access_path_t path, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(HP_APM_FUNC_CTRL_REG, BIT(path));
|
||||
} else {
|
||||
REG_CLR_BIT(HP_APM_FUNC_CTRL_REG, BIT(path));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable all controller filters in HP-APM
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_enable_ctrl_filter_all(bool enable)
|
||||
{
|
||||
REG_WRITE(HP_APM_FUNC_CTRL_REG, enable ? UINT32_MAX : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable region filter in HP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_enable_region_filter(uint32_t regn_num, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(HP_APM_REGION_FILTER_EN_REG, BIT(regn_num));
|
||||
} else {
|
||||
REG_CLR_BIT(HP_APM_REGION_FILTER_EN_REG, BIT(regn_num));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set region start address in HP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param addr Start address
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_set_region_start_addr(uint32_t regn_num, uint32_t addr)
|
||||
{
|
||||
REG_WRITE(HP_APM_REGION0_ADDR_START_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set region end address in HP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param addr End address
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_set_region_end_addr(uint32_t regn_num, uint32_t addr)
|
||||
{
|
||||
REG_WRITE(HP_APM_REGION0_ADDR_END_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set security mode region attributes in HP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param mode Security mode
|
||||
* @param regn_pms Region PMS attributes
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_set_sec_mode_region_attr(uint32_t regn_num, apm_security_mode_t mode, uint32_t regn_pms)
|
||||
{
|
||||
uint32_t reg = HP_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num;
|
||||
uint32_t val = REG_READ(reg);
|
||||
val &= ~APM_REGION_PMS_MASK(mode);
|
||||
val |= APM_REGION_PMS_FIELD(mode, regn_pms);
|
||||
REG_WRITE(reg, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Lock security mode region attributes in HP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_lock_sec_mode_region_attr(uint32_t regn_num)
|
||||
{
|
||||
REG_SET_BIT(HP_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num, APM_REGION_LOCK_BIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception data (regn, master, security mode) from HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception data
|
||||
*/
|
||||
static inline uint32_t apm_ll_hp_apm_get_excp_data(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return REG_READ(HP_APM_M0_EXCEPTION_INFO0_REG + APM_EXCP_INFO_OFFSET * path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception status from HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception type
|
||||
*/
|
||||
static inline uint32_t apm_ll_hp_apm_get_excp_type(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return REG_READ(HP_APM_M0_STATUS_REG + APM_EXCP_INFO_OFFSET * path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception address from HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception address
|
||||
*/
|
||||
static inline uint32_t apm_ll_hp_apm_get_excp_addr(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return REG_READ(HP_APM_M0_EXCEPTION_INFO1_REG + APM_EXCP_INFO_OFFSET * path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception information from HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param info Pointer to store exception information
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_get_excp_info(apm_ctrl_access_path_t path, apm_ctrl_exception_info_t *info)
|
||||
{
|
||||
hp_apm_m0_exception_info0_reg_t reg;
|
||||
reg.val = apm_ll_hp_apm_get_excp_data(path);
|
||||
info->regn = reg.m0_exception_region;
|
||||
info->mode = reg.m0_exception_mode;
|
||||
info->id = reg.m0_exception_id;
|
||||
|
||||
info->type = apm_ll_hp_apm_get_excp_type(path);
|
||||
info->addr = apm_ll_hp_apm_get_excp_addr(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear controller exception status in HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_clear_ctrl_excp_status(apm_ctrl_access_path_t path)
|
||||
{
|
||||
REG_SET_BIT(HP_APM_M0_STATUS_CLR_REG + APM_EXCP_INFO_OFFSET * path, APM_EXCP_STATUS_CLR_BIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller interrupt in HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_enable_ctrl_intr(apm_ctrl_access_path_t path, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(HP_APM_INT_EN_REG, BIT(path));
|
||||
} else {
|
||||
REG_CLR_BIT(HP_APM_INT_EN_REG, BIT(path));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get controller interrupt source number from HP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Interrupt source number
|
||||
*/
|
||||
static inline int apm_ll_hp_apm_get_ctrl_intr_src(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return ETS_HP_APM_M0_INTR_SOURCE + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller clock gating in HP-APM
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_hp_apm_enable_ctrl_clk_gating(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_CLR_BIT(HP_APM_CLOCK_GATE_REG, HP_APM_CLK_EN);
|
||||
} else {
|
||||
REG_SET_BIT(HP_APM_CLOCK_GATE_REG, HP_APM_CLK_EN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller filter for specific path in LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_enable_ctrl_filter(apm_ctrl_access_path_t path, bool enable)
|
||||
{
|
||||
(void)path;
|
||||
if (enable) {
|
||||
REG_SET_BIT(LP_APM_FUNC_CTRL_REG, LP_APM_M0_FUNC_EN);
|
||||
} else {
|
||||
REG_CLR_BIT(LP_APM_FUNC_CTRL_REG, LP_APM_M0_FUNC_EN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable all controller filters in LP-APM
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_enable_ctrl_filter_all(bool enable)
|
||||
{
|
||||
REG_WRITE(LP_APM_FUNC_CTRL_REG, enable ? UINT32_MAX : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable region filter in LP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_enable_region_filter(uint32_t regn_num, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(LP_APM_REGION_FILTER_EN_REG, BIT(regn_num));
|
||||
} else {
|
||||
REG_CLR_BIT(LP_APM_REGION_FILTER_EN_REG, BIT(regn_num));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set region start address in LP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param addr Start address
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_set_region_start_addr(uint32_t regn_num, uint32_t addr)
|
||||
{
|
||||
REG_WRITE(LP_APM_REGION0_ADDR_START_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set region end address in LP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param addr End address
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_set_region_end_addr(uint32_t regn_num, uint32_t addr)
|
||||
{
|
||||
REG_WRITE(LP_APM_REGION0_ADDR_END_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set security mode region attributes in LP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param mode Security mode
|
||||
* @param regn_pms Region PMS attributes
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_set_sec_mode_region_attr(uint32_t regn_num, apm_security_mode_t mode, uint32_t regn_pms)
|
||||
{
|
||||
uint32_t reg = LP_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num;
|
||||
uint32_t val = REG_READ(reg);
|
||||
val &= ~APM_REGION_PMS_MASK(mode);
|
||||
val |= APM_REGION_PMS_FIELD(mode, regn_pms);
|
||||
REG_WRITE(reg, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Lock security mode region attributes in LP-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_lock_sec_mode_region_attr(uint32_t regn_num)
|
||||
{
|
||||
REG_SET_BIT(LP_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num, APM_REGION_LOCK_BIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception data (regn, master, security mode) from LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception data
|
||||
*/
|
||||
static inline uint32_t apm_ll_lp_apm_get_excp_data(apm_ctrl_access_path_t path)
|
||||
{
|
||||
(void)path;
|
||||
return REG_READ(LP_APM_M0_EXCEPTION_INFO0_REG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception status from LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception type
|
||||
*/
|
||||
static inline uint32_t apm_ll_lp_apm_get_excp_type(apm_ctrl_access_path_t path)
|
||||
{
|
||||
(void)path;
|
||||
return REG_READ(LP_APM_M0_STATUS_REG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception address from LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception address
|
||||
*/
|
||||
static inline uint32_t apm_ll_lp_apm_get_excp_addr(apm_ctrl_access_path_t path)
|
||||
{
|
||||
(void)path;
|
||||
return REG_READ(LP_APM_M0_EXCEPTION_INFO1_REG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception information from LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param info Pointer to store exception information
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_get_excp_info(apm_ctrl_access_path_t path, apm_ctrl_exception_info_t *info)
|
||||
{
|
||||
lp_apm_m0_exception_info0_reg_t reg;
|
||||
reg.val = apm_ll_lp_apm_get_excp_data(path);
|
||||
info->regn = reg.m0_exception_region;
|
||||
info->mode = reg.m0_exception_mode;
|
||||
info->id = reg.m0_exception_id;
|
||||
|
||||
info->type = apm_ll_lp_apm_get_excp_type(path);
|
||||
info->addr = apm_ll_lp_apm_get_excp_addr(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear controller exception status in LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_clear_ctrl_excp_status(apm_ctrl_access_path_t path)
|
||||
{
|
||||
(void)path;
|
||||
REG_SET_BIT(LP_APM_M0_STATUS_CLR_REG, LP_APM_M0_EXCEPTION_STATUS_CLR);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller interrupt in LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_enable_ctrl_intr(apm_ctrl_access_path_t path, bool enable)
|
||||
{
|
||||
(void)path;
|
||||
if (enable) {
|
||||
REG_SET_BIT(LP_APM_INT_EN_REG, LP_APM_M0_APM_INT_EN);
|
||||
} else {
|
||||
REG_CLR_BIT(LP_APM_INT_EN_REG, LP_APM_M0_APM_INT_EN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller clock gating in LP-APM
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_lp_apm_enable_ctrl_clk_gating(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_CLR_BIT(LP_APM_CLOCK_GATE_REG, LP_APM_CLK_EN);
|
||||
} else {
|
||||
REG_SET_BIT(LP_APM_CLOCK_GATE_REG, LP_APM_CLK_EN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get controller interrupt source number from LP-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Interrupt source number
|
||||
*/
|
||||
static inline int apm_ll_lp_apm_get_ctrl_intr_src(apm_ctrl_access_path_t path)
|
||||
{
|
||||
(void)path;
|
||||
return ETS_LP_APM_M0_INTR_SOURCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller filter for specific path in CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_enable_ctrl_filter(apm_ctrl_access_path_t path, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(CPU_APM_FUNC_CTRL_REG, BIT(path));
|
||||
} else {
|
||||
REG_CLR_BIT(CPU_APM_FUNC_CTRL_REG, BIT(path));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable all controller filters in CPU-APM
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_enable_ctrl_filter_all(bool enable)
|
||||
{
|
||||
REG_WRITE(CPU_APM_FUNC_CTRL_REG, enable ? UINT32_MAX : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable region filter in CPU-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_enable_region_filter(uint32_t regn_num, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(CPU_APM_REGION_FILTER_EN_REG, BIT(regn_num));
|
||||
} else {
|
||||
REG_CLR_BIT(CPU_APM_REGION_FILTER_EN_REG, BIT(regn_num));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set region start address in CPU-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param addr Start address
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_set_region_start_addr(uint32_t regn_num, uint32_t addr)
|
||||
{
|
||||
REG_WRITE(CPU_APM_REGION0_ADDR_START_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set region end address in CPU-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param addr End address
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_set_region_end_addr(uint32_t regn_num, uint32_t addr)
|
||||
{
|
||||
REG_WRITE(CPU_APM_REGION0_ADDR_END_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set security mode region attributes in CPU-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
* @param mode Security mode
|
||||
* @param regn_pms Region PMS attributes
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_set_sec_mode_region_attr(uint32_t regn_num, apm_security_mode_t mode, uint32_t regn_pms)
|
||||
{
|
||||
uint32_t reg = CPU_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num;
|
||||
uint32_t val = REG_READ(reg);
|
||||
val &= ~APM_REGION_PMS_MASK(mode);
|
||||
val |= APM_REGION_PMS_FIELD(mode, regn_pms);
|
||||
REG_WRITE(reg, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Lock security mode region attributes in CPU-APM
|
||||
*
|
||||
* @param regn_num Region number
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_lock_sec_mode_region_attr(uint32_t regn_num)
|
||||
{
|
||||
REG_SET_BIT(CPU_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num, APM_REGION_LOCK_BIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception data (regn, master, security mode) from CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception data
|
||||
*/
|
||||
static inline uint32_t apm_ll_cpu_apm_get_excp_data(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return REG_READ(CPU_APM_M0_EXCEPTION_INFO0_REG + APM_EXCP_INFO_OFFSET * path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception status from CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception type
|
||||
*/
|
||||
static inline uint32_t apm_ll_cpu_apm_get_excp_type(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return REG_READ(CPU_APM_M0_STATUS_REG + APM_EXCP_INFO_OFFSET * path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception address from CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Exception address
|
||||
*/
|
||||
static inline uint32_t apm_ll_cpu_apm_get_excp_addr(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return REG_READ(CPU_APM_M0_EXCEPTION_INFO1_REG + APM_EXCP_INFO_OFFSET * path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get exception information from CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param info Pointer to store exception information
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_get_excp_info(apm_ctrl_access_path_t path, apm_ctrl_exception_info_t *info)
|
||||
{
|
||||
cpu_apm_m0_exception_info0_reg_t reg;
|
||||
reg.val = apm_ll_cpu_apm_get_excp_data(path);
|
||||
info->regn = reg.apm_m0_exception_region;
|
||||
info->mode = reg.apm_m0_exception_mode;
|
||||
info->id = reg.apm_m0_exception_id;
|
||||
|
||||
info->type = apm_ll_cpu_apm_get_excp_type(path);
|
||||
info->addr = apm_ll_cpu_apm_get_excp_addr(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear controller exception status in CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_clear_ctrl_excp_status(apm_ctrl_access_path_t path)
|
||||
{
|
||||
REG_SET_BIT(CPU_APM_M0_STATUS_CLR_REG + APM_EXCP_INFO_OFFSET * path, APM_EXCP_STATUS_CLR_BIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller interrupt in CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_enable_ctrl_intr(apm_ctrl_access_path_t path, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(CPU_APM_INT_EN_REG, BIT(path));
|
||||
} else {
|
||||
REG_CLR_BIT(CPU_APM_INT_EN_REG, BIT(path));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable controller clock gating in CPU-APM
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_cpu_apm_enable_ctrl_clk_gating(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_CLR_BIT(CPU_APM_CLOCK_GATE_REG, CPU_APM_CLK_EN);
|
||||
} else {
|
||||
REG_SET_BIT(CPU_APM_CLOCK_GATE_REG, CPU_APM_CLK_EN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get controller interrupt source number from CPU-APM
|
||||
*
|
||||
* @param path Access path
|
||||
* @return Interrupt source number
|
||||
*/
|
||||
static inline int apm_ll_cpu_apm_get_ctrl_intr_src(apm_ctrl_access_path_t path)
|
||||
{
|
||||
return ETS_CPU_APM_M0_INTR_SOURCE + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable/disable APM reset event bypass
|
||||
*
|
||||
* @param enable True to enable, false to disable
|
||||
*/
|
||||
static inline void apm_ll_enable_reset_event_bypass(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(PCR_RESET_EVENT_BYPASS_REG, PCR_RESET_EVENT_BYPASS_APM);
|
||||
} else {
|
||||
REG_CLR_BIT(PCR_RESET_EVENT_BYPASS_REG, PCR_RESET_EVENT_BYPASS_APM);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,256 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "hal/assert.h"
|
||||
#include "hal/ecc_types.h"
|
||||
#include "soc/ecc_mult_reg.h"
|
||||
#include "soc/pcr_struct.h"
|
||||
#include "soc/pcr_reg.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
ECC_PARAM_PX = 0x0,
|
||||
ECC_PARAM_PY,
|
||||
ECC_PARAM_K,
|
||||
ECC_PARAM_QX,
|
||||
ECC_PARAM_QY,
|
||||
ECC_PARAM_QZ,
|
||||
} ecc_ll_param_t;
|
||||
|
||||
/**
|
||||
* @brief Enable the bus clock for ECC peripheral module
|
||||
*
|
||||
* @param true to enable the module, false to disable the module
|
||||
*/
|
||||
static inline void ecc_ll_enable_bus_clock(bool enable)
|
||||
{
|
||||
PCR.ecc_conf.ecc_clk_en = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset the ECC peripheral module
|
||||
*/
|
||||
static inline void ecc_ll_reset_register(void)
|
||||
{
|
||||
PCR.ecc_conf.ecc_rst_en = 1;
|
||||
PCR.ecc_conf.ecc_rst_en = 0;
|
||||
// Clear reset on ECDSA, otherwise ECC is held in reset
|
||||
PCR.ecdsa_conf.ecdsa_rst_en = 0;
|
||||
}
|
||||
|
||||
static inline void ecc_ll_power_up(void)
|
||||
{
|
||||
/* Power up the ECC peripheral (default state is power-down) */
|
||||
REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD);
|
||||
REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PD);
|
||||
}
|
||||
|
||||
static inline void ecc_ll_power_down(void)
|
||||
{
|
||||
/* Power down the ECC peripheral */
|
||||
REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PU);
|
||||
REG_SET_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD);
|
||||
}
|
||||
|
||||
static inline void ecc_ll_enable_interrupt(void)
|
||||
{
|
||||
REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 1);
|
||||
}
|
||||
|
||||
static inline void ecc_ll_disable_interrupt(void)
|
||||
{
|
||||
REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 0);
|
||||
}
|
||||
|
||||
static inline void ecc_ll_clear_interrupt(void)
|
||||
{
|
||||
REG_SET_FIELD(ECC_MULT_INT_CLR_REG, ECC_MULT_CALC_DONE_INT_CLR, 1);
|
||||
}
|
||||
|
||||
static inline void ecc_ll_set_mode(ecc_mode_t mode)
|
||||
{
|
||||
switch(mode) {
|
||||
case ECC_MODE_POINT_MUL:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 0);
|
||||
break;
|
||||
case ECC_MODE_VERIFY:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 2);
|
||||
break;
|
||||
case ECC_MODE_VERIFY_THEN_POINT_MUL:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 3);
|
||||
break;
|
||||
case ECC_MODE_JACOBIAN_POINT_MUL:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 4);
|
||||
break;
|
||||
case ECC_MODE_POINT_ADD:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 5);
|
||||
break;
|
||||
case ECC_MODE_JACOBIAN_POINT_VERIFY:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 6);
|
||||
break;
|
||||
case ECC_MODE_POINT_VERIFY_JACOBIAN_MUL:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 7);
|
||||
break;
|
||||
case ECC_MODE_MOD_ADD:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 8);
|
||||
break;
|
||||
case ECC_MODE_MOD_SUB:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 9);
|
||||
break;
|
||||
case ECC_MODE_MOD_MUL:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 10);
|
||||
break;
|
||||
case ECC_MODE_INVERSE_MUL:
|
||||
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 11);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported mode");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ecc_ll_set_curve(ecc_curve_t curve)
|
||||
{
|
||||
switch(curve) {
|
||||
case ECC_CURVE_SECP256R1:
|
||||
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_KEY_LENGTH);
|
||||
break;
|
||||
case ECC_CURVE_SECP192R1:
|
||||
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_KEY_LENGTH);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported curve");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ecc_ll_set_mod_base(ecc_mod_base_t base)
|
||||
{
|
||||
switch(base) {
|
||||
case ECC_MOD_N:
|
||||
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE);
|
||||
break;
|
||||
case ECC_MOD_P:
|
||||
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported curve");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
|
||||
} else {
|
||||
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ecc_ll_write_param(ecc_ll_param_t param, const uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint32_t reg;
|
||||
uint32_t word;
|
||||
switch (param) {
|
||||
case ECC_PARAM_PX:
|
||||
reg = ECC_MULT_PX_MEM;
|
||||
break;
|
||||
case ECC_PARAM_PY:
|
||||
reg = ECC_MULT_PY_MEM;
|
||||
break;
|
||||
case ECC_PARAM_K:
|
||||
reg = ECC_MULT_K_MEM;
|
||||
break;
|
||||
case ECC_PARAM_QX:
|
||||
reg = ECC_MULT_QX_MEM;
|
||||
break;
|
||||
case ECC_PARAM_QY:
|
||||
reg = ECC_MULT_QY_MEM;
|
||||
break;
|
||||
case ECC_PARAM_QZ:
|
||||
reg = ECC_MULT_QZ_MEM;
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i += 4) {
|
||||
memcpy(&word, buf + i, 4);
|
||||
REG_WRITE(reg + i, word);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ecc_ll_start_calc(void)
|
||||
{
|
||||
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_START);
|
||||
}
|
||||
|
||||
static inline int ecc_ll_is_calc_finished(void)
|
||||
{
|
||||
return REG_GET_FIELD(ECC_MULT_INT_RAW_REG, ECC_MULT_CALC_DONE_INT_RAW);
|
||||
}
|
||||
|
||||
static inline ecc_mode_t ecc_ll_get_mode(void)
|
||||
{
|
||||
return (ecc_mode_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE));
|
||||
}
|
||||
|
||||
static inline int ecc_ll_get_verification_result(void)
|
||||
{
|
||||
return REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_VERIFICATION_RESULT);
|
||||
}
|
||||
|
||||
static inline ecc_curve_t ecc_ll_get_curve(void)
|
||||
{
|
||||
return (ecc_curve_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_KEY_LENGTH));
|
||||
}
|
||||
|
||||
static inline ecc_mod_base_t ecc_ll_get_mod_base(void)
|
||||
{
|
||||
return (ecc_mod_base_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE));
|
||||
}
|
||||
|
||||
static inline void ecc_ll_read_param(ecc_ll_param_t param, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint32_t reg;
|
||||
switch (param) {
|
||||
case ECC_PARAM_PX:
|
||||
reg = ECC_MULT_PX_MEM;
|
||||
break;
|
||||
case ECC_PARAM_PY:
|
||||
reg = ECC_MULT_PY_MEM;
|
||||
break;
|
||||
case ECC_PARAM_K:
|
||||
reg = ECC_MULT_K_MEM;
|
||||
break;
|
||||
case ECC_PARAM_QX:
|
||||
reg = ECC_MULT_QX_MEM;
|
||||
break;
|
||||
case ECC_PARAM_QY:
|
||||
reg = ECC_MULT_QY_MEM;
|
||||
break;
|
||||
case ECC_PARAM_QZ:
|
||||
reg = ECC_MULT_QZ_MEM;
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(buf, (void *)reg, len);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,459 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "hal/assert.h"
|
||||
#include "soc/ecdsa_reg.h"
|
||||
#include "soc/pcr_struct.h"
|
||||
#include "soc/efuse_periph.h"
|
||||
#include "hal/ecdsa_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Memory blocks of ECDSA parameters
|
||||
*/
|
||||
typedef enum {
|
||||
ECDSA_PARAM_R,
|
||||
ECDSA_PARAM_S,
|
||||
ECDSA_PARAM_Z,
|
||||
ECDSA_PARAM_QAX,
|
||||
ECDSA_PARAM_QAY
|
||||
} ecdsa_ll_param_t;
|
||||
|
||||
/**
|
||||
* @brief Interrupt types in ECDSA
|
||||
*/
|
||||
typedef enum {
|
||||
ECDSA_INT_CALC_DONE,
|
||||
ECDSA_INT_SHA_RELEASE,
|
||||
} ecdsa_ll_intr_type_t;
|
||||
|
||||
/**
|
||||
* @brief Stages of ECDSA operation
|
||||
*/
|
||||
typedef enum {
|
||||
ECDSA_STAGE_START_CALC,
|
||||
ECDSA_STAGE_LOAD_DONE,
|
||||
ECDSA_STAGE_GET_DONE
|
||||
} ecdsa_ll_stage_t;
|
||||
|
||||
/**
|
||||
* @brief States of ECDSA peripheral
|
||||
*/
|
||||
typedef enum {
|
||||
ECDSA_STATE_IDLE,
|
||||
ECDSA_STATE_LOAD,
|
||||
ECDSA_STATE_GET,
|
||||
ECDSA_STATE_BUSY
|
||||
} ecdsa_ll_state_t;
|
||||
|
||||
/**
|
||||
* @brief Types of SHA
|
||||
*/
|
||||
typedef enum {
|
||||
ECDSA_SHA_224,
|
||||
ECDSA_SHA_256
|
||||
} ecdsa_ll_sha_type_t;
|
||||
|
||||
/**
|
||||
* @brief Operation modes of SHA
|
||||
*/
|
||||
typedef enum {
|
||||
ECDSA_MODE_SHA_START,
|
||||
ECDSA_MODE_SHA_CONTINUE
|
||||
} ecdsa_ll_sha_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Get the state of ECDSA peripheral
|
||||
*
|
||||
* @return State of ECDSA
|
||||
*/
|
||||
static inline uint32_t ecdsa_ll_get_state(void)
|
||||
{
|
||||
return REG_GET_FIELD(ECDSA_STATE_REG, ECDSA_BUSY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable the bus clock for ECDSA peripheral module
|
||||
*
|
||||
* @param true to enable the module, false to disable the module
|
||||
*/
|
||||
static inline void ecdsa_ll_enable_bus_clock(bool enable)
|
||||
{
|
||||
PCR.ecdsa_conf.ecdsa_clk_en = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset the ECDSA peripheral module
|
||||
*/
|
||||
static inline void ecdsa_ll_reset_register(void)
|
||||
{
|
||||
PCR.ecdsa_conf.ecdsa_rst_en = 1;
|
||||
PCR.ecdsa_conf.ecdsa_rst_en = 0;
|
||||
|
||||
while (ecdsa_ll_get_state() != ECDSA_STATE_IDLE) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable interrupt of a given type
|
||||
*
|
||||
* @param type Interrupt type
|
||||
*/
|
||||
static inline void ecdsa_ll_enable_intr(ecdsa_ll_intr_type_t type)
|
||||
{
|
||||
switch (type) {
|
||||
case ECDSA_INT_CALC_DONE:
|
||||
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_PREP_DONE_INT_ENA, 1);
|
||||
break;
|
||||
case ECDSA_INT_SHA_RELEASE:
|
||||
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_SHA_RELEASE_INT_ENA, 1);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported interrupt type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disable interrupt of a given type
|
||||
*
|
||||
* @param type Interrupt type
|
||||
*/
|
||||
static inline void ecdsa_ll_disable_intr(ecdsa_ll_intr_type_t type)
|
||||
{
|
||||
switch (type) {
|
||||
case ECDSA_INT_CALC_DONE:
|
||||
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_PREP_DONE_INT_ENA, 0);
|
||||
break;
|
||||
case ECDSA_INT_SHA_RELEASE:
|
||||
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_SHA_RELEASE_INT_ENA, 0);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported interrupt type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear interrupt of a given type
|
||||
*
|
||||
* @param type Interrupt type
|
||||
*/
|
||||
static inline void ecdsa_ll_clear_intr(ecdsa_ll_intr_type_t type)
|
||||
{
|
||||
switch (type) {
|
||||
case ECDSA_INT_CALC_DONE:
|
||||
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_PREP_DONE_INT_CLR, 1);
|
||||
break;
|
||||
case ECDSA_INT_SHA_RELEASE:
|
||||
REG_SET_FIELD(ECDSA_INT_CLR_REG, ECDSA_SHA_RELEASE_INT_CLR, 1);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported interrupt type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set working mode of ECDSA
|
||||
*
|
||||
* @param mode Mode of operation
|
||||
*/
|
||||
static inline void ecdsa_ll_set_mode(ecdsa_mode_t mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case ECDSA_MODE_SIGN_VERIFY:
|
||||
REG_SET_FIELD(ECDSA_CONF_REG, ECDSA_WORK_MODE, 0);
|
||||
break;
|
||||
case ECDSA_MODE_SIGN_GEN:
|
||||
REG_SET_FIELD(ECDSA_CONF_REG, ECDSA_WORK_MODE, 1);
|
||||
break;
|
||||
case ECDSA_MODE_EXPORT_PUBKEY:
|
||||
REG_SET_FIELD(ECDSA_CONF_REG, ECDSA_WORK_MODE, 2);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported mode");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set curve for ECDSA operation
|
||||
*
|
||||
* @param curve ECDSA curve
|
||||
*/
|
||||
static inline void ecdsa_ll_set_curve(ecdsa_curve_t curve)
|
||||
{
|
||||
switch (curve) {
|
||||
case ECDSA_CURVE_SECP256R1:
|
||||
REG_SET_BIT(ECDSA_CONF_REG, ECDSA_ECC_CURVE);
|
||||
break;
|
||||
case ECDSA_CURVE_SECP192R1:
|
||||
REG_CLR_BIT(ECDSA_CONF_REG, ECDSA_ECC_CURVE);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported curve");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the source of `Z` (SHA message)
|
||||
*
|
||||
* @param mode Mode of SHA generation
|
||||
*/
|
||||
static inline void ecdsa_ll_set_z_mode(ecdsa_ll_sha_mode_t mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case ECDSA_Z_USE_SHA_PERI:
|
||||
REG_CLR_BIT(ECDSA_CONF_REG, ECDSA_SOFTWARE_SET_Z);
|
||||
break;
|
||||
case ECDSA_Z_USER_PROVIDED:
|
||||
REG_SET_BIT(ECDSA_CONF_REG, ECDSA_SOFTWARE_SET_Z);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported curve");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the signature generation type of ECDSA operation
|
||||
*
|
||||
* @param type Type of the ECDSA signature
|
||||
*/
|
||||
static inline void ecdsa_ll_set_k_type(ecdsa_sign_type_t type)
|
||||
{
|
||||
switch (type) {
|
||||
case ECDSA_K_TYPE_TRNG:
|
||||
REG_CLR_BIT(ECDSA_CONF_REG, ECDSA_DETERMINISTIC_K);
|
||||
break;
|
||||
case ECDSA_K_TYPE_DETERMINISITIC:
|
||||
REG_SET_BIT(ECDSA_CONF_REG, ECDSA_DETERMINISTIC_K);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported K type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the loop number value that is used for deterministic derivation of K
|
||||
*
|
||||
* @param loop_number Loop number for deterministic K
|
||||
*/
|
||||
static inline void ecdsa_ll_set_deterministic_loop(uint16_t loop_number)
|
||||
{
|
||||
REG_SET_FIELD(ECDSA_CONF_REG, ECDSA_DETERMINISTIC_LOOP, loop_number);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the stage of ECDSA operation
|
||||
*
|
||||
* @param stage Stage of operation
|
||||
*/
|
||||
static inline void ecdsa_ll_set_stage(ecdsa_ll_stage_t stage)
|
||||
{
|
||||
switch (stage) {
|
||||
case ECDSA_STAGE_START_CALC:
|
||||
REG_SET_BIT(ECDSA_START_REG, ECDSA_START);
|
||||
break;
|
||||
case ECDSA_STAGE_LOAD_DONE:
|
||||
REG_SET_BIT(ECDSA_START_REG, ECDSA_LOAD_DONE);
|
||||
break;
|
||||
case ECDSA_STAGE_GET_DONE:
|
||||
REG_SET_BIT(ECDSA_START_REG, ECDSA_GET_DONE);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported state");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the SHA type
|
||||
*
|
||||
* @param type Type of SHA
|
||||
*/
|
||||
static inline void ecdsa_ll_sha_set_type(ecdsa_ll_sha_type_t type)
|
||||
{
|
||||
switch (type) {
|
||||
case ECDSA_SHA_224:
|
||||
REG_SET_FIELD(ECDSA_SHA_MODE_REG, ECDSA_SHA_MODE, 1);
|
||||
break;
|
||||
case ECDSA_SHA_256:
|
||||
REG_SET_FIELD(ECDSA_SHA_MODE_REG, ECDSA_SHA_MODE, 2);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the SHA operation mode
|
||||
*
|
||||
* @param mode Mode of SHA operation
|
||||
*/
|
||||
static inline void ecdsa_ll_sha_set_mode(ecdsa_ll_sha_mode_t mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case ECDSA_MODE_SHA_START:
|
||||
REG_SET_BIT(ECDSA_SHA_START_REG, ECDSA_SHA_START);
|
||||
break;
|
||||
case ECDSA_MODE_SHA_CONTINUE:
|
||||
REG_SET_BIT(ECDSA_SHA_CONTINUE_REG, ECDSA_SHA_CONTINUE);
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Unsupported type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if SHA is busy
|
||||
*
|
||||
* @return - true, if SHA is busy
|
||||
* - false, if SHA is IDLE
|
||||
*/
|
||||
static inline bool ecdsa_ll_sha_is_busy(void)
|
||||
{
|
||||
return REG_GET_BIT(ECDSA_SHA_BUSY_REG, ECDSA_SHA_BUSY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write the ECDSA parameter
|
||||
*
|
||||
* @param param Parameter to be written
|
||||
* @param buf Buffer containing data
|
||||
* @param len Length of buffer
|
||||
*/
|
||||
static inline void ecdsa_ll_write_param(ecdsa_ll_param_t param, const uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint32_t reg;
|
||||
uint32_t word;
|
||||
switch (param) {
|
||||
case ECDSA_PARAM_R:
|
||||
reg = ECDSA_R_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_S:
|
||||
reg = ECDSA_S_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_Z:
|
||||
reg = ECDSA_Z_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_QAX:
|
||||
reg = ECDSA_QAX_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_QAY:
|
||||
reg = ECDSA_QAY_MEM;
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i += 4) {
|
||||
memcpy(&word, buf + i, 4);
|
||||
REG_WRITE(reg + i, word);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read the ECDSA parameter
|
||||
*
|
||||
* @param param Parameter to be read
|
||||
* @param buf Buffer where the data will be written
|
||||
* @param len Length of buffer
|
||||
*/
|
||||
static inline void ecdsa_ll_read_param(ecdsa_ll_param_t param, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint32_t reg;
|
||||
switch (param) {
|
||||
case ECDSA_PARAM_R:
|
||||
reg = ECDSA_R_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_S:
|
||||
reg = ECDSA_S_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_Z:
|
||||
reg = ECDSA_Z_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_QAX:
|
||||
reg = ECDSA_QAX_MEM;
|
||||
break;
|
||||
case ECDSA_PARAM_QAY:
|
||||
reg = ECDSA_QAY_MEM;
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(buf, (void *)reg, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the ECDSA operation is successful
|
||||
*
|
||||
* @return - 1, if ECDSA operation succeeds
|
||||
* - 0, otherwise
|
||||
*/
|
||||
static inline int ecdsa_ll_get_operation_result(void)
|
||||
{
|
||||
return REG_GET_BIT(ECDSA_RESULT_REG, ECDSA_OPERATION_RESULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the k value is greater than the curve order.
|
||||
*
|
||||
* @return 0, k value is not greater than the curve order. In this case, the k value is the set k value.
|
||||
* @return 1, k value is greater than than the curve order. In this case, the k value is the set (k mod n).
|
||||
*/
|
||||
static inline int ecdsa_ll_check_k_value(void)
|
||||
{
|
||||
return REG_GET_BIT(ECDSA_RESULT_REG, ECDSA_K_VALUE_WARNING);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the ECDSA deterministic mode is supported
|
||||
*/
|
||||
static inline bool ecdsa_ll_is_deterministic_mode_supported(void)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the ECDSA key block in eFuse
|
||||
*
|
||||
* @param curve ECDSA curve type
|
||||
* @param efuse_blk eFuse block number
|
||||
*/
|
||||
__attribute__((always_inline)) static inline void ecdsa_ll_set_ecdsa_key_blk(ecdsa_curve_t curve, int efuse_blk)
|
||||
{
|
||||
(void) curve;
|
||||
EFUSE0.conf.cfg_ecdsa_blk = efuse_blk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the ECDSA peripheral is supported on this chip revision
|
||||
* For ESP32-C61, ECDSA is always supported
|
||||
*/
|
||||
static inline bool ecdsa_ll_is_supported(void)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "soc/hwcrypto_reg.h"
|
||||
#include "soc/pcr_struct.h"
|
||||
#include "hal/sha_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief Enable the bus clock for SHA peripheral module
|
||||
*
|
||||
* @param enable true to enable the module, false to disable the module
|
||||
*/
|
||||
static inline void sha_ll_enable_bus_clock(bool enable)
|
||||
{
|
||||
PCR.sha_conf.sha_clk_en = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset the SHA peripheral module
|
||||
*/
|
||||
static inline void sha_ll_reset_register(void)
|
||||
{
|
||||
PCR.sha_conf.sha_rst_en = 1;
|
||||
PCR.sha_conf.sha_rst_en = 0;
|
||||
|
||||
// Clear reset on digital signature, hmac and ecdsa also, otherwise SHA is held in reset
|
||||
PCR.ds_conf.ds_rst_en = 0;
|
||||
PCR.hmac_conf.hmac_rst_en = 0;
|
||||
PCR.ecdsa_conf.ecdsa_rst_en = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Load the mode for the SHA engine
|
||||
*
|
||||
* @param sha_type The SHA algorithm type
|
||||
*/
|
||||
static inline void sha_ll_set_mode(esp_sha_type sha_type)
|
||||
{
|
||||
REG_WRITE(SHA_MODE_REG, sha_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start a new SHA block conversions (no initial hash in HW)
|
||||
*
|
||||
* @param sha_type The SHA algorithm type
|
||||
*/
|
||||
static inline void sha_ll_start_block(esp_sha_type sha_type)
|
||||
{
|
||||
(void) sha_type;
|
||||
REG_WRITE(SHA_START_REG, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Continue a SHA block conversion (initial hash in HW)
|
||||
*
|
||||
* @param sha_type The SHA algorithm type
|
||||
*/
|
||||
static inline void sha_ll_continue_block(esp_sha_type sha_type)
|
||||
{
|
||||
(void) sha_type;
|
||||
REG_WRITE(SHA_CONTINUE_REG, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start a new SHA message conversion using DMA (no initial hash in HW)
|
||||
*/
|
||||
static inline void sha_ll_start_dma(void)
|
||||
{
|
||||
REG_WRITE(SHA_DMA_START_REG, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Continue a SHA message conversion using DMA (initial hash in HW)
|
||||
*/
|
||||
static inline void sha_ll_continue_dma(void)
|
||||
{
|
||||
REG_WRITE(SHA_DMA_CONTINUE_REG, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Load the current hash digest to digest register
|
||||
*
|
||||
* @note Happens automatically on ESP32C6
|
||||
*
|
||||
* @param sha_type The SHA algorithm type
|
||||
*/
|
||||
static inline void sha_ll_load(esp_sha_type sha_type)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets the number of message blocks to be hashed
|
||||
*
|
||||
* @note DMA operation only
|
||||
*
|
||||
* @param num_blocks Number of message blocks to process
|
||||
*/
|
||||
static inline void sha_ll_set_block_num(size_t num_blocks)
|
||||
{
|
||||
REG_WRITE(SHA_DMA_BLOCK_NUM_REG, num_blocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the SHA engine is currently busy hashing a block
|
||||
*
|
||||
* @return true SHA engine busy
|
||||
* @return false SHA engine idle
|
||||
*/
|
||||
static inline bool sha_ll_busy(void)
|
||||
{
|
||||
return REG_READ(SHA_BUSY_REG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write a text (message) block to the SHA engine
|
||||
*
|
||||
* @param input_text Input buffer to be written to the SHA engine
|
||||
* @param block_word_len Number of words in block
|
||||
*/
|
||||
static inline void sha_ll_fill_text_block(const void *input_text, size_t block_word_len)
|
||||
{
|
||||
uint32_t *data_words = (uint32_t *)input_text;
|
||||
uint32_t *reg_addr_buf = (uint32_t *)(SHA_M_MEM);
|
||||
|
||||
for (int i = 0; i < block_word_len; i++) {
|
||||
REG_WRITE(®_addr_buf[i], data_words[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read the message digest from the SHA engine
|
||||
*
|
||||
* @param sha_type The SHA algorithm type
|
||||
* @param digest_state Buffer that message digest will be written to
|
||||
* @param digest_word_len Length of the message digest
|
||||
*/
|
||||
static inline void sha_ll_read_digest(esp_sha_type sha_type, void *digest_state, size_t digest_word_len)
|
||||
{
|
||||
uint32_t *digest_state_words = (uint32_t *)digest_state;
|
||||
const size_t REG_WIDTH = sizeof(uint32_t);
|
||||
|
||||
for (size_t i = 0; i < digest_word_len; i++) {
|
||||
digest_state_words[i] = REG_READ(SHA_H_MEM + (i * REG_WIDTH));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write the message digest to the SHA engine
|
||||
*
|
||||
* @param sha_type The SHA algorithm type
|
||||
* @param digest_state Message digest to be written to SHA engine
|
||||
* @param digest_word_len Length of the message digest
|
||||
*/
|
||||
static inline void sha_ll_write_digest(esp_sha_type sha_type, void *digest_state, size_t digest_word_len)
|
||||
{
|
||||
uint32_t *digest_state_words = (uint32_t *)digest_state;
|
||||
uint32_t *reg_addr_buf = (uint32_t *)(SHA_H_MEM);
|
||||
|
||||
for (int i = 0; i < digest_word_len; i++) {
|
||||
REG_WRITE(®_addr_buf[i], digest_state_words[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user