mbed TLS Build

Dependents:   Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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