Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha256的数据放在PSRAM时计算不正确 (IDFGH-10690) #11915

Closed
3 tasks done
dengbq1234 opened this issue Jul 20, 2023 · 5 comments
Closed
3 tasks done

sha256的数据放在PSRAM时计算不正确 (IDFGH-10690) #11915

dengbq1234 opened this issue Jul 20, 2023 · 5 comments
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF

Comments

@dengbq1234
Copy link

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

v4.4.4

Operating System used.

Windows

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

CMD

Development Kit.

Esp32-s3-krovo-3

Power Supply used.

External 5V

What is the expected behavior?

通过mbedtls_sha256*相关的API,能够计算出正确的sha256值,无论数据是放在片内RAM还是PSRAM.

What is the actual behavior?

数据在片内RAM时,能够计算出正确的sha256值,放在PSRAM时,计算错误.

Steps to reproduce.

见工程源码:
hello_world_ks3.zip

运行结果:
屏幕截图 2023-06-09 101212

正确的sha256是:
3fe3881ad5ad724f2a63c38f26b37c56c0132840b9e8388d950620954777dd6b

Debug Logs.

No response

More Information.

必现问题

@dengbq1234 dengbq1234 added the Type: Bug bugs in IDF label Jul 20, 2023
@espressif-bot espressif-bot added the Status: Opened Issue is new label Jul 20, 2023
@github-actions github-actions bot changed the title sha256的数据放在PSRAM时计算不正确 sha256的数据放在PSRAM时计算不正确 (IDFGH-10690) Jul 20, 2023
@mahavirj
Copy link
Member

@dengbq1234

Can you please check if the following patch helps to fix this issue:

diff --git components/mbedtls/port/sha/dma/sha.c components/mbedtls/port/sha/dma/sha.c
index a0fff4acd5..3146fa957a 100644
--- components/mbedtls/port/sha/dma/sha.c
+++ components/mbedtls/port/sha/dma/sha.c
@@ -215,7 +215,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");
@@ -249,6 +248,15 @@ int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
         buf = dma_cap_buf;
     }
 
+    /* Number of DMA operations based on maximum chunk size in single operation */
+    uint32_t dma_op_num;
+    if (ilen > 0) {
+        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,

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Jul 27, 2023
@dengbq1234
Copy link
Author

dengbq1234 commented Jul 31, 2023 via email

@mahavirj
Copy link
Member

@dengbq1234

Please try to apply with this command git apply -p0 </path/to/patch>

@dengbq1234
Copy link
Author

dengbq1234 commented Aug 1, 2023 via email

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: In Progress Work is in progress labels Aug 1, 2023
espressif-bot pushed a commit that referenced this issue Aug 2, 2023
Covers a test scenario described in following issue:
#11915
@mahavirj
Copy link
Member

mahavirj commented Aug 2, 2023

Fix has been merged with 735c0c3, you can try on master branch now.

espressif-bot pushed a commit that referenced this issue Aug 22, 2023
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: #11915
espressif-bot pushed a commit that referenced this issue Aug 22, 2023
Covers a test scenario described in following issue:
#11915
espressif-bot pushed a commit that referenced this issue Aug 23, 2023
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: #11915
espressif-bot pushed a commit that referenced this issue Aug 23, 2023
Covers a test scenario described in following issue:
#11915
espressif-bot pushed a commit that referenced this issue Sep 1, 2023
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: #11915
espressif-bot pushed a commit that referenced this issue Sep 1, 2023
Covers a test scenario described in following issue:
#11915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF
Projects
None yet
Development

No branches or pull requests

3 participants