From 78a1670334d2192fb29305c3a7354f5d997cfaf9 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Thu, 10 Sep 2020 09:58:26 -0500 Subject: [PATCH 1/4] Fix mp_radix_size off by 1 error --- IDE/Espressif/ESP-IDF/test/test_wolfssl.c | 12 ++++++------ src/ssl.c | 1 - wolfcrypt/src/integer.c | 2 +- wolfcrypt/src/tfm.c | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/IDE/Espressif/ESP-IDF/test/test_wolfssl.c b/IDE/Espressif/ESP-IDF/test/test_wolfssl.c index a4f4c80f2..7edf53e18 100644 --- a/IDE/Espressif/ESP-IDF/test/test_wolfssl.c +++ b/IDE/Espressif/ESP-IDF/test/test_wolfssl.c @@ -335,7 +335,7 @@ int mp_unitest_mul(const char* strZ, const char* strX, const char* strY, int ver } mp_radix_size(&z, 16, &radixZ_size); - bufZ = (char*)XMALLOC(radixZ_size + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + bufZ = (char*)XMALLOC(radixZ_size, NULL, DYNAMIC_TYPE_TMP_BUFFER); if(bufZ != NULL) { mp_toradix(&z, bufZ, 16); bufZ[radixZ_size] ='\0'; @@ -350,7 +350,7 @@ int mp_unitest_mul(const char* strZ, const char* strX, const char* strY, int ver mp_radix_size(&y, 16, &radixY_size); radixX_size = max(radixX_size, radixY_size); - buf = (char*)XMALLOC(radixX_size + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + buf = (char*)XMALLOC(radixX_size, NULL, DYNAMIC_TYPE_TMP_BUFFER); if(buf != NULL) { mp_toradix(&x, buf, 16); buf[radixX_size] ='\0'; @@ -410,7 +410,7 @@ int mp_unitest_mulmod(const char* strZ, const char* strX, const char* strY, } mp_radix_size(&z, 16, &radixZ_size); - bufZ = (char*)XMALLOC(radixZ_size + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + bufZ = (char*)XMALLOC(radixZ_size, NULL, DYNAMIC_TYPE_TMP_BUFFER); if(bufZ != NULL) { mp_toradix(&z, bufZ, 16); bufZ[radixZ_size] ='\0'; @@ -427,7 +427,7 @@ int mp_unitest_mulmod(const char* strZ, const char* strX, const char* strY, mp_radix_size(&m, 16, &radixM_size); radixX_size = max(radixX_size, max(radixY_size, radixM_size)); - buf = (char*)XMALLOC(radixX_size + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + buf = (char*)XMALLOC(radixX_size, NULL, DYNAMIC_TYPE_TMP_BUFFER); if(buf != NULL) { mp_toradix(&x, buf, 16); buf[radixX_size] ='\0'; @@ -491,7 +491,7 @@ int mp_unitest_exptmod(const char* strZ, const char* strX, const char* strY, } mp_radix_size(&z, 16, &radixZ_size); - bufZ = (char*)XMALLOC(radixZ_size + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + bufZ = (char*)XMALLOC(radixZ_size, NULL, DYNAMIC_TYPE_TMP_BUFFER); if(bufZ != NULL) { mp_toradix(&z, bufZ, 16); bufZ[radixZ_size] ='\0'; @@ -508,7 +508,7 @@ int mp_unitest_exptmod(const char* strZ, const char* strX, const char* strY, mp_radix_size(&m, 16, &radixM_size); radixX_size = max(radixX_size, max(radixY_size, radixM_size)); - buf = (char*)XMALLOC(radixX_size + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + buf = (char*)XMALLOC(radixX_size, NULL, DYNAMIC_TYPE_TMP_BUFFER); if(buf != NULL) { mp_toradix(&x, buf, 16); buf[radixX_size] ='\0'; diff --git a/src/ssl.c b/src/ssl.c index f7c2b3aad..dcf092585 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -46562,7 +46562,6 @@ char *wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn) WOLFSSL_MSG("mp_radix_size failure"); return NULL; } - len += 1; /* add one for null terminator */ buf = (char*)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL); if (buf == NULL) { diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 6eaff1a62..306d7cef9 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 = 0; + digs = 1; /* if it's negative add one for the sign */ if (a->sign == MP_NEG) { diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 04159b833..8b9f3566c 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 = 0; + digs = 1; /* if it's negative add one for the sign */ if (a->sign == FP_NEG) { From 4688f5fa59870b19ec8be08e118c08ca16fb21f8 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Fri, 11 Sep 2020 08:39:34 -0500 Subject: [PATCH 2/4] 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 From 6451c4e4717ce2134d5df7a33ec7e1d695159636 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Mon, 14 Sep 2020 10:00:02 -0500 Subject: [PATCH 3/4] Fix for negative values --- wolfcrypt/src/tfm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index a839bc50c..39ed8a9de 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -5485,7 +5485,7 @@ 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 ((digs & 1) && (radix == 16)) { + if (((a->sign == FP_NEG) ? !(digs & 1) : (digs & 1)) && (radix == 16)) { ++digs; } #endif From 5d69c9f1dc63bc4a19124078175e2b5782a60cb7 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Tue, 15 Sep 2020 09:12:45 -0500 Subject: [PATCH 4/4] Move neg check --- wolfcrypt/src/integer.c | 10 +++++----- wolfcrypt/src/tfm.c | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) 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