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_ECDH_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 );
394 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1" ) == 0 )
399 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1" ) == 0 )
404 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1" ) == 0 )
409 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1" ) == 0 )
414 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
421 printf(
"Expected integer for parameter and got: %s\n", str );
425 void test_suite_ecdh_primitive_random(
int id )
458 void test_suite_ecdh_primitive_testvec(
int id,
char *dA_str,
char *xA_str,
char *yA_str,
459 char *dB_str,
char *xB_str,
char *yB_str,
464 mpi dA, dB, zA, zB, check;
476 rnd_info_A.
buf = rnd_buf_A;
480 if( grp.
nbits % 8 != 0 )
482 unsigned char shift = 8 - ( grp.
nbits % 8 );
485 for( i = 0; i < rnd_info_A.
length - 1; i++ )
486 rnd_buf_A[i] = rnd_buf_A[i] << shift
487 | rnd_buf_A[i+1] >> ( 8 - shift );
489 rnd_buf_A[rnd_info_A.
length-1] <<= shift;
492 rnd_info_B.
buf = rnd_buf_B;
496 if( grp.
nbits % 8 != 0 )
498 unsigned char shift = 8 - ( grp.
nbits % 8 );
501 for( i = 0; i < rnd_info_B.
length - 1; i++ )
502 rnd_buf_B[i] = rnd_buf_B[i] << shift
503 | rnd_buf_B[i+1] >> ( 8 - shift );
505 rnd_buf_B[rnd_info_B.
length-1] <<= shift;
537 void test_suite_ecdh_exchange(
int id )
540 unsigned char buf[1000];
541 const unsigned char *vbuf;
551 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
556 memset( buf, 0x00,
sizeof( buf ) );
580 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
582 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
588 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
590 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
596 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
598 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
604 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
606 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
612 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
614 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
631 #if defined(TEST_SUITE_ACTIVE)
632 if( strcmp( params[0],
"ecdh_primitive_random" ) == 0 )
639 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
643 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
645 test_suite_ecdh_primitive_random( param1 );
651 if( strcmp( params[0],
"ecdh_primitive_testvec" ) == 0 )
655 char *param2 = params[2];
656 char *param3 = params[3];
657 char *param4 = params[4];
658 char *param5 = params[5];
659 char *param6 = params[6];
660 char *param7 = params[7];
661 char *param8 = params[8];
665 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
669 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
678 test_suite_ecdh_primitive_testvec( param1, param2, param3, param4, param5, param6, param7, param8 );
684 if( strcmp( params[0],
"ecdh_exchange" ) == 0 )
691 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
695 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
697 test_suite_ecdh_exchange( param1 );
705 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
719 ret = fgets( buf, len, f );
723 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
724 buf[strlen(buf) - 1] =
'\0';
725 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
726 buf[strlen(buf) - 1] =
'\0';
739 while( *p !=
'\0' && p < buf + len )
749 if( p + 1 < buf + len )
761 for( i = 0; i < cnt; i++ )
768 if( *p ==
'\\' && *(p + 1) ==
'n' )
773 else if( *p ==
'\\' && *(p + 1) ==
':' )
778 else if( *p ==
'\\' && *(p + 1) ==
'?' )
794 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
795 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_ecdh.data";
800 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
801 unsigned char alloc_buf[1000000];
805 file = fopen( filename,
"r" );
808 fprintf( stderr,
"Failed to open\n" );
812 while( !feof( file ) )
816 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
818 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
819 fprintf( stdout,
" " );
820 for( i = strlen( buf ) + 1; i < 67; i++ )
821 fprintf( stdout,
"." );
822 fprintf( stdout,
" " );
827 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
831 if( strcmp( params[0],
"depends_on" ) == 0 )
833 for( i = 1; i < cnt; i++ )
837 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
848 if( skip == 1 || ret == 3 )
851 fprintf( stdout,
"----\n" );
856 fprintf( stdout,
"PASS\n" );
861 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
868 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
870 if( strlen(buf) != 0 )
872 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
878 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
879 if( total_errors == 0 )
880 fprintf( stdout,
"PASSED" );
882 fprintf( stdout,
"FAILED" );
884 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
885 total_tests - total_errors, total_tests, total_skipped );
887 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
888 #if defined(POLARSSL_MEMORY_DEBUG)
889 memory_buffer_alloc_status();
894 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)
Generate a public key and a TLS ServerKeyExchange payload.
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)
Generate a public key and a TLS ClientKeyExchange payload.
int get_line(FILE *f, char *buf, size_t len)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Memory allocation layer (Deprecated to platform layer)
int dispatch_test(int cnt, char *params[50])
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.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int ecdh_read_public(ecdh_context *ctx, const unsigned char *buf, size_t blen)
Parse and process a TLS ClientKeyExchange payload.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
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 Raw function that only does the core computation.
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, const char *test)
#define PUT_UINT32_BE(n, b, i)
void mpi_init(mpi *X)
Initialize one MPI.
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.
#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.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
void ecp_group_free(ecp_group *grp)
Free the components of an ECP group.
static int unhexify(unsigned char *obuf, const char *ibuf)
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse and procress a TLS ServerKeyExhange payload.
Elliptic curve Diffie-Hellman.
#define POLARSSL_ECP_MAX_BYTES
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 parse_arguments(char *buf, size_t len, 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.
void ecp_point_free(ecp_point *pt)
Free the components of a point.