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