Skip to content

Commit 05aa4f5

Browse files
Make mp_get_digit refactor FIPS friendly.
1 parent 2366718 commit 05aa4f5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

tests/api/test_wolfmath.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ int test_mp_get_digit_count(void)
4646

4747
ExpectIntEQ(mp_init(&a), 0);
4848

49+
#ifdef HAVE_FIPS
50+
ExpectIntEQ(get_digit_count(NULL), 0);
51+
ExpectIntEQ(get_digit_count(&a), 0);
52+
#else
4953
ExpectIntEQ(mp_get_digit_count(NULL), 0);
5054
ExpectIntEQ(mp_get_digit_count(&a), 0);
55+
#endif
5156

5257
mp_clear(&a);
5358
#endif
@@ -67,8 +72,13 @@ int test_mp_get_digit(void)
6772
XMEMSET(&a, 0, sizeof(mp_int));
6873

6974
ExpectIntEQ(mp_init(&a), MP_OKAY);
75+
#ifdef HAVE_FIPS
76+
ExpectIntEQ(get_digit(NULL, n), 0);
77+
ExpectIntEQ(get_digit(&a, n), 0);
78+
#else
7079
ExpectIntEQ(mp_get_digit(NULL, n), 0);
7180
ExpectIntEQ(mp_get_digit(&a, n), 0);
81+
#endif
7282

7383
mp_clear(&a);
7484
#endif
@@ -89,10 +99,17 @@ int test_mp_get_rand_digit(void)
8999

90100
ExpectIntEQ(wc_InitRng(&rng), 0);
91101

102+
#ifdef HAVE_FIPS
103+
ExpectIntEQ(get_rand_digit(&rng, &d), 0);
104+
ExpectIntEQ(get_rand_digit(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
105+
ExpectIntEQ(get_rand_digit(NULL, &d), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
106+
ExpectIntEQ(get_rand_digit(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
107+
#else
92108
ExpectIntEQ(mp_get_rand_digit(&rng, &d), 0);
93109
ExpectIntEQ(mp_get_rand_digit(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
94110
ExpectIntEQ(mp_get_rand_digit(NULL, &d), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
95111
ExpectIntEQ(mp_get_rand_digit(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
112+
#endif
96113

97114
DoExpectIntEQ(wc_FreeRng(&rng), 0);
98115
#endif

wolfcrypt/src/wolfmath.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,23 @@ void mp_reverse(unsigned char *s, int len)
8585
}
8686
}
8787

88+
#ifdef HAVE_FIPS
89+
int get_digit_count(const mp_int* a)
90+
#else
8891
int mp_get_digit_count(const mp_int* a)
92+
#endif
8993
{
9094
if (a == NULL)
9195
return 0;
9296

9397
return (int)a->used;
9498
}
9599

100+
#ifdef HAVE_FIPS
101+
mp_digit get_digit(const mp_int* a, int n)
102+
#else
96103
mp_digit mp_get_digit(const mp_int* a, int n)
104+
#endif
97105
{
98106
if (a == NULL)
99107
return 0;
@@ -138,10 +146,18 @@ int mp_cond_copy(mp_int* a, int copy, mp_int* b)
138146
* mp_get_digit() returns 0 when index greater than available digit.
139147
*/
140148
for (i = 0; i < a->used; i++) {
149+
#ifdef HAVE_FIPS
150+
b->dp[i] ^= (get_digit(a, (int)i) ^ get_digit(b, (int)i)) & mask;
151+
#else
141152
b->dp[i] ^= (mp_get_digit(a, (int)i) ^ mp_get_digit(b, (int)i)) & mask;
153+
#endif
142154
}
143155
for (; i < b->used; i++) {
156+
#ifdef HAVE_FIPS
157+
b->dp[i] ^= (get_digit(a, (int)i) ^ get_digit(b, (int)i)) & mask;
158+
#else
144159
b->dp[i] ^= (mp_get_digit(a, (int)i) ^ mp_get_digit(b, (int)i)) & mask;
160+
#endif
145161
}
146162
b->used ^= (a->used ^ b->used) & (wc_mp_size_t)mask;
147163
#if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
@@ -156,7 +172,11 @@ int mp_cond_copy(mp_int* a, int copy, mp_int* b)
156172

157173

158174
#ifndef WC_NO_RNG
175+
#ifdef HAVE_FIPS
176+
int get_rand_digit(WC_RNG* rng, mp_digit* d)
177+
#else
159178
int mp_get_rand_digit(WC_RNG* rng, mp_digit* d)
179+
#endif
160180
{
161181
return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
162182
}

wolfssl/wolfcrypt/wolfmath.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ This library provides big integer math functions.
8383

8484
#if !defined(NO_BIG_INT)
8585
/* common math functions */
86+
#ifdef HAVE_FIPS
87+
MP_API int get_digit_count(const mp_int* a);
88+
MP_API mp_digit get_digit(const mp_int* a, int n);
89+
MP_API int get_rand_digit(WC_RNG* rng, mp_digit* d);
90+
#else
8691
MP_API int mp_get_digit_count(const mp_int* a);
8792
MP_API mp_digit mp_get_digit(const mp_int* a, int n);
8893
MP_API int mp_get_rand_digit(WC_RNG* rng, mp_digit* d);
94+
#endif
8995
WOLFSSL_LOCAL void mp_reverse(unsigned char *s, int len);
9096

9197
WOLFSSL_API int mp_cond_copy(mp_int* a, int copy, mp_int* b);

0 commit comments

Comments
 (0)