3 * $Id: rsa-recover.c,v 1.7 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @rsa_recover@ --- *
40 * Arguments: @rsa_priv *rp@ = pointer to parameter block
42 * Returns: Zero if all went well, nonzero if the parameters make no
45 * Use: Derives the full set of RSA parameters given a minimal set.
48 int rsa_recover(rsa_priv *rp)
50 /* --- If there is no modulus, calculate it --- */
55 rp->n = mp_mul(MP_NEW, rp->p, rp->q);
58 /* --- If there are no factors, compute them --- */
60 else if (!rp->p || !rp->q) {
62 /* --- If one is missing, use simple division to recover the other --- */
67 mp_div(&rp->q, &r, rp->n, rp->p);
69 mp_div(&rp->p, &r, rp->n, rp->q);
70 if (!MP_EQ(r, MP_ZERO)) {
77 /* --- Otherwise use the public and private moduli --- */
79 else if (!rp->e || !rp->d)
90 /* --- Work out the appropriate exponent --- *
92 * I need to compute %$s$% and %$t$% such that %$2^s t = e d - 1$%, and
96 t = mp_mul(MP_NEW, rp->e, rp->d);
97 t = mp_sub(t, t, MP_ONE);
100 /* --- Set up for the exponentiation --- */
102 mpmont_create(&mm, rp->n);
103 m1 = mp_sub(MP_NEW, rp->n, mm.r);
105 /* --- Now for the main loop --- *
107 * Choose candidate integers and attempt to factor the modulus.
110 mp_build(&a, &aw, &aw + 1);
115 /* --- Choose a random %$a$% and calculate %$z = a^t \bmod n$% --- *
117 * If %$z \equiv 1$% or %$z \equiv -1 \pmod n$% then this iteration
122 z = mpmont_mul(&mm, z, &a, mm.r2);
123 z = mpmont_expr(&mm, z, z, t);
124 if (MP_EQ(z, mm.r) || MP_EQ(z, m1))
127 /* --- Now square until something interesting happens --- *
129 * Compute %$z^{2i} \bmod n$%. Eventually, I'll either get %$-1$% or
130 * %$1$%. If the former, the number is uninteresting, and I need to
131 * restart. If the latter, the previous number minus 1 has a common
136 mp *zz = mp_sqr(MP_NEW, z);
137 zz = mpmont_reduce(&mm, zz, zz);
138 if (MP_EQ(zz, mm.r)) {
141 } else if (MP_EQ(zz, m1)) {
150 /* --- Do the factoring --- *
152 * Here's how it actually works. I've found an interesting square
153 * root of %$1 \pmod n$%. Any square root of 1 must be congruent to
154 * %$\pm 1$% modulo both %$p$% and %$q$%. Both congruent to %$1$% is
155 * boring, as is both congruent to %$-1$%. Subtracting one from the
156 * result makes it congruent to %$0$% modulo %$p$% or %$q$% (and
157 * nobody cares which), and hence can be extracted by a GCD
162 z = mpmont_reduce(&mm, z, z);
163 z = mp_sub(z, z, MP_ONE);
165 mp_gcd(&rp->p, 0, 0, rp->n, z);
167 mp_div(&rp->q, 0, rp->n, rp->p);
171 if (MP_CMP(rp->p, <, rp->q)) {
180 /* --- If %$e$% or %$d$% is missing, recalculate it --- */
182 if (!rp->e || !rp->d) {
187 /* --- Compute %$\varphi(n)$% --- */
189 phi = mp_sub(MP_NEW, rp->n, rp->p);
190 phi = mp_sub(phi, phi, rp->q);
191 phi = mp_add(phi, phi, MP_ONE);
192 p1 = mp_sub(MP_NEW, rp->p, MP_ONE);
193 q1 = mp_sub(MP_NEW, rp->q, MP_ONE);
194 mp_gcd(&g, 0, 0, p1, q1);
195 mp_div(&phi, 0, phi, g);
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 mp *p1 = mp_sub(MP_NEW, rp->p, MP_ONE);
228 mp_div(0, &rp->dp, rp->d, p1);
232 mp *q1 = mp_sub(MP_NEW, rp->q, MP_ONE);
233 mp_div(0, &rp->dq, rp->d, q1);
242 /*----- That's all, folks -------------------------------------------------*/