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