From c9efa00871a253203be3a3fa1d618d4fd5567236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 24 Aug 2017 10:25:06 +0200 Subject: [PATCH] ECP: Use explicit state assignments Incrementing the state is error-prone as we can end up doing it too many times (loops) or not enough (skipped branches), or just make programming mistakes (eg. the state was incremented twice at the end, so it ended up with a value not in the enum...) This is the first step of the rework, the next one will rationalize where the state assignments are done. --- library/ecp.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index f2c7448a3..fc4838a0d 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1563,7 +1563,7 @@ static int ecp_precompute_comb( const mbedtls_ecp_group *grp, if( rs_ctx != NULL && rs_ctx->rsm != NULL ) { rs_ctx->rsm->i = 0; - rs_ctx->rsm->state++; + rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl; } #endif @@ -1585,7 +1585,7 @@ norm_dbl: #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->rsm != NULL ) - rs_ctx->rsm->state++; + rs_ctx->rsm->state = ecp_rsm_pre_add; #endif /* @@ -1607,7 +1607,7 @@ add: #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->rsm != NULL ) - rs_ctx->rsm->state++; + rs_ctx->rsm->state = ecp_rsm_pre_norm_add; #endif /* @@ -1628,7 +1628,7 @@ norm_add: #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->rsm != NULL ) - rs_ctx->rsm->state++; + rs_ctx->rsm->state = ecp_rsm_T_done; #endif cleanup: @@ -1730,7 +1730,7 @@ cleanup: { if( ret == 0 ) { - rs_ctx->rsm->state++; + rs_ctx->rsm->state = ecp_rsm_final_norm; rs_ctx->rsm->i = 0; } else if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) @@ -1834,7 +1834,7 @@ static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp, #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->rsm != NULL ) - rs_ctx->rsm->state++; + rs_ctx->rsm->state = ecp_rsm_final_norm; #endif } @@ -2420,14 +2420,14 @@ int mbedtls_ecp_muladd_restartable( MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) ); #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->ma != NULL ) - rs_ctx->ma->state++; + rs_ctx->ma->state = ecp_rsma_mul2; mul2: #endif MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) ); #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->ma != NULL ) - rs_ctx->ma->state++; + rs_ctx->ma->state = ecp_rsma_add; add: #endif @@ -2435,7 +2435,7 @@ add: MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) ); #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->ma != NULL ) - rs_ctx->ma->state++; + rs_ctx->ma->state = ecp_rsma_norm; norm: #endif