3 * $Id: rsa-gen.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
5 * RSA parameter generation
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 ------------------------------------------------------*/
32 #include <mLib/dstr.h>
39 #include "strongprime.h"
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- @rsa_gen@ --- *
45 * Arguments: @rsa_priv *rp@ = pointer to block to be filled in
46 * @unsigned nbits@ = required modulus size in bits
47 * @grand *r@ = random number source
48 * @unsigned n@ = number of attempts to make
49 * @pgen_proc *event@ = event handler function
50 * @void *ectx@ = argument for the event handler
52 * Returns: Zero if all went well, nonzero otherwise.
54 * Use: Constructs a pair of strong RSA primes and other useful RSA
55 * parameters. A small encryption exponent is chosen if
59 int rsa_gen(rsa_priv
*rp
, unsigned nbits
, grand
*r
, unsigned n
,
60 pgen_proc
*event
, void *ectx
)
65 /* --- Bits of initialization --- */
67 rp
->e
= mp_fromulong(MP_NEW
, 0x10001);
70 /* --- Generate strong primes %$p$% and %$q$% --- *
72 * Constrain the GCD of @q@ to ensure that overly small private exponents
73 * are impossible. Current results suggest that if %$d < n^{0.29}$% then
74 * it can be guessed fairly easily. This implementation is rather more
75 * conservative about that sort of thing.
79 if ((rp
->p
= strongprime("p", MP_NEWSEC
, nbits
/2, r
, n
, event
, ectx
)) == 0)
82 /* --- Do painful fiddling with GCD steppers --- */
88 if ((q
= strongprime_setup("q", MP_NEWSEC
, &g
.jp
, nbits
/ 2,
89 r
, n
, event
, ectx
)) == 0)
91 g
.r
= mp_lsr(MP_NEW
, rp
->p
, 1);
94 q
= pgen("q", q
, q
, event
, ectx
, n
, pgen_gcdstep
, &g
,
95 rabin_iters(nbits
/2), pgen_test
, &rb
);
108 /* --- Ensure that %$p > q$% --- *
110 * Also ensure that %$p$% and %$q$% are sufficiently different to deter
111 * square-root-based factoring methods.
114 phi
= mp_sub(phi
, rp
->p
, rp
->q
);
115 if (MP_LEN(phi
) * 4 < MP_LEN(rp
->p
) * 3 ||
116 MP_LEN(phi
) * 4 < MP_LEN(rp
->q
) * 3) {
125 if (phi
->f
& MP_NEG
) {
131 /* --- Work out the modulus and the CRT coefficient --- */
133 rp
->n
= mp_mul(MP_NEW
, rp
->p
, rp
->q
);
134 rp
->q_inv
= mp_modinv(MP_NEW
, rp
->q
, rp
->p
);
136 /* --- Work out %$\varphi(n) = (p - 1)(q - 1)$% --- *
138 * Save on further multiplications by noting that %$n = pq$% is known and
139 * that %$(p - 1)(q - 1) = pq - p - q + 1$%. To minimize the size of @d@
140 * (useful for performance reasons, although not very because an overly
141 * small @d@ will be rejected for security reasons) this is then divided by
142 * %$\gcd(p - 1, q - 1)$%.
145 phi
= mp_sub(phi
, rp
->n
, rp
->p
);
146 phi
= mp_sub(phi
, phi
, rp
->q
);
147 phi
= mp_add(phi
, phi
, MP_ONE
);
148 phi
= mp_lsr(phi
, phi
, 1);
149 mp_div(&phi
, 0, phi
, g
.g
);
151 /* --- Decide on a public exponent --- *
153 * Simultaneously compute the private exponent.
156 mp_gcd(&g
.g
, 0, &rp
->d
, phi
, rp
->e
);
157 if (!MP_EQ(g
.g
, MP_ONE
) && MP_LEN(rp
->d
) * 4 > MP_LEN(rp
->n
) * 3)
160 /* --- Work out exponent residues --- */
162 rp
->dp
= MP_NEW
; phi
= mp_sub(phi
, rp
->p
, MP_ONE
);
163 mp_div(0, &rp
->dp
, rp
->d
, phi
);
165 rp
->dq
= MP_NEW
; phi
= mp_sub(phi
, rp
->q
, MP_ONE
);
166 mp_div(0, &rp
->dq
, rp
->d
, phi
);
174 /* --- Tidy up when something goes wrong --- */
191 /*----- That's all, folks -------------------------------------------------*/