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,
"-1" ) == 0 )
373 if( strcmp( str,
"POLARSSL_CIPHER_ARC4_128" ) == 0 )
380 printf(
"Expected integer for parameter and got: %s\n", str );
384 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
385 int length_val,
int pad_mode )
387 size_t length = length_val, outlen, total_len, i;
388 unsigned char key[32];
389 unsigned char iv[16];
390 unsigned char ad[13];
391 unsigned char tag[16];
392 unsigned char inbuf[64];
393 unsigned char encbuf[64];
394 unsigned char decbuf[64];
403 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
404 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
406 memset( key, 0x2a,
sizeof( key ) );
420 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
433 for( i = 0; i < 3; i++ )
435 memset( iv , 0x00 + i,
sizeof( iv ) );
436 memset( ad, 0x10 + i,
sizeof( ad ) );
437 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
439 memset( encbuf, 0,
sizeof( encbuf ) );
440 memset( decbuf, 0,
sizeof( decbuf ) );
441 memset( tag, 0,
sizeof( tag ) );
449 #if defined(POLARSSL_CIPHER_MODE_AEAD)
460 total_len < length &&
466 #if defined(POLARSSL_CIPHER_MODE_AEAD)
472 total_len > length &&
481 total_len < length &&
487 #if defined(POLARSSL_CIPHER_MODE_AEAD)
503 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
504 int length_val,
int ret )
506 size_t length = length_val;
507 unsigned char key[32];
508 unsigned char iv[16];
513 unsigned char inbuf[64];
514 unsigned char encbuf[64];
518 memset( key, 0, 32 );
519 memset( iv , 0, 16 );
521 memset( &ctx, 0,
sizeof( ctx ) );
523 memset( inbuf, 5, 64 );
524 memset( encbuf, 0, 64 );
533 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
540 #if defined(POLARSSL_CIPHER_MODE_AEAD)
552 void test_suite_dec_empty_buf()
554 unsigned char key[32];
555 unsigned char iv[16];
560 unsigned char encbuf[64];
561 unsigned char decbuf[64];
565 memset( key, 0, 32 );
566 memset( iv , 0, 16 );
568 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
570 memset( encbuf, 0, 64 );
571 memset( decbuf, 0, 64 );
585 #if defined(POLARSSL_CIPHER_MODE_AEAD)
593 &ctx_dec, decbuf + outlen, &outlen ) );
599 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
600 int second_length_val )
602 size_t first_length = first_length_val;
603 size_t second_length = second_length_val;
604 size_t length = first_length + second_length;
605 unsigned char key[32];
606 unsigned char iv[16];
612 unsigned char inbuf[64];
613 unsigned char encbuf[64];
614 unsigned char decbuf[64];
617 size_t totaloutlen = 0;
619 memset( key, 0, 32 );
620 memset( iv , 0, 16 );
622 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
623 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
625 memset( inbuf, 5, 64 );
626 memset( encbuf, 0, 64 );
627 memset( decbuf, 0, 64 );
645 #if defined(POLARSSL_CIPHER_MODE_AEAD)
652 totaloutlen = outlen;
654 totaloutlen += outlen;
657 totaloutlen < length &&
661 totaloutlen += outlen;
664 totaloutlen > length &&
669 totaloutlen = outlen;
673 totaloutlen < length &&
677 totaloutlen += outlen;
687 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
688 char *hex_key,
char *hex_iv,
689 char *hex_cipher,
char *hex_clear,
690 char *hex_ad,
char *hex_tag,
691 int finish_result,
int tag_result )
693 unsigned char key[50];
694 unsigned char iv[50];
695 unsigned char cipher[200];
696 unsigned char clear[200];
697 unsigned char ad[200];
698 unsigned char tag[20];
699 size_t key_len, iv_len, cipher_len, clear_len;
700 #if defined(POLARSSL_CIPHER_MODE_AEAD)
701 size_t ad_len, tag_len;
704 unsigned char output[200];
705 size_t outlen, total_len;
707 memset( key, 0x00,
sizeof( key ) );
708 memset( iv, 0x00,
sizeof( iv ) );
709 memset( cipher, 0x00,
sizeof( cipher ) );
710 memset( clear, 0x00,
sizeof( clear ) );
711 memset( ad, 0x00,
sizeof( ad ) );
712 memset( tag, 0x00,
sizeof( tag ) );
713 memset( output, 0x00,
sizeof( output ) );
717 cipher_len =
unhexify( cipher, hex_cipher );
718 clear_len =
unhexify( clear, hex_clear );
719 #if defined(POLARSSL_CIPHER_MODE_AEAD)
731 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
739 #if defined(POLARSSL_CIPHER_MODE_AEAD)
750 #if defined(POLARSSL_CIPHER_MODE_AEAD)
755 if( 0 == finish_result && 0 == tag_result )
758 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
764 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
765 char *hex_input,
char *hex_result,
768 unsigned char key[50];
769 unsigned char input[16];
770 unsigned char result[16];
773 unsigned char output[32];
776 memset( key, 0x00,
sizeof( key ) );
777 memset( input, 0x00,
sizeof( input ) );
778 memset( result, 0x00,
sizeof( result ) );
779 memset( output, 0x00,
sizeof( output ) );
802 if( 0 == finish_result )
809 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
810 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
825 #ifdef POLARSSL_CIPHER_MODE_CBC
826 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
830 unsigned char input[16];
834 memset( &ctx, 0,
sizeof( ctx ) );
840 ilen =
unhexify( input, input_str );
848 #ifdef POLARSSL_SELF_TEST
849 void test_suite_cipher_selftest()
864 if( strcmp( str,
"POLARSSL_ARC4_C" ) == 0 )
866 #if defined(POLARSSL_ARC4_C)
883 #if defined(TEST_SUITE_ACTIVE)
884 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
888 char *param2 = params[2];
895 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
899 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
901 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
902 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
903 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
905 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
911 if( strcmp( params[0],
"enc_fail" ) == 0 )
922 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
926 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
927 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
928 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
929 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
930 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
932 test_suite_enc_fail( param1, param2, param3, param4, param5 );
938 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
944 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
949 test_suite_dec_empty_buf( );
955 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
965 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
969 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
970 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
971 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
972 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
974 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
980 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
985 char *param3 = params[3];
986 char *param4 = params[4];
987 char *param5 = params[5];
988 char *param6 = params[6];
989 char *param7 = params[7];
990 char *param8 = params[8];
996 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1000 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1001 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1008 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1009 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1011 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1017 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1022 char *param3 = params[3];
1023 char *param4 = params[4];
1024 char *param5 = params[5];
1029 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1033 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1034 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1038 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1040 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1046 if( strcmp( params[0],
"set_padding" ) == 0 )
1048 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1056 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1060 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1061 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1062 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1064 test_suite_set_padding( param1, param2, param3 );
1071 if( strcmp( params[0],
"check_padding" ) == 0 )
1073 #ifdef POLARSSL_CIPHER_MODE_CBC
1076 char *param2 = params[2];
1082 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1086 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1088 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1089 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1091 test_suite_check_padding( param1, param2, param3, param4 );
1098 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1100 #ifdef POLARSSL_SELF_TEST
1105 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1110 test_suite_cipher_selftest( );
1119 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1133 ret = fgets( buf, len, f );
1137 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1138 buf[strlen(buf) - 1] =
'\0';
1139 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1140 buf[strlen(buf) - 1] =
'\0';
1151 params[cnt++] = cur;
1153 while( *p !=
'\0' && p < buf + len )
1163 if( p + 1 < buf + len )
1166 params[cnt++] = cur;
1175 for( i = 0; i < cnt; i++ )
1182 if( *p ==
'\\' && *(p + 1) ==
'n' )
1187 else if( *p ==
'\\' && *(p + 1) ==
':' )
1192 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1208 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1209 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.arc4.data";
1214 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1215 unsigned char alloc_buf[1000000];
1216 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1219 file = fopen( filename,
"r" );
1222 fprintf( stderr,
"Failed to open\n" );
1226 while( !feof( file ) )
1230 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1232 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1233 fprintf( stdout,
" " );
1234 for( i = strlen( buf ) + 1; i < 67; i++ )
1235 fprintf( stdout,
"." );
1236 fprintf( stdout,
" " );
1241 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1245 if( strcmp( params[0],
"depends_on" ) == 0 )
1247 for( i = 1; i < cnt; i++ )
1251 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1262 if( skip == 1 || ret == 3 )
1265 fprintf( stdout,
"----\n" );
1270 fprintf( stdout,
"PASS\n" );
1275 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1282 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1284 if( strlen(buf) != 0 )
1286 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1292 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1293 if( total_errors == 0 )
1294 fprintf( stdout,
"PASSED" );
1296 fprintf( stdout,
"FAILED" );
1298 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1299 total_tests - total_errors, total_tests, total_skipped );
1301 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1302 #if defined(POLARSSL_MEMORY_DEBUG)
1303 memory_buffer_alloc_status();
1305 memory_buffer_alloc_free();
1308 return( total_errors != 0 );