diff --git a/tests/unit-mcdc/test_dh_fault_whitebox.c b/tests/unit-mcdc/test_dh_fault_whitebox.c index 1f5ca11903d..d1c57037276 100644 --- a/tests/unit-mcdc/test_dh_fault_whitebox.c +++ b/tests/unit-mcdc/test_dh_fault_whitebox.c @@ -39,7 +39,7 @@ * (b) size-capacity rows: sp_int is a fixed SP_INT_DIGITS-digit struct * (SP_INT_BITS=4096 here); mp_read_unsigned_bin()/sp_exptmod() reject * inputs that do not fit *by construction* (sp_read_unsigned_bin: inSz - * > a->size*SP_WORD_SIZEOF; sp_exptmod_ex: m->used*2 >= SP_INT_DIGITS). + * > a->size*SP_WORD_OCTETS; sp_exptmod_ex: m->used*2 >= SP_INT_DIGITS). * Passing an oversized buffer (here: 2000 zero bytes, always over the * ~1024-1032 byte capacity regardless of 32/64-bit sp_int digits) or a * modulus bigger than SP_INT_BITS deterministically fails that call diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 24d65221cad..0296eb44470 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -5426,7 +5426,7 @@ void sp_forcezero(sp_int* a) /* Zeroize when a valid pointer passed in. */ if (a != NULL) { /* Ensure all data zeroized - data not zeroed when used decreases. */ - ForceZero(a->dp, a->size * (word32)SP_WORD_SIZEOF); + ForceZero(a->dp, a->size * (word32)sizeof(sp_int_digit)); /* Set back to zero. */ #ifdef HAVE_WOLF_BIGINT /* Zeroize the raw data as well. */ @@ -5453,7 +5453,7 @@ static void _sp_copy(const sp_int* a, sp_int* r) r->dp[0] = 0; } else { - XMEMCPY(r->dp, a->dp, a->used * (word32)SP_WORD_SIZEOF); + XMEMCPY(r->dp, a->dp, a->used * (word32)sizeof(sp_int_digit)); } /* Set number of used words in result. */ r->used = a->used;/* // NOLINT(clang-analyzer-core.uninitialized.Assign) */ @@ -6217,7 +6217,7 @@ int sp_set_bit(sp_int* a, int i) * check below rather than wrapping. */ int wi = (i < 0) ? 0 : (i >> SP_WORD_SHIFT); -#if SP_INT_DIGITS < (65536 / SP_WORD_SIZEOF) +#if SP_INT_DIGITS < (65536 / SP_WORD_OCTETS) /* Check bit index isn't bigger than maximum allowed. */ if (i > SP_INT_DIGITS * SP_WORD_SIZE) { err = MP_VAL; @@ -8584,9 +8584,9 @@ int sp_lshd(sp_int* a, int s) } if (err == MP_OKAY) { /* Move up digits. */ - XMEMMOVE(a->dp + s, a->dp, a->used * (word32)SP_WORD_SIZEOF); + XMEMMOVE(a->dp + s, a->dp, a->used * (word32)sizeof(sp_int_digit)); /* Back fill with zeros. */ - XMEMSET(a->dp, 0, (size_t)s * SP_WORD_SIZEOF); + XMEMSET(a->dp, 0, (size_t)s * sizeof(sp_int_digit)); /* Update used. */ a->used = (sp_size_t)(a->used + s); /* Remove leading zeros. */ @@ -8652,13 +8652,14 @@ static int sp_lshb(sp_int* a, int n) /* Only digits to move and ensure not zero. */ else if (s > 0) { /* Move up digits. */ - XMEMMOVE(a->dp + s, a->dp, a->used * (word32)SP_WORD_SIZEOF); + XMEMMOVE(a->dp + s, a->dp, + a->used * (word32)sizeof(sp_int_digit)); } /* Update used digit count. */ a->used = (sp_size_t)(a->used + s); /* Back fill with zeros. */ - XMEMSET(a->dp, 0, (word32)SP_WORD_SIZEOF * s); + XMEMSET(a->dp, 0, (word32)sizeof(sp_int_digit) * s); } } @@ -8735,10 +8736,12 @@ int sp_rshb(const sp_int* a, int n, sp_int* r) r->used = (sp_size_t)(a->used - i); /* Move digits down. */ if (r == a) { - XMEMMOVE(r->dp, r->dp + i, (word32)SP_WORD_SIZEOF * r->used); + XMEMMOVE(r->dp, r->dp + i, + (word32)sizeof(sp_int_digit) * r->used); } else { - XMEMCPY(r->dp, a->dp + i, (word32)SP_WORD_SIZEOF * r->used); + XMEMCPY(r->dp, a->dp + i, + (word32)sizeof(sp_int_digit) * r->used); } } else { @@ -15007,7 +15010,7 @@ int sp_mod_2d(const sp_int* a, int e, sp_int* r) /* Copy a into r if not same pointer. */ if (a != r) { sp_size_t cnt = (a->used < digits) ? a->used : digits; - XMEMCPY(r->dp, a->dp, cnt * (word32)SP_WORD_SIZEOF); + XMEMCPY(r->dp, a->dp, cnt * (word32)sizeof(sp_int_digit)); r->used = a->used; #ifdef WOLFSSL_SP_INT_NEGATIVE r->sign = a->sign; @@ -18343,7 +18346,7 @@ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz) } /* Check a has enough space for number. */ - if ((err == MP_OKAY) && (inSz > (word32)a->size * SP_WORD_SIZEOF)) { + if ((err == MP_OKAY) && (inSz > (word32)a->size * SP_WORD_OCTETS)) { err = MP_VAL; } @@ -18352,18 +18355,18 @@ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz) int i; int j = 0; - a->used = (sp_size_t)((inSz + SP_WORD_SIZEOF - 1) / SP_WORD_SIZEOF); + a->used = (sp_size_t)((inSz + SP_WORD_OCTETS - 1) / SP_WORD_OCTETS); #if defined(BIG_ENDIAN_ORDER) && !defined(WOLFSSL_SP_INT_DIGIT_ALIGN) /* Data endian matches representation of number. * Directly copy if we don't have alignment issues. */ - for (i = (int)(inSz-1); i > SP_WORD_SIZEOF-1; i -= SP_WORD_SIZEOF) { - a->dp[j++] = *(sp_int_digit*)(in + i - (SP_WORD_SIZEOF - 1)); + for (i = (int)(inSz-1); i > SP_WORD_OCTETS-1; i -= SP_WORD_OCTETS) { + a->dp[j++] = *(sp_int_digit*)(in + i - (SP_WORD_OCTETS - 1)); } #else /* Construct digit from required number of bytes. */ - for (i = (int)(inSz-1); i >= SP_WORD_SIZEOF - 1; i -= SP_WORD_SIZEOF) { + for (i = (int)(inSz-1); i >= SP_WORD_OCTETS - 1; i -= SP_WORD_OCTETS) { a->dp[j] = ((sp_int_digit)in[i - 0] << 0) #if SP_WORD_SIZE >= 16 | ((sp_int_digit)in[i - 1] << 8) @@ -18554,7 +18557,7 @@ int sp_to_unsigned_bin_len_ct(const sp_int* a, byte* out, int outSz) d = a->dp[i]; /* Place each byte of a digit into the buffer. */ - for (b = 0; (j >= 0) && (b < SP_WORD_SIZEOF); b++) { + for (b = 0; (j >= 0) && (b < SP_WORD_OCTETS); b++) { out[j--] = (byte)(d & mask); d >>= 8; } @@ -19203,7 +19206,7 @@ int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap) } /* Get number of digits required to handle required number of bytes. */ - digits = ((unsigned int)len + SP_WORD_SIZEOF - 1) / SP_WORD_SIZEOF; + digits = ((unsigned int)len + SP_WORD_OCTETS - 1) / SP_WORD_OCTETS; /* Ensure result has space. */ if (r->size < digits) { err = MP_VAL; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 11414e8850c..c041d38da72 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -93353,7 +93353,7 @@ static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng) ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer)); if (ret != WC_NO_ERR_TRACE(MP_VAL)) return WC_TEST_RET_ENC_EC(ret); - ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1); + ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_OCTETS + 1); if (ret != WC_NO_ERR_TRACE(MP_VAL)) return WC_TEST_RET_ENC_EC(ret); diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 4149f1c0e62..6637d70a6b9 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -291,8 +291,14 @@ extern "C" { #endif #endif +/* SP_WORD_OCTETS: number of octets (8-bit units) in an SP digit. + * Use for counts of octets in encoded data (e.g. sp_read_unsigned_bin()), + * XALIGNED() and preprocessor conditionals - must be an integer literal. + * Not the size of a digit in memory where CHAR_BIT != 8 (e.g. TI C28x) - use + * sizeof(sp_int_digit) for memory operations (XMEMCPY, XMEMSET, ForceZero). + */ #if SP_WORD_SIZE == 8 - #define SP_WORD_SIZEOF 1 + #define SP_WORD_OCTETS 1 typedef sp_uint8 sp_int_digit; typedef sp_int8 sp_int_sdigit; @@ -301,7 +307,7 @@ extern "C" { #define SP_MASK 0xffU #elif SP_WORD_SIZE == 16 - #define SP_WORD_SIZEOF 2 + #define SP_WORD_OCTETS 2 typedef sp_uint16 sp_int_digit; typedef sp_int16 sp_int_sdigit; @@ -310,7 +316,7 @@ extern "C" { #define SP_MASK 0xffffU #elif SP_WORD_SIZE == 32 - #define SP_WORD_SIZEOF 4 + #define SP_WORD_OCTETS 4 typedef sp_uint32 sp_int_digit; typedef sp_int32 sp_int_sdigit; @@ -319,7 +325,7 @@ extern "C" { #define SP_MASK 0xffffffffU #elif SP_WORD_SIZE == 64 - #define SP_WORD_SIZEOF 8 + #define SP_WORD_OCTETS 8 typedef sp_uint64 sp_int_digit; typedef sp_int64 sp_int_sdigit; @@ -976,7 +982,7 @@ while (0) #define WOLF_BIGINT_DEFINED #endif -#if SP_INT_DIGITS < (65536 / SP_WORD_SIZEOF) +#if SP_INT_DIGITS < (65536 / SP_WORD_OCTETS) /* Type for number of digits. */ typedef word16 sp_size_t; #else @@ -1010,7 +1016,7 @@ typedef struct sp_int { struct WC_BIGINT raw; #endif /** Data of number. */ - XALIGNED(SP_WORD_SIZEOF) sp_int_digit dp[SP_INT_DIGITS]; + XALIGNED(SP_WORD_OCTETS) sp_int_digit dp[SP_INT_DIGITS]; } sp_int; typedef struct sp_int_minimal { @@ -1027,7 +1033,7 @@ typedef struct sp_int_minimal { struct WC_BIGINT raw; #endif /** First digit of number. */ - XALIGNED(SP_WORD_SIZEOF) sp_int_digit dp[1]; + XALIGNED(SP_WORD_OCTETS) sp_int_digit dp[1]; } sp_int_minimal; /* MP_INT_SIZEOF_DIGITS() requires that sizeof(sp_int) is a multiple of