9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
288 #ifdef POLARSSL_AES_C
290 #define TEST_SUITE_ACTIVE
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
366 printf(
"Expected integer for parameter and got: %s\n", str );
370 void test_suite_aes_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
371 char *hex_dst_string,
int setkey_result )
373 unsigned char key_str[100];
374 unsigned char src_str[100];
375 unsigned char dst_str[100];
376 unsigned char output[100];
380 memset(key_str, 0x00, 100);
381 memset(src_str, 0x00, 100);
382 memset(dst_str, 0x00, 100);
383 memset(output, 0x00, 100);
385 key_len =
unhexify( key_str, hex_key_string );
386 unhexify( src_str, hex_src_string );
389 if( setkey_result == 0 )
392 hexify( dst_str, output, 16 );
394 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
398 void test_suite_aes_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
399 char *hex_dst_string,
int setkey_result )
401 unsigned char key_str[100];
402 unsigned char src_str[100];
403 unsigned char dst_str[100];
404 unsigned char output[100];
408 memset(key_str, 0x00, 100);
409 memset(src_str, 0x00, 100);
410 memset(dst_str, 0x00, 100);
411 memset(output, 0x00, 100);
413 key_len =
unhexify( key_str, hex_key_string );
414 unhexify( src_str, hex_src_string );
417 if( setkey_result == 0 )
420 hexify( dst_str, output, 16 );
422 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
426 #ifdef POLARSSL_CIPHER_MODE_CBC
427 void test_suite_aes_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
428 char *hex_src_string,
char *hex_dst_string,
431 unsigned char key_str[100];
432 unsigned char iv_str[100];
433 unsigned char src_str[100];
434 unsigned char dst_str[100];
435 unsigned char output[100];
437 int key_len, data_len;
439 memset(key_str, 0x00, 100);
440 memset(iv_str, 0x00, 100);
441 memset(src_str, 0x00, 100);
442 memset(dst_str, 0x00, 100);
443 memset(output, 0x00, 100);
445 key_len =
unhexify( key_str, hex_key_string );
447 data_len =
unhexify( src_str, hex_src_string );
451 if( cbc_result == 0 )
453 hexify( dst_str, output, data_len );
455 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
460 #ifdef POLARSSL_CIPHER_MODE_CBC
461 void test_suite_aes_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
462 char *hex_src_string,
char *hex_dst_string,
465 unsigned char key_str[100];
466 unsigned char iv_str[100];
467 unsigned char src_str[100];
468 unsigned char dst_str[100];
469 unsigned char output[100];
471 int key_len, data_len;
473 memset(key_str, 0x00, 100);
474 memset(iv_str, 0x00, 100);
475 memset(src_str, 0x00, 100);
476 memset(dst_str, 0x00, 100);
477 memset(output, 0x00, 100);
479 key_len =
unhexify( key_str, hex_key_string );
481 data_len =
unhexify( src_str, hex_src_string );
487 hexify( dst_str, output, data_len );
489 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
494 #ifdef POLARSSL_CIPHER_MODE_CFB
495 void test_suite_aes_encrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
496 char *hex_src_string,
char *hex_dst_string )
498 unsigned char key_str[100];
499 unsigned char iv_str[100];
500 unsigned char src_str[100];
501 unsigned char dst_str[100];
502 unsigned char output[100];
504 size_t iv_offset = 0;
507 memset(key_str, 0x00, 100);
508 memset(iv_str, 0x00, 100);
509 memset(src_str, 0x00, 100);
510 memset(dst_str, 0x00, 100);
511 memset(output, 0x00, 100);
513 key_len =
unhexify( key_str, hex_key_string );
515 unhexify( src_str, hex_src_string );
519 hexify( dst_str, output, 16 );
521 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
525 #ifdef POLARSSL_CIPHER_MODE_CFB
526 void test_suite_aes_decrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
527 char *hex_src_string,
char *hex_dst_string )
529 unsigned char key_str[100];
530 unsigned char iv_str[100];
531 unsigned char src_str[100];
532 unsigned char dst_str[100];
533 unsigned char output[100];
535 size_t iv_offset = 0;
538 memset(key_str, 0x00, 100);
539 memset(iv_str, 0x00, 100);
540 memset(src_str, 0x00, 100);
541 memset(dst_str, 0x00, 100);
542 memset(output, 0x00, 100);
544 key_len =
unhexify( key_str, hex_key_string );
546 unhexify( src_str, hex_src_string );
550 hexify( dst_str, output, 16 );
552 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
556 #ifdef POLARSSL_SELF_TEST
557 void test_suite_aes_selftest()
583 #if defined(TEST_SUITE_ACTIVE)
584 if( strcmp( params[0],
"aes_encrypt_ecb" ) == 0 )
587 char *param1 = params[1];
588 char *param2 = params[2];
589 char *param3 = params[3];
594 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
601 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
603 test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
609 if( strcmp( params[0],
"aes_decrypt_ecb" ) == 0 )
612 char *param1 = params[1];
613 char *param2 = params[2];
614 char *param3 = params[3];
619 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
626 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
628 test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
634 if( strcmp( params[0],
"aes_encrypt_cbc" ) == 0 )
636 #ifdef POLARSSL_CIPHER_MODE_CBC
638 char *param1 = params[1];
639 char *param2 = params[2];
640 char *param3 = params[3];
641 char *param4 = params[4];
646 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
654 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
656 test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
663 if( strcmp( params[0],
"aes_decrypt_cbc" ) == 0 )
665 #ifdef POLARSSL_CIPHER_MODE_CBC
667 char *param1 = params[1];
668 char *param2 = params[2];
669 char *param3 = params[3];
670 char *param4 = params[4];
675 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
683 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
685 test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
692 if( strcmp( params[0],
"aes_encrypt_cfb128" ) == 0 )
694 #ifdef POLARSSL_CIPHER_MODE_CFB
696 char *param1 = params[1];
697 char *param2 = params[2];
698 char *param3 = params[3];
699 char *param4 = params[4];
703 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
712 test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
719 if( strcmp( params[0],
"aes_decrypt_cfb128" ) == 0 )
721 #ifdef POLARSSL_CIPHER_MODE_CFB
723 char *param1 = params[1];
724 char *param2 = params[2];
725 char *param3 = params[3];
726 char *param4 = params[4];
730 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
739 test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
746 if( strcmp( params[0],
"aes_selftest" ) == 0 )
748 #ifdef POLARSSL_SELF_TEST
753 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
758 test_suite_aes_selftest( );
767 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
781 ret = fgets( buf, len, f );
785 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
786 buf[strlen(buf) - 1] =
'\0';
787 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
788 buf[strlen(buf) - 1] =
'\0';
801 while( *p !=
'\0' && p < buf + len )
811 if( p + 1 < buf + len )
823 for( i = 0; i < cnt; i++ )
830 if( *p ==
'\\' && *(p + 1) ==
'n' )
835 else if( *p ==
'\\' && *(p + 1) ==
':' )
840 else if( *p ==
'\\' && *(p + 1) ==
'?' )
856 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
857 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_aes.cbc.data";
862 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
863 unsigned char alloc_buf[1000000];
864 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
867 file = fopen( filename,
"r" );
870 fprintf( stderr,
"Failed to open\n" );
874 while( !feof( file ) )
878 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
880 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
881 fprintf( stdout,
" " );
882 for( i = strlen( buf ) + 1; i < 67; i++ )
883 fprintf( stdout,
"." );
884 fprintf( stdout,
" " );
889 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
893 if( strcmp( params[0],
"depends_on" ) == 0 )
895 for( i = 1; i < cnt; i++ )
899 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
910 if( skip == 1 || ret == 3 )
913 fprintf( stdout,
"----\n" );
918 fprintf( stdout,
"PASS\n" );
923 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
930 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
932 if( strlen(buf) != 0 )
934 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
940 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
941 if( total_errors == 0 )
942 fprintf( stdout,
"PASSED" );
944 fprintf( stdout,
"FAILED" );
946 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
947 total_tests - total_errors, total_tests, total_skipped );
949 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
950 #if defined(POLARSSL_MEMORY_DEBUG)
951 memory_buffer_alloc_status();
953 memory_buffer_alloc_free();
956 return( total_errors != 0 );