Remove "internal RNG" code from ECP
This is no longer needed, as the RNG param is now mandatory. Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
parent
d51aaad4c9
commit
7962bfaa79
224
library/ecp.c
224
library/ecp.c
@ -101,16 +101,6 @@
|
|||||||
|
|
||||||
#include "ecp_internal_alt.h"
|
#include "ecp_internal_alt.h"
|
||||||
|
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
#if defined(MBEDTLS_HMAC_DRBG_C)
|
|
||||||
#include "mbedtls/hmac_drbg.h"
|
|
||||||
#elif defined(MBEDTLS_CTR_DRBG_C)
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
#else
|
|
||||||
#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
|
|
||||||
#endif
|
|
||||||
#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
||||||
|
|
||||||
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
||||||
!defined(inline) && !defined(__cplusplus)
|
!defined(inline) && !defined(__cplusplus)
|
||||||
#define inline __inline
|
#define inline __inline
|
||||||
@ -124,144 +114,6 @@
|
|||||||
static unsigned long add_count, dbl_count, mul_count;
|
static unsigned long add_count, dbl_count, mul_count;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
/*
|
|
||||||
* Currently ecp_mul() takes a RNG function as an argument, used for
|
|
||||||
* side-channel protection, but it can be NULL. The initial reasoning was
|
|
||||||
* that people will pass non-NULL RNG when they care about side-channels, but
|
|
||||||
* unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
|
|
||||||
* no opportunity for the user to do anything about it.
|
|
||||||
*
|
|
||||||
* The obvious strategies for addressing that include:
|
|
||||||
* - change those APIs so that they take RNG arguments;
|
|
||||||
* - require a global RNG to be available to all crypto modules.
|
|
||||||
*
|
|
||||||
* Unfortunately those would break compatibility. So what we do instead is
|
|
||||||
* have our own internal DRBG instance, seeded from the secret scalar.
|
|
||||||
*
|
|
||||||
* The following is a light-weight abstraction layer for doing that with
|
|
||||||
* HMAC_DRBG (first choice) or CTR_DRBG.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HMAC_DRBG_C)
|
|
||||||
|
|
||||||
/* DRBG context type */
|
|
||||||
typedef mbedtls_hmac_drbg_context ecp_drbg_context;
|
|
||||||
|
|
||||||
/* DRBG context init */
|
|
||||||
static inline void ecp_drbg_init( ecp_drbg_context *ctx )
|
|
||||||
{
|
|
||||||
mbedtls_hmac_drbg_init( ctx );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DRBG context free */
|
|
||||||
static inline void ecp_drbg_free( ecp_drbg_context *ctx )
|
|
||||||
{
|
|
||||||
mbedtls_hmac_drbg_free( ctx );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DRBG function */
|
|
||||||
static inline int ecp_drbg_random( void *p_rng,
|
|
||||||
unsigned char *output, size_t output_len )
|
|
||||||
{
|
|
||||||
return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DRBG context seeding */
|
|
||||||
static int ecp_drbg_seed( ecp_drbg_context *ctx,
|
|
||||||
const mbedtls_mpi *secret, size_t secret_len )
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
|
|
||||||
/* The list starts with strong hashes */
|
|
||||||
const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
|
|
||||||
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
|
|
||||||
|
|
||||||
if( secret_len > MBEDTLS_ECP_MAX_BYTES )
|
|
||||||
{
|
|
||||||
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret,
|
|
||||||
secret_bytes, secret_len ) );
|
|
||||||
|
|
||||||
ret = mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_bytes, secret_len );
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
mbedtls_platform_zeroize( secret_bytes, secret_len );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(MBEDTLS_CTR_DRBG_C)
|
|
||||||
|
|
||||||
/* DRBG context type */
|
|
||||||
typedef mbedtls_ctr_drbg_context ecp_drbg_context;
|
|
||||||
|
|
||||||
/* DRBG context init */
|
|
||||||
static inline void ecp_drbg_init( ecp_drbg_context *ctx )
|
|
||||||
{
|
|
||||||
mbedtls_ctr_drbg_init( ctx );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DRBG context free */
|
|
||||||
static inline void ecp_drbg_free( ecp_drbg_context *ctx )
|
|
||||||
{
|
|
||||||
mbedtls_ctr_drbg_free( ctx );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DRBG function */
|
|
||||||
static inline int ecp_drbg_random( void *p_rng,
|
|
||||||
unsigned char *output, size_t output_len )
|
|
||||||
{
|
|
||||||
return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
|
|
||||||
* we need to pass an entropy function when seeding. So we use a dummy
|
|
||||||
* function for that, and pass the actual entropy as customisation string.
|
|
||||||
* (During seeding of CTR_DRBG the entropy input and customisation string are
|
|
||||||
* concatenated before being used to update the secret state.)
|
|
||||||
*/
|
|
||||||
static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
|
|
||||||
{
|
|
||||||
(void) ctx;
|
|
||||||
memset( out, 0, len );
|
|
||||||
return( 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DRBG context seeding */
|
|
||||||
static int ecp_drbg_seed( ecp_drbg_context *ctx,
|
|
||||||
const mbedtls_mpi *secret, size_t secret_len )
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
|
|
||||||
|
|
||||||
if( secret_len > MBEDTLS_ECP_MAX_BYTES )
|
|
||||||
{
|
|
||||||
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret,
|
|
||||||
secret_bytes, secret_len ) );
|
|
||||||
|
|
||||||
ret = mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL,
|
|
||||||
secret_bytes, secret_len );
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
mbedtls_platform_zeroize( secret_bytes, secret_len );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
|
|
||||||
#endif /* DRBG modules */
|
|
||||||
#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||||
/*
|
/*
|
||||||
* Maximum number of "basic operations" to be done in a row.
|
* Maximum number of "basic operations" to be done in a row.
|
||||||
@ -309,10 +161,6 @@ struct mbedtls_ecp_restart_mul
|
|||||||
ecp_rsm_comb_core, /* ecp_mul_comb_core() */
|
ecp_rsm_comb_core, /* ecp_mul_comb_core() */
|
||||||
ecp_rsm_final_norm, /* do the final normalization */
|
ecp_rsm_final_norm, /* do the final normalization */
|
||||||
} state;
|
} state;
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_context drbg_ctx;
|
|
||||||
unsigned char drbg_seeded;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -325,10 +173,6 @@ static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
|
|||||||
ctx->T = NULL;
|
ctx->T = NULL;
|
||||||
ctx->T_size = 0;
|
ctx->T_size = 0;
|
||||||
ctx->state = ecp_rsm_init;
|
ctx->state = ecp_rsm_init;
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_init( &ctx->drbg_ctx );
|
|
||||||
ctx->drbg_seeded = 0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -350,10 +194,6 @@ static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
|
|||||||
mbedtls_free( ctx->T );
|
mbedtls_free( ctx->T );
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_free( &ctx->drbg_ctx );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ecp_restart_rsm_init( ctx );
|
ecp_restart_rsm_init( ctx );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2068,9 +1908,7 @@ static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R
|
|||||||
i = d;
|
i = d;
|
||||||
MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
|
MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
|
||||||
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
if( f_rng != 0 )
|
if( f_rng != 0 )
|
||||||
#endif
|
|
||||||
MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
|
MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2204,9 +2042,7 @@ final_norm:
|
|||||||
*
|
*
|
||||||
* Avoid the leak by randomizing coordinates before we normalize them.
|
* Avoid the leak by randomizing coordinates before we normalize them.
|
||||||
*/
|
*/
|
||||||
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
if( f_rng != 0 )
|
if( f_rng != 0 )
|
||||||
#endif
|
|
||||||
MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
|
MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) );
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
|
MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
|
||||||
@ -2286,42 +2122,9 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||||||
size_t d;
|
size_t d;
|
||||||
unsigned char T_size = 0, T_ok = 0;
|
unsigned char T_size = 0, T_ok = 0;
|
||||||
mbedtls_ecp_point *T = NULL;
|
mbedtls_ecp_point *T = NULL;
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_context drbg_ctx;
|
|
||||||
|
|
||||||
ecp_drbg_init( &drbg_ctx );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ECP_RS_ENTER( rsm );
|
ECP_RS_ENTER( rsm );
|
||||||
|
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
if( f_rng == NULL )
|
|
||||||
{
|
|
||||||
/* Adjust pointers */
|
|
||||||
f_rng = &ecp_drbg_random;
|
|
||||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
||||||
if( rs_ctx != NULL && rs_ctx->rsm != NULL )
|
|
||||||
p_rng = &rs_ctx->rsm->drbg_ctx;
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
p_rng = &drbg_ctx;
|
|
||||||
|
|
||||||
/* Initialize internal DRBG if necessary */
|
|
||||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
||||||
if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
|
|
||||||
rs_ctx->rsm->drbg_seeded == 0 )
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
const size_t m_len = ( grp->nbits + 7 ) / 8;
|
|
||||||
MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m, m_len ) );
|
|
||||||
}
|
|
||||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
||||||
if( rs_ctx != NULL && rs_ctx->rsm != NULL )
|
|
||||||
rs_ctx->rsm->drbg_seeded = 1;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
||||||
|
|
||||||
/* Is P the base point ? */
|
/* Is P the base point ? */
|
||||||
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
|
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
|
||||||
p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
|
p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
|
||||||
@ -2393,10 +2196,6 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_free( &drbg_ctx );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* does T belong to the group? */
|
/* does T belong to the group? */
|
||||||
if( T == grp->T )
|
if( T == grp->T )
|
||||||
T = NULL;
|
T = NULL;
|
||||||
@ -2583,23 +2382,8 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||||||
unsigned char b;
|
unsigned char b;
|
||||||
mbedtls_ecp_point RP;
|
mbedtls_ecp_point RP;
|
||||||
mbedtls_mpi PX;
|
mbedtls_mpi PX;
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_context drbg_ctx;
|
|
||||||
|
|
||||||
ecp_drbg_init( &drbg_ctx );
|
|
||||||
#endif
|
|
||||||
mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
|
mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
|
||||||
|
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
if( f_rng == NULL )
|
|
||||||
{
|
|
||||||
const size_t m_len = ( grp->nbits + 7 ) / 8;
|
|
||||||
MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m, m_len ) );
|
|
||||||
f_rng = &ecp_drbg_random;
|
|
||||||
p_rng = &drbg_ctx;
|
|
||||||
}
|
|
||||||
#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
||||||
|
|
||||||
/* Save PX and read from P before writing to R, in case P == R */
|
/* Save PX and read from P before writing to R, in case P == R */
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
|
||||||
@ -2613,9 +2397,7 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||||||
MOD_ADD( RP.X );
|
MOD_ADD( RP.X );
|
||||||
|
|
||||||
/* Randomize coordinates of the starting point */
|
/* Randomize coordinates of the starting point */
|
||||||
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
if( f_rng != NULL )
|
if( f_rng != NULL )
|
||||||
#endif
|
|
||||||
MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
|
MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
|
||||||
|
|
||||||
/* Loop invariant: R = result so far, RP = R + P */
|
/* Loop invariant: R = result so far, RP = R + P */
|
||||||
@ -2648,18 +2430,12 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||||||
*
|
*
|
||||||
* Avoid the leak by randomizing coordinates before we normalize them.
|
* Avoid the leak by randomizing coordinates before we normalize them.
|
||||||
*/
|
*/
|
||||||
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
if( f_rng != NULL )
|
if( f_rng != NULL )
|
||||||
#endif
|
|
||||||
MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
|
MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
|
MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
||||||
ecp_drbg_free( &drbg_ctx );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
|
mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
|
||||||
|
|
||||||
return( ret );
|
return( ret );
|
||||||
|
Loading…
Reference in New Issue
Block a user