From 5bd38b1144420d234c4411914433ff31256e9168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 23 Aug 2017 16:55:59 +0200 Subject: [PATCH] Replace memset() calls with xxx_init() calls And follow calloc() calls with xxx_init() too --- library/ecdh.c | 17 ++++++++++++++--- library/ecdsa.c | 18 ++++++++++-------- library/ecp.c | 48 +++++++++++++++++++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/library/ecdh.c b/library/ecdh.c index cae3b290f..b8a7dbf0a 100644 --- a/library/ecdh.c +++ b/library/ecdh.c @@ -126,9 +126,18 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, */ 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 @@ void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) 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 19d0004b5..f3b3cf26d 100644 --- a/library/ecdsa.c +++ b/library/ecdsa.c @@ -69,7 +69,9 @@ struct mbedtls_ecdsa_restart_ver */ 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->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 ) { - 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 @@ static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx ) 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 @@ struct mbedtls_ecdsa_restart_det */ 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 @@ static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx ) 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 365372a44..6675c475c 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -138,7 +138,11 @@ struct mbedtls_ecp_restart_mul */ 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 ); } - 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 ) { - 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->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 ) { - 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 ) 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 @@ void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ) 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 @@ static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp, 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 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, 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 */