3 #ifdef POLARSSL_CIPHER_C
7 #if defined(POLARSSL_GCM_C)
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19 typedef UINT32 uint32_t;
32 #define GET_UINT32_BE(n,b,i) \
34 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
35 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
36 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
37 | ( (uint32_t) (b)[(i) + 3] ); \
42 #define PUT_UINT32_BE(n,b,i) \
44 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
45 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
46 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
47 (b)[(i) + 3] = (unsigned char) ( (n) ); \
51 static int unhexify(
unsigned char *obuf,
const char *ibuf)
54 int len = strlen(ibuf) / 2;
55 assert(!(strlen(ibuf) %1));
60 if( c >=
'0' && c <=
'9' )
62 else if( c >=
'a' && c <=
'f' )
64 else if( c >=
'A' && c <=
'F' )
70 if( c2 >=
'0' && c2 <=
'9' )
72 else if( c2 >=
'a' && c2 <=
'f' )
74 else if( c2 >=
'A' && c2 <=
'F' )
79 *obuf++ = ( c << 4 ) | c2;
85 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
97 *obuf++ =
'a' + h - 10;
102 *obuf++ =
'a' + l - 10;
118 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
122 if( rng_state != NULL )
125 for( i = 0; i < len; ++i )
136 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
138 if( rng_state != NULL )
141 memset( output, 0, len );
168 if( rng_state == NULL )
177 memcpy( output, info->
buf, use_len );
178 info->
buf += use_len;
182 if( len - use_len > 0 )
183 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
212 uint32_t i, *k, sum, delta=0x9E3779B9;
213 unsigned char result[4];
215 if( rng_state == NULL )
222 size_t use_len = ( len > 4 ) ? 4 : len;
225 for( i = 0; i < 32; i++ )
227 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
229 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
233 memcpy( output, result, use_len );
249 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
252 const char *ibuf = in;
254 assert( len == strlen(ibuf) / 2 );
255 assert(!(strlen(ibuf) %1));
257 obuf = out + (len - 1);
261 if( c >=
'0' && c <=
'9' )
263 else if( c >=
'a' && c <=
'f' )
265 else if( c >=
'A' && c <=
'F' )
271 if( c2 >=
'0' && c2 <=
'9' )
273 else if( c2 >=
'a' && c2 <=
'f' )
275 else if( c2 >=
'A' && c2 <=
'F' )
280 *obuf-- = ( c << 4 ) | c2;
292 #ifdef POLARSSL_CIPHER_C
294 #define TEST_SUITE_ACTIVE
303 printf(
"FAILED\n" );
304 printf(
" %s\n", test );
309 #define TEST_ASSERT( TEST ) \
310 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
311 if( test_errors) return; \
316 if( (*str)[0] !=
'"' ||
317 (*str)[strlen( *str ) - 1] !=
'"' )
319 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
324 (*str)[strlen( *str ) - 1] =
'\0';
336 for( i = 0; i < strlen( str ); i++ )
338 if( i == 0 && str[i] ==
'-' )
344 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
345 str[i - 1] ==
'0' && str[i] ==
'x' )
351 if( str[i] <
'0' || str[i] >
'9' )
361 *value = strtol( str, NULL, 16 );
363 *value = strtol( str, NULL, 10 );
368 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
373 if( strcmp( str,
"POLARSSL_CIPHER_AES_256_CFB128" ) == 0 )
378 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_ECB" ) == 0 )
383 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
388 if( strcmp( str,
"POLARSSL_CIPHER_AES_192_ECB" ) == 0 )
393 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
398 if( strcmp( str,
"POLARSSL_CIPHER_AES_256_CBC" ) == 0 )
403 if( strcmp( str,
"POLARSSL_CIPHER_AES_192_CFB128" ) == 0 )
408 if( strcmp( str,
"POLARSSL_DECRYPT" ) == 0 )
413 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
418 if( strcmp( str,
"POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
423 if( strcmp( str,
"POLARSSL_CIPHER_AES_256_ECB" ) == 0 )
428 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
433 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
438 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
443 if( strcmp( str,
"POLARSSL_CIPHER_AES_192_CBC" ) == 0 )
448 if( strcmp( str,
"POLARSSL_ENCRYPT" ) == 0 )
453 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CTR" ) == 0 )
458 if( strcmp( str,
"-1" ) == 0 )
463 if( strcmp( str,
"POLARSSL_PADDING_PKCS7" ) == 0 )
470 printf(
"Expected integer for parameter and got: %s\n", str );
474 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
475 int length_val,
int pad_mode )
477 size_t length = length_val, outlen, total_len, i;
478 unsigned char key[32];
479 unsigned char iv[16];
480 unsigned char ad[13];
481 unsigned char tag[16];
482 unsigned char inbuf[64];
483 unsigned char encbuf[64];
484 unsigned char decbuf[64];
493 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
494 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
496 memset( key, 0x2a,
sizeof( key ) );
510 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
523 for( i = 0; i < 3; i++ )
525 memset( iv , 0x00 + i,
sizeof( iv ) );
526 memset( ad, 0x10 + i,
sizeof( ad ) );
527 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
529 memset( encbuf, 0,
sizeof( encbuf ) );
530 memset( decbuf, 0,
sizeof( decbuf ) );
531 memset( tag, 0,
sizeof( tag ) );
539 #if defined(POLARSSL_CIPHER_MODE_AEAD)
550 total_len < length &&
556 #if defined(POLARSSL_CIPHER_MODE_AEAD)
562 total_len > length &&
571 total_len < length &&
577 #if defined(POLARSSL_CIPHER_MODE_AEAD)
593 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
594 int length_val,
int ret )
596 size_t length = length_val;
597 unsigned char key[32];
598 unsigned char iv[16];
603 unsigned char inbuf[64];
604 unsigned char encbuf[64];
608 memset( key, 0, 32 );
609 memset( iv , 0, 16 );
611 memset( &ctx, 0,
sizeof( ctx ) );
613 memset( inbuf, 5, 64 );
614 memset( encbuf, 0, 64 );
623 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
630 #if defined(POLARSSL_CIPHER_MODE_AEAD)
642 void test_suite_dec_empty_buf()
644 unsigned char key[32];
645 unsigned char iv[16];
650 unsigned char encbuf[64];
651 unsigned char decbuf[64];
655 memset( key, 0, 32 );
656 memset( iv , 0, 16 );
658 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
660 memset( encbuf, 0, 64 );
661 memset( decbuf, 0, 64 );
675 #if defined(POLARSSL_CIPHER_MODE_AEAD)
683 &ctx_dec, decbuf + outlen, &outlen ) );
689 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
690 int second_length_val )
692 size_t first_length = first_length_val;
693 size_t second_length = second_length_val;
694 size_t length = first_length + second_length;
695 unsigned char key[32];
696 unsigned char iv[16];
702 unsigned char inbuf[64];
703 unsigned char encbuf[64];
704 unsigned char decbuf[64];
707 size_t totaloutlen = 0;
709 memset( key, 0, 32 );
710 memset( iv , 0, 16 );
712 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
713 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
715 memset( inbuf, 5, 64 );
716 memset( encbuf, 0, 64 );
717 memset( decbuf, 0, 64 );
735 #if defined(POLARSSL_CIPHER_MODE_AEAD)
742 totaloutlen = outlen;
744 totaloutlen += outlen;
747 totaloutlen < length &&
751 totaloutlen += outlen;
754 totaloutlen > length &&
759 totaloutlen = outlen;
763 totaloutlen < length &&
767 totaloutlen += outlen;
777 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
778 char *hex_key,
char *hex_iv,
779 char *hex_cipher,
char *hex_clear,
780 char *hex_ad,
char *hex_tag,
781 int finish_result,
int tag_result )
783 unsigned char key[50];
784 unsigned char iv[50];
785 unsigned char cipher[200];
786 unsigned char clear[200];
787 unsigned char ad[200];
788 unsigned char tag[20];
789 size_t key_len, iv_len, cipher_len, clear_len;
790 #if defined(POLARSSL_CIPHER_MODE_AEAD)
791 size_t ad_len, tag_len;
794 unsigned char output[200];
795 size_t outlen, total_len;
797 memset( key, 0x00,
sizeof( key ) );
798 memset( iv, 0x00,
sizeof( iv ) );
799 memset( cipher, 0x00,
sizeof( cipher ) );
800 memset( clear, 0x00,
sizeof( clear ) );
801 memset( ad, 0x00,
sizeof( ad ) );
802 memset( tag, 0x00,
sizeof( tag ) );
803 memset( output, 0x00,
sizeof( output ) );
807 cipher_len =
unhexify( cipher, hex_cipher );
808 clear_len =
unhexify( clear, hex_clear );
809 #if defined(POLARSSL_CIPHER_MODE_AEAD)
821 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
829 #if defined(POLARSSL_CIPHER_MODE_AEAD)
840 #if defined(POLARSSL_CIPHER_MODE_AEAD)
845 if( 0 == finish_result && 0 == tag_result )
848 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
854 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
855 char *hex_input,
char *hex_result,
858 unsigned char key[50];
859 unsigned char input[16];
860 unsigned char result[16];
863 unsigned char output[32];
866 memset( key, 0x00,
sizeof( key ) );
867 memset( input, 0x00,
sizeof( input ) );
868 memset( result, 0x00,
sizeof( result ) );
869 memset( output, 0x00,
sizeof( output ) );
892 if( 0 == finish_result )
899 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
900 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
915 #ifdef POLARSSL_CIPHER_MODE_CBC
916 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
920 unsigned char input[16];
924 memset( &ctx, 0,
sizeof( ctx ) );
930 ilen =
unhexify( input, input_str );
938 #ifdef POLARSSL_SELF_TEST
939 void test_suite_cipher_selftest()
954 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN" ) == 0 )
956 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
962 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ZEROS" ) == 0 )
964 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
970 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS" ) == 0 )
972 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
978 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
980 #if defined(POLARSSL_AES_C)
986 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
988 #if defined(POLARSSL_CIPHER_MODE_CBC)
994 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
996 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
1002 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
1004 #if defined(POLARSSL_CIPHER_MODE_CFB)
1010 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CTR" ) == 0 )
1012 #if defined(POLARSSL_CIPHER_MODE_CTR)
1029 #if defined(TEST_SUITE_ACTIVE)
1030 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
1034 char *param2 = params[2];
1041 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1045 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1047 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1048 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1049 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1051 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1057 if( strcmp( params[0],
"enc_fail" ) == 0 )
1068 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1072 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1073 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1074 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1075 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1076 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1078 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1084 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
1090 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1095 test_suite_dec_empty_buf( );
1101 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1111 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1115 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1116 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1117 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1118 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1120 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1126 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1131 char *param3 = params[3];
1132 char *param4 = params[4];
1133 char *param5 = params[5];
1134 char *param6 = params[6];
1135 char *param7 = params[7];
1136 char *param8 = params[8];
1142 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1146 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1147 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1154 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1155 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1157 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1163 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1168 char *param3 = params[3];
1169 char *param4 = params[4];
1170 char *param5 = params[5];
1175 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1179 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1180 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1184 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1186 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1192 if( strcmp( params[0],
"set_padding" ) == 0 )
1194 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1202 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1206 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1207 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1208 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1210 test_suite_set_padding( param1, param2, param3 );
1217 if( strcmp( params[0],
"check_padding" ) == 0 )
1219 #ifdef POLARSSL_CIPHER_MODE_CBC
1222 char *param2 = params[2];
1228 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1232 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1234 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1235 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1237 test_suite_check_padding( param1, param2, param3, param4 );
1244 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1246 #ifdef POLARSSL_SELF_TEST
1251 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1256 test_suite_cipher_selftest( );
1265 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1279 ret = fgets( buf, len, f );
1283 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1284 buf[strlen(buf) - 1] =
'\0';
1285 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1286 buf[strlen(buf) - 1] =
'\0';
1297 params[cnt++] = cur;
1299 while( *p !=
'\0' && p < buf + len )
1309 if( p + 1 < buf + len )
1312 params[cnt++] = cur;
1321 for( i = 0; i < cnt; i++ )
1328 if( *p ==
'\\' && *(p + 1) ==
'n' )
1333 else if( *p ==
'\\' && *(p + 1) ==
':' )
1338 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1354 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1355 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.aes.data";
1360 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1361 unsigned char alloc_buf[1000000];
1362 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1365 file = fopen( filename,
"r" );
1368 fprintf( stderr,
"Failed to open\n" );
1372 while( !feof( file ) )
1376 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1378 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1379 fprintf( stdout,
" " );
1380 for( i = strlen( buf ) + 1; i < 67; i++ )
1381 fprintf( stdout,
"." );
1382 fprintf( stdout,
" " );
1387 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1391 if( strcmp( params[0],
"depends_on" ) == 0 )
1393 for( i = 1; i < cnt; i++ )
1397 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1408 if( skip == 1 || ret == 3 )
1411 fprintf( stdout,
"----\n" );
1416 fprintf( stdout,
"PASS\n" );
1421 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1428 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1430 if( strlen(buf) != 0 )
1432 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1438 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1439 if( total_errors == 0 )
1440 fprintf( stdout,
"PASSED" );
1442 fprintf( stdout,
"FAILED" );
1444 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1445 total_tests - total_errors, total_tests, total_skipped );
1447 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1448 #if defined(POLARSSL_MEMORY_DEBUG)
1449 memory_buffer_alloc_status();
1451 memory_buffer_alloc_free();
1454 return( total_errors != 0 );