6 #define WANT_NOT_RND_MPI
10 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #if defined(WANT_NOT_RND_MPI)
15 #if defined(POLARSSL_BIGNUM_C)
18 #error "not_rnd_mpi() need bignum.c"
24 typedef UINT32 uint32_t;
37 #define GET_UINT32_BE(n,b,i) \
39 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
40 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
41 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
42 | ( (uint32_t) (b)[(i) + 3] ); \
47 #define PUT_UINT32_BE(n,b,i) \
49 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
50 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
51 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
52 (b)[(i) + 3] = (unsigned char) ( (n) ); \
56 static int unhexify(
unsigned char *obuf,
const char *ibuf)
59 int len = strlen(ibuf) / 2;
60 assert(!(strlen(ibuf) %1));
65 if( c >=
'0' && c <=
'9' )
67 else if( c >=
'a' && c <=
'f' )
69 else if( c >=
'A' && c <=
'F' )
75 if( c2 >=
'0' && c2 <=
'9' )
77 else if( c2 >=
'a' && c2 <=
'f' )
79 else if( c2 >=
'A' && c2 <=
'F' )
84 *obuf++ = ( c << 4 ) | c2;
90 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
102 *obuf++ =
'a' + h - 10;
107 *obuf++ =
'a' + l - 10;
123 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
127 if( rng_state != NULL )
130 for( i = 0; i < len; ++i )
141 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
143 if( rng_state != NULL )
146 memset( output, 0, len );
173 if( rng_state == NULL )
182 memcpy( output, info->
buf, use_len );
183 info->
buf += use_len;
187 if( len - use_len > 0 )
188 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
217 uint32_t i, *k, sum, delta=0x9E3779B9;
218 unsigned char result[4];
220 if( rng_state == NULL )
227 size_t use_len = ( len > 4 ) ? 4 : len;
230 for( i = 0; i < 32; i++ )
232 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
234 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
238 memcpy( output, result, use_len );
245 #if defined(WANT_NOT_RND_MPI)
254 #define ciL (sizeof(t_uint))
255 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
256 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
258 char *str = (
char *) in;
267 X.
n = CHARS_TO_LIMBS( len );
273 assert( strlen( str ) / 2 == len );
285 #ifdef POLARSSL_ECDH_C
287 #define TEST_SUITE_ACTIVE
295 if( test_errors == 1 )
296 printf(
"FAILED\n" );
297 printf(
" %s\n", test );
302 #define TEST_ASSERT( TEST ) \
303 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
304 if( test_errors) return; \
309 if( (*str)[0] !=
'"' ||
310 (*str)[strlen( *str ) - 1] !=
'"' )
312 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
317 (*str)[strlen( *str ) - 1] =
'\0';
329 for( i = 0; i < strlen( str ); i++ )
331 if( i == 0 && str[i] ==
'-' )
337 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
338 str[i - 1] ==
'0' && str[i] ==
'x' )
344 if( str[i] <
'0' || str[i] >
'9' )
354 *value = strtol( str, NULL, 16 );
356 *value = strtol( str, NULL, 10 );
361 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1" ) == 0 )
366 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1" ) == 0 )
371 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
376 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1" ) == 0 )
381 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1" ) == 0 )
388 printf(
"Expected integer for parameter and got: %s\n", str );
392 void test_suite_ecdh_primitive_random(
int id )
424 void test_suite_ecdh_primitive_testvec(
int id,
char *dA_str,
char *xA_str,
char *yA_str,
425 char *dB_str,
char *xB_str,
char *yB_str,
430 mpi dA, dB, zA, zB, check;
465 void test_suite_ecdh_exchange(
int id )
468 unsigned char buf[1000];
469 const unsigned char *vbuf;
479 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
484 memset( buf, 0x00,
sizeof( buf ) );
507 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
509 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
515 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
517 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
523 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
525 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
531 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
533 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
539 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
541 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
558 #if defined(TEST_SUITE_ACTIVE)
559 if( strcmp( params[0],
"ecdh_primitive_random" ) == 0 )
566 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
570 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
572 test_suite_ecdh_primitive_random( param1 );
578 if( strcmp( params[0],
"ecdh_primitive_testvec" ) == 0 )
582 char *param2 = params[2];
583 char *param3 = params[3];
584 char *param4 = params[4];
585 char *param5 = params[5];
586 char *param6 = params[6];
587 char *param7 = params[7];
588 char *param8 = params[8];
592 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
596 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
605 test_suite_ecdh_primitive_testvec( param1, param2, param3, param4, param5, param6, param7, param8 );
611 if( strcmp( params[0],
"ecdh_exchange" ) == 0 )
618 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
622 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
624 test_suite_ecdh_exchange( param1 );
632 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
646 ret = fgets( buf, len, f );
650 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
651 buf[strlen(buf) - 1] =
'\0';
652 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
653 buf[strlen(buf) - 1] =
'\0';
666 while( *p !=
'\0' && p < buf + len )
676 if( p + 1 < buf + len )
688 for( i = 0; i < cnt; i++ )
695 if( *p ==
'\\' && *(p + 1) ==
'n' )
700 else if( *p ==
'\\' && *(p + 1) ==
':' )
705 else if( *p ==
'\\' && *(p + 1) ==
'?' )
721 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
722 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_ecdh.data";
727 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
728 unsigned char alloc_buf[1000000];
729 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
732 file = fopen( filename,
"r" );
735 fprintf( stderr,
"Failed to open\n" );
739 while( !feof( file ) )
743 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
745 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
746 fprintf( stdout,
" " );
747 for( i = strlen( buf ) + 1; i < 67; i++ )
748 fprintf( stdout,
"." );
749 fprintf( stdout,
" " );
754 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
758 if( strcmp( params[0],
"depends_on" ) == 0 )
760 for( i = 1; i < cnt; i++ )
764 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
775 if( skip == 1 || ret == 3 )
778 fprintf( stdout,
"----\n" );
781 else if( ret == 0 && test_errors == 0 )
783 fprintf( stdout,
"PASS\n" );
788 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
795 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
797 if( strlen(buf) != 0 )
799 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
805 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
806 if( total_errors == 0 )
807 fprintf( stdout,
"PASSED" );
809 fprintf( stdout,
"FAILED" );
811 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
812 total_tests - total_errors, total_tests, total_skipped );
814 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
815 #if defined(POLARSSL_MEMORY_DEBUG)
816 memory_buffer_alloc_status();
818 memory_buffer_alloc_free();
821 return( total_errors != 0 );
int ecdh_make_params(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and write the ServerKeyExhange parameters.
int ecdh_make_public(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and export the client's public value.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int ecdh_calc_secret(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret.
Info structure for the pseudo random function.
int ecdh_read_public(ecdh_context *ctx, const unsigned char *buf, size_t blen)
Parse and import the client's public value.
int ecdh_compute_shared(ecp_group *grp, mpi *z, const ecp_point *Q, const mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Compute shared secret.
int ecdh_gen_public(ecp_group *grp, mpi *d, ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a public key.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
Configuration options (set of defines)
static int test_assert(int correct, char *test)
#define PUT_UINT32_BE(n, b, i)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
Multi-precision integer library.
#define TEST_ASSERT(TEST)
ECP point structure (jacobian coordinates)
int ecp_is_zero(ecp_point *pt)
Tell if a point is zero.
void ecp_point_init(ecp_point *pt)
Initialize a point (as zero)
void mpi_free(mpi *X)
Unallocate one MPI.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void ecp_group_free(ecp_group *grp)
Free the components of an ECP group.
static int unhexify(unsigned char *obuf, const char *ibuf)
int parse_arguments(char *buf, size_t len, char *params[50])
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse the ServerKeyExhange parameters.
Elliptic curve Diffie-Hellman.
int ecp_use_known_dp(ecp_group *grp, ecp_group_id index)
Set a group using well-known domain parameters.
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)
void ecp_group_init(ecp_group *grp)
Initialize a group (to something meaningless)
int dispatch_test(int cnt, char *params[50])
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void ecdh_init(ecdh_context *ctx)
Initialize context.
int verify_int(char *str, int *value)
void ecdh_free(ecdh_context *ctx)
Free context.
int get_line(FILE *f, char *buf, size_t len)
void ecp_point_free(ecp_point *pt)
Free the components of a point.