From a4d905a0306c2350e0541b933b0291272c22fb37 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 29 Aug 2017 16:41:08 +0200 Subject: [PATCH 1/3] make sure fast_mp_montgomery_reduce() doesn't BOF This fixes #63 --- bn_fast_mp_montgomery_reduce.c | 4 ++++ bn_mp_montgomery_reduce.c | 1 + 2 files changed, 5 insertions(+) diff --git a/bn_fast_mp_montgomery_reduce.c b/bn_fast_mp_montgomery_reduce.c index 54d9b0a..43a4d37 100644 --- a/bn_fast_mp_montgomery_reduce.c +++ b/bn_fast_mp_montgomery_reduce.c @@ -28,6 +28,10 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) int ix, res, olduse; mp_word W[MP_WARRAY]; + if (x->used > MP_WARRAY) { + return MP_VAL; + } + /* get old used count */ olduse = x->used; diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c index a38173e..a9c7752 100644 --- a/bn_mp_montgomery_reduce.c +++ b/bn_mp_montgomery_reduce.c @@ -29,6 +29,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) */ digs = (n->used * 2) + 1; if ((digs < MP_WARRAY) && + (x->used <= MP_WARRAY) && (n->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { return fast_mp_montgomery_reduce(x, n, rho); From 1d03522625f46214733e8e143a4765c01fc146f9 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 29 Aug 2017 16:53:31 +0200 Subject: [PATCH 2/3] make sure fast_s_mp_mul_digs() doesn't BOF This fixes #60 and #80 --- bn_fast_s_mp_mul_digs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c index 558d151..875798e 100644 --- a/bn_fast_s_mp_mul_digs.c +++ b/bn_fast_s_mp_mul_digs.c @@ -87,7 +87,7 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) { mp_digit *tmpc; tmpc = c->dp; - for (ix = 0; ix < (pa + 1); ix++) { + for (ix = 0; ix < pa; ix++) { /* now extract the previous digit [below the carry] */ *tmpc++ = W[ix]; } From 356084ee3104b05d84f3e2a0bb69267e7b043aaf Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 29 Aug 2017 23:53:02 +0200 Subject: [PATCH 3/3] improve mp_lshd This fixes #61 --- bn_mp_lshd.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c index 888989a..b49b545 100644 --- a/bn_mp_lshd.c +++ b/bn_mp_lshd.c @@ -24,6 +24,10 @@ int mp_lshd(mp_int *a, int b) if (b <= 0) { return MP_OKAY; } + /* no need to shift 0 around */ + if (mp_iszero(a) == MP_YES) { + return MP_OKAY; + } /* grow to fit the new digits */ if (a->alloc < (a->used + b)) {