2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2012-11-07 21:39:45 +01:00
|
|
|
#include <polarssl/ecp.h>
|
2013-08-16 13:38:47 +02:00
|
|
|
|
|
|
|
#define POLARSSL_ECP_PF_UNKNOWN -1
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2013-08-23 16:35:32 +02:00
|
|
|
* depends_on:POLARSSL_ECP_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-11-30 15:10:14 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_curve_info( int id, int tls_id, int size, char *name )
|
|
|
|
{
|
|
|
|
const ecp_curve_info *by_id, *by_tls, *by_name;
|
|
|
|
|
|
|
|
TEST_ASSERT( ( by_id = ecp_curve_info_from_grp_id( id ) ) != NULL );
|
|
|
|
TEST_ASSERT( ( by_tls = ecp_curve_info_from_tls_id( tls_id ) ) != NULL );
|
|
|
|
TEST_ASSERT( ( by_name = ecp_curve_info_from_name( name ) ) != NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( by_id == by_tls );
|
|
|
|
TEST_ASSERT( by_id == by_name );
|
|
|
|
|
|
|
|
TEST_ASSERT( by_id->size == size );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_small_add( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
|
|
|
|
char *y_b, int c_zero, int x_c, int y_c )
|
2012-11-07 21:39:45 +01:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( a_zero )
|
2012-11-07 21:39:45 +01:00
|
|
|
ecp_set_zero( &A );
|
|
|
|
else
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( b_zero )
|
2012-11-07 21:39:45 +01:00
|
|
|
ecp_set_zero( &B );
|
|
|
|
else
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( c_zero )
|
2012-11-19 20:16:28 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
else
|
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( c_zero )
|
2012-11-19 20:16:28 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
else
|
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_small_sub( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
|
|
|
|
char *y_b, int c_zero, int x_c, int y_c )
|
2012-11-19 21:23:27 +01:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( a_zero )
|
2012-11-19 21:23:27 +01:00
|
|
|
ecp_set_zero( &A );
|
|
|
|
else
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
|
2012-11-19 21:23:27 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( b_zero )
|
2012-11-19 21:23:27 +01:00
|
|
|
ecp_set_zero( &B );
|
|
|
|
else
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
|
2012-11-19 21:23:27 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( c_zero )
|
2012-11-19 21:23:27 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
|
|
|
|
else
|
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
|
2012-11-19 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-19 21:23:27 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_small_mul( int m_str, int r_zero, int x_r, int y_r, int ret )
|
2012-11-07 21:39:45 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point R;
|
|
|
|
mpi m;
|
2013-09-02 14:29:09 +02:00
|
|
|
rnd_pseudo_info rnd_info;
|
2012-11-07 21:39:45 +01:00
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &R );
|
|
|
|
mpi_init( &m );
|
2013-09-02 14:29:09 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_group_read_string( &grp, 10,
|
|
|
|
"47", "4", "17", "42", "13" ) == 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_lset( &m, m_str ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-09-02 14:29:09 +02:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) == ret );
|
|
|
|
|
2013-11-21 09:28:03 +01:00
|
|
|
if( ret == 0 )
|
2013-09-02 14:29:09 +02:00
|
|
|
{
|
2013-11-21 09:28:03 +01:00
|
|
|
if( r_zero )
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
|
|
|
|
}
|
2013-09-02 14:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try again with randomization */
|
|
|
|
ecp_point_free( &R );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == ret );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-11-21 09:28:03 +01:00
|
|
|
if( ret == 0 )
|
2012-11-07 21:39:45 +01:00
|
|
|
{
|
2013-11-21 09:28:03 +01:00
|
|
|
if( r_zero )
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
|
|
|
|
}
|
2012-11-07 21:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &R );
|
|
|
|
mpi_free( &m );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_small_check_pub( int x, int y, int z, int ret )
|
2012-11-24 12:05:44 +01:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_lset( &P.X, x ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &P.Y, y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &P.Z, z ) == 0 );
|
2012-11-24 12:05:44 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
|
2012-11-24 12:05:44 +01:00
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &P );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-24 12:05:44 +01:00
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_check_pub_mx( int grp_id, char *key_hex, int ret )
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point P;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &P );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, grp_id ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_read_string( &P.X, 16, key_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &P.Z, 1 ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &P );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
|
|
|
|
char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
|
|
|
|
char *yZ_str )
|
2012-11-07 21:39:45 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
2012-11-07 12:24:22 +01:00
|
|
|
ecp_point R;
|
|
|
|
mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
|
2013-09-02 14:29:09 +02:00
|
|
|
rnd_pseudo_info rnd_info;
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2012-11-07 12:24:22 +01: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 );
|
2013-09-02 14:29:09 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2012-11-24 12:05:44 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
|
2013-09-02 14:29:09 +02:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
|
2012-11-24 12:05:44 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2013-09-02 14:29:09 +02:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
2012-11-24 12:05:44 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
|
2013-09-02 14:29:09 +02:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
|
2012-11-24 12:05:44 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2013-09-02 14:29:09 +02:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
2012-11-24 12:05:44 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 12:24:22 +01: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 21:39:45 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-11 20:45:18 +01:00
|
|
|
|
2013-12-04 11:49:20 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_test_vec_x( int id, char *dA_hex, char *xA_hex,
|
|
|
|
char *dB_hex, char *xB_hex, char *xS_hex )
|
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point R;
|
|
|
|
mpi dA, xA, dB, xB, xS;
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
|
|
|
ecp_group_init( &grp ); ecp_point_init( &R );
|
|
|
|
mpi_init( &dA ); mpi_init( &xA );
|
|
|
|
mpi_init( &dB ); mpi_init( &xB );
|
|
|
|
mpi_init( &xS );
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_read_string( &dA, 16, dA_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &dB, 16, dB_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xA, 16, xA_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xB, 16, xB_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &xS, 16, xS_hex ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xS ) == 0 );
|
|
|
|
|
2013-12-04 18:39:17 +01:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
|
|
|
|
|
2013-12-04 18:39:17 +01:00
|
|
|
TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R.X, &xS ) == 0 );
|
|
|
|
|
|
|
|
ecp_group_free( &grp ); ecp_point_free( &R );
|
|
|
|
mpi_free( &dA ); mpi_free( &xA );
|
|
|
|
mpi_free( &dB ); mpi_free( &xB );
|
|
|
|
mpi_free( &xS );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_fast_mod( int id, char *N_str )
|
2012-11-11 20:45:18 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
mpi N, R;
|
|
|
|
|
|
|
|
mpi_init( &N ); mpi_init( &R );
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
|
2013-10-21 14:52:21 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
|
|
|
TEST_ASSERT( grp.modp != NULL );
|
2012-11-11 20:45:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_write_binary( int id, char *x, char *y, char *z, int format,
|
|
|
|
char *out, int blen, int ret )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point P;
|
|
|
|
unsigned char buf[256], str[512];
|
2013-02-10 12:22:46 +01:00
|
|
|
size_t olen;
|
2012-11-24 14:10:14 +01:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
memset( str, 0, sizeof( str ) );
|
|
|
|
|
|
|
|
ecp_group_init( &grp ); ecp_point_init( &P );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
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:10:14 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
|
|
|
|
&olen, buf, blen ) == ret );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ret == 0 )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
2012-11-24 15:19:55 +01:00
|
|
|
hexify( str, buf, olen );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
|
2012-11-24 14:10:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp ); ecp_point_free( &P );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_read_binary( int id, char *input, char *x, char *y, char *z,
|
|
|
|
int ret )
|
2012-11-24 16:19:42 +01: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 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
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 );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
ilen = unhexify( buf, input );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ret == 0 )
|
2012-11-24 16:19:42 +01:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
|
|
|
|
int ret )
|
2013-02-10 13:00:20 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point P;
|
|
|
|
mpi X, Y, Z;
|
|
|
|
size_t ilen;
|
|
|
|
unsigned char buf[256];
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char *vbuf = buf;
|
2013-02-10 13:00:20 +01:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
|
|
|
|
ecp_group_init( &grp ); ecp_point_init( &P );
|
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
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 );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
ilen = unhexify( buf, input );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ret == 0 )
|
2013-02-10 13:00:20 +01:00
|
|
|
{
|
|
|
|
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 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( *vbuf == 0x00 );
|
2013-02-10 13:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp ); ecp_point_free( &P );
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_tls_write_read_point( int id )
|
2013-01-26 14:42:45 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
2013-02-10 11:15:11 +01:00
|
|
|
ecp_point pt;
|
|
|
|
unsigned char buf[256];
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char *vbuf;
|
2013-02-10 12:22:46 +01:00
|
|
|
size_t olen;
|
2013-01-26 14:42:45 +01:00
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
2013-02-10 11:15:11 +01:00
|
|
|
ecp_point_init( &pt );
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
|
2013-02-10 12:22:46 +01:00
|
|
|
POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
|
2014-03-26 14:12:20 +01:00
|
|
|
== POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
|
2013-02-10 12:22:46 +01:00
|
|
|
POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
|
|
|
|
TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
|
2013-02-10 12:22:46 +01:00
|
|
|
POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( ecp_is_zero( &pt ) );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
|
|
|
|
TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
|
2013-02-10 12:22:46 +01:00
|
|
|
POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
TEST_ASSERT( ecp_is_zero( &pt ) );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-01-26 14:42:45 +01:00
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
2013-02-10 11:15:11 +01:00
|
|
|
ecp_point_free( &pt );
|
2013-01-26 14:42:45 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_tls_read_group( char *record, int result, int bits )
|
2013-02-09 17:03:58 +01:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
unsigned char buf[10];
|
2013-02-10 13:20:52 +01:00
|
|
|
const unsigned char *vbuf = buf;
|
2013-02-09 17:03:58 +01:00
|
|
|
int len, ret;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
len = unhexify( buf, record );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2013-02-10 13:20:52 +01:00
|
|
|
ret = ecp_tls_read_group( &grp, &vbuf, len );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ret == result );
|
2013-02-09 17:03:58 +01:00
|
|
|
if( ret == 0)
|
2013-02-10 13:20:52 +01:00
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
|
2013-02-10 13:20:52 +01:00
|
|
|
TEST_ASSERT( *vbuf == 0x00 );
|
|
|
|
}
|
2013-02-09 17:03:58 +01:00
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_tls_write_read_group( int id )
|
2013-02-10 12:51:17 +01:00
|
|
|
{
|
|
|
|
ecp_group grp1, grp2;
|
|
|
|
unsigned char buf[10];
|
2013-02-10 13:20:52 +01:00
|
|
|
const unsigned char *vbuf = buf;
|
2013-02-10 12:51:17 +01:00
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ecp_group_init( &grp1 );
|
|
|
|
ecp_group_init( &grp2 );
|
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
|
2013-02-10 12:51:17 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
|
2013-02-10 13:20:52 +01:00
|
|
|
TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 );
|
2013-02-10 12:51:17 +01:00
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
|
|
|
|
TEST_ASSERT( grp1.id == grp2.id );
|
|
|
|
}
|
|
|
|
|
|
|
|
ecp_group_free( &grp1 );
|
|
|
|
ecp_group_free( &grp2 );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-10 12:51:17 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2013-12-04 11:08:01 +01:00
|
|
|
void ecp_check_privkey( int id, char *key_hex, int ret )
|
2013-07-01 14:06:13 +02:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
mpi d;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
mpi_init( &d );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
TEST_ASSERT( mpi_read_string( &d, 16, key_hex ) == 0 );
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
TEST_ASSERT( ecp_check_privkey( &grp, &d ) == ret );
|
2013-07-01 14:06:13 +02:00
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
mpi_free( &d );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_gen_keypair( int id )
|
2013-02-10 11:15:11 +01:00
|
|
|
{
|
|
|
|
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 ) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
|
|
|
|
== 0 );
|
|
|
|
|
2013-07-01 14:06:13 +02:00
|
|
|
TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
|
2013-07-16 17:15:03 +02:00
|
|
|
TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &Q );
|
|
|
|
mpi_free( &d );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-11-30 14:13:16 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_gen_key( int id )
|
|
|
|
{
|
|
|
|
ecp_keypair key;
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
|
|
|
ecp_keypair_init( &key );
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
|
|
|
|
TEST_ASSERT( ecp_check_privkey( &key.grp, &key.d ) == 0 );
|
|
|
|
|
|
|
|
ecp_keypair_free( &key );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-10-10 12:48:03 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
|
2013-08-20 11:48:36 +02:00
|
|
|
void ecp_selftest()
|
2012-11-13 20:57:00 +01:00
|
|
|
{
|
|
|
|
TEST_ASSERT( ecp_self_test( 0 ) == 0 );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|