Add _init() and _free() for hash modules
diff --git a/library/sha512.c b/library/sha512.c
index a29c80a..f1d1525 100644
--- a/library/sha512.c
+++ b/library/sha512.c
@@ -133,6 +133,19 @@
UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
};
+void sha512_init( sha512_context *ctx )
+{
+ memset( ctx, 0, sizeof( sha512_context ) );
+}
+
+void sha512_free( sha512_context *ctx )
+{
+ if( ctx == NULL )
+ return;
+
+ polarssl_zeroize( ctx, sizeof( sha512_context ) );
+}
+
/*
* SHA-512 context setup
*/
@@ -336,11 +349,11 @@
{
sha512_context ctx;
+ sha512_init( &ctx );
sha512_starts( &ctx, is384 );
sha512_update( &ctx, input, ilen );
sha512_finish( &ctx, output );
-
- polarssl_zeroize( &ctx, sizeof( sha512_context ) );
+ sha512_free( &ctx );
}
#if defined(POLARSSL_FS_IO)
@@ -357,14 +370,14 @@
if( ( f = fopen( path, "rb" ) ) == NULL )
return( POLARSSL_ERR_SHA512_FILE_IO_ERROR );
+ sha512_init( &ctx );
sha512_starts( &ctx, is384 );
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
sha512_update( &ctx, buf, n );
sha512_finish( &ctx, output );
-
- polarssl_zeroize( &ctx, sizeof( sha512_context ) );
+ sha512_free( &ctx );
if( ferror( f ) != 0 )
{
@@ -455,11 +468,11 @@
{
sha512_context ctx;
+ sha512_init( &ctx );
sha512_hmac_starts( &ctx, key, keylen, is384 );
sha512_hmac_update( &ctx, input, ilen );
sha512_hmac_finish( &ctx, output );
-
- polarssl_zeroize( &ctx, sizeof( sha512_context ) );
+ sha512_free( &ctx );
}
#if defined(POLARSSL_SELF_TEST)
@@ -686,11 +699,13 @@
*/
int sha512_self_test( int verbose )
{
- int i, j, k, buflen;
+ int i, j, k, buflen, ret = 0;
unsigned char buf[1024];
unsigned char sha512sum[64];
sha512_context ctx;
+ sha512_init( &ctx );
+
for( i = 0; i < 6; i++ )
{
j = i % 3;
@@ -719,7 +734,8 @@
if( verbose != 0 )
polarssl_printf( "failed\n" );
- return( 1 );
+ ret = 1;
+ goto exit;
}
if( verbose != 0 )
@@ -758,7 +774,8 @@
if( verbose != 0 )
polarssl_printf( "failed\n" );
- return( 1 );
+ ret = 1;
+ goto exit;
}
if( verbose != 0 )
@@ -768,7 +785,10 @@
if( verbose != 0 )
polarssl_printf( "\n" );
- return( 0 );
+exit:
+ sha512_free( &ctx );
+
+ return( ret );
}
#endif /* POLARSSL_SELF_TEST */