1/* BEGIN_HEADER */ 2#include "mbedtls/md.h" 3/* END_HEADER */ 4 5/* BEGIN_DEPENDENCIES 6 * depends_on:MBEDTLS_MD_C 7 * END_DEPENDENCIES 8 */ 9 10/* BEGIN_CASE */ 11void mbedtls_md_process( ) 12{ 13 const int *md_type_ptr; 14 const mbedtls_md_info_t *info; 15 mbedtls_md_context_t ctx; 16 unsigned char buf[150]; 17 18 mbedtls_md_init( &ctx ); 19 20 /* 21 * Very minimal testing of mbedtls_md_process, just make sure the various 22 * xxx_process_wrap() function pointers are valid. (Testing that they 23 * indeed do the right thing whould require messing with the internal 24 * state of the underlying mbedtls_md/sha context.) 25 * 26 * Also tests that mbedtls_md_list() only returns valid MDs. 27 */ 28 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ ) 29 { 30 info = mbedtls_md_info_from_type( *md_type_ptr ); 31 TEST_ASSERT( info != NULL ); 32 TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 ); 33 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 ); 34 mbedtls_md_free( &ctx ); 35 } 36 37exit: 38 mbedtls_md_free( &ctx ); 39} 40/* END_CASE */ 41 42/* BEGIN_CASE */ 43void md_null_args( ) 44{ 45 mbedtls_md_context_t ctx; 46 const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) ); 47 unsigned char buf[1] = { 0 }; 48 49 mbedtls_md_init( &ctx ); 50 51 TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 ); 52 TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE ); 53 TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL ); 54 55 TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL ); 56 57 TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 58 TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 59 60 TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 61 TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 62 63 TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 64 TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 65 66 TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 67 TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 68 69 TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 70 71#if defined(MBEDTLS_FS_IO) 72 TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 73#endif 74 75 TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 ) 76 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 77 TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 ) 78 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 79 80 TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 ) 81 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 82 TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 ) 83 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 84 85 TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf ) 86 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 87 TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf ) 88 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 89 90 TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 91 TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 92 93 TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf ) 94 == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 95 96 TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 97 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); 98 99 /* Ok, this is not NULL arg but NULL return... */ 100 TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL ); 101 TEST_ASSERT( mbedtls_md_info_from_string( "no such md" ) == NULL ); 102} 103/* END_CASE */ 104 105/* BEGIN_CASE */ 106void md_info( int md_type, char *md_name, int md_size ) 107{ 108 const mbedtls_md_info_t *md_info; 109 const int *md_type_ptr; 110 int found; 111 112 md_info = mbedtls_md_info_from_type( md_type ); 113 TEST_ASSERT( md_info != NULL ); 114 TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) ); 115 116 TEST_ASSERT( mbedtls_md_get_type( md_info ) == (mbedtls_md_type_t) md_type ); 117 TEST_ASSERT( mbedtls_md_get_size( md_info ) == (unsigned char) md_size ); 118 TEST_ASSERT( strcmp( mbedtls_md_get_name( md_info ), md_name ) == 0 ); 119 120 found = 0; 121 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ ) 122 if( *md_type_ptr == md_type ) 123 found = 1; 124 TEST_ASSERT( found == 1 ); 125} 126/* END_CASE */ 127 128/* BEGIN_CASE */ 129void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string ) 130{ 131 char md_name[100]; 132 unsigned char src_str[1000]; 133 unsigned char hash_str[1000]; 134 unsigned char output[100]; 135 const mbedtls_md_info_t *md_info = NULL; 136 137 memset( md_name, 0x00, 100 ); 138 memset( src_str, 0x00, 1000 ); 139 memset( hash_str, 0x00, 1000 ); 140 memset( output, 0x00, 100 ); 141 142 strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 ); 143 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); 144 md_info = mbedtls_md_info_from_string(md_name); 145 TEST_ASSERT( md_info != NULL ); 146 147 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) ); 148 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 149 150 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 151} 152/* END_CASE */ 153 154/* BEGIN_CASE */ 155void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string ) 156{ 157 char md_name[100]; 158 unsigned char src_str[10000]; 159 unsigned char hash_str[10000]; 160 unsigned char output[100]; 161 int src_len; 162 const mbedtls_md_info_t *md_info = NULL; 163 164 memset( md_name, 0x00, 100 ); 165 memset( src_str, 0x00, 10000 ); 166 memset( hash_str, 0x00, 10000 ); 167 memset( output, 0x00, 100 ); 168 169 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); 170 md_info = mbedtls_md_info_from_string( md_name ); 171 TEST_ASSERT( md_info != NULL ); 172 173 src_len = unhexify( src_str, hex_src_string ); 174 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, src_len, output ) ); 175 176 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 177 178 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 179} 180/* END_CASE */ 181 182/* BEGIN_CASE */ 183void md_text_multi( char *text_md_name, char *text_src_string, 184 char *hex_hash_string ) 185{ 186 char md_name[100]; 187 unsigned char src_str[1000]; 188 unsigned char hash_str[1000]; 189 unsigned char output[100]; 190 int halfway, len; 191 192 const mbedtls_md_info_t *md_info = NULL; 193 mbedtls_md_context_t ctx, ctx_copy; 194 195 mbedtls_md_init( &ctx ); 196 mbedtls_md_init( &ctx_copy ); 197 198 memset( md_name, 0x00, 100 ); 199 memset( src_str, 0x00, 1000 ); 200 memset( hash_str, 0x00, 1000 ); 201 memset( output, 0x00, 100 ); 202 203 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 ); 204 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); 205 len = strlen( (char *) src_str ); 206 halfway = len / 2; 207 208 md_info = mbedtls_md_info_from_string(md_name); 209 TEST_ASSERT( md_info != NULL ); 210 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) ); 211 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) ); 212 213 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) ); 214 TEST_ASSERT ( ctx.md_ctx != NULL ); 215 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) ); 216 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) ); 217 218 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) ); 219 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) ); 220 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 221 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 222 223 /* Test clone */ 224 memset( hash_str, 0x00, 1000 ); 225 memset( output, 0x00, 100 ); 226 227 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) ); 228 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) ); 229 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 230 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 231 232exit: 233 mbedtls_md_free( &ctx ); 234 mbedtls_md_free( &ctx_copy ); 235} 236/* END_CASE */ 237 238/* BEGIN_CASE */ 239void md_hex_multi( char *text_md_name, char *hex_src_string, 240 char *hex_hash_string ) 241{ 242 char md_name[100]; 243 unsigned char src_str[10000]; 244 unsigned char hash_str[10000]; 245 unsigned char output[100]; 246 int src_len, halfway; 247 const mbedtls_md_info_t *md_info = NULL; 248 mbedtls_md_context_t ctx, ctx_copy; 249 250 mbedtls_md_init( &ctx ); 251 mbedtls_md_init( &ctx_copy ); 252 253 memset( md_name, 0x00, 100 ); 254 memset( src_str, 0x00, 10000 ); 255 memset( hash_str, 0x00, 10000 ); 256 memset( output, 0x00, 100 ); 257 258 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); 259 md_info = mbedtls_md_info_from_string(md_name); 260 TEST_ASSERT( md_info != NULL ); 261 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) ); 262 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) ); 263 264 src_len = unhexify( src_str, hex_src_string ); 265 halfway = src_len / 2; 266 267 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) ); 268 TEST_ASSERT ( ctx.md_ctx != NULL ); 269 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) ); 270 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) ); 271 272 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, src_len - halfway) ); 273 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) ); 274 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 275 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 276 277 /* Test clone */ 278 memset( hash_str, 0x00, 10000 ); 279 memset( output, 0x00, 100 ); 280 281 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, src_len - halfway ) ); 282 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) ); 283 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 284 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 285 286exit: 287 mbedtls_md_free( &ctx ); 288 mbedtls_md_free( &ctx_copy ); 289} 290/* END_CASE */ 291 292/* BEGIN_CASE */ 293void mbedtls_md_hmac( char *text_md_name, int trunc_size, char *hex_key_string, 294 char *hex_src_string, char *hex_hash_string ) 295{ 296 char md_name[100]; 297 unsigned char src_str[10000]; 298 unsigned char key_str[10000]; 299 unsigned char hash_str[10000]; 300 unsigned char output[100]; 301 int key_len, src_len; 302 const mbedtls_md_info_t *md_info = NULL; 303 304 memset( md_name, 0x00, 100 ); 305 memset( src_str, 0x00, 10000 ); 306 memset( key_str, 0x00, 10000 ); 307 memset( hash_str, 0x00, 10000 ); 308 memset( output, 0x00, 100 ); 309 310 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); 311 md_info = mbedtls_md_info_from_string( md_name ); 312 TEST_ASSERT( md_info != NULL ); 313 314 key_len = unhexify( key_str, hex_key_string ); 315 src_len = unhexify( src_str, hex_src_string ); 316 317 TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 ); 318 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 319 320 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); 321} 322/* END_CASE */ 323 324/* BEGIN_CASE */ 325void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string, 326 char *hex_src_string, char *hex_hash_string ) 327{ 328 char md_name[100]; 329 unsigned char src_str[10000]; 330 unsigned char key_str[10000]; 331 unsigned char hash_str[10000]; 332 unsigned char output[100]; 333 int key_len, src_len, halfway; 334 const mbedtls_md_info_t *md_info = NULL; 335 mbedtls_md_context_t ctx; 336 337 mbedtls_md_init( &ctx ); 338 339 memset( md_name, 0x00, 100 ); 340 memset( src_str, 0x00, 10000 ); 341 memset( key_str, 0x00, 10000 ); 342 memset( hash_str, 0x00, 10000 ); 343 memset( output, 0x00, 100 ); 344 345 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); 346 md_info = mbedtls_md_info_from_string( md_name ); 347 TEST_ASSERT( md_info != NULL ); 348 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) ); 349 350 key_len = unhexify( key_str, hex_key_string ); 351 src_len = unhexify( src_str, hex_src_string ); 352 halfway = src_len / 2; 353 354 TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str, key_len ) ); 355 TEST_ASSERT ( ctx.md_ctx != NULL ); 356 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, halfway ) ); 357 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str + halfway, src_len - halfway ) ); 358 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) ); 359 360 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 361 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); 362 363 /* Test again, for reset() */ 364 memset( hash_str, 0x00, 10000 ); 365 memset( output, 0x00, 100 ); 366 367 TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) ); 368 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, halfway ) ); 369 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str + halfway, src_len - halfway ) ); 370 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) ); 371 372 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 373 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); 374 375exit: 376 mbedtls_md_free( &ctx ); 377} 378/* END_CASE */ 379 380/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ 381void mbedtls_md_file( char *text_md_name, char *filename, char *hex_hash_string ) 382{ 383 char md_name[100]; 384 unsigned char hash_str[1000]; 385 unsigned char output[100]; 386 const mbedtls_md_info_t *md_info = NULL; 387 388 memset( md_name, 0x00, 100 ); 389 memset( hash_str, 0x00, 1000 ); 390 memset( output, 0x00, 100 ); 391 392 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); 393 md_info = mbedtls_md_info_from_string( md_name ); 394 TEST_ASSERT( md_info != NULL ); 395 396 TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 ); 397 hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); 398 399 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); 400} 401/* END_CASE */ 402