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 */