From 4688f5fa59870b19ec8be08e118c08ca16fb21f8 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Fri, 11 Sep 2020 08:39:34 -0500 Subject: [PATCH] Handle leading zero --- wolfcrypt/src/integer.c | 9 ++++++++- wolfcrypt/src/tfm.c | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) 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