]> granicus.if.org Git - esp-idf/commitdiff
hwcrypto sha: Fix initialisation of SHA hardware in esp_shaX_start functions
authorAngus Gratton <angus@espressif.com>
Fri, 9 Sep 2016 04:27:34 +0000 (14:27 +1000)
committerAngus Gratton <angus@espressif.com>
Fri, 9 Sep 2016 04:27:53 +0000 (14:27 +1000)
Problem exposed by previous commit.

components/esp32/hwcrypto/sha.c

index 78ecbf77141ab76be3d82277c26f3add32b7c319..06b00c54ab692f48309d026f26e485eefa27c698 100644 (file)
@@ -86,8 +86,9 @@ void esp_sha1_clone( esp_sha_context *dst, const esp_sha_context *src )
  */
 void esp_sha1_start( esp_sha_context *ctx )
 {
-    esp_sha_acquire_hardware();
     ctx->context_type = SHA1;
+    esp_sha_acquire_hardware();
+    ets_sha_init(&ctx->context);
 }
 
 /*
@@ -143,12 +144,11 @@ void esp_sha256_clone( esp_sha_context *dst, const esp_sha_context *src )
  */
 void esp_sha256_start( esp_sha_context *ctx, int is224 )
 {
-    esp_sha_acquire_hardware();
-    ets_sha_init(&ctx->context);
-
     if ( is224 == 0 ) {
         /* SHA-256 */
         ctx->context_type = SHA2_256;
+        esp_sha_acquire_hardware();
+        ets_sha_init(&ctx->context);
     } else {
         /* SHA-224 is not supported! */
         ctx->context_type = SHA_INVALID;
@@ -160,7 +160,10 @@ void esp_sha256_start( esp_sha_context *ctx, int is224 )
  */
 void esp_sha256_update( esp_sha_context *ctx, const unsigned char *input, size_t ilen )
 {
-    esp_sha_update(ctx, input, ilen, 64);
+    if( ctx->context_type == SHA2_256 ) {
+        esp_sha_update(ctx, input, ilen, 64);
+    }
+    /* SHA-224 is a no-op */
 }
 
 /*
@@ -170,12 +173,12 @@ void esp_sha256_finish( esp_sha_context *ctx, unsigned char output[32] )
 {
     if ( ctx->context_type == SHA2_256 ) {
         ets_sha_finish(&ctx->context, ctx->context_type, output);
+        esp_sha_release_hardware();
     } else {
         /* No hardware SHA-224 support, but mbedTLS API doesn't allow failure.
            For now, zero the output to make it clear it's not valid. */
         bzero( output, 28 );
     }
-    esp_sha_release_hardware();
 }
 
 /*
@@ -218,9 +221,6 @@ void esp_sha512_clone( esp_sha_context *dst, const esp_sha_context *src )
  */
 void esp_sha512_start( esp_sha_context *ctx, int is384 )
 {
-    esp_sha_acquire_hardware();
-    ets_sha_init(&ctx->context);
-
     if ( is384 == 0 ) {
         /* SHA-512 */
         ctx->context_type = SHA2_512;
@@ -228,6 +228,8 @@ void esp_sha512_start( esp_sha_context *ctx, int is384 )
         /* SHA-384 */
         ctx->context_type = SHA2_384;
     }
+    esp_sha_acquire_hardware();
+    ets_sha_init(&ctx->context);
 }
 
 /*