9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
289 #define TEST_SUITE_ACTIVE
298 printf(
"FAILED\n" );
299 printf(
" %s\n", test );
304 #define TEST_ASSERT( TEST ) \
305 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
306 if( test_errors) return; \
311 if( (*str)[0] !=
'"' ||
312 (*str)[strlen( *str ) - 1] !=
'"' )
314 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
319 (*str)[strlen( *str ) - 1] =
'\0';
331 for( i = 0; i < strlen( str ); i++ )
333 if( i == 0 && str[i] ==
'-' )
339 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
340 str[i - 1] ==
'0' && str[i] ==
'x' )
346 if( str[i] <
'0' || str[i] >
'9' )
356 *value = strtol( str, NULL, 16 );
358 *value = strtol( str, NULL, 10 );
365 printf(
"Expected integer for parameter and got: %s\n", str );
369 #ifdef POLARSSL_SHA1_C
370 void test_suite_sha1_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
371 char *hex_hash_string)
373 unsigned char src_str[10000];
374 unsigned char key_str[10000];
375 unsigned char hash_str[10000];
376 unsigned char output[41];
377 int key_len, src_len;
379 memset(src_str, 0x00, 10000);
380 memset(key_str, 0x00, 10000);
381 memset(hash_str, 0x00, 10000);
382 memset(output, 0x00, 41);
384 key_len =
unhexify( key_str, hex_key_string );
385 src_len =
unhexify( src_str, hex_src_string );
387 sha1_hmac( key_str, key_len, src_str, src_len, output );
388 hexify( hash_str, output, 20 );
390 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
394 #ifdef POLARSSL_SHA256_C
395 void test_suite_sha224_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
396 char *hex_hash_string)
398 unsigned char src_str[10000];
399 unsigned char key_str[10000];
400 unsigned char hash_str[10000];
401 unsigned char output[57];
402 int key_len, src_len;
404 memset(src_str, 0x00, 10000);
405 memset(key_str, 0x00, 10000);
406 memset(hash_str, 0x00, 10000);
407 memset(output, 0x00, 57);
409 key_len =
unhexify( key_str, hex_key_string );
410 src_len =
unhexify( src_str, hex_src_string );
412 sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
413 hexify( hash_str, output, 28 );
415 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
419 #ifdef POLARSSL_SHA256_C
420 void test_suite_sha256_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
421 char *hex_hash_string)
423 unsigned char src_str[10000];
424 unsigned char key_str[10000];
425 unsigned char hash_str[10000];
426 unsigned char output[65];
427 int key_len, src_len;
429 memset(src_str, 0x00, 10000);
430 memset(key_str, 0x00, 10000);
431 memset(hash_str, 0x00, 10000);
432 memset(output, 0x00, 65);
434 key_len =
unhexify( key_str, hex_key_string );
435 src_len =
unhexify( src_str, hex_src_string );
437 sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
438 hexify( hash_str, output, 32 );
440 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
444 #ifdef POLARSSL_SHA512_C
445 void test_suite_sha384_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
446 char *hex_hash_string)
448 unsigned char src_str[10000];
449 unsigned char key_str[10000];
450 unsigned char hash_str[10000];
451 unsigned char output[97];
452 int key_len, src_len;
454 memset(src_str, 0x00, 10000);
455 memset(key_str, 0x00, 10000);
456 memset(hash_str, 0x00, 10000);
457 memset(output, 0x00, 97);
459 key_len =
unhexify( key_str, hex_key_string );
460 src_len =
unhexify( src_str, hex_src_string );
462 sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
463 hexify( hash_str, output, 48 );
465 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
469 #ifdef POLARSSL_SHA512_C
470 void test_suite_sha512_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
471 char *hex_hash_string)
473 unsigned char src_str[10000];
474 unsigned char key_str[10000];
475 unsigned char hash_str[10000];
476 unsigned char output[129];
477 int key_len, src_len;
479 memset(src_str, 0x00, 10000);
480 memset(key_str, 0x00, 10000);
481 memset(hash_str, 0x00, 10000);
482 memset(output, 0x00, 129);
484 key_len =
unhexify( key_str, hex_key_string );
485 src_len =
unhexify( src_str, hex_src_string );
487 sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
488 hexify( hash_str, output, 64 );
490 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
502 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
504 #if defined(POLARSSL_SHA512_C)
510 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
512 #if defined(POLARSSL_SHA256_C)
518 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
520 #if defined(POLARSSL_SHA1_C)
537 #if defined(TEST_SUITE_ACTIVE)
538 if( strcmp( params[0],
"sha1_hmac" ) == 0 )
540 #ifdef POLARSSL_SHA1_C
543 char *param2 = params[2];
544 char *param3 = params[3];
545 char *param4 = params[4];
549 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
553 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
558 test_suite_sha1_hmac( param1, param2, param3, param4 );
565 if( strcmp( params[0],
"sha224_hmac" ) == 0 )
567 #ifdef POLARSSL_SHA256_C
570 char *param2 = params[2];
571 char *param3 = params[3];
572 char *param4 = params[4];
576 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
580 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
585 test_suite_sha224_hmac( param1, param2, param3, param4 );
592 if( strcmp( params[0],
"sha256_hmac" ) == 0 )
594 #ifdef POLARSSL_SHA256_C
597 char *param2 = params[2];
598 char *param3 = params[3];
599 char *param4 = params[4];
603 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
607 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
612 test_suite_sha256_hmac( param1, param2, param3, param4 );
619 if( strcmp( params[0],
"sha384_hmac" ) == 0 )
621 #ifdef POLARSSL_SHA512_C
624 char *param2 = params[2];
625 char *param3 = params[3];
626 char *param4 = params[4];
630 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
634 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
639 test_suite_sha384_hmac( param1, param2, param3, param4 );
646 if( strcmp( params[0],
"sha512_hmac" ) == 0 )
648 #ifdef POLARSSL_SHA512_C
651 char *param2 = params[2];
652 char *param3 = params[3];
653 char *param4 = params[4];
657 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
661 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
666 test_suite_sha512_hmac( param1, param2, param3, param4 );
675 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
689 ret = fgets( buf, len, f );
693 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
694 buf[strlen(buf) - 1] =
'\0';
695 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
696 buf[strlen(buf) - 1] =
'\0';
709 while( *p !=
'\0' && p < buf + len )
719 if( p + 1 < buf + len )
731 for( i = 0; i < cnt; i++ )
738 if( *p ==
'\\' && *(p + 1) ==
'n' )
743 else if( *p ==
'\\' && *(p + 1) ==
':' )
748 else if( *p ==
'\\' && *(p + 1) ==
'?' )
764 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
765 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_hmac_shax.data";
770 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
771 unsigned char alloc_buf[1000000];
772 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
775 file = fopen( filename,
"r" );
778 fprintf( stderr,
"Failed to open\n" );
782 while( !feof( file ) )
786 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
788 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
789 fprintf( stdout,
" " );
790 for( i = strlen( buf ) + 1; i < 67; i++ )
791 fprintf( stdout,
"." );
792 fprintf( stdout,
" " );
797 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
801 if( strcmp( params[0],
"depends_on" ) == 0 )
803 for( i = 1; i < cnt; i++ )
807 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
818 if( skip == 1 || ret == 3 )
821 fprintf( stdout,
"----\n" );
826 fprintf( stdout,
"PASS\n" );
831 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
838 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
840 if( strlen(buf) != 0 )
842 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
848 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
849 if( total_errors == 0 )
850 fprintf( stdout,
"PASSED" );
852 fprintf( stdout,
"FAILED" );
854 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
855 total_tests - total_errors, total_tests, total_skipped );
857 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
858 #if defined(POLARSSL_MEMORY_DEBUG)
859 memory_buffer_alloc_status();
861 memory_buffer_alloc_free();
864 return( total_errors != 0 );
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Info structure for the pseudo random function.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void sha256_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = HMAC-SHA-256( hmac key, input buffer )
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void sha1_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[20])
Output = HMAC-SHA-1( hmac key, input buffer )
Configuration options (set of defines)
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
#define TEST_ASSERT(TEST)
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
int parse_arguments(char *buf, size_t len, char *params[50])
void sha512_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = HMAC-SHA-512( hmac key, input buffer )
int verify_string(char **str)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
SHA-1 cryptographic hash function.
int dispatch_test(int cnt, char *params[50])
SHA-384 and SHA-512 cryptographic hash function.
#define PUT_UINT32_BE(n, b, i)
int verify_int(char *str, int *value)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
SHA-224 and SHA-256 cryptographic hash function.
int get_line(FILE *f, char *buf, size_t len)