9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
13 #if defined(WANT_NOT_RND_MPI)
14 #if defined(POLARSSL_BIGNUM_C)
17 #error "not_rnd_mpi() need bignum.c"
23 typedef UINT32 uint32_t;
36 #define GET_UINT32_BE(n,b,i) \
38 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
39 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
40 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
41 | ( (uint32_t) (b)[(i) + 3] ); \
46 #define PUT_UINT32_BE(n,b,i) \
48 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
49 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
50 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
51 (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 static int unhexify(
unsigned char *obuf,
const char *ibuf)
58 int len = strlen(ibuf) / 2;
59 assert(!(strlen(ibuf) %1));
64 if( c >=
'0' && c <=
'9' )
66 else if( c >=
'a' && c <=
'f' )
68 else if( c >=
'A' && c <=
'F' )
74 if( c2 >=
'0' && c2 <=
'9' )
76 else if( c2 >=
'a' && c2 <=
'f' )
78 else if( c2 >=
'A' && c2 <=
'F' )
83 *obuf++ = ( c << 4 ) | c2;
89 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
101 *obuf++ =
'a' + h - 10;
106 *obuf++ =
'a' + l - 10;
122 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
126 if( rng_state != NULL )
129 for( i = 0; i < len; ++i )
140 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
142 if( rng_state != NULL )
145 memset( output, 0, len );
172 if( rng_state == NULL )
181 memcpy( output, info->
buf, use_len );
182 info->
buf += use_len;
186 if( len - use_len > 0 )
187 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
216 uint32_t i, *k, sum, delta=0x9E3779B9;
217 unsigned char result[4];
219 if( rng_state == NULL )
226 size_t use_len = ( len > 4 ) ? 4 : len;
229 for( i = 0; i < 32; i++ )
231 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
233 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
237 memcpy( output, result, use_len );
244 #if defined(WANT_NOT_RND_MPI)
253 #define ciL (sizeof(t_uint))
254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
255 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
257 char *str = (
char *) in;
266 X.
n = CHARS_TO_LIMBS( len );
272 assert( strlen( str ) / 2 == len );
284 #ifdef POLARSSL_AES_C
286 #define TEST_SUITE_ACTIVE
294 if( test_errors == 1 )
295 printf(
"FAILED\n" );
296 printf(
" %s\n", test );
301 #define TEST_ASSERT( TEST ) \
302 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
303 if( test_errors) return; \
308 if( (*str)[0] !=
'"' ||
309 (*str)[strlen( *str ) - 1] !=
'"' )
311 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
316 (*str)[strlen( *str ) - 1] =
'\0';
328 for( i = 0; i < strlen( str ); i++ )
330 if( i == 0 && str[i] ==
'-' )
336 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
337 str[i - 1] ==
'0' && str[i] ==
'x' )
343 if( str[i] <
'0' || str[i] >
'9' )
353 *value = strtol( str, NULL, 16 );
355 *value = strtol( str, NULL, 10 );
360 if( strcmp( str,
"POLARSSL_ERR_AES_INVALID_KEY_LENGTH" ) == 0 )
365 #ifdef POLARSSL_CIPHER_MODE_CBC
366 if( strcmp( str,
"POLARSSL_ERR_AES_INVALID_INPUT_LENGTH" ) == 0 )
371 #endif // POLARSSL_CIPHER_MODE_CBC
374 printf(
"Expected integer for parameter and got: %s\n", str );
378 void test_suite_aes_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
379 char *hex_dst_string,
int setkey_result )
381 unsigned char key_str[100];
382 unsigned char src_str[100];
383 unsigned char dst_str[100];
384 unsigned char output[100];
388 memset(key_str, 0x00, 100);
389 memset(src_str, 0x00, 100);
390 memset(dst_str, 0x00, 100);
391 memset(output, 0x00, 100);
393 key_len =
unhexify( key_str, hex_key_string );
394 unhexify( src_str, hex_src_string );
397 if( setkey_result == 0 )
400 hexify( dst_str, output, 16 );
402 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
406 void test_suite_aes_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
407 char *hex_dst_string,
int setkey_result )
409 unsigned char key_str[100];
410 unsigned char src_str[100];
411 unsigned char dst_str[100];
412 unsigned char output[100];
416 memset(key_str, 0x00, 100);
417 memset(src_str, 0x00, 100);
418 memset(dst_str, 0x00, 100);
419 memset(output, 0x00, 100);
421 key_len =
unhexify( key_str, hex_key_string );
422 unhexify( src_str, hex_src_string );
425 if( setkey_result == 0 )
428 hexify( dst_str, output, 16 );
430 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
434 #ifdef POLARSSL_CIPHER_MODE_CBC
435 void test_suite_aes_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
436 char *hex_src_string,
char *hex_dst_string,
439 unsigned char key_str[100];
440 unsigned char iv_str[100];
441 unsigned char src_str[100];
442 unsigned char dst_str[100];
443 unsigned char output[100];
445 int key_len, data_len;
447 memset(key_str, 0x00, 100);
448 memset(iv_str, 0x00, 100);
449 memset(src_str, 0x00, 100);
450 memset(dst_str, 0x00, 100);
451 memset(output, 0x00, 100);
453 key_len =
unhexify( key_str, hex_key_string );
455 data_len =
unhexify( src_str, hex_src_string );
459 if( cbc_result == 0 )
461 hexify( dst_str, output, data_len );
463 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
468 #ifdef POLARSSL_CIPHER_MODE_CBC
469 void test_suite_aes_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
470 char *hex_src_string,
char *hex_dst_string,
473 unsigned char key_str[100];
474 unsigned char iv_str[100];
475 unsigned char src_str[100];
476 unsigned char dst_str[100];
477 unsigned char output[100];
479 int key_len, data_len;
481 memset(key_str, 0x00, 100);
482 memset(iv_str, 0x00, 100);
483 memset(src_str, 0x00, 100);
484 memset(dst_str, 0x00, 100);
485 memset(output, 0x00, 100);
487 key_len =
unhexify( key_str, hex_key_string );
489 data_len =
unhexify( src_str, hex_src_string );
495 hexify( dst_str, output, data_len );
497 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
502 #ifdef POLARSSL_CIPHER_MODE_CFB
503 void test_suite_aes_encrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
504 char *hex_src_string,
char *hex_dst_string )
506 unsigned char key_str[100];
507 unsigned char iv_str[100];
508 unsigned char src_str[100];
509 unsigned char dst_str[100];
510 unsigned char output[100];
512 size_t iv_offset = 0;
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);
521 key_len =
unhexify( key_str, hex_key_string );
523 unhexify( src_str, hex_src_string );
527 hexify( dst_str, output, 16 );
529 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
533 #ifdef POLARSSL_CIPHER_MODE_CFB
534 void test_suite_aes_decrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
535 char *hex_src_string,
char *hex_dst_string )
537 unsigned char key_str[100];
538 unsigned char iv_str[100];
539 unsigned char src_str[100];
540 unsigned char dst_str[100];
541 unsigned char output[100];
543 size_t iv_offset = 0;
546 memset(key_str, 0x00, 100);
547 memset(iv_str, 0x00, 100);
548 memset(src_str, 0x00, 100);
549 memset(dst_str, 0x00, 100);
550 memset(output, 0x00, 100);
552 key_len =
unhexify( key_str, hex_key_string );
554 unhexify( src_str, hex_src_string );
558 hexify( dst_str, output, 16 );
560 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
564 #ifdef POLARSSL_SELF_TEST
565 void test_suite_aes_selftest()
580 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
582 #if defined(POLARSSL_SELF_TEST)
599 #if defined(TEST_SUITE_ACTIVE)
600 if( strcmp( params[0],
"aes_encrypt_ecb" ) == 0 )
603 char *param1 = params[1];
604 char *param2 = params[2];
605 char *param3 = params[3];
610 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
617 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
619 test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
625 if( strcmp( params[0],
"aes_decrypt_ecb" ) == 0 )
628 char *param1 = params[1];
629 char *param2 = params[2];
630 char *param3 = params[3];
635 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
642 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
644 test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
650 if( strcmp( params[0],
"aes_encrypt_cbc" ) == 0 )
652 #ifdef POLARSSL_CIPHER_MODE_CBC
654 char *param1 = params[1];
655 char *param2 = params[2];
656 char *param3 = params[3];
657 char *param4 = params[4];
662 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
670 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
672 test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
679 if( strcmp( params[0],
"aes_decrypt_cbc" ) == 0 )
681 #ifdef POLARSSL_CIPHER_MODE_CBC
683 char *param1 = params[1];
684 char *param2 = params[2];
685 char *param3 = params[3];
686 char *param4 = params[4];
691 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
699 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
701 test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
708 if( strcmp( params[0],
"aes_encrypt_cfb128" ) == 0 )
710 #ifdef POLARSSL_CIPHER_MODE_CFB
712 char *param1 = params[1];
713 char *param2 = params[2];
714 char *param3 = params[3];
715 char *param4 = params[4];
719 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
728 test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
735 if( strcmp( params[0],
"aes_decrypt_cfb128" ) == 0 )
737 #ifdef POLARSSL_CIPHER_MODE_CFB
739 char *param1 = params[1];
740 char *param2 = params[2];
741 char *param3 = params[3];
742 char *param4 = params[4];
746 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
755 test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
762 if( strcmp( params[0],
"aes_selftest" ) == 0 )
764 #ifdef POLARSSL_SELF_TEST
769 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
774 test_suite_aes_selftest( );
783 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
797 ret = fgets( buf, len, f );
801 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
802 buf[strlen(buf) - 1] =
'\0';
803 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
804 buf[strlen(buf) - 1] =
'\0';
817 while( *p !=
'\0' && p < buf + len )
827 if( p + 1 < buf + len )
839 for( i = 0; i < cnt; i++ )
846 if( *p ==
'\\' && *(p + 1) ==
'n' )
851 else if( *p ==
'\\' && *(p + 1) ==
':' )
856 else if( *p ==
'\\' && *(p + 1) ==
'?' )
872 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
873 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_aes.rest.data";
878 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
879 unsigned char alloc_buf[1000000];
880 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
883 file = fopen( filename,
"r" );
886 fprintf( stderr,
"Failed to open\n" );
890 while( !feof( file ) )
894 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
896 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
897 fprintf( stdout,
" " );
898 for( i = strlen( buf ) + 1; i < 67; i++ )
899 fprintf( stdout,
"." );
900 fprintf( stdout,
" " );
905 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
909 if( strcmp( params[0],
"depends_on" ) == 0 )
911 for( i = 1; i < cnt; i++ )
915 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
926 if( skip == 1 || ret == 3 )
929 fprintf( stdout,
"----\n" );
932 else if( ret == 0 && test_errors == 0 )
934 fprintf( stdout,
"PASS\n" );
939 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
946 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
948 if( strlen(buf) != 0 )
950 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
956 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
957 if( total_errors == 0 )
958 fprintf( stdout,
"PASSED" );
960 fprintf( stdout,
"FAILED" );
962 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
963 total_tests - total_errors, total_tests, total_skipped );
965 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
966 #if defined(POLARSSL_MEMORY_DEBUG)
967 memory_buffer_alloc_status();
969 memory_buffer_alloc_free();
972 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.
Info structure for the pseudo random function.
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.
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, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
static int unhexify(unsigned char *obuf, const char *ibuf)
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.
#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Invalid key length.
int parse_arguments(char *buf, size_t len, char *params[50])
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
int verify_int(char *str, int *value)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Invalid data input length.
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.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int get_line(FILE *f, char *buf, size_t len)