feat(mbedtls/sha): New API for setting SHA mode

This commit is contained in:
harshal.patil
2025-06-18 16:46:39 +05:30
parent e7a76ff71e
commit 5210e576d5
32 changed files with 279 additions and 135 deletions
@@ -50,12 +50,19 @@ extern "C" {
*/
void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output);
/**
* @brief Set the mode for the SHA engine
*
* @param sha_type The SHA algorithm type
*/
void esp_sha_set_mode(esp_sha_type sha_type);
/** @brief Execute SHA block operation
*
* @note This is a piece of a SHA algorithm, rather than an entire SHA
* algorithm.
*
* @note Call esp_sha_acquire_hardware() before calling this
* @note Call esp_sha_acquire_hardware() and esp_sha_set_mode() before calling this
* function.
*
* @param sha_type SHA algorithm to use.
@@ -78,7 +85,7 @@ void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_
* @note This is a piece of a SHA algorithm, rather than an entire SHA
* algorithm.
*
* @note Call esp_sha_aquire_hardware() before calling this
* @note Call esp_sha_aquire_hardware() and esp_sha_set_mode() before calling this
* function.
*
* @param sha_type SHA algorithm to use.
@@ -145,7 +152,6 @@ void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state);
*/
void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state);
/**
* @brief Enables the SHA and crypto DMA peripheral and takes the
* locks for both of them.
@@ -1,16 +1,8 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "hal/sha_types.h"
@@ -69,6 +61,13 @@ extern "C" {
*/
void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output);
/**
* @brief Set the mode for the SHA engine
*
* @param sha_type The SHA algorithm type
*/
void esp_sha_set_mode(esp_sha_type sha_type);
/* @brief Begin to execute a single SHA block operation
*
* @note This is a piece of a SHA algorithm, rather than an entire SHA
@@ -113,6 +113,9 @@ static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uin
int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
{
esp_sha_acquire_hardware();
esp_sha_set_mode(ctx->mode);
esp_internal_sha_update_state(ctx);
#if SOC_SHA_SUPPORT_DMA
@@ -166,6 +169,8 @@ int mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, s
esp_sha_acquire_hardware();
esp_sha_set_mode(ctx->mode);
esp_internal_sha_update_state(ctx);
#if SOC_SHA_SUPPORT_DMA
@@ -126,6 +126,9 @@ static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const
int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
{
esp_sha_acquire_hardware();
esp_sha_set_mode(ctx->mode);
esp_internal_sha_update_state(ctx);
#if SOC_SHA_SUPPORT_DMA
@@ -185,6 +188,8 @@ int mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *inpu
esp_sha_acquire_hardware();
esp_sha_set_mode(ctx->mode);
esp_internal_sha_update_state(ctx);
#if SOC_SHA_SUPPORT_DMA
@@ -160,6 +160,8 @@ int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx, const unsigned
esp_sha_acquire_hardware();
esp_sha_set_mode(ctx->mode);
ret = esp_internal_sha_update_state(ctx);
if (ret != 0) {
esp_sha_release_hardware();
@@ -220,6 +222,8 @@ int mbedtls_sha512_update(mbedtls_sha512_context *ctx, const unsigned char *inpu
esp_sha_acquire_hardware();
esp_sha_set_mode(ctx->mode);
int ret = esp_internal_sha_update_state(ctx);
if (ret != 0) {
+7 -1
View File
@@ -102,6 +102,12 @@ void esp_sha_release_hardware(void)
esp_crypto_sha_aes_lock_release();
}
void esp_sha_set_mode(esp_sha_type sha_type)
{
sha_hal_wait_idle();
sha_hal_set_mode(sha_type);
}
void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block)
{
sha_hal_hash_block(sha_type, data_block, block_length(sha_type) / 4, is_first_block);
@@ -284,7 +290,7 @@ static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, u
return -1;
}
sha_hal_hash_dma(sha_type, num_blks, is_first_block);
sha_hal_hash_dma(num_blks, is_first_block);
sha_hal_wait_idle();
@@ -50,7 +50,7 @@ static portMUX_TYPE memory_block_lock = portMUX_INITIALIZER_UNLOCKED;
/* Binary semaphore managing the state of each concurrent SHA engine.
Available = noone is using this SHA engine
Available = no one is using this SHA engine
Taken = a SHA session is running on this SHA engine
Indexes:
@@ -209,6 +209,11 @@ void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
esp_sha_unlock_memory_block();
}
void esp_sha_set_mode(esp_sha_type sha_type)
{
sha_hal_set_mode(sha_type);
}
void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool first_block)
{
#ifndef NDEBUG