2019-05-09 23:34:06 -04:00
|
|
|
/*
|
2020-08-13 04:30:59 -04:00
|
|
|
* ESP hardware accelerated SHA1/256/512 implementation
|
2019-05-09 23:34:06 -04:00
|
|
|
* based on mbedTLS FIPS-197 compliant version.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2020-01-16 01:31:10 -05:00
|
|
|
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE Ltd
|
2019-05-09 23:34:06 -04:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The SHA-1 standard was published by NIST in 1993.
|
|
|
|
*
|
|
|
|
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/lock.h>
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2024-03-07 14:10:26 -05:00
|
|
|
#include "esp_private/esp_crypto_lock_internal.h"
|
2024-03-26 01:59:28 -04:00
|
|
|
#include "esp_private/esp_cache_private.h"
|
2020-01-16 01:31:10 -05:00
|
|
|
#include "esp_log.h"
|
2022-07-21 07:14:41 -04:00
|
|
|
#include "esp_memory_utils.h"
|
2020-02-07 07:08:34 -05:00
|
|
|
#include "esp_crypto_lock.h"
|
2021-11-17 04:43:22 -05:00
|
|
|
#include "esp_attr.h"
|
2024-03-07 13:07:47 -05:00
|
|
|
#include "esp_crypto_dma.h"
|
2023-11-06 06:57:14 -05:00
|
|
|
#include "esp_cache.h"
|
2024-03-26 01:59:28 -04:00
|
|
|
#include "hal/dma_types.h"
|
2022-02-11 02:30:54 -05:00
|
|
|
#include "soc/ext_mem_defs.h"
|
2020-02-07 07:08:34 -05:00
|
|
|
#include "soc/periph_defs.h"
|
2019-12-26 02:25:24 -05:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/semphr.h"
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2021-10-25 05:13:46 -04:00
|
|
|
#include "esp_private/periph_ctrl.h"
|
2020-01-16 01:31:10 -05:00
|
|
|
#include "sys/param.h"
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#include "sha/sha_dma.h"
|
|
|
|
#include "hal/sha_hal.h"
|
2024-03-07 14:10:26 -05:00
|
|
|
#include "hal/sha_ll.h"
|
2020-10-28 22:51:36 -04:00
|
|
|
#include "soc/soc_caps.h"
|
2021-01-14 02:25:06 -05:00
|
|
|
#include "esp_sha_dma_priv.h"
|
2024-04-26 04:12:08 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#ifdef SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT
|
|
|
|
#include "esp_flash_encrypt.h"
|
|
|
|
#endif /* SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT */
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2021-01-14 02:25:06 -05:00
|
|
|
#if SOC_SHA_GDMA
|
2021-02-25 02:06:41 -05:00
|
|
|
#define SHA_LOCK() esp_crypto_sha_aes_lock_acquire()
|
|
|
|
#define SHA_RELEASE() esp_crypto_sha_aes_lock_release()
|
2020-08-13 04:30:59 -04:00
|
|
|
#elif SOC_SHA_CRYPTO_DMA
|
|
|
|
#define SHA_LOCK() esp_crypto_dma_lock_acquire()
|
|
|
|
#define SHA_RELEASE() esp_crypto_dma_lock_release()
|
2024-03-07 14:10:26 -05:00
|
|
|
#include "hal/crypto_dma_ll.h"
|
2020-08-13 04:30:59 -04:00
|
|
|
#endif
|
2020-04-08 04:37:51 -04:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
const static char *TAG = "esp-sha";
|
2020-08-13 04:30:59 -04:00
|
|
|
|
|
|
|
void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state)
|
|
|
|
{
|
|
|
|
sha_hal_write_digest(sha_type, digest_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
|
|
|
|
{
|
|
|
|
sha_hal_read_digest(sha_type, digest_state);
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:34:06 -04:00
|
|
|
/* Return block size (in bytes) for a given SHA type */
|
2019-12-26 02:25:24 -05:00
|
|
|
inline static size_t block_length(esp_sha_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
2019-05-09 23:34:06 -04:00
|
|
|
case SHA1:
|
|
|
|
case SHA2_224:
|
|
|
|
case SHA2_256:
|
|
|
|
return 64;
|
2020-12-22 06:37:59 -05:00
|
|
|
#if SOC_SHA_SUPPORT_SHA384
|
2019-05-09 23:34:06 -04:00
|
|
|
case SHA2_384:
|
2020-12-22 06:37:59 -05:00
|
|
|
#endif
|
|
|
|
#if SOC_SHA_SUPPORT_SHA512
|
2019-05-09 23:34:06 -04:00
|
|
|
case SHA2_512:
|
2020-12-22 06:37:59 -05:00
|
|
|
#endif
|
|
|
|
#if SOC_SHA_SUPPORT_SHA512_T
|
2019-12-26 02:25:24 -05:00
|
|
|
case SHA2_512224:
|
|
|
|
case SHA2_512256:
|
|
|
|
case SHA2_512T:
|
2020-12-22 06:37:59 -05:00
|
|
|
#endif
|
2019-05-09 23:34:06 -04:00
|
|
|
return 128;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Enable SHA peripheral and then lock it */
|
|
|
|
void esp_sha_acquire_hardware()
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2020-08-13 04:30:59 -04:00
|
|
|
SHA_LOCK(); /* Released when releasing hw with esp_sha_release_hardware() */
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2024-03-07 14:10:26 -05:00
|
|
|
SHA_RCC_ATOMIC() {
|
|
|
|
sha_ll_enable_bus_clock(true);
|
|
|
|
#if SOC_AES_CRYPTO_DMA
|
|
|
|
crypto_dma_ll_enable_bus_clock(true);
|
2020-08-13 04:30:59 -04:00
|
|
|
#endif
|
2024-03-07 14:10:26 -05:00
|
|
|
sha_ll_reset_register();
|
|
|
|
#if SOC_AES_CRYPTO_DMA
|
|
|
|
crypto_dma_ll_reset_register();
|
|
|
|
#endif
|
|
|
|
}
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Disable SHA peripheral block and then release it */
|
|
|
|
void esp_sha_release_hardware()
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2024-03-07 14:10:26 -05:00
|
|
|
SHA_RCC_ATOMIC() {
|
|
|
|
sha_ll_enable_bus_clock(false);
|
|
|
|
#if SOC_AES_CRYPTO_DMA
|
|
|
|
crypto_dma_ll_enable_bus_clock(false);
|
2020-08-13 04:30:59 -04:00
|
|
|
#endif
|
2024-03-07 14:10:26 -05:00
|
|
|
}
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
SHA_RELEASE();
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
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;
|
|
|
|
}
|
2020-04-08 04:37:51 -04:00
|
|
|
|
|
|
|
/* 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,
|
2020-08-13 04:30:59 -04:00
|
|
|
const uint8_t *buf, uint32_t buf_len, bool is_first_block)
|
2020-04-08 04:37:51 -04:00
|
|
|
{
|
|
|
|
size_t blk_len = 0;
|
2020-08-13 04:30:59 -04:00
|
|
|
size_t blk_word_len = 0;
|
2020-04-08 04:37:51 -04:00
|
|
|
int num_block = 0;
|
|
|
|
|
|
|
|
blk_len = block_length(sha_type);
|
2020-08-13 04:30:59 -04:00
|
|
|
blk_word_len = blk_len / 4;
|
2020-04-08 04:37:51 -04:00
|
|
|
num_block = ilen / blk_len;
|
|
|
|
|
|
|
|
if (buf_len != 0) {
|
2020-08-13 04:30:59 -04:00
|
|
|
sha_hal_hash_block(sha_type, buf, blk_word_len, is_first_block);
|
2020-04-08 04:37:51 -04:00
|
|
|
is_first_block = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < num_block; i++) {
|
2020-08-13 04:30:59 -04:00
|
|
|
sha_hal_hash_block(sha_type, input + blk_len * i, blk_word_len, is_first_block);
|
2020-04-08 04:37:51 -04:00
|
|
|
is_first_block = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
static DRAM_ATTR crypto_dma_desc_t s_dma_descr_input;
|
|
|
|
static DRAM_ATTR crypto_dma_desc_t s_dma_descr_buf;
|
|
|
|
|
2024-04-26 04:12:08 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
#ifdef SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT
|
|
|
|
static esp_err_t esp_sha_dma_process_ext(esp_sha_type sha_type, const void *input, uint32_t ilen,
|
|
|
|
const void *buf, uint32_t buf_len, bool is_first_block,
|
|
|
|
bool realloc_input, bool realloc_buf)
|
|
|
|
{
|
|
|
|
int ret = ESP_FAIL;
|
|
|
|
void *input_copy = NULL;
|
|
|
|
void *buf_copy = NULL;
|
|
|
|
|
|
|
|
const void *dma_input = NULL;
|
|
|
|
const void *dma_buf = NULL;
|
|
|
|
|
|
|
|
uint32_t heap_caps = 0;
|
|
|
|
|
|
|
|
if (realloc_input) {
|
|
|
|
heap_caps = MALLOC_CAP_8BIT | (esp_ptr_external_ram(input) ? MALLOC_CAP_SPIRAM : MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
|
|
|
|
input_copy = heap_caps_aligned_alloc(SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT, ilen, heap_caps);
|
|
|
|
if (input_copy == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to allocate aligned SPIRAM memory");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
memcpy(input_copy, input, ilen);
|
|
|
|
dma_input = input_copy;
|
|
|
|
} else {
|
|
|
|
dma_input = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realloc_buf) {
|
|
|
|
heap_caps = MALLOC_CAP_8BIT | (esp_ptr_external_ram(buf) ? MALLOC_CAP_SPIRAM : MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
|
|
|
|
buf_copy = heap_caps_aligned_alloc(SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT, buf_len, heap_caps);
|
|
|
|
if (buf_copy == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to allocate aligned internal memory");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
memcpy(buf_copy, buf, buf_len);
|
|
|
|
dma_buf = buf_copy;
|
|
|
|
} else {
|
|
|
|
dma_buf = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = esp_sha_dma_process(sha_type, dma_input, ilen, dma_buf, buf_len, is_first_block);
|
|
|
|
|
|
|
|
if (realloc_input) {
|
|
|
|
free(input_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realloc_buf) {
|
|
|
|
free(buf_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT */
|
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
memset(&s_dma_descr_input, 0, sizeof(crypto_dma_desc_t));
|
|
|
|
memset(&s_dma_descr_buf, 0, sizeof(crypto_dma_desc_t));
|
|
|
|
|
2024-04-26 04:12:08 -04:00
|
|
|
/* When SHA-DMA operations are carried out using external memory with external memory encryption enabled,
|
|
|
|
we need to make sure that the addresses and the sizes of the buffers on which the DMA operates are 16 byte-aligned. */
|
|
|
|
#ifdef SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT
|
|
|
|
if (esp_flash_encryption_enabled()) {
|
|
|
|
if (esp_ptr_external_ram(input) || esp_ptr_external_ram(buf) || esp_ptr_in_drom(input) || esp_ptr_in_drom(buf)) {
|
|
|
|
bool input_needs_realloc = false;
|
|
|
|
bool buf_needs_realloc = false;
|
|
|
|
|
|
|
|
if (ilen && ((intptr_t)(input) & (SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT - 1)) != 0) {
|
|
|
|
input_needs_realloc = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf_len && ((intptr_t)(buf) & (SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT - 1)) != 0) {
|
|
|
|
buf_needs_realloc = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_needs_realloc || buf_needs_realloc) {
|
|
|
|
return esp_sha_dma_process_ext(sha_type, input, ilen, buf, buf_len, is_first_block, input_needs_realloc, buf_needs_realloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT */
|
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
/* 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;
|
|
|
|
}
|
2024-04-26 04:12:08 -04:00
|
|
|
/* Check after input to override head if there is any buf*/
|
2024-03-07 13:07:47 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-03-26 01:59:28 -04:00
|
|
|
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
|
|
|
|
if (ilen) {
|
|
|
|
ESP_ERROR_CHECK(esp_cache_msync(&s_dma_descr_input, sizeof(crypto_dma_desc_t), ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED));
|
|
|
|
ESP_ERROR_CHECK(esp_cache_msync(s_dma_descr_input.buffer, s_dma_descr_input.dw0.length, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf_len) {
|
|
|
|
ESP_ERROR_CHECK(esp_cache_msync(&s_dma_descr_buf, sizeof(crypto_dma_desc_t), ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED));
|
|
|
|
ESP_ERROR_CHECK(esp_cache_msync(s_dma_descr_buf.buffer, s_dma_descr_buf.dw0.length, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED));
|
|
|
|
}
|
|
|
|
#endif /* SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE */
|
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
if (esp_sha_dma_start(dma_descr_head) != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "esp_sha_dma_start failed, no DMA channel available");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-04-08 04:37:51 -04:00
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
sha_hal_hash_dma(sha_type, num_blks, is_first_block);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2024-03-07 13:07:47 -05:00
|
|
|
sha_hal_wait_idle();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
/* Performs SHA on multiple blocks at a time using DMA
|
|
|
|
splits up into smaller operations for inputs that exceed a single DMA list
|
|
|
|
*/
|
|
|
|
int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
|
|
|
|
const void *buf, uint32_t buf_len, bool is_first_block)
|
2019-12-26 02:25:24 -05:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2020-04-08 04:37:51 -04:00
|
|
|
unsigned char *dma_cap_buf = NULL;
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-04-08 04:37:51 -04:00
|
|
|
if (buf_len > block_length(sha_type)) {
|
2020-01-16 01:31:10 -05:00
|
|
|
ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block");
|
|
|
|
return -1;
|
2019-12-26 02:25:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
/* DMA cannot access memory in flash, hash block by block instead of using DMA */
|
2022-03-31 03:07:51 -04:00
|
|
|
if (!s_check_dma_capable(input) && (ilen != 0)) {
|
2020-04-08 04:37:51 -04:00
|
|
|
esp_sha_block_mode(sha_type, input, ilen, buf, buf_len, is_first_block);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-22 00:45:36 -05:00
|
|
|
#if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE)
|
2020-04-08 04:37:51 -04:00
|
|
|
if (esp_ptr_external_ram(input)) {
|
2023-11-06 06:57:14 -05:00
|
|
|
esp_cache_msync((void *)input, ilen, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
2020-04-08 04:37:51 -04:00
|
|
|
}
|
|
|
|
if (esp_ptr_external_ram(buf)) {
|
2023-11-06 06:57:14 -05:00
|
|
|
esp_cache_msync((void *)buf, buf_len, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
2020-01-16 01:31:10 -05:00
|
|
|
}
|
|
|
|
#endif
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2020-04-08 04:37:51 -04:00
|
|
|
/* Copy to internal buf if buf is in non DMA capable memory */
|
2022-03-31 03:07:51 -04:00
|
|
|
if (!s_check_dma_capable(buf) && (buf_len != 0)) {
|
2020-11-17 23:22:47 -05:00
|
|
|
dma_cap_buf = heap_caps_malloc(sizeof(unsigned char) * buf_len, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
|
2020-04-08 04:37:51 -04:00
|
|
|
if (dma_cap_buf == NULL) {
|
2020-03-16 07:29:59 -04:00
|
|
|
ESP_LOGE(TAG, "Failed to allocate buf memory");
|
2020-04-08 04:37:51 -04:00
|
|
|
ret = -1;
|
2020-01-16 01:31:10 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-04-08 04:37:51 -04:00
|
|
|
memcpy(dma_cap_buf, buf, buf_len);
|
|
|
|
buf = dma_cap_buf;
|
2020-03-30 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
2023-07-26 06:01:12 -04:00
|
|
|
uint32_t dma_op_num;
|
|
|
|
|
|
|
|
if (ilen > 0) {
|
|
|
|
/* Number of DMA operations based on maximum chunk size in single operation */
|
|
|
|
dma_op_num = (ilen + SOC_SHA_DMA_MAX_BUFFER_SIZE - 1) / SOC_SHA_DMA_MAX_BUFFER_SIZE;
|
|
|
|
} else {
|
|
|
|
/* For zero input length, we must allow at-least 1 DMA operation to see
|
|
|
|
* if there is any pending data that is yet to be copied out */
|
|
|
|
dma_op_num = 1;
|
|
|
|
}
|
2020-03-30 22:54:22 -04:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* The max amount of blocks in a single hardware operation is 2^6 - 1 = 63
|
|
|
|
Thus we only do a single DMA input list + dma buf list,
|
|
|
|
which is max 3968/64 + 64/64 = 63 blocks */
|
|
|
|
for (int i = 0; i < dma_op_num; i++) {
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
int dma_chunk_len = MIN(ilen, SOC_SHA_DMA_MAX_BUFFER_SIZE);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2020-04-08 04:37:51 -04:00
|
|
|
ret = esp_sha_dma_process(sha_type, input, dma_chunk_len, buf, buf_len, is_first_block);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2020-03-30 22:54:22 -04:00
|
|
|
goto cleanup;
|
2020-01-16 01:31:10 -05:00
|
|
|
}
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
ilen -= dma_chunk_len;
|
2022-11-18 07:29:39 -05:00
|
|
|
input = (uint8_t *)input + dma_chunk_len;
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
// Only append buf to the first operation
|
|
|
|
buf_len = 0;
|
2020-04-08 04:37:51 -04:00
|
|
|
is_first_block = false;
|
2019-12-26 02:25:24 -05:00
|
|
|
}
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
cleanup:
|
2020-04-08 04:37:51 -04:00
|
|
|
free(dma_cap_buf);
|
2020-01-16 01:31:10 -05:00
|
|
|
return ret;
|
|
|
|
}
|