9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
13 #if defined(WANT_NOT_RND_MPI)
14 #if defined(POLARSSL_BIGNUM_C)
17 #error "not_rnd_mpi() need bignum.c"
23 typedef UINT32 uint32_t;
36 #define GET_UINT32_BE(n,b,i) \
38 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
39 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
40 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
41 | ( (uint32_t) (b)[(i) + 3] ); \
46 #define PUT_UINT32_BE(n,b,i) \
48 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
49 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
50 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
51 (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 static int unhexify(
unsigned char *obuf,
const char *ibuf)
58 int len = strlen(ibuf) / 2;
59 assert(!(strlen(ibuf) %1));
64 if( c >=
'0' && c <=
'9' )
66 else if( c >=
'a' && c <=
'f' )
68 else if( c >=
'A' && c <=
'F' )
74 if( c2 >=
'0' && c2 <=
'9' )
76 else if( c2 >=
'a' && c2 <=
'f' )
78 else if( c2 >=
'A' && c2 <=
'F' )
83 *obuf++ = ( c << 4 ) | c2;
89 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
101 *obuf++ =
'a' + h - 10;
106 *obuf++ =
'a' + l - 10;
122 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
126 if( rng_state != NULL )
129 for( i = 0; i < len; ++i )
140 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
142 if( rng_state != NULL )
145 memset( output, 0, len );
172 if( rng_state == NULL )
181 memcpy( output, info->
buf, use_len );
182 info->
buf += use_len;
186 if( len - use_len > 0 )
187 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
216 uint32_t i, *k, sum, delta=0x9E3779B9;
217 unsigned char result[4];
219 if( rng_state == NULL )
226 size_t use_len = ( len > 4 ) ? 4 : len;
229 for( i = 0; i < 32; i++ )
231 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
233 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
237 memcpy( output, result, use_len );
244 #if defined(WANT_NOT_RND_MPI)
253 #define ciL (sizeof(t_uint))
254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
255 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
257 char *str = (
char *) in;
266 X.
n = CHARS_TO_LIMBS( len );
272 assert( strlen( str ) / 2 == len );
285 #define TEST_SUITE_ACTIVE
293 if( test_errors == 1 )
294 printf(
"FAILED\n" );
295 printf(
" %s\n", test );
300 #define TEST_ASSERT( TEST ) \
301 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
302 if( test_errors) return; \
307 if( (*str)[0] !=
'"' ||
308 (*str)[strlen( *str ) - 1] !=
'"' )
310 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
315 (*str)[strlen( *str ) - 1] =
'\0';
327 for( i = 0; i < strlen( str ); i++ )
329 if( i == 0 && str[i] ==
'-' )
335 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
336 str[i - 1] ==
'0' && str[i] ==
'x' )
342 if( str[i] <
'0' || str[i] >
'9' )
352 *value = strtol( str, NULL, 16 );
354 *value = strtol( str, NULL, 10 );
361 printf(
"Expected integer for parameter and got: %s\n", str );
365 #ifdef POLARSSL_SHA1_C
366 void test_suite_sha1_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
367 char *hex_hash_string)
369 unsigned char src_str[10000];
370 unsigned char key_str[10000];
371 unsigned char hash_str[10000];
372 unsigned char output[41];
373 int key_len, src_len;
375 memset(src_str, 0x00, 10000);
376 memset(key_str, 0x00, 10000);
377 memset(hash_str, 0x00, 10000);
378 memset(output, 0x00, 41);
380 key_len =
unhexify( key_str, hex_key_string );
381 src_len =
unhexify( src_str, hex_src_string );
383 sha1_hmac( key_str, key_len, src_str, src_len, output );
384 hexify( hash_str, output, 20 );
386 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
390 #ifdef POLARSSL_SHA256_C
391 void test_suite_sha224_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
392 char *hex_hash_string)
394 unsigned char src_str[10000];
395 unsigned char key_str[10000];
396 unsigned char hash_str[10000];
397 unsigned char output[57];
398 int key_len, src_len;
400 memset(src_str, 0x00, 10000);
401 memset(key_str, 0x00, 10000);
402 memset(hash_str, 0x00, 10000);
403 memset(output, 0x00, 57);
405 key_len =
unhexify( key_str, hex_key_string );
406 src_len =
unhexify( src_str, hex_src_string );
408 sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
409 hexify( hash_str, output, 28 );
411 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
415 #ifdef POLARSSL_SHA256_C
416 void test_suite_sha256_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
417 char *hex_hash_string)
419 unsigned char src_str[10000];
420 unsigned char key_str[10000];
421 unsigned char hash_str[10000];
422 unsigned char output[65];
423 int key_len, src_len;
425 memset(src_str, 0x00, 10000);
426 memset(key_str, 0x00, 10000);
427 memset(hash_str, 0x00, 10000);
428 memset(output, 0x00, 65);
430 key_len =
unhexify( key_str, hex_key_string );
431 src_len =
unhexify( src_str, hex_src_string );
433 sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
434 hexify( hash_str, output, 32 );
436 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
440 #ifdef POLARSSL_SHA512_C
441 void test_suite_sha384_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
442 char *hex_hash_string)
444 unsigned char src_str[10000];
445 unsigned char key_str[10000];
446 unsigned char hash_str[10000];
447 unsigned char output[97];
448 int key_len, src_len;
450 memset(src_str, 0x00, 10000);
451 memset(key_str, 0x00, 10000);
452 memset(hash_str, 0x00, 10000);
453 memset(output, 0x00, 97);
455 key_len =
unhexify( key_str, hex_key_string );
456 src_len =
unhexify( src_str, hex_src_string );
458 sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
459 hexify( hash_str, output, 48 );
461 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
465 #ifdef POLARSSL_SHA512_C
466 void test_suite_sha512_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
467 char *hex_hash_string)
469 unsigned char src_str[10000];
470 unsigned char key_str[10000];
471 unsigned char hash_str[10000];
472 unsigned char output[129];
473 int key_len, src_len;
475 memset(src_str, 0x00, 10000);
476 memset(key_str, 0x00, 10000);
477 memset(hash_str, 0x00, 10000);
478 memset(output, 0x00, 129);
480 key_len =
unhexify( key_str, hex_key_string );
481 src_len =
unhexify( src_str, hex_src_string );
483 sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
484 hexify( hash_str, output, 64 );
486 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
498 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
500 #if defined(POLARSSL_SHA512_C)
506 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
508 #if defined(POLARSSL_SHA1_C)
514 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
516 #if defined(POLARSSL_SHA256_C)
533 #if defined(TEST_SUITE_ACTIVE)
534 if( strcmp( params[0],
"sha1_hmac" ) == 0 )
536 #ifdef POLARSSL_SHA1_C
539 char *param2 = params[2];
540 char *param3 = params[3];
541 char *param4 = params[4];
545 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
549 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
554 test_suite_sha1_hmac( param1, param2, param3, param4 );
561 if( strcmp( params[0],
"sha224_hmac" ) == 0 )
563 #ifdef POLARSSL_SHA256_C
566 char *param2 = params[2];
567 char *param3 = params[3];
568 char *param4 = params[4];
572 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
576 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
581 test_suite_sha224_hmac( param1, param2, param3, param4 );
588 if( strcmp( params[0],
"sha256_hmac" ) == 0 )
590 #ifdef POLARSSL_SHA256_C
593 char *param2 = params[2];
594 char *param3 = params[3];
595 char *param4 = params[4];
599 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
603 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
608 test_suite_sha256_hmac( param1, param2, param3, param4 );
615 if( strcmp( params[0],
"sha384_hmac" ) == 0 )
617 #ifdef POLARSSL_SHA512_C
620 char *param2 = params[2];
621 char *param3 = params[3];
622 char *param4 = params[4];
626 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
630 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
635 test_suite_sha384_hmac( param1, param2, param3, param4 );
642 if( strcmp( params[0],
"sha512_hmac" ) == 0 )
644 #ifdef POLARSSL_SHA512_C
647 char *param2 = params[2];
648 char *param3 = params[3];
649 char *param4 = params[4];
653 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
657 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
662 test_suite_sha512_hmac( param1, param2, param3, param4 );
671 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
685 ret = fgets( buf, len, f );
689 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
690 buf[strlen(buf) - 1] =
'\0';
691 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
692 buf[strlen(buf) - 1] =
'\0';
705 while( *p !=
'\0' && p < buf + len )
715 if( p + 1 < buf + len )
727 for( i = 0; i < cnt; i++ )
734 if( *p ==
'\\' && *(p + 1) ==
'n' )
739 else if( *p ==
'\\' && *(p + 1) ==
':' )
744 else if( *p ==
'\\' && *(p + 1) ==
'?' )
760 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
761 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_hmac_shax.data";
766 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
767 unsigned char alloc_buf[1000000];
768 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
771 file = fopen( filename,
"r" );
774 fprintf( stderr,
"Failed to open\n" );
778 while( !feof( file ) )
782 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
784 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
785 fprintf( stdout,
" " );
786 for( i = strlen( buf ) + 1; i < 67; i++ )
787 fprintf( stdout,
"." );
788 fprintf( stdout,
" " );
793 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
797 if( strcmp( params[0],
"depends_on" ) == 0 )
799 for( i = 1; i < cnt; i++ )
803 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
814 if( skip == 1 || ret == 3 )
817 fprintf( stdout,
"----\n" );
820 else if( ret == 0 && test_errors == 0 )
822 fprintf( stdout,
"PASS\n" );
827 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
834 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
836 if( strlen(buf) != 0 )
838 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
844 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
845 if( total_errors == 0 )
846 fprintf( stdout,
"PASSED" );
848 fprintf( stdout,
"FAILED" );
850 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
851 total_tests - total_errors, total_tests, total_skipped );
853 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
854 #if defined(POLARSSL_MEMORY_DEBUG)
855 memory_buffer_alloc_status();
857 memory_buffer_alloc_free();
860 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[])
Multi-precision integer library.
#define TEST_ASSERT(TEST)
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 mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
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)