From 66b718ac8297e7a0e9f7c45b29054a09f3813f93 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Wed, 26 Jul 2023 15:31:12 +0530 Subject: [PATCH] fix(sha): DMA mode iteration calculation issue for certain data lengths SHA hardware DMA mode calculation had off-by-one error for specific input lengths. This was causing last chunk of the input data not being fed to the hardware accelerator and hence resulting in an incorrect final result. Closes: https://github.com/espressif/esp-idf/issues/11915 --- components/mbedtls/port/sha/dma/sha.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/components/mbedtls/port/sha/dma/sha.c b/components/mbedtls/port/sha/dma/sha.c index cfb09603d105..609fe97e72b3 100644 --- a/components/mbedtls/port/sha/dma/sha.c +++ b/components/mbedtls/port/sha/dma/sha.c @@ -219,7 +219,6 @@ int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen, { int ret = 0; unsigned char *dma_cap_buf = NULL; - int dma_op_num = ( ilen / (SOC_SHA_DMA_MAX_BUFFER_SIZE + 1) ) + 1; if (buf_len > block_length(sha_type)) { ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block"); @@ -253,6 +252,16 @@ int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen, buf = dma_cap_buf; } + 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; + } /* 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,