From aa14607a6f2444f1a4ee8cc5a1a5f542d897e410 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Mon, 21 Mar 2022 10:43:06 +1000 Subject: [PATCH] TFM fp_div_2_ct: rework to avoid overflow Don't set the overflow word. Instead integrate the div by 2 into the function so that the overflow word doesn't need to be stored. --- wolfcrypt/src/tfm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 5d282caa4..05c397d84 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -894,11 +894,13 @@ int fp_div_2_mod_ct(fp_int *a, fp_int *b, fp_int *c) c->dp[i] = (fp_digit)w; w >>= DIGIT_BIT; } - c->dp[i] = (fp_digit)w; + for (i = 0; i < b->used-1; i++) { + c->dp[i] = (c->dp[i] >> 1) | (c->dp[i+1] << (DIGIT_BIT - 1)); + } + c->dp[i] = (c->dp[i] >> 1) | ((fp_digit)w << (DIGIT_BIT - 1)); c->used = i + 1; c->sign = FP_ZPOS; fp_clamp(c); - fp_div_2(c, c); return FP_OKAY; }