3 #ifdef POLARSSL_CIPHER_C
7 #if defined(POLARSSL_GCM_C)
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(WANT_NOT_RND_MPI)
18 #if defined(POLARSSL_BIGNUM_C)
21 #error "not_rnd_mpi() need bignum.c"
27 typedef UINT32 uint32_t;
40 #define GET_UINT32_BE(n,b,i) \
42 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
43 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
44 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
45 | ( (uint32_t) (b)[(i) + 3] ); \
50 #define PUT_UINT32_BE(n,b,i) \
52 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
53 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
54 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
55 (b)[(i) + 3] = (unsigned char) ( (n) ); \
59 static int unhexify(
unsigned char *obuf,
const char *ibuf)
62 int len = strlen(ibuf) / 2;
63 assert(!(strlen(ibuf) %1));
68 if( c >=
'0' && c <=
'9' )
70 else if( c >=
'a' && c <=
'f' )
72 else if( c >=
'A' && c <=
'F' )
78 if( c2 >=
'0' && c2 <=
'9' )
80 else if( c2 >=
'a' && c2 <=
'f' )
82 else if( c2 >=
'A' && c2 <=
'F' )
87 *obuf++ = ( c << 4 ) | c2;
93 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
105 *obuf++ =
'a' + h - 10;
110 *obuf++ =
'a' + l - 10;
126 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
130 if( rng_state != NULL )
133 for( i = 0; i < len; ++i )
144 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
146 if( rng_state != NULL )
149 memset( output, 0, len );
176 if( rng_state == NULL )
185 memcpy( output, info->
buf, use_len );
186 info->
buf += use_len;
190 if( len - use_len > 0 )
191 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
220 uint32_t i, *k, sum, delta=0x9E3779B9;
221 unsigned char result[4];
223 if( rng_state == NULL )
230 size_t use_len = ( len > 4 ) ? 4 : len;
233 for( i = 0; i < 32; i++ )
235 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
237 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
241 memcpy( output, result, use_len );
248 #if defined(WANT_NOT_RND_MPI)
257 #define ciL (sizeof(t_uint))
258 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
259 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
261 char *str = (
char *) in;
270 X.
n = CHARS_TO_LIMBS( len );
276 assert( strlen( str ) / 2 == len );
288 #ifdef POLARSSL_CIPHER_C
290 #define TEST_SUITE_ACTIVE
298 if( test_errors == 1 )
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
364 #ifdef POLARSSL_CIPHER_MODE_CBC
365 if( strcmp( str,
"POLARSSL_PADDING_PKCS7" ) == 0 )
370 #endif // POLARSSL_CIPHER_MODE_CBC
371 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
372 if( strcmp( str,
"POLARSSL_PADDING_PKCS7" ) == 0 )
377 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
378 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
379 if( strcmp( str,
"POLARSSL_ERR_CIPHER_BAD_INPUT_DATA" ) == 0 )
384 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
385 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
386 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CTR" ) == 0 )
391 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
392 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
393 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
398 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
399 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
400 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
405 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
406 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
407 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CTR" ) == 0 )
412 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
413 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
414 if( strcmp( str,
"-1" ) == 0 )
419 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
420 #ifdef POLARSSL_CIPHER_MODE_CBC
421 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
426 #endif // POLARSSL_CIPHER_MODE_CBC
427 #ifdef POLARSSL_CIPHER_MODE_CBC
428 if( strcmp( str,
"POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
433 #endif // POLARSSL_CIPHER_MODE_CBC
434 #ifdef POLARSSL_CIPHER_MODE_CBC
435 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
440 #endif // POLARSSL_CIPHER_MODE_CBC
441 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
442 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE" ) == 0 )
447 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
448 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
449 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CBC" ) == 0 )
454 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
455 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
456 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
461 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
462 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
463 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
468 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
469 #ifdef POLARSSL_CIPHER_MODE_CBC
470 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
475 #endif // POLARSSL_CIPHER_MODE_CBC
476 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
477 if( strcmp( str,
"POLARSSL_CIPHER_DES_CBC" ) == 0 )
482 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
483 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
484 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
489 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
490 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
491 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CFB128" ) == 0 )
496 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
497 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
498 if( strcmp( str,
"POLARSSL_CIPHER_NULL" ) == 0 )
503 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
504 #ifdef POLARSSL_CIPHER_MODE_CBC
505 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
510 #endif // POLARSSL_CIPHER_MODE_CBC
513 printf(
"Expected integer for parameter and got: %s\n", str );
517 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
518 int length_val,
int pad_mode )
520 size_t length = length_val, outlen, total_len, i;
521 unsigned char key[32];
522 unsigned char iv[16];
523 unsigned char ad[13];
524 unsigned char tag[16];
525 unsigned char inbuf[64];
526 unsigned char encbuf[64];
527 unsigned char decbuf[64];
536 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
537 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
539 memset( key, 0x2a,
sizeof( key ) );
553 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
566 for( i = 0; i < 3; i++ )
568 memset( iv , 0x00 + i,
sizeof( iv ) );
569 memset( ad, 0x10 + i,
sizeof( ad ) );
570 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
572 memset( encbuf, 0,
sizeof( encbuf ) );
573 memset( decbuf, 0,
sizeof( decbuf ) );
574 memset( tag, 0,
sizeof( tag ) );
582 #if defined(POLARSSL_CIPHER_MODE_AEAD)
593 total_len < length &&
599 #if defined(POLARSSL_CIPHER_MODE_AEAD)
605 total_len > length &&
614 total_len < length &&
620 #if defined(POLARSSL_CIPHER_MODE_AEAD)
636 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
637 int length_val,
int ret )
639 size_t length = length_val;
640 unsigned char key[32];
641 unsigned char iv[16];
646 unsigned char inbuf[64];
647 unsigned char encbuf[64];
651 memset( key, 0, 32 );
652 memset( iv , 0, 16 );
654 memset( &ctx, 0,
sizeof( ctx ) );
656 memset( inbuf, 5, 64 );
657 memset( encbuf, 0, 64 );
666 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
673 #if defined(POLARSSL_CIPHER_MODE_AEAD)
685 void test_suite_dec_empty_buf()
687 unsigned char key[32];
688 unsigned char iv[16];
693 unsigned char encbuf[64];
694 unsigned char decbuf[64];
698 memset( key, 0, 32 );
699 memset( iv , 0, 16 );
701 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
703 memset( encbuf, 0, 64 );
704 memset( decbuf, 0, 64 );
718 #if defined(POLARSSL_CIPHER_MODE_AEAD)
726 &ctx_dec, decbuf + outlen, &outlen ) );
732 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
733 int second_length_val )
735 size_t first_length = first_length_val;
736 size_t second_length = second_length_val;
737 size_t length = first_length + second_length;
738 unsigned char key[32];
739 unsigned char iv[16];
745 unsigned char inbuf[64];
746 unsigned char encbuf[64];
747 unsigned char decbuf[64];
750 size_t totaloutlen = 0;
752 memset( key, 0, 32 );
753 memset( iv , 0, 16 );
755 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
756 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
758 memset( inbuf, 5, 64 );
759 memset( encbuf, 0, 64 );
760 memset( decbuf, 0, 64 );
778 #if defined(POLARSSL_CIPHER_MODE_AEAD)
785 totaloutlen = outlen;
787 totaloutlen += outlen;
790 totaloutlen < length &&
794 totaloutlen += outlen;
797 totaloutlen > length &&
802 totaloutlen = outlen;
806 totaloutlen < length &&
810 totaloutlen += outlen;
820 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
821 char *hex_key,
char *hex_iv,
822 char *hex_cipher,
char *hex_clear,
823 char *hex_ad,
char *hex_tag,
824 int finish_result,
int tag_result )
826 unsigned char key[50];
827 unsigned char iv[50];
828 unsigned char cipher[200];
829 unsigned char clear[200];
830 unsigned char ad[200];
831 unsigned char tag[20];
832 size_t key_len, iv_len, cipher_len, clear_len;
833 #if defined(POLARSSL_CIPHER_MODE_AEAD)
834 size_t ad_len, tag_len;
837 unsigned char output[200];
838 size_t outlen, total_len;
840 memset( key, 0x00,
sizeof( key ) );
841 memset( iv, 0x00,
sizeof( iv ) );
842 memset( cipher, 0x00,
sizeof( cipher ) );
843 memset( clear, 0x00,
sizeof( clear ) );
844 memset( ad, 0x00,
sizeof( ad ) );
845 memset( tag, 0x00,
sizeof( tag ) );
846 memset( output, 0x00,
sizeof( output ) );
850 cipher_len =
unhexify( cipher, hex_cipher );
851 clear_len =
unhexify( clear, hex_clear );
852 #if defined(POLARSSL_CIPHER_MODE_AEAD)
864 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
872 #if defined(POLARSSL_CIPHER_MODE_AEAD)
883 #if defined(POLARSSL_CIPHER_MODE_AEAD)
888 if( 0 == finish_result && 0 == tag_result )
891 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
897 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
898 char *hex_input,
char *hex_result,
901 unsigned char key[50];
902 unsigned char input[16];
903 unsigned char result[16];
906 unsigned char output[32];
909 memset( key, 0x00,
sizeof( key ) );
910 memset( input, 0x00,
sizeof( input ) );
911 memset( result, 0x00,
sizeof( result ) );
912 memset( output, 0x00,
sizeof( output ) );
935 if( 0 == finish_result )
942 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
943 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
958 #ifdef POLARSSL_CIPHER_MODE_CBC
959 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
963 unsigned char input[16];
967 memset( &ctx, 0,
sizeof( ctx ) );
973 ilen =
unhexify( input, input_str );
981 #ifdef POLARSSL_SELF_TEST
982 void test_suite_cipher_selftest()
997 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN" ) == 0 )
999 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
1005 if( strcmp( str,
"POLARSSL_BLOWFISH_C" ) == 0 )
1007 #if defined(POLARSSL_BLOWFISH_C)
1013 if( strcmp( str,
"POLARSSL_CAMELLIA_C" ) == 0 )
1015 #if defined(POLARSSL_CAMELLIA_C)
1021 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
1023 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
1029 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CTR" ) == 0 )
1031 #if defined(POLARSSL_CIPHER_MODE_CTR)
1037 if( strcmp( str,
"POLARSSL_CIPHER_NULL_CIPHER" ) == 0 )
1039 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1045 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
1047 #if defined(POLARSSL_AES_C)
1053 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
1055 #if defined(POLARSSL_DES_C)
1061 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
1063 #if defined(POLARSSL_CIPHER_MODE_CFB)
1069 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
1071 #if defined(POLARSSL_CIPHER_MODE_CBC)
1077 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ZEROS" ) == 0 )
1079 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
1085 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS" ) == 0 )
1087 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
1104 #if defined(TEST_SUITE_ACTIVE)
1105 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
1109 char *param2 = params[2];
1116 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1120 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1122 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1123 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1124 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1126 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1132 if( strcmp( params[0],
"enc_fail" ) == 0 )
1143 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
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 );
1151 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1153 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1159 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
1165 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1170 test_suite_dec_empty_buf( );
1176 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1186 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1190 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1191 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1192 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1193 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1195 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1201 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1206 char *param3 = params[3];
1207 char *param4 = params[4];
1208 char *param5 = params[5];
1209 char *param6 = params[6];
1210 char *param7 = params[7];
1211 char *param8 = params[8];
1217 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1221 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1222 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1229 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1230 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1232 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1238 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1243 char *param3 = params[3];
1244 char *param4 = params[4];
1245 char *param5 = params[5];
1250 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1254 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1255 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1259 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1261 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1267 if( strcmp( params[0],
"set_padding" ) == 0 )
1269 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1277 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1281 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1282 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1283 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1285 test_suite_set_padding( param1, param2, param3 );
1292 if( strcmp( params[0],
"check_padding" ) == 0 )
1294 #ifdef POLARSSL_CIPHER_MODE_CBC
1297 char *param2 = params[2];
1303 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1307 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1309 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1310 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1312 test_suite_check_padding( param1, param2, param3, param4 );
1319 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1321 #ifdef POLARSSL_SELF_TEST
1326 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1331 test_suite_cipher_selftest( );
1340 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1354 ret = fgets( buf, len, f );
1358 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1359 buf[strlen(buf) - 1] =
'\0';
1360 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1361 buf[strlen(buf) - 1] =
'\0';
1372 params[cnt++] = cur;
1374 while( *p !=
'\0' && p < buf + len )
1384 if( p + 1 < buf + len )
1387 params[cnt++] = cur;
1396 for( i = 0; i < cnt; i++ )
1403 if( *p ==
'\\' && *(p + 1) ==
'n' )
1408 else if( *p ==
'\\' && *(p + 1) ==
':' )
1413 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1429 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1430 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.padding.data";
1435 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1436 unsigned char alloc_buf[1000000];
1437 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1440 file = fopen( filename,
"r" );
1443 fprintf( stderr,
"Failed to open\n" );
1447 while( !feof( file ) )
1451 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1453 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1454 fprintf( stdout,
" " );
1455 for( i = strlen( buf ) + 1; i < 67; i++ )
1456 fprintf( stdout,
"." );
1457 fprintf( stdout,
" " );
1462 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1466 if( strcmp( params[0],
"depends_on" ) == 0 )
1468 for( i = 1; i < cnt; i++ )
1472 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1483 if( skip == 1 || ret == 3 )
1486 fprintf( stdout,
"----\n" );
1489 else if( ret == 0 && test_errors == 0 )
1491 fprintf( stdout,
"PASS\n" );
1496 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1503 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1505 if( strlen(buf) != 0 )
1507 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1513 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1514 if( total_errors == 0 )
1515 fprintf( stdout,
"PASSED" );
1517 fprintf( stdout,
"FAILED" );
1519 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1520 total_tests - total_errors, total_tests, total_skipped );
1522 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1523 #if defined(POLARSSL_MEMORY_DEBUG)
1524 memory_buffer_alloc_status();
1526 memory_buffer_alloc_free();
1529 return( total_errors != 0 );
#define PUT_UINT32_BE(n, b, i)
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
The selected feature is not available.
Info structure for the pseudo random function.
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
zero padding (not reversible!)
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
static int unhexify(unsigned char *obuf, const char *ibuf)
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Configuration options (set of defines)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
#define POLARSSL_ERR_CIPHER_INVALID_PADDING
Input data contains invalid padding and is rejected.
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
const cipher_info_t * cipher_info
Information about the associated cipher.
#define TEST_ASSERT(TEST)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
int parse_arguments(char *buf, size_t len, char *params[50])
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
cipher_mode_t mode
Cipher mode (e.g.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
never pad (full blocks only)
Galois/Counter mode for 128-bit block ciphers.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_int(char *str, int *value)
int cipher_self_test(int verbose)
Checkup routine.
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
int get_line(FILE *f, char *buf, size_t len)