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 IDE/C2000/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CFLAGS="-v28 --abi=eabi --float_support=fpu32 --tmu_support=tmu1 -O2 \
# regression surface for this port, plus hash.c, which HashML-DSA
# (wc_MlDsaKey_VerifyCtxHash) calls for the digest size and OID.
SRCS="error wc_port memory logging misc coding hash \
sha sha256 sha512 sha3 wc_mldsa random rng_noisesrc ecc sp_int sp_c32 \
sha sha256 sha512 sha3 wc_mldsa random rng_noisesrc ecc sp_int sp_c32 wolfmath \
aes cmac chacha poly1305 cryptocb \
curve25519 ed25519 fe_operations ge_operations \
curve448 ed448 fe_448 ge_448"
Expand Down
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
69 changes: 36 additions & 33 deletions wolfcrypt/src/sp_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -5454,7 +5454,7 @@ void sp_clear(sp_int* a)
/* Zeroize when a valid pointer passed in. */
if (a != NULL) {
/* ForceZero so that the stores are not removed as dead. */
ForceZero(a->dp, a->size * (word32)SP_WORD_SIZEOF);
ForceZero(a->dp, a->size * (word32)sizeof(sp_int_digit));
#ifdef HAVE_WOLF_BIGINT
/* Zeroize the raw data as well. */
wc_bigint_zero(&a->raw);
Expand Down Expand Up @@ -5494,7 +5494,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 @@ -6254,7 +6254,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 @@ -8618,9 +8618,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 @@ -8686,13 +8686,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 @@ -8769,10 +8770,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 @@ -9578,7 +9581,8 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r)
#endif
for (k = 1; (int)k <= ((int)a->used - 1) + ((int)b->used - 1); k++) {
i = (sp_size_t)(k - (b->used - 1));
i &= (sp_size_t)(((unsigned int)i >> (sizeof(i) * 8 - 1)) - 1U);
i &= (sp_size_t)(((unsigned int)i >>
(CHAR_BIT * sizeof(i) - 1)) - 1U);
j = (int)(k - i);
for (; (i < a->used) && (j >= 0); i++, j--) {
w = (sp_int_word)a->dp[i] * b->dp[j];
Expand Down Expand Up @@ -15328,7 +15332,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 @@ -18767,7 +18771,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 @@ -18776,18 +18780,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 @@ -18902,7 +18906,7 @@ int sp_to_unsigned_bin_len(const sp_int* a, byte* out, int outSz)
sp_int_digit d = a->dp[i];
/* Place each byte of a digit into the buffer. */
for (b = 0; b < SP_WORD_SIZE; b += 8) {
out[j--] = (byte)d;
out[j--] = WC_OCTET(d);
d >>= 8;
/* Stop if the output buffer is filled. */
if (j < 0) {
Expand Down Expand Up @@ -18984,8 +18988,8 @@ int sp_to_unsigned_bin_len_ct(const sp_int* a, byte* out, int outSz)

d = a->dp[idx];
/* Place each byte of a digit into the buffer. */
for (b = 0; (j >= 0) && (b < SP_WORD_SIZEOF); b++) {
out[j--] = (byte)(d & mask);
for (b = 0; (j >= 0) && (b < SP_WORD_OCTETS); b++) {
out[j--] = WC_OCTET(d & mask);
d >>= 8;
}
}
Expand Down Expand Up @@ -19643,7 +19647,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 Expand Up @@ -19703,23 +19707,13 @@ int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap)
printf(".");
fflush(stdout);
#endif /* SHOW_GEN */
/* Generate bytes into digit array. */
err = wc_RNG_GenerateBlock(rng, (byte*)r->dp, (word32)len);
/* Generate len octets of random data into digit array. */
err = mp_rand_mem(rng, r->dp, WC_PACKED_CELLS(len));
Comment thread
kojiws marked this conversation as resolved.
Resolved
Comment thread
kojiws marked this conversation as resolved.
Resolved
if (err != 0) {
err = MP_VAL;
break;
}

/* Set top bits to ensure bit length required is generated.
* Also set second top to help ensure product of two primes is
* going to be twice the number of bits of each.
*/
#ifdef LITTLE_ENDIAN_ORDER
((byte*)r->dp)[len-1] |= 0x80 | 0x40;
#else
((byte*)(r->dp + r->used - 1))[0] |= 0x80 | 0x40;
#endif /* LITTLE_ENDIAN_ORDER */

#ifdef BIG_ENDIAN_ORDER
/* Bytes were put into wrong place when less than full digit. */
if (bits != 0) {
Expand All @@ -19732,6 +19726,15 @@ int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap)
r->dp[r->used - 1] &= ((sp_int_digit)1 << bits) - 1;
}
#endif /* WOLFSSL_SP_MATH_ALL */
/* Set top bits to ensure bit length required is generated.
* Also set second top to help ensure product of two primes is
* going to be twice the number of bits of each.
* Set by bit position in the value, after the top digit is adjusted,
* so that it does not depend on how the octets are laid out in
* memory. Both bits are in the top octet, so in the top digit.
*/
r->dp[r->used - 1] |= (sp_int_digit)3 <<
(((unsigned int)len * 8U - 2U) % SP_WORD_SIZE);
/* Set mandatory low bits
* - bottom bit to make odd.
* - For BBS, second lowest too to make Blum integer (3 mod 4).
Expand Down Expand Up @@ -20157,7 +20160,7 @@ static int _sp_prime_random_trials(const sp_int* a, int trials, int* result,
/* Keep trying random numbers until all trials complete. */
while (trials > 0) {
/* Generate random trial number. */
err = wc_RNG_GenerateBlock(rng, (byte*)b->dp, baseSz);
err = mp_rand_mem(rng, b->dp, WC_PACKED_CELLS(baseSz));
Comment thread
kojiws marked this conversation as resolved.
Resolved
if (err != MP_OKAY) {
break;
}
Expand Down
72 changes: 70 additions & 2 deletions wolfcrypt/src/wolfmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,77 @@ int mp_cond_copy(mp_int* a, int copy, mp_int* b)


#ifndef WC_NO_RNG
#if CHAR_BIT != 8
#ifndef MP_RAND_MEM_CHUNK_OCTETS
/* Number of octets generated at a time. */
#define MP_RAND_MEM_CHUNK_OCTETS 64
#endif

/* Fill memory with random bits, such as the digits of an mp_int, where a char
* is wider than 8 bits (e.g. TI C28x).
*
* wc_RNG_GenerateBlock() puts one octet in each byte, which leaves the upper
* bits of each byte zero. Here every bit of the memory is random: the octets
* are packed WC_OCTETS_PER_BYTE to a byte, lowest octet first.
*
* Where a char is 8 bits, mp_rand_mem() is a macro for wc_RNG_GenerateBlock()
* in wolfmath.h.
*
* Filling digits this way relies on every bit of a digit being used. For SP
* math, sp_int.h checks that sp_int_digit has exactly SP_WORD_SIZE bits.
*
* @param [in] rng Random number generator.
* @param [out] mem Memory to fill.
* @param [in] sz Size of memory in bytes, i.e. in units of sizeof().
*
* @return 0 on success.
* @return BAD_FUNC_ARG when rng or mem is NULL.
* @return Other negative value when generating random data fails.
*/
int mp_rand_mem(WC_RNG* rng, void* mem, word32 sz)
{
int ret = 0;
byte* out = (byte*)mem;

if ((rng == NULL) || (mem == NULL)) {
ret = BAD_FUNC_ARG;
}
else {
byte octets[MP_RAND_MEM_CHUNK_OCTETS];
word32 cnt;
word32 i;
word32 j;

while ((ret == 0) && (sz > 0)) {
/* Number of bytes of memory to fill this time. */
cnt = MP_RAND_MEM_CHUNK_OCTETS / WC_OCTETS_PER_BYTE;
if (cnt > sz) {
cnt = sz;
}
ret = wc_RNG_GenerateBlock(rng, octets, cnt * WC_OCTETS_PER_BYTE);
if (ret == 0) {
for (i = 0; i < cnt; i++) {
byte b = 0;
for (j = 0; j < WC_OCTETS_PER_BYTE; j++) {
b = (byte)(b | ((word32)WC_OCTET(
octets[i * WC_OCTETS_PER_BYTE + j]) << (8 * j)));
}
out[i] = b;
}
out += cnt;
sz -= cnt;
}
}
ForceZero(octets, sizeof(octets));
}

return ret;
}
#endif /* CHAR_BIT != 8 */

int mp_get_rand_digit(WC_RNG* rng, mp_digit* d)
{
return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
return mp_rand_mem(rng, d, sizeof(mp_digit));
}

int mp_rand(mp_int* a, int digits, WC_RNG* rng)
Expand Down Expand Up @@ -197,7 +265,7 @@ int mp_rand(mp_int* a, int digits, WC_RNG* rng)
#endif
/* fill the data with random bytes */
if (ret == MP_OKAY) {
ret = wc_RNG_GenerateBlock(rng, (byte*)a->dp, (word32)cnt);
ret = mp_rand_mem(rng, a->dp, (word32)cnt);
Comment thread
kojiws marked this conversation as resolved.
Resolved
}
if (ret == MP_OKAY) {
#ifdef USE_INTEGER_HEAP_MATH
Expand Down
Loading
Loading