4 #ifdef POLARSSL_BIGNUM_C
11 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 typedef UINT32 uint32_t;
30 #define GET_UINT32_BE(n,b,i) \
32 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
33 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
34 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
35 | ( (uint32_t) (b)[(i) + 3] ); \
40 #define PUT_UINT32_BE(n,b,i) \
42 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
43 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
44 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
45 (b)[(i) + 3] = (unsigned char) ( (n) ); \
49 static int unhexify(
unsigned char *obuf,
const char *ibuf)
52 int len = strlen(ibuf) / 2;
53 assert(!(strlen(ibuf) %1));
58 if( c >=
'0' && c <=
'9' )
60 else if( c >=
'a' && c <=
'f' )
62 else if( c >=
'A' && c <=
'F' )
68 if( c2 >=
'0' && c2 <=
'9' )
70 else if( c2 >=
'a' && c2 <=
'f' )
72 else if( c2 >=
'A' && c2 <=
'F' )
77 *obuf++ = ( c << 4 ) | c2;
83 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
95 *obuf++ =
'a' + h - 10;
100 *obuf++ =
'a' + l - 10;
116 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
120 if( rng_state != NULL )
123 for( i = 0; i < len; ++i )
134 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
136 if( rng_state != NULL )
139 memset( output, 0, len );
166 if( rng_state == NULL )
175 memcpy( output, info->
buf, use_len );
176 info->
buf += use_len;
180 if( len - use_len > 0 )
181 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
210 uint32_t i, *k, sum, delta=0x9E3779B9;
211 unsigned char result[4];
213 if( rng_state == NULL )
220 size_t use_len = ( len > 4 ) ? 4 : len;
223 for( i = 0; i < 32; i++ )
225 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
227 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
231 memcpy( output, result, use_len );
247 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
250 const char *ibuf = in;
252 assert( len == strlen(ibuf) / 2 );
253 assert(!(strlen(ibuf) %1));
255 obuf = out + (len - 1);
259 if( c >=
'0' && c <=
'9' )
261 else if( c >=
'a' && c <=
'f' )
263 else if( c >=
'A' && c <=
'F' )
269 if( c2 >=
'0' && c2 <=
'9' )
271 else if( c2 >=
'a' && c2 <=
'f' )
273 else if( c2 >=
'A' && c2 <=
'F' )
278 *obuf-- = ( c << 4 ) | c2;
290 #ifdef POLARSSL_DHM_C
291 #ifdef POLARSSL_BIGNUM_C
293 #define TEST_SUITE_ACTIVE
302 printf(
"FAILED\n" );
303 printf(
" %s\n", test );
308 #define TEST_ASSERT( TEST ) \
309 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
310 if( test_errors) return; \
315 if( (*str)[0] !=
'"' ||
316 (*str)[strlen( *str ) - 1] !=
'"' )
318 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
323 (*str)[strlen( *str ) - 1] =
'\0';
335 for( i = 0; i < strlen( str ); i++ )
337 if( i == 0 && str[i] ==
'-' )
343 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
344 str[i - 1] ==
'0' && str[i] ==
'x' )
350 if( str[i] <
'0' || str[i] >
'9' )
360 *value = strtol( str, NULL, 16 );
362 *value = strtol( str, NULL, 10 );
369 printf(
"Expected integer for parameter and got: %s\n", str );
373 void test_suite_dhm_do_dhm(
int radix_P,
char *input_P,
374 int radix_G,
char *input_G )
378 unsigned char ske[1000];
379 unsigned char *p = ske;
380 unsigned char pub_cli[1000];
381 unsigned char sec_srv[1000];
382 unsigned char sec_cli[1000];
384 size_t pub_cli_len = 0;
385 size_t sec_srv_len = 1000;
386 size_t sec_cli_len = 1000;
392 memset( ske, 0x00, 1000 );
393 memset( pub_cli, 0x00, 1000 );
394 memset( sec_srv, 0x00, 1000 );
395 memset( sec_cli, 0x00, 1000 );
404 pub_cli_len = x_size;
422 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
425 for( i = 0; i < 3; i++ )
432 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
455 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
461 #ifdef POLARSSL_SELF_TEST
462 void test_suite_dhm_selftest()
489 #if defined(TEST_SUITE_ACTIVE)
490 if( strcmp( params[0],
"dhm_do_dhm" ) == 0 )
494 char *param2 = params[2];
496 char *param4 = params[4];
500 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
504 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
506 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
509 test_suite_dhm_do_dhm( param1, param2, param3, param4 );
515 if( strcmp( params[0],
"dhm_selftest" ) == 0 )
517 #ifdef POLARSSL_SELF_TEST
522 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
527 test_suite_dhm_selftest( );
536 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
550 ret = fgets( buf, len, f );
554 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
555 buf[strlen(buf) - 1] =
'\0';
556 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
557 buf[strlen(buf) - 1] =
'\0';
570 while( *p !=
'\0' && p < buf + len )
580 if( p + 1 < buf + len )
592 for( i = 0; i < cnt; i++ )
599 if( *p ==
'\\' && *(p + 1) ==
'n' )
604 else if( *p ==
'\\' && *(p + 1) ==
':' )
609 else if( *p ==
'\\' && *(p + 1) ==
'?' )
625 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
626 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_dhm.data";
631 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
632 unsigned char alloc_buf[1000000];
633 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
636 file = fopen( filename,
"r" );
639 fprintf( stderr,
"Failed to open\n" );
643 while( !feof( file ) )
647 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
649 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
650 fprintf( stdout,
" " );
651 for( i = strlen( buf ) + 1; i < 67; i++ )
652 fprintf( stdout,
"." );
653 fprintf( stdout,
" " );
658 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
662 if( strcmp( params[0],
"depends_on" ) == 0 )
664 for( i = 1; i < cnt; i++ )
668 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
679 if( skip == 1 || ret == 3 )
682 fprintf( stdout,
"----\n" );
687 fprintf( stdout,
"PASS\n" );
692 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
699 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
701 if( strlen(buf) != 0 )
703 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
709 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
710 if( total_errors == 0 )
711 fprintf( stdout,
"PASSED" );
713 fprintf( stdout,
"FAILED" );
715 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
716 total_tests - total_errors, total_tests, total_skipped );
718 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
719 #if defined(POLARSSL_MEMORY_DEBUG)
720 memory_buffer_alloc_status();
722 memory_buffer_alloc_free();
725 return( total_errors != 0 );
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
Info structure for the pseudo random function.
int dhm_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define TEST_ASSERT(TEST)
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
#define PUT_UINT32_BE(n, b, i)
Diffie-Hellman-Merkle key exchange.
int parse_arguments(char *buf, size_t len, char *params[50])
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.
int dispatch_test(int cnt, char *params[50])
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 the components of a DHM key.
int verify_int(char *str, int *value)
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
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 get_line(FILE *f, char *buf, size_t len)
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.