Introduce polarssl_zeroize() instead of memset() for zeroization
diff --git a/library/sha512.c b/library/sha512.c
index c13e0d9..a29c80a 100644
--- a/library/sha512.c
+++ b/library/sha512.c
@@ -48,6 +48,11 @@
#define polarssl_printf printf
#endif
+/* Implementation that should never be optimized out by the compiler */
+static void polarssl_zeroize( void *v, size_t n ) {
+ volatile unsigned char *p = v; while( n-- ) *p++ = 0;
+}
+
#if !defined(POLARSSL_SHA512_ALT)
/*
@@ -335,7 +340,7 @@
sha512_update( &ctx, input, ilen );
sha512_finish( &ctx, output );
- memset( &ctx, 0, sizeof( sha512_context ) );
+ polarssl_zeroize( &ctx, sizeof( sha512_context ) );
}
#if defined(POLARSSL_FS_IO)
@@ -359,7 +364,7 @@
sha512_finish( &ctx, output );
- memset( &ctx, 0, sizeof( sha512_context ) );
+ polarssl_zeroize( &ctx, sizeof( sha512_context ) );
if( ferror( f ) != 0 )
{
@@ -400,7 +405,7 @@
sha512_starts( ctx, is384 );
sha512_update( ctx, ctx->ipad, 128 );
- memset( sum, 0, sizeof( sum ) );
+ polarssl_zeroize( sum, sizeof( sum ) );
}
/*
@@ -429,7 +434,7 @@
sha512_update( ctx, tmpbuf, hlen );
sha512_finish( ctx, output );
- memset( tmpbuf, 0, sizeof( tmpbuf ) );
+ polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
}
/*
@@ -454,7 +459,7 @@
sha512_hmac_update( &ctx, input, ilen );
sha512_hmac_finish( &ctx, output );
- memset( &ctx, 0, sizeof( sha512_context ) );
+ polarssl_zeroize( &ctx, sizeof( sha512_context ) );
}
#if defined(POLARSSL_SELF_TEST)