3 * $Id: rsa-recover.c,v 1.1 1999/12/22 15:50:45 mdw Exp $
5 * Recover RSA parameters
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: rsa-recover.c,v $
33 * Revision 1.1 1999/12/22 15:50:45 mdw
34 * Initial RSA support.
38 /*----- Header files ------------------------------------------------------*/
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @rsa_recover@ --- *
48 * Arguments: @rsa_param *rp@ = pointer to parameter block
50 * Returns: Zero if all went well, nonzero if the parameters make no
53 * Use: Derives the full set of RSA parameters given a minimal set.
56 int rsa_recover(rsa_param
*rp
)
58 /* --- If there is no modulus, calculate it --- */
63 rp
->n
= mp_mul(MP_NEW
, rp
->p
, rp
->q
);
66 /* --- If there are no factors, compute them --- */
68 else if (!rp
->p
|| !rp
->q
) {
70 /* --- If one is missing, use simple division to recover the other --- */
75 mp_div(&rp
->q
, &r
, rp
->n
, rp
->p
);
77 mp_div(&rp
->p
, &r
, rp
->n
, rp
->q
);
78 if (MP_CMP(r
, !=, MP_ZERO
)) {
85 /* --- Otherwise use the public and private moduli --- */
87 else if (rp
->e
&& rp
->d
) {
97 /* --- Work out the appropriate exponent --- *
99 * I need to compute %$s$% and %$t$% such that %$2^s t = e d - 1$%, and
103 t
= mp_mul(MP_NEW
, rp
->e
, rp
->d
);
104 t
= mp_sub(t
, t
, MP_ONE
);
115 /* --- Set up for the exponentiation --- */
117 mpmont_create(&mm
, rp
->n
);
118 m1
= mp_sub(MP_NEW
, rp
->n
, mm
.r
);
120 /* --- Now for the main loop --- *
122 * Choose candidate integers and attempt to factor the modulus.
125 mp_build(&a
, &aw
, &aw
+ 1);
130 /* --- Choose a random %$a$% and calculate %$z = a^t \bmod n$% --- *
132 * If %$z \equiv 1$% or %$z \equiv -1 \pmod n$% then this iteration
137 z
= mpmont_expr(&mm
, z
, &a
, t
);
138 if (MP_CMP(z
, ==, mm
.r
) || MP_CMP(z
, ==, m1
))
141 /* --- Now square until something interesting happens --- *
143 * Compute %$z^{2i} \bmod n$%. Eventually, I'll either get %$-1$% or
144 * %$1$%. If the former, the number is uninteresting, and I need to
145 * restart. If the latter, the previous number minus 1 has a common
150 mp
*zz
= mp_sqr(MP_NEW
, z
);
151 zz
= mpmont_reduce(&mm
, zz
, zz
);
152 if (MP_CMP(zz
, ==, mm
.r
)) {
155 } else if (MP_CMP(zz
, ==, m1
)) {
164 /* --- Do the factoring --- *
166 * Here's how it actually works. I've found an interesting square
167 * root of %$1 \pmod n$%. Any square root of 1 must be congruent to
168 * %$\pm 1$% modulo both %$p$% and %$q$%. Both congruent to %$1$% is
169 * boring, as is both congruent to %$-1$%. Subtracting one from the
170 * result makes it congruent to %$0$% modulo %$p$% or %$q$% (and
171 * nobody cares which), and hence can be extracted by a GCD
176 z
= mpmont_reduce(&mm
, z
, z
);
177 z
= mp_sub(z
, z
, MP_ONE
);
179 mp_gcd(&rp
->p
, 0, 0, rp
->n
, z
);
181 mp_div(&rp
->q
, 0, rp
->n
, rp
->p
);
189 /* --- If %$e$% or %$d$% is missing, recalculate it --- */
191 if (!rp
->e
|| !rp
->d
) {
195 /* --- Compute %$\varphi(n)$% --- */
197 phi
= mp_sub(MP_NEW
, rp
->n
, rp
->p
);
198 phi
= mp_sub(phi
, phi
, rp
->q
);
199 phi
= mp_add(phi
, phi
, MP_ONE
);
201 /* --- Recover the other exponent --- */
204 mp_gcd(&g
, 0, &rp
->d
, phi
, rp
->e
);
206 mp_gcd(&g
, 0, &rp
->e
, phi
, rp
->d
);
213 if (MP_CMP(g
, !=, MP_ONE
)) {
220 /* --- Compute %$q^{-1} \bmod p$% --- */
223 mp_gcd(0, 0, &rp
->q_inv
, rp
->p
, rp
->q
);
225 /* --- Compute %$d \bmod (p - 1)$% and %$d \bmod (q - 1)$% --- */
228 mp
*p1
= mp_sub(MP_NEW
, rp
->p
, MP_ONE
);
229 mp_div(0, &rp
->dp
, rp
->d
, p1
);
233 mp
*q1
= mp_sub(MP_NEW
, rp
->q
, MP_ONE
);
234 mp_div(0, &rp
->dq
, rp
->d
, q1
);
243 /*----- That's all, folks -------------------------------------------------*/