1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(POLARSSL_PLATFORM_C)
20 #define polarssl_malloc malloc
21 #define polarssl_free free
26 typedef UINT32 uint32_t;
39 #define GET_UINT32_BE(n,b,i) \
41 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44 | ( (uint32_t) (b)[(i) + 3] ); \
49 #define PUT_UINT32_BE(n,b,i) \
51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54 (b)[(i) + 3] = (unsigned char) ( (n) ); \
58 static int unhexify(
unsigned char *obuf,
const char *ibuf)
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1));
67 if( c >=
'0' && c <=
'9' )
69 else if( c >=
'a' && c <=
'f' )
71 else if( c >=
'A' && c <=
'F' )
77 if( c2 >=
'0' && c2 <=
'9' )
79 else if( c2 >=
'a' && c2 <=
'f' )
81 else if( c2 >=
'A' && c2 <=
'F' )
86 *obuf++ = ( c << 4 ) | c2;
92 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
104 *obuf++ =
'a' + h - 10;
109 *obuf++ =
'a' + l - 10;
126 size_t actual_len = len != 0 ? len : 1;
131 memset( p, 0x00, actual_len );
150 *olen = strlen(ibuf) / 2;
156 assert( obuf != NULL );
172 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
174 #if !defined(__OpenBSD__)
177 if( rng_state != NULL )
180 for( i = 0; i < len; ++i )
183 if( rng_state != NULL )
186 arc4random_buf( output, len );
197 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
199 if( rng_state != NULL )
202 memset( output, 0, len );
229 if( rng_state == NULL )
238 memcpy( output, info->
buf, use_len );
239 info->
buf += use_len;
243 if( len - use_len > 0 )
244 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
276 if( rng_state == NULL )
283 size_t use_len = ( len > 4 ) ? 4 : len;
286 for( i = 0; i < 32; i++ )
288 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
290 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
294 memcpy( out, result, use_len );
306 #if defined(POLARSSL_PLATFORM_C)
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
318 #define TEST_SUITE_ACTIVE
320 static int test_assert(
int correct,
const char *test )
327 printf(
"FAILED\n" );
328 printf(
" %s\n", test );
333 #define TEST_ASSERT( TEST ) \
334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335 if( test_errors) goto exit; \
340 if( (*str)[0] !=
'"' ||
341 (*str)[strlen( *str ) - 1] !=
'"' )
343 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
348 (*str)[strlen( *str ) - 1] =
'\0';
360 for( i = 0; i < strlen( str ); i++ )
362 if( i == 0 && str[i] ==
'-' )
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] ==
'0' && str[i] ==
'x' )
375 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
376 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
377 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
387 *value = strtol( str, NULL, 16 );
389 *value = strtol( str, NULL, 10 );
394 if( strcmp( str,
"POLARSSL_MD_RIPEMD160" ) == 0 )
399 if( strcmp( str,
"POLARSSL_MD_MD5" ) == 0 )
404 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
409 if( strcmp( str,
"POLARSSL_MD_SHA224" ) == 0 )
414 if( strcmp( str,
"POLARSSL_MD_MD4" ) == 0 )
419 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
424 if( strcmp( str,
"POLARSSL_MD_MD2" ) == 0 )
429 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
434 if( strcmp( str,
"POLARSSL_MD_SHA384" ) == 0 )
441 printf(
"Expected integer for parameter and got: %s\n", str );
445 void test_suite_md_process( )
447 const int *md_type_ptr;
450 unsigned char buf[150];
462 for( md_type_ptr =
md_list(); *md_type_ptr != 0; md_type_ptr++ )
475 void test_suite_md_null_args( )
479 unsigned char buf[1] = { 0 };
533 void test_suite_md_info(
int md_type,
char *md_name,
int md_size )
536 const int *md_type_ptr;
547 for( md_type_ptr =
md_list(); *md_type_ptr != 0; md_type_ptr++ )
548 if( *md_type_ptr == md_type )
556 void test_suite_md_text(
char *text_md_name,
char *text_src_string,
char *hex_hash_string )
559 unsigned char src_str[1000];
560 unsigned char hash_str[1000];
561 unsigned char output[100];
564 memset(md_name, 0x00, 100);
565 memset(src_str, 0x00, 1000);
566 memset(hash_str, 0x00, 1000);
567 memset(output, 0x00, 100);
569 strncpy( (
char *) src_str, text_src_string,
sizeof(src_str) - 1 );
570 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
574 TEST_ASSERT ( 0 ==
md( md_info, src_str, strlen( (
char *) src_str ), output ) );
577 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
583 void test_suite_md_hex(
char *text_md_name,
char *hex_src_string,
char *hex_hash_string )
586 unsigned char src_str[10000];
587 unsigned char hash_str[10000];
588 unsigned char output[100];
592 memset(md_name, 0x00, 100);
593 memset(src_str, 0x00, 10000);
594 memset(hash_str, 0x00, 10000);
595 memset(output, 0x00, 100);
597 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
601 src_len =
unhexify( src_str, hex_src_string );
602 TEST_ASSERT ( 0 ==
md( md_info, src_str, src_len, output ) );
606 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
612 void test_suite_md_text_multi(
char *text_md_name,
char *text_src_string,
613 char *hex_hash_string )
616 unsigned char src_str[1000];
617 unsigned char hash_str[1000];
618 unsigned char output[100];
625 memset(md_name, 0x00, 100);
626 memset(src_str, 0x00, 1000);
627 memset(hash_str, 0x00, 1000);
628 memset(output, 0x00, 100);
630 strncpy( (
char *) src_str, text_src_string,
sizeof(src_str) - 1 );
631 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
643 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
649 void test_suite_md_hex_multi(
char *text_md_name,
char *hex_src_string,
650 char *hex_hash_string )
653 unsigned char src_str[10000];
654 unsigned char hash_str[10000];
655 unsigned char output[100];
662 memset(md_name, 0x00, 100);
663 memset(src_str, 0x00, 10000);
664 memset(hash_str, 0x00, 10000);
665 memset(output, 0x00, 100);
667 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
672 src_len =
unhexify( src_str, hex_src_string );
681 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
687 void test_suite_md_hmac(
char *text_md_name,
int trunc_size,
char *hex_key_string,
688 char *hex_src_string,
char *hex_hash_string )
691 unsigned char src_str[10000];
692 unsigned char key_str[10000];
693 unsigned char hash_str[10000];
694 unsigned char output[100];
695 int key_len, src_len;
698 memset(md_name, 0x00, 100);
699 memset(src_str, 0x00, 10000);
700 memset(key_str, 0x00, 10000);
701 memset(hash_str, 0x00, 10000);
702 memset(output, 0x00, 100);
704 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
708 key_len =
unhexify( key_str, hex_key_string );
709 src_len =
unhexify( src_str, hex_src_string );
714 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
720 void test_suite_md_hmac_multi(
char *text_md_name,
int trunc_size,
char *hex_key_string,
721 char *hex_src_string,
char *hex_hash_string )
724 unsigned char src_str[10000];
725 unsigned char key_str[10000];
726 unsigned char hash_str[10000];
727 unsigned char output[100];
728 int key_len, src_len;
734 memset(md_name, 0x00, 100);
735 memset(src_str, 0x00, 10000);
736 memset(key_str, 0x00, 10000);
737 memset(hash_str, 0x00, 10000);
738 memset(output, 0x00, 100);
740 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
745 key_len =
unhexify( key_str, hex_key_string );
746 src_len =
unhexify( src_str, hex_src_string );
754 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
757 memset(hash_str, 0x00, 10000);
758 memset(output, 0x00, 100);
765 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
771 #ifdef POLARSSL_FS_IO
772 void test_suite_md_file(
char *text_md_name,
char *filename,
char *hex_hash_string )
775 unsigned char hash_str[1000];
776 unsigned char output[100];
779 memset(md_name, 0x00, 100);
780 memset(hash_str, 0x00, 1000);
781 memset(output, 0x00, 100);
783 strncpy( (
char *) md_name, text_md_name,
sizeof(md_name) - 1 );
787 md_file( md_info, filename, output);
790 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
806 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
808 #if defined(POLARSSL_SHA1_C)
814 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
816 #if defined(POLARSSL_MD2_C)
822 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
824 #if defined(POLARSSL_MD5_C)
830 if( strcmp( str,
"POLARSSL_RIPEMD160_C" ) == 0 )
832 #if defined(POLARSSL_RIPEMD160_C)
838 if( strcmp( str,
"POLARSSL_MD_C" ) == 0 )
840 #if defined(POLARSSL_MD_C)
846 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
848 #if defined(POLARSSL_MD4_C)
854 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
856 #if defined(POLARSSL_SHA512_C)
862 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
864 #if defined(POLARSSL_SHA256_C)
881 #if defined(TEST_SUITE_ACTIVE)
882 if( strcmp( params[0],
"md_process" ) == 0 )
888 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
893 test_suite_md_process( );
899 if( strcmp( params[0],
"md_null_args" ) == 0 )
905 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
910 test_suite_md_null_args( );
916 if( strcmp( params[0],
"md_info" ) == 0 )
920 char *param2 = params[2];
925 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
929 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
931 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
933 test_suite_md_info( param1, param2, param3 );
939 if( strcmp( params[0],
"md_text" ) == 0 )
942 char *param1 = params[1];
943 char *param2 = params[2];
944 char *param3 = params[3];
948 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
956 test_suite_md_text( param1, param2, param3 );
962 if( strcmp( params[0],
"md_hex" ) == 0 )
965 char *param1 = params[1];
966 char *param2 = params[2];
967 char *param3 = params[3];
971 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
979 test_suite_md_hex( param1, param2, param3 );
985 if( strcmp( params[0],
"md_text_multi" ) == 0 )
988 char *param1 = params[1];
989 char *param2 = params[2];
990 char *param3 = params[3];
994 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1002 test_suite_md_text_multi( param1, param2, param3 );
1008 if( strcmp( params[0],
"md_hex_multi" ) == 0 )
1011 char *param1 = params[1];
1012 char *param2 = params[2];
1013 char *param3 = params[3];
1017 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1025 test_suite_md_hex_multi( param1, param2, param3 );
1031 if( strcmp( params[0],
"md_hmac" ) == 0 )
1034 char *param1 = params[1];
1036 char *param3 = params[3];
1037 char *param4 = params[4];
1038 char *param5 = params[5];
1042 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1047 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1052 test_suite_md_hmac( param1, param2, param3, param4, param5 );
1058 if( strcmp( params[0],
"md_hmac_multi" ) == 0 )
1061 char *param1 = params[1];
1063 char *param3 = params[3];
1064 char *param4 = params[4];
1065 char *param5 = params[5];
1069 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1074 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1079 test_suite_md_hmac_multi( param1, param2, param3, param4, param5 );
1085 if( strcmp( params[0],
"md_file" ) == 0 )
1087 #ifdef POLARSSL_FS_IO
1089 char *param1 = params[1];
1090 char *param2 = params[2];
1091 char *param3 = params[3];
1095 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1103 test_suite_md_file( param1, param2, param3 );
1112 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1126 ret = fgets( buf, len, f );
1130 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1131 buf[strlen(buf) - 1] =
'\0';
1132 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1133 buf[strlen(buf) - 1] =
'\0';
1144 params[cnt++] = cur;
1146 while( *p !=
'\0' && p < buf + len )
1156 if( p + 1 < buf + len )
1159 params[cnt++] = cur;
1168 for( i = 0; i < cnt; i++ )
1175 if( *p ==
'\\' && *(p + 1) ==
'n' )
1180 else if( *p ==
'\\' && *(p + 1) ==
':' )
1185 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1201 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1202 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_md.data";
1207 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1208 unsigned char alloc_buf[1000000];
1212 file = fopen( filename,
"r" );
1215 fprintf( stderr,
"Failed to open\n" );
1219 while( !feof( file ) )
1223 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1225 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1226 fprintf( stdout,
" " );
1227 for( i = strlen( buf ) + 1; i < 67; i++ )
1228 fprintf( stdout,
"." );
1229 fprintf( stdout,
" " );
1234 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1238 if( strcmp( params[0],
"depends_on" ) == 0 )
1240 for( i = 1; i < cnt; i++ )
1244 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1255 if( skip == 1 || ret == 3 )
1258 fprintf( stdout,
"----\n" );
1263 fprintf( stdout,
"PASS\n" );
1268 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1275 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1277 if( strlen(buf) != 0 )
1279 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1285 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1286 if( total_errors == 0 )
1287 fprintf( stdout,
"PASSED" );
1289 fprintf( stdout,
"FAILED" );
1291 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1292 total_tests - total_errors, total_tests, total_skipped );
1294 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1295 #if defined(POLARSSL_MEMORY_DEBUG)
1296 memory_buffer_alloc_status();
1301 return( total_errors != 0 );
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
int get_line(FILE *f, char *buf, size_t len)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
Memory allocation layer (Deprecated to platform layer)
Info structure for the pseudo random function.
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int md_file(const md_info_t *md_info, const char *path, unsigned char *output)
Output = message_digest( file contents )
#define PUT_UINT32_BE(n, b, i)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
int md_process(md_context_t *ctx, const unsigned char *data)
Configuration options (set of defines)
int dispatch_test(int cnt, char *params[50])
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
const md_info_t * md_info_from_string(const char *md_name)
Returns the message digest information associated with the given digest name.
static int test_assert(int correct, const char *test)
static md_type_t md_get_type(const md_info_t *md_info)
Returns the type of the message digest output.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
#define POLARSSL_ERR_MD_BAD_INPUT_DATA
Bad input parameters to function.
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
const int * md_list(void)
Returns the list of digests supported by the generic digest module.
int md_hmac_starts(md_context_t *ctx, const unsigned char *key, size_t keylen)
Generic HMAC context setup.
void * md_ctx
Digest-specific context.
Generic message digest wrapper.
int md_hmac(const md_info_t *md_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
Output = Generic_HMAC( hmac key, input buffer )
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int md_hmac_reset(md_context_t *ctx)
Generic HMAC context reset.
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
static int unhexify(unsigned char *obuf, const char *ibuf)
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int verify_int(char *str, int *value)
int parse_arguments(char *buf, size_t len, char *params[50])
Message digest information.
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
Generic message digest context.