Reintroduce trimming of input in mbedtls_mpi_mul_int()
Removing the trimming has significant memory impact. While it is clearly what
we want to do eventually for constant-time'ness, it should be fixed alongside
a strategy to contain the ramifications on memory usage.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
diff --git a/library/bignum.c b/library/bignum.c
index 493ffa2..6f634b5 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -1500,8 +1500,12 @@
MPI_VALIDATE_RET( X != NULL );
MPI_VALIDATE_RET( A != NULL );
+ size_t n = A->n;
+ while( n > 0 && A->p[n - 1] == 0 )
+ --n;
+
/* The general method below doesn't work if b==0. */
- if( b == 0 )
+ if( b == 0 || n == 0 )
return( mbedtls_mpi_lset( X, 0 ) );
/* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
@@ -1517,9 +1521,9 @@
*
* Note that calculating A*b as 0 + A*b doesn't work as-is because
* A,X can be the same. */
- MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n + 1 ) );
+ MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
- mbedtls_mpi_core_mla( X->p, X->n, A->p, A->n, b - 1 );
+ mbedtls_mpi_core_mla( X->p, X->n, A->p, n, b - 1 );
cleanup:
return( ret );