chiark
/
gitweb
/
~mdw
/
catacomb
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
rsa-recover.c: First stage cleanup: hoist variable declarations.
[catacomb]
/
pub
/
rsa-recover.c
diff --git
a/pub/rsa-recover.c
b/pub/rsa-recover.c
index 9f22f230458f2d3416dd2200345c4aefc307b060..c09ca4a473935ead32d106d66d64450c1754b957 100644
(file)
--- a/
pub/rsa-recover.c
+++ b/
pub/rsa-recover.c
@@
-49,6
+49,14
@@
int rsa_recover(rsa_priv *rp)
{
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) {
/* --- 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) {
/* --- 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
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 {
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 --- *
*
/* --- Work out the appropriate exponent --- *
*
@@
-95,14
+95,14
@@
int rsa_recover(rsa_priv *rp)
* %$t$% is odd.
*/
* %$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);
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 --- *
*
/* --- Now for the main loop --- *
*
@@
-135,7
+135,7
@@
int rsa_recover(rsa_priv *rp)
*/
for (;;) {
*/
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);
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;
}
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) {
/* --- 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)$% --- */
/* --- 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);
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_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 --- */
/* --- 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) {
/* --- 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_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);
}
mp_div(0, &rp->dq, rp->d, q1);
mp_drop(q1);
}