mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-25 20:15:42 +01:00
Create functions for restartable sign
This commit is contained in:
parent
5314f234ca
commit
addb10efac
@ -187,6 +187,35 @@ int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t
|
|||||||
int (*f_rng)(void *, unsigned char *, size_t),
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
void *p_rng );
|
void *p_rng );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Restartable version of \c mbedtls_ecdsa_write_signature()
|
||||||
|
*
|
||||||
|
* \note Performs the same job as \c mbedtls_ecdsa_write_signature()
|
||||||
|
* but can return early and restart according to the limit
|
||||||
|
* set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
|
||||||
|
*
|
||||||
|
* \param ctx ECDSA context
|
||||||
|
* \param md_alg Algorithm that was used to hash the message
|
||||||
|
* \param hash Message hash
|
||||||
|
* \param hlen Length of hash
|
||||||
|
* \param sig Buffer that will hold the signature
|
||||||
|
* \param slen Length of the signature written
|
||||||
|
* \param f_rng RNG function
|
||||||
|
* \param p_rng RNG parameter
|
||||||
|
* \param rs_ctx Restart context
|
||||||
|
*
|
||||||
|
* \return See \c mbedtls_ecdsa_write_signature(), or
|
||||||
|
* MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
|
||||||
|
* operations was reached: see \c mbedtls_ecp_set_max_ops().
|
||||||
|
*/
|
||||||
|
int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
|
||||||
|
mbedtls_md_type_t md_alg,
|
||||||
|
const unsigned char *hash, size_t hlen,
|
||||||
|
unsigned char *sig, size_t *slen,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
|
void *p_rng,
|
||||||
|
mbedtls_ecdsa_restart_ctx *rs_ctx );
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
||||||
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
|
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||||
#if defined(MBEDTLS_DEPRECATED_WARNING)
|
#if defined(MBEDTLS_DEPRECATED_WARNING)
|
||||||
|
@ -127,14 +127,18 @@ cleanup:
|
|||||||
* Compute ECDSA signature of a hashed message (SEC1 4.1.3)
|
* Compute ECDSA signature of a hashed message (SEC1 4.1.3)
|
||||||
* Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
|
* Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
|
||||||
*/
|
*/
|
||||||
int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
|
||||||
|
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
||||||
|
mbedtls_ecdsa_restart_ctx *rs_ctx )
|
||||||
{
|
{
|
||||||
int ret, key_tries, sign_tries, blind_tries;
|
int ret, key_tries, sign_tries, blind_tries;
|
||||||
mbedtls_ecp_point R;
|
mbedtls_ecp_point R;
|
||||||
mbedtls_mpi k, e, t;
|
mbedtls_mpi k, e, t;
|
||||||
|
|
||||||
|
(void) rs_ctx; // temporary
|
||||||
|
|
||||||
/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
|
/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
|
||||||
if( grp->N.p == NULL )
|
if( grp->N.p == NULL )
|
||||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||||
@ -212,13 +216,26 @@ cleanup:
|
|||||||
return( ret );
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compute ECDSA signature of a hashed message
|
||||||
|
*/
|
||||||
|
int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
||||||
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
||||||
|
{
|
||||||
|
return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
|
||||||
|
f_rng, p_rng, NULL ) );
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
||||||
/*
|
/*
|
||||||
* Deterministic signature wrapper
|
* Deterministic signature wrapper
|
||||||
*/
|
*/
|
||||||
int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
|
||||||
|
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||||
mbedtls_md_type_t md_alg )
|
mbedtls_md_type_t md_alg,
|
||||||
|
mbedtls_ecdsa_restart_ctx *rs_ctx )
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
mbedtls_hmac_drbg_context rng_ctx;
|
mbedtls_hmac_drbg_context rng_ctx;
|
||||||
@ -227,6 +244,8 @@ int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi
|
|||||||
const mbedtls_md_info_t *md_info;
|
const mbedtls_md_info_t *md_info;
|
||||||
mbedtls_mpi h;
|
mbedtls_mpi h;
|
||||||
|
|
||||||
|
(void) rs_ctx; // temporary
|
||||||
|
|
||||||
if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
|
if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
|
||||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||||
|
|
||||||
@ -248,6 +267,16 @@ cleanup:
|
|||||||
|
|
||||||
return( ret );
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Deterministic signature wrapper
|
||||||
|
*/
|
||||||
|
int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
||||||
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||||
|
mbedtls_md_type_t md_alg )
|
||||||
|
{
|
||||||
|
return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg, NULL ) );
|
||||||
|
}
|
||||||
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -427,11 +456,13 @@ static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
|
|||||||
/*
|
/*
|
||||||
* Compute and write signature
|
* Compute and write signature
|
||||||
*/
|
*/
|
||||||
int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
|
int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
|
||||||
|
mbedtls_md_type_t md_alg,
|
||||||
const unsigned char *hash, size_t hlen,
|
const unsigned char *hash, size_t hlen,
|
||||||
unsigned char *sig, size_t *slen,
|
unsigned char *sig, size_t *slen,
|
||||||
int (*f_rng)(void *, unsigned char *, size_t),
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
void *p_rng )
|
void *p_rng,
|
||||||
|
mbedtls_ecdsa_restart_ctx *rs_ctx )
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
mbedtls_mpi r, s;
|
mbedtls_mpi r, s;
|
||||||
@ -443,13 +474,13 @@ int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t
|
|||||||
(void) f_rng;
|
(void) f_rng;
|
||||||
(void) p_rng;
|
(void) p_rng;
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
|
MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
|
||||||
hash, hlen, md_alg ) );
|
hash, hlen, md_alg, rs_ctx ) );
|
||||||
#else
|
#else
|
||||||
(void) md_alg;
|
(void) md_alg;
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
|
MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
|
||||||
hash, hlen, f_rng, p_rng ) );
|
hash, hlen, f_rng, p_rng, rs_ctx ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
|
MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
|
||||||
@ -461,6 +492,19 @@ cleanup:
|
|||||||
return( ret );
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compute and write signature
|
||||||
|
*/
|
||||||
|
int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
|
||||||
|
const unsigned char *hash, size_t hlen,
|
||||||
|
unsigned char *sig, size_t *slen,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
|
void *p_rng )
|
||||||
|
{
|
||||||
|
return( mbedtls_ecdsa_write_signature_restartable(
|
||||||
|
ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
|
||||||
|
}
|
||||||
|
|
||||||
#if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
|
#if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
|
||||||
defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
||||||
int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
|
int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
|
||||||
|
Loading…
Reference in New Issue
Block a user