1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
8 #ifdef POLARSSL_BIGNUM_C
15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19 #if defined(POLARSSL_PLATFORM_C)
22 #define polarssl_malloc malloc
23 #define polarssl_free free
28 typedef UINT32 uint32_t;
41 #define GET_UINT32_BE(n,b,i) \
43 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
44 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
45 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
46 | ( (uint32_t) (b)[(i) + 3] ); \
51 #define PUT_UINT32_BE(n,b,i) \
53 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
54 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
55 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
56 (b)[(i) + 3] = (unsigned char) ( (n) ); \
60 static int unhexify(
unsigned char *obuf,
const char *ibuf)
63 int len = strlen(ibuf) / 2;
64 assert(!(strlen(ibuf) %1));
69 if( c >=
'0' && c <=
'9' )
71 else if( c >=
'a' && c <=
'f' )
73 else if( c >=
'A' && c <=
'F' )
79 if( c2 >=
'0' && c2 <=
'9' )
81 else if( c2 >=
'a' && c2 <=
'f' )
83 else if( c2 >=
'A' && c2 <=
'F' )
88 *obuf++ = ( c << 4 ) | c2;
94 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
106 *obuf++ =
'a' + h - 10;
111 *obuf++ =
'a' + l - 10;
128 size_t actual_len = len != 0 ? len : 1;
133 memset( p, 0x00, actual_len );
152 *olen = strlen(ibuf) / 2;
158 assert( obuf != NULL );
174 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
176 #if !defined(__OpenBSD__)
179 if( rng_state != NULL )
182 for( i = 0; i < len; ++i )
185 if( rng_state != NULL )
188 arc4random_buf( output, len );
199 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
201 if( rng_state != NULL )
204 memset( output, 0, len );
231 if( rng_state == NULL )
240 memcpy( output, info->
buf, use_len );
241 info->
buf += use_len;
245 if( len - use_len > 0 )
246 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
275 uint32_t i, *k, sum, delta=0x9E3779B9;
276 unsigned char result[4], *out = output;
278 if( rng_state == NULL )
285 size_t use_len = ( len > 4 ) ? 4 : len;
288 for( i = 0; i < 32; i++ )
290 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
292 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
296 memcpy( out, result, use_len );
308 #if defined(POLARSSL_PLATFORM_C)
311 #define polarssl_printf printf
312 #define polarssl_malloc malloc
313 #define polarssl_free free
318 #ifdef POLARSSL_DHM_C
319 #ifdef POLARSSL_BIGNUM_C
321 #define TEST_SUITE_ACTIVE
323 static int test_assert(
int correct,
const char *test )
330 printf(
"FAILED\n" );
331 printf(
" %s\n", test );
336 #define TEST_ASSERT( TEST ) \
337 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
338 if( test_errors) goto exit; \
343 if( (*str)[0] !=
'"' ||
344 (*str)[strlen( *str ) - 1] !=
'"' )
346 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
351 (*str)[strlen( *str ) - 1] =
'\0';
363 for( i = 0; i < strlen( str ); i++ )
365 if( i == 0 && str[i] ==
'-' )
371 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
372 str[i - 1] ==
'0' && str[i] ==
'x' )
378 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
379 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
380 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
390 *value = strtol( str, NULL, 16 );
392 *value = strtol( str, NULL, 10 );
399 printf(
"Expected integer for parameter and got: %s\n", str );
403 void test_suite_dhm_do_dhm(
int radix_P,
char *input_P,
404 int radix_G,
char *input_G )
408 unsigned char ske[1000];
409 unsigned char *p = ske;
410 unsigned char pub_cli[1000];
411 unsigned char sec_srv[1000];
412 unsigned char sec_cli[1000];
414 size_t pub_cli_len = 0;
415 size_t sec_srv_len = 1000;
416 size_t sec_cli_len = 1000;
422 memset( ske, 0x00, 1000 );
423 memset( pub_cli, 0x00, 1000 );
424 memset( sec_srv, 0x00, 1000 );
425 memset( sec_cli, 0x00, 1000 );
434 pub_cli_len = x_size;
452 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
455 for( i = 0; i < 3; i++ )
462 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
485 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
492 #ifdef POLARSSL_FS_IO
493 void test_suite_dhm_file(
char *filename,
char *p,
char *g,
int len )
516 #ifdef POLARSSL_SELF_TEST
517 void test_suite_dhm_selftest()
547 #if defined(TEST_SUITE_ACTIVE)
548 if( strcmp( params[0],
"dhm_do_dhm" ) == 0 )
552 char *param2 = params[2];
554 char *param4 = params[4];
558 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
562 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
564 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
567 test_suite_dhm_do_dhm( param1, param2, param3, param4 );
573 if( strcmp( params[0],
"dhm_file" ) == 0 )
575 #ifdef POLARSSL_FS_IO
577 char *param1 = params[1];
578 char *param2 = params[2];
579 char *param3 = params[3];
584 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
591 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
593 test_suite_dhm_file( param1, param2, param3, param4 );
600 if( strcmp( params[0],
"dhm_selftest" ) == 0 )
602 #ifdef POLARSSL_SELF_TEST
607 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
612 test_suite_dhm_selftest( );
621 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
635 ret = fgets( buf, len, f );
639 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
640 buf[strlen(buf) - 1] =
'\0';
641 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
642 buf[strlen(buf) - 1] =
'\0';
655 while( *p !=
'\0' && p < buf + len )
665 if( p + 1 < buf + len )
677 for( i = 0; i < cnt; i++ )
684 if( *p ==
'\\' && *(p + 1) ==
'n' )
689 else if( *p ==
'\\' && *(p + 1) ==
':' )
694 else if( *p ==
'\\' && *(p + 1) ==
'?' )
710 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
711 const char *filename =
"/tmp/B.alu7eg1d/BUILD/polarssl-1.3.9/tests/suites/test_suite_dhm.data";
716 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
717 unsigned char alloc_buf[1000000];
721 file = fopen( filename,
"r" );
724 fprintf( stderr,
"Failed to open\n" );
728 while( !feof( file ) )
732 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
734 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
735 fprintf( stdout,
" " );
736 for( i = strlen( buf ) + 1; i < 67; i++ )
737 fprintf( stdout,
"." );
738 fprintf( stdout,
" " );
743 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
747 if( strcmp( params[0],
"depends_on" ) == 0 )
749 for( i = 1; i < cnt; i++ )
753 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
764 if( skip == 1 || ret == 3 )
767 fprintf( stdout,
"----\n" );
772 fprintf( stdout,
"PASS\n" );
777 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
784 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
786 if( strlen(buf) != 0 )
788 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
794 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
795 if( total_errors == 0 )
796 fprintf( stdout,
"PASSED" );
798 fprintf( stdout,
"FAILED" );
800 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
801 total_tests - total_errors, total_tests, total_skipped );
803 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
804 #if defined(POLARSSL_MEMORY_DEBUG)
805 memory_buffer_alloc_status();
810 return( total_errors != 0 );
int dispatch_test(int cnt, char *params[50])
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
Memory allocation layer (Deprecated to platform layer)
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
int get_line(FILE *f, char *buf, size_t len)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int dhm_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
void mpi_init(mpi *X)
Initialize one MPI.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
void dhm_init(dhm_context *ctx)
Initialize DHM context.
void mpi_free(mpi *X)
Unallocate one MPI.
#define TEST_ASSERT(TEST)
Diffie-Hellman-Merkle key exchange.
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 mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int verify_string(char **str)
int dhm_make_public(dhm_context *ctx, int x_size, unsigned char *output, size_t olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Create own private value X and export G^X.
size_t mpi_size(const mpi *X)
Return the total size in bytes.
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().
void dhm_free(dhm_context *ctx)
Free and clear the components of a DHM key.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_int(char *str, int *value)
int dhm_parse_dhmfile(dhm_context *dhm, const char *path)
Load and parse DHM parameters.
int dhm_make_params(dhm_context *ctx, int x_size, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and write the ServerKeyExchange parameters.
int dhm_read_public(dhm_context *ctx, const unsigned char *input, size_t ilen)
Import the peer's public value G^Y.
int dhm_calc_secret(dhm_context *ctx, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret (G^Y)^X mod P.