mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
refactor(esp_hal_security): Updated esp_hal_security build and includes
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2020-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 "hal/ecc_types.h"
|
||||
#include "soc/ecc_mult_reg.h"
|
||||
#include "soc/system_struct.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
ECC_PARAM_PX = 0x0,
|
||||
ECC_PARAM_PY,
|
||||
ECC_PARAM_K,
|
||||
} 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)
|
||||
{
|
||||
SYSTEM.perip_clk_en1.crypto_ecc_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 ecc_ll_enable_bus_clock(...) do { \
|
||||
(void)__DECLARE_RCC_ATOMIC_ENV; \
|
||||
ecc_ll_enable_bus_clock(__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
* @brief Reset the ECC peripheral module
|
||||
*/
|
||||
static inline void ecc_ll_reset_register(void)
|
||||
{
|
||||
SYSTEM.perip_rst_en1.crypto_ecc_rst = 1;
|
||||
SYSTEM.perip_rst_en1.crypto_ecc_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 ecc_ll_reset_register(...) do { \
|
||||
(void)__DECLARE_RCC_ATOMIC_ENV; \
|
||||
ecc_ll_reset_register(__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
static inline void ecc_ll_power_up(void) {}
|
||||
static inline void ecc_ll_power_down(void) {}
|
||||
|
||||
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;
|
||||
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_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_1_REG;
|
||||
break;
|
||||
case ECC_PARAM_PY:
|
||||
reg = ECC_MULT_PY_1_REG;
|
||||
break;
|
||||
case ECC_PARAM_K:
|
||||
reg = ECC_MULT_K_1_REG;
|
||||
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 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_1_REG;
|
||||
break;
|
||||
case ECC_PARAM_PY:
|
||||
reg = ECC_MULT_PY_1_REG;
|
||||
break;
|
||||
case ECC_PARAM_K:
|
||||
reg = ECC_MULT_K_1_REG;
|
||||
break;
|
||||
default:
|
||||
HAL_ASSERT(false && "Invalid parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(buf, (void *)reg, len);
|
||||
}
|
||||
|
||||
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
|
||||
{
|
||||
// Not supported for ESP32-C2
|
||||
(void) enable; //unused
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "soc/hwcrypto_reg.h"
|
||||
#include "soc/system_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)
|
||||
{
|
||||
SYSTEM.perip_clk_en1.crypto_sha_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 sha_ll_enable_bus_clock(...) do { \
|
||||
(void)__DECLARE_RCC_ATOMIC_ENV; \
|
||||
sha_ll_enable_bus_clock(__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
* @brief Reset the SHA peripheral module
|
||||
*/
|
||||
static inline void sha_ll_reset_register(void)
|
||||
{
|
||||
SYSTEM.perip_rst_en1.crypto_sha_rst = 1;
|
||||
SYSTEM.perip_rst_en1.crypto_sha_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 sha_ll_reset_register(...) do { \
|
||||
(void)__DECLARE_RCC_ATOMIC_ENV; \
|
||||
sha_ll_reset_register(__VA_ARGS__); \
|
||||
} while(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 ESP32S3
|
||||
*
|
||||
* @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_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_TEXT_BASE);
|
||||
|
||||
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_BASE + (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_BASE);
|
||||
|
||||
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