aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Wiklander <jens.wiklander@linaro.org>2019-05-21 22:52:10 +0200
committerJérôme Forissier <jerome@forissier.org>2019-05-22 12:59:03 +0200
commit41e5aa8f18c4d48083341ff3df9e75f0c77cf703 (patch)
treebbcafebe3b3775d63ea298b08bdc080f24c244a0
parent628e1eb09852e7b260077db810dcc09045481916 (diff)
libmbedtls: mbedtls_mpi_exp_mod(): reduce stack usage
68df6eb0f256 ("libmbedtls: mbedtls_mpi_exp_mod(): reduce stack usage") from branch import/mbedtls-2.16.0 The W variable is 3072 bytes on AArch64 with MBEDTLS_MPI_WINDOW_SIZE set to 6 for maximum performance. Instead of allocating such a large variable on the stack use mempool_alloc(). Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org> Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
-rw-r--r--lib/libmbedtls/mbedtls/library/bignum.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/libmbedtls/mbedtls/library/bignum.c b/lib/libmbedtls/mbedtls/library/bignum.c
index 8279d7d6..4c1840a0 100644
--- a/lib/libmbedtls/mbedtls/library/bignum.c
+++ b/lib/libmbedtls/mbedtls/library/bignum.c
@@ -1788,7 +1788,9 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
size_t i, j, nblimbs;
size_t bufsize, nbits;
mbedtls_mpi_uint ei, mm, state;
- mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
+ mbedtls_mpi RR, T, Apos;
+ mbedtls_mpi *W;
+ const size_t array_size_W = 2 << MBEDTLS_MPI_WINDOW_SIZE;
int neg;
MPI_VALIDATE_RET( X != NULL );
@@ -1802,13 +1804,18 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
+ W = mempool_alloc( mbedtls_mpi_mempool,
+ sizeof( mbedtls_mpi ) * array_size_W );
+ if( W == NULL )
+ return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+
/*
* Init temps and window size
*/
mbedtls_mpi_montg_init( &mm, N );
mbedtls_mpi_init_mempool( &RR ); mbedtls_mpi_init_mempool( &T );
mbedtls_mpi_init_mempool( &Apos );
- for( i = 0; i < ARRAY_SIZE(W); i++ )
+ for( i = 0; i < array_size_W; i++ )
mbedtls_mpi_init_mempool( W + i );
i = mbedtls_mpi_bitlen( E );
@@ -1981,8 +1988,9 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
cleanup:
- for( i = 0; i < ARRAY_SIZE(W); i++ )
+ for( i = 0; i < array_size_W; i++ )
mbedtls_mpi_free( W + i );
+ mempool_free( mbedtls_mpi_mempool , W );
mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );