1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_CAMELLIA_C
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_CAMELLIA_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_ERR_CAMELLIA_INVALID_KEY_LENGTH" ) == 0 )
399 #ifdef POLARSSL_CIPHER_MODE_CBC
400 if( strcmp( str,
"POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH" ) == 0 )
405 #endif // POLARSSL_CIPHER_MODE_CBC
408 printf(
"Expected integer for parameter and got: %s\n", str );
412 void test_suite_camellia_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
413 char *hex_dst_string,
int setkey_result )
415 unsigned char key_str[100];
416 unsigned char src_str[100];
417 unsigned char dst_str[100];
418 unsigned char output[100];
422 memset(key_str, 0x00, 100);
423 memset(src_str, 0x00, 100);
424 memset(dst_str, 0x00, 100);
425 memset(output, 0x00, 100);
428 key_len =
unhexify( key_str, hex_key_string );
429 unhexify( src_str, hex_src_string );
432 if( setkey_result == 0 )
435 hexify( dst_str, output, 16 );
437 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
444 void test_suite_camellia_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
445 char *hex_dst_string,
int setkey_result )
447 unsigned char key_str[100];
448 unsigned char src_str[100];
449 unsigned char dst_str[100];
450 unsigned char output[100];
454 memset(key_str, 0x00, 100);
455 memset(src_str, 0x00, 100);
456 memset(dst_str, 0x00, 100);
457 memset(output, 0x00, 100);
460 key_len =
unhexify( key_str, hex_key_string );
461 unhexify( src_str, hex_src_string );
464 if( setkey_result == 0 )
467 hexify( dst_str, output, 16 );
469 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
476 #ifdef POLARSSL_CIPHER_MODE_CBC
477 void test_suite_camellia_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
478 char *hex_src_string,
char *hex_dst_string,
481 unsigned char key_str[100];
482 unsigned char iv_str[100];
483 unsigned char src_str[100];
484 unsigned char dst_str[100];
485 unsigned char output[100];
487 int key_len, data_len;
489 memset(key_str, 0x00, 100);
490 memset(iv_str, 0x00, 100);
491 memset(src_str, 0x00, 100);
492 memset(dst_str, 0x00, 100);
493 memset(output, 0x00, 100);
496 key_len =
unhexify( key_str, hex_key_string );
498 data_len =
unhexify( src_str, hex_src_string );
502 if( cbc_result == 0 )
504 hexify( dst_str, output, data_len );
506 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
514 #ifdef POLARSSL_CIPHER_MODE_CBC
515 void test_suite_camellia_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
516 char *hex_src_string,
char *hex_dst_string,
519 unsigned char key_str[100];
520 unsigned char iv_str[100];
521 unsigned char src_str[100];
522 unsigned char dst_str[100];
523 unsigned char output[100];
525 int key_len, data_len;
527 memset(key_str, 0x00, 100);
528 memset(iv_str, 0x00, 100);
529 memset(src_str, 0x00, 100);
530 memset(dst_str, 0x00, 100);
531 memset(output, 0x00, 100);
534 key_len =
unhexify( key_str, hex_key_string );
536 data_len =
unhexify( src_str, hex_src_string );
540 if( cbc_result == 0 )
542 hexify( dst_str, output, data_len );
544 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
552 #ifdef POLARSSL_CIPHER_MODE_CFB
553 void test_suite_camellia_encrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
554 char *hex_src_string,
char *hex_dst_string )
556 unsigned char key_str[100];
557 unsigned char iv_str[100];
558 unsigned char src_str[100];
559 unsigned char dst_str[100];
560 unsigned char output[100];
562 size_t iv_offset = 0;
565 memset(key_str, 0x00, 100);
566 memset(iv_str, 0x00, 100);
567 memset(src_str, 0x00, 100);
568 memset(dst_str, 0x00, 100);
569 memset(output, 0x00, 100);
572 key_len =
unhexify( key_str, hex_key_string );
574 unhexify( src_str, hex_src_string );
578 hexify( dst_str, output, 16 );
580 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
587 #ifdef POLARSSL_CIPHER_MODE_CFB
588 void test_suite_camellia_decrypt_cfb128(
char *hex_key_string,
char *hex_iv_string,
589 char *hex_src_string,
char *hex_dst_string )
591 unsigned char key_str[100];
592 unsigned char iv_str[100];
593 unsigned char src_str[100];
594 unsigned char dst_str[100];
595 unsigned char output[100];
597 size_t iv_offset = 0;
600 memset(key_str, 0x00, 100);
601 memset(iv_str, 0x00, 100);
602 memset(src_str, 0x00, 100);
603 memset(dst_str, 0x00, 100);
604 memset(output, 0x00, 100);
607 key_len =
unhexify( key_str, hex_key_string );
609 unhexify( src_str, hex_src_string );
613 hexify( dst_str, output, 16 );
615 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
622 #ifdef POLARSSL_SELF_TEST
623 void test_suite_camellia_selftest()
641 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
643 #if defined(POLARSSL_SELF_TEST)
649 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CFB" ) == 0 )
651 #if defined(POLARSSL_CIPHER_MODE_CFB)
668 #if defined(TEST_SUITE_ACTIVE)
669 if( strcmp( params[0],
"camellia_encrypt_ecb" ) == 0 )
672 char *param1 = params[1];
673 char *param2 = params[2];
674 char *param3 = params[3];
679 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
686 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
688 test_suite_camellia_encrypt_ecb( param1, param2, param3, param4 );
694 if( strcmp( params[0],
"camellia_decrypt_ecb" ) == 0 )
697 char *param1 = params[1];
698 char *param2 = params[2];
699 char *param3 = params[3];
704 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
711 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
713 test_suite_camellia_decrypt_ecb( param1, param2, param3, param4 );
719 if( strcmp( params[0],
"camellia_encrypt_cbc" ) == 0 )
721 #ifdef POLARSSL_CIPHER_MODE_CBC
723 char *param1 = params[1];
724 char *param2 = params[2];
725 char *param3 = params[3];
726 char *param4 = params[4];
731 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
739 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
741 test_suite_camellia_encrypt_cbc( param1, param2, param3, param4, param5 );
748 if( strcmp( params[0],
"camellia_decrypt_cbc" ) == 0 )
750 #ifdef POLARSSL_CIPHER_MODE_CBC
752 char *param1 = params[1];
753 char *param2 = params[2];
754 char *param3 = params[3];
755 char *param4 = params[4];
760 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
768 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
770 test_suite_camellia_decrypt_cbc( param1, param2, param3, param4, param5 );
777 if( strcmp( params[0],
"camellia_encrypt_cfb128" ) == 0 )
779 #ifdef POLARSSL_CIPHER_MODE_CFB
781 char *param1 = params[1];
782 char *param2 = params[2];
783 char *param3 = params[3];
784 char *param4 = params[4];
788 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
797 test_suite_camellia_encrypt_cfb128( param1, param2, param3, param4 );
804 if( strcmp( params[0],
"camellia_decrypt_cfb128" ) == 0 )
806 #ifdef POLARSSL_CIPHER_MODE_CFB
808 char *param1 = params[1];
809 char *param2 = params[2];
810 char *param3 = params[3];
811 char *param4 = params[4];
815 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
824 test_suite_camellia_decrypt_cfb128( param1, param2, param3, param4 );
831 if( strcmp( params[0],
"camellia_selftest" ) == 0 )
833 #ifdef POLARSSL_SELF_TEST
838 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
843 test_suite_camellia_selftest( );
852 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
866 ret = fgets( buf, len, f );
870 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
871 buf[strlen(buf) - 1] =
'\0';
872 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
873 buf[strlen(buf) - 1] =
'\0';
886 while( *p !=
'\0' && p < buf + len )
896 if( p + 1 < buf + len )
908 for( i = 0; i < cnt; i++ )
915 if( *p ==
'\\' && *(p + 1) ==
'n' )
920 else if( *p ==
'\\' && *(p + 1) ==
':' )
925 else if( *p ==
'\\' && *(p + 1) ==
'?' )
941 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
942 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_camellia.data";
947 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
948 unsigned char alloc_buf[1000000];
952 file = fopen( filename,
"r" );
955 fprintf( stderr,
"Failed to open\n" );
959 while( !feof( file ) )
963 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
965 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
966 fprintf( stdout,
" " );
967 for( i = strlen( buf ) + 1; i < 67; i++ )
968 fprintf( stdout,
"." );
969 fprintf( stdout,
" " );
974 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
978 if( strcmp( params[0],
"depends_on" ) == 0 )
980 for( i = 1; i < cnt; i++ )
984 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
995 if( skip == 1 || ret == 3 )
998 fprintf( stdout,
"----\n" );
1003 fprintf( stdout,
"PASS\n" );
1008 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1015 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1017 if( strlen(buf) != 0 )
1019 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1025 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1026 if( total_errors == 0 )
1027 fprintf( stdout,
"PASSED" );
1029 fprintf( stdout,
"FAILED" );
1031 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1032 total_tests - total_errors, total_tests, total_skipped );
1034 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1035 #if defined(POLARSSL_MEMORY_DEBUG)
1036 memory_buffer_alloc_status();
1041 return( total_errors != 0 );
Memory allocation layer (Deprecated to platform layer)
int camellia_crypt_cbc(camellia_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int get_line(FILE *f, char *buf, size_t len)
Configuration options (set of defines)
int camellia_setkey_enc(camellia_context *ctx, const unsigned char *key, unsigned int keysize)
CAMELLIA key schedule (encryption)
static int test_assert(int correct, const char *test)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int camellia_self_test(int verbose)
Checkup routine.
int camellia_crypt_cfb128(camellia_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CFB128 buffer encryption/decryption.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
void camellia_init(camellia_context *ctx)
Initialize CAMELLIA context.
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
int parse_arguments(char *buf, size_t len, char *params[50])
#define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Invalid data input length.
CAMELLIA context structure.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
void camellia_free(camellia_context *ctx)
Clear CAMELLIA context.
int camellia_crypt_ecb(camellia_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
CAMELLIA-ECB block encryption/decryption.
int camellia_setkey_dec(camellia_context *ctx, const unsigned char *key, unsigned int keysize)
CAMELLIA key schedule (decryption)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
static int unhexify(unsigned char *obuf, const char *ibuf)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_int(char *str, int *value)
#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Invalid key length.
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 rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.