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()
697 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
699 #if defined(POLARSSL_CIPHER_MODE_CFB)
716 #if defined(TEST_SUITE_ACTIVE)
717 if( strcmp( params[0],
"aes_encrypt_ecb" ) == 0 )
720 char *param1 = params[1];
721 char *param2 = params[2];
722 char *param3 = params[3];
727 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
734 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
736 test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
742 if( strcmp( params[0],
"aes_decrypt_ecb" ) == 0 )
745 char *param1 = params[1];
746 char *param2 = params[2];
747 char *param3 = params[3];
752 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
759 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
761 test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
767 if( strcmp( params[0],
"aes_encrypt_cbc" ) == 0 )
769 #ifdef POLARSSL_CIPHER_MODE_CBC
771 char *param1 = params[1];
772 char *param2 = params[2];
773 char *param3 = params[3];
774 char *param4 = params[4];
779 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
787 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
789 test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
796 if( strcmp( params[0],
"aes_decrypt_cbc" ) == 0 )
798 #ifdef POLARSSL_CIPHER_MODE_CBC
800 char *param1 = params[1];
801 char *param2 = params[2];
802 char *param3 = params[3];
803 char *param4 = params[4];
808 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
816 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
818 test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
825 if( strcmp( params[0],
"aes_encrypt_cfb128" ) == 0 )
827 #ifdef POLARSSL_CIPHER_MODE_CFB
829 char *param1 = params[1];
830 char *param2 = params[2];
831 char *param3 = params[3];
832 char *param4 = params[4];
836 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
845 test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
852 if( strcmp( params[0],
"aes_decrypt_cfb128" ) == 0 )
854 #ifdef POLARSSL_CIPHER_MODE_CFB
856 char *param1 = params[1];
857 char *param2 = params[2];
858 char *param3 = params[3];
859 char *param4 = params[4];
863 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
872 test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
879 if( strcmp( params[0],
"aes_encrypt_cfb8" ) == 0 )
881 #ifdef POLARSSL_CIPHER_MODE_CFB
883 char *param1 = params[1];
884 char *param2 = params[2];
885 char *param3 = params[3];
886 char *param4 = params[4];
890 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
899 test_suite_aes_encrypt_cfb8( param1, param2, param3, param4 );
906 if( strcmp( params[0],
"aes_decrypt_cfb8" ) == 0 )
908 #ifdef POLARSSL_CIPHER_MODE_CFB
910 char *param1 = params[1];
911 char *param2 = params[2];
912 char *param3 = params[3];
913 char *param4 = params[4];
917 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
926 test_suite_aes_decrypt_cfb8( param1, param2, param3, param4 );
933 if( strcmp( params[0],
"aes_selftest" ) == 0 )
935 #ifdef POLARSSL_SELF_TEST
940 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
945 test_suite_aes_selftest( );
954 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
968 ret = fgets( buf, len, f );
972 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
973 buf[strlen(buf) - 1] =
'\0';
974 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
975 buf[strlen(buf) - 1] =
'\0';
988 while( *p !=
'\0' && p < buf + len )
998 if( p + 1 < buf + len )
1001 params[cnt++] = cur;
1010 for( i = 0; i < cnt; i++ )
1017 if( *p ==
'\\' && *(p + 1) ==
'n' )
1022 else if( *p ==
'\\' && *(p + 1) ==
':' )
1027 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1043 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1044 const char *filename =
"/tmp/B.9R4AfU/BUILD/polarssl-1.3.9/tests/suites/test_suite_aes.cfb.data";
1049 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1050 unsigned char alloc_buf[1000000];
1054 file = fopen( filename,
"r" );
1057 fprintf( stderr,
"Failed to open\n" );
1061 while( !feof( file ) )
1065 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1067 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1068 fprintf( stdout,
" " );
1069 for( i = strlen( buf ) + 1; i < 67; i++ )
1070 fprintf( stdout,
"." );
1071 fprintf( stdout,
" " );
1076 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1080 if( strcmp( params[0],
"depends_on" ) == 0 )
1082 for( i = 1; i < cnt; i++ )
1086 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1097 if( skip == 1 || ret == 3 )
1100 fprintf( stdout,
"----\n" );
1105 fprintf( stdout,
"PASS\n" );
1110 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1117 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1119 if( strlen(buf) != 0 )
1121 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1127 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1128 if( total_errors == 0 )
1129 fprintf( stdout,
"PASSED" );
1131 fprintf( stdout,
"FAILED" );
1133 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1134 total_tests - total_errors, total_tests, total_skipped );
1136 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1137 #if defined(POLARSSL_MEMORY_DEBUG)
1138 memory_buffer_alloc_status();
1143 return( total_errors != 0 );