From 441f3a7f1f2deeb66efc17f3072892e2c53c2b8c Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Mon, 14 Oct 2019 16:43:45 -0700 Subject: [PATCH 1/3] Add leading zero for odd number of hex digits --- tests/api.c | 2 +- wolfcrypt/src/tfm.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index fa53eef67..7795ff068 100644 --- a/tests/api.c +++ b/tests/api.c @@ -24095,7 +24095,7 @@ static void test_wolfSSL_X509_get_serialNumber(void) X509_free(x509); /* free's a */ AssertNotNull(serialHex = BN_bn2hex(bn)); - AssertStrEQ(serialHex, "1"); + AssertStrEQ(serialHex, "01"); OPENSSL_free(serialHex); AssertIntEQ(BN_get_word(bn), 1); diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index c5e4aac2b..899503426 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -4780,6 +4780,12 @@ int mp_toradix (mp_int *a, char *str, int radix) ++digs; } + /* For hexadecimal output, add zero when number of digits is odd */ + if ((digs & 1) && (radix == 16)) { + *str++ = fp_s_rmap[0]; + ++digs; + } + /* reverse the digits of the string. In this case _s points * to the first digit [excluding the sign] of the number] */ From 1a18e3bba8c74d36985089c4d2460473873cc512 Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Tue, 15 Oct 2019 11:54:58 -0700 Subject: [PATCH 2/3] Add leading zero padding for odd hex ASCII digits --- tests/api.c | 4 ++++ wolfcrypt/src/integer.c | 8 +++++++- wolfcrypt/src/tfm.c | 6 +++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/api.c b/tests/api.c index 7795ff068..c963004b6 100644 --- a/tests/api.c +++ b/tests/api.c @@ -24095,7 +24095,11 @@ static void test_wolfSSL_X509_get_serialNumber(void) X509_free(x509); /* free's a */ AssertNotNull(serialHex = BN_bn2hex(bn)); +#ifdef OPENSSL_EXTRA AssertStrEQ(serialHex, "01"); +#else + AssertStrEQ(serialHex, "1"); +#endif OPENSSL_free(serialHex); AssertIntEQ(BN_get_word(bn), 1); diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index b623b9dfe..893902b09 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -5233,7 +5233,13 @@ int mp_toradix (mp_int *a, char *str, int radix) *str++ = mp_s_rmap[d]; ++digs; } - +#ifdef OPENSSL_EXTRA + /* For hexadecimal output, add zero padding when number of digits is odd */ + if ((digs & 1) && (radix == 16)) { + *str++ = mp_s_rmap[0]; + ++digs; + } +#endif /* reverse the digits of the string. In this case _s points * to the first digit [excluding the sign] of the number] */ diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 899503426..14309c0ef 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -4779,13 +4779,13 @@ int mp_toradix (mp_int *a, char *str, int radix) *str++ = fp_s_rmap[d]; ++digs; } - - /* For hexadecimal output, add zero when number of digits is odd */ +#ifdef OPENSSL_EXTRA + /* For hexadecimal output, add zero padding when number of digits is odd */ if ((digs & 1) && (radix == 16)) { *str++ = fp_s_rmap[0]; ++digs; } - +#endif /* reverse the digits of the string. In this case _s points * to the first digit [excluding the sign] of the number] */ From 1267987c31ac414eeba98cee5cfd63f82bc9976e Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Tue, 15 Oct 2019 12:24:57 -0700 Subject: [PATCH 3/3] Review comment --- tests/api.c | 2 +- wolfcrypt/src/integer.c | 2 +- wolfcrypt/src/tfm.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/api.c b/tests/api.c index c963004b6..3840f64ae 100644 --- a/tests/api.c +++ b/tests/api.c @@ -24095,7 +24095,7 @@ static void test_wolfSSL_X509_get_serialNumber(void) X509_free(x509); /* free's a */ AssertNotNull(serialHex = BN_bn2hex(bn)); -#ifdef OPENSSL_EXTRA +#ifndef WC_DISABLE_RADIX_ZERO_PAD AssertStrEQ(serialHex, "01"); #else AssertStrEQ(serialHex, "1"); diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 893902b09..e94c36e11 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -5233,7 +5233,7 @@ int mp_toradix (mp_int *a, char *str, int radix) *str++ = mp_s_rmap[d]; ++digs; } -#ifdef OPENSSL_EXTRA +#ifndef WC_DISABLE_RADIX_ZERO_PAD /* For hexadecimal output, add zero padding when number of digits is odd */ if ((digs & 1) && (radix == 16)) { *str++ = mp_s_rmap[0]; diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 14309c0ef..daa7d070e 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -4779,7 +4779,7 @@ int mp_toradix (mp_int *a, char *str, int radix) *str++ = fp_s_rmap[d]; ++digs; } -#ifdef OPENSSL_EXTRA +#ifndef WC_DISABLE_RADIX_ZERO_PAD /* For hexadecimal output, add zero padding when number of digits is odd */ if ((digs & 1) && (radix == 16)) { *str++ = fp_s_rmap[0];