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 if( strcmp( str,
"POLARSSL_CIPHER_DES_EDE3_CBC" ) == 0 )
369 if( strcmp( str,
"POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
374 if( strcmp( str,
"POLARSSL_CIPHER_DES_CBC" ) == 0 )
379 if( strcmp( str,
"POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
384 if( strcmp( str,
"POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
389 if( strcmp( str,
"POLARSSL_CIPHER_DES_EDE_CBC" ) == 0 )
394 if( strcmp( str,
"-1" ) == 0 )
399 if( strcmp( str,
"POLARSSL_PADDING_ZEROS" ) == 0 )
404 if( strcmp( str,
"POLARSSL_PADDING_NONE" ) == 0 )
411 printf(
"Expected integer for parameter and got: %s\n", str );
415 void test_suite_enc_dec_buf(
int cipher_id,
char *cipher_string,
int key_len,
416 int length_val,
int pad_mode )
418 size_t length = length_val, outlen, total_len, i;
419 unsigned char key[32];
420 unsigned char iv[16];
421 unsigned char ad[13];
422 unsigned char tag[16];
423 unsigned char inbuf[64];
424 unsigned char encbuf[64];
425 unsigned char decbuf[64];
434 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
435 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
437 memset( key, 0x2a,
sizeof( key ) );
451 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
464 for( i = 0; i < 3; i++ )
466 memset( iv , 0x00 + i,
sizeof( iv ) );
467 memset( ad, 0x10 + i,
sizeof( ad ) );
468 memset( inbuf, 0x20 + i,
sizeof( inbuf ) );
470 memset( encbuf, 0,
sizeof( encbuf ) );
471 memset( decbuf, 0,
sizeof( decbuf ) );
472 memset( tag, 0,
sizeof( tag ) );
480 #if defined(POLARSSL_CIPHER_MODE_AEAD)
491 total_len < length &&
497 #if defined(POLARSSL_CIPHER_MODE_AEAD)
503 total_len > length &&
512 total_len < length &&
518 #if defined(POLARSSL_CIPHER_MODE_AEAD)
534 void test_suite_enc_fail(
int cipher_id,
int pad_mode,
int key_len,
535 int length_val,
int ret )
537 size_t length = length_val;
538 unsigned char key[32];
539 unsigned char iv[16];
544 unsigned char inbuf[64];
545 unsigned char encbuf[64];
549 memset( key, 0, 32 );
550 memset( iv , 0, 16 );
552 memset( &ctx, 0,
sizeof( ctx ) );
554 memset( inbuf, 5, 64 );
555 memset( encbuf, 0, 64 );
564 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
571 #if defined(POLARSSL_CIPHER_MODE_AEAD)
583 void test_suite_dec_empty_buf()
585 unsigned char key[32];
586 unsigned char iv[16];
591 unsigned char encbuf[64];
592 unsigned char decbuf[64];
596 memset( key, 0, 32 );
597 memset( iv , 0, 16 );
599 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
601 memset( encbuf, 0, 64 );
602 memset( decbuf, 0, 64 );
616 #if defined(POLARSSL_CIPHER_MODE_AEAD)
624 &ctx_dec, decbuf + outlen, &outlen ) );
630 void test_suite_enc_dec_buf_multipart(
int cipher_id,
int key_len,
int first_length_val,
631 int second_length_val )
633 size_t first_length = first_length_val;
634 size_t second_length = second_length_val;
635 size_t length = first_length + second_length;
636 unsigned char key[32];
637 unsigned char iv[16];
643 unsigned char inbuf[64];
644 unsigned char encbuf[64];
645 unsigned char decbuf[64];
648 size_t totaloutlen = 0;
650 memset( key, 0, 32 );
651 memset( iv , 0, 16 );
653 memset( &ctx_dec, 0,
sizeof( ctx_dec ) );
654 memset( &ctx_enc, 0,
sizeof( ctx_enc ) );
656 memset( inbuf, 5, 64 );
657 memset( encbuf, 0, 64 );
658 memset( decbuf, 0, 64 );
676 #if defined(POLARSSL_CIPHER_MODE_AEAD)
683 totaloutlen = outlen;
685 totaloutlen += outlen;
688 totaloutlen < length &&
692 totaloutlen += outlen;
695 totaloutlen > length &&
700 totaloutlen = outlen;
704 totaloutlen < length &&
708 totaloutlen += outlen;
718 void test_suite_decrypt_test_vec(
int cipher_id,
int pad_mode,
719 char *hex_key,
char *hex_iv,
720 char *hex_cipher,
char *hex_clear,
721 char *hex_ad,
char *hex_tag,
722 int finish_result,
int tag_result )
724 unsigned char key[50];
725 unsigned char iv[50];
726 unsigned char cipher[200];
727 unsigned char clear[200];
728 unsigned char ad[200];
729 unsigned char tag[20];
730 size_t key_len, iv_len, cipher_len, clear_len;
731 #if defined(POLARSSL_CIPHER_MODE_AEAD)
732 size_t ad_len, tag_len;
735 unsigned char output[200];
736 size_t outlen, total_len;
738 memset( key, 0x00,
sizeof( key ) );
739 memset( iv, 0x00,
sizeof( iv ) );
740 memset( cipher, 0x00,
sizeof( cipher ) );
741 memset( clear, 0x00,
sizeof( clear ) );
742 memset( ad, 0x00,
sizeof( ad ) );
743 memset( tag, 0x00,
sizeof( tag ) );
744 memset( output, 0x00,
sizeof( output ) );
748 cipher_len =
unhexify( cipher, hex_cipher );
749 clear_len =
unhexify( clear, hex_clear );
750 #if defined(POLARSSL_CIPHER_MODE_AEAD)
762 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
770 #if defined(POLARSSL_CIPHER_MODE_AEAD)
781 #if defined(POLARSSL_CIPHER_MODE_AEAD)
786 if( 0 == finish_result && 0 == tag_result )
789 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
795 void test_suite_test_vec_ecb(
int cipher_id,
int operation,
char *hex_key,
796 char *hex_input,
char *hex_result,
799 unsigned char key[50];
800 unsigned char input[16];
801 unsigned char result[16];
804 unsigned char output[32];
807 memset( key, 0x00,
sizeof( key ) );
808 memset( input, 0x00,
sizeof( input ) );
809 memset( result, 0x00,
sizeof( result ) );
810 memset( output, 0x00,
sizeof( output ) );
833 if( 0 == finish_result )
840 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
841 void test_suite_set_padding(
int cipher_id,
int pad_mode,
int ret )
856 #ifdef POLARSSL_CIPHER_MODE_CBC
857 void test_suite_check_padding(
int pad_mode,
char *input_str,
int ret,
int dlen_check )
861 unsigned char input[16];
865 memset( &ctx, 0,
sizeof( ctx ) );
871 ilen =
unhexify( input, input_str );
879 #ifdef POLARSSL_SELF_TEST
880 void test_suite_cipher_selftest()
895 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
897 #if defined(POLARSSL_CIPHER_MODE_CBC)
903 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
905 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
911 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
913 #if defined(POLARSSL_DES_C)
930 #if defined(TEST_SUITE_ACTIVE)
931 if( strcmp( params[0],
"enc_dec_buf" ) == 0 )
935 char *param2 = params[2];
942 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
946 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
948 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
949 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
950 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
952 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
958 if( strcmp( params[0],
"enc_fail" ) == 0 )
969 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
973 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
974 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
975 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
976 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
977 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
979 test_suite_enc_fail( param1, param2, param3, param4, param5 );
985 if( strcmp( params[0],
"dec_empty_buf" ) == 0 )
991 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
996 test_suite_dec_empty_buf( );
1002 if( strcmp( params[0],
"enc_dec_buf_multipart" ) == 0 )
1012 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1016 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1017 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1018 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1019 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1021 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1027 if( strcmp( params[0],
"decrypt_test_vec" ) == 0 )
1032 char *param3 = params[3];
1033 char *param4 = params[4];
1034 char *param5 = params[5];
1035 char *param6 = params[6];
1036 char *param7 = params[7];
1037 char *param8 = params[8];
1043 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1047 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1048 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1055 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1056 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1058 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1064 if( strcmp( params[0],
"test_vec_ecb" ) == 0 )
1069 char *param3 = params[3];
1070 char *param4 = params[4];
1071 char *param5 = params[5];
1076 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1080 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1081 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1085 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1087 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1093 if( strcmp( params[0],
"set_padding" ) == 0 )
1095 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1103 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1107 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1108 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1109 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1111 test_suite_set_padding( param1, param2, param3 );
1118 if( strcmp( params[0],
"check_padding" ) == 0 )
1120 #ifdef POLARSSL_CIPHER_MODE_CBC
1123 char *param2 = params[2];
1129 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1133 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1135 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1136 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1138 test_suite_check_padding( param1, param2, param3, param4 );
1145 if( strcmp( params[0],
"cipher_selftest" ) == 0 )
1147 #ifdef POLARSSL_SELF_TEST
1152 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1157 test_suite_cipher_selftest( );
1166 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1180 ret = fgets( buf, len, f );
1184 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1185 buf[strlen(buf) - 1] =
'\0';
1186 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1187 buf[strlen(buf) - 1] =
'\0';
1198 params[cnt++] = cur;
1200 while( *p !=
'\0' && p < buf + len )
1210 if( p + 1 < buf + len )
1213 params[cnt++] = cur;
1222 for( i = 0; i < cnt; i++ )
1229 if( *p ==
'\\' && *(p + 1) ==
'n' )
1234 else if( *p ==
'\\' && *(p + 1) ==
':' )
1239 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1255 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1256 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.des.data";
1261 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1262 unsigned char alloc_buf[1000000];
1263 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1266 file = fopen( filename,
"r" );
1269 fprintf( stderr,
"Failed to open\n" );
1273 while( !feof( file ) )
1277 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1279 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1280 fprintf( stdout,
" " );
1281 for( i = strlen( buf ) + 1; i < 67; i++ )
1282 fprintf( stdout,
"." );
1283 fprintf( stdout,
" " );
1288 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1292 if( strcmp( params[0],
"depends_on" ) == 0 )
1294 for( i = 1; i < cnt; i++ )
1298 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1309 if( skip == 1 || ret == 3 )
1312 fprintf( stdout,
"----\n" );
1315 else if( ret == 0 && test_errors == 0 )
1317 fprintf( stdout,
"PASS\n" );
1322 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1329 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1331 if( strlen(buf) != 0 )
1333 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1339 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1340 if( total_errors == 0 )
1341 fprintf( stdout,
"PASSED" );
1343 fprintf( stdout,
"FAILED" );
1345 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1346 total_tests - total_errors, total_tests, total_skipped );
1348 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1349 #if defined(POLARSSL_MEMORY_DEBUG)
1350 memory_buffer_alloc_status();
1352 memory_buffer_alloc_free();
1355 return( total_errors != 0 );
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
static int unhexify(unsigned char *obuf, const char *ibuf)
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.
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Configuration options (set of defines)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
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_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
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.
#define PUT_UINT32_BE(n, b, i)
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
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 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.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
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)