From 646393bb1e6b4d30a8119eea28ae520af5e2b7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 20 Apr 2017 10:03:45 +0200 Subject: [PATCH] Move ops count to top-level context When a restartable function calls another restartable function, the current ops_count needs to be shared to avoid either doing too many operations or returning IN_PROGRESS uselessly. So it needs to be in the top-level context rather than a specific sub-context. --- include/mbedtls/ecp.h | 3 ++- library/ecp.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index d9e62f079..9193b77b9 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -184,7 +184,8 @@ typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx; */ typedef struct { - mbedtls_ecp_restart_mul_ctx *rsm; /*!< restart context for ecp_mul() */ + unsigned ops_done; /*!< current ops count */ + mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */ } mbedtls_ecp_restart_ctx; #endif /* MBEDTLS_ECP_EARLY_RETURN */ diff --git a/library/ecp.c b/library/ecp.c index fcc3ae077..ecc18152d 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -104,7 +104,6 @@ void mbedtls_ecp_set_max_ops( unsigned max_ops ) * Restart context type for interrupted operations */ struct mbedtls_ecp_restart_mul { - unsigned ops_done; /* number of operations done this time */ mbedtls_ecp_point R; /* current intermediate result */ size_t i; /* current index in various loops, 0 outside */ mbedtls_ecp_point *T; /* table for precomputed points */ @@ -164,6 +163,8 @@ void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx ) if( ctx == NULL ) return; + ctx->ops_done = 0; + ecp_restart_mul_free( ctx->rsm ); mbedtls_free( ctx->rsm ); ctx->rsm = NULL; @@ -183,7 +184,7 @@ static int ecp_check_budget( const mbedtls_ecp_group *grp, mbedtls_ecp_restart_ctx *rs_ctx, unsigned ops ) { - if( rs_ctx != NULL && rs_ctx->rsm != NULL ) + if( rs_ctx != NULL && ecp_max_ops != 0 ) { /* scale depending on curve size: the chosen reference is 256-bit, * and multiplication is quadratic. Round to the closest integer. */ @@ -193,11 +194,11 @@ static int ecp_check_budget( const mbedtls_ecp_group *grp, ops *= 2; /* avoid infinite loops: always allow first step */ - if( rs_ctx->rsm->ops_done != 0 && rs_ctx->rsm->ops_done + ops > ecp_max_ops ) + if( rs_ctx->ops_done != 0 && rs_ctx->ops_done + ops > ecp_max_ops ) return( MBEDTLS_ERR_ECP_IN_PROGRESS ); /* update running count */ - rs_ctx->rsm->ops_done += ops; + rs_ctx->ops_done += ops; } return( 0 ); @@ -1759,7 +1760,7 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, #endif #if defined(MBEDTLS_ECP_EARLY_RETURN) - /* set up restart context if needed */ + /* set up our own sub-context if needed */ if( ecp_max_ops != 0 && rs_ctx != NULL && rs_ctx->rsm == NULL ) { rs_ctx->rsm = mbedtls_calloc( 1, sizeof( mbedtls_ecp_restart_mul_ctx ) ); @@ -1770,8 +1771,8 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, } /* reset ops count for this call */ - if( rs_ctx != NULL && rs_ctx->rsm != NULL ) - rs_ctx->rsm->ops_done = 0; + if( rs_ctx != NULL ) + rs_ctx->ops_done = 0; #endif /* Is P the base point ? */