2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/md.h"
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_MD_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2014-03-29 14:54:50 +00:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_md_process( )
|
2014-03-29 14:54:50 +00:00
|
|
|
{
|
|
|
|
const int *md_type_ptr;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *info;
|
|
|
|
mbedtls_md_context_t ctx;
|
2014-04-02 15:52:04 +00:00
|
|
|
unsigned char buf[150];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &ctx );
|
2014-04-02 15:52:04 +00:00
|
|
|
|
|
|
|
/*
|
2015-04-08 10:49:31 +00:00
|
|
|
* Very minimal testing of mbedtls_md_process, just make sure the various
|
2014-04-02 15:52:04 +00:00
|
|
|
* xxx_process_wrap() function pointers are valid. (Testing that they
|
|
|
|
* indeed do the right thing whould require messing with the internal
|
2015-04-08 10:49:31 +00:00
|
|
|
* state of the underlying mbedtls_md/sha context.)
|
2014-04-02 15:52:04 +00:00
|
|
|
*
|
2015-04-08 10:49:31 +00:00
|
|
|
* Also tests that mbedtls_md_list() only returns valid MDs.
|
2014-04-02 15:52:04 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
|
2014-04-02 15:52:04 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
info = mbedtls_md_info_from_type( *md_type_ptr );
|
2014-04-17 14:07:20 +00:00
|
|
|
TEST_ASSERT( info != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 );
|
|
|
|
mbedtls_md_free( &ctx );
|
2014-04-02 15:52:04 +00:00
|
|
|
}
|
2014-07-10 13:26:12 +00:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_free( &ctx );
|
2014-03-29 14:54:50 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-06-13 14:34:30 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_null_args( )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) );
|
2014-06-13 14:34:30 +00:00
|
|
|
unsigned char buf[1] = { 0 };
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &ctx );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
|
|
|
|
TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 14:34:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2015-03-26 11:42:35 +00:00
|
|
|
|
|
|
|
/* Ok, this is not NULL arg but NULL return... */
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL );
|
|
|
|
TEST_ASSERT( mbedtls_md_info_from_string( "no such md" ) == NULL );
|
2014-06-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-03-29 14:54:50 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_info( int md_type, char *md_name, int md_size )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info;
|
2014-03-29 14:54:50 +00:00
|
|
|
const int *md_type_ptr;
|
|
|
|
int found;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_type( md_type );
|
2014-04-17 14:07:20 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) );
|
2014-03-29 14:54:50 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_md_get_type( md_info ) == (mbedtls_md_type_t) md_type );
|
|
|
|
TEST_ASSERT( mbedtls_md_get_size( md_info ) == (unsigned char) md_size );
|
|
|
|
TEST_ASSERT( strcmp( mbedtls_md_get_name( md_info ), md_name ) == 0 );
|
2014-03-29 14:54:50 +00:00
|
|
|
|
|
|
|
found = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
|
2014-03-29 14:54:50 +00:00
|
|
|
if( *md_type_ptr == md_type )
|
|
|
|
found = 1;
|
|
|
|
TEST_ASSERT( found == 1 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char hash_str[1000];
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(hash_str, 0x00, 1000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
|
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
|
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char hash_str[10000];
|
|
|
|
unsigned char output[100];
|
|
|
|
int src_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, src_len, output ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_text_multi( char *text_md_name, char *text_src_string,
|
|
|
|
char *hex_hash_string )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char hash_str[1000];
|
|
|
|
unsigned char output[100];
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
|
|
|
mbedtls_md_context_t ctx;
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(hash_str, 0x00, 1000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
|
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2014-07-10 13:26:12 +00:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_free( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_hex_multi( char *text_md_name, char *hex_src_string,
|
|
|
|
char *hex_hash_string )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char hash_str[10000];
|
|
|
|
unsigned char output[100];
|
|
|
|
int src_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
|
|
|
mbedtls_md_context_t ctx;
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, src_len ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2014-07-10 13:26:12 +00:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_free( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
|
2013-08-20 09:48:36 +00:00
|
|
|
char *hex_src_string, char *hex_hash_string )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
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;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(key_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
|
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* 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 )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
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;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
|
|
|
mbedtls_md_context_t ctx;
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(key_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str, key_len ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, src_len ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
|
2013-08-20 09:48:36 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2014-03-29 13:43:44 +00:00
|
|
|
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);
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, src_len ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2014-07-10 13:26:12 +00:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_free( &ctx );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2013-09-15 13:20:37 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
|
|
|
void mbedtls_md_file( char *text_md_name, char *filename, char *hex_hash_string )
|
2011-01-06 14:20:01 +00:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char hash_str[1000];
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 14:20:01 +00:00
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(hash_str, 0x00, 1000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 14:20:01 +00:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_file( md_info, filename, output);
|
|
|
|
hexify( hash_str, output, mbedtls_md_get_size(md_info) );
|
2011-01-06 14:20:01 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 14:20:01 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|