From d5253bba324b2a31ad752f152ac424ccbf5dc00f Mon Sep 17 00:00:00 2001 From: gabor-mezei-arm Date: Thu, 30 Jul 2020 16:41:25 +0200 Subject: [PATCH] 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 --- ...oizations_of_sensitive_data_in_PKCS5_and_SHA.txt | 5 +++++ library/pkcs5.c | 4 ++++ library/sha1.c | 9 +++++++++ library/sha256.c | 6 ++++++ library/sha512.c | 13 +++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 ChangeLog.d/zeroizations_of_sensitive_data_in_PKCS5_and_SHA.txt 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 000000000..f8445615c --- /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 8a80aa5d0..69b85deeb 100644 --- a/library/pkcs5.c +++ b/library/pkcs5.c @@ -312,6 +312,10 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *p 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 8682abd74..4bb51982f 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -313,6 +313,15 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, 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 5169584b6..2decd08f0 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -281,6 +281,12 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, 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 36d5d9614..37a01dd7b 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -312,6 +312,19 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, ctx->state[6] += G; ctx->state[7] += H; + /* 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( &F, sizeof( F ) ); + mbedtls_platform_zeroize( &G, sizeof( G ) ); + mbedtls_platform_zeroize( &H, sizeof( H ) ); + mbedtls_platform_zeroize( &W, sizeof( W ) ); + mbedtls_platform_zeroize( &temp1, sizeof( temp1 ) ); + mbedtls_platform_zeroize( &temp2, sizeof( temp2 ) ); + return( 0 ); }