Skip to content

Commit 62da1f6

Browse files
wolfmath: zero the fast math mp_int in mp_rand() before filling it
- mp_rand() calls mp_zero() before setting used on fast math. - test_mp_rand checks the digits above used on fast math. Issue: F-18454
1 parent b323439 commit 62da1f6

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎tests/api/test_wolfmath.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ int test_mp_rand(void)
353353
WC_RNG rng;
354354
int digits = 1;
355355
byte wide[5 * sizeof(mp_digit)];
356+
#if defined(USE_FAST_MATH) && \
357+
(!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0))
358+
int i;
359+
#endif
356360

357361
XMEMSET(&a, 0, sizeof(mp_int));
358362
XMEMSET(&rng, 0, sizeof(WC_RNG));
@@ -380,6 +384,13 @@ int test_mp_rand(void)
380384
ExpectIntEQ(mp_rand(&a, 2, &rng), 0);
381385
ExpectIntLE(mp_count_bits(&a), 2 * DIGIT_BIT);
382386
ExpectIntGT(mp_count_bits(&a), DIGIT_BIT);
387+
#if defined(USE_FAST_MATH) && \
388+
(!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0))
389+
/* Fast math needs every digit at and above used to be zero. */
390+
for (i = a.used; i < FP_SIZE; i++) {
391+
ExpectTrue(a.dp[i] == 0);
392+
}
393+
#endif
383394

384395
mp_clear(&a);
385396
DoExpectIntEQ(wc_FreeRng(&rng), 0);

‎wolfcrypt/src/wolfmath.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ int mp_rand(mp_int* a, int digits, WC_RNG* rng)
193193
ret = BAD_FUNC_ARG;
194194
}
195195
if (ret == MP_OKAY) {
196+
#ifdef USE_FAST_MATH
197+
mp_zero(a);
198+
#endif
196199
a->used = (wc_mp_size_t)digits;
197200
}
198201
#endif

0 commit comments

Comments
 (0)