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_ARC4_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_arc4_crypt(
char *hex_src_string,
char *hex_key_string,
401 char *hex_dst_string )
403 unsigned char src_str[1000];
404 unsigned char key_str[1000];
405 unsigned char dst_str[1000];
406 unsigned char dst_hexstr[2000];
407 int src_len, key_len;
410 memset(src_str, 0x00, 1000);
411 memset(key_str, 0x00, 1000);
412 memset(dst_str, 0x00, 1000);
413 memset(dst_hexstr, 0x00, 2000);
416 src_len =
unhexify( src_str, hex_src_string );
417 key_len =
unhexify( key_str, hex_key_string );
421 hexify( dst_hexstr, dst_str, src_len );
423 TEST_ASSERT( strcmp( (
char *) dst_hexstr, hex_dst_string ) == 0 );
429 #ifdef POLARSSL_SELF_TEST
430 void test_suite_arc4_selftest()
448 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
450 #if defined(POLARSSL_SELF_TEST)
467 #if defined(TEST_SUITE_ACTIVE)
468 if( strcmp( params[0],
"arc4_crypt" ) == 0 )
471 char *param1 = params[1];
472 char *param2 = params[2];
473 char *param3 = params[3];
477 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
485 test_suite_arc4_crypt( param1, param2, param3 );
491 if( strcmp( params[0],
"arc4_selftest" ) == 0 )
493 #ifdef POLARSSL_SELF_TEST
498 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
503 test_suite_arc4_selftest( );
512 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
526 ret = fgets( buf, len, f );
530 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
531 buf[strlen(buf) - 1] =
'\0';
532 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
533 buf[strlen(buf) - 1] =
'\0';
546 while( *p !=
'\0' && p < buf + len )
556 if( p + 1 < buf + len )
568 for( i = 0; i < cnt; i++ )
575 if( *p ==
'\\' && *(p + 1) ==
'n' )
580 else if( *p ==
'\\' && *(p + 1) ==
':' )
585 else if( *p ==
'\\' && *(p + 1) ==
'?' )
601 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
602 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_arc4.data";
607 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
608 unsigned char alloc_buf[1000000];
612 file = fopen( filename,
"r" );
615 fprintf( stderr,
"Failed to open\n" );
619 while( !feof( file ) )
623 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
625 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
626 fprintf( stdout,
" " );
627 for( i = strlen( buf ) + 1; i < 67; i++ )
628 fprintf( stdout,
"." );
629 fprintf( stdout,
" " );
634 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
638 if( strcmp( params[0],
"depends_on" ) == 0 )
640 for( i = 1; i < cnt; i++ )
644 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
655 if( skip == 1 || ret == 3 )
658 fprintf( stdout,
"----\n" );
663 fprintf( stdout,
"PASS\n" );
668 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
675 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
677 if( strlen(buf) != 0 )
679 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
685 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
686 if( total_errors == 0 )
687 fprintf( stdout,
"PASSED" );
689 fprintf( stdout,
"FAILED" );
691 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
692 total_tests - total_errors, total_tests, total_skipped );
694 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
695 #if defined(POLARSSL_MEMORY_DEBUG)
696 memory_buffer_alloc_status();
701 return( total_errors != 0 );
Memory allocation layer (Deprecated to platform layer)
int arc4_crypt(arc4_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
ARC4 cipher function.
void arc4_init(arc4_context *ctx)
Initialize ARC4 context.
void arc4_setup(arc4_context *ctx, const unsigned char *key, unsigned int keylen)
ARC4 key schedule.
Info structure for the pseudo random function.
#define PUT_UINT32_BE(n, b, i)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int dispatch_test(int cnt, char *params[50])
int arc4_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
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 test_assert(int correct, const char *test)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
void arc4_free(arc4_context *ctx)
Clear ARC4 context.
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 * zero_alloc(size_t len)
Allocate and zeroize a buffer.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int verify_string(char **str)
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 get_line(FILE *f, char *buf, size_t len)
int verify_int(char *str, int *value)
The ARCFOUR stream cipher.
int parse_arguments(char *buf, size_t len, char *params[50])