9f22f230458f2d3416dd2200345c4aefc307b060
[catacomb] / pub / rsa-recover.c
1 /* -*-c-*-
2 *
3 * Recover RSA parameters
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "mp.h"
31 #include "mpmont.h"
32 #include "rsa.h"
33
34 /*----- Main code ---------------------------------------------------------*/
35
36 /* --- @rsa_recover@ --- *
37 *
38 * Arguments: @rsa_priv *rp@ = pointer to parameter block
39 *
40 * Returns: Zero if all went well, nonzero if the parameters make no
41 * sense.
42 *
43 * Use: Derives the full set of RSA parameters given a minimal set.
44 *
45 * On failure, the parameter block might be partially filled in,
46 * but the @rsa_privfree@ function will be able to free it
47 * successfully.
48 */
49
50 int rsa_recover(rsa_priv *rp)
51 {
52 /* --- If there is no modulus, calculate it --- */
53
54 if (!rp->n) {
55 if (!rp->p || !rp->q)
56 return (-1);
57 rp->n = mp_mul(MP_NEW, rp->p, rp->q);
58 }
59
60 /* --- If there are no factors, compute them --- */
61
62 else if (!rp->p || !rp->q) {
63
64 /* --- If one is missing, use simple division to recover the other --- */
65
66 if (rp->p || rp->q) {
67 mp *r = MP_NEW;
68 if (rp->p)
69 mp_div(&rp->q, &r, rp->n, rp->p);
70 else
71 mp_div(&rp->p, &r, rp->n, rp->q);
72 if (!MP_EQ(r, MP_ZERO)) {
73 mp_drop(r);
74 return (-1);
75 }
76 mp_drop(r);
77 }
78
79 /* --- Otherwise use the public and private moduli --- */
80
81 else if (!rp->e || !rp->d)
82 return (-1);
83 else {
84 mp *t;
85 size_t s;
86 mp a; mpw aw;
87 mp *m1;
88 mpmont mm;
89 int i;
90 mp *z = MP_NEW;
91
92 /* --- Work out the appropriate exponent --- *
93 *
94 * I need to compute %$s$% and %$t$% such that %$2^s t = e d - 1$%, and
95 * %$t$% is odd.
96 */
97
98 t = mp_mul(MP_NEW, rp->e, rp->d);
99 t = mp_sub(t, t, MP_ONE);
100 t = mp_odd(t, t, &s);
101
102 /* --- Set up for the exponentiation --- */
103
104 mpmont_create(&mm, rp->n);
105 m1 = mp_sub(MP_NEW, rp->n, mm.r);
106
107 /* --- Now for the main loop --- *
108 *
109 * Choose candidate integers and attempt to factor the modulus.
110 */
111
112 mp_build(&a, &aw, &aw + 1);
113 i = 0;
114 for (;;) {
115 again:
116
117 /* --- Choose a random %$a$% and calculate %$z = a^t \bmod n$% --- *
118 *
119 * If %$z \equiv 1$% or %$z \equiv -1 \pmod n$% then this iteration
120 * is a failure.
121 */
122
123 aw = primetab[i++];
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))
127 continue;
128
129 /* --- Now square until something interesting happens --- *
130 *
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
134 * factor with %$n$%.
135 */
136
137 for (;;) {
138 mp *zz = mp_sqr(MP_NEW, z);
139 zz = mpmont_reduce(&mm, zz, zz);
140 if (MP_EQ(zz, mm.r)) {
141 mp_drop(zz);
142 goto done;
143 } else if (MP_EQ(zz, m1)) {
144 mp_drop(zz);
145 goto again;
146 }
147 mp_drop(z);
148 z = zz;
149 }
150 }
151
152 /* --- Do the factoring --- *
153 *
154 * Here's how it actually works. I've found an interesting square
155 * root of %$1 \pmod n$%. Any square root of 1 must be congruent to
156 * %$\pm 1$% modulo both %$p$% and %$q$%. Both congruent to %$1$% is
157 * boring, as is both congruent to %$-1$%. Subtracting one from the
158 * result makes it congruent to %$0$% modulo %$p$% or %$q$% (and
159 * nobody cares which), and hence can be extracted by a GCD
160 * operation.
161 */
162
163 done:
164 z = mpmont_reduce(&mm, z, z);
165 z = mp_sub(z, z, MP_ONE);
166 rp->p = MP_NEW;
167 mp_gcd(&rp->p, 0, 0, rp->n, z);
168 rp->q = MP_NEW;
169 mp_div(&rp->q, 0, rp->n, rp->p);
170 mp_drop(z);
171 mp_drop(t);
172 mp_drop(m1);
173 if (MP_CMP(rp->p, <, rp->q)) {
174 z = rp->p;
175 rp->p = rp->q;
176 rp->q = z;
177 }
178 mpmont_destroy(&mm);
179 }
180 }
181
182 /* --- If %$e$% or %$d$% is missing, recalculate it --- */
183
184 if (!rp->e || !rp->d) {
185 mp *phi;
186 mp *g = MP_NEW;
187 mp *p1, *q1;
188
189 /* --- Compute %$\varphi(n)$% --- */
190
191 phi = mp_sub(MP_NEW, rp->n, rp->p);
192 phi = mp_sub(phi, phi, rp->q);
193 phi = mp_add(phi, phi, MP_ONE);
194 p1 = mp_sub(MP_NEW, rp->p, MP_ONE);
195 q1 = mp_sub(MP_NEW, rp->q, MP_ONE);
196 mp_gcd(&g, 0, 0, p1, q1);
197 mp_div(&phi, 0, phi, g);
198 mp_drop(p1);
199 mp_drop(q1);
200
201 /* --- Recover the other exponent --- */
202
203 if (rp->e)
204 mp_gcd(&g, 0, &rp->d, phi, rp->e);
205 else if (rp->d)
206 mp_gcd(&g, 0, &rp->e, phi, rp->d);
207 else {
208 mp_drop(phi);
209 mp_drop(g);
210 return (-1);
211 }
212
213 mp_drop(phi);
214 if (!MP_EQ(g, MP_ONE)) {
215 mp_drop(g);
216 return (-1);
217 }
218 mp_drop(g);
219 }
220
221 /* --- Compute %$q^{-1} \bmod p$% --- */
222
223 if (!rp->q_inv)
224 mp_gcd(0, 0, &rp->q_inv, rp->p, rp->q);
225
226 /* --- Compute %$d \bmod (p - 1)$% and %$d \bmod (q - 1)$% --- */
227
228 if (!rp->dp) {
229 mp *p1 = mp_sub(MP_NEW, rp->p, MP_ONE);
230 mp_div(0, &rp->dp, rp->d, p1);
231 mp_drop(p1);
232 }
233 if (!rp->dq) {
234 mp *q1 = mp_sub(MP_NEW, rp->q, MP_ONE);
235 mp_div(0, &rp->dq, rp->d, q1);
236 mp_drop(q1);
237 }
238
239 /* --- Done --- */
240
241 return (0);
242 }
243
244 /*----- That's all, folks -------------------------------------------------*/