From 53e79f1053137643a3f95fbe9eb1685391bf895a Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 21 Dec 2020 12:41:32 -0800 Subject: [PATCH] Fix for `mp_radix_size` with radix 2 and mp_int equal to zero. Fix applies to normal and fast math only. ZD11419. --- wolfcrypt/src/integer.c | 5 ++++- wolfcrypt/src/tfm.c | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 7bfba65b4..81987f96d 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -5256,7 +5256,10 @@ int mp_radix_size (mp_int *a, int radix, int *size) /* special case for binary */ if (radix == MP_RADIX_BIN) { - *size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1; + *size = mp_count_bits(a); + if (*size == 0) + *size = 1; + *size += (a->sign == MP_NEG ? 1 : 0) + 1; /* "-" sign + null term */ return MP_OKAY; } diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 6475f669b..aefcdcfe3 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -5550,8 +5550,11 @@ int mp_radix_size (mp_int *a, int radix, int *size) /* special case for binary */ if (radix == 2) { - *size = fp_count_bits (a) + (a->sign == FP_NEG ? 1 : 0) + 1; - return FP_YES; + *size = fp_count_bits(a); + if (*size == 0) + *size = 1; + *size += (a->sign == FP_NEG ? 1 : 0) + 1; /* "-" sign + null term */ + return FP_OKAY; } /* make sure the radix is in range */