aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Wiklander <jens.wiklander@linaro.org>2019-04-17 12:31:40 +0200
committerJoakim Bech <joakim.bech@linaro.org>2019-04-18 14:51:02 +0700
commit4b5c81cc18db44f317d1b67646c3efb32153133c (patch)
tree44e579f72c70d873e4ef41a74e3d0da7003a60c4
parent27b5e34b47fbd3028586ce81d62ce46c55432667 (diff)
core: ltc: fix preallocation of MPI bignums
Fixes the preallocation to make room for the actual content also in crypto_bignum_allocate() by calling mbedtls_mpi_grow(). Acked-by: Etienne Carriere <etienne.carriere@linaro.org> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
-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;
}