aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/lib/libtomcrypt/mpi_desc.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/core/lib/libtomcrypt/mpi_desc.c b/core/lib/libtomcrypt/mpi_desc.c
index f1551adb..dba33738 100644
--- a/core/lib/libtomcrypt/mpi_desc.c
+++ b/core/lib/libtomcrypt/mpi_desc.c
@@ -21,6 +21,11 @@
/* Size needed for xtest to pass reliably on both ARM32 and ARM64 */
#define MPI_MEMPOOL_SIZE (42 * 1024)
+/* From mbedtls/library/bignum.c */
+#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
+#define biL (ciL << 3) /* bits in limb */
+#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
+
#if defined(_CFG_CORE_LTC_PAGER)
/* allocate pageable_zi vmem for mp scratch memory pool */
static struct mempool *get_mp_scratch_memory_pool(void)
@@ -714,12 +719,18 @@ void crypto_bignum_copy(struct bignum *to, const struct bignum *from)
mbedtls_mpi_copy((mbedtls_mpi *)to, (const mbedtls_mpi *)from);
}
-struct bignum *crypto_bignum_allocate(size_t size_bits __unused)
+struct bignum *crypto_bignum_allocate(size_t size_bits)
{
mbedtls_mpi *bn = malloc(sizeof(*bn));
- if (bn)
- mbedtls_mpi_init(bn);
+ if (!bn)
+ return NULL;
+
+ mbedtls_mpi_init(bn);
+ if (mbedtls_mpi_grow(bn, BITS_TO_LIMBS(size_bits))) {
+ free(bn);
+ return NULL;
+ }
return (struct bignum *)bn;
}