Zeroize internal buffers and variables in PKCS and SHA

Zeroising of local buffers and variables which are used for calculations in
mbedtls_pkcs5_pbkdf2_hmac() and mbedtls_internal_sha*_process() functions
to erase sensitive data from memory.
Checked all function for possible missing zeroisation in PKCS and SHA.

Signed-off-by: gabor-mezei-arm <gabor.mezei@arm.com>
diff --git a/ChangeLog.d/zeroizations_of_sensitive_data_in_PKCS5_and_SHA.txt b/ChangeLog.d/zeroizations_of_sensitive_data_in_PKCS5_and_SHA.txt
new file mode 100644
index 0000000..f844561
--- /dev/null
+++ b/ChangeLog.d/zeroizations_of_sensitive_data_in_PKCS5_and_SHA.txt
@@ -0,0 +1,5 @@
+Security
+   * Zeroising of local buffers and variables which are used for calculations
+     in mbedtls_pkcs5_pbkdf2_hmac() and mbedtls_internal_sha*_process()
+     functions to erase sensitive data from memory. Reported by
+     Johan Malmgren and Johan Uppman Bruce from Sectra.
diff --git a/library/pkcs5.c b/library/pkcs5.c
index fc52248..049d27b 100644
--- a/library/pkcs5.c
+++ b/library/pkcs5.c
@@ -290,6 +290,10 @@
                 break;
     }
 
+    /* Zeroise buffers to clear sensitive data from memory. */
+    mbedtls_platform_zeroize( work, MBEDTLS_MD_MAX_SIZE );
+    mbedtls_platform_zeroize( md1, MBEDTLS_MD_MAX_SIZE );
+
     return( 0 );
 }
 
diff --git a/library/sha1.c b/library/sha1.c
index 79bac6b..c973455 100644
--- a/library/sha1.c
+++ b/library/sha1.c
@@ -285,6 +285,15 @@
     ctx->state[3] += D;
     ctx->state[4] += E;
 
+    /* Zeroise buffers and variables to clear sensitive data from memory. */
+    mbedtls_platform_zeroize( &A, sizeof( A ) );
+    mbedtls_platform_zeroize( &B, sizeof( B ) );
+    mbedtls_platform_zeroize( &C, sizeof( C ) );
+    mbedtls_platform_zeroize( &D, sizeof( D ) );
+    mbedtls_platform_zeroize( &E, sizeof( E ) );
+    mbedtls_platform_zeroize( &W, sizeof( W ) );
+    mbedtls_platform_zeroize( &temp, sizeof( temp ) );
+
     return( 0 );
 }
 
diff --git a/library/sha256.c b/library/sha256.c
index d8ddda5..0124fb7 100644
--- a/library/sha256.c
+++ b/library/sha256.c
@@ -253,6 +253,12 @@
     for( i = 0; i < 8; i++ )
         ctx->state[i] += A[i];
 
+    /* Zeroise buffers and variables to clear sensitive data from memory. */
+    mbedtls_platform_zeroize( &A, sizeof( A ) );
+    mbedtls_platform_zeroize( &W, sizeof( W ) );
+    mbedtls_platform_zeroize( &temp1, sizeof( temp1 ) );
+    mbedtls_platform_zeroize( &temp2, sizeof( temp2 ) );
+
     return( 0 );
 }
 
diff --git a/library/sha512.c b/library/sha512.c
index 37fc96d..08f4dd5 100644
--- a/library/sha512.c
+++ b/library/sha512.c
@@ -311,6 +311,12 @@
     for( i = 0; i < 8; i++ )
         ctx->state[i] += A[i];
 
+    /* Zeroise buffers and variables to clear sensitive data from memory. */
+    mbedtls_platform_zeroize( &A, sizeof( A ) );
+    mbedtls_platform_zeroize( &W, sizeof( W ) );
+    mbedtls_platform_zeroize( &temp1, sizeof( temp1 ) );
+    mbedtls_platform_zeroize( &temp2, sizeof( temp2 ) );
+
     return( 0 );
 }