chiark / gitweb /
pub/gkcdsa.c: Fix label in test failure output.
[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
46 int rsa_recover(rsa_priv *rp)
47 {
48   /* --- If there is no modulus, calculate it --- */
49
50   if (!rp->n) {
51     if (!rp->p || !rp->q)
52       return (-1);
53     rp->n = mp_mul(MP_NEW, rp->p, rp->q);
54   }
55
56   /* --- If there are no factors, compute them --- */
57
58   else if (!rp->p || !rp->q) {
59
60     /* --- If one is missing, use simple division to recover the other --- */
61
62     if (rp->p || rp->q) {
63       mp *r = MP_NEW;
64       if (rp->p)
65         mp_div(&rp->q, &r, rp->n, rp->p);
66       else
67         mp_div(&rp->p, &r, rp->n, rp->q);
68       if (!MP_EQ(r, MP_ZERO)) {
69         mp_drop(r);
70         return (-1);
71       }
72       mp_drop(r);
73     }
74
75     /* --- Otherwise use the public and private moduli --- */
76
77     else if (!rp->e || !rp->d)
78       return (-1);
79     else {
80       mp *t;
81       size_t s;
82       mp a; mpw aw;
83       mp *m1;
84       mpmont mm;
85       int i;
86       mp *z = MP_NEW;
87
88       /* --- Work out the appropriate exponent --- *
89        *
90        * I need to compute %$s$% and %$t$% such that %$2^s t = e d - 1$%, and
91        * %$t$% is odd.
92        */
93
94       t = mp_mul(MP_NEW, rp->e, rp->d);
95       t = mp_sub(t, t, MP_ONE);
96       t = mp_odd(t, t, &s);
97
98       /* --- Set up for the exponentiation --- */
99
100       mpmont_create(&mm, rp->n);
101       m1 = mp_sub(MP_NEW, rp->n, mm.r);
102
103       /* --- Now for the main loop --- *
104        *
105        * Choose candidate integers and attempt to factor the modulus.
106        */
107
108       mp_build(&a, &aw, &aw + 1);
109       i = 0;
110       for (;;) {
111       again:
112
113         /* --- Choose a random %$a$% and calculate %$z = a^t \bmod n$% --- *
114          *
115          * If %$z \equiv 1$% or %$z \equiv -1 \pmod n$% then this iteration
116          * is a failure.
117          */
118
119         aw = primetab[i++];
120         z = mpmont_mul(&mm, z, &a, mm.r2);
121         z = mpmont_expr(&mm, z, z, t);
122         if (MP_EQ(z, mm.r) || MP_EQ(z, m1))
123           continue;
124
125         /* --- Now square until something interesting happens --- *
126          *
127          * Compute %$z^{2i} \bmod n$%.  Eventually, I'll either get %$-1$% or
128          * %$1$%.  If the former, the number is uninteresting, and I need to
129          * restart.  If the latter, the previous number minus 1 has a common
130          * factor with %$n$%.
131          */
132
133         for (;;) {
134           mp *zz = mp_sqr(MP_NEW, z);
135           zz = mpmont_reduce(&mm, zz, zz);
136           if (MP_EQ(zz, mm.r)) {
137             mp_drop(zz);
138             goto done;
139           } else if (MP_EQ(zz, m1)) {
140             mp_drop(zz);
141             goto again;
142           }
143           mp_drop(z);
144           z = zz;
145         }
146       }
147
148       /* --- Do the factoring --- *
149        *
150        * Here's how it actually works.  I've found an interesting square
151        * root of %$1 \pmod n$%.  Any square root of 1 must be congruent to
152        * %$\pm 1$% modulo both %$p$% and %$q$%.  Both congruent to %$1$% is
153        * boring, as is both congruent to %$-1$%.  Subtracting one from the
154        * result makes it congruent to %$0$% modulo %$p$% or %$q$% (and
155        * nobody cares which), and hence can be extracted by a GCD
156        * operation.
157        */
158
159     done:
160       z = mpmont_reduce(&mm, z, z);
161       z = mp_sub(z, z, MP_ONE);
162       rp->p = MP_NEW;
163       mp_gcd(&rp->p, 0, 0, rp->n, z);
164       rp->q = MP_NEW;
165       mp_div(&rp->q, 0, rp->n, rp->p);
166       mp_drop(z);
167       mp_drop(t);
168       mp_drop(m1);
169       if (MP_CMP(rp->p, <, rp->q)) {
170         z = rp->p;
171         rp->p = rp->q;
172         rp->q = z;
173       }
174       mpmont_destroy(&mm);
175     }
176   }
177
178   /* --- If %$e$% or %$d$% is missing, recalculate it --- */
179
180   if (!rp->e || !rp->d) {
181     mp *phi;
182     mp *g = MP_NEW;
183     mp *p1, *q1;
184
185     /* --- Compute %$\varphi(n)$% --- */
186
187     phi = mp_sub(MP_NEW, rp->n, rp->p);
188     phi = mp_sub(phi, phi, rp->q);
189     phi = mp_add(phi, phi, MP_ONE);
190     p1 = mp_sub(MP_NEW, rp->p, MP_ONE);
191     q1 = mp_sub(MP_NEW, rp->q, MP_ONE);
192     mp_gcd(&g, 0, 0, p1, q1);
193     mp_div(&phi, 0, phi, g);
194     mp_drop(p1);
195     mp_drop(q1);
196
197     /* --- Recover the other exponent --- */
198
199     if (rp->e)
200       mp_gcd(&g, 0, &rp->d, phi, rp->e);
201     else if (rp->d)
202       mp_gcd(&g, 0, &rp->e, phi, rp->d);
203     else {
204       mp_drop(phi);
205       mp_drop(g);
206       return (-1);
207     }
208
209     mp_drop(phi);
210     if (!MP_EQ(g, MP_ONE)) {
211       mp_drop(g);
212       return (-1);
213     }
214     mp_drop(g);
215   }
216
217   /* --- Compute %$q^{-1} \bmod p$% --- */
218
219   if (!rp->q_inv)
220     mp_gcd(0, 0, &rp->q_inv, rp->p, rp->q);
221
222   /* --- Compute %$d \bmod (p - 1)$% and %$d \bmod (q - 1)$% --- */
223
224   if (!rp->dp) {
225     mp *p1 = mp_sub(MP_NEW, rp->p, MP_ONE);
226     mp_div(0, &rp->dp, rp->d, p1);
227     mp_drop(p1);
228   }
229   if (!rp->dq) {
230     mp *q1 = mp_sub(MP_NEW, rp->q, MP_ONE);
231     mp_div(0, &rp->dq, rp->d, q1);
232     mp_drop(q1);
233   }
234
235   /* --- Done --- */
236
237   return (0);
238 }
239
240 /*----- That's all, folks -------------------------------------------------*/