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
316 #ifdef POLARSSL_AES_C
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 );
396 printf(
"Expected integer for parameter and got: %s\n", str );
400 void test_suite_aes_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
401 char *hex_dst_string,
int setkey_result )
403 unsigned char key_str[100];
404 unsigned char src_str[100];
405 unsigned char dst_str[100];
406 unsigned char output[100];
410 memset(key_str, 0x00, 100);
411 memset(src_str, 0x00, 100);
412 memset(dst_str, 0x00, 100);
413 memset(output, 0x00, 100);
416 key_len =
unhexify( key_str, hex_key_string );
417 unhexify( src_str, hex_src_string );
420 if( setkey_result == 0 )
423 hexify( dst_str, output, 16 );
425 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
432 void test_suite_aes_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
433 char *hex_dst_string,
int setkey_result )
435 unsigned char key_str[100];
436 unsigned char src_str[100];
437 unsigned char dst_str[100];
438 unsigned char output[100];
442 memset(key_str, 0x00, 100);
443 memset(src_str, 0x00, 100);
444 memset(dst_str, 0x00, 100);
445 memset(output, 0x00, 100);
448 key_len =
unhexify( key_str, hex_key_string );
449 unhexify( src_str, hex_src_string );
452 if( setkey_result == 0 )
455 hexify( dst_str, output, 16 );
457 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
464 #ifdef POLARSSL_CIPHER_MODE_CBC
465 void test_suite_aes_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
466 char *hex_src_string,
char *hex_dst_string,
469 unsigned char key_str[100];
470 unsigned char iv_str[100];
471 unsigned char src_str[100];
472 unsigned char dst_str[100];
473 unsigned char output[100];
475 int key_len, data_len;
477 memset(key_str, 0x00, 100);
478 memset(iv_str, 0x00, 100);
479 memset(src_str, 0x00, 100);
480 memset(dst_str, 0x00, 100);
481 memset(output, 0x00, 100);
484 key_len =
unhexify( key_str, hex_key_string );
486 data_len =
unhexify( src_str, hex_src_string );
490 if( cbc_result == 0 )
492 hexify( dst_str, output, data_len );
494 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
502 #ifdef POLARSSL_CIPHER_MODE_CBC
503 void test_suite_aes_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
504 char *hex_src_string,
char *hex_dst_string,
507 unsigned char key_str[100];
508 unsigned char iv_str[100];
509 unsigned char src_str[100];
510 unsigned char dst_str[100];
511 unsigned char output[100];
513 int key_len, data_len;
515 memset(key_str, 0x00, 100);
516 memset(iv_str, 0x00, 100);
517 memset(src_str, 0x00, 100);
518 memset(dst_str, 0x00, 100);
519 memset(output, 0x00, 100);
522 key_len =
unhexify( key_str, hex_key_string );
524 data_len =
unhexify( src_str, hex_src_string );
530 hexify( dst_str, output, data_len );
532 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
540 #ifdef POLARSSL_CIPHER_MODE_CFB
541 void test_suite_aes_encrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
542 char *hex_src_string,
char *hex_dst_string )
544 unsigned char key_str[100];
545 unsigned char iv_str[100];
546 unsigned char src_str[100];
547 unsigned char dst_str[100];
548 unsigned char output[100];
550 size_t iv_offset = 0;
553 memset(key_str, 0x00, 100);
554 memset(iv_str, 0x00, 100);
555 memset(src_str, 0x00, 100);
556 memset(dst_str, 0x00, 100);
557 memset(output, 0x00, 100);
560 key_len =
unhexify( key_str, hex_key_string );
562 unhexify( src_str, hex_src_string );
566 hexify( dst_str, output, 16 );
568 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
575 #ifdef POLARSSL_CIPHER_MODE_CFB
576 void test_suite_aes_decrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
577 char *hex_src_string,
char *hex_dst_string )
579 unsigned char key_str[100];
580 unsigned char iv_str[100];
581 unsigned char src_str[100];
582 unsigned char dst_str[100];
583 unsigned char output[100];
585 size_t iv_offset = 0;
588 memset(key_str, 0x00, 100);
589 memset(iv_str, 0x00, 100);
590 memset(src_str, 0x00, 100);
591 memset(dst_str, 0x00, 100);
592 memset(output, 0x00, 100);
595 key_len =
unhexify( key_str, hex_key_string );
597 unhexify( src_str, hex_src_string );
601 hexify( dst_str, output, 16 );
603 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
610 #ifdef POLARSSL_CIPHER_MODE_CFB
611 void test_suite_aes_encrypt_cfb8(
char *hex_key_string,
char *hex_iv_string,
612 char *hex_src_string,
char *hex_dst_string )
614 unsigned char key_str[100];
615 unsigned char iv_str[100];
616 unsigned char src_str[100];
617 unsigned char dst_str[100];
618 unsigned char output[100];
620 int key_len, src_len;
622 memset(key_str, 0x00, 100);
623 memset(iv_str, 0x00, 100);
624 memset(src_str, 0x00, 100);
625 memset(dst_str, 0x00, 100);
626 memset(output, 0x00, 100);
629 key_len =
unhexify( key_str, hex_key_string );
631 src_len =
unhexify( src_str, hex_src_string );
635 hexify( dst_str, output, src_len );
637 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
644 #ifdef POLARSSL_CIPHER_MODE_CFB
645 void test_suite_aes_decrypt_cfb8(
char *hex_key_string,
char *hex_iv_string,
646 char *hex_src_string,
char *hex_dst_string )
648 unsigned char key_str[100];
649 unsigned char iv_str[100];
650 unsigned char src_str[100];
651 unsigned char dst_str[100];
652 unsigned char output[100];
654 int key_len, src_len;
656 memset(key_str, 0x00, 100);
657 memset(iv_str, 0x00, 100);
658 memset(src_str, 0x00, 100);
659 memset(dst_str, 0x00, 100);
660 memset(output, 0x00, 100);
663 key_len =
unhexify( key_str, hex_key_string );
665 src_len =
unhexify( src_str, hex_src_string );
669 hexify( dst_str, output, src_len );
671 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
678 #ifdef POLARSSL_SELF_TEST
679 void test_suite_aes_selftest()
708 #if defined(TEST_SUITE_ACTIVE)
709 if( strcmp( params[0],
"aes_encrypt_ecb" ) == 0 )
712 char *param1 = params[1];
713 char *param2 = params[2];
714 char *param3 = params[3];
719 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
726 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
728 test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
734 if( strcmp( params[0],
"aes_decrypt_ecb" ) == 0 )
737 char *param1 = params[1];
738 char *param2 = params[2];
739 char *param3 = params[3];
744 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
751 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
753 test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
759 if( strcmp( params[0],
"aes_encrypt_cbc" ) == 0 )
761 #ifdef POLARSSL_CIPHER_MODE_CBC
763 char *param1 = params[1];
764 char *param2 = params[2];
765 char *param3 = params[3];
766 char *param4 = params[4];
771 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
779 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
781 test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
788 if( strcmp( params[0],
"aes_decrypt_cbc" ) == 0 )
790 #ifdef POLARSSL_CIPHER_MODE_CBC
792 char *param1 = params[1];
793 char *param2 = params[2];
794 char *param3 = params[3];
795 char *param4 = params[4];
800 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
808 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
810 test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
817 if( strcmp( params[0],
"aes_encrypt_cfb128" ) == 0 )
819 #ifdef POLARSSL_CIPHER_MODE_CFB
821 char *param1 = params[1];
822 char *param2 = params[2];
823 char *param3 = params[3];
824 char *param4 = params[4];
828 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
837 test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
844 if( strcmp( params[0],
"aes_decrypt_cfb128" ) == 0 )
846 #ifdef POLARSSL_CIPHER_MODE_CFB
848 char *param1 = params[1];
849 char *param2 = params[2];
850 char *param3 = params[3];
851 char *param4 = params[4];
855 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
864 test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
871 if( strcmp( params[0],
"aes_encrypt_cfb8" ) == 0 )
873 #ifdef POLARSSL_CIPHER_MODE_CFB
875 char *param1 = params[1];
876 char *param2 = params[2];
877 char *param3 = params[3];
878 char *param4 = params[4];
882 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
891 test_suite_aes_encrypt_cfb8( param1, param2, param3, param4 );
898 if( strcmp( params[0],
"aes_decrypt_cfb8" ) == 0 )
900 #ifdef POLARSSL_CIPHER_MODE_CFB
902 char *param1 = params[1];
903 char *param2 = params[2];
904 char *param3 = params[3];
905 char *param4 = params[4];
909 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
918 test_suite_aes_decrypt_cfb8( param1, param2, param3, param4 );
925 if( strcmp( params[0],
"aes_selftest" ) == 0 )
927 #ifdef POLARSSL_SELF_TEST
932 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
937 test_suite_aes_selftest( );
946 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
960 ret = fgets( buf, len, f );
964 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
965 buf[strlen(buf) - 1] =
'\0';
966 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
967 buf[strlen(buf) - 1] =
'\0';
980 while( *p !=
'\0' && p < buf + len )
990 if( p + 1 < buf + len )
1002 for( i = 0; i < cnt; i++ )
1009 if( *p ==
'\\' && *(p + 1) ==
'n' )
1014 else if( *p ==
'\\' && *(p + 1) ==
':' )
1019 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1035 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1036 const char *filename =
"/tmp/B.9tfrh6rj/BUILD/polarssl-1.3.9/tests/suites/test_suite_aes.ecb.data";
1041 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1042 unsigned char alloc_buf[1000000];
1046 file = fopen( filename,
"r" );
1049 fprintf( stderr,
"Failed to open\n" );
1053 while( !feof( file ) )
1057 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1059 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1060 fprintf( stdout,
" " );
1061 for( i = strlen( buf ) + 1; i < 67; i++ )
1062 fprintf( stdout,
"." );
1063 fprintf( stdout,
" " );
1068 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1072 if( strcmp( params[0],
"depends_on" ) == 0 )
1074 for( i = 1; i < cnt; i++ )
1078 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1089 if( skip == 1 || ret == 3 )
1092 fprintf( stdout,
"----\n" );
1097 fprintf( stdout,
"PASS\n" );
1102 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1109 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1111 if( strlen(buf) != 0 )
1113 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1119 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1120 if( total_errors == 0 )
1121 fprintf( stdout,
"PASSED" );
1123 fprintf( stdout,
"FAILED" );
1125 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1126 total_tests - total_errors, total_tests, total_skipped );
1128 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1129 #if defined(POLARSSL_MEMORY_DEBUG)
1130 memory_buffer_alloc_status();
1135 return( total_errors != 0 );
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
Memory allocation layer (Deprecated to platform layer)
int get_line(FILE *f, char *buf, size_t len)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int aes_crypt_cfb128(aes_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CFB128 buffer encryption/decryption.
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 unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
Configuration options (set of defines)
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
static int test_assert(int correct, const char *test)
#define PUT_UINT32_BE(n, b, i)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
int aes_crypt_cbc(aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
int aes_self_test(int verbose)
Checkup routine.
int aes_crypt_cfb8(aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CFB8 buffer encryption/decryption.
#define TEST_ASSERT(TEST)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
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.
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
int parse_arguments(char *buf, size_t len, char *params[50])
void aes_free(aes_context *ctx)
Clear AES context.
int verify_int(char *str, int *value)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int aes_setkey_enc(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (encryption)
int aes_crypt_ecb(aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
AES-ECB block encryption/decryption.
void aes_init(aes_context *ctx)
Initialize AES context.