From 3d98b9744272652609931b451bcde71ec0d1392a Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 20 Sep 2017 11:47:49 +0100 Subject: [PATCH] Modify zeroize internal buffers in md modules Modify all the following functions to zeroize an internal buffer before exiting the function. The buffer could potentially contain confidential data read from a file. * md2_file() * md4_file() * md5_file() * ripemd160_file() * sha1_file() * sha256_file() * sha512_file() --- library/md2.c | 16 ++++++++-------- library/md4.c | 16 ++++++++-------- library/md5.c | 16 ++++++++-------- library/ripemd160.c | 16 ++++++++-------- library/sha1.c | 16 ++++++++-------- library/sha256.c | 16 ++++++++-------- library/sha512.c | 16 ++++++++-------- 7 files changed, 56 insertions(+), 56 deletions(-) diff --git a/library/md2.c b/library/md2.c index 2ac7eba61..2d6123f12 100644 --- a/library/md2.c +++ b/library/md2.c @@ -217,6 +217,7 @@ void md2( const unsigned char *input, size_t ilen, unsigned char output[16] ) */ int md2_file( const char *path, unsigned char output[16] ) { + int ret = 0; FILE *f; size_t n; md2_context ctx; @@ -231,17 +232,16 @@ int md2_file( const char *path, unsigned char output[16] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) md2_update( &ctx, buf, n ); - md2_finish( &ctx, output ); - md2_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_MD2_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_MD2_FILE_IO_ERROR; + else + md2_finish( &ctx, output ); + md2_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/md4.c b/library/md4.c index 8754d2f52..9c4a9b80a 100644 --- a/library/md4.c +++ b/library/md4.c @@ -313,6 +313,7 @@ void md4( const unsigned char *input, size_t ilen, unsigned char output[16] ) */ int md4_file( const char *path, unsigned char output[16] ) { + int ret = 0; FILE *f; size_t n; md4_context ctx; @@ -327,17 +328,16 @@ int md4_file( const char *path, unsigned char output[16] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) md4_update( &ctx, buf, n ); - md4_finish( &ctx, output ); - md4_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_MD4_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_MD4_FILE_IO_ERROR; + else + md4_finish( &ctx, output ); + md4_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/md5.c b/library/md5.c index 8c7ed1fc3..4a0f25191 100644 --- a/library/md5.c +++ b/library/md5.c @@ -330,6 +330,7 @@ void md5( const unsigned char *input, size_t ilen, unsigned char output[16] ) */ int md5_file( const char *path, unsigned char output[16] ) { + int ret = 0; FILE *f; size_t n; md5_context ctx; @@ -344,17 +345,16 @@ int md5_file( const char *path, unsigned char output[16] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) md5_update( &ctx, buf, n ); - md5_finish( &ctx, output ); - md5_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_MD5_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_MD5_FILE_IO_ERROR; + else + md5_finish( &ctx, output ); + md5_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/ripemd160.c b/library/ripemd160.c index 2c196f42b..7b5d02e2e 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -388,6 +388,7 @@ void ripemd160( const unsigned char *input, size_t ilen, */ int ripemd160_file( const char *path, unsigned char output[20] ) { + int ret = 0; FILE *f; size_t n; ripemd160_context ctx; @@ -402,17 +403,16 @@ int ripemd160_file( const char *path, unsigned char output[20] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) ripemd160_update( &ctx, buf, n ); - ripemd160_finish( &ctx, output ); - ripemd160_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR; + else + ripemd160_finish( &ctx, output ); + ripemd160_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/sha1.c b/library/sha1.c index 44de8727e..a5a235b64 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -363,6 +363,7 @@ void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ) */ int sha1_file( const char *path, unsigned char output[20] ) { + int ret = 0; FILE *f; size_t n; sha1_context ctx; @@ -377,17 +378,16 @@ int sha1_file( const char *path, unsigned char output[20] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) sha1_update( &ctx, buf, n ); - sha1_finish( &ctx, output ); - sha1_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA1_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_SHA1_FILE_IO_ERROR; + else + sha1_finish( &ctx, output ); + sha1_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/sha256.c b/library/sha256.c index 674fdf25e..caae79f9b 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -366,6 +366,7 @@ void sha256( const unsigned char *input, size_t ilen, */ int sha256_file( const char *path, unsigned char output[32], int is224 ) { + int ret = 0; FILE *f; size_t n; sha256_context ctx; @@ -380,17 +381,16 @@ int sha256_file( const char *path, unsigned char output[32], int is224 ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) sha256_update( &ctx, buf, n ); - sha256_finish( &ctx, output ); - sha256_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA256_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_SHA256_FILE_IO_ERROR; + else + sha256_finish( &ctx, output ); + sha256_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/sha512.c b/library/sha512.c index bd607e0ca..5e51f7f0b 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -370,6 +370,7 @@ void sha512( const unsigned char *input, size_t ilen, */ int sha512_file( const char *path, unsigned char output[64], int is384 ) { + int ret = 0; FILE *f; size_t n; sha512_context ctx; @@ -384,17 +385,16 @@ int sha512_file( const char *path, unsigned char output[64], int is384 ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) sha512_update( &ctx, buf, n ); - sha512_finish( &ctx, output ); - sha512_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA512_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_SHA512_FILE_IO_ERROR; + else + sha512_finish( &ctx, output ); + sha512_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */