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_DES_EDE_CBC" ) == 0 )
373 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
378 if( strcmp( str,
"POLARSSL_CIPHER_DES_CBC" ) == 0 )
383 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
388 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
393 if( strcmp( str,
"POLARSSL_CIPHER_DES_EDE3_CBC" ) == 0 )
398 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
403 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
408 if( strcmp( str,
"-1" ) == 0 )
415 printf(
"Expected integer for parameter and got: %s\n", str );
419 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
420 int length_val,
int pad_mode )
422 size_t length = length_val, outlen, total_len, i;
423 unsigned char key[32];
424 unsigned char iv[16];
425 unsigned char ad[13];
426 unsigned char tag[16];
427 unsigned char inbuf[64];
428 unsigned char encbuf[64];
429 unsigned char decbuf[64];
438 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
439 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
441 memset( key, 0x2a,
sizeof( key ) );
455 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
468 for( i = 0; i < 3; i++ )
470 memset( iv , 0x00 + i,
sizeof( iv ) );
471 memset( ad, 0x10 + i,
sizeof( ad ) );
472 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
474 memset( encbuf, 0,
sizeof( encbuf ) );
475 memset( decbuf, 0,
sizeof( decbuf ) );
476 memset( tag, 0,
sizeof( tag ) );
484 #if defined(POLARSSL_CIPHER_MODE_AEAD)
495 total_len < length &&
501 #if defined(POLARSSL_CIPHER_MODE_AEAD)
507 total_len > length &&
516 total_len < length &&
522 #if defined(POLARSSL_CIPHER_MODE_AEAD)
538 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
539 int length_val,
int ret )
541 size_t length = length_val;
542 unsigned char key[32];
543 unsigned char iv[16];
548 unsigned char inbuf[64];
549 unsigned char encbuf[64];
553 memset( key, 0, 32 );
554 memset( iv , 0, 16 );
556 memset( &ctx, 0,
sizeof( ctx ) );
558 memset( inbuf, 5, 64 );
559 memset( encbuf, 0, 64 );
568 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
575 #if defined(POLARSSL_CIPHER_MODE_AEAD)
587 void test_suite_dec_empty_buf()
589 unsigned char key[32];
590 unsigned char iv[16];
595 unsigned char encbuf[64];
596 unsigned char decbuf[64];
600 memset( key, 0, 32 );
601 memset( iv , 0, 16 );
603 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
605 memset( encbuf, 0, 64 );
606 memset( decbuf, 0, 64 );
620 #if defined(POLARSSL_CIPHER_MODE_AEAD)
628 &ctx_dec, decbuf + outlen, &outlen ) );
634 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
635 int second_length_val )
637 size_t first_length = first_length_val;
638 size_t second_length = second_length_val;
639 size_t length = first_length + second_length;
640 unsigned char key[32];
641 unsigned char iv[16];
647 unsigned char inbuf[64];
648 unsigned char encbuf[64];
649 unsigned char decbuf[64];
652 size_t totaloutlen = 0;
654 memset( key, 0, 32 );
655 memset( iv , 0, 16 );
657 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
658 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
660 memset( inbuf, 5, 64 );
661 memset( encbuf, 0, 64 );
662 memset( decbuf, 0, 64 );
680 #if defined(POLARSSL_CIPHER_MODE_AEAD)
687 totaloutlen = outlen;
689 totaloutlen += outlen;
692 totaloutlen < length &&
696 totaloutlen += outlen;
699 totaloutlen > length &&
704 totaloutlen = outlen;
708 totaloutlen < length &&
712 totaloutlen += outlen;
722 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
723 char *hex_key,
char *hex_iv,
724 char *hex_cipher,
char *hex_clear,
725 char *hex_ad,
char *hex_tag,
726 int finish_result,
int tag_result )
728 unsigned char key[50];
729 unsigned char iv[50];
730 unsigned char cipher[200];
731 unsigned char clear[200];
732 unsigned char ad[200];
733 unsigned char tag[20];
734 size_t key_len, iv_len, cipher_len, clear_len;
735 #if defined(POLARSSL_CIPHER_MODE_AEAD)
736 size_t ad_len, tag_len;
739 unsigned char output[200];
740 size_t outlen, total_len;
742 memset( key, 0x00,
sizeof( key ) );
743 memset( iv, 0x00,
sizeof( iv ) );
744 memset( cipher, 0x00,
sizeof( cipher ) );
745 memset( clear, 0x00,
sizeof( clear ) );
746 memset( ad, 0x00,
sizeof( ad ) );
747 memset( tag, 0x00,
sizeof( tag ) );
748 memset( output, 0x00,
sizeof( output ) );
752 cipher_len =
unhexify( cipher, hex_cipher );
753 clear_len =
unhexify( clear, hex_clear );
754 #if defined(POLARSSL_CIPHER_MODE_AEAD)
766 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
774 #if defined(POLARSSL_CIPHER_MODE_AEAD)
785 #if defined(POLARSSL_CIPHER_MODE_AEAD)
790 if( 0 == finish_result && 0 == tag_result )
793 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
799 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
800 char *hex_input,
char *hex_result,
803 unsigned char key[50];
804 unsigned char input[16];
805 unsigned char result[16];
808 unsigned char output[32];
811 memset( key, 0x00,
sizeof( key ) );
812 memset( input, 0x00,
sizeof( input ) );
813 memset( result, 0x00,
sizeof( result ) );
814 memset( output, 0x00,
sizeof( output ) );
837 if( 0 == finish_result )
844 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
845 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
860 #ifdef POLARSSL_CIPHER_MODE_CBC
861 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
865 unsigned char input[16];
869 memset( &ctx, 0,
sizeof( ctx ) );
875 ilen =
unhexify( input, input_str );
883 #ifdef POLARSSL_SELF_TEST
884 void test_suite_cipher_selftest()
899 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
901 #if defined(POLARSSL_DES_C)
907 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
909 #if defined(POLARSSL_CIPHER_MODE_CBC)
915 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
917 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
934 #if defined(TEST_SUITE_ACTIVE)
935 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
939 char *param2 = params[2];
946 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
950 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
952 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
953 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
954 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
956 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
962 if( strcmp( params[0],
"enc_fail" ) == 0 )
973 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
977 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
978 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
979 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
980 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
981 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
983 test_suite_enc_fail( param1, param2, param3, param4, param5 );
989 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
995 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1000 test_suite_dec_empty_buf( );
1006 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1016 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1020 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1021 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1022 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1023 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1025 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1031 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1036 char *param3 = params[3];
1037 char *param4 = params[4];
1038 char *param5 = params[5];
1039 char *param6 = params[6];
1040 char *param7 = params[7];
1041 char *param8 = params[8];
1047 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1051 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1052 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1059 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1060 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1062 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1068 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1073 char *param3 = params[3];
1074 char *param4 = params[4];
1075 char *param5 = params[5];
1080 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1084 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1085 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1089 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1091 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1097 if( strcmp( params[0],
"set_padding" ) == 0 )
1099 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1107 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1111 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1112 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1113 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1115 test_suite_set_padding( param1, param2, param3 );
1122 if( strcmp( params[0],
"check_padding" ) == 0 )
1124 #ifdef POLARSSL_CIPHER_MODE_CBC
1127 char *param2 = params[2];
1133 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1137 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1139 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1140 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1142 test_suite_check_padding( param1, param2, param3, param4 );
1149 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1151 #ifdef POLARSSL_SELF_TEST
1156 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1161 test_suite_cipher_selftest( );
1170 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1184 ret = fgets( buf, len, f );
1188 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1189 buf[strlen(buf) - 1] =
'\0';
1190 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1191 buf[strlen(buf) - 1] =
'\0';
1202 params[cnt++] = cur;
1204 while( *p !=
'\0' && p < buf + len )
1214 if( p + 1 < buf + len )
1217 params[cnt++] = cur;
1226 for( i = 0; i < cnt; i++ )
1233 if( *p ==
'\\' && *(p + 1) ==
'n' )
1238 else if( *p ==
'\\' && *(p + 1) ==
':' )
1243 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1259 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1260 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.des.data";
1265 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1266 unsigned char alloc_buf[1000000];
1267 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1270 file = fopen( filename,
"r" );
1273 fprintf( stderr,
"Failed to open\n" );
1277 while( !feof( file ) )
1281 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1283 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1284 fprintf( stdout,
" " );
1285 for( i = strlen( buf ) + 1; i < 67; i++ )
1286 fprintf( stdout,
"." );
1287 fprintf( stdout,
" " );
1292 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1296 if( strcmp( params[0],
"depends_on" ) == 0 )
1298 for( i = 1; i < cnt; i++ )
1302 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1313 if( skip == 1 || ret == 3 )
1316 fprintf( stdout,
"----\n" );
1321 fprintf( stdout,
"PASS\n" );
1326 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1333 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1335 if( strlen(buf) != 0 )
1337 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1343 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1344 if( total_errors == 0 )
1345 fprintf( stdout,
"PASSED" );
1347 fprintf( stdout,
"FAILED" );
1349 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1350 total_tests - total_errors, total_tests, total_skipped );
1352 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1353 #if defined(POLARSSL_MEMORY_DEBUG)
1354 memory_buffer_alloc_status();
1356 memory_buffer_alloc_free();
1359 return( total_errors != 0 );