Skip to content

Commit

Permalink
libmbedtls: use mempool_calloc() for temporary memory
Browse files Browse the repository at this point in the history
mbedtls_mpi_exp_mod_optionally_safe() need a large chunk of temporary
memory for the mbedtls_mpi_core_exp_mod() function. The amount of memory
is too much to to reliably allocate from the heap. So use
mempool_calloc() instead of mbedtls_calloc(), similar to using
mbedtls_mpi_init_mempool() instead of mbedtls_mpi_init().

Signed-off-by: Jens Wiklander <[email protected]>
  • Loading branch information
jenswi-linaro committed Dec 12, 2024
1 parent ae0b267 commit 99829d2
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/libmbedtls/mbedtls/library/bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,8 @@ static int mbedtls_mpi_exp_mod_optionally_safe(mbedtls_mpi *X, const mbedtls_mpi
* Allocate working memory for mbedtls_mpi_core_exp_mod()
*/
size_t T_limbs = mbedtls_mpi_core_exp_mod_working_limbs(N->n, E->n);
mbedtls_mpi_uint *T = (mbedtls_mpi_uint *) mbedtls_calloc(T_limbs, sizeof(mbedtls_mpi_uint));
mbedtls_mpi_uint *T = mempool_calloc(mbedtls_mpi_mempool, T_limbs,
sizeof(mbedtls_mpi_uint));
if (T == NULL) {
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
}
Expand Down Expand Up @@ -1830,7 +1831,8 @@ static int mbedtls_mpi_exp_mod_optionally_safe(mbedtls_mpi *X, const mbedtls_mpi

cleanup:

mbedtls_mpi_zeroize_and_free(T, T_limbs);
mbedtls_mpi_zeroize(T, T_limbs);
mempool_free(mbedtls_mpi_mempool, T);

if (prec_RR == NULL || prec_RR->p == NULL) {
mbedtls_mpi_free(&RR);
Expand Down

0 comments on commit 99829d2

Please sign in to comment.