Scale ops count for larger curves
From a user's perspective, you want a "basic operation" to take approximately
the same amount of time regardless of the curve size, especially since max_ops
is a global setting: otherwise if you pick a limit suitable for P-384 then
when you do an operation on P-256 it will return way more often than needed.
Said otherwise, a user is actually interested in actual running time, and we
do the API in terms of "basic ops" for practical reasons (no timers) but then
we should make sure it's a good proxy for running time.
diff --git a/library/ecp.c b/library/ecp.c
index b3bddbf..ec2e3cd 100644
--- a/library/ecp.c
+++ b/library/ecp.c
@@ -166,10 +166,18 @@
{
if( grp->rs != NULL )
{
+ /* scale depending on curve size: the chosen reference is 256-bit,
+ * and multiplication is quadratic. Round to the closest integer. */
+ if( grp->pbits >= 512 )
+ ops *= 4;
+ else if( grp->pbits >= 384 )
+ ops *= 2;
+
/* avoid infinite loops: always allow first step */
if( grp->rs->ops_done != 0 && grp->rs->ops_done + ops > ecp_max_ops )
return( MBEDTLS_ERR_ECP_IN_PROGRESS );
+ /* update running count */
grp->rs->ops_done += ops;
}