2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecdh.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_ECDH_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_primitive_random( int id )
|
2013-01-26 16:20:32 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point qA, qB;
|
|
|
|
mbedtls_mpi dA, dB, zA, zB;
|
2013-01-26 16:20:32 +01:00
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
2015-04-08 12:49:31 +02: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 16:20:32 +01:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
|
2013-01-26 16:20:32 +01:00
|
|
|
== 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
|
2013-01-26 16:20:32 +01:00
|
|
|
== 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA,
|
2013-09-02 14:29:09 +02:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB,
|
2013-09-02 14:29:09 +02:00
|
|
|
NULL, NULL ) == 0 );
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 );
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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 16:20:32 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void ecdh_primitive_testvec( int id, data_t * rnd_buf_A, char * xA_str,
|
|
|
|
char * yA_str, data_t * rnd_buf_B,
|
2017-06-09 05:32:58 +02:00
|
|
|
char * xB_str, char * yB_str, char * z_str )
|
2013-01-27 08:56:21 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point qA, qB;
|
|
|
|
mbedtls_mpi dA, dB, zA, zB, check;
|
2014-01-23 16:55:18 +01:00
|
|
|
rnd_buf_info rnd_info_A, rnd_info_B;
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2015-04-08 12:49:31 +02: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 08:56:21 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_info_A.buf = rnd_buf_A->x;
|
|
|
|
rnd_info_A.length = rnd_buf_A->len;
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Fix rnd_buf_A->x by shifting it left if necessary */
|
2014-01-23 16:55:18 +01:00
|
|
|
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++ )
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_A->x[i] = rnd_buf_A->x[i] << shift
|
|
|
|
| rnd_buf_A->x[i+1] >> ( 8 - shift );
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_A->x[rnd_info_A.length-1] <<= shift;
|
2014-01-23 16:55:18 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_info_B.buf = rnd_buf_B->x;
|
|
|
|
rnd_info_B.length = rnd_buf_B->len;
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Fix rnd_buf_B->x by shifting it left if necessary */
|
2014-01-23 16:55:18 +01:00
|
|
|
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++ )
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_B->x[i] = rnd_buf_B->x[i] << shift
|
|
|
|
| rnd_buf_B->x[i+1] >> ( 8 - shift );
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_B->x[rnd_info_B.length-1] <<= shift;
|
2014-01-23 16:55:18 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
|
2014-01-23 16:55:18 +01:00
|
|
|
rnd_buffer_rand, &rnd_info_A ) == 0 );
|
2015-04-08 12:49:31 +02: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 08:56:21 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
|
2014-01-23 16:55:18 +01:00
|
|
|
rnd_buffer_rand, &rnd_info_B ) == 0 );
|
2015-04-08 12:49:31 +02: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 08:56:21 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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 08:56:21 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-11 20:28:55 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdh_exchange( int id )
|
2013-02-11 20:28:55 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdh_context srv, cli;
|
2013-02-11 20:28:55 +01:00
|
|
|
unsigned char buf[1000];
|
|
|
|
const unsigned char *vbuf;
|
|
|
|
size_t len;
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdh_init( &srv );
|
|
|
|
mbedtls_ecdh_init( &cli );
|
2013-02-11 20:28:55 +01:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 );
|
2013-02-11 20:28:55 +01:00
|
|
|
|
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
|
2013-02-11 20:28:55 +01:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
|
2013-02-11 20:28:55 +01:00
|
|
|
|
2013-02-11 22:05:42 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
|
2013-02-11 21:51:45 +01:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
|
2013-02-11 21:51:45 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
|
2013-09-02 14:29:09 +02:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02: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 22:05:42 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdh_free( &srv );
|
|
|
|
mbedtls_ecdh_free( &cli );
|
2013-02-11 20:28:55 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
void ecdh_restart( int id, char *dA_str, char *dB_str, char *z_str,
|
2017-05-18 12:35:37 +02:00
|
|
|
int enable, int max_ops, int min_restart, int max_restart )
|
2017-04-27 10:38:52 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_ecdh_context srv, cli;
|
|
|
|
unsigned char buf[1000];
|
|
|
|
const unsigned char *vbuf;
|
|
|
|
size_t len;
|
|
|
|
unsigned char z[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
size_t z_len;
|
|
|
|
unsigned char rnd_buf_A[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
unsigned char rnd_buf_B[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
rnd_buf_info rnd_info_A, rnd_info_B;
|
|
|
|
int cnt_restart;
|
|
|
|
|
|
|
|
mbedtls_ecdh_init( &srv );
|
|
|
|
mbedtls_ecdh_init( &cli );
|
|
|
|
|
|
|
|
z_len = unhexify( z, z_str );
|
|
|
|
|
|
|
|
rnd_info_A.buf = rnd_buf_A;
|
|
|
|
rnd_info_A.length = unhexify( rnd_buf_A, dA_str );
|
|
|
|
|
|
|
|
rnd_info_B.buf = rnd_buf_B;
|
|
|
|
rnd_info_B.length = unhexify( rnd_buf_B, dB_str );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 );
|
|
|
|
|
|
|
|
/* otherwise we would have to fix the random buffer,
|
|
|
|
* as in ecdh_primitive_test_vec */
|
|
|
|
TEST_ASSERT( srv.grp.nbits % 8 == 0 );
|
|
|
|
|
2017-05-18 12:35:37 +02:00
|
|
|
/* set up restart parameters */
|
2017-04-27 10:38:52 +02:00
|
|
|
mbedtls_ecp_set_max_ops( max_ops );
|
|
|
|
|
2017-05-18 12:35:37 +02:00
|
|
|
if( enable)
|
|
|
|
{
|
|
|
|
mbedtls_ecdh_enable_restart( &srv );
|
|
|
|
mbedtls_ecdh_enable_restart( &cli );
|
|
|
|
}
|
|
|
|
|
2017-04-27 10:38:52 +02:00
|
|
|
/* server writes its paramaters */
|
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_ecdh_make_params( &srv, &len, buf, sizeof( buf ),
|
|
|
|
rnd_buffer_rand, &rnd_info_A );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
/* client read server params */
|
|
|
|
vbuf = buf;
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
|
|
|
|
|
|
|
|
/* client writes its key share */
|
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_ecdh_make_public( &cli, &len, buf, sizeof( buf ),
|
|
|
|
rnd_buffer_rand, &rnd_info_B );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
/* server reads client key share */
|
|
|
|
TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
|
|
|
|
|
|
|
|
/* server computes shared secret */
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_ecdh_calc_secret( &srv, &len, buf, sizeof( buf ),
|
|
|
|
NULL, NULL );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( len == z_len );
|
|
|
|
TEST_ASSERT( memcmp( buf, z, len ) == 0 );
|
|
|
|
|
|
|
|
/* client computes shared secret */
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_ecdh_calc_secret( &cli, &len, buf, sizeof( buf ),
|
|
|
|
NULL, NULL );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( len == z_len );
|
|
|
|
TEST_ASSERT( memcmp( buf, z, len ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecdh_free( &srv );
|
|
|
|
mbedtls_ecdh_free( &cli );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|