| /* BEGIN_HEADER */ |
| #include "mbedtls/md.h" |
| /* END_HEADER */ |
| |
| /* BEGIN_DEPENDENCIES |
| * depends_on:POLARSSL_MD_C |
| * END_DEPENDENCIES |
| */ |
| |
| /* BEGIN_CASE */ |
| void md_process( ) |
| { |
| const int *md_type_ptr; |
| const md_info_t *info; |
| md_context_t ctx; |
| unsigned char buf[150]; |
| |
| md_init( &ctx ); |
| |
| /* |
| * Very minimal testing of md_process, just make sure the various |
| * xxx_process_wrap() function pointers are valid. (Testing that they |
| * indeed do the right thing whould require messing with the internal |
| * state of the underlying md/sha context.) |
| * |
| * Also tests that md_list() only returns valid MDs. |
| */ |
| for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ ) |
| { |
| info = md_info_from_type( *md_type_ptr ); |
| TEST_ASSERT( info != NULL ); |
| TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 ); |
| TEST_ASSERT( md_process( &ctx, buf ) == 0 ); |
| md_free( &ctx ); |
| } |
| |
| exit: |
| md_free( &ctx ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_null_args( ) |
| { |
| md_context_t ctx; |
| const md_info_t *info = md_info_from_type( *( md_list() ) ); |
| unsigned char buf[1] = { 0 }; |
| |
| md_init( &ctx ); |
| |
| TEST_ASSERT( md_get_size( NULL ) == 0 ); |
| |
| TEST_ASSERT( md_get_type( NULL ) == POLARSSL_MD_NONE ); |
| |
| TEST_ASSERT( md_info_from_string( NULL ) == NULL ); |
| |
| TEST_ASSERT( md_init_ctx( &ctx, NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_init_ctx( NULL, info ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_starts( NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_starts( &ctx ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_update( NULL, buf, 1 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_update( &ctx, buf, 1 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_finish( NULL, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_finish( &ctx, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md( NULL, buf, 1, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_file( NULL, "", buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_hmac_starts( NULL, buf, 1 ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_hmac_starts( &ctx, buf, 1 ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_hmac_update( NULL, buf, 1 ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_hmac_update( &ctx, buf, 1 ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_hmac_finish( NULL, buf ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_hmac_finish( &ctx, buf ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_hmac_reset( NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_hmac_reset( &ctx ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_hmac( NULL, buf, 1, buf, 1, buf ) |
| == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| |
| TEST_ASSERT( md_process( NULL, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| TEST_ASSERT( md_process( &ctx, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_info( int md_type, char *md_name, int md_size ) |
| { |
| const md_info_t *md_info; |
| const int *md_type_ptr; |
| int found; |
| |
| md_info = md_info_from_type( md_type ); |
| TEST_ASSERT( md_info != NULL ); |
| TEST_ASSERT( md_info == md_info_from_string( md_name ) ); |
| |
| TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type ); |
| TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size ); |
| |
| found = 0; |
| for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ ) |
| if( *md_type_ptr == md_type ) |
| found = 1; |
| TEST_ASSERT( found == 1 ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string ) |
| { |
| char md_name[100]; |
| unsigned char src_str[1000]; |
| unsigned char hash_str[1000]; |
| unsigned char output[100]; |
| const md_info_t *md_info = NULL; |
| |
| memset(md_name, 0x00, 100); |
| memset(src_str, 0x00, 1000); |
| memset(hash_str, 0x00, 1000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 ); |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string(md_name); |
| TEST_ASSERT( md_info != NULL ); |
| |
| TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) ); |
| hexify( hash_str, output, md_get_size(md_info) ); |
| |
| TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string ) |
| { |
| char md_name[100]; |
| unsigned char src_str[10000]; |
| unsigned char hash_str[10000]; |
| unsigned char output[100]; |
| int src_len; |
| const md_info_t *md_info = NULL; |
| |
| memset(md_name, 0x00, 100); |
| memset(src_str, 0x00, 10000); |
| memset(hash_str, 0x00, 10000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string(md_name); |
| TEST_ASSERT( md_info != NULL ); |
| |
| src_len = unhexify( src_str, hex_src_string ); |
| TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) ); |
| |
| hexify( hash_str, output, md_get_size(md_info) ); |
| |
| TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_text_multi( char *text_md_name, char *text_src_string, |
| char *hex_hash_string ) |
| { |
| char md_name[100]; |
| 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_init( &ctx ); |
| |
| memset(md_name, 0x00, 100); |
| memset(src_str, 0x00, 1000); |
| memset(hash_str, 0x00, 1000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 ); |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string(md_name); |
| TEST_ASSERT( md_info != NULL ); |
| TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) ); |
| |
| TEST_ASSERT ( 0 == md_starts( &ctx ) ); |
| 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 ) ); |
| |
| hexify( hash_str, output, md_get_size(md_info) ); |
| |
| TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); |
| |
| exit: |
| md_free( &ctx ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_hex_multi( char *text_md_name, char *hex_src_string, |
| char *hex_hash_string ) |
| { |
| char md_name[100]; |
| unsigned char src_str[10000]; |
| unsigned char hash_str[10000]; |
| unsigned char output[100]; |
| int src_len; |
| const md_info_t *md_info = NULL; |
| md_context_t ctx; |
| |
| md_init( &ctx ); |
| |
| memset(md_name, 0x00, 100); |
| memset(src_str, 0x00, 10000); |
| memset(hash_str, 0x00, 10000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string(md_name); |
| TEST_ASSERT( md_info != NULL ); |
| 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 ) ); |
| |
| hexify( hash_str, output, md_get_size(md_info) ); |
| |
| TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); |
| |
| exit: |
| md_free( &ctx ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string, |
| char *hex_src_string, char *hex_hash_string ) |
| { |
| char md_name[100]; |
| unsigned char src_str[10000]; |
| unsigned char key_str[10000]; |
| unsigned char hash_str[10000]; |
| unsigned char output[100]; |
| int key_len, src_len; |
| const md_info_t *md_info = NULL; |
| |
| memset(md_name, 0x00, 100); |
| memset(src_str, 0x00, 10000); |
| memset(key_str, 0x00, 10000); |
| memset(hash_str, 0x00, 10000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string( md_name ); |
| TEST_ASSERT( md_info != NULL ); |
| |
| key_len = unhexify( key_str, hex_key_string ); |
| src_len = unhexify( src_str, hex_src_string ); |
| |
| TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 ); |
| hexify( hash_str, output, md_get_size(md_info) ); |
| |
| TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE */ |
| void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string, |
| char *hex_src_string, char *hex_hash_string ) |
| { |
| char md_name[100]; |
| unsigned char src_str[10000]; |
| unsigned char key_str[10000]; |
| unsigned char hash_str[10000]; |
| unsigned char output[100]; |
| int key_len, src_len; |
| const md_info_t *md_info = NULL; |
| md_context_t ctx; |
| |
| md_init( &ctx ); |
| |
| memset(md_name, 0x00, 100); |
| memset(src_str, 0x00, 10000); |
| memset(key_str, 0x00, 10000); |
| memset(hash_str, 0x00, 10000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string( md_name ); |
| TEST_ASSERT( md_info != NULL ); |
| TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) ); |
| |
| key_len = unhexify( key_str, hex_key_string ); |
| src_len = unhexify( src_str, hex_src_string ); |
| |
| TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) ); |
| TEST_ASSERT ( ctx.md_ctx != NULL ); |
| TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) ); |
| TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) ); |
| |
| hexify( hash_str, output, md_get_size(md_info) ); |
| TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| |
| /* Test again, for reset() */ |
| memset(hash_str, 0x00, 10000); |
| memset(output, 0x00, 100); |
| |
| 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 ) ); |
| |
| hexify( hash_str, output, md_get_size(md_info) ); |
| TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| |
| exit: |
| md_free( &ctx ); |
| } |
| /* END_CASE */ |
| |
| /* BEGIN_CASE depends_on:POLARSSL_FS_IO */ |
| void md_file( char *text_md_name, char *filename, char *hex_hash_string ) |
| { |
| char md_name[100]; |
| unsigned char hash_str[1000]; |
| unsigned char output[100]; |
| const md_info_t *md_info = NULL; |
| |
| memset(md_name, 0x00, 100); |
| memset(hash_str, 0x00, 1000); |
| memset(output, 0x00, 100); |
| |
| strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); |
| md_info = md_info_from_string( md_name ); |
| TEST_ASSERT( md_info != NULL ); |
| |
| md_file( md_info, filename, output); |
| hexify( hash_str, output, md_get_size(md_info) ); |
| |
| TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); |
| } |
| /* END_CASE */ |