3 * Recover RSA parameters
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- @rsa_recover@ --- *
38 * Arguments: @rsa_priv *rp@ = pointer to parameter block
40 * Returns: Zero if all went well, nonzero if the parameters make no
43 * Use: Derives the full set of RSA parameters given a minimal set.
45 * On failure, the parameter block might be partially filled in,
46 * but the @rsa_privfree@ function will be able to free it
50 int rsa_recover(rsa_priv *rp)
56 mp *g = MP_NEW, *r = MP_NEW, *t = MP_NEW;
57 mp *m1 = MP_NEW, *z = MP_NEW, *zz = MP_NEW;
58 mp *phi = MP_NEW, *p1 = MP_NEW, *q1 = MP_NEW;
60 /* --- If there is no modulus, calculate it --- */
65 rp->n = mp_mul(MP_NEW, rp->p, rp->q);
68 /* --- If there are no factors, compute them --- */
70 else if (!rp->p || !rp->q) {
72 /* --- If one is missing, use simple division to recover the other --- */
76 mp_div(&rp->q, &r, rp->n, rp->p);
78 mp_div(&rp->p, &r, rp->n, rp->q);
79 if (!MP_EQ(r, MP_ZERO)) {
86 /* --- Otherwise use the public and private moduli --- */
88 else if (!rp->e || !rp->d)
92 /* --- Work out the appropriate exponent --- *
94 * I need to compute %$s$% and %$t$% such that %$2^s t = e d - 1$%, and
98 t = mp_mul(t, rp->e, rp->d);
99 t = mp_sub(t, t, MP_ONE);
100 t = mp_odd(t, t, &s);
102 /* --- Set up for the exponentiation --- */
104 mpmont_create(&mm, rp->n);
105 m1 = mp_sub(m1, rp->n, mm.r);
107 /* --- Now for the main loop --- *
109 * Choose candidate integers and attempt to factor the modulus.
112 mp_build(&a, &aw, &aw + 1);
117 /* --- Choose a random %$a$% and calculate %$z = a^t \bmod n$% --- *
119 * If %$z \equiv 1$% or %$z \equiv -1 \pmod n$% then this iteration
124 z = mpmont_mul(&mm, z, &a, mm.r2);
125 z = mpmont_expr(&mm, z, z, t);
126 if (MP_EQ(z, mm.r) || MP_EQ(z, m1))
129 /* --- Now square until something interesting happens --- *
131 * Compute %$z^{2i} \bmod n$%. Eventually, I'll either get %$-1$% or
132 * %$1$%. If the former, the number is uninteresting, and I need to
133 * restart. If the latter, the previous number minus 1 has a common
139 zz = mpmont_reduce(&mm, zz, zz);
140 if (MP_EQ(zz, mm.r)) {
143 } else if (MP_EQ(zz, m1)) {
153 /* --- Do the factoring --- *
155 * Here's how it actually works. I've found an interesting square
156 * root of %$1 \pmod n$%. Any square root of 1 must be congruent to
157 * %$\pm 1$% modulo both %$p$% and %$q$%. Both congruent to %$1$% is
158 * boring, as is both congruent to %$-1$%. Subtracting one from the
159 * result makes it congruent to %$0$% modulo %$p$% or %$q$% (and
160 * nobody cares which), and hence can be extracted by a GCD
165 z = mpmont_reduce(&mm, z, z);
166 z = mp_sub(z, z, MP_ONE);
168 mp_gcd(&rp->p, 0, 0, rp->n, z);
170 mp_div(&rp->q, 0, rp->n, rp->p);
174 if (MP_CMP(rp->p, <, rp->q)) {
183 /* --- If %$e$% or %$d$% is missing, recalculate it --- */
185 if (!rp->e || !rp->d) {
187 /* --- Compute %$\varphi(n)$% --- */
189 phi = mp_sub(phi, rp->n, rp->p);
190 phi = mp_sub(phi, phi, rp->q);
191 phi = mp_add(phi, phi, MP_ONE);
192 p1 = mp_sub(p1, rp->p, MP_ONE);
193 q1 = mp_sub(q1, rp->q, MP_ONE);
194 mp_gcd(&g, 0, 0, p1, q1);
195 mp_div(&phi, 0, phi, g);
196 mp_drop(p1); p1 = MP_NEW;
197 mp_drop(q1); q1 = MP_NEW;
199 /* --- Recover the other exponent --- */
202 mp_gcd(&g, 0, &rp->d, phi, rp->e);
204 mp_gcd(&g, 0, &rp->e, phi, rp->d);
212 if (!MP_EQ(g, MP_ONE)) {
219 /* --- Compute %$q^{-1} \bmod p$% --- */
222 mp_gcd(0, 0, &rp->q_inv, rp->p, rp->q);
224 /* --- Compute %$d \bmod (p - 1)$% and %$d \bmod (q - 1)$% --- */
227 p1 = mp_sub(p1, rp->p, MP_ONE);
228 mp_div(0, &rp->dp, rp->d, p1);
232 q1 = mp_sub(q1, rp->q, MP_ONE);
233 mp_div(0, &rp->dq, rp->d, q1);
242 /*----- That's all, folks -------------------------------------------------*/