From 39716737c19a93c96aa9d0a989cc14cfccdc3e46 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Fri, 2 Dec 2022 09:11:35 +1000 Subject: [PATCH] SP int: fix which APIs are public available wiht WOLFSSL_SP_MATH Make sp_rshd not available when WOLFSSL_SP_MATH in header. sp_rshd is not required by any wolfCrypt code. Fix sp_rshd comment on #endif Make sp_div publicly available in some WOLFSSL_SP_MATH builds. Delare sp_div for some WOLFSSL_SP_MATH builds. Fix test.c to compile with WOLFSL_SP_MATH and HAVE_VALGRIND. --- wolfcrypt/src/sp_int.c | 6 +----- wolfcrypt/test/test.c | 24 ++++++++++++++++++------ wolfssl/wolfcrypt/sp_int.h | 6 +++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index bc8c6c685..2ac80864c 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -7661,8 +7661,7 @@ void sp_rshd(sp_int* a, int c) } } } -#endif /* (WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY) || !NO_DH || - * HAVE_ECC || (!NO_RSA && !WOLFSSL_RSA_VERIFY_ONLY) */ +#endif /* WOLFSSL_SP_MATH_ALL */ #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \ (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \ @@ -7955,9 +7954,6 @@ static int _sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* trial) * @return MP_VAL when a or d is NULL, r and rem are NULL, or d is 0. * @return MP_MEM when dynamic memory allocation fails. */ -#ifndef WOLFSSL_SP_MATH_ALL -static -#endif int sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* rem) { int err = MP_OKAY; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index d888bc2f1..409e4e8c1 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -41311,7 +41311,11 @@ static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng) mp_copy(r1, a); if (mp_lshd(r1, i) != MP_OKAY) return -12681; +#ifndef WOLFSSL_SP_MATH mp_rshd(r1, i); +#else + mp_rshb(r1, i * SP_WORD_SIZE); +#endif if (mp_cmp(a, r1) != MP_EQ) return -12682; } @@ -41655,7 +41659,7 @@ static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng) if (ret != MP_LT) return -12772; -#if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY) +#ifdef WOLFSSL_SP_MATH_ALL mp_rshd(NULL, 1); #endif @@ -42655,13 +42659,18 @@ static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng) ret = mp_lshd(a, k); if (ret != MP_OKAY) return -13024; +#ifndef WOLFSSL_SP_MATH mp_rshd(a, k); +#else + mp_rshb(a, k * SP_WORD_SIZE); +#endif if (mp_cmp(a, b) != MP_EQ) return -13025; } } } +#ifndef WOLFSSL_SP_MATH mp_zero(a); mp_rshd(a, 1); if (!mp_iszero(a)) @@ -42676,12 +42685,15 @@ static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng) mp_rshd(a, 2); if (!mp_iszero(a)) return -13028; +#endif return 0; } #endif -#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) +#if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \ + (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \ + !defined(WOLFSSL_RSA_PUBLIC_ONLY)) static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem, WC_RNG* rng) { @@ -42788,10 +42800,10 @@ static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem, * a digit. So mp_div must detect and handle. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8 */ - mp_set(a, 1); - mp_mul_2d(a, DIGIT_BIT * 3 - 1, a); - mp_set(d, 1); - mp_mul_2d(d, DIGIT_BIT * 2 - 1, d); + mp_zero(a); + mp_set_bit(a, DIGIT_BIT * 3 - 1); + mp_zero(d); + mp_set_bit(d, DIGIT_BIT * 2 - 1); mp_add_d(d, 1, d); ret = mp_div(a, d, r, rem); if (ret != MP_OKAY) diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 4fedada38..8d6c41e46 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -881,10 +881,14 @@ MP_API int sp_addmod_ct (sp_int* a, sp_int* b, sp_int* c, sp_int* d); #endif MP_API int sp_lshd(sp_int* a, int s); +#ifdef WOLFSSL_SP_MATH_ALL MP_API void sp_rshd(sp_int* a, int c); +#endif MP_API int sp_rshb(sp_int* a, int n, sp_int* r); -#ifdef WOLFSSL_SP_MATH_ALL +#if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \ + (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \ + !defined(WOLFSSL_RSA_PUBLIC_ONLY)) MP_API int sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* rem); #endif MP_API int sp_mod(sp_int* a, sp_int* m, sp_int* r);