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));
/* 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
2 changes: 1 addition & 1 deletion wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 13 additions & 7 deletions wolfssl/wolfcrypt/sp_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Loading