9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
288 #ifdef POLARSSL_ECDH_C
290 #define TEST_SUITE_ACTIVE
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
364 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1" ) == 0 )
369 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1" ) == 0 )
374 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
379 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1" ) == 0 )
384 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1" ) == 0 )
391 printf(
"Expected integer for parameter and got: %s\n", str );
395 void test_suite_ecdh_primitive_random(
int id )
427 void test_suite_ecdh_primitive_testvec(
int id,
char *dA_str,
char *xA_str,
char *yA_str,
428 char *dB_str,
char *xB_str,
char *yB_str,
433 mpi dA, dB, zA, zB, check;
468 void test_suite_ecdh_exchange(
int id )
471 unsigned char buf[1000];
472 const unsigned char *vbuf;
482 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
487 memset( buf, 0x00,
sizeof( buf ) );
510 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
512 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
518 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
520 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
526 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
528 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
534 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
536 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
542 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
544 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
561 #if defined(TEST_SUITE_ACTIVE)
562 if( strcmp( params[0],
"ecdh_primitive_random" ) == 0 )
569 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
573 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
575 test_suite_ecdh_primitive_random( param1 );
581 if( strcmp( params[0],
"ecdh_primitive_testvec" ) == 0 )
585 char *param2 = params[2];
586 char *param3 = params[3];
587 char *param4 = params[4];
588 char *param5 = params[5];
589 char *param6 = params[6];
590 char *param7 = params[7];
591 char *param8 = params[8];
595 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
599 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
608 test_suite_ecdh_primitive_testvec( param1, param2, param3, param4, param5, param6, param7, param8 );
614 if( strcmp( params[0],
"ecdh_exchange" ) == 0 )
621 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
625 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
627 test_suite_ecdh_exchange( param1 );
635 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
649 ret = fgets( buf, len, f );
653 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
654 buf[strlen(buf) - 1] =
'\0';
655 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
656 buf[strlen(buf) - 1] =
'\0';
669 while( *p !=
'\0' && p < buf + len )
679 if( p + 1 < buf + len )
691 for( i = 0; i < cnt; i++ )
698 if( *p ==
'\\' && *(p + 1) ==
'n' )
703 else if( *p ==
'\\' && *(p + 1) ==
':' )
708 else if( *p ==
'\\' && *(p + 1) ==
'?' )
724 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
725 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_ecdh.data";
730 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
731 unsigned char alloc_buf[1000000];
732 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
735 file = fopen( filename,
"r" );
738 fprintf( stderr,
"Failed to open\n" );
742 while( !feof( file ) )
746 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
748 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
749 fprintf( stdout,
" " );
750 for( i = strlen( buf ) + 1; i < 67; i++ )
751 fprintf( stdout,
"." );
752 fprintf( stdout,
" " );
757 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
761 if( strcmp( params[0],
"depends_on" ) == 0 )
763 for( i = 1; i < cnt; i++ )
767 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
778 if( skip == 1 || ret == 3 )
781 fprintf( stdout,
"----\n" );
786 fprintf( stdout,
"PASS\n" );
791 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
798 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
800 if( strlen(buf) != 0 )
802 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
808 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
809 if( total_errors == 0 )
810 fprintf( stdout,
"PASSED" );
812 fprintf( stdout,
"FAILED" );
814 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
815 total_tests - total_errors, total_tests, total_skipped );
817 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
818 #if defined(POLARSSL_MEMORY_DEBUG)
819 memory_buffer_alloc_status();
821 memory_buffer_alloc_free();
824 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.
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
int main(int argc, char *argv[])
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
#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.