Add _init() and _free() for hash modules
diff --git a/library/sha256.c b/library/sha256.c
index 5633f9e..4fc6698 100644
--- a/library/sha256.c
+++ b/library/sha256.c
@@ -78,6 +78,19 @@
}
#endif
+void sha256_init( sha256_context *ctx )
+{
+ memset( ctx, 0, sizeof( sha256_context ) );
+}
+
+void sha256_free( sha256_context *ctx )
+{
+ if( ctx == NULL )
+ return;
+
+ polarssl_zeroize( ctx, sizeof( sha256_context ) );
+}
+
/*
* SHA-256 context setup
*/
@@ -338,11 +351,11 @@
{
sha256_context ctx;
+ sha256_init( &ctx );
sha256_starts( &ctx, is224 );
sha256_update( &ctx, input, ilen );
sha256_finish( &ctx, output );
-
- polarssl_zeroize( &ctx, sizeof( sha256_context ) );
+ sha256_free( &ctx );
}
#if defined(POLARSSL_FS_IO)
@@ -359,14 +372,14 @@
if( ( f = fopen( path, "rb" ) ) == NULL )
return( POLARSSL_ERR_SHA256_FILE_IO_ERROR );
+ sha256_init( &ctx );
sha256_starts( &ctx, is224 );
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
sha256_update( &ctx, buf, n );
sha256_finish( &ctx, output );
-
- polarssl_zeroize( &ctx, sizeof( sha256_context ) );
+ sha256_free( &ctx );
if( ferror( f ) != 0 )
{
@@ -457,11 +470,11 @@
{
sha256_context ctx;
+ sha256_init( &ctx );
sha256_hmac_starts( &ctx, key, keylen, is224 );
sha256_hmac_update( &ctx, input, ilen );
sha256_hmac_finish( &ctx, output );
-
- polarssl_zeroize( &ctx, sizeof( sha256_context ) );
+ sha256_free( &ctx );
}
#if defined(POLARSSL_SELF_TEST)
@@ -632,11 +645,13 @@
*/
int sha256_self_test( int verbose )
{
- int i, j, k, buflen;
+ int i, j, k, buflen, ret = 0;
unsigned char buf[1024];
unsigned char sha256sum[32];
sha256_context ctx;
+ sha256_init( &ctx );
+
for( i = 0; i < 6; i++ )
{
j = i % 3;
@@ -665,7 +680,8 @@
if( verbose != 0 )
polarssl_printf( "failed\n" );
- return( 1 );
+ ret = 1;
+ goto exit;
}
if( verbose != 0 )
@@ -704,7 +720,8 @@
if( verbose != 0 )
polarssl_printf( "failed\n" );
- return( 1 );
+ ret = 1;
+ goto exit;
}
if( verbose != 0 )
@@ -714,7 +731,10 @@
if( verbose != 0 )
polarssl_printf( "\n" );
- return( 0 );
+exit:
+ sha256_free( &ctx );
+
+ return( ret );
}
#endif /* POLARSSL_SELF_TEST */