Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/unit-mcdc/test_dh_fault_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 20 additions & 17 deletions wolfcrypt/src/sp_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Comment thread
kojiws marked this conversation as resolved.
Resolved
/* Set back to zero. */
#ifdef HAVE_WOLF_BIGINT
/* Zeroize the raw data as well. */
Expand All @@ -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) */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading