Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ECP keypair export function #5642

Merged
merged 7 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.d/mbedtls_ecp_export.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Features
* Add a function to export ECP keypair parameters.
mpg marked this conversation as resolved.
Show resolved Hide resolved
Fixes #4838.
20 changes: 20 additions & 0 deletions include/mbedtls/ecp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,26 @@ int mbedtls_ecp_check_pub_priv(
const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );

/**
* \brief This function exports generic key-pair parameters.
*
* \param key The key pair to export from.
* \param grp Slot for exported ECP group.
* It must point to an initialized ECP group.
* \param d Slot for the exported secret value.
* It must point to an initialized mpi.
* \param Q Slot for the exported public value.
* It must point to an initialized ECP point.
*
* \return \c 0 on success,
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
* \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if key id doesn't
* correspond to a known group.
* \return Another negative error code on other kinds of failure.
*/
int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
mbedtls_mpi *d, mbedtls_ecp_point *Q);

#if defined(MBEDTLS_SELF_TEST)

/**
Expand Down
24 changes: 24 additions & 0 deletions library/ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3356,6 +3356,30 @@ int mbedtls_ecp_check_pub_priv(
return( ret );
}

/*
* Export generic key-pair parameters.
*/
int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
mbedtls_mpi *d, mbedtls_ecp_point *Q)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
ECP_VALIDATE_RET( key != NULL );
mpg marked this conversation as resolved.
Show resolved Hide resolved
ECP_VALIDATE_RET( grp != NULL );
ECP_VALIDATE_RET( d != NULL );
ECP_VALIDATE_RET( Q != NULL );

if( ( ret = mbedtls_ecp_group_copy( grp, &key->grp ) ) != 0 )
return ret;

if( ( ret = mbedtls_mpi_copy( d, &key->d ) ) != 0 )
return ret;

if( ( ret = mbedtls_ecp_copy( Q, &key->Q ) ) != 0 )
return ret;

return 0;
}

#if defined(MBEDTLS_SELF_TEST)

/*
Expand Down
8 changes: 8 additions & 0 deletions tests/suites/test_suite_ecp.data
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,11 @@ fix_negative:"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff":
# The first call to fix_negative in the test case of issue #4296.
ECP fix_negative: #4296.1
fix_negative:"8A4DD4C8B42C5EAED15FE4F4579F4CE513EC90A94010BF000000000000000000":-1:256

ECP export key parameters #1 (OK)
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
ecp_export:MBEDTLS_ECP_DP_SECP256R1:"37cc56d976091e5a723ec7592dff206eee7cf9069174d0ad14b5f76822596292":"4ee500d82311ffea2fd2345d5d16bd8a88c26b770d55cd8a2a0efa01c8b4edff":"00f12a1320760270a83cbffd53f6031ef76a5d86c8a204f2c30ca9ebf51f0f0ea7":0:0

ECP export key parameters #2 (invalid group)
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
ecp_export:MBEDTLS_ECP_DP_SECP256R1:"37cc56d976091e5a723ec7592dff206eee7cf9069174d0ad14b5f76822596292":"4ee500d82311ffea2fd2345d5d16bd8a88c26b770d55cd8a2a0efa01c8b4edff":"00f12a1320760270a83cbffd53f6031ef76a5d86c8a204f2c30ca9ebf51f0f0ea7":MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:1
75 changes: 75 additions & 0 deletions tests/suites/test_suite_ecp.function
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,44 @@
mbedtls_ecp_point_free( x ); \
mbedtls_ecp_point_init( x );

/* Auxiliary function to compare two mbedtls_ecp_group objects. */
inline static int mbedtls_ecp_group_cmp( mbedtls_ecp_group *P,
mpg marked this conversation as resolved.
Show resolved Hide resolved
mbedtls_ecp_group *Q )
{
if( mbedtls_mpi_cmp_mpi( &P->P, &Q->P ) != 0 )
return 1;
if( mbedtls_mpi_cmp_mpi( &P->A, &Q->A ) != 0 )
return 1;
if( mbedtls_mpi_cmp_mpi( &P->B, &Q->B ) != 0 )
return 1;
if( mbedtls_mpi_cmp_mpi( &P->N, &Q->N ) != 0 )
return 1;
if( mbedtls_ecp_point_cmp( &P->G, &Q->G ) != 0 )
return 1;
if( P->id != Q->id )
return 1;
if( P->pbits != Q->pbits )
return 1;
if( P->nbits != Q->nbits )
return 1;
if( P->h != Q->h )
return 1;
if( P->modp != Q->modp )
return 1;
if( P->t_pre != Q->t_pre )
return 1;
if( P->t_post != Q->t_post )
return 1;
if( P->t_data != Q->t_data )
return 1;
if( P->T_size != Q->T_size )
return 1;
if( P->T != Q->T )
return 1;

return 0;
}

/* END_HEADER */

/* BEGIN_DEPENDENCIES
Expand Down Expand Up @@ -988,3 +1026,40 @@ void ecp_selftest( )
TEST_ASSERT( mbedtls_ecp_self_test( 1 ) == 0 );
}
/* END_CASE */

/* BEGIN_CASE */
void ecp_export( int id, char * Qx, char * Qy,char * d, int expected_ret, int invalid_grp )
{
mbedtls_ecp_keypair key;
mbedtls_ecp_group export_grp;
mbedtls_mpi export_d;
mbedtls_ecp_point export_Q;

mbedtls_ecp_group_init( &export_grp );
mbedtls_ecp_group_init( &key.grp );
mbedtls_mpi_init( &export_d );
mbedtls_ecp_point_init( &export_Q );

mbedtls_ecp_keypair_init( &key );
if( invalid_grp == 0 )
TEST_ASSERT( mbedtls_ecp_group_load( &key.grp, id ) == 0 );
TEST_ASSERT( mbedtls_ecp_point_read_string( &key.Q, 16, Qx, Qy ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &key.d, 16, d ) == 0 );

TEST_EQUAL( mbedtls_ecp_export( &key, &export_grp,
&export_d, &export_Q ), expected_ret );

if( expected_ret == 0 )
{
TEST_EQUAL( mbedtls_ecp_point_cmp( &key.Q, &export_Q ), 0 );
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &key.d, &export_d ), 0 );
TEST_EQUAL( mbedtls_ecp_group_cmp( &key.grp, &export_grp ), 0 );
}

exit:
mbedtls_ecp_keypair_free( &key );
mbedtls_ecp_group_free( &export_grp );
mbedtls_mpi_free( &export_d );
mbedtls_ecp_point_free( &export_Q );
}
/* END_CASE */