1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_CIPHER_C
11 #if defined(POLARSSL_GCM_C)
17 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
21 #if defined(POLARSSL_PLATFORM_C)
24 #define polarssl_malloc malloc
25 #define polarssl_free free
30 typedef UINT32 uint32_t;
43 #define GET_UINT32_BE(n,b,i) \
45 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48 | ( (uint32_t) (b)[(i) + 3] ); \
53 #define PUT_UINT32_BE(n,b,i) \
55 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58 (b)[(i) + 3] = (unsigned char) ( (n) ); \
62 static int unhexify(
unsigned char *obuf,
const char *ibuf)
65 int len = strlen(ibuf) / 2;
66 assert(!(strlen(ibuf) %1));
71 if( c >=
'0' && c <=
'9' )
73 else if( c >=
'a' && c <=
'f' )
75 else if( c >=
'A' && c <=
'F' )
81 if( c2 >=
'0' && c2 <=
'9' )
83 else if( c2 >=
'a' && c2 <=
'f' )
85 else if( c2 >=
'A' && c2 <=
'F' )
90 *obuf++ = ( c << 4 ) | c2;
96 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
108 *obuf++ =
'a' + h - 10;
113 *obuf++ =
'a' + l - 10;
130 size_t actual_len = len != 0 ? len : 1;
135 memset( p, 0x00, actual_len );
154 *olen = strlen(ibuf) / 2;
160 assert( obuf != NULL );
176 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
178 #if !defined(__OpenBSD__)
181 if( rng_state != NULL )
184 for( i = 0; i < len; ++i )
187 if( rng_state != NULL )
190 arc4random_buf( output, len );
201 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
203 if( rng_state != NULL )
206 memset( output, 0, len );
233 if( rng_state == NULL )
242 memcpy( output, info->
buf, use_len );
243 info->
buf += use_len;
247 if( len - use_len > 0 )
248 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
277 uint32_t i, *k, sum, delta=0x9E3779B9;
278 unsigned char result[4], *out = output;
280 if( rng_state == NULL )
287 size_t use_len = ( len > 4 ) ? 4 : len;
290 for( i = 0; i < 32; i++ )
292 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
294 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
298 memcpy( out, result, use_len );
310 #if defined(POLARSSL_PLATFORM_C)
313 #define polarssl_printf printf
314 #define polarssl_malloc malloc
315 #define polarssl_free free
320 #ifdef POLARSSL_CIPHER_C
322 #define TEST_SUITE_ACTIVE
324 static int test_assert(
int correct,
const char *test )
331 printf(
"FAILED\n" );
332 printf(
" %s\n", test );
337 #define TEST_ASSERT( TEST ) \
338 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
339 if( test_errors) goto exit; \
344 if( (*str)[0] !=
'"' ||
345 (*str)[strlen( *str ) - 1] !=
'"' )
347 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
352 (*str)[strlen( *str ) - 1] =
'\0';
364 for( i = 0; i < strlen( str ); i++ )
366 if( i == 0 && str[i] ==
'-' )
372 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
373 str[i - 1] ==
'0' && str[i] ==
'x' )
379 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
380 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
381 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
391 *value = strtol( str, NULL, 16 );
393 *value = strtol( str, NULL, 10 );
398 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
399 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CBC" ) == 0 )
404 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
405 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
406 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
411 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
412 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
413 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CTR" ) == 0 )
418 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
419 #ifdef POLARSSL_CIPHER_MODE_CBC
420 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
425 #endif // POLARSSL_CIPHER_MODE_CBC
426 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
427 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CTR" ) == 0 )
432 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
433 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
434 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CBC" ) == 0 )
439 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
440 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
441 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
446 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
447 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
448 if( strcmp( str,
"-1" ) == 0 )
453 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
454 #ifdef POLARSSL_CIPHER_MODE_CBC
455 if( strcmp( str,
"POLARSSL_PADDING_PKCS7" ) == 0 )
460 #endif // POLARSSL_CIPHER_MODE_CBC
461 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
462 if( strcmp( str,
"POLARSSL_PADDING_PKCS7" ) == 0 )
467 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
468 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
469 if( strcmp( str,
"POLARSSL_CIPHER_NULL" ) == 0 )
474 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
475 #ifdef POLARSSL_CIPHER_MODE_CBC
476 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
481 #endif // POLARSSL_CIPHER_MODE_CBC
482 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
483 if( strcmp( str,
"POLARSSL_CIPHER_AES_128_CFB128" ) == 0 )
488 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
489 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
490 if( strcmp( str,
"POLARSSL_CIPHER_CAMELLIA_128_CFB128" ) == 0 )
495 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
496 #ifdef POLARSSL_CIPHER_MODE_CBC
497 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
502 #endif // POLARSSL_CIPHER_MODE_CBC
503 #ifdef POLARSSL_CIPHER_MODE_CBC
504 if( strcmp( str,
"POLARSSL_ERR_CIPHER_INVALID_PADDING" ) == 0 )
509 #endif // POLARSSL_CIPHER_MODE_CBC
510 #ifdef POLARSSL_CIPHER_MODE_CBC
511 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
516 #endif // POLARSSL_CIPHER_MODE_CBC
517 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
518 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
523 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
524 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
525 if( strcmp( str,
"POLARSSL_CIPHER_DES_CBC" ) == 0 )
530 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
531 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
532 if( strcmp( str,
"POLARSSL_ERR_CIPHER_BAD_INPUT_DATA" ) == 0 )
537 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
538 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
539 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE" ) == 0 )
544 #endif // POLARSSL_CIPHER_MODE_WITH_PADDING
547 printf(
"Expected integer for parameter and got: %s\n", str );
551 void test_suite_cipher_list( )
553 const int *cipher_type;
555 for( cipher_type =
cipher_list(); *cipher_type != 0; cipher_type++ )
562 void test_suite_cipher_null_args( )
566 unsigned char buf[1] = { 0 };
600 #if defined(POLARSSL_GCM_C)
617 #if defined(POLARSSL_GCM_C)
633 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
634 int length_val,
int pad_mode )
636 size_t length = length_val, outlen, total_len, i;
637 unsigned char key[32];
638 unsigned char iv[16];
639 unsigned char ad[13];
640 unsigned char tag[16];
641 unsigned char inbuf[64];
642 unsigned char encbuf[64];
643 unsigned char decbuf[64];
655 memset( key, 0x2a,
sizeof( key ) );
669 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
682 for( i = 0; i < 3; i++ )
684 memset( iv , 0x00 + i,
sizeof( iv ) );
685 memset( ad, 0x10 + i,
sizeof( ad ) );
686 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
688 memset( encbuf, 0,
sizeof( encbuf ) );
689 memset( decbuf, 0,
sizeof( decbuf ) );
690 memset( tag, 0,
sizeof( tag ) );
698 #if defined(POLARSSL_GCM_C)
709 total_len < length &&
715 #if defined(POLARSSL_GCM_C)
721 total_len > length &&
730 total_len < length &&
736 #if defined(POLARSSL_GCM_C)
753 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
754 int length_val,
int ret )
756 size_t length = length_val;
757 unsigned char key[32];
758 unsigned char iv[16];
763 unsigned char inbuf[64];
764 unsigned char encbuf[64];
768 memset( key, 0, 32 );
769 memset( iv , 0, 16 );
773 memset( inbuf, 5, 64 );
774 memset( encbuf, 0, 64 );
783 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
790 #if defined(POLARSSL_GCM_C)
803 void test_suite_dec_empty_buf()
805 unsigned char key[32];
806 unsigned char iv[16];
811 unsigned char encbuf[64];
812 unsigned char decbuf[64];
816 memset( key, 0, 32 );
817 memset( iv , 0, 16 );
821 memset( encbuf, 0, 64 );
822 memset( decbuf, 0, 64 );
836 #if defined(POLARSSL_GCM_C)
844 &ctx_dec, decbuf + outlen, &outlen ) );
851 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
852 int second_length_val )
854 size_t first_length = first_length_val;
855 size_t second_length = second_length_val;
856 size_t length = first_length + second_length;
857 unsigned char key[32];
858 unsigned char iv[16];
864 unsigned char inbuf[64];
865 unsigned char encbuf[64];
866 unsigned char decbuf[64];
869 size_t totaloutlen = 0;
871 memset( key, 0, 32 );
872 memset( iv , 0, 16 );
877 memset( inbuf, 5, 64 );
878 memset( encbuf, 0, 64 );
879 memset( decbuf, 0, 64 );
897 #if defined(POLARSSL_GCM_C)
904 totaloutlen = outlen;
906 totaloutlen += outlen;
909 totaloutlen < length &&
913 totaloutlen += outlen;
916 totaloutlen > length &&
921 totaloutlen = outlen;
925 totaloutlen < length &&
929 totaloutlen += outlen;
940 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
941 char *hex_key,
char *hex_iv,
942 char *hex_cipher,
char *hex_clear,
943 char *hex_ad,
char *hex_tag,
944 int finish_result,
int tag_result )
946 unsigned char key[50];
947 unsigned char iv[50];
948 unsigned char cipher[200];
949 unsigned char clear[200];
950 unsigned char ad[200];
951 unsigned char tag[20];
952 size_t key_len, iv_len, cipher_len, clear_len;
953 #if defined(POLARSSL_GCM_C)
954 size_t ad_len, tag_len;
957 unsigned char output[200];
958 size_t outlen, total_len;
962 memset( key, 0x00,
sizeof( key ) );
963 memset( iv, 0x00,
sizeof( iv ) );
964 memset( cipher, 0x00,
sizeof( cipher ) );
965 memset( clear, 0x00,
sizeof( clear ) );
966 memset( ad, 0x00,
sizeof( ad ) );
967 memset( tag, 0x00,
sizeof( tag ) );
968 memset( output, 0x00,
sizeof( output ) );
972 cipher_len =
unhexify( cipher, hex_cipher );
973 clear_len =
unhexify( clear, hex_clear );
974 #if defined(POLARSSL_GCM_C)
986 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
994 #if defined(POLARSSL_GCM_C)
1001 total_len += outlen;
1004 total_len += outlen;
1005 #if defined(POLARSSL_GCM_C)
1010 if( 0 == finish_result && 0 == tag_result )
1013 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
1020 #ifdef POLARSSL_CIPHER_MODE_AEAD
1021 void test_suite_auth_crypt_tv(
int cipher_id,
char *hex_key,
char *hex_iv,
1022 char *hex_ad,
char *hex_cipher,
1023 char *hex_tag,
char *hex_clear )
1026 unsigned char key[50];
1027 unsigned char iv[50];
1028 unsigned char cipher[200];
1029 unsigned char clear[200];
1030 unsigned char ad[200];
1031 unsigned char tag[20];
1032 unsigned char my_tag[20];
1033 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
1035 unsigned char output[200];
1040 memset( key, 0x00,
sizeof( key ) );
1041 memset( iv, 0x00,
sizeof( iv ) );
1042 memset( cipher, 0x00,
sizeof( cipher ) );
1043 memset( clear, 0x00,
sizeof( clear ) );
1044 memset( ad, 0x00,
sizeof( ad ) );
1045 memset( tag, 0x00,
sizeof( tag ) );
1046 memset( my_tag, 0xFF,
sizeof( my_tag ) );
1047 memset( output, 0xFF,
sizeof( output ) );
1049 key_len =
unhexify( key, hex_key );
1051 cipher_len =
unhexify( cipher, hex_cipher );
1053 tag_len =
unhexify( tag, hex_tag );
1062 cipher, cipher_len, output, &outlen,
1070 if( strcmp( hex_clear,
"FAIL" ) == 0 )
1079 clear_len =
unhexify( clear, hex_clear );
1081 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
1084 memset( output, 0xFF,
sizeof( output ) );
1088 clear, clear_len, output, &outlen,
1093 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
1094 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
1108 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
1109 char *hex_input,
char *hex_result,
1112 unsigned char key[50];
1113 unsigned char input[16];
1114 unsigned char result[16];
1117 unsigned char output[32];
1122 memset( key, 0x00,
sizeof( key ) );
1123 memset( input, 0x00,
sizeof( input ) );
1124 memset( result, 0x00,
sizeof( result ) );
1125 memset( output, 0x00,
sizeof( output ) );
1131 key_len =
unhexify( key, hex_key );
1141 output, &outlen ) );
1148 if( 0 == finish_result )
1156 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1157 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
1175 #ifdef POLARSSL_CIPHER_MODE_CBC
1176 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
1180 unsigned char input[16];
1190 ilen =
unhexify( input, input_str );
1201 #ifdef POLARSSL_SELF_TEST
1202 void test_suite_cipher_selftest()
1220 if( strcmp( str,
"POLARSSL_CAMELLIA_C" ) == 0 )
1222 #if defined(POLARSSL_CAMELLIA_C)
1228 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ZEROS" ) == 0 )
1230 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
1236 if( strcmp( str,
"POLARSSL_CIPHER_NULL_CIPHER" ) == 0 )
1238 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1244 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
1246 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
1252 if( strcmp( str,
"POLARSSL_BLOWFISH_C" ) == 0 )
1254 #if defined(POLARSSL_BLOWFISH_C)
1260 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS" ) == 0 )
1262 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
1268 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
1270 #if defined(POLARSSL_AES_C)
1276 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN" ) == 0 )
1278 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
1284 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
1286 #if defined(POLARSSL_CIPHER_MODE_CBC)
1292 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
1294 #if defined(POLARSSL_DES_C)
1300 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
1302 #if defined(POLARSSL_CIPHER_MODE_CFB)
1308 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CTR" ) == 0 )
1310 #if defined(POLARSSL_CIPHER_MODE_CTR)
1327 #if defined(TEST_SUITE_ACTIVE)
1328 if( strcmp( params[0],
"cipher_list" ) == 0 )
1334 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1339 test_suite_cipher_list( );
1345 if( strcmp( params[0],
"cipher_null_args" ) == 0 )
1351 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1356 test_suite_cipher_null_args( );
1362 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
1366 char *param2 = params[2];
1373 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1377 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1379 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1380 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1381 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1383 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1389 if( strcmp( params[0],
"enc_fail" ) == 0 )
1400 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1404 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1405 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1406 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1407 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1408 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1410 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1416 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
1422 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1427 test_suite_dec_empty_buf( );
1433 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1443 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1447 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1448 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1449 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1450 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1452 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1458 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1463 char *param3 = params[3];
1464 char *param4 = params[4];
1465 char *param5 = params[5];
1466 char *param6 = params[6];
1467 char *param7 = params[7];
1468 char *param8 = params[8];
1474 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1478 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1479 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1486 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1487 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1489 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1495 if( strcmp( params[0],
"auth_crypt_tv" ) == 0 )
1497 #ifdef POLARSSL_CIPHER_MODE_AEAD
1500 char *param2 = params[2];
1501 char *param3 = params[3];
1502 char *param4 = params[4];
1503 char *param5 = params[5];
1504 char *param6 = params[6];
1505 char *param7 = params[7];
1509 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1513 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1521 test_suite_auth_crypt_tv( param1, param2, param3, param4, param5, param6, param7 );
1528 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1533 char *param3 = params[3];
1534 char *param4 = params[4];
1535 char *param5 = params[5];
1540 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1544 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1545 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1549 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1551 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1557 if( strcmp( params[0],
"set_padding" ) == 0 )
1559 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1567 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1571 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1572 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1573 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1575 test_suite_set_padding( param1, param2, param3 );
1582 if( strcmp( params[0],
"check_padding" ) == 0 )
1584 #ifdef POLARSSL_CIPHER_MODE_CBC
1587 char *param2 = params[2];
1593 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1597 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1599 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1600 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1602 test_suite_check_padding( param1, param2, param3, param4 );
1609 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1611 #ifdef POLARSSL_SELF_TEST
1616 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1621 test_suite_cipher_selftest( );
1630 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1644 ret = fgets( buf, len, f );
1648 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1649 buf[strlen(buf) - 1] =
'\0';
1650 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1651 buf[strlen(buf) - 1] =
'\0';
1662 params[cnt++] = cur;
1664 while( *p !=
'\0' && p < buf + len )
1674 if( p + 1 < buf + len )
1677 params[cnt++] = cur;
1686 for( i = 0; i < cnt; i++ )
1693 if( *p ==
'\\' && *(p + 1) ==
'n' )
1698 else if( *p ==
'\\' && *(p + 1) ==
':' )
1703 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1719 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1720 const char *filename =
"/tmp/B.alu7eg1d/BUILD/polarssl-1.3.9/tests/suites/test_suite_cipher.padding.data";
1725 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1726 unsigned char alloc_buf[1000000];
1730 file = fopen( filename,
"r" );
1733 fprintf( stderr,
"Failed to open\n" );
1737 while( !feof( file ) )
1741 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1743 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1744 fprintf( stdout,
" " );
1745 for( i = strlen( buf ) + 1; i < 67; i++ )
1746 fprintf( stdout,
"." );
1747 fprintf( stdout,
" " );
1752 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1756 if( strcmp( params[0],
"depends_on" ) == 0 )
1758 for( i = 1; i < cnt; i++ )
1762 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1773 if( skip == 1 || ret == 3 )
1776 fprintf( stdout,
"----\n" );
1781 fprintf( stdout,
"PASS\n" );
1786 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1793 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1795 if( strlen(buf) != 0 )
1797 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1803 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1804 if( total_errors == 0 )
1805 fprintf( stdout,
"PASSED" );
1807 fprintf( stdout,
"FAILED" );
1809 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1810 total_tests - total_errors, total_tests, total_skipped );
1812 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1813 #if defined(POLARSSL_MEMORY_DEBUG)
1814 memory_buffer_alloc_status();
1819 return( total_errors != 0 );
#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.
Memory allocation layer (Deprecated to platform layer)
#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
The selected feature is not available.
static int cipher_get_iv_size(const cipher_context_t *ctx)
Returns the size of the cipher's IV/NONCE in bytes.
Info structure for the pseudo random function.
void cipher_init(cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
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)
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, const char *test)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
const cipher_info_t * cipher_info
Information about the associated cipher.
#define PUT_UINT32_BE(n, b, i)
int get_line(FILE *f, char *buf, size_t len)
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_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_auth_encrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, unsigned char *tag, size_t tag_len)
Generic autenticated encryption (AEAD ciphers).
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 unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int cipher_auth_decrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, const unsigned char *tag, size_t tag_len)
Generic autenticated decryption (AEAD ciphers).
int dispatch_test(int cnt, char *params[50])
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.
#define TEST_ASSERT(TEST)
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
void cipher_free(cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
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 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 parse_arguments(char *buf, size_t len, char *params[50])
never pad (full blocks only)
Galois/Counter mode for 128-bit block ciphers.
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
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.
#define POLARSSL_ERR_CIPHER_AUTH_FAILED
Authentication failed (for AEAD modes).