diff --git a/components/esp_rom/include/esp32p4/rom/sha.h b/components/esp_rom/include/esp32p4/rom/sha.h index e9a299f1fe..c4dec1f9c2 100644 --- a/components/esp_rom/include/esp32p4/rom/sha.h +++ b/components/esp_rom/include/esp32p4/rom/sha.h @@ -18,6 +18,11 @@ typedef enum { SHA1 = 0, SHA2_224, SHA2_256, + SHA2_384, + SHA2_512, + SHA2_512224, + SHA2_512256, + SHA2_512T, SHA_TYPE_MAX } SHA_TYPE; diff --git a/components/hal/esp32p4/include/hal/sha_ll.h b/components/hal/esp32p4/include/hal/sha_ll.h new file mode 100644 index 0000000000..ddebeb4ad8 --- /dev/null +++ b/components/hal/esp32p4/include/hal/sha_ll.h @@ -0,0 +1,168 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "soc/hwcrypto_reg.h" +#include "hal/sha_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @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) +{ + REG_WRITE(SHA_MODE_REG, 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) +{ + REG_WRITE(SHA_MODE_REG, sha_type); + REG_WRITE(SHA_CONTINUE_REG, 1); +} + +/** + * @brief Start a new SHA message conversion using DMA (no initial hash in HW) + * + * @param sha_type The SHA algorithm type + */ +static inline void sha_ll_start_dma(esp_sha_type sha_type) +{ + REG_WRITE(SHA_MODE_REG, sha_type); + REG_WRITE(SHA_DMA_START_REG, 1); +} + +/** + * @brief Continue a SHA message conversion using DMA (initial hash in HW) + * + * @param sha_type The SHA algorithm type + */ +static inline void sha_ll_continue_dma(esp_sha_type sha_type) +{ + REG_WRITE(SHA_MODE_REG, sha_type); + REG_WRITE(SHA_DMA_CONTINUE_REG, 1); +} + +/** + * @brief Load the current hash digest to digest register + * + * @note Happens automatically on ESP32P4 + * + * @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]); + } +} + +/** + * @brief Sets SHA512_t T_string parameter + * + * @param t_string T_string parameter + */ +static inline void sha_ll_t_string_set(uint32_t t_string) +{ + REG_WRITE(SHA_T_STRING_REG, t_string); +} + +/** + * @brief Sets SHA512_t T_string parameter's length + * + * @param t_len T_string parameter length + */ +static inline void sha_ll_t_len_set(uint8_t t_len) +{ + REG_WRITE(SHA_T_LENGTH_REG, t_len); +} + +#ifdef __cplusplus +} +#endif diff --git a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in index e1431d210f..dd7f9cdaa8 100644 --- a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in @@ -143,6 +143,10 @@ config SOC_MPI_SUPPORTED bool default y +config SOC_SHA_SUPPORTED + bool + default y + config SOC_HMAC_SUPPORTED bool default y @@ -1015,6 +1019,26 @@ config SOC_SHA_SUPPORT_SHA256 bool default y +config SOC_SHA_SUPPORT_SHA384 + bool + default y + +config SOC_SHA_SUPPORT_SHA512 + bool + default y + +config SOC_SHA_SUPPORT_SHA512_224 + bool + default y + +config SOC_SHA_SUPPORT_SHA512_256 + bool + default y + +config SOC_SHA_SUPPORT_SHA512_T + bool + default y + config SOC_ECDSA_SUPPORT_EXPORT_PUBKEY bool default y diff --git a/components/soc/esp32p4/include/soc/sha_reg.h b/components/soc/esp32p4/include/soc/sha_reg.h index 11181a16e1..888da914b3 100644 --- a/components/soc/esp32p4/include/soc/sha_reg.h +++ b/components/soc/esp32p4/include/soc/sha_reg.h @@ -23,6 +23,30 @@ extern "C" { #define SHA_MODE_V 0x00000007U #define SHA_MODE_S 0 +/** SHA_T_STRING_REG register + * SHA 512/t configuration register 0. + */ +#define SHA_T_STRING_REG (DR_REG_SHA_BASE + 0x4) +/** SHA_T_STRING : R/W; bitpos: [31:0]; default: 0; + * Sha t_string (used if and only if mode == SHA_512/t). + */ +#define SHA_T_STRING 0xFFFFFFFFU +#define SHA_T_STRING_M (SHA_T_STRING_V << SHA_T_STRING_S) +#define SHA_T_STRING_V 0xFFFFFFFFU +#define SHA_T_STRING_S 0 + +/** SHA_T_LENGTH_REG register + * SHA 512/t configuration register 1. + */ +#define SHA_T_LENGTH_REG (DR_REG_SHA_BASE + 0x8) +/** SHA_T_LENGTH : R/W; bitpos: [5:0]; default: 0; + * Sha t_length (used if and only if mode == SHA_512/t). + */ +#define SHA_T_LENGTH 0x0000003FU +#define SHA_T_LENGTH_M (SHA_T_LENGTH_V << SHA_T_LENGTH_S) +#define SHA_T_LENGTH_V 0x0000003FU +#define SHA_T_LENGTH_S 0 + /** SHA_DMA_BLOCK_NUM_REG register * DMA configuration register 0. */ diff --git a/components/soc/esp32p4/include/soc/sha_struct.h b/components/soc/esp32p4/include/soc/sha_struct.h index efdd1179f4..016cb8844c 100644 --- a/components/soc/esp32p4/include/soc/sha_struct.h +++ b/components/soc/esp32p4/include/soc/sha_struct.h @@ -25,6 +25,33 @@ typedef union { uint32_t val; } sha_mode_reg_t; +/** Type of t_string register + * SHA 512/t configuration register 0. + */ +typedef union { + struct { + /** t_string : R/W; bitpos: [31:0]; default: 0; + * Sha t_string (used if and only if mode == SHA_512/t). + */ + uint32_t t_string:32; + }; + uint32_t val; +} sha_t_string_reg_t; + +/** Type of t_length register + * SHA 512/t configuration register 1. + */ +typedef union { + struct { + /** t_length : R/W; bitpos: [5:0]; default: 0; + * Sha t_length (used if and only if mode == SHA_512/t). + */ + uint32_t t_length:6; + uint32_t reserved_6:26; + }; + uint32_t val; +} sha_t_length_reg_t; + /** Type of dma_block_num register * DMA configuration register 0. */ @@ -62,7 +89,7 @@ typedef union { /** continue : RO; bitpos: [31:1]; default: 0; * Reserved. */ - uint32_t conti:31; + uint32_t continue:31; }; uint32_t val; } sha_continue_reg_t; @@ -162,10 +189,11 @@ typedef union { typedef struct { volatile sha_mode_reg_t mode; - uint32_t reserved_004[2]; + volatile sha_t_string_reg_t t_string; + volatile sha_t_length_reg_t t_length; volatile sha_dma_block_num_reg_t dma_block_num; volatile sha_start_reg_t start; - volatile sha_continue_reg_t conti; + volatile sha_continue_reg_t continue; volatile sha_busy_reg_t busy; volatile sha_dma_start_reg_t dma_start; volatile sha_dma_continue_reg_t dma_continue; diff --git a/components/soc/esp32p4/include/soc/soc_caps.h b/components/soc/esp32p4/include/soc/soc_caps.h index d08762bff8..1aef3b8b98 100644 --- a/components/soc/esp32p4/include/soc/soc_caps.h +++ b/components/soc/esp32p4/include/soc/soc_caps.h @@ -57,7 +57,7 @@ #define SOC_SYSTIMER_SUPPORTED 1 #define SOC_AES_SUPPORTED 1 #define SOC_MPI_SUPPORTED 1 -// #define SOC_SHA_SUPPORTED 1 //TODO: IDF-7541 +#define SOC_SHA_SUPPORTED 1 #define SOC_HMAC_SUPPORTED 1 #define SOC_DIG_SIGN_SUPPORTED 1 #define SOC_ECC_SUPPORTED 1 @@ -426,6 +426,11 @@ #define SOC_SHA_SUPPORT_SHA1 (1) #define SOC_SHA_SUPPORT_SHA224 (1) #define SOC_SHA_SUPPORT_SHA256 (1) +#define SOC_SHA_SUPPORT_SHA384 (1) +#define SOC_SHA_SUPPORT_SHA512 (1) +#define SOC_SHA_SUPPORT_SHA512_224 (1) +#define SOC_SHA_SUPPORT_SHA512_256 (1) +#define SOC_SHA_SUPPORT_SHA512_T (1) /*--------------------------- ECDSA CAPS ---------------------------------------*/ #define SOC_ECDSA_SUPPORT_EXPORT_PUBKEY (1)