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