From 8eebd012b963a2f2783d54bb6e7363b451f38bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 9 Aug 2013 16:21:34 +0200 Subject: [PATCH] Add an ecdsa_genkey() function --- include/polarssl/ecdsa.h | 14 ++++++++++++++ library/ecdsa.c | 11 +++++++++++ tests/suites/test_suite_ecdsa.function | 4 +--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/polarssl/ecdsa.h b/include/polarssl/ecdsa.h index 6dcb2838b..cd1987e31 100644 --- a/include/polarssl/ecdsa.h +++ b/include/polarssl/ecdsa.h @@ -125,6 +125,20 @@ int ecdsa_read_signature( ecdsa_context *ctx, const unsigned char *hash, size_t hlen, const unsigned char *sig, size_t slen ); +/** + * \brief Generate an ECDSA keypair on the given curve + * + * \param ctx ECDSA context in which the keypair should be stored + * \param grp Group (elliptic curve) to use. One of the various + * POLARSSL_ECP_DP_XXX macros depending on configuration. + * \param f_rng RNG function + * \param p_rng RNG parameter + * + * \return 0 on success, or a POLARSSL_ERR_ECP code. + */ +int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); + /** * \brief Initialize context * diff --git a/library/ecdsa.c b/library/ecdsa.c index cf5355c17..6746233b4 100644 --- a/library/ecdsa.c +++ b/library/ecdsa.c @@ -273,6 +273,17 @@ int ecdsa_read_signature( ecdsa_context *ctx, return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) ); } +/* + * Generate key pair + */ +int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) +{ + return( ecp_use_known_dp( &ctx->grp, gid ) || + ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); +} + + /* * Initialize context */ diff --git a/tests/suites/test_suite_ecdsa.function b/tests/suites/test_suite_ecdsa.function index 46dd1b3a5..b4d6ffdc6 100644 --- a/tests/suites/test_suite_ecdsa.function +++ b/tests/suites/test_suite_ecdsa.function @@ -95,9 +95,7 @@ void ecdsa_write_read_random( int id ) TEST_ASSERT( rnd_pseudo_rand( &rnd_info, hash, sizeof( hash ) ) == 0 ); /* generate signing key */ - TEST_ASSERT( ecp_use_known_dp( &ctx.grp, id ) == 0 ); - TEST_ASSERT( ecp_gen_keypair( &ctx.grp, &ctx.d, &ctx.Q, - &rnd_pseudo_rand, &rnd_info ) == 0 ); + TEST_ASSERT( ecdsa_genkey( &ctx, id, &rnd_pseudo_rand, &rnd_info ) == 0 ); /* generate and write signature, then read and verify it */ TEST_ASSERT( ecdsa_write_signature( &ctx, hash, sizeof( hash ),