diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index 989557367..e71a44510 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -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 kp 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) /** diff --git a/library/ecp.c b/library/ecp.c index ba76abbd1..71114cd90 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -3356,6 +3356,30 @@ cleanup: 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 ); + 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) /*