From 7e4ec8412498ebd3bc509bff7ff582945f255634 Mon Sep 17 00:00:00 2001 From: Kareem Date: Thu, 12 Jun 2025 15:07:31 -0700 Subject: [PATCH] Add macros for legacy get_digit functions for FIPS/selftest. --- tests/api/test_wolfmath.c | 17 ----------------- wolfcrypt/src/wolfmath.c | 24 ------------------------ wolfssl/wolfcrypt/wolfmath.h | 12 ++++++------ 3 files changed, 6 insertions(+), 47 deletions(-) diff --git a/tests/api/test_wolfmath.c b/tests/api/test_wolfmath.c index a8145b6e7..7d15b4bd4 100644 --- a/tests/api/test_wolfmath.c +++ b/tests/api/test_wolfmath.c @@ -46,13 +46,8 @@ int test_mp_get_digit_count(void) ExpectIntEQ(mp_init(&a), 0); -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) - ExpectIntEQ(get_digit_count(NULL), 0); - ExpectIntEQ(get_digit_count(&a), 0); -#else ExpectIntEQ(mp_get_digit_count(NULL), 0); ExpectIntEQ(mp_get_digit_count(&a), 0); -#endif mp_clear(&a); #endif @@ -72,13 +67,8 @@ int test_mp_get_digit(void) XMEMSET(&a, 0, sizeof(mp_int)); ExpectIntEQ(mp_init(&a), MP_OKAY); -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) - ExpectIntEQ(get_digit(NULL, n), 0); - ExpectIntEQ(get_digit(&a, n), 0); -#else ExpectIntEQ(mp_get_digit(NULL, n), 0); ExpectIntEQ(mp_get_digit(&a, n), 0); -#endif mp_clear(&a); #endif @@ -99,17 +89,10 @@ int test_mp_get_rand_digit(void) ExpectIntEQ(wc_InitRng(&rng), 0); -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) - ExpectIntEQ(get_rand_digit(&rng, &d), 0); - ExpectIntEQ(get_rand_digit(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(get_rand_digit(NULL, &d), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(get_rand_digit(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); -#else ExpectIntEQ(mp_get_rand_digit(&rng, &d), 0); ExpectIntEQ(mp_get_rand_digit(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(mp_get_rand_digit(NULL, &d), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(mp_get_rand_digit(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); -#endif DoExpectIntEQ(wc_FreeRng(&rng), 0); #endif diff --git a/wolfcrypt/src/wolfmath.c b/wolfcrypt/src/wolfmath.c index f39d2d937..9ca1865bd 100644 --- a/wolfcrypt/src/wolfmath.c +++ b/wolfcrypt/src/wolfmath.c @@ -85,11 +85,7 @@ void mp_reverse(unsigned char *s, int len) } } -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) -int get_digit_count(const mp_int* a) -#else int mp_get_digit_count(const mp_int* a) -#endif { if (a == NULL) return 0; @@ -97,11 +93,7 @@ int mp_get_digit_count(const mp_int* a) return (int)a->used; } -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) -mp_digit get_digit(const mp_int* a, int n) -#else mp_digit mp_get_digit(const mp_int* a, int n) -#endif { if (a == NULL) return 0; @@ -146,18 +138,10 @@ int mp_cond_copy(mp_int* a, int copy, mp_int* b) * mp_get_digit() returns 0 when index greater than available digit. */ for (i = 0; i < a->used; i++) { -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) - b->dp[i] ^= (get_digit(a, (int)i) ^ get_digit(b, (int)i)) & mask; -#else b->dp[i] ^= (mp_get_digit(a, (int)i) ^ mp_get_digit(b, (int)i)) & mask; -#endif } for (; i < b->used; i++) { -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) - b->dp[i] ^= (get_digit(a, (int)i) ^ get_digit(b, (int)i)) & mask; -#else b->dp[i] ^= (mp_get_digit(a, (int)i) ^ mp_get_digit(b, (int)i)) & mask; -#endif } b->used ^= (a->used ^ b->used) & (wc_mp_size_t)mask; #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \ @@ -172,11 +156,7 @@ int mp_cond_copy(mp_int* a, int copy, mp_int* b) #ifndef WC_NO_RNG -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) -int get_rand_digit(WC_RNG* rng, mp_digit* d) -#else int mp_get_rand_digit(WC_RNG* rng, mp_digit* d) -#endif { return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit)); } @@ -225,11 +205,7 @@ int mp_rand(mp_int* a, int digits, WC_RNG* rng) #endif /* ensure top digit is not zero */ while ((ret == MP_OKAY) && (a->dp[a->used - 1] == 0)) { -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) - ret = get_rand_digit(rng, &a->dp[a->used - 1]); -#else ret = mp_get_rand_digit(rng, &a->dp[a->used - 1]); -#endif #ifdef USE_INTEGER_HEAP_MATH a->dp[a->used - 1] &= MP_MASK; #endif diff --git a/wolfssl/wolfcrypt/wolfmath.h b/wolfssl/wolfcrypt/wolfmath.h index 49c6c750a..bfdda62ad 100644 --- a/wolfssl/wolfcrypt/wolfmath.h +++ b/wolfssl/wolfcrypt/wolfmath.h @@ -83,17 +83,17 @@ This library provides big integer math functions. #if !defined(NO_BIG_INT) /* common math functions */ -#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) -MP_API int get_digit_count(const mp_int* a); -MP_API mp_digit get_digit(const mp_int* a, int n); -MP_API int get_rand_digit(WC_RNG* rng, mp_digit* d); -#else 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); -#endif WOLFSSL_LOCAL void mp_reverse(unsigned char *s, int len); +#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) +#define get_digit_count mp_get_digit_count +#define get_digit mp_get_digit +#define get_rand_digit mp_get_rand_digit +#endif + WOLFSSL_API int mp_cond_copy(mp_int* a, int copy, mp_int* b); WOLFSSL_API int mp_rand(mp_int* a, int digits, WC_RNG* rng); #endif