diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 306d7cef9..ba8777897 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -5242,7 +5242,7 @@ int mp_radix_size (mp_int *a, int radix, int *size) } /* digs is the digit count */ - digs = 1; + digs = 0; /* if it's negative add one for the sign */ if (a->sign == MP_NEG) { @@ -5267,6 +5267,13 @@ int mp_radix_size (mp_int *a, int radix, int *size) } mp_clear (&t); +#ifndef WC_DISABLE_RADIX_ZERO_PAD + /* For hexadecimal output, add zero padding when number of digits is odd */ + if ((digs & 1) && (radix == 16)) { + ++digs; + } +#endif + /* return digs + 1, the 1 is for the NULL byte that would be required. */ *size = digs + 1; return MP_OKAY; diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 8b9f3566c..a839bc50c 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -5451,7 +5451,7 @@ int mp_radix_size (mp_int *a, int radix, int *size) } /* digs is the digit count */ - digs = 1; + digs = 0; /* if it's negative add one for the sign */ if (a->sign == FP_NEG) { @@ -5483,6 +5483,13 @@ int mp_radix_size (mp_int *a, int radix, int *size) } fp_zero (t); +#ifndef WC_DISABLE_RADIX_ZERO_PAD + /* For hexadecimal output, add zero padding when number of digits is odd */ + if ((digs & 1) && (radix == 16)) { + ++digs; + } +#endif + /* return digs + 1, the 1 is for the NULL byte that would be required. */ *size = digs + 1; #ifdef WOLFSSL_SMALL_STACK