2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/md.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_MD_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2014-03-29 15:54:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_md_process( )
|
2014-03-29 15:54:50 +01:00
|
|
|
{
|
|
|
|
const int *md_type_ptr;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *info;
|
|
|
|
mbedtls_md_context_t ctx;
|
2014-04-02 17:52:04 +02:00
|
|
|
unsigned char buf[150];
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
2022-02-17 14:38:26 +01:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
2014-04-02 17:52:04 +02:00
|
|
|
|
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Very minimal testing of mbedtls_md_process, just make sure the various
|
2014-04-02 17:52:04 +02: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 12:49:31 +02:00
|
|
|
* state of the underlying mbedtls_md/sha context.)
|
2014-04-02 17:52:04 +02:00
|
|
|
*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Also tests that mbedtls_md_list() only returns valid MDs.
|
2014-04-02 17:52:04 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
|
2014-04-02 17:52:04 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
info = mbedtls_md_info_from_type( *md_type_ptr );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 );
|
2018-11-25 14:10:38 +01:00
|
|
|
TEST_ASSERT( mbedtls_md_starts( &ctx ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 );
|
|
|
|
mbedtls_md_free( &ctx );
|
2014-04-02 17:52:04 +02:00
|
|
|
}
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &ctx );
|
2014-03-29 15:54:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-06-13 16:34:30 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void md_null_args( )
|
2014-06-13 16:34:30 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) );
|
2014-06-13 16:34:30 +02:00
|
|
|
unsigned char buf[1] = { 0 };
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
|
2021-12-28 16:32:00 +01:00
|
|
|
TEST_ASSERT( mbedtls_md_info_from_ctx( NULL ) == NULL );
|
|
|
|
TEST_ASSERT( mbedtls_md_info_from_ctx( &ctx ) == NULL );
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2015-05-28 14:44:00 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2015-05-28 14:44:00 +02:00
|
|
|
#endif
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
|
|
|
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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 12:42:35 +01:00
|
|
|
|
|
|
|
/* Ok, this is not NULL arg but NULL return... */
|
2015-04-08 12:49:31 +02: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 16:34:30 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-03-29 15:54:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void md_info( int md_type, char * md_name, int md_size )
|
2014-03-29 15:54:50 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
2014-03-29 15:54:50 +01:00
|
|
|
const int *md_type_ptr;
|
|
|
|
int found;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_type );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) );
|
2014-03-29 15:54:50 +01:00
|
|
|
|
2015-04-08 12:49:31 +02: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 15:54:50 +01:00
|
|
|
|
|
|
|
found = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
|
2014-03-29 15:54:50 +01:00
|
|
|
if( *md_type_ptr == md_type )
|
|
|
|
found = 1;
|
|
|
|
TEST_ASSERT( found == 1 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void md_text( char * text_md_name, char * text_src_string,
|
2020-06-26 14:33:03 +02:00
|
|
|
data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char output[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( src_str, 0x00, 1000 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02: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 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2020-06-26 14:33:03 +02:00
|
|
|
void md_hex( char * text_md_name, data_t * src_str, data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char output[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
|
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str->x, src_str->len, output ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void md_text_multi( char * text_md_name, char * text_src_string,
|
2020-06-26 14:33:03 +02:00
|
|
|
data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char output[100];
|
2016-07-13 18:09:14 +02:00
|
|
|
int halfway, len;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2016-07-13 18:20:22 +02:00
|
|
|
mbedtls_md_context_t ctx, ctx_copy;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
2016-07-13 18:20:22 +02:00
|
|
|
mbedtls_md_init( &ctx_copy );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( src_str, 0x00, 1000 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2014-04-17 16:06:37 +02:00
|
|
|
strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
|
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
|
2016-07-13 18:09:14 +02:00
|
|
|
len = strlen( (char *) src_str );
|
|
|
|
halfway = len / 2;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
|
2016-07-13 18:20:22 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
|
2021-12-28 16:32:00 +01:00
|
|
|
TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx ) == md_info );
|
|
|
|
TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx_copy ) == md_info );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
2016-07-13 18:09:14 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
|
2016-07-13 18:20:22 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
|
|
|
|
|
2016-07-13 18:09:14 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-13 18:20:22 +02:00
|
|
|
/* Test clone */
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( output, 0x00, 100 );
|
2016-07-13 18:20:22 +02:00
|
|
|
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &ctx );
|
2016-08-26 18:21:14 +02:00
|
|
|
mbedtls_md_free( &ctx_copy );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2020-06-26 14:33:03 +02:00
|
|
|
void md_hex_multi( char * text_md_name, data_t * src_str, data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char output[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2016-07-13 18:20:22 +02:00
|
|
|
mbedtls_md_context_t ctx, ctx_copy;
|
2017-05-30 15:23:15 +02:00
|
|
|
int halfway;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
2016-07-13 18:20:22 +02:00
|
|
|
mbedtls_md_init( &ctx_copy );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_string(md_name);
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
|
2016-07-13 18:20:22 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
|
2021-12-28 16:32:00 +01:00
|
|
|
TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx ) == md_info );
|
|
|
|
TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx_copy ) == md_info );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
halfway = src_str->len / 2;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x, halfway ) );
|
2016-07-13 18:20:22 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x + halfway, src_str->len - halfway) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-13 18:20:22 +02:00
|
|
|
/* Test clone */
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( output, 0x00, 100 );
|
2016-07-13 18:20:22 +02:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str->x + halfway, src_str->len - halfway ) );
|
2016-07-13 18:20:22 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &ctx );
|
2016-08-26 18:21:14 +02:00
|
|
|
mbedtls_md_free( &ctx_copy );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-06-09 05:32:58 +02:00
|
|
|
void mbedtls_md_hmac( char * text_md_name, int trunc_size,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * key_str, data_t * src_str,
|
2020-06-26 14:33:03 +02:00
|
|
|
data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char output[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str->x, key_str->len, src_str->x, src_str->len, output ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
|
|
|
trunc_size, hash->len ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void md_hmac_multi( char * text_md_name, int trunc_size, data_t * key_str,
|
2020-06-26 14:33:03 +02:00
|
|
|
data_t * src_str, data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char output[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
|
|
|
mbedtls_md_context_t ctx;
|
2017-05-30 15:23:15 +02:00
|
|
|
int halfway;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
|
2021-12-28 16:32:00 +01:00
|
|
|
TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx ) == md_info );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
halfway = src_str->len / 2;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str->x, key_str->len ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
|
|
|
trunc_size, hash->len ) == 0 );
|
2014-03-29 14:43:44 +01:00
|
|
|
|
|
|
|
/* Test again, for reset() */
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( output, 0x00, 100 );
|
2014-03-29 14:43:44 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
|
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
|
|
|
trunc_size, hash->len ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &ctx );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-09-15 15:20:37 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_md_file( char * text_md_name, char * filename,
|
2020-06-26 14:33:03 +02:00
|
|
|
data_t * hash )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char output[100];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
memset( md_name, 0x00, 100 );
|
|
|
|
memset( output, 0x00, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-19 14:31:41 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_string( md_name );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2015-06-24 01:08:09 +02:00
|
|
|
TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
|
2020-06-10 11:42:32 +02:00
|
|
|
mbedtls_md_get_size( md_info ),
|
2020-06-26 14:33:03 +02:00
|
|
|
hash->len ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|