From 0d3530b45ded435b7f08e3186de37567f7022dec Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 25 May 2021 16:49:58 -0700 Subject: [PATCH] Cleanup NXP LTC logic. --- wolfcrypt/src/port/nxp/ksdk_port.c | 46 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/wolfcrypt/src/port/nxp/ksdk_port.c b/wolfcrypt/src/port/nxp/ksdk_port.c index 424c0e5c4..cc71a6121 100644 --- a/wolfcrypt/src/port/nxp/ksdk_port.c +++ b/wolfcrypt/src/port/nxp/ksdk_port.c @@ -124,17 +124,16 @@ int mp_mul(mp_int *A, mp_int *B, mp_int *C) /* if unsigned mul can fit into LTC PKHA let's use it, otherwise call software mul */ if ((szA <= LTC_MAX_INT_BYTES / 2) && (szB <= LTC_MAX_INT_BYTES / 2)) { int neg = 0; - -#ifndef WOLFSSL_SP_MATH - neg = (A->sign == B->sign) ? MP_ZPOS : MP_NEG; -#endif - - /* unsigned multiply */ uint8_t *ptrA = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); uint8_t *ptrB = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); uint8_t *ptrN = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); uint8_t *ptrC = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); + /* unsigned multiply */ +#ifndef WOLFSSL_SP_MATH + neg = (A->sign == B->sign) ? MP_ZPOS : MP_NEG; +#endif + if (ptrA && ptrB && ptrN && ptrC) { uint16_t sizeA, sizeB, sizeN, sizeC = 0; @@ -328,34 +327,38 @@ int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d) int res = MP_OKAY; status_t status; int szA, szB, szC; + szA = mp_unsigned_bin_size(a); szB = mp_unsigned_bin_size(b); szC = mp_unsigned_bin_size(c); + if ((szA <= LTC_MAX_INT_BYTES) && (szB <= LTC_MAX_INT_BYTES) && (szC <= LTC_MAX_INT_BYTES)) { - mp_int t; - uint8_t *ptrA = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); uint8_t *ptrB = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); uint8_t *ptrC = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); uint8_t *ptrD = (uint8_t*)XMALLOC(LTC_MAX_INT_BYTES, NULL, DYNAMIC_TYPE_BIGINT); +#ifndef WOLFSSL_SP_MATH /* if A or B is negative, subtract abs(A) or abs(B) from modulus to get * positive integer representation of the same number */ + mp_int t; res = mp_init(&t); -#ifndef WOLFSSL_SP_MATH - if (a->sign) { - if (res == MP_OKAY) - res = mp_add(a, c, &t); - if (res == MP_OKAY) - res = mp_copy(&t, a); - } - if (b->sign) { - if (res == MP_OKAY) - res = mp_add(b, c, &t); - if (res == MP_OKAY) - res = mp_copy(&t, b); + if (res == MP_OKAY) { + if (a->sign) { + if (res == MP_OKAY) + res = mp_add(a, c, &t); + if (res == MP_OKAY) + res = mp_copy(&t, a); + } + if (b->sign) { + if (res == MP_OKAY) + res = mp_add(b, c, &t); + if (res == MP_OKAY) + res = mp_copy(&t, b); + } + mp_clear(&t); } #endif @@ -416,9 +419,6 @@ int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d) if (ptrD) { XFREE(ptrD, NULL, DYNAMIC_TYPE_BIGINT); } - #ifndef USE_FAST_MATH - mp_clear(&t); - #endif } else { #if defined(FREESCALE_LTC_TFM_RSA_4096_ENABLE)