From b64670b51ee8bfbd9970a989ba26f3cb2661261f Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 1 Sep 2023 17:17:12 +0530 Subject: [PATCH] fix(aes-gcm): correct the DMA completion wait condition for hardware GCM case DMA operation completion must wait until the last DMA descriptor ownership has been changed to hardware, that is hardware is completed the write operation for entire data. Earlier for the hardware GCM case, the first DMA descriptor was checked and it could have resulted in some race condition for non interrupt (MBEDTLS_AES_USE_INTERRUPT disabled) case. --- components/mbedtls/port/aes/dma/esp_aes.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/mbedtls/port/aes/dma/esp_aes.c b/components/mbedtls/port/aes/dma/esp_aes.c index 240b6882f4..0c465281fd 100644 --- a/components/mbedtls/port/aes/dma/esp_aes.c +++ b/components/mbedtls/port/aes/dma/esp_aes.c @@ -481,6 +481,7 @@ cleanup: int esp_aes_process_dma_gcm(esp_aes_context *ctx, const unsigned char *input, unsigned char *output, size_t len, lldesc_t *aad_desc, size_t aad_len) { lldesc_t *in_desc_head = NULL, *out_desc_head = NULL, *len_desc = NULL; + lldesc_t *out_desc_tail = NULL; /* pointer to the final output descriptor */ lldesc_t stream_in_desc, stream_out_desc; lldesc_t *block_desc = NULL, *block_in_desc = NULL, *block_out_desc = NULL; size_t lldesc_num; @@ -530,6 +531,8 @@ int esp_aes_process_dma_gcm(esp_aes_context *ctx, const unsigned char *input, un lldesc_append(&in_desc_head, block_in_desc); lldesc_append(&out_desc_head, block_out_desc); + + out_desc_tail = &block_out_desc[lldesc_num - 1]; } /* Any leftover bytes which are appended as an additional DMA list */ @@ -541,6 +544,8 @@ int esp_aes_process_dma_gcm(esp_aes_context *ctx, const unsigned char *input, un lldesc_append(&in_desc_head, &stream_in_desc); lldesc_append(&out_desc_head, &stream_out_desc); + + out_desc_tail = &stream_out_desc; } @@ -579,7 +584,7 @@ int esp_aes_process_dma_gcm(esp_aes_context *ctx, const unsigned char *input, un aes_hal_transform_dma_gcm_start(blocks); - esp_aes_dma_wait_complete(use_intr, out_desc_head); + esp_aes_dma_wait_complete(use_intr, out_desc_tail); aes_hal_transform_dma_finish();