From 9934145179d95709f43df0fb72d171e0a4e4ef0b Mon Sep 17 00:00:00 2001 From: Koji Takeda Date: Wed, 11 Nov 2026 22:28:25 +0900 Subject: [PATCH 1/2] Fix SP integer math on targets where a char is wider than 8 bits --- tests/unit-mcdc/test_dh_fault_whitebox.c | 2 +- wolfcrypt/src/sp_int.c | 44 +-- wolfcrypt/test/test.c | 428 ++++++++++++++++++++++- wolfssl/wolfcrypt/sp_int.h | 33 +- 4 files changed, 477 insertions(+), 30 deletions(-) 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 ebb92209b41..0748b57f72f 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -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); @@ -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) */ @@ -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; @@ -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. */ @@ -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); } } @@ -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 { @@ -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]; @@ -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; @@ -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; } @@ -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) @@ -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) { @@ -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; } } @@ -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; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index b05b7da0311..979f0804038 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -95389,6 +95389,402 @@ static wc_test_ret_t mp_test_set_int(mp_int* a) } #endif +#if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY) +#define MP_TEST_CANARY_AVAILABLE + +/* Regression tests for the length of memory operations on SP digits. + * + * Check, using a canary after the allocated digits, that XMEMCPY(), XMEMMOVE(), + * XMEMSET() and ForceZero() in the tested functions cover exactly the intended + * digits on targets where a char is not 8 bits (e.g. TI C28x, where a char is + * 16 bits). + * Where a char is 8 bits, a length in octets is the same as the correct length, + * so these tests can only detect the bug on targets where a char is not 8 bits. + */ + +/* Number of digits (sp_int_digit) in the SP integer used by the tests. */ +#define MP_TEST_CANARY_DIGITS 4 +/* Value of the first char of the canary, incremented for each following + * char. */ +#define MP_TEST_CANARY_VALUE 0x5a +/* Size of the byte arrays holding the test values, in octets. The largest test + * value has one digit more than the SP integer used by the tests (see + * mp_test_canary_rshb() and mp_test_canary_mod_2d()). */ +#define MP_TEST_CANARY_OCTETS ((MP_TEST_CANARY_DIGITS + 1) * SP_WORD_OCTETS) + +/* SP integer with MP_TEST_CANARY_DIGITS digits followed by a canary: + * + * | used | size | ... | dp[0] ... dp[MP_TEST_CANARY_DIGITS - 1] | canary | + * |<------------------------- sp_int -------------------------->| + * + * The canary (MP_TEST_CANARY_DIGITS digits) is long enough to catch all of an + * overrun: where a char is 16 bits, a length in octets is twice the correct + * length, so it writes at most MP_TEST_CANARY_DIGITS digits past the sp_int. + * align is only for aligning the union as an sp_int. + */ +typedef union mp_test_canary_int { + sp_int_minimal align; + byte mem[MP_INT_SIZEOF(MP_TEST_CANARY_DIGITS) + + MP_TEST_CANARY_DIGITS * sizeof(sp_int_digit)]; +} mp_test_canary_int; + +/* mp_test_canary_int is smaller than an sp_int and is used through an sp_int + * pointer. As in sp_int.c, disable warnings of sp_int being partly outside + * array bounds of the variable. */ +PRAGMA_GCC_DIAG_PUSH +PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"") + +/* Initialize the SP integer in buf and fill the canary after its digits. + * + * Returns the SP integer, or NULL on failure. + */ +static sp_int* mp_test_canary_init(mp_test_canary_int* buf) +{ + sp_int* a = (sp_int*)buf; + byte* end = (byte*)buf + sizeof(*buf); + byte* canary; + size_t i; + + XMEMSET(buf, 0, sizeof(*buf)); + if (mp_init_size(a, MP_TEST_CANARY_DIGITS) != MP_OKAY) + return NULL; + + canary = (byte*)&a->dp[MP_TEST_CANARY_DIGITS]; + if ((canary >= end) || ((size_t)(end - canary) < + MP_TEST_CANARY_DIGITS * sizeof(sp_int_digit))) { + return NULL; + } + for (i = 0; i < (size_t)(end - canary); i++) + canary[i] = (byte)(MP_TEST_CANARY_VALUE + i); + + return a; +} + +/* Check that the canary after the digits of the SP integer in buf is + * unchanged. + * + * Returns 1 when unchanged, 0 otherwise. + */ +static int mp_test_canary_ok(mp_test_canary_int* buf) +{ + sp_int* a = (sp_int*)buf; + const byte* end = (const byte*)buf + sizeof(*buf); + const byte* canary = (const byte*)&a->dp[MP_TEST_CANARY_DIGITS]; + size_t i; + + for (i = 0; i < (size_t)(end - canary); i++) { + if (canary[i] != (byte)(MP_TEST_CANARY_VALUE + i)) + return 0; + } + return 1; +} + +/* Set a to the non-negative value encoded in the first digits * SP_WORD_OCTETS + * octets of in. mp_read_unsigned_bin() keeps the sign, so zero a first. */ +static int mp_test_canary_set(mp_int* a, const byte* in, int digits) +{ + mp_zero(a); + return mp_read_unsigned_bin(a, in, (word32)(digits * SP_WORD_OCTETS)); +} + +/* sp_clear(): ForceZero() of all allocated digits. */ +static wc_test_ret_t mp_test_canary_clear(const byte* pat) +{ + mp_test_canary_int buf; + sp_int* r; + int i; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(r, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + if (r->used != MP_TEST_CANARY_DIGITS) + return WC_TEST_RET_ENC_NC; + + mp_clear(r); + + if (r->used != 0) + return WC_TEST_RET_ENC_NC; + for (i = 0; i < MP_TEST_CANARY_DIGITS; i++) { + if (r->dp[i] != 0) + return WC_TEST_RET_ENC_I(i); + } + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} + +#if !defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC) || \ + !defined(NO_DSA) || defined(WOLFSSL_SP_PRIME_GEN) +/* sp_forcezero(): ForceZero() of all allocated digits. */ +static wc_test_ret_t mp_test_canary_forcezero(const byte* pat) +{ + mp_test_canary_int buf; + sp_int* r; + int i; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(r, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + if (r->used != MP_TEST_CANARY_DIGITS) + return WC_TEST_RET_ENC_NC; + + mp_forcezero(r); + + if (r->used != 0) + return WC_TEST_RET_ENC_NC; + for (i = 0; i < MP_TEST_CANARY_DIGITS; i++) { + if (r->dp[i] != 0) + return WC_TEST_RET_ENC_I(i); + } + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} +#endif + +/* sp_copy(): XMEMCPY() of all allocated digits. */ +static wc_test_ret_t mp_test_canary_copy(mp_int* a, const byte* pat) +{ + mp_test_canary_int buf; + sp_int* r; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(a, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + ret = mp_copy(a, r); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_cmp(a, r) != MP_EQ) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} + +/* Same as mp_test_canary_set() but with the least significant digit set to zero. + * This is the expected result of shifting the first (digits - 1) digits of pat + * left by one digit. + */ +static int mp_test_canary_set_lshd(mp_int* exp, const byte* pat, int digits) +{ + byte in[MP_TEST_CANARY_OCTETS]; + int sz = (digits - 1) * SP_WORD_OCTETS; + + XMEMCPY(in, pat, (size_t)sz); + XMEMSET(in + sz, 0, SP_WORD_OCTETS); + return mp_test_canary_set(exp, in, digits); +} + +#if !defined(NO_DH) || defined(HAVE_ECC) || (!defined(NO_RSA) && \ + defined(WC_RSA_BLINDING) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) +/* sp_lshd(): XMEMMOVE() up to the last allocated digit and XMEMSET(). */ +static wc_test_ret_t mp_test_canary_lshd(mp_int* exp, const byte* pat) +{ + mp_test_canary_int buf; + sp_int* r; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(r, pat, MP_TEST_CANARY_DIGITS - 1); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + ret = mp_test_canary_set_lshd(exp, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + ret = mp_lshd(r, 1); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_cmp(exp, r) != MP_EQ) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} +#endif + +/* sp_mul_2d() by whole digits, which uses sp_lshb(): XMEMMOVE() up to the last + * allocated digit and XMEMSET(). */ +static wc_test_ret_t mp_test_canary_mul_2d(mp_int* a, mp_int* exp, + const byte* pat) +{ + mp_test_canary_int buf; + sp_int* r; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(a, pat, MP_TEST_CANARY_DIGITS - 1); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + ret = mp_test_canary_set_lshd(exp, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + ret = mp_mul_2d(a, SP_WORD_SIZE, r); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_cmp(exp, r) != MP_EQ) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} + +/* sp_rshb() by whole digits: XMEMCPY() of all allocated digits when the result + * is a different SP integer, and XMEMMOVE() when it is the same. + * sp_rshb() is called directly as mp_rshb() only shifts in place. */ +static wc_test_ret_t mp_test_canary_rshb(mp_int* a, mp_int* exp, + const byte* pat) +{ + mp_test_canary_int buf; + sp_int* r; + int ret; + + /* Different SP integer: shifting a value of MP_TEST_CANARY_DIGITS + 1 + * digits right by one digit copies MP_TEST_CANARY_DIGITS digits into r. */ + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(a, pat, MP_TEST_CANARY_DIGITS + 1); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + ret = mp_test_canary_set(exp, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + ret = sp_rshb(a, SP_WORD_SIZE, r); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_cmp(exp, r) != MP_EQ) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + /* Same SP integer: move digits down within r. */ + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + ret = mp_test_canary_set(r, pat, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + ret = mp_test_canary_set(exp, pat, MP_TEST_CANARY_DIGITS - 1); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + ret = sp_rshb(r, SP_WORD_SIZE, r); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_cmp(exp, r) != MP_EQ) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} + +/* sp_mod_2d() with all allocated digits of the result: XMEMCPY() of all + * allocated digits. */ +static wc_test_ret_t mp_test_canary_mod_2d(mp_int* a, mp_int* exp, + const byte* pat) +{ + mp_test_canary_int buf; + byte in[MP_TEST_CANARY_OCTETS]; + sp_int* r; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + /* A value of MP_TEST_CANARY_DIGITS + 1 digits, so that the result keeps + * MP_TEST_CANARY_DIGITS digits and the top digit is dropped. */ + ret = mp_test_canary_set(a, pat, MP_TEST_CANARY_DIGITS + 1); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + /* Expected: the low MP_TEST_CANARY_DIGITS digits with the top bit + * cleared. */ + XMEMCPY(in, pat + SP_WORD_OCTETS, MP_TEST_CANARY_DIGITS * SP_WORD_OCTETS); + in[0] &= 0x7f; + ret = mp_test_canary_set(exp, in, MP_TEST_CANARY_DIGITS); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + ret = mp_mod_2d(a, MP_TEST_CANARY_DIGITS * SP_WORD_SIZE - 1, r); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_cmp(exp, r) != MP_EQ) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} + +/* Run all memory operation length tests. a and exp are work SP integers. */ +static wc_test_ret_t mp_test_canary(mp_int* a, mp_int* exp) +{ + byte pat[MP_TEST_CANARY_OCTETS]; + wc_test_ret_t ret; + int i; + + /* Non-zero octets, so that every value has its full number of digits. */ + for (i = 0; i < (int)sizeof(pat); i++) + pat[i] = (byte)(0xa0 + i); + + if ((ret = mp_test_canary_clear(pat)) != 0) + return ret; +#if !defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC) || \ + !defined(NO_DSA) || defined(WOLFSSL_SP_PRIME_GEN) + if ((ret = mp_test_canary_forcezero(pat)) != 0) + return ret; +#endif + if ((ret = mp_test_canary_copy(a, pat)) != 0) + return ret; +#if !defined(NO_DH) || defined(HAVE_ECC) || (!defined(NO_RSA) && \ + defined(WC_RSA_BLINDING) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) + if ((ret = mp_test_canary_lshd(exp, pat)) != 0) + return ret; +#endif + if ((ret = mp_test_canary_mul_2d(a, exp, pat)) != 0) + return ret; + if ((ret = mp_test_canary_rshb(a, exp, pat)) != 0) + return ret; + if ((ret = mp_test_canary_mod_2d(a, exp, pat)) != 0) + return ret; + + return 0; +} + +PRAGMA_GCC_DIAG_POP +#endif /* WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY */ + #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng) { @@ -95471,7 +95867,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); @@ -97636,13 +98032,37 @@ static wc_test_ret_t mp_test_exch(mp_int* a, mp_int* b, mp_int* r1, return 0; } -/* Testing mp_to_unsigned_bin_len_ct against the variable-time export. */ +/* Testing mp_to_unsigned_bin_len_ct against the variable-time export and + * against the expected octets. */ static wc_test_ret_t mp_test_to_unsigned_bin_len_ct(mp_int* a) { int ret; + int i; + byte in[20]; byte buf[40]; byte buf2[40]; + /* The output must be exactly the octets read in, front padded with + * zeros. Every octet is non-zero, so that an octet that is not + * truncated (e.g. where a char is wider than 8 bits) changes the + * output. */ + for (i = 0; i < (int)sizeof(in); i++) + in[i] = (byte)(0xa0 + i); + mp_zero(a); + ret = mp_read_unsigned_bin(a, in, (word32)sizeof(in)); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + XMEMSET(buf, 0xff, sizeof(buf)); + ret = mp_to_unsigned_bin_len_ct(a, buf, (int)sizeof(in) + 4); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + for (i = 0; i < 4; i++) { + if (buf[i] != 0) + return WC_TEST_RET_ENC_I(i); + } + if (XMEMCMP(buf + 4, in, sizeof(in)) != 0) + return WC_TEST_RET_ENC_NC; + /* mp_to_unsigned_bin_len_ct must match the variable-time export. */ ret = mp_set(a, 1); if (ret == MP_OKAY) @@ -98548,6 +98968,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void) #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) if ((ret = mp_test_set_int(a)) != 0) goto done; +#endif +#ifdef MP_TEST_CANARY_AVAILABLE + if ((ret = mp_test_canary(a, b)) != 0) + goto done; #endif if ((ret = mp_test_cmp(a, r1)) != 0) goto done; diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 4149f1c0e62..c537bd80457 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; @@ -334,6 +340,19 @@ extern "C" { #error Word size not defined #endif +/* SP_WORD_SIZEOF: deprecated, same as SP_WORD_OCTETS. Kept mainly for FIPS + * test code that is maintained outside of the wolfSSL repository and still uses + * it, and also for any user code that uses it. + * Only defined where a char is 8 bits, which means SP_WORD_OCTETS and + * sizeof(sp_int_digit) are the same. Not available when building wolfSSL + * itself - use SP_WORD_OCTETS for octet counts and sizeof(sp_int_digit) for + * memory operations. + */ +#if !defined(BUILDING_WOLFSSL) && (SP_UCHAR_BITS == 8) && \ + !defined(SP_WORD_SIZEOF) + #define SP_WORD_SIZEOF SP_WORD_OCTETS +#endif + /* Define an SP digit. */ #ifndef WOLFSSL_SP_ASM /* SP C code uses n/m bits and therefore needs a signed type. */ @@ -976,7 +995,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 +1029,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 +1046,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 From 49854062af268002cdb5e2363b4c9dbe79b16c61 Mon Sep 17 00:00:00 2001 From: Koji Takeda Date: Mon, 16 Nov 2026 19:10:48 +0900 Subject: [PATCH 2/2] Fill digits with random bits on targets where a char is wider than 8 bits --- IDE/C2000/compile.sh | 2 +- wolfcrypt/src/sp_int.c | 25 ++++----- wolfcrypt/src/wolfmath.c | 72 +++++++++++++++++++++++- wolfcrypt/test/test.c | 105 +++++++++++++++++++++++++++++++++++ wolfssl/wolfcrypt/sp_int.h | 16 +++++- wolfssl/wolfcrypt/wolfmath.h | 11 ++++ 6 files changed, 212 insertions(+), 19 deletions(-) diff --git a/IDE/C2000/compile.sh b/IDE/C2000/compile.sh index 3c5dbd7861d..acebdcfc876 100755 --- a/IDE/C2000/compile.sh +++ b/IDE/C2000/compile.sh @@ -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" diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 0748b57f72f..e4cdad538d8 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -19707,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)); 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) { @@ -19736,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). @@ -20161,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)); if (err != MP_OKAY) { break; } diff --git a/wolfcrypt/src/wolfmath.c b/wolfcrypt/src/wolfmath.c index f073be133e3..2faae29b03b 100644 --- a/wolfcrypt/src/wolfmath.c +++ b/wolfcrypt/src/wolfmath.c @@ -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) @@ -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); } if (ret == MP_OKAY) { #ifdef USE_INTEGER_HEAP_MATH diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 979f0804038..e1d3a34a721 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -95747,6 +95747,37 @@ static wc_test_ret_t mp_test_canary_mod_2d(mp_int* a, mp_int* exp, return 0; } +#if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \ + !defined(NO_DSA)) && !defined(WC_NO_RNG) +/* sp_rand_prime(): the random data fills only the octets requested, and the + * prime has the number of bits requested. Where a char is wider than 8 bits, + * random data generated with wc_RNG_GenerateBlock() directly into the digits + * covers more memory than the digits needed and the top bits are set outside + * of them. */ +static wc_test_ret_t mp_test_canary_rand_prime(WC_RNG* rng) +{ + mp_test_canary_int buf; + sp_int* r; + int len = MP_TEST_CANARY_DIGITS * SP_WORD_OCTETS; + int ret; + + r = mp_test_canary_init(&buf); + if (r == NULL) + return WC_TEST_RET_ENC_NC; + + ret = mp_rand_prime(r, len, rng, NULL); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + + if (mp_count_bits(r) != len * 8) + return WC_TEST_RET_ENC_NC; + if (!mp_test_canary_ok(&buf)) + return WC_TEST_RET_ENC_NC; + + return 0; +} +#endif + /* Run all memory operation length tests. a and exp are work SP integers. */ static wc_test_ret_t mp_test_canary(mp_int* a, mp_int* exp) { @@ -95785,6 +95816,71 @@ static wc_test_ret_t mp_test_canary(mp_int* a, mp_int* exp) PRAGMA_GCC_DIAG_POP #endif /* WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY */ +#if !defined(WC_NO_RNG) && !defined(USE_INTEGER_HEAP_MATH) +/* Number of digits generated by mp_test_rand(). */ +#define MP_TEST_RAND_DIGITS 4 +/* Number of times random data is generated by mp_test_rand(). The chance that + * an octet position never has a bit set is 2^-(8 * MP_TEST_RAND_CNT). */ +#define MP_TEST_RAND_CNT 16 + +/* Check that every octet position of the digits has a bit set. + * + * Returns 1 when every octet position has a bit set, 0 otherwise. + */ +static int mp_test_rand_all_octets(const mp_digit* acc, int digits) +{ + int i; + unsigned int j; + + for (i = 0; i < digits; i++) { + for (j = 0; j < (unsigned int)sizeof(mp_digit) * WC_OCTETS_PER_BYTE; + j++) { + if (((acc[i] >> (8 * j)) & 0xff) == 0) + return 0; + } + } + return 1; +} + +/* mp_rand() and mp_get_rand_digit(): every bit of the digits is random. + * Where a char is wider than 8 bits, random data generated with + * wc_RNG_GenerateBlock() directly into the digits leaves the upper bits of + * each char zero. */ +static wc_test_ret_t mp_test_rand(mp_int* a, WC_RNG* rng) +{ + mp_digit acc[MP_TEST_RAND_DIGITS]; + mp_digit d; + int i; + int j; + int ret; + + XMEMSET(acc, 0, sizeof(acc)); + for (i = 0; i < MP_TEST_RAND_CNT; i++) { + ret = mp_rand(a, MP_TEST_RAND_DIGITS, rng); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + if (a->used != MP_TEST_RAND_DIGITS) + return WC_TEST_RET_ENC_NC; + for (j = 0; j < MP_TEST_RAND_DIGITS; j++) + acc[j] |= a->dp[j]; + } + if (!mp_test_rand_all_octets(acc, MP_TEST_RAND_DIGITS)) + return WC_TEST_RET_ENC_NC; + + acc[0] = 0; + for (i = 0; i < MP_TEST_RAND_CNT; i++) { + ret = mp_get_rand_digit(rng, &d); + if (ret != MP_OKAY) + return WC_TEST_RET_ENC_EC(ret); + acc[0] |= d; + } + if (!mp_test_rand_all_octets(acc, 1)) + return WC_TEST_RET_ENC_NC; + + return 0; +} +#endif /* !WC_NO_RNG && !USE_INTEGER_HEAP_MATH */ + #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng) { @@ -98972,6 +99068,15 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void) #ifdef MP_TEST_CANARY_AVAILABLE if ((ret = mp_test_canary(a, b)) != 0) goto done; +#if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \ + !defined(NO_DSA)) && !defined(WC_NO_RNG) + if ((ret = mp_test_canary_rand_prime(&rng)) != 0) + goto done; +#endif +#endif +#if !defined(WC_NO_RNG) && !defined(USE_INTEGER_HEAP_MATH) + if ((ret = mp_test_rand(a, &rng)) != 0) + goto done; #endif if ((ret = mp_test_cmp(a, r1)) != 0) goto done; diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index c537bd80457..19b3ae3bfaf 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -60,9 +60,9 @@ extern "C" { typedef char sp_int7; #elif UCHAR_MAX == 65535 /* CHAR_BIT == 16 (e.g. TI C28x): the smallest addressable type is 16-bit, - * so there is no native 8-bit type. An "8-bit" SP value is a 16-bit char - * cell holding an octet (0..255); byte I/O masks to an octet (the same - * CHAR_BIT!=8 handling used elsewhere - see WOLFSSL_WIDE_BYTE). */ + * so there is no native 8-bit type. sp_uint8 is a 16-bit char used for + * small values such as the sign; it cannot be a digit, so SP_WORD_SIZE 8 + * is not supported (see below). */ #define SP_UCHAR_BITS 16 typedef unsigned char sp_uint8; @@ -340,6 +340,16 @@ extern "C" { #error Word size not defined #endif +/* sp_int_digit must have exactly SP_WORD_SIZE bits: the code relies on a cast + * to sp_int_digit truncating to SP_WORD_SIZE bits, and on all the bits of a + * digit being used, e.g. when filling digits with random data. Where a char is + * wider than 8 bits (e.g. TI C28x) there is no 8-bit type, so SP_WORD_SIZE 8 + * is not supported. */ +#if (SP_WORD_SIZE == 8) && (SP_UCHAR_BITS != 8) + #error SP_WORD_SIZE 8 is not supported where a char is wider than 8 bits +#endif +wc_static_assert(sizeof(sp_int_digit) * CHAR_BIT == SP_WORD_SIZE); + /* SP_WORD_SIZEOF: deprecated, same as SP_WORD_OCTETS. Kept mainly for FIPS * test code that is maintained outside of the wolfSSL repository and still uses * it, and also for any user code that uses it. diff --git a/wolfssl/wolfcrypt/wolfmath.h b/wolfssl/wolfcrypt/wolfmath.h index 1353f40d17a..353597ae3be 100644 --- a/wolfssl/wolfcrypt/wolfmath.h +++ b/wolfssl/wolfcrypt/wolfmath.h @@ -99,6 +99,17 @@ This library provides big integer math functions. MP_API int mp_get_digit_count(const mp_int* a); MP_API mp_digit mp_get_digit(const mp_int* a, int n); MP_API int mp_get_rand_digit(WC_RNG* rng, mp_digit* d); +/* Fill memory with random bits, e.g. the digits of an mp_int. sz is in bytes, + * i.e. in units of sizeof(). */ +#if CHAR_BIT == 8 + /* A byte is an octet, so the random octets fill the memory directly. A + * macro rather than a function, so that the call is to + * wc_RNG_GenerateBlock() itself, e.g. for tests that interpose it. */ + #define mp_rand_mem(rng, mem, sz) \ + wc_RNG_GenerateBlock((rng), (byte*)(mem), (sz)) +#else +WOLFSSL_LOCAL int mp_rand_mem(WC_RNG* rng, void* mem, word32 sz); +#endif WOLFSSL_LOCAL void mp_reverse(unsigned char *s, int len); #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)