xtensa/esp32: Add support for hardware accelerated SHA

This enables the use of the cryptographic accelerator within
the ESP32. The support algorithms are: SHA1, SHA256, SHA384
and SHA512.

Signed-off-by: Vlad Pruteanu <pruteanuvlad1611@yahoo.com>
This commit is contained in:
Vlad Pruteanu
2025-03-08 18:26:33 +02:00
committed by Xiang Xiao
parent de0c697db8
commit 82effd4b2f
9 changed files with 1272 additions and 1 deletions
@@ -360,6 +360,13 @@ disables the NuttShell to get the best possible score.
.. note:: As the NSH is disabled, the application will start as soon as the
system is turned on.
crypto
--------
This configuration enables support for the cryptographic hardware and
the /dev/crypto device file. Currently, only the hashing operation is
supported.
cxx
---
@@ -417,7 +417,7 @@ RSA No
RTC Yes
SD/MMC No
SDIO No
SHA No
SHA Yes
SPI Yes
SPIFLASH Yes
SPIRAM Yes
+5
View File
@@ -794,6 +794,11 @@ config ESP32_TWAI0
config ESP32_AES_ACCELERATOR
bool "AES Accelerator"
default n
config ESP32_SHA_ACCELERATOR
bool "SHA Accelerator"
default n
---help---
Enable ESP32 SHA accelerator support.
config ESP32_PID
bool "PID Controller"
+8
View File
@@ -192,6 +192,14 @@ ifeq ($(CONFIG_ESP32_AES_ACCELERATOR),y)
CHIP_CSRCS += esp32_aes.c
endif
ifeq ($(CONFIG_ESP32_SHA_ACCELERATOR),y)
CHIP_CSRCS += esp32_sha.c
endif
ifeq ($(CONFIG_CRYPTO_CRYPTODEV_HARDWARE),y)
CHIP_CSRCS += esp32_crypto.c
endif
ifeq ($(CONFIG_ESP32_RTC),y)
CHIP_CSRCS += esp32_rtc.c
ifeq ($(CONFIG_RTC_DRIVER),y)
File diff suppressed because it is too large Load Diff
+399
View File
@@ -0,0 +1,399 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_sha.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_ESP32_SHA_ACCELERATOR
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <debug.h>
#include <nuttx/mutex.h>
#include "xtensa.h"
#include "hal/sha_hal.h"
#include "periph_ctrl.h"
#include "esp32_sha.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static bool g_sha_inited;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: esp32_sha_block
*
* Description:
* Performs SHA on multiple blocks at a time.
*
* Input Parameters:
* ctx - The SHA context
* data - Input message to be hashed on single block
* len - Length of the input message on single block
* buf - Input message to be hashed on multiple blocks
* buf_len - Length of the input message on multiple blocks
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
static int esp32_sha_block(struct esp32_sha_context_s *ctx,
const unsigned char *buffer)
{
sha_hal_hash_block(ctx->mode, (const void *)buffer,
ctx->block_size / 32, ctx->first_block);
ctx->first_block = false;
sha_hal_wait_idle();
if (ctx->final_block)
{
sha_hal_read_digest(ctx->mode, ctx->state);
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: esp32_sha1_starts
*
* Description:
* Starts a SHA-1 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha1_starts(struct esp32_sha_context_s *ctx)
{
memset(ctx, 0, sizeof(struct esp32_sha_context_s));
ctx->mode = ESP32_SHA1_1;
ctx->output_size = 160;
ctx->block_size = 512;
return OK;
}
/****************************************************************************
* Name: esp32_sha_update
*
* Description:
* Feeds an input buffer into an ongoing SHA checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to use
* input - The buffer holding the input data
* ilen - The length of the input data in Bytes
*
* Returned Value:
* OK is returned on success.
* Otherwise, a negated errno value is returned.
*
****************************************************************************/
int esp32_sha_update(struct esp32_sha_context_s *ctx,
const unsigned char *input,
size_t ilen)
{
unsigned int i;
unsigned int j;
j = (uint32_t)((ctx->count[0] >> 3) & ((ctx->block_size >> 3) - 1));
ctx->count[0] += (ilen << 3);
if (ctx->count[0] < (ilen << 3))
{
if (ctx->mode == ESP32_SHA1_1 || ctx->mode == ESP32_SHA2_256)
return ERROR;
ctx->count[1]++;
}
if ((j + ilen) > ((ctx->block_size >> 3) - 1))
{
memcpy(&ctx->buffer[j], input, (i = (ctx->block_size >> 3) - j));
if (ctx->sha_state == ESP32_SHA_STATE_INIT)
{
ctx->first_block = true;
ctx->sha_state = ESP32_SHA_STATE_IN_PROCESS;
}
else if (ctx->sha_state == ESP32_SHA_STATE_IN_PROCESS)
{
ctx->first_block = false;
}
esp32_sha_block(ctx, ctx->buffer);
for (; i + ((ctx->block_size >> 3) - 1) < ilen;
i += (ctx->block_size >> 3))
esp32_sha_block(ctx, &input[i]);
j = 0;
}
else
{
i = 0;
}
memcpy(&ctx->buffer[j], &input[i], ilen - i);
return OK;
}
/****************************************************************************
* Name: esp32_sha_finish
*
* Description:
* Finishes the SHA operation,
* and writes the result to the output buffer.
*
* Input Parameters:
* ctx - The SHA context to use
* output - The SHA-1 checksum result
*
* Returned Value:
* OK is returned on success.
* Otherwise, a negated errno value is returned.
*
****************************************************************************/
int esp32_sha_finish(struct esp32_sha_context_s *ctx,
unsigned char output[64])
{
uint32_t aux;
unsigned int i;
unsigned char finalcount[16];
if (ctx->mode == ESP32_SHA1_1 || ctx->mode == ESP32_SHA2_256)
for (i = 0; i < 8; i++)
finalcount[i] = (unsigned char)((ctx->count[0] >>
((8 - 1 - i) * 8)) & 255);
/* SHA384 and SHA512 use 1024 bits to store the message length */
else
{
for (i = 0; i < 8; i++)
finalcount[i] = (unsigned char)((ctx->count[1] >>
((8 - 1 - i) * 8)) & 255);
for (i = 8; i < 16; i++)
finalcount[i] = (unsigned char)((ctx->count[0] >>
((16 - 1 - i) * 8)) & 255);
}
esp32_sha_update(ctx, (unsigned char *)"\200", 1);
while ((ctx->count[0] & (ctx->block_size - 8)) !=
ctx->block_size - (ctx->block_size >> 3))
esp32_sha_update(ctx, (unsigned char *)"\0", 1);
ctx->final_block = true;
if (ctx->mode == ESP32_SHA1_1 || ctx->mode == ESP32_SHA2_256)
esp32_sha_update(ctx, finalcount, 8);
/* SHA384 and SHA512 use 1024 bits to store the message length */
else
esp32_sha_update(ctx, finalcount, 16);
if (ctx->mode == ESP32_SHA3_384 || ctx->mode == ESP32_SHA3_512)
{
/* For these ciphers swap each pair of words */
for (i = 0; i < ctx->output_size / 32; i += 2)
{
aux = ctx->state[i + 1];
ctx->state[i + 1] = ctx->state[i];
ctx->state[i] = aux;
}
}
for (i = 0; i < ctx->output_size / 8; i++)
{
output[i] = (unsigned char)((ctx->state[i >> 2] >>
((3 - (i & 3)) * 8)) & 255);
}
explicit_bzero(&finalcount, sizeof(finalcount));
explicit_bzero(ctx, sizeof(*ctx));
return 0;
}
/****************************************************************************
* Name: esp32_sha1_free
*
* Description:
* Clears a SHA context.
*
* Input Parameters:
* ctx - The SHA context to clear
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32_sha1_free(struct esp32_sha_context_s *ctx)
{
if (ctx == NULL)
return;
memset(ctx, 0, sizeof(struct esp32_sha_context_s));
}
/****************************************************************************
* Name: esp32_sha256_starts
*
* Description:
* Starts a SHA-256 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha256_starts(struct esp32_sha_context_s *ctx)
{
memset(ctx, 0, sizeof(struct esp32_sha_context_s));
ctx->mode = ESP32_SHA2_256;
ctx->output_size = 256;
ctx->block_size = 512;
return OK;
}
/****************************************************************************
* Name: esp32_sha384_starts
*
* Description:
* Starts a SHA-384 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha384_starts(struct esp32_sha_context_s *ctx)
{
memset(ctx, 0, sizeof(struct esp32_sha_context_s));
ctx->mode = ESP32_SHA3_384;
ctx->output_size = 384;
ctx->block_size = 1024;
return OK;
}
/****************************************************************************
* Name: esp32_sha512_starts
*
* Description:
* Starts a SHA-512 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha512_starts(struct esp32_sha_context_s *ctx)
{
memset(ctx, 0, sizeof(struct esp32_sha_context_s));
ctx->mode = ESP32_SHA3_512;
ctx->output_size = 512;
ctx->block_size = 1024;
return OK;
}
/****************************************************************************
* Name: esp32_sha_init
*
* Description:
* Initialize ESP32 SHA hardware.
*
* Input Parameters:
* None
*
* Returned Value:
* OK is returned on success.
* Otherwise, a negated errno value is returned.
*
****************************************************************************/
int esp32_sha_init(void)
{
if (!g_sha_inited)
{
periph_module_enable(PERIPH_SHA_MODULE);
g_sha_inited = true;
}
else
{
return -EBUSY;
}
return OK;
}
#endif
+226
View File
@@ -0,0 +1,226 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_sha.h
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_ESP32_SHA_H
#define __ARCH_XTENSA_SRC_ESP32_ESP32_SHA_H
/****************************************************************************
* Included Files
****************************************************************************/
#ifndef __ASSEMBLY__
#ifdef __cplusplus
extern "C"
{
#endif
#include <nuttx/config.h>
#include <stdint.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
enum esp32_sha_type_e
{
ESP32_SHA1_1 = 0,
ESP32_SHA2_256,
ESP32_SHA3_384,
ESP32_SHA3_512,
ESP32_SHA_TYPE_MAX
};
enum esp32_sha_state_e
{
ESP32_SHA_STATE_INIT,
ESP32_SHA_STATE_IN_PROCESS
};
/* SHA context structure */
struct esp32_sha_context_s
{
uint64_t count[2]; /* number of bits processed */
uint32_t state[16]; /* intermediate digest state */
bool final_block;
unsigned char buffer[128]; /* data block being processed */
bool first_block; /* if first then true else false */
uint16_t output_size;
uint16_t block_size;
enum esp32_sha_type_e mode;
enum esp32_sha_state_e sha_state;
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Functions Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32_sha_init
*
* Description:
* Initialize ESP32 SHA hardware.
*
* Input Parameters:
* None
*
* Returned Value:
* OK is returned on success. Otherwise, a negated errno value is returned.
*
****************************************************************************/
int esp32_sha_init(void);
/****************************************************************************
* Name: esp32_sha1_init
*
* Description:
* Initializes a SHA context.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32_sha1_init(struct esp32_sha_context_s *ctx);
/****************************************************************************
* Name: esp32_sha1_starts
*
* Description:
* Starts a SHA checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha1_starts(struct esp32_sha_context_s *ctx);
/****************************************************************************
* Name: esp32_sha_update
*
* Description:
* Feeds an input buffer into an ongoing SHA checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to use
* input - The buffer holding the input data
* ilen - The length of the input data in Bytes
*
* Returned Value:
* OK is returned on success.
* Otherwise, a negated errno value is returned.
*
****************************************************************************/
int esp32_sha_update(struct esp32_sha_context_s *ctx,
const unsigned char *input,
size_t ilen);
/****************************************************************************
* Name: esp32_sha_finish
*
* Description:
* Finishes the SHA operation,
* and writes the result to the output buffer.
*
* Input Parameters:
* ctx - The SHA context to use
* output - The SHA checksum result
*
* Returned Value:
* OK is returned on success.
* Otherwise, a negated errno value is returned.
*
****************************************************************************/
int esp32_sha_finish(struct esp32_sha_context_s *ctx,
unsigned char output[64]);
/****************************************************************************
* Name: esp32_sha256_starts
*
* Description:
* Starts a SHA-256 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha256_starts(struct esp32_sha_context_s *ctx);
/****************************************************************************
* Name: esp32_sha384_starts
*
* Description:
* Starts a SHA-384 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha384_starts(struct esp32_sha_context_s *ctx);
/****************************************************************************
* Name: esp32_sha512_starts
*
* Description:
* Starts a SHA-512 checksum calculation.
*
* Input Parameters:
* ctx - The SHA context to initialize
*
* Returned Value:
* OK is returned on success.
*
****************************************************************************/
int esp32_sha512_starts(struct esp32_sha_context_s *ctx);
#ifdef __cplusplus
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_SHA_H */
+2
View File
@@ -128,8 +128,10 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)uart_hal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)mmu_hal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)i2c_hal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)sha_hal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log$(DELIM)log_noos.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log$(DELIM)log.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)dport_access_common.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)adc_periph.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)gpio_periph.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)ledc_periph.c
@@ -0,0 +1,57 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_LEDS is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ALLOW_BSD_COMPONENTS=y
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32-devkitc"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32_DEVKITC=y
CONFIG_ARCH_CHIP="esp32"
CONFIG_ARCH_CHIP_ESP32=y
CONFIG_ARCH_CHIP_ESP32WROVER=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_CRYPTO=y
CONFIG_CRYPTO_CRYPTODEV=y
CONFIG_CRYPTO_CRYPTODEV_HARDWARE=y
CONFIG_CRYPTO_RANDOM_POOL=y
CONFIG_DEFAULT_TASK_STACKSIZE=8192
CONFIG_ESP32_SHA_ACCELERATOR=y
CONFIG_ESP32_UART0=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INIT_STACKSIZE=6096
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_NSH_STRERROR=y
CONFIG_PREALLOC_TIMERS=0
CONFIG_RAM_SIZE=314688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=3
CONFIG_START_MONTH=5
CONFIG_START_YEAR=2025
CONFIG_SYSLOG_BUFFER=y
CONFIG_SYSTEM_NSH=y
CONFIG_TESTING_CRYPTO=y
CONFIG_TESTING_CRYPTO_HASH=y
CONFIG_UART0_SERIAL_CONSOLE=y