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_XTEA_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 );
396 printf(
"Expected integer for parameter and got: %s\n", str );
400 void test_suite_xtea_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
401 char *hex_dst_string )
403 unsigned char key_str[100];
404 unsigned char src_str[100];
405 unsigned char dst_str[100];
406 unsigned char output[100];
409 memset(key_str, 0x00, 100);
410 memset(src_str, 0x00, 100);
411 memset(dst_str, 0x00, 100);
412 memset(output, 0x00, 100);
414 unhexify( key_str, hex_key_string );
415 unhexify( src_str, hex_src_string );
419 hexify( dst_str, output, 8 );
421 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
427 void test_suite_xtea_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
428 char *hex_dst_string )
430 unsigned char key_str[100];
431 unsigned char src_str[100];
432 unsigned char dst_str[100];
433 unsigned char output[100];
436 memset(key_str, 0x00, 100);
437 memset(src_str, 0x00, 100);
438 memset(dst_str, 0x00, 100);
439 memset(output, 0x00, 100);
441 unhexify( key_str, hex_key_string );
442 unhexify( src_str, hex_src_string );
446 hexify( dst_str, output, 8 );
448 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
454 void test_suite_xtea_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
455 char *hex_src_string,
char *hex_dst_string )
457 unsigned char key_str[100];
458 unsigned char src_str[100];
459 unsigned char dst_str[100];
460 unsigned char iv_str[100];
461 unsigned char output[100];
465 memset(key_str, 0x00, 100);
466 memset(src_str, 0x00, 100);
467 memset(dst_str, 0x00, 100);
468 memset(iv_str, 0x00, 100);
469 memset(output, 0x00, 100);
471 unhexify( key_str, hex_key_string );
473 len =
unhexify( src_str, hex_src_string );
477 src_str, output ) == 0 );
478 hexify( dst_str, output, len );
480 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
486 void test_suite_xtea_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
487 char *hex_src_string,
char *hex_dst_string )
489 unsigned char key_str[100];
490 unsigned char src_str[100];
491 unsigned char dst_str[100];
492 unsigned char iv_str[100];
493 unsigned char output[100];
497 memset(key_str, 0x00, 100);
498 memset(src_str, 0x00, 100);
499 memset(dst_str, 0x00, 100);
500 memset(iv_str, 0x00, 100);
501 memset(output, 0x00, 100);
503 unhexify( key_str, hex_key_string );
505 len =
unhexify( src_str, hex_src_string );
509 src_str, output ) == 0 );
510 hexify( dst_str, output, len );
512 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
518 #ifdef POLARSSL_SELF_TEST
519 void test_suite_xtea_selftest()
537 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
539 #if defined(POLARSSL_SELF_TEST)
556 #if defined(TEST_SUITE_ACTIVE)
557 if( strcmp( params[0],
"xtea_encrypt_ecb" ) == 0 )
560 char *param1 = params[1];
561 char *param2 = params[2];
562 char *param3 = params[3];
566 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
574 test_suite_xtea_encrypt_ecb( param1, param2, param3 );
580 if( strcmp( params[0],
"xtea_decrypt_ecb" ) == 0 )
583 char *param1 = params[1];
584 char *param2 = params[2];
585 char *param3 = params[3];
589 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
597 test_suite_xtea_decrypt_ecb( param1, param2, param3 );
603 if( strcmp( params[0],
"xtea_encrypt_cbc" ) == 0 )
606 char *param1 = params[1];
607 char *param2 = params[2];
608 char *param3 = params[3];
609 char *param4 = params[4];
613 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
622 test_suite_xtea_encrypt_cbc( param1, param2, param3, param4 );
628 if( strcmp( params[0],
"xtea_decrypt_cbc" ) == 0 )
631 char *param1 = params[1];
632 char *param2 = params[2];
633 char *param3 = params[3];
634 char *param4 = params[4];
638 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
647 test_suite_xtea_decrypt_cbc( param1, param2, param3, param4 );
653 if( strcmp( params[0],
"xtea_selftest" ) == 0 )
655 #ifdef POLARSSL_SELF_TEST
660 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
665 test_suite_xtea_selftest( );
674 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
688 ret = fgets( buf, len, f );
692 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
693 buf[strlen(buf) - 1] =
'\0';
694 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
695 buf[strlen(buf) - 1] =
'\0';
708 while( *p !=
'\0' && p < buf + len )
718 if( p + 1 < buf + len )
730 for( i = 0; i < cnt; i++ )
737 if( *p ==
'\\' && *(p + 1) ==
'n' )
742 else if( *p ==
'\\' && *(p + 1) ==
':' )
747 else if( *p ==
'\\' && *(p + 1) ==
'?' )
763 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
764 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_xtea.data";
769 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
770 unsigned char alloc_buf[1000000];
774 file = fopen( filename,
"r" );
777 fprintf( stderr,
"Failed to open\n" );
781 while( !feof( file ) )
785 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
787 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
788 fprintf( stdout,
" " );
789 for( i = strlen( buf ) + 1; i < 67; i++ )
790 fprintf( stdout,
"." );
791 fprintf( stdout,
" " );
796 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
800 if( strcmp( params[0],
"depends_on" ) == 0 )
802 for( i = 1; i < cnt; i++ )
806 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
817 if( skip == 1 || ret == 3 )
820 fprintf( stdout,
"----\n" );
825 fprintf( stdout,
"PASS\n" );
830 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
837 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
839 if( strlen(buf) != 0 )
841 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
847 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
848 if( total_errors == 0 )
849 fprintf( stdout,
"PASSED" );
851 fprintf( stdout,
"FAILED" );
853 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
854 total_tests - total_errors, total_tests, total_skipped );
856 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
857 #if defined(POLARSSL_MEMORY_DEBUG)
858 memory_buffer_alloc_status();
863 return( total_errors != 0 );
Memory allocation layer (Deprecated to platform layer)
#define PUT_UINT32_BE(n, b, i)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static int unhexify(unsigned char *obuf, const char *ibuf)
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
int xtea_crypt_ecb(xtea_context *ctx, int mode, const unsigned char input[8], unsigned char output[8])
XTEA cipher function.
int main(int argc, char *argv[])
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
void xtea_setup(xtea_context *ctx, const unsigned char key[16])
XTEA key schedule.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int 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.
XTEA block cipher (32-bit)
int parse_arguments(char *buf, size_t len, char *params[50])
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_string(char **str)
int xtea_crypt_cbc(xtea_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
XTEA CBC cipher function.
int dispatch_test(int cnt, char *params[50])
int verify_int(char *str, int *value)
int xtea_self_test(int verbose)
Checkup routine.
int get_line(FILE *f, char *buf, size_t len)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.