mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-12-23 08:05:27 +00:00
Implement parameter validation in ECDH module
This commit is contained in:
parent
e77ef2ad33
commit
91796d7471
|
@ -35,9 +35,16 @@
|
|||
#if defined(MBEDTLS_ECDH_C)
|
||||
|
||||
#include "mbedtls/ecdh.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/* Parameter validation macros based on platform_util.h */
|
||||
#define ECDH_VALIDATE_RET( cond ) \
|
||||
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
|
||||
#define ECDH_VALIDATE( cond ) \
|
||||
MBEDTLS_INTERNAL_VALIDATE( cond )
|
||||
|
||||
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
||||
typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
|
||||
#endif
|
||||
|
@ -57,6 +64,10 @@ static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
|
|||
mbedtls_ecp_restart_ctx *rs_ctx )
|
||||
{
|
||||
int ret;
|
||||
ECDH_VALIDATE_RET( grp != NULL );
|
||||
ECDH_VALIDATE_RET( d != NULL );
|
||||
ECDH_VALIDATE_RET( Q != NULL );
|
||||
ECDH_VALIDATE_RET( f_rng != NULL );
|
||||
|
||||
/* If multiplication is in progress, we already generated a privkey */
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
|
@ -78,6 +89,10 @@ int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp
|
|||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng )
|
||||
{
|
||||
ECDH_VALIDATE_RET( grp != NULL );
|
||||
ECDH_VALIDATE_RET( d != NULL );
|
||||
ECDH_VALIDATE_RET( Q != NULL );
|
||||
ECDH_VALIDATE_RET( f_rng != NULL );
|
||||
return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
|
||||
}
|
||||
#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
|
||||
|
@ -123,6 +138,10 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
|
|||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng )
|
||||
{
|
||||
ECDH_VALIDATE_RET( grp != NULL );
|
||||
ECDH_VALIDATE_RET( Q != NULL );
|
||||
ECDH_VALIDATE_RET( d != NULL );
|
||||
ECDH_VALIDATE_RET( z != NULL );
|
||||
return( ecdh_compute_shared_restartable( grp, z, Q, d,
|
||||
f_rng, p_rng, NULL ) );
|
||||
}
|
||||
|
@ -146,6 +165,8 @@ static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
|
|||
*/
|
||||
void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
|
||||
{
|
||||
ECDH_VALIDATE( ctx != NULL );
|
||||
|
||||
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
||||
ecdh_init_internal( ctx );
|
||||
mbedtls_ecp_point_init( &ctx->Vi );
|
||||
|
@ -181,8 +202,7 @@ static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
|
|||
*/
|
||||
int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
|
||||
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
||||
return( ecdh_setup_internal( ctx, grp_id ) );
|
||||
|
@ -218,8 +238,7 @@ static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
|
|||
*/
|
||||
void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return;
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
|
||||
ctx->restart_enabled = 1;
|
||||
}
|
||||
|
@ -318,9 +337,10 @@ int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
|
|||
void *p_rng )
|
||||
{
|
||||
int restart_enabled = 0;
|
||||
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
ECDH_VALIDATE_RET( olen != NULL );
|
||||
ECDH_VALIDATE_RET( buf != NULL );
|
||||
ECDH_VALIDATE_RET( f_rng != NULL );
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
restart_enabled = ctx->restart_enabled;
|
||||
|
@ -366,9 +386,10 @@ int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
|
|||
{
|
||||
int ret;
|
||||
mbedtls_ecp_group_id grp_id;
|
||||
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
ECDH_VALIDATE_RET( buf != NULL );
|
||||
ECDH_VALIDATE_RET( *buf != NULL );
|
||||
ECDH_VALIDATE_RET( end != NULL );
|
||||
|
||||
if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
|
||||
!= 0 )
|
||||
|
@ -420,9 +441,10 @@ int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
|
|||
mbedtls_ecdh_side side )
|
||||
{
|
||||
int ret;
|
||||
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
ECDH_VALIDATE_RET( key != NULL );
|
||||
ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
|
||||
side == MBEDTLS_ECDH_THEIRS );
|
||||
|
||||
if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
|
||||
return( ret );
|
||||
|
@ -488,9 +510,9 @@ int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
|
|||
void *p_rng )
|
||||
{
|
||||
int restart_enabled = 0;
|
||||
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
ECDH_VALIDATE_RET( olen != NULL );
|
||||
ECDH_VALIDATE_RET( buf != NULL );
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
restart_enabled = ctx->restart_enabled;
|
||||
|
@ -535,8 +557,8 @@ static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
|
|||
int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
|
||||
const unsigned char *buf, size_t blen )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
ECDH_VALIDATE_RET( buf != NULL );
|
||||
|
||||
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
||||
return( ecdh_read_public_internal( ctx, buf, blen ) );
|
||||
|
@ -607,9 +629,9 @@ int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
|
|||
void *p_rng )
|
||||
{
|
||||
int restart_enabled = 0;
|
||||
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
||||
ECDH_VALIDATE_RET( ctx != NULL );
|
||||
ECDH_VALIDATE_RET( olen != NULL );
|
||||
ECDH_VALIDATE_RET( buf != NULL );
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
restart_enabled = ctx->restart_enabled;
|
||||
|
|
Loading…
Reference in a new issue