mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2025-12-24 08:16:33 +01:00
Dissociate TLS and internal EC curve identifiers
Allows to add new curves before they get a TLS number
This commit is contained in:
parent
ef009ffde9
commit
7038039f2e
4 changed files with 119 additions and 51 deletions
|
|
@ -655,9 +655,11 @@ int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
|
|||
SECP521R1_P, SECP521R1_B,
|
||||
SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) );
|
||||
#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
|
||||
}
|
||||
|
||||
return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
|
||||
default:
|
||||
grp->id = POLARSSL_ECP_DP_NONE;
|
||||
return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -665,7 +667,7 @@ int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
|
|||
*/
|
||||
int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
|
||||
{
|
||||
ecp_group_id id;
|
||||
unsigned int named_curve;
|
||||
|
||||
/*
|
||||
* We expect at least three bytes (see below)
|
||||
|
|
@ -682,10 +684,10 @@ int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
|
|||
/*
|
||||
* Next two bytes are the namedcurve value
|
||||
*/
|
||||
id = *(*buf)++;
|
||||
id <<= 8;
|
||||
id |= *(*buf)++;
|
||||
return ecp_use_known_dp( grp, id );
|
||||
named_curve = *(*buf)++;
|
||||
named_curve <<= 8;
|
||||
named_curve |= *(*buf)++;
|
||||
return ecp_use_known_dp( grp, ecp_grp_id_from_named_curve( named_curve ) );
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -694,6 +696,8 @@ int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
|
|||
int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
|
||||
unsigned char *buf, size_t blen )
|
||||
{
|
||||
unsigned int named_curve;
|
||||
|
||||
/*
|
||||
* We are going to write 3 bytes (see below)
|
||||
*/
|
||||
|
|
@ -709,12 +713,61 @@ int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
|
|||
/*
|
||||
* Next two bytes are the namedcurve value
|
||||
*/
|
||||
buf[0] = grp->id >> 8;
|
||||
buf[1] = grp->id & 0xFF;
|
||||
named_curve = ecp_named_curve_from_grp_id( grp->id );
|
||||
buf[0] = named_curve >> 8;
|
||||
buf[1] = named_curve & 0xFF;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Hard-coded values are temporary, will be reimplemented soon */
|
||||
ecp_group_id ecp_grp_id_from_named_curve( unsigned int curve )
|
||||
{
|
||||
switch( curve )
|
||||
{
|
||||
case 19:
|
||||
return( POLARSSL_ECP_DP_SECP192R1 );
|
||||
|
||||
case 21:
|
||||
return( POLARSSL_ECP_DP_SECP224R1 );
|
||||
|
||||
case 23:
|
||||
return( POLARSSL_ECP_DP_SECP256R1 );
|
||||
|
||||
case 24:
|
||||
return( POLARSSL_ECP_DP_SECP384R1 );
|
||||
|
||||
case 25:
|
||||
return( POLARSSL_ECP_DP_SECP521R1 );
|
||||
|
||||
default:
|
||||
return( POLARSSL_ECP_DP_NONE );
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int ecp_named_curve_from_grp_id( ecp_group_id id )
|
||||
{
|
||||
switch( id )
|
||||
{
|
||||
case POLARSSL_ECP_DP_SECP192R1:
|
||||
return( 19 );
|
||||
|
||||
case POLARSSL_ECP_DP_SECP224R1:
|
||||
return( 21 );
|
||||
|
||||
case POLARSSL_ECP_DP_SECP256R1:
|
||||
return( 23 );
|
||||
|
||||
case POLARSSL_ECP_DP_SECP384R1:
|
||||
return( 24 );
|
||||
|
||||
case POLARSSL_ECP_DP_SECP521R1:
|
||||
return( 25 );
|
||||
|
||||
default:
|
||||
return( 0 );
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Fast mod-p functions expect their argument to be in the 0..p^2 range.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -241,23 +241,23 @@ static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
|
|||
|
||||
#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
|
||||
elliptic_curve_list[elliptic_curve_len++] = 0x00;
|
||||
elliptic_curve_list[elliptic_curve_len++] = POLARSSL_ECP_DP_SECP521R1;
|
||||
elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP521R1 );
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
|
||||
elliptic_curve_list[elliptic_curve_len++] = 0x00;
|
||||
elliptic_curve_list[elliptic_curve_len++] = POLARSSL_ECP_DP_SECP384R1;
|
||||
elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP384R1 );
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
|
||||
elliptic_curve_list[elliptic_curve_len++] = 0x00;
|
||||
elliptic_curve_list[elliptic_curve_len++] = POLARSSL_ECP_DP_SECP256R1;
|
||||
elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP256R1 );
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
|
||||
elliptic_curve_list[elliptic_curve_len++] = 0x00;
|
||||
elliptic_curve_list[elliptic_curve_len++] = POLARSSL_ECP_DP_SECP224R1;
|
||||
elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP224R1 );
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
|
||||
elliptic_curve_list[elliptic_curve_len++] = 0x00;
|
||||
elliptic_curve_list[elliptic_curve_len++] = POLARSSL_ECP_DP_SECP192R1;
|
||||
elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP192R1 );
|
||||
#endif
|
||||
|
||||
if( elliptic_curve_len == 0 )
|
||||
|
|
|
|||
|
|
@ -503,6 +503,7 @@ static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
|
|||
{
|
||||
size_t list_size;
|
||||
const unsigned char *p;
|
||||
ecp_group_id grp_id;
|
||||
|
||||
list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
|
||||
if( list_size + 2 != len ||
|
||||
|
|
@ -515,38 +516,39 @@ static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
|
|||
p = buf + 2;
|
||||
while( list_size > 0 )
|
||||
{
|
||||
grp_id = ecp_grp_id_from_named_curve( ( p[0] << 8 ) | p[1] );
|
||||
#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
|
||||
if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
|
||||
if( grp_id == POLARSSL_ECP_DP_SECP192R1 )
|
||||
{
|
||||
ssl->handshake->ec_curve = p[1];
|
||||
ssl->handshake->ec_curve = grp_id;
|
||||
return( 0 );
|
||||
}
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
|
||||
if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
|
||||
if( grp_id == POLARSSL_ECP_DP_SECP224R1 )
|
||||
{
|
||||
ssl->handshake->ec_curve = p[1];
|
||||
ssl->handshake->ec_curve = grp_id;
|
||||
return( 0 );
|
||||
}
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
|
||||
if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
|
||||
if( grp_id == POLARSSL_ECP_DP_SECP256R1 )
|
||||
{
|
||||
ssl->handshake->ec_curve = p[1];
|
||||
ssl->handshake->ec_curve = grp_id;
|
||||
return( 0 );
|
||||
}
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
|
||||
if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
|
||||
if( grp_id == POLARSSL_ECP_DP_SECP384R1 )
|
||||
{
|
||||
ssl->handshake->ec_curve = p[1];
|
||||
ssl->handshake->ec_curve = grp_id;
|
||||
return( 0 );
|
||||
}
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
|
||||
if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
|
||||
if( grp_id == POLARSSL_ECP_DP_SECP521R1 )
|
||||
{
|
||||
ssl->handshake->ec_curve = p[1];
|
||||
ssl->handshake->ec_curve = grp_id;
|
||||
return( 0 );
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue