Jelajahi Sumber

Merge branch 'fix/mbedtls_internal_shaX_process_api_port' into 'master'

fix(mbedtls): Fix the port for mbedtls_internal_shaX_process api

See merge request espressif/esp-idf!24545
Mahavir Jain 2 tahun lalu
induk
melakukan
48ead0158e

+ 16 - 8
components/mbedtls/port/sha/block/esp_sha1.c

@@ -5,7 +5,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-1 standard was published by NIST in 1993.
@@ -90,6 +90,16 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
     return 0;
 }
 
+static void esp_internal_sha_update_state(mbedtls_sha1_context *ctx)
+{
+    if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
+        ctx->first_block = true;
+        ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
+    } else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
+        ctx->first_block = false;
+        esp_sha_write_digest_state(ctx->mode, ctx->state);
+    }
+}
 
 static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uint8_t *data)
 {
@@ -103,7 +113,9 @@ static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uin
 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
 {
     esp_sha_acquire_hardware();
+    esp_internal_sha_update_state(ctx);
     esp_sha_block(ctx->mode, data, ctx->first_block);
+    esp_sha_read_digest_state(ctx->mode, ctx->state);
     esp_sha_release_hardware();
     return 0;
 }
@@ -138,12 +150,8 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
     if ( (ilen >= 64) || local_len) {
 
         esp_sha_acquire_hardware();
-        if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
-            ctx->first_block = true;
-            ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
-        } else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
-           esp_sha_write_digest_state(SHA1, ctx->state);
-        }
+
+        esp_internal_sha_update_state(ctx);
 
         /* First process buffered block, if any */
         if ( local_len ) {
@@ -181,7 +189,7 @@ static const unsigned char sha1_padding[64] = {
  */
 int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
 {
-    int ret;
+    int ret = -1;
     uint32_t last, padn;
     uint32_t high, low;
     unsigned char msglen[8];

+ 16 - 8
components/mbedtls/port/sha/block/esp_sha256.c

@@ -5,7 +5,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
@@ -103,6 +103,17 @@ int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
     return 0;
 }
 
+static void esp_internal_sha_update_state(mbedtls_sha256_context *ctx)
+{
+    if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
+        ctx->first_block = true;
+        ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
+    } else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
+        ctx->first_block = false;
+        esp_sha_write_digest_state(ctx->mode, ctx->state);
+    }
+}
+
 static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const uint8_t *data)
 {
     esp_sha_block(ctx->mode, data, ctx->first_block);
@@ -115,7 +126,9 @@ static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const
 int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
 {
     esp_sha_acquire_hardware();
+    esp_internal_sha_update_state(ctx);
     esp_sha_block(ctx->mode, data, ctx->first_block);
+    esp_sha_read_digest_state(ctx->mode, ctx->state);
     esp_sha_release_hardware();
     return 0;
 }
@@ -156,13 +169,8 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
     if ( (ilen >= 64) || local_len) {
 
         esp_sha_acquire_hardware();
-        if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
-            ctx->first_block = true;
 
-            ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
-        } else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
-            esp_sha_write_digest_state(ctx->mode, ctx->state);
-        }
+        esp_internal_sha_update_state(ctx);
 
         /* First process buffered block, if any */
         if ( local_len ) {
@@ -200,7 +208,7 @@ static const unsigned char sha256_padding[64] = {
  */
 int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
 {
-    int ret;
+    int ret = -1;
     uint32_t last, padn;
     uint32_t high, low;
     unsigned char msglen[8];

+ 37 - 15
components/mbedtls/port/sha/block/esp_sha512.c

@@ -5,7 +5,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-512 Secure Hash Standard was published by NIST in 2002.
@@ -125,6 +125,27 @@ int mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
     return 0;
 }
 
+static int esp_internal_sha_update_state(mbedtls_sha512_context *ctx)
+{
+    if (ctx->sha_state == ESP_SHA512_STATE_INIT) {
+        if (ctx->mode == SHA2_512T) {
+            int ret = -1;
+            if ((ret = esp_sha_512_t_init_hash(ctx->t_val)) != 0) {
+                return ret;
+            }
+            ctx->first_block = false;
+        } else {
+            ctx->first_block = true;
+        }
+        ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;
+
+    } else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
+        ctx->first_block = false;
+        esp_sha_write_digest_state(ctx->mode, ctx->state);
+    }
+    return 0;
+}
+
 static void esp_internal_sha512_block_process(mbedtls_sha512_context *ctx, const uint8_t *data)
 {
     esp_sha_block(ctx->mode, data, ctx->first_block);
@@ -136,10 +157,19 @@ static void esp_internal_sha512_block_process(mbedtls_sha512_context *ctx, const
 
 int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
 {
+    int ret = -1;
     esp_sha_acquire_hardware();
+
+    ret = esp_internal_sha_update_state(ctx);
+    if (ret != 0) {
+        esp_sha_release_hardware();
+        return ret;
+    }
+
     esp_sha_block(ctx->mode, data, ctx->first_block);
+    esp_sha_read_digest_state(ctx->mode, ctx->state);
     esp_sha_release_hardware();
-    return 0;
+    return ret;
 }
 
 /*
@@ -178,18 +208,11 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
 
         esp_sha_acquire_hardware();
 
-        if (ctx->sha_state == ESP_SHA512_STATE_INIT) {
-
-            if (ctx->mode == SHA2_512T) {
-                esp_sha_512_t_init_hash(ctx->t_val);
-                ctx->first_block = false;
-            } else {
-                ctx->first_block = true;
-            }
-            ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;
+        int ret = esp_internal_sha_update_state(ctx);
 
-        } else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
-            esp_sha_write_digest_state(ctx->mode, ctx->state);
+        if (ret != 0) {
+            esp_sha_release_hardware();
+            return ret;
         }
 
         /* First process buffered block, if any */
@@ -206,7 +229,6 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
         esp_sha_read_digest_state(ctx->mode, ctx->state);
 
         esp_sha_release_hardware();
-
     }
 
     if ( ilen > 0 ) {
@@ -232,7 +254,7 @@ static const unsigned char sha512_padding[128] = {
  */
 int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
 {
-    int ret;
+    int ret = -1;
     size_t last, padn;
     uint64_t high, low;
     unsigned char msglen[16];

+ 27 - 16
components/mbedtls/port/sha/dma/esp_sha1.c

@@ -5,7 +5,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-1 standard was published by NIST in 1993.
@@ -88,6 +88,16 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
     return 0;
 }
 
+static void esp_internal_sha_update_state(mbedtls_sha1_context *ctx)
+{
+    if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
+        ctx->first_block = true;
+        ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
+    } else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
+        ctx->first_block = false;
+        esp_sha_write_digest_state(ctx->mode, ctx->state);
+    }
+}
 
 static int esp_internal_sha1_dma_process(mbedtls_sha1_context *ctx,
         const uint8_t *data, size_t len,
@@ -98,16 +108,23 @@ static int esp_internal_sha1_dma_process(mbedtls_sha1_context *ctx,
 
 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
 {
-    int ret;
+    int ret = -1;
     esp_sha_acquire_hardware();
+    esp_internal_sha_update_state(ctx);
+
     ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block);
+    if (ret != 0) {
+        esp_sha_release_hardware();
+        return ret;
+    }
+
+    esp_sha_read_digest_state(ctx->mode, ctx->state);
     esp_sha_release_hardware();
     return ret;
 }
 
 int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
 {
-    int ret;
     size_t fill;
     uint32_t left, len, local_len = 0;
 
@@ -138,25 +155,19 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
     if ( len || local_len) {
 
         esp_sha_acquire_hardware();
-        if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
-            ctx->first_block = true;
 
-            ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
-        } else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
-            ctx->first_block = false;
-            esp_sha_write_digest_state(SHA1, ctx->state);
-        }
+        esp_internal_sha_update_state(ctx);
 
-        ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
+        int ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
+        if (ret != 0) {
+            esp_sha_release_hardware();
+            return ret;
+        }
 
         esp_sha_read_digest_state(SHA1, ctx->state);
 
         esp_sha_release_hardware();
 
-        if (ret != 0) {
-            return ret;
-        }
-
     }
 
     if ( ilen > 0 ) {
@@ -178,7 +189,7 @@ static const unsigned char sha1_padding[64] = {
  */
 int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
 {
-    int ret;
+    int ret = -1;
     uint32_t last, padn;
     uint32_t high, low;
     unsigned char msglen[8];

+ 28 - 16
components/mbedtls/port/sha/dma/esp_sha256.c

@@ -5,7 +5,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
@@ -100,11 +100,30 @@ int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
     return 0;
 }
 
+static void esp_internal_sha_update_state(mbedtls_sha256_context *ctx)
+{
+    if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
+        ctx->first_block = true;
+        ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
+    } else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
+        ctx->first_block = false;
+        esp_sha_write_digest_state(ctx->mode, ctx->state);
+    }
+}
+
 int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
 {
-    int ret;
+    int ret = -1;
     esp_sha_acquire_hardware();
+    esp_internal_sha_update_state(ctx);
+
     ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block);
+    if (ret != 0) {
+        esp_sha_release_hardware();
+        return ret;
+    }
+
+    esp_sha_read_digest_state(ctx->mode, ctx->state);
     esp_sha_release_hardware();
 
     return ret;
@@ -116,7 +135,6 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned
 int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
                                size_t ilen )
 {
-    int ret = 0;
     size_t fill;
     uint32_t left, len, local_len = 0;
 
@@ -148,24 +166,18 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
 
     if ( len || local_len) {
         esp_sha_acquire_hardware();
+        esp_internal_sha_update_state(ctx);
 
-        if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
-            ctx->first_block = true;
-            ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
-        } else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
-            ctx->first_block = false;
-            esp_sha_write_digest_state(ctx->mode, ctx->state);
-        }
+        int ret = esp_sha_dma(ctx->mode, input, len,  ctx->buffer, local_len, ctx->first_block);
 
-        ret = esp_sha_dma(ctx->mode, input, len,  ctx->buffer, local_len, ctx->first_block);
+        if (ret != 0) {
+            esp_sha_release_hardware();
+            return ret;
+        }
 
         esp_sha_read_digest_state(ctx->mode, ctx->state);
 
         esp_sha_release_hardware();
-
-        if (ret != 0) {
-            return ret;
-        }
     }
 
     if ( ilen > 0 ) {
@@ -187,7 +199,7 @@ static const unsigned char sha256_padding[64] = {
  */
 int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
 {
-    int ret;
+    int ret = -1;
     uint32_t last, padn;
     uint32_t high, low;
     unsigned char msglen[8];

+ 45 - 20
components/mbedtls/port/sha/dma/esp_sha512.c

@@ -5,7 +5,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-512 Secure Hash Standard was published by NIST in 2002.
@@ -122,6 +122,26 @@ int mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
     return 0;
 }
 
+static int esp_internal_sha_update_state(mbedtls_sha512_context *ctx)
+{
+    if (ctx->sha_state == ESP_SHA512_STATE_INIT) {
+        if (ctx->mode == SHA2_512T) {
+            int ret = -1;
+            if ((ret = esp_sha_512_t_init_hash(ctx->t_val)) != 0) {
+                return ret;
+            }
+            ctx->first_block = false;
+        } else {
+            ctx->first_block = true;
+        }
+        ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;
+    } else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
+        ctx->first_block = false;
+        esp_sha_write_digest_state(ctx->mode, ctx->state);
+    }
+    return 0;
+}
+
 static int esp_internal_sha512_dma_process(mbedtls_sha512_context *ctx,
         const uint8_t *data, size_t len,
         uint8_t *buf, size_t buf_len)
@@ -135,9 +155,22 @@ static int esp_internal_sha512_dma_process(mbedtls_sha512_context *ctx,
 
 int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
 {
-    int ret;
+    int ret = -1;
     esp_sha_acquire_hardware();
+
+    ret = esp_internal_sha_update_state(ctx);
+    if (ret != 0) {
+        esp_sha_release_hardware();
+        return ret;
+    }
+
     ret = esp_internal_sha512_dma_process(ctx, data, 128, 0, 0);
+    if (ret != 0) {
+        esp_sha_release_hardware();
+        return ret;
+    }
+
+    esp_sha_read_digest_state(ctx->mode, ctx->state);
     esp_sha_release_hardware();
 
     return ret;
@@ -150,7 +183,6 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned
 int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
                                size_t ilen )
 {
-    int ret;
     size_t fill;
     unsigned int left, len, local_len = 0;
 
@@ -182,31 +214,24 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
 
         esp_sha_acquire_hardware();
 
-        if (ctx->sha_state == ESP_SHA512_STATE_INIT) {
-
-            if (ctx->mode == SHA2_512T) {
-                esp_sha_512_t_init_hash(ctx->t_val);
-                ctx->first_block = false;
-            } else {
-                ctx->first_block = true;
-            }
-            ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;
+        int ret = esp_internal_sha_update_state(ctx);
 
-        } else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
-            ctx->first_block = false;
-            esp_sha_write_digest_state(ctx->mode, ctx->state);
+        if (ret != 0) {
+            esp_sha_release_hardware();
+            return ret;
         }
 
         ret = esp_internal_sha512_dma_process(ctx, input, len, ctx->buffer, local_len);
 
-        esp_sha_read_digest_state(ctx->mode, ctx->state);
-
-        esp_sha_release_hardware();
-
         if (ret != 0) {
+            esp_sha_release_hardware();
             return ret;
         }
 
+        esp_sha_read_digest_state(ctx->mode, ctx->state);
+
+        esp_sha_release_hardware();
+
     }
 
 
@@ -233,7 +258,7 @@ static const unsigned char sha512_padding[128] = {
  */
 int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
 {
-    int ret;
+    int ret = -1;
     size_t last, padn;
     uint64_t high, low;
     unsigned char msglen[16];

+ 43 - 29
components/mbedtls/port/sha/parallel_engine/esp_sha1.c

@@ -7,7 +7,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-1 standard was published by NIST in 1993.
@@ -121,30 +121,6 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
 }
 
 
-static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );
-
-int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
-{
-    bool first_block = false;
-    if (ctx->mode == ESP_MBEDTLS_SHA1_UNUSED) {
-        /* try to use hardware for this digest */
-        if (esp_sha_try_lock_engine(SHA1)) {
-            ctx->mode = ESP_MBEDTLS_SHA1_HARDWARE;
-            first_block = true;
-        } else {
-            ctx->mode = ESP_MBEDTLS_SHA1_SOFTWARE;
-        }
-    }
-
-    if (ctx->mode == ESP_MBEDTLS_SHA1_HARDWARE) {
-        esp_sha_block(SHA1, data, first_block);
-    } else {
-        mbedtls_sha1_software_process(ctx, data);
-    }
-
-    return 0;
-}
-
 static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
 {
     uint32_t temp, W[16], A, B, C, D, E;
@@ -301,12 +277,46 @@ static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsi
     ctx->state[4] += E;
 }
 
+
+static int esp_internal_sha1_parallel_engine_process( mbedtls_sha1_context *ctx, const unsigned char data[64], bool read_digest )
+{
+    bool first_block = false;
+
+    if (ctx->mode == ESP_MBEDTLS_SHA1_UNUSED) {
+        /* try to use hardware for this digest */
+        if (esp_sha_try_lock_engine(SHA1)) {
+            ctx->mode = ESP_MBEDTLS_SHA1_HARDWARE;
+            first_block = true;
+        } else {
+            ctx->mode = ESP_MBEDTLS_SHA1_SOFTWARE;
+        }
+    }
+
+    if (ctx->mode == ESP_MBEDTLS_SHA1_HARDWARE) {
+        esp_sha_block(SHA1, data, first_block);
+        if (read_digest) {
+            esp_sha_read_digest_state(SHA1, ctx->state);
+        }
+    } else {
+        mbedtls_sha1_software_process(ctx, data);
+    }
+
+    return 0;
+}
+
+
+int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
+{
+    return esp_internal_sha1_parallel_engine_process(ctx, data, true);
+}
+
+
 /*
  * SHA-1 process buffer
  */
 int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
 {
-    int ret;
+    int ret = -1;
     size_t fill;
     uint32_t left;
 
@@ -327,7 +337,7 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
     if ( left && ilen >= fill ) {
         memcpy( (void *) (ctx->buffer + left), input, fill );
 
-        if ( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) {
+        if ( ( ret = esp_internal_sha1_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
             return ret;
         }
 
@@ -337,7 +347,7 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
     }
 
     while ( ilen >= 64 ) {
-        if ( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 ) {
+        if ( ( ret = esp_internal_sha1_parallel_engine_process( ctx, input, false ) ) != 0 ) {
             return ret;
         }
 
@@ -345,6 +355,10 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
         ilen  -= 64;
     }
 
+    if (ctx->mode == ESP_MBEDTLS_SHA1_HARDWARE) {
+        esp_sha_read_digest_state(SHA1, ctx->state);
+    }
+
     if ( ilen > 0 ) {
         memcpy( (void *) (ctx->buffer + left), input, ilen );
     }
@@ -364,7 +378,7 @@ static const unsigned char sha1_padding[64] = {
  */
 int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
 {
-    int ret;
+    int ret = -1;
     uint32_t last, padn;
     uint32_t high, low;
     unsigned char msglen[8];

+ 43 - 29
components/mbedtls/port/sha/parallel_engine/esp_sha256.c

@@ -7,7 +7,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
@@ -179,30 +179,6 @@ static const uint32_t K[] = {
     d += temp1; h = temp1 + temp2;              \
 }
 
-static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
-
-int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
-{
-    bool first_block = false;
-
-    if (ctx->mode == ESP_MBEDTLS_SHA256_UNUSED) {
-        /* try to use hardware for this digest */
-        if (!ctx->is224 && esp_sha_try_lock_engine(SHA2_256)) {
-            ctx->mode = ESP_MBEDTLS_SHA256_HARDWARE;
-            first_block = true;
-        } else {
-            ctx->mode = ESP_MBEDTLS_SHA256_SOFTWARE;
-        }
-    }
-
-    if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
-        esp_sha_block(SHA2_256, data, first_block);
-    } else {
-        mbedtls_sha256_software_process(ctx, data);
-    }
-
-    return 0;
-}
 
 static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
 {
@@ -260,13 +236,47 @@ static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const
     }
 }
 
+
+static int esp_internal_sha256_parallel_engine_process( mbedtls_sha256_context *ctx, const unsigned char data[64], bool read_digest )
+{
+    bool first_block = false;
+
+    if (ctx->mode == ESP_MBEDTLS_SHA256_UNUSED) {
+        /* try to use hardware for this digest */
+        if (!ctx->is224 && esp_sha_try_lock_engine(SHA2_256)) {
+            ctx->mode = ESP_MBEDTLS_SHA256_HARDWARE;
+            first_block = true;
+        } else {
+            ctx->mode = ESP_MBEDTLS_SHA256_SOFTWARE;
+        }
+    }
+
+    if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
+        esp_sha_block(SHA2_256, data, first_block);
+        if (read_digest) {
+            esp_sha_read_digest_state(SHA2_256, ctx->state);
+        }
+    } else {
+        mbedtls_sha256_software_process(ctx, data);
+    }
+
+    return 0;
+}
+
+
+int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
+{
+    return esp_internal_sha256_parallel_engine_process(ctx, data, true);
+}
+
+
 /*
  * SHA-256 process buffer
  */
 int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
                                size_t ilen )
 {
-    int ret;
+    int ret = -1;
     size_t fill;
     uint32_t left;
 
@@ -287,7 +297,7 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
     if ( left && ilen >= fill ) {
         memcpy( (void *) (ctx->buffer + left), input, fill );
 
-        if ( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) {
+        if ( ( ret = esp_internal_sha256_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
             return ret;
         }
 
@@ -297,7 +307,7 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
     }
 
     while ( ilen >= 64 ) {
-        if ( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 ) {
+        if ( ( ret = esp_internal_sha256_parallel_engine_process( ctx, input, false ) ) != 0 ) {
             return ret;
         }
 
@@ -305,6 +315,10 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
         ilen  -= 64;
     }
 
+    if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
+        esp_sha_read_digest_state(SHA2_256, ctx->state);
+    }
+
     if ( ilen > 0 ) {
         memcpy( (void *) (ctx->buffer + left), input, ilen );
     }
@@ -324,7 +338,7 @@ static const unsigned char sha256_padding[64] = {
  */
 int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
 {
-    int ret;
+    int ret = -1;
     uint32_t last, padn;
     uint32_t high, low;
     unsigned char msglen[8];

+ 43 - 29
components/mbedtls/port/sha/parallel_engine/esp_sha512.c

@@ -7,7 +7,7 @@
  *
  * SPDX-License-Identifier: Apache-2.0
  *
- * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  */
 /*
  *  The SHA-512 Secure Hash Standard was published by NIST in 2002.
@@ -206,30 +206,6 @@ static const uint64_t K[80] = {
     UL64(0x5FCB6FAB3AD6FAEC),  UL64(0x6C44198C4A475817)
 };
 
-static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const unsigned char data[128] );
-
-int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
-{
-    bool first_block = false;
-
-    if (ctx->mode == ESP_MBEDTLS_SHA512_UNUSED) {
-        /* try to use hardware for this digest */
-        if (esp_sha_try_lock_engine(sha_type(ctx))) {
-            ctx->mode = ESP_MBEDTLS_SHA512_HARDWARE;
-            first_block = true;
-        } else {
-            ctx->mode = ESP_MBEDTLS_SHA512_SOFTWARE;
-        }
-    }
-
-    if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
-        esp_sha_block(sha_type(ctx), data, first_block);
-    } else {
-        mbedtls_sha512_software_process(ctx, data);
-    }
-
-    return 0;
-}
 
 static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
 {
@@ -296,13 +272,47 @@ static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const
     ctx->state[7] += H;
 }
 
+
+static int esp_internal_sha512_parallel_engine_process( mbedtls_sha512_context *ctx, const unsigned char data[128], bool read_digest )
+{
+    bool first_block = false;
+
+    if (ctx->mode == ESP_MBEDTLS_SHA512_UNUSED) {
+        /* try to use hardware for this digest */
+        if (esp_sha_try_lock_engine(sha_type(ctx))) {
+            ctx->mode = ESP_MBEDTLS_SHA512_HARDWARE;
+            first_block = true;
+        } else {
+            ctx->mode = ESP_MBEDTLS_SHA512_SOFTWARE;
+        }
+    }
+
+    if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
+        esp_sha_block(sha_type(ctx), data, first_block);
+        if (read_digest) {
+            esp_sha_read_digest_state(sha_type(ctx), ctx->state);
+        }
+    } else {
+        mbedtls_sha512_software_process(ctx, data);
+    }
+
+    return 0;
+}
+
+
+int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
+{
+    return esp_internal_sha512_parallel_engine_process(ctx, data, true);
+}
+
+
 /*
  * SHA-512 process buffer
  */
 int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
                                size_t ilen )
 {
-    int ret;
+    int ret = -1;
     size_t fill;
     unsigned int left;
 
@@ -321,7 +331,7 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
 
     if ( left && ilen >= fill ) {
         memcpy( (void *) (ctx->buffer + left), input, fill );
-        if ( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) {
+        if ( ( ret = esp_internal_sha512_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
             return ret;
         }
 
@@ -331,7 +341,7 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
     }
 
     while ( ilen >= 128 ) {
-        if ( ( ret = mbedtls_internal_sha512_process( ctx, input ) ) != 0 ) {
+        if ( ( ret = esp_internal_sha512_parallel_engine_process( ctx, input, false ) ) != 0 ) {
             return ret;
         }
 
@@ -339,6 +349,10 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
         ilen  -= 128;
     }
 
+    if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
+        esp_sha_read_digest_state(sha_type(ctx), ctx->state);
+    }
+
     if ( ilen > 0 ) {
         memcpy( (void *) (ctx->buffer + left), input, ilen );
     }
@@ -362,7 +376,7 @@ static const unsigned char sha512_padding[128] = {
  */
 int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
 {
-    int ret;
+    int ret = -1;
     size_t last, padn;
     uint64_t high, low;
     unsigned char msglen[16];

+ 88 - 0
components/mbedtls/test_apps/main/test_sha.c

@@ -142,4 +142,92 @@ TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
 #endif
 }
 
+
+#if CONFIG_MBEDTLS_HARDWARE_SHA
+
+TEST_CASE("Test mbedtls_internal_sha_process()", "[hw_crypto]")
+{
+    const size_t BUFFER_SZ = 128;
+    int ret;
+    unsigned char output[64] = { 0 };
+    void *buffer = heap_caps_malloc(BUFFER_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
+    TEST_ASSERT_NOT_NULL(buffer);
+    memset(buffer, 0xEE, BUFFER_SZ);
+
+    mbedtls_sha1_context sha1_ctx;
+
+    const uint8_t sha1_expected[20] = { 0x41, 0x63, 0x12, 0x5b, 0x9c, 0x68, 0x85, 0xc8,
+                                        0x01, 0x40, 0xf4, 0x03, 0x5d, 0x0d, 0x84, 0x0e,
+                                        0xa4, 0xae, 0x4d, 0xe9 };
+
+    mbedtls_sha1_init(&sha1_ctx);
+    mbedtls_sha1_starts(&sha1_ctx);
+
+    ret = mbedtls_internal_sha1_process(&sha1_ctx, buffer);
+    TEST_ASSERT_EQUAL(0, ret);
+
+    ret = mbedtls_internal_sha1_process(&sha1_ctx, buffer);
+    TEST_ASSERT_EQUAL(0, ret);
+
+#if SOC_SHA_ENDIANNESS_BE
+    for (int i = 0; i < sizeof(sha1_ctx.state)/sizeof(sha1_ctx.state[0]); i++)
+    {
+        *(uint32_t *)(output + i*4) = __builtin_bswap32(sha1_ctx.state[i]);
+    }
+#else
+    memcpy(output, sha1_ctx.state, 20);
+#endif
+
+    // Check if the intermediate states are correct
+    TEST_ASSERT_EQUAL_HEX8_ARRAY(sha1_expected, output, sizeof(sha1_expected));
+
+    ret = mbedtls_sha1_finish(&sha1_ctx, output);
+    TEST_ASSERT_EQUAL(0, ret);
+
+    mbedtls_sha1_free(&sha1_ctx);
+
+#if SOC_SHA_SUPPORT_SHA512
+    mbedtls_sha512_context sha512_ctx;
+
+    const uint8_t sha512_expected[64] = { 0x3c, 0x77, 0x5f, 0xb0, 0x3b, 0x25, 0x8d, 0x3b,
+                                        0xa9, 0x28, 0xa2, 0x29, 0xf2, 0x14, 0x7d, 0xb3,
+                                        0x64, 0x1e, 0x76, 0xd5, 0x0b, 0xbc, 0xdf, 0xb4,
+                                        0x75, 0x1d, 0xe7, 0x7f, 0x62, 0x83, 0xdd, 0x78,
+                                        0x6b, 0x0e, 0xa4, 0xd2, 0xbe, 0x51, 0x56, 0xd4,
+                                        0xfe, 0x3b, 0xa3, 0x3a, 0xd7, 0xf6, 0xd3, 0xb3,
+                                        0xe7, 0x9d, 0xb5, 0xe6, 0x76, 0x35, 0x2a, 0xae,
+                                        0x07, 0x0a, 0x3a, 0x03, 0x44, 0xf0, 0xb8, 0xfe };
+
+    mbedtls_sha512_init(&sha512_ctx);
+    mbedtls_sha512_starts(&sha512_ctx, 0);
+
+    ret = mbedtls_internal_sha512_process(&sha512_ctx, buffer);
+    TEST_ASSERT_EQUAL(0, ret);
+
+    ret = mbedtls_internal_sha512_process(&sha512_ctx, buffer);
+    TEST_ASSERT_EQUAL(0, ret);
+
+#if SOC_SHA_ENDIANNESS_BE
+    for (int i = 0; i < sizeof(sha512_ctx.state)/sizeof(sha512_ctx.state[0]); i++)
+    {
+        *(uint64_t *)(output + i*8) = __builtin_bswap64(sha512_ctx.state[i]);
+    }
+#else
+    memcpy(output, sha512_ctx.state, 64);
+#endif
+
+    // Check if the intermediate states are correct
+    TEST_ASSERT_EQUAL_HEX8_ARRAY(sha512_expected, output, sizeof(sha512_expected));
+
+    ret = mbedtls_sha512_finish(&sha512_ctx, output);
+    TEST_ASSERT_EQUAL(0, ret);
+
+    mbedtls_sha512_free(&sha512_ctx);
+
+#endif
+    free(buffer);
+
+}
+#endif
+
 #endif // SOC_SHA_SUPPORTED

+ 4 - 0
components/soc/esp32/include/soc/Kconfig.soc_caps.in

@@ -691,6 +691,10 @@ config SOC_SHA_SUPPORT_PARALLEL_ENG
     bool
     default y
 
+config SOC_SHA_ENDIANNESS_BE
+    bool
+    default y
+
 config SOC_SHA_SUPPORT_SHA1
     bool
     default y

+ 3 - 0
components/soc/esp32/include/soc/soc_caps.h

@@ -338,6 +338,9 @@
 /* ESP32 style SHA engine, where multiple states can be stored in parallel */
 #define SOC_SHA_SUPPORT_PARALLEL_ENG    (1)
 
+/* ESP32's SHA peripheral processes and stores data in big-endian format */
+#define SOC_SHA_ENDIANNESS_BE           (1)
+
 /* Supported HW algorithms */
 #define SOC_SHA_SUPPORT_SHA1            (1)
 #define SOC_SHA_SUPPORT_SHA256          (1)

+ 3 - 0
components/soc/esp32c2/include/soc/soc_caps.h

@@ -185,6 +185,9 @@
 
 /*--------------------------- SHA CAPS ---------------------------------------*/
 
+/* Due to very limited availability of the DMA channels, DMA support for the SHA peripheral is disabled */
+// #define SOC_SHA_SUPPORT_DMA             (1)
+
 /* The SHA engine is able to resume hashing from a user */
 #define SOC_SHA_SUPPORT_RESUME          (1)