2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/ecdh.h"
|
2018-11-07 21:07:58 +00:00
|
|
|
|
|
|
|
static int load_public_key( int grp_id, const char *point_str,
|
|
|
|
mbedtls_ecp_keypair *ecp )
|
|
|
|
{
|
|
|
|
int ok = 0;
|
2019-02-22 10:30:14 +00:00
|
|
|
unsigned char point_buf[MBEDTLS_ECP_MAX_PT_LEN];
|
2018-11-07 21:07:58 +00:00
|
|
|
size_t point_len = unhexify( point_buf, point_str );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
|
|
|
|
&ecp->Q,
|
|
|
|
point_buf,
|
|
|
|
point_len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &ecp->grp,
|
|
|
|
&ecp->Q ) == 0 );
|
|
|
|
ok = 1;
|
|
|
|
exit:
|
|
|
|
return( ok );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_private_key( int grp_id, const char *private_key_str,
|
|
|
|
mbedtls_ecp_keypair *ecp,
|
|
|
|
rnd_pseudo_info *rnd_info )
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
unsigned char private_key_buf[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
size_t private_key_len = unhexify( private_key_buf, private_key_str );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
|
|
|
|
private_key_buf,
|
|
|
|
private_key_len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
|
|
|
|
/* Calculate the public key from the private key. */
|
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,
|
|
|
|
&ecp->grp.G,
|
|
|
|
&rnd_pseudo_rand, rnd_info ) == 0 );
|
|
|
|
ok = 1;
|
|
|
|
exit:
|
|
|
|
return( ok );
|
|
|
|
}
|
|
|
|
|
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
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_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
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point qA, qB;
|
|
|
|
mbedtls_mpi dA, dB, zA, zB;
|
2013-01-26 15:20:32 +00:00
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
|
|
|
|
mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
|
|
|
|
mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB );
|
2013-01-26 15:20:32 +00:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
2015-05-11 16:46:47 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-01-26 15:20:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
|
2013-01-26 15:20:32 +00:00
|
|
|
== 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
|
2013-01-26 15:20:32 +00:00
|
|
|
== 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA,
|
2013-09-02 12:29:09 +00:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB,
|
2013-09-02 12:29:09 +00:00
|
|
|
NULL, NULL ) == 0 );
|
2013-01-26 15:20:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 );
|
2013-01-26 15:20:32 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
|
|
|
|
mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
|
|
|
|
mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB );
|
2013-01-26 15:20:32 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point qA, qB;
|
|
|
|
mbedtls_mpi dA, dB, zA, zB, check;
|
|
|
|
unsigned char rnd_buf_A[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
unsigned char rnd_buf_B[MBEDTLS_ECP_MAX_BYTES];
|
2014-01-23 15:55:18 +00:00
|
|
|
rnd_buf_info rnd_info_A, rnd_info_B;
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
|
|
|
|
mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
|
|
|
|
mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); mbedtls_mpi_init( &check );
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2015-05-11 16:46:47 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2014-01-23 15:55:18 +00:00
|
|
|
rnd_info_A.buf = rnd_buf_A;
|
|
|
|
rnd_info_A.length = unhexify( rnd_buf_A, dA_str );
|
|
|
|
|
|
|
|
/* Fix rnd_buf_A by shifting it left if necessary */
|
|
|
|
if( grp.nbits % 8 != 0 )
|
|
|
|
{
|
|
|
|
unsigned char shift = 8 - ( grp.nbits % 8 );
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for( i = 0; i < rnd_info_A.length - 1; i++ )
|
|
|
|
rnd_buf_A[i] = rnd_buf_A[i] << shift
|
|
|
|
| rnd_buf_A[i+1] >> ( 8 - shift );
|
|
|
|
|
|
|
|
rnd_buf_A[rnd_info_A.length-1] <<= shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
rnd_info_B.buf = rnd_buf_B;
|
|
|
|
rnd_info_B.length = unhexify( rnd_buf_B, dB_str );
|
|
|
|
|
|
|
|
/* Fix rnd_buf_B by shifting it left if necessary */
|
|
|
|
if( grp.nbits % 8 != 0 )
|
|
|
|
{
|
|
|
|
unsigned char shift = 8 - ( grp.nbits % 8 );
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for( i = 0; i < rnd_info_B.length - 1; i++ )
|
|
|
|
rnd_buf_B[i] = rnd_buf_B[i] << shift
|
|
|
|
| rnd_buf_B[i+1] >> ( 8 - shift );
|
|
|
|
|
|
|
|
rnd_buf_B[rnd_info_B.length-1] <<= shift;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
|
2014-01-23 15:55:18 +00:00
|
|
|
rnd_buffer_rand, &rnd_info_A ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( ! mbedtls_ecp_is_zero( &qA ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.Y, &check ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
|
2014-01-23 15:55:18 +00:00
|
|
|
rnd_buffer_rand, &rnd_info_B ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( ! mbedtls_ecp_is_zero( &qB ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, z_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &check ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zB, &check ) == 0 );
|
2013-01-27 07:56:21 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
|
|
|
|
mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
|
|
|
|
mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); mbedtls_mpi_free( &check );
|
2013-01-27 07:56:21 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecdh_context srv, cli;
|
2013-02-11 19:28:55 +00:00
|
|
|
unsigned char buf[1000];
|
|
|
|
const unsigned char *vbuf;
|
|
|
|
size_t len;
|
|
|
|
rnd_pseudo_info rnd_info;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecdh_init( &srv );
|
|
|
|
mbedtls_ecdh_init( &cli );
|
2013-02-11 19:28:55 +00:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
2015-05-11 16:46:47 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 );
|
2013-02-11 19:28:55 +00:00
|
|
|
|
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
|
2013-02-11 19:28:55 +00:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
|
2013-02-11 19:28:55 +00:00
|
|
|
|
2013-02-11 21:05:42 +00:00
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
|
2013-02-11 20:51:45 +00:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
|
2013-02-11 20:51:45 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
|
2013-09-02 12:29:09 +00:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &len, buf, 1000, NULL, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
|
2013-02-11 21:05:42 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecdh_free( &srv );
|
|
|
|
mbedtls_ecdh_free( &cli );
|
2013-02-11 19:28:55 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2018-11-07 21:07:58 +00:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_exchange_calc_secret( int grp_id,
|
|
|
|
char *our_private_key,
|
|
|
|
char *their_point,
|
|
|
|
int ours_first,
|
|
|
|
char *expected_str )
|
|
|
|
{
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
unsigned char expected_buf[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
size_t expected_len;
|
|
|
|
mbedtls_ecp_keypair our_key;
|
|
|
|
mbedtls_ecp_keypair their_key;
|
|
|
|
mbedtls_ecdh_context ecdh;
|
|
|
|
unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
size_t shared_secret_length = 0;
|
|
|
|
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
mbedtls_ecdh_init( &ecdh );
|
|
|
|
mbedtls_ecp_keypair_init( &our_key );
|
|
|
|
mbedtls_ecp_keypair_init( &their_key );
|
|
|
|
|
|
|
|
expected_len = unhexify( expected_buf, expected_str );
|
|
|
|
|
|
|
|
if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
|
|
|
|
goto exit;
|
|
|
|
if( ! load_public_key( grp_id, their_point, &their_key ) )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* Import the keys to the ECDH calculation. */
|
|
|
|
if( ours_first )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the ECDH calculation. */
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_calc_secret(
|
|
|
|
&ecdh,
|
|
|
|
&shared_secret_length,
|
|
|
|
shared_secret, sizeof( shared_secret ),
|
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
|
|
|
TEST_ASSERT( shared_secret_length == expected_len );
|
|
|
|
TEST_ASSERT( memcmp( expected_buf, shared_secret,
|
|
|
|
shared_secret_length ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecdh_free( &ecdh );
|
|
|
|
mbedtls_ecp_keypair_free( &our_key );
|
|
|
|
mbedtls_ecp_keypair_free( &their_key );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-11-07 21:09:29 +00:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_exchange_get_params_fail( int our_grp_id,
|
|
|
|
char *our_private_key,
|
|
|
|
int their_grp_id,
|
|
|
|
char *their_point,
|
|
|
|
int ours_first,
|
|
|
|
int expected_ret )
|
|
|
|
{
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
mbedtls_ecp_keypair our_key;
|
|
|
|
mbedtls_ecp_keypair their_key;
|
|
|
|
mbedtls_ecdh_context ecdh;
|
|
|
|
|
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
mbedtls_ecdh_init( &ecdh );
|
|
|
|
mbedtls_ecp_keypair_init( &our_key );
|
|
|
|
mbedtls_ecp_keypair_init( &their_key );
|
|
|
|
|
|
|
|
if( ! load_private_key( our_grp_id, our_private_key, &our_key, &rnd_info ) )
|
|
|
|
goto exit;
|
|
|
|
if( ! load_public_key( their_grp_id, their_point, &their_key ) )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if( ours_first )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) ==
|
|
|
|
expected_ret );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) ==
|
|
|
|
expected_ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecdh_free( &ecdh );
|
|
|
|
mbedtls_ecp_keypair_free( &our_key );
|
|
|
|
mbedtls_ecp_keypair_free( &their_key );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|