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 if( strcmp( str,
"POLARSSL_DECRYPT" ) == 0 )
403 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_ECB" ) == 0 )
408 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
413 if( strcmp( str,
"POLARSSL_ENCRYPT" ) == 0 )
418 if( strcmp( str,
"-1" ) == 0 )
423 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
428 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
433 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
438 if( strcmp( str,
"POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
443 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
448 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
453 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
460 printf(
"Expected integer for parameter and got: %s\n", str );
464 void test_suite_cipher_list( )
466 const int *cipher_type;
468 for( cipher_type =
cipher_list(); *cipher_type != 0; cipher_type++ )
475 void test_suite_cipher_null_args( )
479 unsigned char buf[1] = { 0 };
513 #if defined(POLARSSL_GCM_C)
530 #if defined(POLARSSL_GCM_C)
546 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
547 int length_val,
int pad_mode )
549 size_t length = length_val, outlen, total_len, i;
550 unsigned char key[32];
551 unsigned char iv[16];
552 unsigned char ad[13];
553 unsigned char tag[16];
554 unsigned char inbuf[64];
555 unsigned char encbuf[64];
556 unsigned char decbuf[64];
568 memset( key, 0x2a,
sizeof( key ) );
582 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
595 for( i = 0; i < 3; i++ )
597 memset( iv , 0x00 + i,
sizeof( iv ) );
598 memset( ad, 0x10 + i,
sizeof( ad ) );
599 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
601 memset( encbuf, 0,
sizeof( encbuf ) );
602 memset( decbuf, 0,
sizeof( decbuf ) );
603 memset( tag, 0,
sizeof( tag ) );
611 #if defined(POLARSSL_GCM_C)
622 total_len < length &&
628 #if defined(POLARSSL_GCM_C)
634 total_len > length &&
643 total_len < length &&
649 #if defined(POLARSSL_GCM_C)
666 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
667 int length_val,
int ret )
669 size_t length = length_val;
670 unsigned char key[32];
671 unsigned char iv[16];
676 unsigned char inbuf[64];
677 unsigned char encbuf[64];
681 memset( key, 0, 32 );
682 memset( iv , 0, 16 );
686 memset( inbuf, 5, 64 );
687 memset( encbuf, 0, 64 );
696 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
703 #if defined(POLARSSL_GCM_C)
716 void test_suite_dec_empty_buf()
718 unsigned char key[32];
719 unsigned char iv[16];
724 unsigned char encbuf[64];
725 unsigned char decbuf[64];
729 memset( key, 0, 32 );
730 memset( iv , 0, 16 );
734 memset( encbuf, 0, 64 );
735 memset( decbuf, 0, 64 );
749 #if defined(POLARSSL_GCM_C)
757 &ctx_dec, decbuf + outlen, &outlen ) );
764 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
765 int second_length_val )
767 size_t first_length = first_length_val;
768 size_t second_length = second_length_val;
769 size_t length = first_length + second_length;
770 unsigned char key[32];
771 unsigned char iv[16];
777 unsigned char inbuf[64];
778 unsigned char encbuf[64];
779 unsigned char decbuf[64];
782 size_t totaloutlen = 0;
784 memset( key, 0, 32 );
785 memset( iv , 0, 16 );
790 memset( inbuf, 5, 64 );
791 memset( encbuf, 0, 64 );
792 memset( decbuf, 0, 64 );
810 #if defined(POLARSSL_GCM_C)
817 totaloutlen = outlen;
819 totaloutlen += outlen;
822 totaloutlen < length &&
826 totaloutlen += outlen;
829 totaloutlen > length &&
834 totaloutlen = outlen;
838 totaloutlen < length &&
842 totaloutlen += outlen;
853 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
854 char *hex_key,
char *hex_iv,
855 char *hex_cipher,
char *hex_clear,
856 char *hex_ad,
char *hex_tag,
857 int finish_result,
int tag_result )
859 unsigned char key[50];
860 unsigned char iv[50];
861 unsigned char cipher[200];
862 unsigned char clear[200];
863 unsigned char ad[200];
864 unsigned char tag[20];
865 size_t key_len, iv_len, cipher_len, clear_len;
866 #if defined(POLARSSL_GCM_C)
867 size_t ad_len, tag_len;
870 unsigned char output[200];
871 size_t outlen, total_len;
875 memset( key, 0x00,
sizeof( key ) );
876 memset( iv, 0x00,
sizeof( iv ) );
877 memset( cipher, 0x00,
sizeof( cipher ) );
878 memset( clear, 0x00,
sizeof( clear ) );
879 memset( ad, 0x00,
sizeof( ad ) );
880 memset( tag, 0x00,
sizeof( tag ) );
881 memset( output, 0x00,
sizeof( output ) );
885 cipher_len =
unhexify( cipher, hex_cipher );
886 clear_len =
unhexify( clear, hex_clear );
887 #if defined(POLARSSL_GCM_C)
899 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
907 #if defined(POLARSSL_GCM_C)
918 #if defined(POLARSSL_GCM_C)
923 if( 0 == finish_result && 0 == tag_result )
926 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
933 #ifdef POLARSSL_CIPHER_MODE_AEAD
934 void test_suite_auth_crypt_tv(
int cipher_id,
char *hex_key,
char *hex_iv,
935 char *hex_ad,
char *hex_cipher,
936 char *hex_tag,
char *hex_clear )
939 unsigned char key[50];
940 unsigned char iv[50];
941 unsigned char cipher[200];
942 unsigned char clear[200];
943 unsigned char ad[200];
944 unsigned char tag[20];
945 unsigned char my_tag[20];
946 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
948 unsigned char output[200];
953 memset( key, 0x00,
sizeof( key ) );
954 memset( iv, 0x00,
sizeof( iv ) );
955 memset( cipher, 0x00,
sizeof( cipher ) );
956 memset( clear, 0x00,
sizeof( clear ) );
957 memset( ad, 0x00,
sizeof( ad ) );
958 memset( tag, 0x00,
sizeof( tag ) );
959 memset( my_tag, 0xFF,
sizeof( my_tag ) );
960 memset( output, 0xFF,
sizeof( output ) );
964 cipher_len =
unhexify( cipher, hex_cipher );
975 cipher, cipher_len, output, &outlen,
983 if( strcmp( hex_clear,
"FAIL" ) == 0 )
992 clear_len =
unhexify( clear, hex_clear );
994 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
997 memset( output, 0xFF,
sizeof( output ) );
1001 clear, clear_len, output, &outlen,
1006 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
1007 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
1021 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
1022 char *hex_input,
char *hex_result,
1025 unsigned char key[50];
1026 unsigned char input[16];
1027 unsigned char result[16];
1030 unsigned char output[32];
1035 memset( key, 0x00,
sizeof( key ) );
1036 memset( input, 0x00,
sizeof( input ) );
1037 memset( result, 0x00,
sizeof( result ) );
1038 memset( output, 0x00,
sizeof( output ) );
1044 key_len =
unhexify( key, hex_key );
1054 output, &outlen ) );
1061 if( 0 == finish_result )
1069 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1070 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
1088 #ifdef POLARSSL_CIPHER_MODE_CBC
1089 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
1093 unsigned char input[16];
1103 ilen =
unhexify( input, input_str );
1114 #ifdef POLARSSL_SELF_TEST
1115 void test_suite_cipher_selftest()
1133 if( strcmp( str,
"POLARSSL_BLOWFISH_C" ) == 0 )
1135 #if defined(POLARSSL_BLOWFISH_C)
1141 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
1143 #if defined(POLARSSL_CIPHER_MODE_CFB)
1149 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
1151 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
1157 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CTR" ) == 0 )
1159 #if defined(POLARSSL_CIPHER_MODE_CTR)
1165 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
1167 #if defined(POLARSSL_CIPHER_MODE_CBC)
1184 #if defined(TEST_SUITE_ACTIVE)
1185 if( strcmp( params[0],
"cipher_list" ) == 0 )
1191 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1196 test_suite_cipher_list( );
1202 if( strcmp( params[0],
"cipher_null_args" ) == 0 )
1208 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1213 test_suite_cipher_null_args( );
1219 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
1223 char *param2 = params[2];
1230 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1234 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1236 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1237 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1238 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1240 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1246 if( strcmp( params[0],
"enc_fail" ) == 0 )
1257 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1261 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1262 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1263 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1264 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1265 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1267 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1273 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
1279 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1284 test_suite_dec_empty_buf( );
1290 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1300 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1304 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1305 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1306 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1307 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1309 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1315 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1320 char *param3 = params[3];
1321 char *param4 = params[4];
1322 char *param5 = params[5];
1323 char *param6 = params[6];
1324 char *param7 = params[7];
1325 char *param8 = params[8];
1331 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1335 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1336 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1343 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1344 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1346 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1352 if( strcmp( params[0],
"auth_crypt_tv" ) == 0 )
1354 #ifdef POLARSSL_CIPHER_MODE_AEAD
1357 char *param2 = params[2];
1358 char *param3 = params[3];
1359 char *param4 = params[4];
1360 char *param5 = params[5];
1361 char *param6 = params[6];
1362 char *param7 = params[7];
1366 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1370 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1378 test_suite_auth_crypt_tv( param1, param2, param3, param4, param5, param6, param7 );
1385 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1390 char *param3 = params[3];
1391 char *param4 = params[4];
1392 char *param5 = params[5];
1397 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1401 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1402 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1406 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1408 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1414 if( strcmp( params[0],
"set_padding" ) == 0 )
1416 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1424 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1428 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1429 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1430 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1432 test_suite_set_padding( param1, param2, param3 );
1439 if( strcmp( params[0],
"check_padding" ) == 0 )
1441 #ifdef POLARSSL_CIPHER_MODE_CBC
1444 char *param2 = params[2];
1450 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1454 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1456 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1457 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1459 test_suite_check_padding( param1, param2, param3, param4 );
1466 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1468 #ifdef POLARSSL_SELF_TEST
1473 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1478 test_suite_cipher_selftest( );
1487 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1501 ret = fgets( buf, len, f );
1505 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1506 buf[strlen(buf) - 1] =
'\0';
1507 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1508 buf[strlen(buf) - 1] =
'\0';
1519 params[cnt++] = cur;
1521 while( *p !=
'\0' && p < buf + len )
1531 if( p + 1 < buf + len )
1534 params[cnt++] = cur;
1543 for( i = 0; i < cnt; i++ )
1550 if( *p ==
'\\' && *(p + 1) ==
'n' )
1555 else if( *p ==
'\\' && *(p + 1) ==
':' )
1560 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1576 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1577 const char *filename =
"/tmp/B.z5jajb5_/BUILD/polarssl-1.3.9/tests/suites/test_suite_cipher.blowfish.data";
1582 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1583 unsigned char alloc_buf[1000000];
1587 file = fopen( filename,
"r" );
1590 fprintf( stderr,
"Failed to open\n" );
1594 while( !feof( file ) )
1598 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1600 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1601 fprintf( stdout,
" " );
1602 for( i = strlen( buf ) + 1; i < 67; i++ )
1603 fprintf( stdout,
"." );
1604 fprintf( stdout,
" " );
1609 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1613 if( strcmp( params[0],
"depends_on" ) == 0 )
1615 for( i = 1; i < cnt; i++ )
1619 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1630 if( skip == 1 || ret == 3 )
1633 fprintf( stdout,
"----\n" );
1638 fprintf( stdout,
"PASS\n" );
1643 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1650 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1652 if( strlen(buf) != 0 )
1654 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1660 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1661 if( total_errors == 0 )
1662 fprintf( stdout,
"PASSED" );
1664 fprintf( stdout,
"FAILED" );
1666 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1667 total_tests - total_errors, total_tests, total_skipped );
1669 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1670 #if defined(POLARSSL_MEMORY_DEBUG)
1671 memory_buffer_alloc_status();
1676 return( total_errors != 0 );
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Memory allocation layer (Deprecated to platform layer)
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 rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
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.
int get_line(FILE *f, char *buf, size_t len)
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 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dispatch_test(int cnt, char *params[50])
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).
#define PUT_UINT32_BE(n, b, i)
int parse_arguments(char *buf, size_t len, char *params[50])
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
#define TEST_ASSERT(TEST)
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
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.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_string(char **str)
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.
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).