From f2d45696fc0060f54eac1b1937fa6bcdf7799af0 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 22 Dec 2014 20:32:58 +0000 Subject: [PATCH] rsa-recover.c: First stage cleanup: hoist variable declarations. Remove all of the variable declarations from inner blocks and hoist them to toplevel. Initialize `mp' variables once, and use their initial values, rather than writing `MP_NEW' explicitly in the first assignment. There's no functional change here. Two small (temporary) warts. Firstly, the handling of `z' and `zz' in the factoring loop is rather nasty, repeatedly freeing and recreating `zz'; and secondly `p1' and `q1' are used in two separate places. To prevent conflicts here, reset the relevant variables to `MP_NEW' after freeing them. --- pub/rsa-recover.c | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/pub/rsa-recover.c b/pub/rsa-recover.c index 9f22f230..c09ca4a4 100644 --- a/pub/rsa-recover.c +++ b/pub/rsa-recover.c @@ -49,6 +49,14 @@ int rsa_recover(rsa_priv *rp) { + int i; + size_t s; + mpmont mm; + mp a; mpw aw; + mp *g = MP_NEW, *r = MP_NEW, *t = MP_NEW; + mp *m1 = MP_NEW, *z = MP_NEW, *zz = MP_NEW; + mp *phi = MP_NEW, *p1 = MP_NEW, *q1 = MP_NEW; + /* --- If there is no modulus, calculate it --- */ if (!rp->n) { @@ -64,7 +72,6 @@ int rsa_recover(rsa_priv *rp) /* --- If one is missing, use simple division to recover the other --- */ if (rp->p || rp->q) { - mp *r = MP_NEW; if (rp->p) mp_div(&rp->q, &r, rp->n, rp->p); else @@ -81,13 +88,6 @@ int rsa_recover(rsa_priv *rp) else if (!rp->e || !rp->d) return (-1); else { - mp *t; - size_t s; - mp a; mpw aw; - mp *m1; - mpmont mm; - int i; - mp *z = MP_NEW; /* --- Work out the appropriate exponent --- * * @@ -95,14 +95,14 @@ int rsa_recover(rsa_priv *rp) * %$t$% is odd. */ - t = mp_mul(MP_NEW, rp->e, rp->d); + t = mp_mul(t, rp->e, rp->d); t = mp_sub(t, t, MP_ONE); t = mp_odd(t, t, &s); /* --- Set up for the exponentiation --- */ mpmont_create(&mm, rp->n); - m1 = mp_sub(MP_NEW, rp->n, mm.r); + m1 = mp_sub(m1, rp->n, mm.r); /* --- Now for the main loop --- * * @@ -135,7 +135,7 @@ int rsa_recover(rsa_priv *rp) */ for (;;) { - mp *zz = mp_sqr(MP_NEW, z); + zz = mp_sqr(zz, z); zz = mpmont_reduce(&mm, zz, zz); if (MP_EQ(zz, mm.r)) { mp_drop(zz); @@ -146,6 +146,7 @@ int rsa_recover(rsa_priv *rp) } mp_drop(z); z = zz; + zz = MP_NEW; } } @@ -182,21 +183,18 @@ int rsa_recover(rsa_priv *rp) /* --- If %$e$% or %$d$% is missing, recalculate it --- */ if (!rp->e || !rp->d) { - mp *phi; - mp *g = MP_NEW; - mp *p1, *q1; /* --- Compute %$\varphi(n)$% --- */ - phi = mp_sub(MP_NEW, rp->n, rp->p); + phi = mp_sub(phi, rp->n, rp->p); phi = mp_sub(phi, phi, rp->q); phi = mp_add(phi, phi, MP_ONE); - p1 = mp_sub(MP_NEW, rp->p, MP_ONE); - q1 = mp_sub(MP_NEW, rp->q, MP_ONE); + p1 = mp_sub(p1, rp->p, MP_ONE); + q1 = mp_sub(q1, rp->q, MP_ONE); mp_gcd(&g, 0, 0, p1, q1); mp_div(&phi, 0, phi, g); - mp_drop(p1); - mp_drop(q1); + mp_drop(p1); p1 = MP_NEW; + mp_drop(q1); q1 = MP_NEW; /* --- Recover the other exponent --- */ @@ -226,12 +224,12 @@ int rsa_recover(rsa_priv *rp) /* --- Compute %$d \bmod (p - 1)$% and %$d \bmod (q - 1)$% --- */ if (!rp->dp) { - mp *p1 = mp_sub(MP_NEW, rp->p, MP_ONE); + p1 = mp_sub(p1, rp->p, MP_ONE); mp_div(0, &rp->dp, rp->d, p1); mp_drop(p1); } if (!rp->dq) { - mp *q1 = mp_sub(MP_NEW, rp->q, MP_ONE); + q1 = mp_sub(q1, rp->q, MP_ONE); mp_div(0, &rp->dq, rp->d, q1); mp_drop(q1); } -- 2.11.0