Add ecp_tls_write_group()

This commit is contained in:
Manuel Pégourié-Gonnard 2013-02-10 12:06:19 +01:00
parent 6282acaec2
commit b325887fad
2 changed files with 56 additions and 7 deletions

View File

@ -53,6 +53,11 @@ typedef struct
} }
ecp_point; ecp_point;
/*
* RFC 4492 defines an enum NamedCurve with two-bytes values
*/
typedef uint16_t ecp_group_id;
/** /**
* \brief ECP group structure * \brief ECP group structure
* *
@ -70,6 +75,7 @@ ecp_point;
*/ */
typedef struct typedef struct
{ {
ecp_group_id id; /*!< RFC 4492 group ID */
mpi P; /*!< prime modulus of the base field */ mpi P; /*!< prime modulus of the base field */
mpi B; /*!< constant term in the equation */ mpi B; /*!< constant term in the equation */
ecp_point G; /*!< generator of the subgroup used */ ecp_point G; /*!< generator of the subgroup used */
@ -284,7 +290,7 @@ int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
* \note Index should be a value of RFC 4492's enum NamdeCurve, * \note Index should be a value of RFC 4492's enum NamdeCurve,
* possibly in the form of a POLARSSL_ECP_DP_XXX macro. * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
*/ */
int ecp_use_known_dp( ecp_group *grp, uint16_t index ); int ecp_use_known_dp( ecp_group *grp, ecp_group_id id );
/** /**
* \brief Set a group from a TLS ECParameters record * \brief Set a group from a TLS ECParameters record
@ -299,6 +305,20 @@ int ecp_use_known_dp( ecp_group *grp, uint16_t index );
*/ */
int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len ); int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len );
/**
* \brief Write the TLS ECParameters record for a group
*
* \param grp ECP group used
* \param olen Number of bytes actually written
* \param buf Buffer to write to
* \param blen Buffer length
*
* \return 0 if successful,
* or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
*/
int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
unsigned char *buf, size_t blen );
/** /**
* \brief Import a point from a TLS ECPoint record * \brief Import a point from a TLS ECPoint record
* *

View File

@ -534,9 +534,11 @@ cleanup:
/* /*
* Set a group using well-known domain parameters * Set a group using well-known domain parameters
*/ */
int ecp_use_known_dp( ecp_group *grp, uint16_t index ) int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
{ {
switch( index ) grp->id = id;
switch( id )
{ {
case POLARSSL_ECP_DP_SECP192R1: case POLARSSL_ECP_DP_SECP192R1:
grp->modp = ecp_mod_p192; grp->modp = ecp_mod_p192;
@ -574,7 +576,7 @@ int ecp_use_known_dp( ecp_group *grp, uint16_t index )
*/ */
int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len ) int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len )
{ {
uint16_t namedcurve; ecp_group_id id;
/* /*
* We expect at least three bytes (see below) * We expect at least three bytes (see below)
@ -589,10 +591,37 @@ int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len )
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
/* /*
* Next two bytes are the namedcurve * Next two bytes are the namedcurve value
*/ */
namedcurve = 256 * buf[0] + buf[1]; id = 256 * buf[0] + buf[1];
return ecp_use_known_dp( grp, namedcurve ); return ecp_use_known_dp( grp, id );
}
/*
* Write the ECParameters record corresponding to a group (RFC 4492)
*/
int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
unsigned char *buf, size_t blen )
{
/*
* We are going to write 3 bytes (see below)
*/
*olen = 3;
if( blen < *olen )
return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
/*
* First byte is curve_type, always named_curve
*/
*buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
/*
* Next two bytes are the namedcurve value
*/
buf[0] = grp->id >> 8;
buf[1] = grp->id && 0xFF;
return 0;
} }
/* /*