2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2013-01-26 15:20:32 +00:00
|
|
|
#include <polarssl/ecdh.h>
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2013-01-26 15:20:32 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2013-08-23 14:35:32 +00:00
|
|
|
* depends_on:POLARSSL_ECDH_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2013-01-26 15:20:32 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_primitive_random( int id )
|
2013-01-26 15:20:32 +00:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point qA, qB;
|
|
|
|
mpi dA, dB, zA, zB;
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &qA ); ecp_point_init( &qB );
|
|
|
|
mpi_init( &dA ); mpi_init( &dB );
|
|
|
|
mpi_init( &zA ); mpi_init( &zB );
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2013-01-26 15:20:32 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
|
|
|
|
== 0 );
|
|
|
|
TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
|
|
|
|
== 0 );
|
|
|
|
TEST_ASSERT( ecdh_compute_shared( &grp, &zA, &qB, &dA ) == 0 );
|
|
|
|
TEST_ASSERT( ecdh_compute_shared( &grp, &zB, &qA, &dB ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &zA, &zB ) == 0 );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &qA ); ecp_point_free( &qB );
|
|
|
|
mpi_free( &dA ); mpi_free( &dB );
|
|
|
|
mpi_free( &zA ); mpi_free( &zB );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_primitive_testvec( int id, char *dA_str, char *xA_str, char *yA_str,
|
|
|
|
char *dB_str, char *xB_str, char *yB_str,
|
|
|
|
char *z_str )
|
2013-01-27 07:56:21 +00:00
|
|
|
{
|
|
|
|
ecp_group grp;
|
|
|
|
ecp_point qA, qB;
|
|
|
|
mpi dA, dB, zA, zB, check;
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &qA ); ecp_point_init( &qB );
|
|
|
|
mpi_init( &dA ); mpi_init( &dB );
|
|
|
|
mpi_init( &zA ); mpi_init( &zB ); mpi_init( &check );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, ¬_rnd, dA_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( ! ecp_is_zero( &qA ) );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &check, 16, xA_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &qA.X, &check ) == 0 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &check, 16, yA_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &qA.Y, &check ) == 0 );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, ¬_rnd, dB_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( ! ecp_is_zero( &qB ) );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &check, 16, xB_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &qB.X, &check ) == 0 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &check, 16, yB_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &qB.Y, &check ) == 0 );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &check, 16, z_str ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
TEST_ASSERT( ecdh_compute_shared( &grp, &zA, &qB, &dA ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &zA, &check ) == 0 );
|
|
|
|
TEST_ASSERT( ecdh_compute_shared( &grp, &zB, &qA, &dB ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &zB, &check ) == 0 );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &qA ); ecp_point_free( &qB );
|
|
|
|
mpi_free( &dA ); mpi_free( &dB );
|
|
|
|
mpi_free( &zA ); mpi_free( &zB ); mpi_free( &check );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2013-02-11 19:28:55 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_exchange( int id )
|
2013-02-11 19:28:55 +00:00
|
|
|
{
|
|
|
|
ecdh_context srv, cli;
|
|
|
|
unsigned char buf[1000];
|
|
|
|
const unsigned char *vbuf;
|
|
|
|
size_t len;
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
|
|
|
ecdh_init( &srv );
|
|
|
|
ecdh_init( &cli );
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( ecp_use_known_dp( &srv.grp, id ) == 0 );
|
2013-02-11 19:28:55 +00:00
|
|
|
|
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
|
|
|
TEST_ASSERT( ecdh_make_params( &srv, &len, buf, 1000,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
|
|
|
TEST_ASSERT( ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
|
|
|
|
|
2013-02-11 21:05:42 +00:00
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
2013-02-11 20:51:45 +00:00
|
|
|
TEST_ASSERT( ecdh_make_public( &cli, &len, buf, 1000,
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
|
|
|
TEST_ASSERT( ecdh_read_public( &srv, buf, len ) == 0 );
|
|
|
|
|
2013-02-11 21:05:42 +00:00
|
|
|
TEST_ASSERT( ecdh_calc_secret( &srv, &len, buf, 1000 ) == 0 );
|
|
|
|
TEST_ASSERT( ecdh_calc_secret( &cli, &len, buf, 1000 ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
|
|
|
|
|
2013-02-11 19:28:55 +00:00
|
|
|
ecdh_free( &srv );
|
|
|
|
ecdh_free( &cli );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|