From d7376252155772f3bfee69b255bbafa30066ee2e Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Thu, 7 Mar 2024 23:37:47 +0530 Subject: [PATCH] feat(mbedtls/sha): Support in AXI-DMA SHA driver layer --- .../port/include/esp_crypto_shared_gdma.h | 2 +- .../port/sha/dma/esp_sha_crypto_dma_impl.c | 2 +- .../mbedtls/port/sha/dma/esp_sha_gdma_impl.c | 4 +- .../port/sha/dma/include/esp_sha_dma_priv.h | 22 +-- components/mbedtls/port/sha/dma/sha.c | 134 ++++++++---------- tools/ci/check_copyright_ignore.txt | 1 - 6 files changed, 74 insertions(+), 91 deletions(-) diff --git a/components/mbedtls/port/include/esp_crypto_shared_gdma.h b/components/mbedtls/port/include/esp_crypto_shared_gdma.h index d6e40fd0fb..d5e96d2857 100644 --- a/components/mbedtls/port/include/esp_crypto_shared_gdma.h +++ b/components/mbedtls/port/include/esp_crypto_shared_gdma.h @@ -27,7 +27,7 @@ extern "C" { * GDMA_TRIG_PERIPH_SHA * @return esp_err_t ESP_FAIL if no GDMA channel available */ -esp_err_t esp_crypto_shared_gdma_start(const lldesc_t *input, const lldesc_t *output, gdma_trigger_peripheral_t peripheral); +esp_err_t esp_crypto_shared_gdma_start(const lldesc_t *input, const lldesc_t *output, gdma_trigger_peripheral_t peripheral) __attribute__((deprecated("use esp_crypto_shared_gdma_start_axi_ahb instead"))); /** * @brief Start a GDMA transfer on the shared crypto DMA channel diff --git a/components/mbedtls/port/sha/dma/esp_sha_crypto_dma_impl.c b/components/mbedtls/port/sha/dma/esp_sha_crypto_dma_impl.c index 331751f95d..c1554d45a8 100644 --- a/components/mbedtls/port/sha/dma/esp_sha_crypto_dma_impl.c +++ b/components/mbedtls/port/sha/dma/esp_sha_crypto_dma_impl.c @@ -12,7 +12,7 @@ #include "hal/crypto_dma_ll.h" -esp_err_t esp_sha_dma_start(const lldesc_t *input) +esp_err_t esp_sha_dma_start(const crypto_dma_desc_t *input) { crypto_dma_ll_set_mode(CRYPTO_DMA_SHA); crypto_dma_ll_reset(); diff --git a/components/mbedtls/port/sha/dma/esp_sha_gdma_impl.c b/components/mbedtls/port/sha/dma/esp_sha_gdma_impl.c index bf65283040..86e2056a71 100644 --- a/components/mbedtls/port/sha/dma/esp_sha_gdma_impl.c +++ b/components/mbedtls/port/sha/dma/esp_sha_gdma_impl.c @@ -7,7 +7,7 @@ #include "esp_sha_dma_priv.h" #include "esp_crypto_shared_gdma.h" -esp_err_t esp_sha_dma_start(const lldesc_t *input) +esp_err_t esp_sha_dma_start(const crypto_dma_desc_t *input) { - return esp_crypto_shared_gdma_start(input, NULL, GDMA_TRIG_PERIPH_SHA); + return esp_crypto_shared_gdma_start_axi_ahb(input, NULL, GDMA_TRIG_PERIPH_SHA); } diff --git a/components/mbedtls/port/sha/dma/include/esp_sha_dma_priv.h b/components/mbedtls/port/sha/dma/include/esp_sha_dma_priv.h index fdb758fc3c..be1e4aec1b 100644 --- a/components/mbedtls/port/sha/dma/include/esp_sha_dma_priv.h +++ b/components/mbedtls/port/sha/dma/include/esp_sha_dma_priv.h @@ -1,21 +1,13 @@ -// Copyright 2020 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: 2020-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once -#include "soc/lldesc.h" #include "soc/soc_caps.h" +#include "esp_crypto_dma.h" #include "esp_err.h" #ifdef __cplusplus @@ -30,7 +22,7 @@ extern "C" { * - ESP_OK: Successfully started the DMA * - ESP_ERR_INVALID_STATE: No DMA channel available */ -esp_err_t esp_sha_dma_start(const lldesc_t *input); +esp_err_t esp_sha_dma_start(const crypto_dma_desc_t *input); #ifdef __cplusplus diff --git a/components/mbedtls/port/sha/dma/sha.c b/components/mbedtls/port/sha/dma/sha.c index a0453c5607..10b0075a92 100644 --- a/components/mbedtls/port/sha/dma/sha.c +++ b/components/mbedtls/port/sha/dma/sha.c @@ -33,7 +33,7 @@ #include "esp_memory_utils.h" #include "esp_crypto_lock.h" #include "esp_attr.h" -#include "soc/lldesc.h" +#include "esp_crypto_dma.h" #include "esp_cache.h" #include "soc/ext_mem_defs.h" #include "soc/periph_defs.h" @@ -58,14 +58,6 @@ #endif const static char *TAG = "esp-sha"; -static bool s_check_dma_capable(const void *p); - -/* These are static due to: - * * Must be in DMA capable memory, so stack is not a safe place to put them - * * To avoid having to malloc/free them for every DMA operation - */ -static DRAM_ATTR lldesc_t s_dma_descr_input; -static DRAM_ATTR lldesc_t s_dma_descr_buf; void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state) { @@ -129,6 +121,16 @@ void esp_sha_release_hardware() SHA_RELEASE(); } +static bool s_check_dma_capable(const void *p) +{ + bool is_capable = false; +#if CONFIG_SPIRAM + is_capable |= esp_ptr_dma_ext_capable(p); +#endif + is_capable |= esp_ptr_dma_capable(p); + + return is_capable; +} /* Hash the input block by block, using non-DMA mode */ static void esp_sha_block_mode(esp_sha_type sha_type, const uint8_t *input, uint32_t ilen, @@ -153,10 +155,60 @@ static void esp_sha_block_mode(esp_sha_type sha_type, const uint8_t *input, uint } } +/* These are static due to: + * * Must be in DMA capable memory, so stack is not a safe place to put them + * * To avoid having to malloc/free them for every DMA operation + */ +static DRAM_ATTR crypto_dma_desc_t s_dma_descr_input; +static DRAM_ATTR crypto_dma_desc_t s_dma_descr_buf; +/* Performs SHA on multiple blocks at a time */ +static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen, + const void *buf, uint32_t buf_len, bool is_first_block) +{ + int ret = 0; + crypto_dma_desc_t *dma_descr_head = NULL; + size_t num_blks = (ilen + buf_len) / block_length(sha_type); -static int esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen, - const void *buf, uint32_t buf_len, bool is_first_block); + memset(&s_dma_descr_input, 0, sizeof(crypto_dma_desc_t)); + memset(&s_dma_descr_buf, 0, sizeof(crypto_dma_desc_t)); + + /* DMA descriptor for Memory to DMA-SHA transfer */ + if (ilen) { + s_dma_descr_input.dw0.length = ilen; + s_dma_descr_input.dw0.size = ilen; + s_dma_descr_input.dw0.owner = 1; + s_dma_descr_input.dw0.suc_eof = 1; + s_dma_descr_input.buffer = (void *) input; + dma_descr_head = &s_dma_descr_input; + } + /* Check after input to overide head if there is any buf*/ + if (buf_len) { + s_dma_descr_buf.dw0.length = buf_len; + s_dma_descr_buf.dw0.size = buf_len; + s_dma_descr_buf.dw0.owner = 1; + s_dma_descr_buf.dw0.suc_eof = 1; + s_dma_descr_buf.buffer = (void *) buf; + dma_descr_head = &s_dma_descr_buf; + } + + /* Link DMA lists */ + if (buf_len && ilen) { + s_dma_descr_buf.dw0.suc_eof = 0; + s_dma_descr_buf.next = (&s_dma_descr_input); + } + + if (esp_sha_dma_start(dma_descr_head) != ESP_OK) { + ESP_LOGE(TAG, "esp_sha_dma_start failed, no DMA channel available"); + return -1; + } + + sha_hal_hash_dma(sha_type, num_blks, is_first_block); + + sha_hal_wait_idle(); + + return ret; +} /* Performs SHA on multiple blocks at a time using DMA splits up into smaller operations for inputs that exceed a single DMA list @@ -235,63 +287,3 @@ cleanup: free(dma_cap_buf); return ret; } - - -/* Performs SHA on multiple blocks at a time */ -static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen, - const void *buf, uint32_t buf_len, bool is_first_block) -{ - int ret = 0; - lldesc_t *dma_descr_head = NULL; - size_t num_blks = (ilen + buf_len) / block_length(sha_type); - - memset(&s_dma_descr_input, 0, sizeof(lldesc_t)); - memset(&s_dma_descr_buf, 0, sizeof(lldesc_t)); - - /* DMA descriptor for Memory to DMA-SHA transfer */ - if (ilen) { - s_dma_descr_input.length = ilen; - s_dma_descr_input.size = ilen; - s_dma_descr_input.owner = 1; - s_dma_descr_input.eof = 1; - s_dma_descr_input.buf = (uint8_t *)input; - dma_descr_head = &s_dma_descr_input; - } - /* Check after input to overide head if there is any buf*/ - if (buf_len) { - s_dma_descr_buf.length = buf_len; - s_dma_descr_buf.size = buf_len; - s_dma_descr_buf.owner = 1; - s_dma_descr_buf.eof = 1; - s_dma_descr_buf.buf = (uint8_t *)buf; - dma_descr_head = &s_dma_descr_buf; - } - - /* Link DMA lists */ - if (buf_len && ilen) { - s_dma_descr_buf.eof = 0; - s_dma_descr_buf.empty = (uint32_t)(&s_dma_descr_input); - } - - if (esp_sha_dma_start(dma_descr_head) != ESP_OK) { - ESP_LOGE(TAG, "esp_sha_dma_start failed, no DMA channel available"); - return -1; - } - - sha_hal_hash_dma(sha_type, num_blks, is_first_block); - - sha_hal_wait_idle(); - - return ret; -} - -static bool s_check_dma_capable(const void *p) -{ - bool is_capable = false; -#if CONFIG_SPIRAM - is_capable |= esp_ptr_dma_ext_capable(p); -#endif - is_capable |= esp_ptr_dma_capable(p); - - return is_capable; -} diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 042ae1a183..0491b31991 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -519,7 +519,6 @@ components/mbedtls/port/include/sha/sha_parallel_engine.h components/mbedtls/port/include/sha1_alt.h components/mbedtls/port/include/sha256_alt.h components/mbedtls/port/include/sha512_alt.h -components/mbedtls/port/sha/dma/include/esp_sha_dma_priv.h components/mbedtls/port/sha/dma/sha.c components/mbedtls/port/sha/parallel_engine/sha.c components/nvs_flash/include/nvs_handle.hpp