Introduce polarssl_zeroize() instead of memset() for zeroization
diff --git a/library/md2.c b/library/md2.c
index 2c8754a..8646f66 100644
--- a/library/md2.c
+++ b/library/md2.c
@@ -39,6 +39,11 @@
#include <stdio.h>
#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_MD2_ALT)
static const unsigned char PI_SUBST[256] =
@@ -178,7 +183,7 @@
md2_update( &ctx, input, ilen );
md2_finish( &ctx, output );
- memset( &ctx, 0, sizeof( md2_context ) );
+ polarssl_zeroize( &ctx, sizeof( md2_context ) );
}
#if defined(POLARSSL_FS_IO)
@@ -202,7 +207,7 @@
md2_finish( &ctx, output );
- memset( &ctx, 0, sizeof( md2_context ) );
+ polarssl_zeroize( &ctx, sizeof( md2_context ) );
if( ferror( f ) != 0 )
{
@@ -242,7 +247,7 @@
md2_starts( ctx );
md2_update( ctx, ctx->ipad, 16 );
- memset( sum, 0, sizeof( sum ) );
+ polarssl_zeroize( sum, sizeof( sum ) );
}
/*
@@ -266,7 +271,7 @@
md2_update( ctx, tmpbuf, 16 );
md2_finish( ctx, output );
- memset( tmpbuf, 0, sizeof( tmpbuf ) );
+ polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
}
/*
@@ -291,7 +296,7 @@
md2_hmac_update( &ctx, input, ilen );
md2_hmac_finish( &ctx, output );
- memset( &ctx, 0, sizeof( md2_context ) );
+ polarssl_zeroize( &ctx, sizeof( md2_context ) );
}
#if defined(POLARSSL_SELF_TEST)