Add ecp_keypair struct, init/free and constants

This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-01 13:40:52 +02:00
parent 7c8934ea0e
commit b8c6e0e3e9
2 changed files with 67 additions and 0 deletions

View File

@ -91,6 +91,25 @@ typedef struct
} }
ecp_group; 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. * 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 */ #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) * \brief Initialize a point (as zero)
*/ */
@ -149,6 +178,11 @@ void ecp_point_init( ecp_point *pt );
*/ */
void ecp_group_init( ecp_group *grp ); 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 * \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 ); 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 * \brief Set a point to zero
* *

View File

@ -90,6 +90,20 @@ void ecp_group_init( ecp_group *grp )
grp->modp = NULL; 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 * Unallocate (the components of) a point
*/ */
@ -117,6 +131,20 @@ void ecp_group_free( ecp_group *grp )
mpi_free( &grp->N ); 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 * Set point to zero
*/ */