From d2a2d61a68ed0dc4b94a8d7041208bb5b5fd7c4a Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Tue, 1 Jul 2014 15:45:49 +0200 Subject: [PATCH] Adapt programs / test suites --- programs/aes/crypt_and_hash.c | 8 ++-- programs/hash/generic_sum.c | 4 +- tests/suites/test_suite_cipher.function | 58 ++++++++++++++----------- tests/suites/test_suite_md.function | 32 ++++++++------ tests/suites/test_suite_pbkdf2.function | 6 ++- tests/suites/test_suite_pkcs5.function | 4 +- 6 files changed, 65 insertions(+), 47 deletions(-) diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c index 3e89ba6fc..f5484d410 100644 --- a/programs/aes/crypt_and_hash.c +++ b/programs/aes/crypt_and_hash.c @@ -95,8 +95,8 @@ int main( int argc, char *argv[] ) off_t filesize, offset; #endif - memset( &cipher_ctx, 0, sizeof( cipher_context_t )); - memset( &md_ctx, 0, sizeof( md_context_t )); + cipher_init( &cipher_ctx ); + md_init( &md_ctx ); /* * Parse the command-line arguments. @@ -533,8 +533,8 @@ exit: memset( buffer, 0, sizeof( buffer ) ); memset( digest, 0, sizeof( digest ) ); - cipher_free_ctx( &cipher_ctx ); - md_free_ctx( &md_ctx ); + cipher_free( &cipher_ctx ); + md_free( &md_ctx ); return( ret ); } diff --git a/programs/hash/generic_sum.c b/programs/hash/generic_sum.c index b5de2a1d6..4d007cf7a 100644 --- a/programs/hash/generic_sum.c +++ b/programs/hash/generic_sum.c @@ -165,7 +165,7 @@ int main( int argc, char *argv[] ) const md_info_t *md_info; md_context_t md_ctx; - memset( &md_ctx, 0, sizeof( md_context_t )); + md_init( &md_ctx ); if( argc == 1 ) { @@ -217,7 +217,7 @@ int main( int argc, char *argv[] ) ret |= generic_print( md_info, argv[i] ); exit: - md_free_ctx( &md_ctx ); + md_free( &md_ctx ); return( ret ); } diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function index 7c9c76d02..3afdff606 100644 --- a/tests/suites/test_suite_cipher.function +++ b/tests/suites/test_suite_cipher.function @@ -29,7 +29,7 @@ void cipher_null_args( ) unsigned char buf[1] = { 0 }; size_t olen; - memset( &ctx, 0, sizeof( cipher_context_t ) ); + cipher_init( &ctx ); TEST_ASSERT( cipher_get_block_size( NULL ) == 0 ); TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 ); @@ -111,8 +111,8 @@ void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, /* * Prepare contexts */ - memset( &ctx_dec, 0, sizeof( ctx_dec ) ); - memset( &ctx_enc, 0, sizeof( ctx_enc ) ); + cipher_init( &ctx_dec ); + cipher_init( &ctx_enc ); memset( key, 0x2a, sizeof( key ) ); @@ -207,8 +207,8 @@ void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, /* * Done */ - TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); - TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); + cipher_free( &ctx_dec ); + cipher_free( &ctx_enc ); } /* END_CASE */ @@ -231,7 +231,7 @@ void enc_fail( int cipher_id, int pad_mode, int key_len, memset( key, 0, 32 ); memset( iv , 0, 16 ); - memset( &ctx, 0, sizeof( ctx ) ); + cipher_init( &ctx ); memset( inbuf, 5, 64 ); memset( encbuf, 0, 64 ); @@ -259,7 +259,7 @@ void enc_fail( int cipher_id, int pad_mode, int key_len, TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) ); /* done */ - TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); + cipher_free( &ctx ); } /* END_CASE */ @@ -279,16 +279,16 @@ void dec_empty_buf() memset( key, 0, 32 ); memset( iv , 0, 16 ); - - memset( &ctx_dec, 0, sizeof( ctx_dec ) ); - + + cipher_init( &ctx_dec ); + memset( encbuf, 0, 64 ); memset( decbuf, 0, 64 ); /* Initialise context */ cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); TEST_ASSERT( NULL != cipher_info); - + TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); @@ -308,7 +308,7 @@ void dec_empty_buf() &ctx_dec, decbuf + outlen, &outlen ) ); TEST_ASSERT( 0 == outlen ); - TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); + cipher_free( &ctx_dec ); } /* END_CASE */ @@ -335,10 +335,10 @@ void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, memset( key, 0, 32 ); memset( iv , 0, 16 ); - - memset( &ctx_dec, 0, sizeof( ctx_dec ) ); - memset( &ctx_enc, 0, sizeof( ctx_enc ) ); - + + cipher_init( &ctx_dec ); + cipher_init( &ctx_enc ); + memset( inbuf, 5, 64 ); memset( encbuf, 0, 64 ); memset( decbuf, 0, 64 ); @@ -346,10 +346,10 @@ void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, /* Initialise enc and dec contexts */ cipher_info = cipher_info_from_type( cipher_id ); TEST_ASSERT( NULL != cipher_info); - + TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); - + TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); @@ -397,8 +397,8 @@ void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); - TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); - TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); + cipher_free( &ctx_dec ); + cipher_free( &ctx_enc ); } /* END_CASE */ @@ -423,6 +423,8 @@ void decrypt_test_vec( int cipher_id, int pad_mode, unsigned char output[200]; size_t outlen, total_len; + cipher_init( &ctx ); + memset( key, 0x00, sizeof( key ) ); memset( iv, 0x00, sizeof( iv ) ); memset( cipher, 0x00, sizeof( cipher ) ); @@ -477,7 +479,7 @@ void decrypt_test_vec( int cipher_id, int pad_mode, TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) ); } - cipher_free_ctx( &ctx ); + cipher_free( &ctx ); } /* END_CASE */ @@ -499,6 +501,8 @@ void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv, unsigned char output[200]; size_t outlen; + cipher_init( &ctx ); + memset( key, 0x00, sizeof( key ) ); memset( iv, 0x00, sizeof( iv ) ); memset( cipher, 0x00, sizeof( cipher ) ); @@ -563,7 +567,7 @@ void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv, cleanup: - cipher_free_ctx( &ctx ); + cipher_free( &ctx ); } /* END_CASE */ @@ -580,6 +584,8 @@ void test_vec_ecb( int cipher_id, int operation, char *hex_key, unsigned char output[32]; size_t outlen; + cipher_init( &ctx ); + memset( key, 0x00, sizeof( key ) ); memset( input, 0x00, sizeof( input ) ); memset( result, 0x00, sizeof( result ) ); @@ -610,7 +616,7 @@ void test_vec_ecb( int cipher_id, int operation, char *hex_key, TEST_ASSERT( 0 == memcmp( output, result, cipher_get_block_size( &ctx ) ) ); - cipher_free_ctx( &ctx ); + cipher_free( &ctx ); } /* END_CASE */ @@ -620,13 +626,15 @@ void set_padding( int cipher_id, int pad_mode, int ret ) const cipher_info_t *cipher_info; cipher_context_t ctx; + cipher_init( &ctx ); + cipher_info = cipher_info_from_type( cipher_id ); TEST_ASSERT( NULL != cipher_info ); TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) ); - TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); + cipher_free( &ctx ); } /* END_CASE */ @@ -639,7 +647,7 @@ void check_padding( int pad_mode, char *input_str, int ret, int dlen_check ) size_t ilen, dlen; /* build a fake context just for getting access to get_padding */ - memset( &ctx, 0, sizeof( ctx ) ); + cipher_init( &ctx ); cipher_info.mode = POLARSSL_MODE_CBC; ctx.cipher_info = &cipher_info; diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function index 9f064434c..338f8af2f 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -15,7 +15,7 @@ void md_process( ) md_context_t ctx; unsigned char buf[150]; - memset( &ctx, 0, sizeof ctx ); + md_init( &ctx ); /* * Very minimal testing of md_process, just make sure the various @@ -31,7 +31,7 @@ void md_process( ) TEST_ASSERT( info != NULL ); TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 ); TEST_ASSERT( md_process( &ctx, buf ) == 0 ); - TEST_ASSERT( md_free_ctx( &ctx ) == 0 ); + md_free( &ctx ); } } /* END_CASE */ @@ -43,7 +43,7 @@ void md_null_args( ) const md_info_t *info = md_info_from_type( *( md_list() ) ); unsigned char buf[1] = { 0 }; - memset( &ctx, 0, sizeof( md_context_t ) ); + md_init( &ctx ); TEST_ASSERT( md_get_size( NULL ) == 0 ); @@ -177,9 +177,11 @@ void md_text_multi( char *text_md_name, char *text_src_string, unsigned char src_str[1000]; unsigned char hash_str[1000]; unsigned char output[100]; - + const md_info_t *md_info = NULL; - md_context_t ctx = MD_CONTEXT_T_INIT; + md_context_t ctx; + + md_init( &ctx ); memset(md_name, 0x00, 100); memset(src_str, 0x00, 1000); @@ -196,8 +198,8 @@ void md_text_multi( char *text_md_name, char *text_src_string, TEST_ASSERT ( ctx.md_ctx != NULL ); TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) ); TEST_ASSERT ( 0 == md_finish( &ctx, output ) ); - TEST_ASSERT ( 0 == md_free_ctx( &ctx ) ); - + md_free( &ctx ); + hexify( hash_str, output, md_get_size(md_info) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); @@ -214,7 +216,9 @@ void md_hex_multi( char *text_md_name, char *hex_src_string, unsigned char output[100]; int src_len; const md_info_t *md_info = NULL; - md_context_t ctx = MD_CONTEXT_T_INIT; + md_context_t ctx; + + md_init( &ctx ); memset(md_name, 0x00, 100); memset(src_str, 0x00, 10000); @@ -227,13 +231,13 @@ void md_hex_multi( char *text_md_name, char *hex_src_string, TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) ); src_len = unhexify( src_str, hex_src_string ); - + TEST_ASSERT ( 0 == md_starts( &ctx ) ); TEST_ASSERT ( ctx.md_ctx != NULL ); TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) ); TEST_ASSERT ( 0 == md_finish( &ctx, output ) ); - TEST_ASSERT ( 0 == md_free_ctx( &ctx ) ); - + md_free( &ctx ); + hexify( hash_str, output, md_get_size(md_info) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); @@ -283,7 +287,9 @@ void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string, unsigned char output[100]; int key_len, src_len; const md_info_t *md_info = NULL; - md_context_t ctx = MD_CONTEXT_T_INIT; + md_context_t ctx; + + md_init( &ctx ); memset(md_name, 0x00, 100); memset(src_str, 0x00, 10000); @@ -314,7 +320,7 @@ void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string, TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) ); TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) ); TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) ); - TEST_ASSERT ( 0 == md_free_ctx( &ctx ) ); + md_free( &ctx ); hexify( hash_str, output, md_get_size(md_info) ); TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); diff --git a/tests/suites/test_suite_pbkdf2.function b/tests/suites/test_suite_pbkdf2.function index bd52bf91e..4b31896b8 100644 --- a/tests/suites/test_suite_pbkdf2.function +++ b/tests/suites/test_suite_pbkdf2.function @@ -17,10 +17,12 @@ void pbkdf2_hmac( int hash, char *hex_password_string, char *hex_salt_string, md_context_t ctx; const md_info_t *info; - + int pw_len, salt_len; unsigned char key[100]; + md_init( &ctx ); + memset(pw_str, 0x00, 100); memset(salt_str, 0x00, 100); memset(dst_str, 0x00, 100); @@ -36,7 +38,7 @@ void pbkdf2_hmac( int hash, char *hex_password_string, char *hex_salt_string, TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 ); TEST_ASSERT( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len, it_cnt, key_len, key ) == 0 ); - TEST_ASSERT( md_free_ctx( &ctx ) == 0 ); + md_free( &ctx ); hexify( dst_str, key, key_len ); TEST_ASSERT( strcmp( (char *) dst_str, result_key_string ) == 0 ); diff --git a/tests/suites/test_suite_pkcs5.function b/tests/suites/test_suite_pkcs5.function index c7455715c..a86778a3c 100644 --- a/tests/suites/test_suite_pkcs5.function +++ b/tests/suites/test_suite_pkcs5.function @@ -22,6 +22,8 @@ void pbkdf2_hmac( int hash, char *hex_password_string, int pw_len, salt_len; unsigned char key[100]; + md_init( &ctx ); + memset(pw_str, 0x00, 100); memset(salt_str, 0x00, 100); memset(dst_str, 0x00, 100); @@ -37,7 +39,7 @@ void pbkdf2_hmac( int hash, char *hex_password_string, TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 ); TEST_ASSERT( pkcs5_pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len, it_cnt, key_len, key ) == 0 ); - TEST_ASSERT( md_free_ctx( &ctx ) == 0 ); + md_free( &ctx ); hexify( dst_str, key, key_len ); TEST_ASSERT( strcmp( (char *) dst_str, result_key_string ) == 0 );