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_AES_128_GCM" ) == 0 )
373 if( strcmp( str,
"POLARSSL_ERR_CIPHER_AUTH_FAILED" ) == 0 )
378 if( strcmp( str,
"-1" ) == 0 )
385 printf(
"Expected integer for parameter and got: %s\n", str );
389 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
390 int length_val,
int pad_mode )
392 size_t length = length_val, outlen, total_len, i;
393 unsigned char key[32];
394 unsigned char iv[16];
395 unsigned char ad[13];
396 unsigned char tag[16];
397 unsigned char inbuf[64];
398 unsigned char encbuf[64];
399 unsigned char decbuf[64];
408 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
409 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
411 memset( key, 0x2a,
sizeof( key ) );
425 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
438 for( i = 0; i < 3; i++ )
440 memset( iv , 0x00 + i,
sizeof( iv ) );
441 memset( ad, 0x10 + i,
sizeof( ad ) );
442 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
444 memset( encbuf, 0,
sizeof( encbuf ) );
445 memset( decbuf, 0,
sizeof( decbuf ) );
446 memset( tag, 0,
sizeof( tag ) );
454 #if defined(POLARSSL_CIPHER_MODE_AEAD)
465 total_len < length &&
471 #if defined(POLARSSL_CIPHER_MODE_AEAD)
477 total_len > length &&
486 total_len < length &&
492 #if defined(POLARSSL_CIPHER_MODE_AEAD)
508 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
509 int length_val,
int ret )
511 size_t length = length_val;
512 unsigned char key[32];
513 unsigned char iv[16];
518 unsigned char inbuf[64];
519 unsigned char encbuf[64];
523 memset( key, 0, 32 );
524 memset( iv , 0, 16 );
526 memset( &ctx, 0,
sizeof( ctx ) );
528 memset( inbuf, 5, 64 );
529 memset( encbuf, 0, 64 );
538 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
545 #if defined(POLARSSL_CIPHER_MODE_AEAD)
557 void test_suite_dec_empty_buf()
559 unsigned char key[32];
560 unsigned char iv[16];
565 unsigned char encbuf[64];
566 unsigned char decbuf[64];
570 memset( key, 0, 32 );
571 memset( iv , 0, 16 );
573 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
575 memset( encbuf, 0, 64 );
576 memset( decbuf, 0, 64 );
590 #if defined(POLARSSL_CIPHER_MODE_AEAD)
598 &ctx_dec, decbuf + outlen, &outlen ) );
604 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
605 int second_length_val )
607 size_t first_length = first_length_val;
608 size_t second_length = second_length_val;
609 size_t length = first_length + second_length;
610 unsigned char key[32];
611 unsigned char iv[16];
617 unsigned char inbuf[64];
618 unsigned char encbuf[64];
619 unsigned char decbuf[64];
622 size_t totaloutlen = 0;
624 memset( key, 0, 32 );
625 memset( iv , 0, 16 );
627 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
628 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
630 memset( inbuf, 5, 64 );
631 memset( encbuf, 0, 64 );
632 memset( decbuf, 0, 64 );
650 #if defined(POLARSSL_CIPHER_MODE_AEAD)
657 totaloutlen = outlen;
659 totaloutlen += outlen;
662 totaloutlen < length &&
666 totaloutlen += outlen;
669 totaloutlen > length &&
674 totaloutlen = outlen;
678 totaloutlen < length &&
682 totaloutlen += outlen;
692 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
693 char *hex_key,
char *hex_iv,
694 char *hex_cipher,
char *hex_clear,
695 char *hex_ad,
char *hex_tag,
696 int finish_result,
int tag_result )
698 unsigned char key[50];
699 unsigned char iv[50];
700 unsigned char cipher[200];
701 unsigned char clear[200];
702 unsigned char ad[200];
703 unsigned char tag[20];
704 size_t key_len, iv_len, cipher_len, clear_len;
705 #if defined(POLARSSL_CIPHER_MODE_AEAD)
706 size_t ad_len, tag_len;
709 unsigned char output[200];
710 size_t outlen, total_len;
712 memset( key, 0x00,
sizeof( key ) );
713 memset( iv, 0x00,
sizeof( iv ) );
714 memset( cipher, 0x00,
sizeof( cipher ) );
715 memset( clear, 0x00,
sizeof( clear ) );
716 memset( ad, 0x00,
sizeof( ad ) );
717 memset( tag, 0x00,
sizeof( tag ) );
718 memset( output, 0x00,
sizeof( output ) );
722 cipher_len =
unhexify( cipher, hex_cipher );
723 clear_len =
unhexify( clear, hex_clear );
724 #if defined(POLARSSL_CIPHER_MODE_AEAD)
736 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
744 #if defined(POLARSSL_CIPHER_MODE_AEAD)
755 #if defined(POLARSSL_CIPHER_MODE_AEAD)
760 if( 0 == finish_result && 0 == tag_result )
763 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
769 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
770 char *hex_input,
char *hex_result,
773 unsigned char key[50];
774 unsigned char input[16];
775 unsigned char result[16];
778 unsigned char output[32];
781 memset( key, 0x00,
sizeof( key ) );
782 memset( input, 0x00,
sizeof( input ) );
783 memset( result, 0x00,
sizeof( result ) );
784 memset( output, 0x00,
sizeof( output ) );
807 if( 0 == finish_result )
814 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
815 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
830 #ifdef POLARSSL_CIPHER_MODE_CBC
831 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
835 unsigned char input[16];
839 memset( &ctx, 0,
sizeof( ctx ) );
845 ilen =
unhexify( input, input_str );
853 #ifdef POLARSSL_SELF_TEST
854 void test_suite_cipher_selftest()
869 if( strcmp( str,
"POLARSSL_GCM_C" ) == 0 )
871 #if defined(POLARSSL_GCM_C)
877 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
879 #if defined(POLARSSL_AES_C)
896 #if defined(TEST_SUITE_ACTIVE)
897 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
901 char *param2 = params[2];
908 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
912 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
914 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
915 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
916 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
918 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
924 if( strcmp( params[0],
"enc_fail" ) == 0 )
935 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
939 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
940 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
941 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
942 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
943 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
945 test_suite_enc_fail( param1, param2, param3, param4, param5 );
951 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
957 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
962 test_suite_dec_empty_buf( );
968 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
978 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
982 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
983 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
984 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
985 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
987 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
993 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
998 char *param3 = params[3];
999 char *param4 = params[4];
1000 char *param5 = params[5];
1001 char *param6 = params[6];
1002 char *param7 = params[7];
1003 char *param8 = params[8];
1009 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1013 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1014 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1021 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1022 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1024 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1030 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1035 char *param3 = params[3];
1036 char *param4 = params[4];
1037 char *param5 = params[5];
1042 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1046 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1047 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1051 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1053 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1059 if( strcmp( params[0],
"set_padding" ) == 0 )
1061 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1069 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1073 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1074 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1075 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1077 test_suite_set_padding( param1, param2, param3 );
1084 if( strcmp( params[0],
"check_padding" ) == 0 )
1086 #ifdef POLARSSL_CIPHER_MODE_CBC
1089 char *param2 = params[2];
1095 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1099 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1101 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1102 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1104 test_suite_check_padding( param1, param2, param3, param4 );
1111 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1113 #ifdef POLARSSL_SELF_TEST
1118 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1123 test_suite_cipher_selftest( );
1132 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1146 ret = fgets( buf, len, f );
1150 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1151 buf[strlen(buf) - 1] =
'\0';
1152 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1153 buf[strlen(buf) - 1] =
'\0';
1164 params[cnt++] = cur;
1166 while( *p !=
'\0' && p < buf + len )
1176 if( p + 1 < buf + len )
1179 params[cnt++] = cur;
1188 for( i = 0; i < cnt; i++ )
1195 if( *p ==
'\\' && *(p + 1) ==
'n' )
1200 else if( *p ==
'\\' && *(p + 1) ==
':' )
1205 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1221 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1222 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.gcm.data";
1227 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1228 unsigned char alloc_buf[1000000];
1229 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1232 file = fopen( filename,
"r" );
1235 fprintf( stderr,
"Failed to open\n" );
1239 while( !feof( file ) )
1243 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1245 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1246 fprintf( stdout,
" " );
1247 for( i = strlen( buf ) + 1; i < 67; i++ )
1248 fprintf( stdout,
"." );
1249 fprintf( stdout,
" " );
1254 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1258 if( strcmp( params[0],
"depends_on" ) == 0 )
1260 for( i = 1; i < cnt; i++ )
1264 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1275 if( skip == 1 || ret == 3 )
1278 fprintf( stdout,
"----\n" );
1283 fprintf( stdout,
"PASS\n" );
1288 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1295 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1297 if( strlen(buf) != 0 )
1299 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1305 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1306 if( total_errors == 0 )
1307 fprintf( stdout,
"PASSED" );
1309 fprintf( stdout,
"FAILED" );
1311 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1312 total_tests - total_errors, total_tests, total_skipped );
1314 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1315 #if defined(POLARSSL_MEMORY_DEBUG)
1316 memory_buffer_alloc_status();
1318 memory_buffer_alloc_free();
1321 return( total_errors != 0 );