Replace memset() calls with xxx_init() calls

And follow calloc() calls with xxx_init() too
This commit is contained in:
Manuel Pégourié-Gonnard 2017-08-23 16:55:59 +02:00
parent 92cceb29bd
commit 5bd38b1144
3 changed files with 61 additions and 22 deletions

View File

@ -126,9 +126,18 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
*/ */
void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
{ {
memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); mbedtls_ecp_group_init( &ctx->grp );
mbedtls_mpi_init( &ctx->d );
mbedtls_ecp_point_init( &ctx->Q );
mbedtls_ecp_point_init( &ctx->Qp );
mbedtls_mpi_init( &ctx->z );
ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
mbedtls_ecp_point_init( &ctx->Vi );
mbedtls_ecp_point_init( &ctx->Vf );
mbedtls_mpi_init( &ctx->_d );
#if defined(MBEDTLS_ECP_RESTARTABLE) #if defined(MBEDTLS_ECP_RESTARTABLE)
ctx->restart_enabled = 0;
mbedtls_ecp_restart_init( &ctx->rs ); mbedtls_ecp_restart_init( &ctx->rs );
#endif #endif
} }
@ -142,17 +151,19 @@ void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
return; return;
mbedtls_ecp_group_free( &ctx->grp ); mbedtls_ecp_group_free( &ctx->grp );
mbedtls_mpi_free( &ctx->d );
mbedtls_ecp_point_free( &ctx->Q ); mbedtls_ecp_point_free( &ctx->Q );
mbedtls_ecp_point_free( &ctx->Qp ); mbedtls_ecp_point_free( &ctx->Qp );
mbedtls_mpi_free( &ctx->z );
mbedtls_ecp_point_free( &ctx->Vi ); mbedtls_ecp_point_free( &ctx->Vi );
mbedtls_ecp_point_free( &ctx->Vf ); mbedtls_ecp_point_free( &ctx->Vf );
mbedtls_mpi_free( &ctx->d );
mbedtls_mpi_free( &ctx->z );
mbedtls_mpi_free( &ctx->_d ); mbedtls_mpi_free( &ctx->_d );
#if defined(MBEDTLS_ECP_RESTARTABLE) #if defined(MBEDTLS_ECP_RESTARTABLE)
mbedtls_ecp_restart_free( &ctx->rs ); mbedtls_ecp_restart_free( &ctx->rs );
#endif #endif
mbedtls_ecdh_init( ctx );
} }
#if defined(MBEDTLS_ECP_RESTARTABLE) #if defined(MBEDTLS_ECP_RESTARTABLE)

View File

@ -69,7 +69,9 @@ struct mbedtls_ecdsa_restart_ver
*/ */
static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx ) static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx )
{ {
memset( ctx, 0, sizeof( *ctx ) ); mbedtls_mpi_init( &ctx->u1 );
mbedtls_mpi_init( &ctx->u2 );
ctx->state = ecdsa_ver_init;
} }
/* /*
@ -83,7 +85,7 @@ static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx )
mbedtls_mpi_free( &ctx->u1 ); mbedtls_mpi_free( &ctx->u1 );
mbedtls_mpi_free( &ctx->u2 ); mbedtls_mpi_free( &ctx->u2 );
memset( ctx, 0, sizeof( *ctx ) ); ecdsa_restart_ver_init( ctx );
} }
/* /*
@ -107,10 +109,11 @@ struct mbedtls_ecdsa_restart_sig
*/ */
static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx ) static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx )
{ {
memset( ctx, 0, sizeof( *ctx ) ); ctx->sign_tries = 0;
ctx->key_tries = 0;
mbedtls_mpi_init( &ctx->k ); mbedtls_mpi_init( &ctx->k );
mbedtls_mpi_init( &ctx->r ); mbedtls_mpi_init( &ctx->r );
ctx->state = ecdsa_sig_init;
} }
/* /*
@ -124,7 +127,7 @@ static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx )
mbedtls_mpi_free( &ctx->k ); mbedtls_mpi_free( &ctx->k );
mbedtls_mpi_free( &ctx->r ); mbedtls_mpi_free( &ctx->r );
memset( ctx, 0, sizeof( *ctx ) ); ecdsa_restart_sig_init( ctx );
} }
#if defined(MBEDTLS_ECDSA_DETERMINISTIC) #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
@ -145,9 +148,8 @@ struct mbedtls_ecdsa_restart_det
*/ */
static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx ) static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
{ {
memset( ctx, 0, sizeof( *ctx ) );
mbedtls_hmac_drbg_init( &ctx->rng_ctx ); mbedtls_hmac_drbg_init( &ctx->rng_ctx );
ctx->state = ecdsa_det_init;
} }
/* /*
@ -160,7 +162,7 @@ static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx )
mbedtls_hmac_drbg_free( &ctx->rng_ctx ); mbedtls_hmac_drbg_free( &ctx->rng_ctx );
memset( ctx, 0, sizeof( *ctx ) ); ecdsa_restart_det_init( ctx );
} }
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */ #endif /* MBEDTLS_ECDSA_DETERMINISTIC */

View File

@ -138,7 +138,11 @@ struct mbedtls_ecp_restart_mul
*/ */
static void ecp_restart_mul_init( mbedtls_ecp_restart_mul_ctx *ctx ) static void ecp_restart_mul_init( mbedtls_ecp_restart_mul_ctx *ctx )
{ {
memset( ctx, 0, sizeof( mbedtls_ecp_restart_mul_ctx ) ); mbedtls_ecp_point_init( &ctx->R );
ctx->i = 0;
ctx->T = NULL;
ctx->T_size = 0;
ctx->state = ecp_rsm_init;
} }
/* /*
@ -160,7 +164,7 @@ static void ecp_restart_mul_free( mbedtls_ecp_restart_mul_ctx *ctx )
mbedtls_free( ctx->T ); mbedtls_free( ctx->T );
} }
memset( ctx, 0, sizeof( mbedtls_ecp_restart_mul_ctx ) ); ecp_restart_mul_init( ctx );
} }
/* /*
@ -183,7 +187,9 @@ struct mbedtls_ecp_restart_muladd
*/ */
static void ecp_restart_muladd_init( mbedtls_ecp_restart_muladd_ctx *ctx ) static void ecp_restart_muladd_init( mbedtls_ecp_restart_muladd_ctx *ctx )
{ {
memset( ctx, 0, sizeof( *ctx ) ); mbedtls_ecp_point_init( &ctx->mP );
mbedtls_ecp_point_init( &ctx->R );
ctx->state = ecp_rsma_mul1;
} }
/* /*
@ -197,7 +203,7 @@ static void ecp_restart_muladd_free( mbedtls_ecp_restart_muladd_ctx *ctx )
mbedtls_ecp_point_free( &ctx->mP ); mbedtls_ecp_point_free( &ctx->mP );
mbedtls_ecp_point_free( &ctx->R ); mbedtls_ecp_point_free( &ctx->R );
memset( ctx, 0, sizeof( *ctx ) ); ecp_restart_muladd_init( ctx );
} }
/* /*
@ -205,7 +211,10 @@ static void ecp_restart_muladd_free( mbedtls_ecp_restart_muladd_ctx *ctx )
*/ */
void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx ) void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
{ {
memset( ctx, 0, sizeof( *ctx ) ); ctx->ops_done = 0;
ctx->depth = 0;
ctx->rsm = NULL;
ctx->ma = NULL;
} }
/* /*
@ -216,16 +225,13 @@ void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
if( ctx == NULL ) if( ctx == NULL )
return; return;
ctx->ops_done = 0;
ctx->depth = 0;
ecp_restart_mul_free( ctx->rsm ); ecp_restart_mul_free( ctx->rsm );
mbedtls_free( ctx->rsm ); mbedtls_free( ctx->rsm );
ctx->rsm = NULL;
ecp_restart_muladd_free( ctx->ma ); ecp_restart_muladd_free( ctx->ma );
mbedtls_free( ctx->ma ); mbedtls_free( ctx->ma );
ctx->ma = NULL;
mbedtls_ecp_restart_init( ctx );
} }
/* /*
@ -463,7 +469,21 @@ void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
if( grp == NULL ) if( grp == NULL )
return; return;
memset( grp, 0, sizeof( mbedtls_ecp_group ) ); grp->id = 0;
mbedtls_mpi_init( &grp->P );
mbedtls_mpi_init( &grp->A );
mbedtls_mpi_init( &grp->B );
mbedtls_ecp_point_init( &grp->G );
mbedtls_mpi_init( &grp->N );
grp->pbits = 0;
grp->nbits = 0;
grp->h = 0;
grp->modp = NULL;
grp->t_pre = NULL;
grp->t_post = NULL;
grp->t_data = NULL;
grp->T = NULL;
grp->T_size = 0;
} }
/* /*
@ -986,6 +1006,9 @@ static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL ) if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
for( i = 0; i < T_size; i++ )
mbedtls_mpi_init( &c[i] );
mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi ); mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
/* /*
@ -1906,6 +1929,9 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED; ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
goto cleanup; goto cleanup;
} }
for( i = 0; i < T_size; i++ )
mbedtls_ecp_point_init( &T[i] );
} }
/* Compute table (or finish computing it) if not done already */ /* Compute table (or finish computing it) if not done already */