From 4d0478a2876d896b57afbb7c9061aae60df5e428 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Mon, 3 Sep 2018 08:32:55 +1000 Subject: [PATCH] Fix fp_div_2d to return remainder correctly If a == c are then a and c don't equal d: calculate d before c If a != c then a doesn't change in calculating c: calculate d after c --- wolfcrypt/src/tfm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index c92faa031..b1aea63aa 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -762,9 +762,8 @@ void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d) return; } - /* get the remainder */ - if (d != NULL) { - /* NOTE: d must not be the same pointer as a or b */ + /* get the remainder before a is changed in calculating c */ + if (a == c && d != NULL) { fp_mod_2d (a, b, d); } @@ -781,6 +780,12 @@ void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d) if (D != 0) { fp_rshb(c, D); } + + /* get the remainder if a is not changed in calculating c */ + if (a != c && d != NULL) { + fp_mod_2d (a, b, d); + } + fp_clamp (c); }