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 );
394 #ifdef POLARSSL_CIPHER_MODE_CBC
395 if( strcmp( str,
"POLARSSL_ERR_AES_INVALID_INPUT_LENGTH" ) == 0 )
400 #endif // POLARSSL_CIPHER_MODE_CBC
401 if( strcmp( str,
"POLARSSL_ERR_AES_INVALID_KEY_LENGTH" ) == 0 )
408 printf(
"Expected integer for parameter and got: %s\n", str );
412 void test_suite_aes_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
413 char *hex_dst_string,
int setkey_result )
415 unsigned char key_str[100];
416 unsigned char src_str[100];
417 unsigned char dst_str[100];
418 unsigned char output[100];
422 memset(key_str, 0x00, 100);
423 memset(src_str, 0x00, 100);
424 memset(dst_str, 0x00, 100);
425 memset(output, 0x00, 100);
428 key_len =
unhexify( key_str, hex_key_string );
429 unhexify( src_str, hex_src_string );
432 if( setkey_result == 0 )
435 hexify( dst_str, output, 16 );
437 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
444 void test_suite_aes_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
445 char *hex_dst_string,
int setkey_result )
447 unsigned char key_str[100];
448 unsigned char src_str[100];
449 unsigned char dst_str[100];
450 unsigned char output[100];
454 memset(key_str, 0x00, 100);
455 memset(src_str, 0x00, 100);
456 memset(dst_str, 0x00, 100);
457 memset(output, 0x00, 100);
460 key_len =
unhexify( key_str, hex_key_string );
461 unhexify( src_str, hex_src_string );
464 if( setkey_result == 0 )
467 hexify( dst_str, output, 16 );
469 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
476 #ifdef POLARSSL_CIPHER_MODE_CBC
477 void test_suite_aes_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
478 char *hex_src_string,
char *hex_dst_string,
481 unsigned char key_str[100];
482 unsigned char iv_str[100];
483 unsigned char src_str[100];
484 unsigned char dst_str[100];
485 unsigned char output[100];
487 int key_len, data_len;
489 memset(key_str, 0x00, 100);
490 memset(iv_str, 0x00, 100);
491 memset(src_str, 0x00, 100);
492 memset(dst_str, 0x00, 100);
493 memset(output, 0x00, 100);
496 key_len =
unhexify( key_str, hex_key_string );
498 data_len =
unhexify( src_str, hex_src_string );
502 if( cbc_result == 0 )
504 hexify( dst_str, output, data_len );
506 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
514 #ifdef POLARSSL_CIPHER_MODE_CBC
515 void test_suite_aes_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
516 char *hex_src_string,
char *hex_dst_string,
519 unsigned char key_str[100];
520 unsigned char iv_str[100];
521 unsigned char src_str[100];
522 unsigned char dst_str[100];
523 unsigned char output[100];
525 int key_len, data_len;
527 memset(key_str, 0x00, 100);
528 memset(iv_str, 0x00, 100);
529 memset(src_str, 0x00, 100);
530 memset(dst_str, 0x00, 100);
531 memset(output, 0x00, 100);
534 key_len =
unhexify( key_str, hex_key_string );
536 data_len =
unhexify( src_str, hex_src_string );
542 hexify( dst_str, output, data_len );
544 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
552 #ifdef POLARSSL_CIPHER_MODE_CFB
553 void test_suite_aes_encrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
554 char *hex_src_string,
char *hex_dst_string )
556 unsigned char key_str[100];
557 unsigned char iv_str[100];
558 unsigned char src_str[100];
559 unsigned char dst_str[100];
560 unsigned char output[100];
562 size_t iv_offset = 0;
565 memset(key_str, 0x00, 100);
566 memset(iv_str, 0x00, 100);
567 memset(src_str, 0x00, 100);
568 memset(dst_str, 0x00, 100);
569 memset(output, 0x00, 100);
572 key_len =
unhexify( key_str, hex_key_string );
574 unhexify( src_str, hex_src_string );
578 hexify( dst_str, output, 16 );
580 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
587 #ifdef POLARSSL_CIPHER_MODE_CFB
588 void test_suite_aes_decrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
589 char *hex_src_string,
char *hex_dst_string )
591 unsigned char key_str[100];
592 unsigned char iv_str[100];
593 unsigned char src_str[100];
594 unsigned char dst_str[100];
595 unsigned char output[100];
597 size_t iv_offset = 0;
600 memset(key_str, 0x00, 100);
601 memset(iv_str, 0x00, 100);
602 memset(src_str, 0x00, 100);
603 memset(dst_str, 0x00, 100);
604 memset(output, 0x00, 100);
607 key_len =
unhexify( key_str, hex_key_string );
609 unhexify( src_str, hex_src_string );
613 hexify( dst_str, output, 16 );
615 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
622 #ifdef POLARSSL_CIPHER_MODE_CFB
623 void test_suite_aes_encrypt_cfb8(
char *hex_key_string,
char *hex_iv_string,
624 char *hex_src_string,
char *hex_dst_string )
626 unsigned char key_str[100];
627 unsigned char iv_str[100];
628 unsigned char src_str[100];
629 unsigned char dst_str[100];
630 unsigned char output[100];
632 int key_len, src_len;
634 memset(key_str, 0x00, 100);
635 memset(iv_str, 0x00, 100);
636 memset(src_str, 0x00, 100);
637 memset(dst_str, 0x00, 100);
638 memset(output, 0x00, 100);
641 key_len =
unhexify( key_str, hex_key_string );
643 src_len =
unhexify( src_str, hex_src_string );
647 hexify( dst_str, output, src_len );
649 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
656 #ifdef POLARSSL_CIPHER_MODE_CFB
657 void test_suite_aes_decrypt_cfb8(
char *hex_key_string,
char *hex_iv_string,
658 char *hex_src_string,
char *hex_dst_string )
660 unsigned char key_str[100];
661 unsigned char iv_str[100];
662 unsigned char src_str[100];
663 unsigned char dst_str[100];
664 unsigned char output[100];
666 int key_len, src_len;
668 memset(key_str, 0x00, 100);
669 memset(iv_str, 0x00, 100);
670 memset(src_str, 0x00, 100);
671 memset(dst_str, 0x00, 100);
672 memset(output, 0x00, 100);
675 key_len =
unhexify( key_str, hex_key_string );
677 src_len =
unhexify( src_str, hex_src_string );
681 hexify( dst_str, output, src_len );
683 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
690 #ifdef POLARSSL_SELF_TEST
691 void test_suite_aes_selftest()
709 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
711 #if defined(POLARSSL_SELF_TEST)
728 #if defined(TEST_SUITE_ACTIVE)
729 if( strcmp( params[0],
"aes_encrypt_ecb" ) == 0 )
732 char *param1 = params[1];
733 char *param2 = params[2];
734 char *param3 = params[3];
739 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
746 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
748 test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
754 if( strcmp( params[0],
"aes_decrypt_ecb" ) == 0 )
757 char *param1 = params[1];
758 char *param2 = params[2];
759 char *param3 = params[3];
764 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
771 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
773 test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
779 if( strcmp( params[0],
"aes_encrypt_cbc" ) == 0 )
781 #ifdef POLARSSL_CIPHER_MODE_CBC
783 char *param1 = params[1];
784 char *param2 = params[2];
785 char *param3 = params[3];
786 char *param4 = params[4];
791 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
799 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
801 test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
808 if( strcmp( params[0],
"aes_decrypt_cbc" ) == 0 )
810 #ifdef POLARSSL_CIPHER_MODE_CBC
812 char *param1 = params[1];
813 char *param2 = params[2];
814 char *param3 = params[3];
815 char *param4 = params[4];
820 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
828 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
830 test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
837 if( strcmp( params[0],
"aes_encrypt_cfb128" ) == 0 )
839 #ifdef POLARSSL_CIPHER_MODE_CFB
841 char *param1 = params[1];
842 char *param2 = params[2];
843 char *param3 = params[3];
844 char *param4 = params[4];
848 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
857 test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
864 if( strcmp( params[0],
"aes_decrypt_cfb128" ) == 0 )
866 #ifdef POLARSSL_CIPHER_MODE_CFB
868 char *param1 = params[1];
869 char *param2 = params[2];
870 char *param3 = params[3];
871 char *param4 = params[4];
875 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
884 test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
891 if( strcmp( params[0],
"aes_encrypt_cfb8" ) == 0 )
893 #ifdef POLARSSL_CIPHER_MODE_CFB
895 char *param1 = params[1];
896 char *param2 = params[2];
897 char *param3 = params[3];
898 char *param4 = params[4];
902 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
911 test_suite_aes_encrypt_cfb8( param1, param2, param3, param4 );
918 if( strcmp( params[0],
"aes_decrypt_cfb8" ) == 0 )
920 #ifdef POLARSSL_CIPHER_MODE_CFB
922 char *param1 = params[1];
923 char *param2 = params[2];
924 char *param3 = params[3];
925 char *param4 = params[4];
929 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
938 test_suite_aes_decrypt_cfb8( param1, param2, param3, param4 );
945 if( strcmp( params[0],
"aes_selftest" ) == 0 )
947 #ifdef POLARSSL_SELF_TEST
952 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
957 test_suite_aes_selftest( );
966 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
980 ret = fgets( buf, len, f );
984 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
985 buf[strlen(buf) - 1] =
'\0';
986 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
987 buf[strlen(buf) - 1] =
'\0';
1000 while( *p !=
'\0' && p < buf + len )
1010 if( p + 1 < buf + len )
1013 params[cnt++] = cur;
1022 for( i = 0; i < cnt; i++ )
1029 if( *p ==
'\\' && *(p + 1) ==
'n' )
1034 else if( *p ==
'\\' && *(p + 1) ==
':' )
1039 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1055 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1056 const char *filename =
"/home/users/builder/rpm/BUILD/polarssl-1.3.9/tests/suites/test_suite_aes.rest.data";
1061 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1062 unsigned char alloc_buf[1000000];
1066 file = fopen( filename,
"r" );
1069 fprintf( stderr,
"Failed to open\n" );
1073 while( !feof( file ) )
1077 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1079 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1080 fprintf( stdout,
" " );
1081 for( i = strlen( buf ) + 1; i < 67; i++ )
1082 fprintf( stdout,
"." );
1083 fprintf( stdout,
" " );
1088 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1092 if( strcmp( params[0],
"depends_on" ) == 0 )
1094 for( i = 1; i < cnt; i++ )
1098 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1109 if( skip == 1 || ret == 3 )
1112 fprintf( stdout,
"----\n" );
1117 fprintf( stdout,
"PASS\n" );
1122 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1129 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1131 if( strlen(buf) != 0 )
1133 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1139 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1140 if( total_errors == 0 )
1141 fprintf( stdout,
"PASSED" );
1143 fprintf( stdout,
"FAILED" );
1145 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1146 total_tests - total_errors, total_tests, total_skipped );
1148 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1149 #if defined(POLARSSL_MEMORY_DEBUG)
1150 memory_buffer_alloc_status();
1155 return( total_errors != 0 );