Skip to content

Commit

Permalink
ECDSA: Add mbedtls_ecdsa_can_do
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph M. Wintersteiger authored and yanesca committed Aug 19, 2019
1 parent 8a0f5bb commit 0082f9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
11 changes: 10 additions & 1 deletion include/mbedtls/ecdsa.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ typedef void mbedtls_ecdsa_restart_ctx;

#endif /* MBEDTLS_ECP_RESTARTABLE */

/**
* \brief This function checks whether a given group can be used
* for ECDSA.
*
* \param gid The ECP group ID to check.
*
* \return \c 1 if the group can be used, \c 0 otherwise
*/
int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid );

/**
* \brief This function computes the ECDSA signature of a
* previously-hashed message.
Expand Down Expand Up @@ -469,7 +479,6 @@ int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
const unsigned char *sig, size_t slen,
mbedtls_ecdsa_restart_ctx *rs_ctx );

/**
* \brief This function generates an ECDSA keypair on the given curve.
*
Expand Down
20 changes: 16 additions & 4 deletions library/ecdsa.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,7 @@ static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
mbedtls_mpi *pk = &k, *pr = r;

/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
if( grp->id == MBEDTLS_ECP_DP_CURVE25519 ||
grp->id == MBEDTLS_ECP_DP_CURVE448 ||
grp->N.p == NULL )
if( !mbedtls_ecdsa_can_do( grp->id ) || grp->N.p == NULL )
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );

/* Make sure d is in range 1..n-1 */
Expand Down Expand Up @@ -380,6 +378,20 @@ static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
return( ret );
}

int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid )
{
switch( gid )
{
#ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
case MBEDTLS_ECP_DP_CURVE25519: return 0;
#endif
#ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
case MBEDTLS_ECP_DP_CURVE448: return 0;
#endif
default: return 1;
}
}

/*
* Compute ECDSA signature of a hashed message
*/
Expand Down Expand Up @@ -504,7 +516,7 @@ static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );

/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
if( grp->N.p == NULL )
if( !mbedtls_ecdsa_can_do( grp->id ) || grp->N.p == NULL )
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );

ECDSA_RS_ENTER( ver );
Expand Down
6 changes: 6 additions & 0 deletions programs/test/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ int main( int argc, char *argv[] )
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
if( mbedtls_ecdsa_can_do( curve_info->grp_id ) == 0 )
continue;

mbedtls_ecdsa_init( &ecdsa );

if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Expand All @@ -854,6 +857,9 @@ int main( int argc, char *argv[] )
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
if( mbedtls_ecdsa_can_do( curve_info->grp_id ) == 0 )
continue;

mbedtls_ecdsa_init( &ecdsa );

if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Expand Down

0 comments on commit 0082f9d

Please sign in to comment.