diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index ba8777897..77dd58cde 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -5244,11 +5244,6 @@ int mp_radix_size (mp_int *a, int radix, int *size) /* digs is the digit count */ digs = 0; - /* if it's negative add one for the sign */ - if (a->sign == MP_NEG) { - ++digs; - } - /* init a copy of the input */ if ((res = mp_init_copy (&t, a)) != MP_OKAY) { return res; @@ -5274,6 +5269,11 @@ int mp_radix_size (mp_int *a, int radix, int *size) } #endif + /* if it's negative add one for the sign */ + if (a->sign == MP_NEG) { + ++digs; + } + /* 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 39ed8a9de..9314375c0 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -5453,11 +5453,6 @@ int mp_radix_size (mp_int *a, int radix, int *size) /* digs is the digit count */ digs = 0; - /* if it's negative add one for the sign */ - if (a->sign == FP_NEG) { - ++digs; - } - #ifdef WOLFSSL_SMALL_STACK t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT); if (t == NULL) @@ -5485,11 +5480,16 @@ int mp_radix_size (mp_int *a, int radix, int *size) #ifndef WC_DISABLE_RADIX_ZERO_PAD /* For hexadecimal output, add zero padding when number of digits is odd */ - if (((a->sign == FP_NEG) ? !(digs & 1) : (digs & 1)) && (radix == 16)) { + if ((digs & 1) && (radix == 16)) { ++digs; } #endif + /* if it's negative add one for the sign */ + if (a->sign == FP_NEG) { + ++digs; + } + /* return digs + 1, the 1 is for the NULL byte that would be required. */ *size = digs + 1; #ifdef WOLFSSL_SMALL_STACK