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
317 #define TEST_SUITE_ACTIVE
326 printf(
"FAILED\n" );
327 printf(
" %s\n", test );
332 #define TEST_ASSERT( TEST ) \
333 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
334 if( test_errors) goto exit; \
339 if( (*str)[0] !=
'"' ||
340 (*str)[strlen( *str ) - 1] !=
'"' )
342 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
347 (*str)[strlen( *str ) - 1] =
'\0';
359 for( i = 0; i < strlen( str ); i++ )
361 if( i == 0 && str[i] ==
'-' )
367 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
368 str[i - 1] ==
'0' && str[i] ==
'x' )
374 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
375 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
376 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
386 *value = strtol( str, NULL, 16 );
388 *value = strtol( str, NULL, 10 );
395 printf(
"Expected integer for parameter and got: %s\n", str );
399 #ifdef POLARSSL_SHA1_C
400 void test_suite_sha1_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
401 char *hex_hash_string)
403 unsigned char src_str[10000];
404 unsigned char key_str[10000];
405 unsigned char hash_str[41];
406 unsigned char output[20];
407 int key_len, src_len;
410 memset(src_str, 0x00,
sizeof src_str);
411 memset(key_str, 0x00,
sizeof key_str);
414 key_len =
unhexify( key_str, hex_key_string );
415 src_len =
unhexify( src_str, hex_src_string );
418 memset(hash_str, 0x00,
sizeof hash_str);
419 memset(output, 0x00,
sizeof output);
421 sha1_hmac( key_str, key_len, src_str, src_len, output );
423 hexify( hash_str, output,
sizeof output );
424 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
427 memset( hash_str, 0x00,
sizeof hash_str );
428 memset( output, 0x00,
sizeof output );
429 memset( &ctx, 0x00,
sizeof ctx );
438 hexify( hash_str, output,
sizeof output );
439 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
442 memset( hash_str, 0x00,
sizeof hash_str );
443 memset( output, 0x00,
sizeof output );
450 hexify( hash_str, output,
sizeof output );
451 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
458 #ifdef POLARSSL_SHA256_C
459 void test_suite_sha224_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
460 char *hex_hash_string)
462 unsigned char src_str[10000];
463 unsigned char key_str[10000];
464 unsigned char hash_str[57];
465 unsigned char output[28];
466 int key_len, src_len;
469 memset(src_str, 0x00,
sizeof src_str);
470 memset(key_str, 0x00,
sizeof key_str);
473 key_len =
unhexify( key_str, hex_key_string );
474 src_len =
unhexify( src_str, hex_src_string );
477 memset(hash_str, 0x00,
sizeof hash_str);
478 memset(output, 0x00,
sizeof output);
480 sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
482 hexify( hash_str, output,
sizeof output );
483 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
486 memset( hash_str, 0x00,
sizeof hash_str );
487 memset( output, 0x00,
sizeof output );
488 memset( &ctx, 0x00,
sizeof ctx );
497 hexify( hash_str, output,
sizeof output );
498 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
501 memset( hash_str, 0x00,
sizeof hash_str );
502 memset( output, 0x00,
sizeof output );
509 hexify( hash_str, output,
sizeof output );
510 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
517 #ifdef POLARSSL_SHA256_C
518 void test_suite_sha256_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
519 char *hex_hash_string)
521 unsigned char src_str[10000];
522 unsigned char key_str[10000];
523 unsigned char hash_str[65];
524 unsigned char output[32];
525 int key_len, src_len;
528 memset(src_str, 0x00,
sizeof src_str);
529 memset(key_str, 0x00,
sizeof key_str);
532 key_len =
unhexify( key_str, hex_key_string );
533 src_len =
unhexify( src_str, hex_src_string );
536 memset(hash_str, 0x00,
sizeof hash_str);
537 memset(output, 0x00,
sizeof output);
539 sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
541 hexify( hash_str, output,
sizeof output );
542 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
545 memset( hash_str, 0x00,
sizeof hash_str );
546 memset( output, 0x00,
sizeof output );
547 memset( &ctx, 0x00,
sizeof ctx );
556 hexify( hash_str, output,
sizeof output );
557 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
560 memset( hash_str, 0x00,
sizeof hash_str );
561 memset( output, 0x00,
sizeof output );
568 hexify( hash_str, output,
sizeof output );
569 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
576 #ifdef POLARSSL_SHA512_C
577 void test_suite_sha384_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
578 char *hex_hash_string)
580 unsigned char src_str[10000];
581 unsigned char key_str[10000];
582 unsigned char hash_str[97];
583 unsigned char output[48];
584 int key_len, src_len;
587 memset(src_str, 0x00,
sizeof src_str);
588 memset(key_str, 0x00,
sizeof key_str);
591 key_len =
unhexify( key_str, hex_key_string );
592 src_len =
unhexify( src_str, hex_src_string );
595 memset(hash_str, 0x00,
sizeof hash_str);
596 memset(output, 0x00,
sizeof output);
598 sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
600 hexify( hash_str, output,
sizeof output );
601 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
604 memset( hash_str, 0x00,
sizeof hash_str );
605 memset( output, 0x00,
sizeof output );
606 memset( &ctx, 0x00,
sizeof ctx );
615 hexify( hash_str, output,
sizeof output );
616 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
619 memset( hash_str, 0x00,
sizeof hash_str );
620 memset( output, 0x00,
sizeof output );
627 hexify( hash_str, output,
sizeof output );
628 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
635 #ifdef POLARSSL_SHA512_C
636 void test_suite_sha512_hmac(
int trunc_size,
char *hex_key_string,
char *hex_src_string,
637 char *hex_hash_string)
639 unsigned char src_str[10000];
640 unsigned char key_str[10000];
641 unsigned char hash_str[129];
642 unsigned char output[64];
643 int key_len, src_len;
646 memset(src_str, 0x00,
sizeof src_str);
647 memset(key_str, 0x00,
sizeof key_str);
650 key_len =
unhexify( key_str, hex_key_string );
651 src_len =
unhexify( src_str, hex_src_string );
654 memset(hash_str, 0x00,
sizeof hash_str);
655 memset(output, 0x00,
sizeof output);
657 sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
659 hexify( hash_str, output,
sizeof output );
660 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
663 memset( hash_str, 0x00,
sizeof hash_str );
664 memset( output, 0x00,
sizeof output );
665 memset( &ctx, 0x00,
sizeof ctx );
674 hexify( hash_str, output,
sizeof output );
675 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
678 memset( hash_str, 0x00,
sizeof hash_str );
679 memset( output, 0x00,
sizeof output );
686 hexify( hash_str, output,
sizeof output );
687 TEST_ASSERT( strncmp( (
char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
702 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
704 #if defined(POLARSSL_SHA512_C)
710 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
712 #if defined(POLARSSL_SHA256_C)
718 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
720 #if defined(POLARSSL_SHA1_C)
737 #if defined(TEST_SUITE_ACTIVE)
738 if( strcmp( params[0],
"sha1_hmac" ) == 0 )
740 #ifdef POLARSSL_SHA1_C
743 char *param2 = params[2];
744 char *param3 = params[3];
745 char *param4 = params[4];
749 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
753 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
758 test_suite_sha1_hmac( param1, param2, param3, param4 );
765 if( strcmp( params[0],
"sha224_hmac" ) == 0 )
767 #ifdef POLARSSL_SHA256_C
770 char *param2 = params[2];
771 char *param3 = params[3];
772 char *param4 = params[4];
776 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
780 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
785 test_suite_sha224_hmac( param1, param2, param3, param4 );
792 if( strcmp( params[0],
"sha256_hmac" ) == 0 )
794 #ifdef POLARSSL_SHA256_C
797 char *param2 = params[2];
798 char *param3 = params[3];
799 char *param4 = params[4];
803 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
807 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
812 test_suite_sha256_hmac( param1, param2, param3, param4 );
819 if( strcmp( params[0],
"sha384_hmac" ) == 0 )
821 #ifdef POLARSSL_SHA512_C
824 char *param2 = params[2];
825 char *param3 = params[3];
826 char *param4 = params[4];
830 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
834 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
839 test_suite_sha384_hmac( param1, param2, param3, param4 );
846 if( strcmp( params[0],
"sha512_hmac" ) == 0 )
848 #ifdef POLARSSL_SHA512_C
851 char *param2 = params[2];
852 char *param3 = params[3];
853 char *param4 = params[4];
857 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
861 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
866 test_suite_sha512_hmac( param1, param2, param3, param4 );
875 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
889 ret = fgets( buf, len, f );
893 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
894 buf[strlen(buf) - 1] =
'\0';
895 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
896 buf[strlen(buf) - 1] =
'\0';
909 while( *p !=
'\0' && p < buf + len )
919 if( p + 1 < buf + len )
931 for( i = 0; i < cnt; i++ )
938 if( *p ==
'\\' && *(p + 1) ==
'n' )
943 else if( *p ==
'\\' && *(p + 1) ==
':' )
948 else if( *p ==
'\\' && *(p + 1) ==
'?' )
964 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
965 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_hmac_shax.data";
970 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
971 unsigned char alloc_buf[1000000];
975 file = fopen( filename,
"r" );
978 fprintf( stderr,
"Failed to open\n" );
982 while( !feof( file ) )
986 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
988 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
989 fprintf( stdout,
" " );
990 for( i = strlen( buf ) + 1; i < 67; i++ )
991 fprintf( stdout,
"." );
992 fprintf( stdout,
" " );
997 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1001 if( strcmp( params[0],
"depends_on" ) == 0 )
1003 for( i = 1; i < cnt; i++ )
1007 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1018 if( skip == 1 || ret == 3 )
1021 fprintf( stdout,
"----\n" );
1026 fprintf( stdout,
"PASS\n" );
1031 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1038 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1040 if( strlen(buf) != 0 )
1042 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1048 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1049 if( total_errors == 0 )
1050 fprintf( stdout,
"PASSED" );
1052 fprintf( stdout,
"FAILED" );
1054 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1055 total_tests - total_errors, total_tests, total_skipped );
1057 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1058 #if defined(POLARSSL_MEMORY_DEBUG)
1059 memory_buffer_alloc_status();
1064 return( total_errors != 0 );
void sha256_hmac_update(sha256_context *ctx, const unsigned char *input, size_t ilen)
SHA-256 HMAC process buffer.
void sha512_hmac_update(sha512_context *ctx, const unsigned char *input, size_t ilen)
SHA-512 HMAC process buffer.
void sha1_hmac_finish(sha1_context *ctx, unsigned char output[20])
SHA-1 HMAC final digest.
Memory allocation layer (Deprecated to platform layer)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
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, const char *test)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
void sha256_hmac_finish(sha256_context *ctx, unsigned char output[32])
SHA-256 HMAC final digest.
#define TEST_ASSERT(TEST)
void sha256_hmac_starts(sha256_context *ctx, const unsigned char *key, size_t keylen, int is224)
SHA-256 HMAC context setup.
void sha1_hmac_reset(sha1_context *ctx)
SHA-1 HMAC context reset.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
SHA-512 context structure.
void sha512_hmac_finish(sha512_context *ctx, unsigned char output[64])
SHA-512 HMAC final digest.
void sha1_free(sha1_context *ctx)
Clear SHA-1 context.
void sha1_hmac_starts(sha1_context *ctx, const unsigned char *key, size_t keylen)
SHA-1 HMAC context setup.
int get_line(FILE *f, char *buf, size_t len)
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 )
void sha256_hmac_reset(sha256_context *ctx)
SHA-256 HMAC context reset.
int dispatch_test(int cnt, char *params[50])
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
void sha256_init(sha256_context *ctx)
Initialize SHA-256 context.
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().
void sha256_free(sha256_context *ctx)
Clear SHA-256 context.
SHA-1 cryptographic hash function.
SHA-384 and SHA-512 cryptographic hash function.
void sha1_init(sha1_context *ctx)
Initialize SHA-1 context.
#define PUT_UINT32_BE(n, b, i)
void sha512_hmac_starts(sha512_context *ctx, const unsigned char *key, size_t keylen, int is384)
SHA-512 HMAC context setup.
void sha512_hmac_reset(sha512_context *ctx)
SHA-512 HMAC context reset.
SHA-256 context structure.
void sha512_free(sha512_context *ctx)
Clear SHA-512 context.
void sha1_hmac_update(sha1_context *ctx, const unsigned char *input, size_t ilen)
SHA-1 HMAC process buffer.
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 parse_arguments(char *buf, size_t len, char *params[50])
void sha512_init(sha512_context *ctx)
Initialize SHA-512 context.