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_CIPHER_CAMELLIA_128_CTR" ) == 0 )
373 if( strcmp( str,
"POLARSSL_CIPHER_NULL" ) == 0 )
378 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
383 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
388 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CBC" ) == 0 )
393 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
398 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
403 if( strcmp( str,
"POLARSSL_CIPHER_DES_CBC" ) == 0 )
408 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
413 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE" ) == 0 )
418 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
423 if( strcmp( str,
"POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
428 if( strcmp( str,
"POLARSSL_ERR_CIPHER_BAD_INPUT_DATA" ) == 0 )
433 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CFB128" ) == 0 )
438 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
443 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
448 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
453 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CTR" ) == 0 )
458 if( strcmp( str,
"POLARSSL_PADDING_PKCS7" ) == 0 )
463 if( strcmp( str,
"-1" ) == 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_DES_C" ) == 0 )
988 #if defined(POLARSSL_DES_C)
994 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
996 #if defined(POLARSSL_CIPHER_MODE_CBC)
1002 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
1004 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
1010 if( strcmp( str,
"POLARSSL_CIPHER_NULL_CIPHER" ) == 0 )
1012 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1018 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
1020 #if defined(POLARSSL_CIPHER_MODE_CFB)
1026 if( strcmp( str,
"POLARSSL_BLOWFISH_C" ) == 0 )
1028 #if defined(POLARSSL_BLOWFISH_C)
1034 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CTR" ) == 0 )
1036 #if defined(POLARSSL_CIPHER_MODE_CTR)
1042 if( strcmp( str,
"POLARSSL_CAMELLIA_C" ) == 0 )
1044 #if defined(POLARSSL_CAMELLIA_C)
1061 #if defined(TEST_SUITE_ACTIVE)
1062 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
1066 char *param2 = params[2];
1073 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1077 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1079 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1080 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1081 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1083 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1089 if( strcmp( params[0],
"enc_fail" ) == 0 )
1100 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1104 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1105 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1106 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1107 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1108 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1110 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1116 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
1122 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1127 test_suite_dec_empty_buf( );
1133 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1143 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1147 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1148 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1149 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1150 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1152 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1158 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1163 char *param3 = params[3];
1164 char *param4 = params[4];
1165 char *param5 = params[5];
1166 char *param6 = params[6];
1167 char *param7 = params[7];
1168 char *param8 = params[8];
1174 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1178 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1179 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1186 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1187 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1189 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1195 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1200 char *param3 = params[3];
1201 char *param4 = params[4];
1202 char *param5 = params[5];
1207 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1211 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1212 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1216 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1218 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1224 if( strcmp( params[0],
"set_padding" ) == 0 )
1226 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1234 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1238 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1239 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1240 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1242 test_suite_set_padding( param1, param2, param3 );
1249 if( strcmp( params[0],
"check_padding" ) == 0 )
1251 #ifdef POLARSSL_CIPHER_MODE_CBC
1254 char *param2 = params[2];
1260 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1264 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1266 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1267 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1269 test_suite_check_padding( param1, param2, param3, param4 );
1276 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1278 #ifdef POLARSSL_SELF_TEST
1283 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1288 test_suite_cipher_selftest( );
1297 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1311 ret = fgets( buf, len, f );
1315 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1316 buf[strlen(buf) - 1] =
'\0';
1317 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1318 buf[strlen(buf) - 1] =
'\0';
1329 params[cnt++] = cur;
1331 while( *p !=
'\0' && p < buf + len )
1341 if( p + 1 < buf + len )
1344 params[cnt++] = cur;
1353 for( i = 0; i < cnt; i++ )
1360 if( *p ==
'\\' && *(p + 1) ==
'n' )
1365 else if( *p ==
'\\' && *(p + 1) ==
':' )
1370 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1386 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1387 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.padding.data";
1392 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1393 unsigned char alloc_buf[1000000];
1394 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1397 file = fopen( filename,
"r" );
1400 fprintf( stderr,
"Failed to open\n" );
1404 while( !feof( file ) )
1408 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1410 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1411 fprintf( stdout,
" " );
1412 for( i = strlen( buf ) + 1; i < 67; i++ )
1413 fprintf( stdout,
"." );
1414 fprintf( stdout,
" " );
1419 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1423 if( strcmp( params[0],
"depends_on" ) == 0 )
1425 for( i = 1; i < cnt; i++ )
1429 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1440 if( skip == 1 || ret == 3 )
1443 fprintf( stdout,
"----\n" );
1448 fprintf( stdout,
"PASS\n" );
1453 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1460 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1462 if( strlen(buf) != 0 )
1464 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1470 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1471 if( total_errors == 0 )
1472 fprintf( stdout,
"PASSED" );
1474 fprintf( stdout,
"FAILED" );
1476 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1477 total_tests - total_errors, total_tests, total_skipped );
1479 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1480 #if defined(POLARSSL_MEMORY_DEBUG)
1481 memory_buffer_alloc_status();
1483 memory_buffer_alloc_free();
1486 return( total_errors != 0 );