From b8c6e0e3e9eee83c481aef30d78296d612301e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 1 Jul 2013 13:40:52 +0200 Subject: [PATCH] Add ecp_keypair struct, init/free and constants --- include/polarssl/ecp.h | 39 +++++++++++++++++++++++++++++++++++++++ library/ecp.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/polarssl/ecp.h b/include/polarssl/ecp.h index 4b0218180..db59a9350 100644 --- a/include/polarssl/ecp.h +++ b/include/polarssl/ecp.h @@ -91,6 +91,25 @@ typedef struct } ecp_group; +/** + * \brief ECP key pair structure + * + * A generic key pair that could be used for ECDSA, fixed ECDH, etc. + * Usage can be restricted to a particular algorithm by the 'alg' field, + * see POLARSSL_ECP_KEY_ALG_* constants (default: unrestricted). + * + * \sa ecdh_context + * \sa ecdsa_context + */ +typedef struct +{ + ecp_group grp; /*!< Elliptic curve and base point */ + mpi d; /*!< our secret value */ + ecp_point Q; /*!< our public value */ + int alg; /*!< algorithm to use this key with */ +} +ecp_keypair; + /** * RFC 5114 defines a number of standardized ECP groups for use with TLS. * @@ -139,6 +158,16 @@ ecp_group; */ #define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */ +/* + * Algorithm identifiers from RFC 5480 for use with EC keys + */ +#define POLARSSL_ECP_KEY_ALG_UNRESTRICTED 0 /**< RFC 5480 2.1.1 */ +#define POLARSSL_ECP_KEY_ALG_ECDH 1 /**< RFC 5480 2.1.2 */ + +#ifdef __cplusplus +extern "C" { +#endif + /** * \brief Initialize a point (as zero) */ @@ -149,6 +178,11 @@ void ecp_point_init( ecp_point *pt ); */ void ecp_group_init( ecp_group *grp ); +/** + * \brief Initialize a key pair (as an invalid one) + */ +void ecp_keypair_init( ecp_keypair *key ); + /** * \brief Free the components of a point */ @@ -159,6 +193,11 @@ void ecp_point_free( ecp_point *pt ); */ void ecp_group_free( ecp_group *grp ); +/** + * \brief Free the components of a key pair + */ +void ecp_keypair_free( ecp_keypair *key ); + /** * \brief Set a point to zero * diff --git a/library/ecp.c b/library/ecp.c index af18e5bee..216fc4330 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -90,6 +90,20 @@ void ecp_group_init( ecp_group *grp ) grp->modp = NULL; } +/* + * Initialize (the components of) a key pair + */ +void ecp_keypair_init( ecp_keypair *key ) +{ + if ( key == NULL ) + return; + + ecp_group_init( &key->grp ); + mpi_init( &key->d ); + ecp_point_init( &key->Q ); + key->alg = POLARSSL_ECP_KEY_ALG_UNRESTRICTED; +} + /* * Unallocate (the components of) a point */ @@ -117,6 +131,20 @@ void ecp_group_free( ecp_group *grp ) mpi_free( &grp->N ); } +/* + * Unallocate (the components of) a key pair + */ +void ecp_keypair_free( ecp_keypair *key ) +{ + if ( key == NULL ) + return; + + ecp_group_free( &key->grp ); + mpi_free( &key->d ); + ecp_point_free( &key->Q ); + key->alg = POLARSSL_ECP_KEY_ALG_UNRESTRICTED; +} + /* * Set point to zero */