mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-05-12 19:32:15 +00:00
mbedtls_ecp_gen_privkey: create subfunctions for each curve type
Put the Montgomery and short Weierstrass implementations of mbedtls_ecp_gen_privkey into their own function which can be tested independently, but will not be part of the public ABI/API. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
parent
48f052fb35
commit
de33213f23
|
@ -3040,28 +3040,14 @@ int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
|
||||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
||||||
* Generate a private key
|
MBEDTLS_STATIC_TESTABLE
|
||||||
*/
|
int mbedtls_ecp_gen_privkey_mx( size_t n_bits,
|
||||||
int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
|
|
||||||
mbedtls_mpi *d,
|
mbedtls_mpi *d,
|
||||||
int (*f_rng)(void *, unsigned char *, size_t),
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
void *p_rng )
|
void *p_rng )
|
||||||
{
|
{
|
||||||
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||||
size_t n_bits;
|
|
||||||
const mbedtls_mpi *N = NULL;
|
|
||||||
|
|
||||||
ECP_VALIDATE_RET( grp != NULL );
|
|
||||||
ECP_VALIDATE_RET( d != NULL );
|
|
||||||
ECP_VALIDATE_RET( f_rng != NULL );
|
|
||||||
|
|
||||||
N = &grp->N;
|
|
||||||
n_bits = grp->nbits;
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
|
||||||
if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
|
|
||||||
{
|
|
||||||
size_t b;
|
size_t b;
|
||||||
size_t n_bytes = ( n_bits + 7 ) / 8;
|
size_t n_bytes = ( n_bits + 7 ) / 8;
|
||||||
|
|
||||||
|
@ -3085,13 +3071,21 @@ int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
|
||||||
{
|
{
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
|
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
cleanup:
|
||||||
|
return( ret );
|
||||||
|
}
|
||||||
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
||||||
if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
|
MBEDTLS_STATIC_TESTABLE
|
||||||
{
|
int mbedtls_ecp_gen_privkey_sw( const mbedtls_mpi *N, size_t n_bits,
|
||||||
|
mbedtls_mpi *d,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
|
void *p_rng )
|
||||||
|
{
|
||||||
/* SEC1 3.2.1: Generate d such that 1 <= n < N */
|
/* SEC1 3.2.1: Generate d such that 1 <= n < N */
|
||||||
|
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
unsigned cmp = 0;
|
unsigned cmp = 0;
|
||||||
size_t n_bytes = ( n_bits + 7 ) / 8;
|
size_t n_bytes = ( n_bits + 7 ) / 8;
|
||||||
|
@ -3131,12 +3125,37 @@ int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
|
while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
|
||||||
}
|
|
||||||
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
return( ret );
|
return( ret );
|
||||||
}
|
}
|
||||||
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generate a private key
|
||||||
|
*/
|
||||||
|
int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
|
||||||
|
mbedtls_mpi *d,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
|
void *p_rng )
|
||||||
|
{
|
||||||
|
ECP_VALIDATE_RET( grp != NULL );
|
||||||
|
ECP_VALIDATE_RET( d != NULL );
|
||||||
|
ECP_VALIDATE_RET( f_rng != NULL );
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
||||||
|
if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
|
||||||
|
return( mbedtls_ecp_gen_privkey_mx( grp->nbits, d, f_rng, p_rng ) );
|
||||||
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
||||||
|
if( mbedtls_ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
|
||||||
|
return( mbedtls_ecp_gen_privkey_sw( &grp->N, grp->nbits, d,
|
||||||
|
f_rng, p_rng ) );
|
||||||
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
||||||
|
|
||||||
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Generate a keypair with configurable base point
|
* Generate a keypair with configurable base point
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#define MBEDTLS_ECP_INVASIVE_H
|
#define MBEDTLS_ECP_INVASIVE_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "mbedtls/bignum.h"
|
||||||
#include "mbedtls/ecp.h"
|
#include "mbedtls/ecp.h"
|
||||||
|
|
||||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
|
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
|
||||||
|
@ -46,6 +47,57 @@
|
||||||
void mbedtls_ecp_fix_negative( mbedtls_mpi *N, signed char c, size_t bits );
|
void mbedtls_ecp_fix_negative( mbedtls_mpi *N, signed char c, size_t bits );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
||||||
|
/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
|
||||||
|
*
|
||||||
|
* This function implements key generation for the set of secret keys
|
||||||
|
* specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
|
||||||
|
* has the lower bits masked but is not necessarily canonical.
|
||||||
|
*
|
||||||
|
* \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
|
||||||
|
* - [RFC7748] https://tools.ietf.org/html/rfc7748
|
||||||
|
*
|
||||||
|
* \p n_bits The position of the high-order bit of the key to generate.
|
||||||
|
* This is the bit-size of the key minus 1:
|
||||||
|
* 254 for Curve25519 or 447 for Curve448.
|
||||||
|
* \param d The randomly generated key. This is a number of size
|
||||||
|
* exactly \p n_bits + 1 bits, with the least significant bits
|
||||||
|
* masked as specified in [Curve25519] and in [RFC7748] §5.
|
||||||
|
* \param f_rng The RNG function.
|
||||||
|
* \param p_rng The RNG context to be passed to \p f_rng.
|
||||||
|
*
|
||||||
|
* \return \c 0 on success.
|
||||||
|
* \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
|
||||||
|
*/
|
||||||
|
int mbedtls_ecp_gen_privkey_mx( size_t n_bits,
|
||||||
|
mbedtls_mpi *d,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
|
void *p_rng );
|
||||||
|
|
||||||
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
||||||
|
/** Generate a private key on a short Weierstrass curve.
|
||||||
|
*
|
||||||
|
* The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
|
||||||
|
* when the RNG is a suitably parametrized instance of HMAC_DRBG.
|
||||||
|
*
|
||||||
|
* \p N The upper bound of the range.
|
||||||
|
* \p n_bits The size of \p N in bits. This value must be correct,
|
||||||
|
* otherwise the result is unpredictable.
|
||||||
|
* \param d A random number, uniformly generated in the range [1, N-1].
|
||||||
|
* \param f_rng The RNG function.
|
||||||
|
* \param p_rng The RNG context to be passed to \p f_rng.
|
||||||
|
*
|
||||||
|
* \return \c 0 on success.
|
||||||
|
* \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
|
||||||
|
*/
|
||||||
|
int mbedtls_ecp_gen_privkey_sw( const mbedtls_mpi *N, size_t n_bits,
|
||||||
|
mbedtls_mpi *d,
|
||||||
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
|
void *p_rng );
|
||||||
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
||||||
|
|
||||||
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
|
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
|
||||||
|
|
||||||
#endif /* MBEDTLS_ECP_INVASIVE_H */
|
#endif /* MBEDTLS_ECP_INVASIVE_H */
|
||||||
|
|
Loading…
Reference in a new issue