diff --git a/include/polarssl/ecp.h b/include/polarssl/ecp.h index 33c09fcf2..7342f4505 100644 --- a/include/polarssl/ecp.h +++ b/include/polarssl/ecp.h @@ -535,6 +535,20 @@ int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); +/** + * \brief Generate a keypair + * + * \param grp_id ECP group identifier + * \param key Destination keypair + * \param f_rng RNG function + * \param p_rng RNG parameter + * + * \return 0 if successful, + * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code + */ +int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); + /** * \brief Checkup routine * diff --git a/library/ecp.c b/library/ecp.c index 0c66d352d..80a10ba94 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1669,6 +1669,20 @@ int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q, return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) ); } +/* + * Generate a keypair, prettier wrapper + */ +int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) +{ + int ret; + + if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 ) + return( ret ); + + return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) ); +} + #if defined(POLARSSL_ECP_NIST_OPTIM) /* * Fast reduction modulo the primes used by the NIST curves. diff --git a/tests/suites/test_suite_ecp.data b/tests/suites/test_suite_ecp.data index 8dafc39bd..92c490a15 100644 --- a/tests/suites/test_suite_ecp.data +++ b/tests/suites/test_suite_ecp.data @@ -247,6 +247,10 @@ ECP gen keypair depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED ecp_gen_keypair:POLARSSL_ECP_DP_SECP192R1 +ECP gen keypair wrapper +depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED +ecp_gen_key:POLARSSL_ECP_DP_SECP192R1 + ECP mod p192 small (more than 192 bits, less limbs than 2 * 192 bits) depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED ecp_fast_mod:POLARSSL_ECP_DP_SECP192R1:"0100000000000103010000000000010201000000000001010100000000000100" diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 8cc5abac3..9473dd442 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -522,6 +522,24 @@ void ecp_gen_keypair( int id ) } /* END_CASE */ +/* BEGIN_CASE */ +void ecp_gen_key( int id ) +{ + ecp_keypair key; + rnd_pseudo_info rnd_info; + + ecp_keypair_init( &key ); + memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); + + TEST_ASSERT( ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 ); + + TEST_ASSERT( ecp_check_pubkey( &key.grp, &key.Q ) == 0 ); + TEST_ASSERT( ecp_check_privkey( &key.grp, &key.d ) == 0 ); + + ecp_keypair_free( &key ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ void ecp_selftest() {