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()
572 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
574 #if defined(POLARSSL_CIPHER_MODE_CFB)
591 #if defined(TEST_SUITE_ACTIVE)
592 if( strcmp( params[0],
"aes_encrypt_ecb" ) == 0 )
595 char *param1 = params[1];
596 char *param2 = params[2];
597 char *param3 = params[3];
602 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
609 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
611 test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
617 if( strcmp( params[0],
"aes_decrypt_ecb" ) == 0 )
620 char *param1 = params[1];
621 char *param2 = params[2];
622 char *param3 = params[3];
627 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
634 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
636 test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
642 if( strcmp( params[0],
"aes_encrypt_cbc" ) == 0 )
644 #ifdef POLARSSL_CIPHER_MODE_CBC
646 char *param1 = params[1];
647 char *param2 = params[2];
648 char *param3 = params[3];
649 char *param4 = params[4];
654 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
662 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
664 test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
671 if( strcmp( params[0],
"aes_decrypt_cbc" ) == 0 )
673 #ifdef POLARSSL_CIPHER_MODE_CBC
675 char *param1 = params[1];
676 char *param2 = params[2];
677 char *param3 = params[3];
678 char *param4 = params[4];
683 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
691 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
693 test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
700 if( strcmp( params[0],
"aes_encrypt_cfb128" ) == 0 )
702 #ifdef POLARSSL_CIPHER_MODE_CFB
704 char *param1 = params[1];
705 char *param2 = params[2];
706 char *param3 = params[3];
707 char *param4 = params[4];
711 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
720 test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
727 if( strcmp( params[0],
"aes_decrypt_cfb128" ) == 0 )
729 #ifdef POLARSSL_CIPHER_MODE_CFB
731 char *param1 = params[1];
732 char *param2 = params[2];
733 char *param3 = params[3];
734 char *param4 = params[4];
738 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
747 test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
754 if( strcmp( params[0],
"aes_selftest" ) == 0 )
756 #ifdef POLARSSL_SELF_TEST
761 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
766 test_suite_aes_selftest( );
775 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
789 ret = fgets( buf, len, f );
793 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
794 buf[strlen(buf) - 1] =
'\0';
795 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
796 buf[strlen(buf) - 1] =
'\0';
809 while( *p !=
'\0' && p < buf + len )
819 if( p + 1 < buf + len )
831 for( i = 0; i < cnt; i++ )
838 if( *p ==
'\\' && *(p + 1) ==
'n' )
843 else if( *p ==
'\\' && *(p + 1) ==
':' )
848 else if( *p ==
'\\' && *(p + 1) ==
'?' )
864 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
865 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_aes.cfb.data";
870 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
871 unsigned char alloc_buf[1000000];
872 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
875 file = fopen( filename,
"r" );
878 fprintf( stderr,
"Failed to open\n" );
882 while( !feof( file ) )
886 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
888 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
889 fprintf( stdout,
" " );
890 for( i = strlen( buf ) + 1; i < 67; i++ )
891 fprintf( stdout,
"." );
892 fprintf( stdout,
" " );
897 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
901 if( strcmp( params[0],
"depends_on" ) == 0 )
903 for( i = 1; i < cnt; i++ )
907 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
918 if( skip == 1 || ret == 3 )
921 fprintf( stdout,
"----\n" );
926 fprintf( stdout,
"PASS\n" );
931 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
938 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
940 if( strlen(buf) != 0 )
942 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
948 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
949 if( total_errors == 0 )
950 fprintf( stdout,
"PASSED" );
952 fprintf( stdout,
"FAILED" );
954 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
955 total_tests - total_errors, total_tests, total_skipped );
957 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
958 #if defined(POLARSSL_MEMORY_DEBUG)
959 memory_buffer_alloc_status();
961 memory_buffer_alloc_free();
964 return( total_errors != 0 );
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define PUT_UINT32_BE(n, b, i)
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.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
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 not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
#define TEST_ASSERT(TEST)
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 parse_arguments(char *buf, size_t len, char *params[50])
static int unhexify(unsigned char *obuf, const char *ibuf)
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int dispatch_test(int cnt, char *params[50])
int verify_int(char *str, int *value)
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.
int get_line(FILE *f, char *buf, size_t len)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.