From 1ffac2f0b6bc584503e8e4aebf572f7ae7e05ade Mon Sep 17 00:00:00 2001 From: Mark Karpeles Date: Mon, 15 Jun 2026 01:13:30 +0900 Subject: [PATCH] Add input check to mp_pack and mp_pack_count When nails == size*8 the (size*8) - nails divisor was zero, causing a division by zero. --- mp_pack.c | 4 ++++ mp_pack_count.c | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mp_pack.c b/mp_pack.c index 447f1fdf9..984a524de 100644 --- a/mp_pack.c +++ b/mp_pack.c @@ -15,6 +15,10 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size mp_int t; + if ((size == 0u) || (nails >= (size * 8u))) { + return MP_VAL; + } + count = mp_pack_count(op, nails, size); if (count > maxcount) { diff --git a/mp_pack_count.c b/mp_pack_count.c index aa682ba6c..48699e16e 100644 --- a/mp_pack_count.c +++ b/mp_pack_count.c @@ -5,7 +5,11 @@ size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) { - size_t bits = (size_t)mp_count_bits(a); + size_t bits; + if ((size == 0u) || (nails >= (size * 8u))) { + return 0u; + } + bits = (size_t)mp_count_bits(a); return ((bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u)); }