1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(POLARSSL_PLATFORM_C)
20 #define polarssl_malloc malloc
21 #define polarssl_free free
26 typedef UINT32 uint32_t;
39 #define GET_UINT32_BE(n,b,i) \
41 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44 | ( (uint32_t) (b)[(i) + 3] ); \
49 #define PUT_UINT32_BE(n,b,i) \
51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54 (b)[(i) + 3] = (unsigned char) ( (n) ); \
58 static int unhexify(
unsigned char *obuf,
const char *ibuf)
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1));
67 if( c >=
'0' && c <=
'9' )
69 else if( c >=
'a' && c <=
'f' )
71 else if( c >=
'A' && c <=
'F' )
77 if( c2 >=
'0' && c2 <=
'9' )
79 else if( c2 >=
'a' && c2 <=
'f' )
81 else if( c2 >=
'A' && c2 <=
'F' )
86 *obuf++ = ( c << 4 ) | c2;
92 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
104 *obuf++ =
'a' + h - 10;
109 *obuf++ =
'a' + l - 10;
126 size_t actual_len = len != 0 ? len : 1;
131 memset( p, 0x00, actual_len );
150 *olen = strlen(ibuf) / 2;
156 assert( obuf != NULL );
172 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
174 #if !defined(__OpenBSD__)
177 if( rng_state != NULL )
180 for( i = 0; i < len; ++i )
183 if( rng_state != NULL )
186 arc4random_buf( output, len );
197 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
199 if( rng_state != NULL )
202 memset( output, 0, len );
229 if( rng_state == NULL )
238 memcpy( output, info->
buf, use_len );
239 info->
buf += use_len;
243 if( len - use_len > 0 )
244 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
276 if( rng_state == NULL )
283 size_t use_len = ( len > 4 ) ? 4 : len;
286 for( i = 0; i < 32; i++ )
288 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
290 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
294 memcpy( out, result, use_len );
306 #if defined(POLARSSL_PLATFORM_C)
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
316 #ifdef POLARSSL_CCM_C
318 #define TEST_SUITE_ACTIVE
320 static int test_assert(
int correct,
const char *test )
327 printf(
"FAILED\n" );
328 printf(
" %s\n", test );
333 #define TEST_ASSERT( TEST ) \
334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335 if( test_errors) goto exit; \
340 if( (*str)[0] !=
'"' ||
341 (*str)[strlen( *str ) - 1] !=
'"' )
343 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
348 (*str)[strlen( *str ) - 1] =
'\0';
360 for( i = 0; i < strlen( str ); i++ )
362 if( i == 0 && str[i] ==
'-' )
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] ==
'0' && str[i] ==
'x' )
375 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
376 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
377 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
387 *value = strtol( str, NULL, 16 );
389 *value = strtol( str, NULL, 10 );
394 if( strcmp( str,
"POLARSSL_CIPHER_ID_AES" ) == 0 )
399 if( strcmp( str,
"POLARSSL_ERR_CCM_BAD_INPUT" ) == 0 )
404 if( strcmp( str,
"POLARSSL_CIPHER_ID_BLOWFISH" ) == 0 )
409 if( strcmp( str,
"POLARSSL_CIPHER_ID_CAMELLIA" ) == 0 )
416 printf(
"Expected integer for parameter and got: %s\n", str );
420 #ifdef POLARSSL_SELF_TEST
421 #ifdef POLARSSL_AES_C
422 void test_suite_ccm_self_test( )
432 void test_suite_ccm_init(
int cipher_id,
int key_size,
int result )
435 unsigned char key[32];
438 memset( key, 0x2A,
sizeof( key ) );
439 TEST_ASSERT( (
unsigned) key_size <= 8 *
sizeof( key ) );
441 ret =
ccm_init( &ctx, cipher_id, key, key_size );
448 #ifdef POLARSSL_AES_C
449 void test_suite_ccm_lengths(
int msg_len,
int iv_len,
int add_len,
int tag_len,
int res )
452 unsigned char key[16];
453 unsigned char msg[10];
454 unsigned char iv[14];
455 unsigned char add[10];
456 unsigned char out[10];
457 unsigned char tag[18];
460 memset( key, 0,
sizeof( key ) );
461 memset( msg, 0,
sizeof( msg ) );
462 memset( iv, 0,
sizeof( iv ) );
463 memset( add, 0,
sizeof( add ) );
464 memset( out, 0,
sizeof( out ) );
465 memset( tag, 0,
sizeof( tag ) );
468 key, 8 *
sizeof( key ) ) == 0 );
471 msg, out, tag, tag_len ) == res );
474 msg, out, tag, tag_len );
486 void test_suite_ccm_encrypt_and_tag(
int cipher_id,
487 char *key_hex,
char *msg_hex,
488 char *iv_hex,
char *add_hex,
491 unsigned char key[32];
492 unsigned char msg[50];
493 unsigned char iv[13];
494 unsigned char add[32];
495 unsigned char result[50];
497 size_t key_len, msg_len, iv_len, add_len, tag_len, result_len;
499 memset( key, 0x00,
sizeof( key ) );
500 memset( msg, 0x00,
sizeof( msg ) );
501 memset( iv, 0x00,
sizeof( iv ) );
502 memset( add, 0x00,
sizeof( add ) );
503 memset( result, 0x00,
sizeof( result ) );
509 result_len =
unhexify( result, result_hex );
510 tag_len = result_len - msg_len;
516 msg, msg, msg + msg_len, tag_len ) == 0 );
518 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
521 TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 );
527 void test_suite_ccm_auth_decrypt(
int cipher_id,
528 char *key_hex,
char *msg_hex,
529 char *iv_hex,
char *add_hex,
530 int tag_len,
char *result_hex )
532 unsigned char key[32];
533 unsigned char msg[50];
534 unsigned char iv[13];
535 unsigned char add[32];
536 unsigned char tag[16];
537 unsigned char result[50];
539 size_t key_len, msg_len, iv_len, add_len, result_len;
542 memset( key, 0x00,
sizeof( key ) );
543 memset( msg, 0x00,
sizeof( msg ) );
544 memset( iv, 0x00,
sizeof( iv ) );
545 memset( add, 0x00,
sizeof( add ) );
546 memset( tag, 0x00,
sizeof( tag ) );
547 memset( result, 0x00,
sizeof( result ) );
554 memcpy( tag, msg + msg_len, tag_len );
556 if( strcmp(
"FAIL", result_hex ) == 0 )
563 result_len =
unhexify( result, result_hex );
570 msg, msg, msg + msg_len, tag_len ) == ret );
574 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
580 for( i = 0; i < msg_len; i++ )
585 TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
600 if( strcmp( str,
"POLARSSL_CAMELLIA_C" ) == 0 )
602 #if defined(POLARSSL_CAMELLIA_C)
608 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
610 #if defined(POLARSSL_AES_C)
616 if( strcmp( str,
"POLARSSL_BLOWFISH_C" ) == 0 )
618 #if defined(POLARSSL_BLOWFISH_C)
635 #if defined(TEST_SUITE_ACTIVE)
636 if( strcmp( params[0],
"ccm_self_test" ) == 0 )
638 #ifdef POLARSSL_SELF_TEST
639 #ifdef POLARSSL_AES_C
644 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
649 test_suite_ccm_self_test( );
657 if( strcmp( params[0],
"ccm_init" ) == 0 )
666 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
670 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
671 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
672 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
674 test_suite_ccm_init( param1, param2, param3 );
680 if( strcmp( params[0],
"ccm_lengths" ) == 0 )
682 #ifdef POLARSSL_AES_C
692 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
696 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
697 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
698 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
699 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
700 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
702 test_suite_ccm_lengths( param1, param2, param3, param4, param5 );
709 if( strcmp( params[0],
"ccm_encrypt_and_tag" ) == 0 )
713 char *param2 = params[2];
714 char *param3 = params[3];
715 char *param4 = params[4];
716 char *param5 = params[5];
717 char *param6 = params[6];
721 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
725 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
732 test_suite_ccm_encrypt_and_tag( param1, param2, param3, param4, param5, param6 );
738 if( strcmp( params[0],
"ccm_auth_decrypt" ) == 0 )
742 char *param2 = params[2];
743 char *param3 = params[3];
744 char *param4 = params[4];
745 char *param5 = params[5];
747 char *param7 = params[7];
751 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
755 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
760 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
763 test_suite_ccm_auth_decrypt( param1, param2, param3, param4, param5, param6, param7 );
771 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
785 ret = fgets( buf, len, f );
789 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
790 buf[strlen(buf) - 1] =
'\0';
791 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
792 buf[strlen(buf) - 1] =
'\0';
805 while( *p !=
'\0' && p < buf + len )
815 if( p + 1 < buf + len )
827 for( i = 0; i < cnt; i++ )
834 if( *p ==
'\\' && *(p + 1) ==
'n' )
839 else if( *p ==
'\\' && *(p + 1) ==
':' )
844 else if( *p ==
'\\' && *(p + 1) ==
'?' )
860 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
861 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_ccm.data";
866 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
867 unsigned char alloc_buf[1000000];
871 file = fopen( filename,
"r" );
874 fprintf( stderr,
"Failed to open\n" );
878 while( !feof( file ) )
882 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
884 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
885 fprintf( stdout,
" " );
886 for( i = strlen( buf ) + 1; i < 67; i++ )
887 fprintf( stdout,
"." );
888 fprintf( stdout,
" " );
893 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
897 if( strcmp( params[0],
"depends_on" ) == 0 )
899 for( i = 1; i < cnt; i++ )
903 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
914 if( skip == 1 || ret == 3 )
917 fprintf( stdout,
"----\n" );
922 fprintf( stdout,
"PASS\n" );
927 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
934 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
936 if( strlen(buf) != 0 )
938 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
944 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
945 if( total_errors == 0 )
946 fprintf( stdout,
"PASSED" );
948 fprintf( stdout,
"FAILED" );
950 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
951 total_tests - total_errors, total_tests, total_skipped );
953 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
954 #if defined(POLARSSL_MEMORY_DEBUG)
955 memory_buffer_alloc_status();
960 return( total_errors != 0 );
int get_line(FILE *f, char *buf, size_t len)
#define POLARSSL_ERR_CCM_BAD_INPUT
Bad input parameters to function.
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])
Memory allocation layer (Deprecated to platform layer)
int dispatch_test(int cnt, char *params[50])
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
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 ccm_auth_decrypt(ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, const unsigned char *tag, size_t tag_len)
CCM buffer authenticated decryption.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int ccm_init(ccm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
CCM initialization (encryption and decryption)
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 ccm_self_test(int verbose)
Checkup routine.
#define PUT_UINT32_BE(n, b, i)
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void ccm_free(ccm_context *ctx)
Free a CCM context and underlying cipher sub-context.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int ccm_encrypt_and_tag(ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, unsigned char *tag, size_t tag_len)
CCM buffer encryption.
static int unhexify(unsigned char *obuf, const char *ibuf)
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)
#define POLARSSL_ERR_CCM_AUTH_FAILED
Authenticated decryption failed.
Counter with CBC-MAC (CCM) for 128-bit block ciphers.