mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-01 10:06:53 +03:00
Moved tests from selftest to tests/test_suite_ecp
This commit is contained in:
committed by
Paul Bakker
parent
a5402fee04
commit
4b8c3f2a1c
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
|
||||
|
Reference in New Issue
Block a user