Skip to content

Commit 6f5f7fd

Browse files
committed
Merge branch 'bugfix/sha_dma_mode_incorrect_result' into 'master'
fix(sha): DMA mode iteration calculation issue for certain data lengths Closes IDFGH-10690 See merge request espressif/esp-idf!25010
2 parents bce97e2 + 224a308 commit 6f5f7fd

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

components/mbedtls/port/sha/dma/sha.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
175175
{
176176
int ret = 0;
177177
unsigned char *dma_cap_buf = NULL;
178-
int dma_op_num = ( ilen / (SOC_SHA_DMA_MAX_BUFFER_SIZE + 1) ) + 1;
179178

180179
if (buf_len > block_length(sha_type)) {
181180
ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block");
@@ -209,6 +208,16 @@ int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
209208
buf = dma_cap_buf;
210209
}
211210

211+
uint32_t dma_op_num;
212+
213+
if (ilen > 0) {
214+
/* Number of DMA operations based on maximum chunk size in single operation */
215+
dma_op_num = (ilen + SOC_SHA_DMA_MAX_BUFFER_SIZE - 1) / SOC_SHA_DMA_MAX_BUFFER_SIZE;
216+
} else {
217+
/* For zero input length, we must allow at-least 1 DMA operation to see
218+
* if there is any pending data that is yet to be copied out */
219+
dma_op_num = 1;
220+
}
212221

213222
/* The max amount of blocks in a single hardware operation is 2^6 - 1 = 63
214223
Thus we only do a single DMA input list + dma buf list,

components/mbedtls/test_apps/main/test_mbedtls_sha.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -527,6 +527,40 @@ TEST_CASE("mbedtls SHA256 PSRAM DMA", "[mbedtls]")
527527
TEST_ASSERT_EQUAL_STRING(expected_hash, hash_str);
528528

529529
}
530+
531+
#if SOC_SHA_SUPPORT_DMA
532+
TEST_CASE("mbedtls SHA256 PSRAM DMA large buffer", "[hw_crypto]")
533+
{
534+
mbedtls_sha256_context sha256_ctx;
535+
unsigned char sha256[32];
536+
537+
const size_t SZ = 257984; // specific size to cover issue in https://github.com/espressif/esp-idf/issues/11915
538+
void *buffer = heap_caps_malloc(SZ, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
539+
TEST_ASSERT_NOT_NULL(buffer);
540+
memset(buffer, 0x55, SZ);
541+
542+
mbedtls_sha256_init(&sha256_ctx);
543+
int r = mbedtls_sha256_starts(&sha256_ctx, false);
544+
TEST_ASSERT_EQUAL(0, r);
545+
r = mbedtls_sha256_update(&sha256_ctx, buffer, SZ);
546+
TEST_ASSERT_EQUAL(0, r);
547+
r = mbedtls_sha256_finish(&sha256_ctx, sha256);
548+
TEST_ASSERT_EQUAL(0, r);
549+
mbedtls_sha256_free(&sha256_ctx);
550+
free(buffer);
551+
552+
/* Check the result. Reference value can be calculated using:
553+
* dd if=/dev/zero bs=257984 count=1 | tr '\000' '\125' | sha256sum
554+
*/
555+
const char *expected_hash = "f2330c9f81ff1c8f0515247faa82be8b6f9685601de6f5dae79172766f136c33";
556+
557+
char hash_str[sizeof(sha256) * 2 + 1];
558+
utils_bin2hex(hash_str, sizeof(hash_str), sha256, sizeof(sha256));
559+
560+
TEST_ASSERT_EQUAL_STRING(expected_hash, hash_str);
561+
}
562+
#endif // SOC_SHA_SUPPORT_DMA
563+
530564
#endif //CONFIG_SPIRAM_USE_MALLOC
531565

532566
#if CONFIG_ESP_SYSTEM_RTC_FAST_MEM_AS_HEAP_DEPCHECK

0 commit comments

Comments
 (0)