2012-11-07 20:39:45 +00:00
|
|
|
BEGIN_HEADER
|
|
|
|
#include <polarssl/ecp.h>
|
|
|
|
END_HEADER
|
|
|
|
|
|
|
|
BEGIN_DEPENDENCIES
|
|
|
|
depends_on:POLARSSL_ECP_C:POLARSSL_BIGNUM_C
|
|
|
|
END_DEPENDENCIES
|
|
|
|
|
|
|
|
BEGIN_CASE
|
|
|
|
ecp_small_add:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point A, B, C;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_group_read_string( &grp, 10,
|
|
|
|
"47", "4", "17", "42", "13" ) == 0 );
|
|
|
|
|
|
|
|
if( {a_zero} )
|
|
|
|
ecp_set_zero( &A );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 );
|
|
|
|
|
|
|
|
if( {b_zero} )
|
|
|
|
ecp_set_zero( &B );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
|
|
|
|
|
|
|
|
if( {c_zero} )
|
2012-11-19 19:16:28 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
|
2012-11-07 20:39:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
|
|
|
|
|
|
|
|
if( {c_zero} )
|
2012-11-19 19:16:28 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
|
2012-11-07 20:39:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2012-11-19 20:23:27 +00:00
|
|
|
BEGIN_CASE
|
|
|
|
ecp_small_sub:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point A, B, C;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_group_read_string( &grp, 10,
|
|
|
|
"47", "4", "17", "42", "13" ) == 0 );
|
|
|
|
|
|
|
|
if( {a_zero} )
|
|
|
|
ecp_set_zero( &A );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 );
|
|
|
|
|
|
|
|
if( {b_zero} )
|
|
|
|
ecp_set_zero( &B );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
|
|
|
|
|
|
|
|
if( {c_zero} )
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2012-11-07 20:39:45 +00:00
|
|
|
BEGIN_CASE
|
2012-11-11 13:33:59 +00:00
|
|
|
ecp_small_mul:m:r_zero:x_r:y_r:ret
|
2012-11-07 20:39:45 +00:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point R;
|
|
|
|
mpi m;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &R );
|
|
|
|
mpi_init( &m );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_group_read_string( &grp, 10,
|
|
|
|
"47", "4", "17", "42", "13" ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_lset( &m, {m} ) == 0 );
|
|
|
|
|
2012-11-11 13:33:59 +00:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G ) == {ret} );
|
2012-11-07 20:39:45 +00:00
|
|
|
|
|
|
|
if( {r_zero} )
|
2012-11-19 19:16:28 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
|
2012-11-07 20:39:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.X, {x_r} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.Y, {y_r} ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &R );
|
|
|
|
mpi_free( &m );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2012-11-24 11:05:44 +00:00
|
|
|
BEGIN_CASE
|
|
|
|
ecp_small_check_pub:x:y:z:ret
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point P;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &P );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_group_read_string( &grp, 10,
|
|
|
|
"47", "4", "17", "42", "13" ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_lset( &P.X, {x} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &P.Y, {y} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &P.Z, {z} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == {ret} );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &P );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2012-11-07 20:39:45 +00:00
|
|
|
BEGIN_CASE
|
2012-11-07 11:24:22 +00:00
|
|
|
ecp_test_vect:id:dA:xA:yA:dB:xB:yB:xZ:yZ
|
2012-11-07 20:39:45 +00:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
2012-11-07 11:24:22 +00:00
|
|
|
ecp_point R;
|
|
|
|
mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
|
2012-11-07 20:39:45 +00:00
|
|
|
|
2012-11-07 11:24:22 +00:00
|
|
|
ecp_group_init( &grp ); ecp_point_init( &R );
|
|
|
|
mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
|
|
|
|
mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
|
2012-11-07 20:39:45 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
|
|
|
|
|
2012-11-24 11:05:44 +00:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
|
|
|
|
2012-11-07 11:24:22 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &dA, 16, {dA} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xA, 16, {xA} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &yA, 16, {yA} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &dB, 16, {dB} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xB, 16, {xB} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &yB, 16, {yB} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xZ, 16, {xZ} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &yZ, 16, {yZ} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
|
2012-11-24 11:05:44 +00:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 11:24:22 +00:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
2012-11-24 11:05:44 +00:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 11:24:22 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
|
2012-11-24 11:05:44 +00:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 11:24:22 +00:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
2012-11-24 11:05:44 +00:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 11:24:22 +00:00
|
|
|
|
|
|
|
ecp_group_free( &grp ); ecp_point_free( &R );
|
|
|
|
mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
|
|
|
|
mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
|
2012-11-07 20:39:45 +00:00
|
|
|
}
|
|
|
|
END_CASE
|
2012-11-11 19:45:18 +00:00
|
|
|
|
|
|
|
BEGIN_CASE
|
|
|
|
ecp_fast_mod:id:N
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
mpi N, R;
|
|
|
|
|
|
|
|
mpi_init( &N ); mpi_init( &R );
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &N, 16, {N} ) == 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store correct result before we touch N
|
|
|
|
*/
|
|
|
|
TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( grp.modp( &N ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use mod rather than addition/substraction in case previous test fails
|
|
|
|
*/
|
|
|
|
TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
|
|
|
|
|
|
|
|
mpi_free( &N ); mpi_free( &R );
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
}
|
|
|
|
END_CASE
|
2012-11-13 19:57:00 +00:00
|
|
|
|
2012-11-24 13:10:14 +00:00
|
|
|
BEGIN_CASE
|
2012-11-24 14:19:55 +00:00
|
|
|
ecp_write_binary:id:x:y:z:format:out:blen:ret
|
2012-11-24 13:10:14 +00:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point P;
|
|
|
|
unsigned char buf[256], str[512];
|
2013-02-10 09:53:04 +00:00
|
|
|
uint8_t olen;
|
2012-11-24 13:10:14 +00:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
memset( str, 0, sizeof( str ) );
|
|
|
|
|
|
|
|
ecp_group_init( &grp ); ecp_point_init( &P );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_read_string( &P.X, 16, {x} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &P.Y, 16, {y} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &P.Z, 16, {z} ) == 0 );
|
|
|
|
|
2012-11-24 14:19:55 +00:00
|
|
|
#define POLARSSL_ECP_PF_UNKNOWN -1
|
2013-02-10 09:58:48 +00:00
|
|
|
TEST_ASSERT( ecp_point_write_binary( &grp, &P, POLARSSL_ECP_PF_{format},
|
2012-11-24 14:19:55 +00:00
|
|
|
&olen, buf, {blen} ) == {ret} );
|
2012-11-24 13:10:14 +00:00
|
|
|
|
|
|
|
if( {ret} == 0 )
|
|
|
|
{
|
2012-11-24 14:19:55 +00:00
|
|
|
hexify( str, buf, olen );
|
2012-11-24 13:10:14 +00:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) str, {out} ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp ); ecp_point_free( &P );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2012-11-24 15:19:42 +00:00
|
|
|
BEGIN_CASE
|
2013-02-10 09:53:04 +00:00
|
|
|
ecp_read_binary:id:input:x:y:z:ret
|
2012-11-24 15:19:42 +00:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point P;
|
|
|
|
mpi X, Y, Z;
|
|
|
|
int ilen;
|
|
|
|
unsigned char buf[256];
|
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
|
|
|
|
ecp_group_init( &grp ); ecp_point_init( &P );
|
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_read_string( &X, 16, {x} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, 16, {y} ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Z, 16, {z} ) == 0 );
|
|
|
|
|
|
|
|
ilen = unhexify( buf, {input} );
|
|
|
|
|
2013-02-10 09:58:48 +00:00
|
|
|
TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == {ret} );
|
2012-11-24 15:19:42 +00:00
|
|
|
|
|
|
|
if( {ret} == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp ); ecp_point_free( &P );
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2013-01-26 13:42:45 +00:00
|
|
|
BEGIN_CASE
|
|
|
|
ecp_gen_keypair:id
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point Q;
|
|
|
|
mpi d;
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &Q );
|
|
|
|
mpi_init( &d );
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
|
|
|
|
== 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &d, &grp.N ) < 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &d, 1 ) >= 0 );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &Q );
|
|
|
|
mpi_free( &d );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2013-02-09 16:03:58 +00:00
|
|
|
BEGIN_CASE
|
|
|
|
ecp_read_params:record:ret:bits
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
unsigned char buf[10];
|
|
|
|
int len, ret;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
|
|
|
|
|
|
|
len = unhexify( buf, {record} );
|
|
|
|
|
|
|
|
ret = ecp_tls_read_group( &grp, buf, len );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == {ret} );
|
|
|
|
if( ret == 0)
|
|
|
|
TEST_ASSERT( mpi_msb( &grp.P ) == {bits} );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
}
|
|
|
|
END_CASE
|
|
|
|
|
2012-11-13 19:57:00 +00:00
|
|
|
BEGIN_CASE
|
|
|
|
ecp_selftest:
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ecp_self_test( 0 ) == 0 );
|
|
|
|
}
|
|
|
|
END_CASE
|