blob: da3e852cda806456f758dff9449d7127ad1c0b40 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/md.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_MD_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void mbedtls_md_process( )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010012{
13 const int *md_type_ptr;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 const mbedtls_md_info_t *info;
15 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020016 unsigned char buf[150];
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_md_init( &ctx );
Thomas Daubney7c4a4862022-03-02 16:47:49 +000019 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020020
21 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022 * Very minimal testing of mbedtls_md_process, just make sure the various
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020023 * xxx_process_wrap() function pointers are valid. (Testing that they
Shaun Case0e7791f2021-12-20 21:14:10 -080024 * indeed do the right thing would require messing with the internal
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 * state of the underlying mbedtls_md/sha context.)
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020026 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 * Also tests that mbedtls_md_list() only returns valid MDs.
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 info = mbedtls_md_info_from_type( *md_type_ptr );
Paul Bakker94b916c2014-04-17 16:07:20 +020032 TEST_ASSERT( info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 );
Ron Eldorb6889d12018-11-25 15:10:38 +020034 TEST_ASSERT( mbedtls_md_starts( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 );
36 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020037 }
Paul Bakkerbd51b262014-07-10 15:26:12 +020038
39exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010041}
42/* END_CASE */
43
44/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010045void md_null_args( )
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_md_context_t ctx;
48 const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020049 unsigned char buf[1] = { 0 };
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 );
54 TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
55 TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
60 TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
63 TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
66 TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
69 TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020072
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020073#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020075#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
78 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
79 TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 )
80 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 )
83 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
84 TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 )
85 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf )
88 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
89 TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf )
90 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
93 TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
96 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
99 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +0100100
101 /* Ok, this is not NULL arg but NULL return... */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL );
103 TEST_ASSERT( mbedtls_md_info_from_string( "no such md" ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200104}
105/* END_CASE */
106
107/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100108void md_info( int md_type, char * md_name, int md_size )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100111 const int *md_type_ptr;
112 int found;
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakker94b916c2014-04-17 16:07:20 +0200115 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 TEST_ASSERT( mbedtls_md_get_type( md_info ) == (mbedtls_md_type_t) md_type );
119 TEST_ASSERT( mbedtls_md_get_size( md_info ) == (unsigned char) md_size );
120 TEST_ASSERT( strcmp( mbedtls_md_get_name( md_info ), md_name ) == 0 );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100121
122 found = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100124 if( *md_type_ptr == md_type )
125 found = 1;
126 TEST_ASSERT( found == 1 );
127}
128/* END_CASE */
129
130/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100131void md_text( char * text_md_name, char * text_src_string,
Ronald Cronac6ae352020-06-26 14:33:03 +0200132 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000133{
134 char md_name[100];
135 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000136 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000138
Paul Bakker5c57e022016-07-19 13:31:41 +0100139 memset( md_name, 0x00, 100 );
140 memset( src_str, 0x00, 1000 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100141 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000142
Paul Bakker5c57e022016-07-19 13:31:41 +0100143 strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 );
144 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000146 TEST_ASSERT( md_info != NULL );
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000149
Ronald Cronac6ae352020-06-26 14:33:03 +0200150 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200151 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200152 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000153}
Paul Bakker33b43f12013-08-20 11:48:36 +0200154/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156/* BEGIN_CASE */
Ronald Cronac6ae352020-06-26 14:33:03 +0200157void md_hex( char * text_md_name, data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000158{
159 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000160 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000162
Paul Bakker5c57e022016-07-19 13:31:41 +0100163 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100164 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000165
Paul Bakker5c57e022016-07-19 13:31:41 +0100166 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
167 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000168 TEST_ASSERT( md_info != NULL );
169
Azim Khand30ca132017-06-09 04:32:58 +0100170 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str->x, src_str->len, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000171
Paul Bakker17373852011-01-06 14:20:01 +0000172
Ronald Cronac6ae352020-06-26 14:33:03 +0200173 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200174 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200175 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000176}
Paul Bakker33b43f12013-08-20 11:48:36 +0200177/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000178
Paul Bakker33b43f12013-08-20 11:48:36 +0200179/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100180void md_text_multi( char * text_md_name, char * text_src_string,
Ronald Cronac6ae352020-06-26 14:33:03 +0200181 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000182{
183 char md_name[100];
184 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000185 unsigned char output[100];
Paul Bakkere35afa22016-07-13 17:09:14 +0100186 int halfway, len;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100189 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_md_init( &ctx );
Paul Bakker97c53c22016-07-13 17:20:22 +0100192 mbedtls_md_init( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000193
Paul Bakker5c57e022016-07-19 13:31:41 +0100194 memset( md_name, 0x00, 100 );
195 memset( src_str, 0x00, 1000 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100196 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000197
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200198 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
199 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakkere35afa22016-07-13 17:09:14 +0100200 len = strlen( (char *) src_str );
201 halfway = len / 2;
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000204 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100206 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000209 TEST_ASSERT ( ctx.md_ctx != NULL );
Paul Bakkere35afa22016-07-13 17:09:14 +0100210 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100211 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
212
Paul Bakkere35afa22016-07-13 17:09:14 +0100213 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200215 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200216 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200217 hash->len) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000218
Paul Bakker97c53c22016-07-13 17:20:22 +0100219 /* Test clone */
Paul Bakker5c57e022016-07-19 13:31:41 +0100220 memset( output, 0x00, 100 );
Paul Bakker97c53c22016-07-13 17:20:22 +0100221
222 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) );
223 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200224 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200225 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200226 hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200227
228exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_md_free( &ctx );
Andres AG99b257c2016-08-26 17:21:14 +0100230 mbedtls_md_free( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000231}
Paul Bakker33b43f12013-08-20 11:48:36 +0200232/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000233
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* BEGIN_CASE */
Ronald Cronac6ae352020-06-26 14:33:03 +0200235void md_hex_multi( char * text_md_name, data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000236{
237 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000238 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100240 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100241 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_md_init( &ctx );
Paul Bakker97c53c22016-07-13 17:20:22 +0100244 mbedtls_md_init( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000245
Paul Bakker5c57e022016-07-19 13:31:41 +0100246 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100247 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000248
Paul Bakker5c57e022016-07-19 13:31:41 +0100249 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000251 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100253 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000254
Azim Khand30ca132017-06-09 04:32:58 +0100255 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000258 TEST_ASSERT ( ctx.md_ctx != NULL );
Azim Khand30ca132017-06-09 04:32:58 +0100259 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x, halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100260 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
261
Azim Khand30ca132017-06-09 04:32:58 +0100262 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x + halfway, src_str->len - halfway) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200264 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200265 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200266 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000267
Paul Bakker97c53c22016-07-13 17:20:22 +0100268 /* Test clone */
Paul Bakker5c57e022016-07-19 13:31:41 +0100269 memset( output, 0x00, 100 );
Paul Bakker97c53c22016-07-13 17:20:22 +0100270
Azim Khand30ca132017-06-09 04:32:58 +0100271 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str->x + halfway, src_str->len - halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100272 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200273 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200274 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200275 hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200276
277exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_md_free( &ctx );
Andres AG99b257c2016-08-26 17:21:14 +0100279 mbedtls_md_free( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000280}
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000282
Paul Bakker33b43f12013-08-20 11:48:36 +0200283/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100284void mbedtls_md_hmac( char * text_md_name, int trunc_size,
Azim Khan5fcca462018-06-29 11:05:32 +0100285 data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200286 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000287{
288 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000289 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000291
Paul Bakker5c57e022016-07-19 13:31:41 +0100292 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100293 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000294
Paul Bakker5c57e022016-07-19 13:31:41 +0100295 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000297 TEST_ASSERT( md_info != NULL );
298
Paul Bakker17373852011-01-06 14:20:01 +0000299
Azim Khand30ca132017-06-09 04:32:58 +0100300 TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str->x, key_str->len, src_str->x, src_str->len, output ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000301
Ronald Cronac6ae352020-06-26 14:33:03 +0200302 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
303 trunc_size, hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000304}
Paul Bakker33b43f12013-08-20 11:48:36 +0200305/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100308void md_hmac_multi( char * text_md_name, int trunc_size, data_t * key_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200309 data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000310{
311 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000312 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 const mbedtls_md_info_t *md_info = NULL;
314 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100315 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000318
Paul Bakker5c57e022016-07-19 13:31:41 +0100319 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100320 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000321
Paul Bakker5c57e022016-07-19 13:31:41 +0100322 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000324 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000326
Azim Khand30ca132017-06-09 04:32:58 +0100327 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000328
Azim Khand30ca132017-06-09 04:32:58 +0100329 TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str->x, key_str->len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000330 TEST_ASSERT ( ctx.md_ctx != NULL );
Azim Khand30ca132017-06-09 04:32:58 +0100331 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
332 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100334
Ronald Cronac6ae352020-06-26 14:33:03 +0200335 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
336 trunc_size, hash->len ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100337
338 /* Test again, for reset() */
Paul Bakker5c57e022016-07-19 13:31:41 +0100339 memset( output, 0x00, 100 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
Azim Khand30ca132017-06-09 04:32:58 +0100342 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
343 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200345
Ronald Cronac6ae352020-06-26 14:33:03 +0200346 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
347 trunc_size, hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200348
349exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000351}
Paul Bakker33b43f12013-08-20 11:48:36 +0200352/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100355void mbedtls_md_file( char * text_md_name, char * filename,
Ronald Cronac6ae352020-06-26 14:33:03 +0200356 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000357{
358 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000359 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000361
Paul Bakker5c57e022016-07-19 13:31:41 +0100362 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100363 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000364
Paul Bakker5c57e022016-07-19 13:31:41 +0100365 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000367 TEST_ASSERT( md_info != NULL );
368
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200369 TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000370
Ronald Cronac6ae352020-06-26 14:33:03 +0200371 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200372 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200373 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000374}
Paul Bakker33b43f12013-08-20 11:48:36 +0200375/* END_CASE */