15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19 #if defined(WANT_NOT_RND_MPI)
20 #if defined(POLARSSL_BIGNUM_C)
23 #error "not_rnd_mpi() need bignum.c"
29 typedef UINT32 uint32_t;
42 #define GET_UINT32_BE(n,b,i) \
44 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
45 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
46 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
47 | ( (uint32_t) (b)[(i) + 3] ); \
52 #define PUT_UINT32_BE(n,b,i) \
54 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
55 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
56 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
57 (b)[(i) + 3] = (unsigned char) ( (n) ); \
61 static int unhexify(
unsigned char *obuf,
const char *ibuf)
64 int len = strlen(ibuf) / 2;
65 assert(!(strlen(ibuf) %1));
70 if( c >=
'0' && c <=
'9' )
72 else if( c >=
'a' && c <=
'f' )
74 else if( c >=
'A' && c <=
'F' )
80 if( c2 >=
'0' && c2 <=
'9' )
82 else if( c2 >=
'a' && c2 <=
'f' )
84 else if( c2 >=
'A' && c2 <=
'F' )
89 *obuf++ = ( c << 4 ) | c2;
95 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
107 *obuf++ =
'a' + h - 10;
112 *obuf++ =
'a' + l - 10;
128 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
132 if( rng_state != NULL )
135 for( i = 0; i < len; ++i )
146 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
148 if( rng_state != NULL )
151 memset( output, 0, len );
178 if( rng_state == NULL )
187 memcpy( output, info->
buf, use_len );
188 info->
buf += use_len;
192 if( len - use_len > 0 )
193 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
222 uint32_t i, *k, sum, delta=0x9E3779B9;
223 unsigned char result[4];
225 if( rng_state == NULL )
232 size_t use_len = ( len > 4 ) ? 4 : len;
235 for( i = 0; i < 32; i++ )
237 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
239 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
243 memcpy( output, result, use_len );
250 #if defined(WANT_NOT_RND_MPI)
259 #define ciL (sizeof(t_uint))
260 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
261 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
263 char *str = (
char *) in;
272 X.
n = CHARS_TO_LIMBS( len );
278 assert( strlen( str ) / 2 == len );
292 #define TEST_SUITE_ACTIVE
300 if( test_errors == 1 )
301 printf(
"FAILED\n" );
302 printf(
" %s\n", test );
307 #define TEST_ASSERT( TEST ) \
308 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
309 if( test_errors) return; \
314 if( (*str)[0] !=
'"' ||
315 (*str)[strlen( *str ) - 1] !=
'"' )
317 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
322 (*str)[strlen( *str ) - 1] =
'\0';
334 for( i = 0; i < strlen( str ); i++ )
336 if( i == 0 && str[i] ==
'-' )
342 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
343 str[i - 1] ==
'0' && str[i] ==
'x' )
349 if( str[i] <
'0' || str[i] >
'9' )
359 *value = strtol( str, NULL, 16 );
361 *value = strtol( str, NULL, 10 );
368 printf(
"Expected integer for parameter and got: %s\n", str );
372 void test_suite_md_text(
char *text_md_name,
char *text_src_string,
char *hex_hash_string )
375 unsigned char src_str[1000];
376 unsigned char hash_str[1000];
377 unsigned char output[100];
380 memset(md_name, 0x00, 100);
381 memset(src_str, 0x00, 1000);
382 memset(hash_str, 0x00, 1000);
383 memset(output, 0x00, 100);
385 strcpy( (
char *) src_str, text_src_string );
387 strncpy( (
char *) md_name, text_md_name, 100 );
391 TEST_ASSERT ( 0 ==
md( md_info, src_str, strlen( (
char *) src_str ), output ) );
394 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
397 void test_suite_md_hex(
char *text_md_name,
char *hex_src_string,
char *hex_hash_string )
400 unsigned char src_str[10000];
401 unsigned char hash_str[10000];
402 unsigned char output[100];
406 memset(md_name, 0x00, 100);
407 memset(src_str, 0x00, 10000);
408 memset(hash_str, 0x00, 10000);
409 memset(output, 0x00, 100);
411 strncpy( (
char *) md_name, text_md_name, 100 );
415 src_len =
unhexify( src_str, hex_src_string );
416 TEST_ASSERT ( 0 ==
md( md_info, src_str, src_len, output ) );
420 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
423 void test_suite_md_text_multi(
char *text_md_name,
char *text_src_string,
424 char *hex_hash_string )
427 unsigned char src_str[1000];
428 unsigned char hash_str[1000];
429 unsigned char output[100];
434 memset(md_name, 0x00, 100);
435 memset(src_str, 0x00, 1000);
436 memset(hash_str, 0x00, 1000);
437 memset(output, 0x00, 100);
439 strcpy( (
char *) src_str, text_src_string );
441 strncpy( (
char *) md_name, text_md_name, 100 );
454 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
457 void test_suite_md_hex_multi(
char *text_md_name,
char *hex_src_string,
458 char *hex_hash_string )
461 unsigned char src_str[10000];
462 unsigned char hash_str[10000];
463 unsigned char output[100];
468 memset(md_name, 0x00, 100);
469 memset(src_str, 0x00, 10000);
470 memset(hash_str, 0x00, 10000);
471 memset(output, 0x00, 100);
473 strncpy( (
char *) md_name, text_md_name, 100 );
478 src_len =
unhexify( src_str, hex_src_string );
488 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
491 void test_suite_md_hmac(
char *text_md_name,
int trunc_size,
char *hex_key_string,
492 char *hex_src_string,
char *hex_hash_string )
495 unsigned char src_str[10000];
496 unsigned char key_str[10000];
497 unsigned char hash_str[10000];
498 unsigned char output[100];
499 int key_len, src_len;
502 memset(md_name, 0x00, 100);
503 memset(src_str, 0x00, 10000);
504 memset(key_str, 0x00, 10000);
505 memset(hash_str, 0x00, 10000);
506 memset(output, 0x00, 100);
508 strncpy( (
char *) md_name, text_md_name, 100 );
512 key_len =
unhexify( key_str, hex_key_string );
513 src_len =
unhexify( src_str, hex_src_string );
518 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
521 void test_suite_md_hmac_multi(
char *text_md_name,
int trunc_size,
char *hex_key_string,
522 char *hex_src_string,
char *hex_hash_string )
525 unsigned char src_str[10000];
526 unsigned char key_str[10000];
527 unsigned char hash_str[10000];
528 unsigned char output[100];
529 int key_len, src_len;
533 memset(md_name, 0x00, 100);
534 memset(src_str, 0x00, 10000);
535 memset(key_str, 0x00, 10000);
536 memset(hash_str, 0x00, 10000);
537 memset(output, 0x00, 100);
539 strncpy( (
char *) md_name, text_md_name, 100 );
544 key_len =
unhexify( key_str, hex_key_string );
545 src_len =
unhexify( src_str, hex_src_string );
555 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
558 #ifdef POLARSSL_FS_IO
559 void test_suite_md_file(
char *text_md_name,
char *filename,
char *hex_hash_string )
562 unsigned char hash_str[1000];
563 unsigned char output[100];
566 memset(md_name, 0x00, 100);
567 memset(hash_str, 0x00, 1000);
568 memset(output, 0x00, 100);
570 strncpy( (
char *) md_name, text_md_name, 100 );
574 md_file( md_info, filename, output);
577 TEST_ASSERT( strcmp( (
char *) hash_str, hex_hash_string ) == 0 );
590 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
592 #if defined(POLARSSL_SHA256_C)
598 if( strcmp( str,
"POLARSSL_MD_C" ) == 0 )
600 #if defined(POLARSSL_MD_C)
606 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
608 #if defined(POLARSSL_SHA1_C)
614 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
616 #if defined(POLARSSL_MD2_C)
622 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
624 #if defined(POLARSSL_MD5_C)
630 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
632 #if defined(POLARSSL_SHA512_C)
638 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
640 #if defined(POLARSSL_MD4_C)
657 #if defined(TEST_SUITE_ACTIVE)
658 if( strcmp( params[0],
"md_text" ) == 0 )
661 char *param1 = params[1];
662 char *param2 = params[2];
663 char *param3 = params[3];
667 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
675 test_suite_md_text( param1, param2, param3 );
681 if( strcmp( params[0],
"md_hex" ) == 0 )
684 char *param1 = params[1];
685 char *param2 = params[2];
686 char *param3 = params[3];
690 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
698 test_suite_md_hex( param1, param2, param3 );
704 if( strcmp( params[0],
"md_text_multi" ) == 0 )
707 char *param1 = params[1];
708 char *param2 = params[2];
709 char *param3 = params[3];
713 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
721 test_suite_md_text_multi( param1, param2, param3 );
727 if( strcmp( params[0],
"md_hex_multi" ) == 0 )
730 char *param1 = params[1];
731 char *param2 = params[2];
732 char *param3 = params[3];
736 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
744 test_suite_md_hex_multi( param1, param2, param3 );
750 if( strcmp( params[0],
"md_hmac" ) == 0 )
753 char *param1 = params[1];
755 char *param3 = params[3];
756 char *param4 = params[4];
757 char *param5 = params[5];
761 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
766 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
771 test_suite_md_hmac( param1, param2, param3, param4, param5 );
777 if( strcmp( params[0],
"md_hmac_multi" ) == 0 )
780 char *param1 = params[1];
782 char *param3 = params[3];
783 char *param4 = params[4];
784 char *param5 = params[5];
788 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
793 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
798 test_suite_md_hmac_multi( param1, param2, param3, param4, param5 );
804 if( strcmp( params[0],
"md_file" ) == 0 )
806 #ifdef POLARSSL_FS_IO
808 char *param1 = params[1];
809 char *param2 = params[2];
810 char *param3 = params[3];
814 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
822 test_suite_md_file( param1, param2, param3 );
831 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
845 ret = fgets( buf, len, f );
849 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
850 buf[strlen(buf) - 1] =
'\0';
851 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
852 buf[strlen(buf) - 1] =
'\0';
865 while( *p !=
'\0' && p < buf + len )
875 if( p + 1 < buf + len )
887 for( i = 0; i < cnt; i++ )
894 if( *p ==
'\\' && *(p + 1) ==
'n' )
899 else if( *p ==
'\\' && *(p + 1) ==
':' )
904 else if( *p ==
'\\' && *(p + 1) ==
'?' )
920 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
921 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_md.data";
926 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
927 unsigned char alloc_buf[1000000];
928 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
931 file = fopen( filename,
"r" );
934 fprintf( stderr,
"Failed to open\n" );
938 while( !feof( file ) )
942 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
944 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
945 fprintf( stdout,
" " );
946 for( i = strlen( buf ) + 1; i < 67; i++ )
947 fprintf( stdout,
"." );
948 fprintf( stdout,
" " );
953 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
957 if( strcmp( params[0],
"depends_on" ) == 0 )
959 for( i = 1; i < cnt; i++ )
963 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
974 if( skip == 1 || ret == 3 )
977 fprintf( stdout,
"----\n" );
980 else if( ret == 0 && test_errors == 0 )
982 fprintf( stdout,
"PASS\n" );
987 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
994 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
996 if( strlen(buf) != 0 )
998 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1004 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1005 if( total_errors == 0 )
1006 fprintf( stdout,
"PASSED" );
1008 fprintf( stdout,
"FAILED" );
1010 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1011 total_tests - total_errors, total_tests, total_skipped );
1013 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1014 #if defined(POLARSSL_MEMORY_DEBUG)
1015 memory_buffer_alloc_status();
1017 memory_buffer_alloc_free();
1020 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 )
#define MD_CONTEXT_T_INIT
Info structure for the pseudo random function.
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
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.
Configuration options (set of defines)
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, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
#define TEST_ASSERT(TEST)
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.
int parse_arguments(char *buf, size_t len, char *params[50])
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
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 )
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.
SHA-1 cryptographic hash function.
int dispatch_test(int cnt, char *params[50])
static int unhexify(unsigned char *obuf, const char *ibuf)
SHA-384 and SHA-512 cryptographic hash function.
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int verify_int(char *str, int *value)
MD4 message digest algorithm (hash function)
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
MD5 message digest algorithm (hash function)
SHA-224 and SHA-256 cryptographic hash function.
Message digest information.
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
MD2 message digest algorithm (hash function)
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
int get_line(FILE *f, char *buf, size_t len)
Generic message digest context.