From 44903ff8aeaa002ecd2e66d54e2ac7813f04086c Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Wed, 9 Dec 2020 15:04:28 -0800 Subject: [PATCH 1/3] Check shift value --- wolfcrypt/src/tfm.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 56fb5993e..4098ebfd9 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -1027,7 +1027,13 @@ void fp_mod_2d(fp_int *a, int b, fp_int *c) c->dp[x] = 0; } /* clear the digit that is not completely outside/inside the modulus */ - c->dp[b / DIGIT_BIT] &= ~((fp_digit)0) >> (DIGIT_BIT - b); + x = DIGIT_BIT - b; + if (x > 0) { + c->dp[b / DIGIT_BIT] &= ~((fp_digit)0) >> x; + } + else + c->dp[b / DIGIT_BIT] &= ~((fp_digit)0) << -x; + fp_clamp (c); } From 4bd49d2b28bbd6babe85e7cafbbb720dd06cddae Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Wed, 9 Dec 2020 17:05:56 -0800 Subject: [PATCH 2/3] Update with a proper check --- wolfcrypt/src/tfm.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 4098ebfd9..b5d76270a 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -1027,12 +1027,10 @@ void fp_mod_2d(fp_int *a, int b, fp_int *c) c->dp[x] = 0; } /* clear the digit that is not completely outside/inside the modulus */ - x = DIGIT_BIT - b; - if (x > 0) { - c->dp[b / DIGIT_BIT] &= ~((fp_digit)0) >> x; + x = DIGIT_BIT - (b % DIGIT_BIT); + if (x != DIGIT_BIT) { + c->dp[b / DIGIT_BIT] &= ~((fp_digit)0) >> x; } - else - c->dp[b / DIGIT_BIT] &= ~((fp_digit)0) << -x; fp_clamp (c); } From 9042843e425180dfc6baf17c341f0907aaaf9746 Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Thu, 10 Dec 2020 16:13:30 -0800 Subject: [PATCH 3/3] Fix shift and clear digits --- wolfcrypt/src/integer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 95c894353..cd021e7f9 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -684,8 +684,10 @@ int mp_mod_2d (mp_int * a, int b, mp_int * c) c->dp[x] = 0; } /* clear the digit that is not completely outside/inside the modulus */ - c->dp[b / DIGIT_BIT] &= (mp_digit) ((((mp_digit) 1) << - (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); + x = DIGIT_BIT - (b % DIGIT_BIT); + if (x != DIGIT_BIT) { + c->dp[b / DIGIT_BIT] &= ~((mp_digit)0) >> x; + } mp_clamp (c); return MP_OKAY; }