Moved tests from selftest to tests/test_suite_ecp
This commit is contained in:
parent
a5402fee04
commit
4b8c3f2a1c
5 changed files with 181 additions and 185 deletions
180
library/ecp.c
180
library/ecp.c
|
@ -416,190 +416,12 @@ cleanup:
|
|||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
|
||||
#include "polarssl/error.h"
|
||||
|
||||
/*
|
||||
* Return true iff P and Q are the same point
|
||||
*/
|
||||
static int ecp_point_eq( const ecp_point *P, const ecp_point *Q )
|
||||
{
|
||||
if( P->is_zero || Q->is_zero )
|
||||
return( P->is_zero && Q->is_zero );
|
||||
|
||||
return( mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
|
||||
mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a point assuming its coordinates are small
|
||||
*/
|
||||
static void ecp_point_print( const ecp_point *P )
|
||||
{
|
||||
if( P->is_zero )
|
||||
printf( "zero\n" );
|
||||
else
|
||||
printf( "(%lu, %lu)\n", P->X.p[0], P->Y.p[0] );
|
||||
}
|
||||
|
||||
#define TEST_GROUP_ORDER 13
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*
|
||||
* Data for basic tests with small values gathered from
|
||||
* http://danher6.100webspace.net/ecc/#EFp_interactivo and double-checked
|
||||
* using Pari-GP.
|
||||
*/
|
||||
int ecp_self_test( int verbose )
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned i;
|
||||
ecp_group grp;
|
||||
ecp_point O, A, B, C, D, E, F, G, H, TMP;
|
||||
ecp_point *add_tbl[][3] =
|
||||
{
|
||||
{ &O, &O, &O }, { &O, &A, &A }, { &A, &O, &A },
|
||||
{ &A, &A, &O }, { &B, &C, &O }, { &C, &B, &O },
|
||||
{ &A, &D, &E }, { &D, &A, &E },
|
||||
{ &B, &D, &F }, { &D, &B, &F },
|
||||
{ &D, &D, &G }, { &B, &B, &H },
|
||||
};
|
||||
mpi m;
|
||||
ecp_point mul_tbl[TEST_GROUP_ORDER + 1];
|
||||
char *mul_tbl_s[TEST_GROUP_ORDER - 1][2] =
|
||||
{
|
||||
{ "17", "42" },
|
||||
{ "20", "01" },
|
||||
{ "14", "11" },
|
||||
{ "34", "33" },
|
||||
{ "21", "32" },
|
||||
{ "27", "30" },
|
||||
{ "27", "17" },
|
||||
{ "21", "15" },
|
||||
{ "34", "14" },
|
||||
{ "14", "36" },
|
||||
{ "20", "46" },
|
||||
{ "17", "05" },
|
||||
};
|
||||
|
||||
ecp_group_init( &grp );
|
||||
|
||||
ecp_point_init( &O ); ecp_point_init( &A ); ecp_point_init( &B );
|
||||
ecp_point_init( &C ); ecp_point_init( &D ); ecp_point_init( &E );
|
||||
ecp_point_init( &F ); ecp_point_init( &G ); ecp_point_init( &H );
|
||||
ecp_point_init( &TMP );
|
||||
|
||||
mpi_init( &m );
|
||||
|
||||
for( i = 0; i <= TEST_GROUP_ORDER; i++ )
|
||||
ecp_point_init( &mul_tbl[i] );
|
||||
|
||||
ecp_set_zero( &O );
|
||||
MPI_CHK( ecp_group_read_string( &grp, 10, "47", "4", "17", "42", "13" ) );
|
||||
MPI_CHK( ecp_point_read_string( &A, 10, "13", "00" ) );
|
||||
MPI_CHK( ecp_point_read_string( &B, 10, "14", "11" ) );
|
||||
MPI_CHK( ecp_point_read_string( &C, 10, "14", "36" ) );
|
||||
MPI_CHK( ecp_point_read_string( &D, 10, "37", "31" ) );
|
||||
MPI_CHK( ecp_point_read_string( &E, 10, "34", "14" ) );
|
||||
MPI_CHK( ecp_point_read_string( &F, 10, "45", "07" ) );
|
||||
MPI_CHK( ecp_point_read_string( &G, 10, "21", "32" ) );
|
||||
MPI_CHK( ecp_point_read_string( &H, 10, "27", "30" ) );
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " ECP test #1 (ecp_add): " );
|
||||
|
||||
for( i = 0; i < sizeof( add_tbl ) / sizeof( add_tbl[0] ); i++ )
|
||||
{
|
||||
MPI_CHK( ecp_add( &grp, &TMP, add_tbl[i][0], add_tbl[i][1] ) );
|
||||
if( ! ecp_point_eq( &TMP, add_tbl[i][2] ) )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
{
|
||||
printf( " failed (%u)\n", i );
|
||||
printf( " GOT: " );
|
||||
ecp_point_print( &TMP );
|
||||
printf( " EXPECTED: " );
|
||||
ecp_point_print( add_tbl[i][2] );
|
||||
}
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
|
||||
MPI_CHK( ecp_copy( &mul_tbl[0], &O ) );
|
||||
for( i = 1; i <= TEST_GROUP_ORDER - 1; i++ )
|
||||
MPI_CHK( ecp_point_read_string( &mul_tbl[i], 10,
|
||||
mul_tbl_s[i-1][0], mul_tbl_s[i-1][1] ) );
|
||||
MPI_CHK( ecp_copy( &mul_tbl[TEST_GROUP_ORDER], &O ) );
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " ECP test #2 (ecp_mul): " );
|
||||
|
||||
for( i = 0; i <= TEST_GROUP_ORDER; i++ )
|
||||
{
|
||||
MPI_CHK( mpi_lset( &m, i ) );
|
||||
MPI_CHK( ecp_mul( &grp, &TMP, &m, &grp.G ) );
|
||||
if( ! ecp_point_eq( &TMP, &mul_tbl[i] ) )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
{
|
||||
printf( " failed (%u)\n", i );
|
||||
printf( " GOT: " );
|
||||
ecp_point_print( &TMP );
|
||||
printf( " EXPECTED: " );
|
||||
ecp_point_print( &mul_tbl[i] );
|
||||
}
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " ECP test #3 (use_known_dp): " );
|
||||
|
||||
for( i = 0; i <= POLARSSL_ECP_DP_SECP521R1; i++ )
|
||||
{
|
||||
MPI_CHK( ecp_use_known_dp( &grp, i ) );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
|
||||
|
||||
cleanup:
|
||||
|
||||
if( ret != 0 && verbose != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_ERROR_C)
|
||||
char error_buf[200];
|
||||
error_strerror( ret, error_buf, 200 );
|
||||
printf( "Unexpected error: %d - %s\n\n", ret, error_buf );
|
||||
#else
|
||||
printf( "Unexpected error: %08X\n", ret );
|
||||
#endif
|
||||
}
|
||||
|
||||
ecp_group_free( &grp ); ecp_point_free( &O ); ecp_point_free( &TMP );
|
||||
|
||||
ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
|
||||
ecp_point_free( &D ); ecp_point_free( &E ); ecp_point_free( &F );
|
||||
ecp_point_free( &G ); ecp_point_free( &H );
|
||||
|
||||
mpi_free( &m );
|
||||
|
||||
for( i = 0; i <= TEST_GROUP_ORDER; i++ )
|
||||
ecp_point_free( &mul_tbl[i] );
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
|
||||
return( ret );
|
||||
return( verbose++ );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -50,7 +50,6 @@
|
|||
#include "polarssl/x509.h"
|
||||
#include "polarssl/xtea.h"
|
||||
#include "polarssl/pbkdf2.h"
|
||||
#include "polarssl/ecp.h"
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
|
@ -156,11 +155,6 @@ int main( int argc, char *argv[] )
|
|||
return( ret );
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( ( ret = ecp_self_test( v ) ) != 0 )
|
||||
return( ret );
|
||||
#endif
|
||||
|
||||
#else
|
||||
printf( " POLARSSL_SELF_TEST not defined.\n" );
|
||||
#endif
|
||||
|
|
|
@ -45,6 +45,7 @@ add_test_suite(ctr_drbg)
|
|||
add_test_suite(debug)
|
||||
add_test_suite(des)
|
||||
add_test_suite(dhm)
|
||||
add_test_suite(ecp)
|
||||
add_test_suite(error)
|
||||
add_test_suite(gcm gcm.encrypt)
|
||||
add_test_suite(gcm gcm.decrypt)
|
||||
|
|
80
tests/suites/test_suite_ecp.data
Normal file
80
tests/suites/test_suite_ecp.data
Normal file
|
@ -0,0 +1,80 @@
|
|||
ECP small addition #1
|
||||
ecp_small_add:1:"":"":1:"":"":1:0:0
|
||||
|
||||
ECP small addition #2
|
||||
ecp_small_add:1:"":"":0:"14":"11":0:14:11
|
||||
|
||||
ECP small addition #3
|
||||
ecp_small_add:0:"13":"00":0:"13":"00":1:0:0
|
||||
|
||||
ECP small addition #4
|
||||
ecp_small_add:0:"14":"11":0:"14":"36":1:0:0
|
||||
|
||||
ECP small addition #5
|
||||
ecp_small_add:0:"13":"00":0:"37":"31":0:34:14
|
||||
|
||||
ECP small addition #6
|
||||
ecp_small_add:0:"14":"11":0:"37":"31":0:45:07
|
||||
|
||||
ECP small addition #7
|
||||
ecp_small_add:0:"37":"31":0:"37":"31":0:21:32
|
||||
|
||||
ECP small addition #8
|
||||
ecp_small_add:0:"14":"11":0:"14":"11":0:27:30
|
||||
|
||||
ECP small multiplication #0
|
||||
ecp_small_mul:0:1:0:0
|
||||
|
||||
ECP small multiplication #1
|
||||
ecp_small_mul:1:0:17:42
|
||||
|
||||
ECP small multiplication #2
|
||||
ecp_small_mul:2:0:20:01
|
||||
|
||||
ECP small multiplication #3
|
||||
ecp_small_mul:3:0:14:11
|
||||
|
||||
ECP small multiplication #4
|
||||
ecp_small_mul:4:0:34:33
|
||||
|
||||
ECP small multiplication #5
|
||||
ecp_small_mul:5:0:21:32
|
||||
|
||||
ECP small multiplication #6
|
||||
ecp_small_mul:6:0:27:30
|
||||
|
||||
ECP small multiplication #7
|
||||
ecp_small_mul:7:0:27:17
|
||||
|
||||
ECP small multiplication #8
|
||||
ecp_small_mul:8:0:21:15
|
||||
|
||||
ECP small multiplication #9
|
||||
ecp_small_mul:9:0:34:14
|
||||
|
||||
ECP small multiplication #10
|
||||
ecp_small_mul:10:0:14:36
|
||||
|
||||
ECP small multiplication #11
|
||||
ecp_small_mul:11:0:20:46
|
||||
|
||||
ECP small multiplication #12
|
||||
ecp_small_mul:12:0:17:05
|
||||
|
||||
ECP small multiplication #13
|
||||
ecp_small_mul:13:1:0:0
|
||||
|
||||
ECP test vectors secp192r1
|
||||
ecp_test_vect:SECP192R1
|
||||
|
||||
ECP test vectors secp224r1
|
||||
ecp_test_vect:SECP224R1
|
||||
|
||||
ECP test vectors secp256r1
|
||||
ecp_test_vect:SECP256R1
|
||||
|
||||
ECP test vectors secp384r1
|
||||
ecp_test_vect:SECP384R1
|
||||
|
||||
ECP test vectors secp521r1
|
||||
ecp_test_vect:SECP521R1
|
99
tests/suites/test_suite_ecp.function
Normal file
99
tests/suites/test_suite_ecp.function
Normal file
|
@ -0,0 +1,99 @@
|
|||
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} )
|
||||
TEST_ASSERT( C.is_zero );
|
||||
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} )
|
||||
TEST_ASSERT( C.is_zero );
|
||||
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
|
||||
|
||||
BEGIN_CASE
|
||||
ecp_small_mul:m:r_zero:x_r:y_r
|
||||
{
|
||||
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 );
|
||||
|
||||
TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G ) == 0 );
|
||||
|
||||
if( {r_zero} )
|
||||
TEST_ASSERT( R.is_zero );
|
||||
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
|
||||
|
||||
BEGIN_CASE
|
||||
ecp_test_vect:id
|
||||
{
|
||||
ecp_group grp;
|
||||
|
||||
ecp_group_init( &grp );
|
||||
|
||||
TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
|
||||
|
||||
ecp_group_free( &grp );
|
||||
}
|
||||
END_CASE
|
Loading…
Reference in a new issue