Replace memset() calls with xxx_init() calls
And follow calloc() calls with xxx_init() too
diff --git a/library/ecdh.c b/library/ecdh.c
index cae3b29..b8a7dbf 100644
--- a/library/ecdh.c
+++ b/library/ecdh.c
@@ -126,9 +126,18 @@
*/
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)
+ ctx->restart_enabled = 0;
mbedtls_ecp_restart_init( &ctx->rs );
#endif
}
@@ -142,17 +151,19 @@
return;
mbedtls_ecp_group_free( &ctx->grp );
+ mbedtls_mpi_free( &ctx->d );
mbedtls_ecp_point_free( &ctx->Q );
mbedtls_ecp_point_free( &ctx->Qp );
+ mbedtls_mpi_free( &ctx->z );
mbedtls_ecp_point_free( &ctx->Vi );
mbedtls_ecp_point_free( &ctx->Vf );
- mbedtls_mpi_free( &ctx->d );
- mbedtls_mpi_free( &ctx->z );
mbedtls_mpi_free( &ctx->_d );
#if defined(MBEDTLS_ECP_RESTARTABLE)
mbedtls_ecp_restart_free( &ctx->rs );
#endif
+
+ mbedtls_ecdh_init( ctx );
}
#if defined(MBEDTLS_ECP_RESTARTABLE)
diff --git a/library/ecdsa.c b/library/ecdsa.c
index 19d0004..f3b3cf2 100644
--- a/library/ecdsa.c
+++ b/library/ecdsa.c
@@ -69,7 +69,9 @@
*/
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 @@
mbedtls_mpi_free( &ctx->u1 );
mbedtls_mpi_free( &ctx->u2 );
- memset( ctx, 0, sizeof( *ctx ) );
+ ecdsa_restart_ver_init( ctx );
}
/*
@@ -107,10 +109,11 @@
*/
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->r );
+ ctx->state = ecdsa_sig_init;
}
/*
@@ -124,7 +127,7 @@
mbedtls_mpi_free( &ctx->k );
mbedtls_mpi_free( &ctx->r );
- memset( ctx, 0, sizeof( *ctx ) );
+ ecdsa_restart_sig_init( ctx );
}
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
@@ -145,9 +148,8 @@
*/
static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
{
- memset( ctx, 0, sizeof( *ctx ) );
-
mbedtls_hmac_drbg_init( &ctx->rng_ctx );
+ ctx->state = ecdsa_det_init;
}
/*
@@ -160,7 +162,7 @@
mbedtls_hmac_drbg_free( &ctx->rng_ctx );
- memset( ctx, 0, sizeof( *ctx ) );
+ ecdsa_restart_det_init( ctx );
}
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
diff --git a/library/ecp.c b/library/ecp.c
index 365372a..6675c47 100644
--- a/library/ecp.c
+++ b/library/ecp.c
@@ -138,7 +138,11 @@
*/
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 @@
mbedtls_free( ctx->T );
}
- memset( ctx, 0, sizeof( mbedtls_ecp_restart_mul_ctx ) );
+ ecp_restart_mul_init( ctx );
}
/*
@@ -183,7 +187,9 @@
*/
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 @@
mbedtls_ecp_point_free( &ctx->mP );
mbedtls_ecp_point_free( &ctx->R );
- memset( ctx, 0, sizeof( *ctx ) );
+ ecp_restart_muladd_init( ctx );
}
/*
@@ -205,7 +211,10 @@
*/
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 @@
if( ctx == NULL )
return;
- ctx->ops_done = 0;
- ctx->depth = 0;
-
ecp_restart_mul_free( ctx->rsm );
mbedtls_free( ctx->rsm );
- ctx->rsm = NULL;
ecp_restart_muladd_free( ctx->ma );
mbedtls_free( ctx->ma );
- ctx->ma = NULL;
+
+ mbedtls_ecp_restart_init( ctx );
}
/*
@@ -463,7 +469,21 @@
if( grp == NULL )
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 @@
if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
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 );
/*
@@ -1906,6 +1929,9 @@
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
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 */