Renamed from `rsa-decrypt', since the name was no longer appropriate.
[u/mdw/catacomb] / strongprime.c
CommitLineData
a30942cc 1/* -*-c-*-
2 *
47566c4d 3 * $Id: strongprime.c,v 1.3 2000/06/17 12:10:09 mdw Exp $
a30942cc 4 *
5 * Generate `strong' prime numbers
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: strongprime.c,v $
47566c4d 33 * Revision 1.3 2000/06/17 12:10:09 mdw
34 * Add some argument checking. Use MP secure memory interface.
35 *
052b36d0 36 * Revision 1.2 2000/02/12 18:21:03 mdw
37 * Overhaul of key management (again).
38 *
a30942cc 39 * Revision 1.1 1999/12/22 15:51:22 mdw
40 * Find `strong' RSA primes using Gordon's algorithm.
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46#include <mLib/dstr.h>
47
48#include "grand.h"
49#include "rand.h"
50#include "mp.h"
51#include "mpmont.h"
52#include "mprand.h"
53#include "pgen.h"
54#include "pfilt.h"
55#include "rabin.h"
56
57/*----- Main code ---------------------------------------------------------*/
58
052b36d0 59/* --- @strongprime_setup@ --- *
a30942cc 60 *
61 * Arguments: @const char *name@ = pointer to name root
052b36d0 62 * @mp *d@ = destination for search start point
63 * @pfilt *f@ = where to store filter jump context
a30942cc 64 * @unsigned nbits@ = number of bits wanted
65 * @grand *r@ = random number source
66 * @unsigned n@ = number of attempts to make
67 * @pgen_proc *event@ = event handler function
68 * @void *ectx@ = argument for the event handler
69 *
052b36d0 70 * Returns: A starting point for a `strong' prime search, or zero.
a30942cc 71 *
052b36d0 72 * Use: Sets up for a strong prime search, so that primes with
73 * particular properties can be found. It's probably important
74 * to note that the number left in the filter context @f@ is
75 * congruent to 2 (mod 4).
a30942cc 76 */
77
052b36d0 78mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
79 grand *r, unsigned n, pgen_proc *event, void *ectx)
a30942cc 80{
052b36d0 81 mp *s, *t, *q;
a30942cc 82 dstr dn = DSTR_INIT;
83
052b36d0 84 mp *rr = d;
a30942cc 85 pgen_filterctx c;
052b36d0 86 pgen_jumpctx j;
a30942cc 87 rabin rb;
88
89 /* --- The bitslop parameter --- *
90 *
91 * There's quite a lot of prime searching to be done. The constant
92 * @BITSLOP@ is a (low) approximation to the base-2 log of the expected
93 * number of steps to find a prime number. Experimentation shows that
94 * numbers around 10 seem to be good.
95 */
96
052b36d0 97#define BITSLOP 12
a30942cc 98
99 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
100
47566c4d 101 assert(((void)"nbits too small in strongprime_setup", nbits/2 > BITSLOP));
a30942cc 102 nbits = nbits/2 - BITSLOP;
103 c.step = 1;
104
105 rr = mprand(rr, nbits, r, 1);
106 DRESET(&dn); dstr_putf(&dn, "%s [s]", name);
47566c4d 107 if ((s = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
052b36d0 108 rabin_iters(nbits), pgen_test, &rb)) == 0)
a30942cc 109 goto fail_s;
a30942cc 110
111 rr = mprand(rr, nbits, r, 1);
112 DRESET(&dn); dstr_putf(&dn, "%s [t]", name);
47566c4d 113 if ((t = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
a30942cc 114 rabin_iters(nbits), pgen_test, &rb)) == 0)
115 goto fail_t;
a30942cc 116
117 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
118
119 rr = mp_lsl(rr, t, 1);
120 pfilt_create(&c.f, rr);
121 rr = mp_lsl(rr, rr, BITSLOP - 1);
122 rr = mp_add(rr, rr, MP_ONE);
123 DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
052b36d0 124 j.j = &c.f;
a30942cc 125 nbits += BITSLOP;
052b36d0 126 q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
127 rabin_iters(nbits), pgen_test, &rb);
a30942cc 128 pfilt_destroy(&c.f);
052b36d0 129 if (!q)
130 goto fail_r;
a30942cc 131
132 /* --- Select a suitable starting-point for finding %$p$% --- *
133 *
134 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
135 */
136
137 {
138 mpmont mm;
139
140 mpmont_create(&mm, q);
141 rr = mp_sub(rr, q, MP_TWO);
142 rr = mpmont_exp(&mm, rr, s, rr);
143 mpmont_destroy(&mm);
144 rr = mp_mul(rr, rr, s);
145 rr = mp_lsl(rr, rr, 1);
146 rr = mp_sub(rr, rr, MP_ONE);
147 }
148
149 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
150
151 {
152 mp *x;
153 x = mp_mul(MP_NEW, q, s);
154 x = mp_lsl(x, x, 1);
052b36d0 155 pfilt_create(f, x);
a30942cc 156 x = mp_lsl(x, x, BITSLOP - 1);
157 rr = mp_add(rr, rr, x);
158 mp_drop(x);
159 }
160
052b36d0 161 /* --- Return the result --- */
a30942cc 162
052b36d0 163#if 0
164fputs("r = ", stdout); mp_writefile(q, stdout, 10); putchar('\n');
165fputs("s = ", stdout); mp_writefile(s, stdout, 10); putchar('\n');
166fputs("t = ", stdout); mp_writefile(t, stdout, 10); putchar('\n');
167#endif
a30942cc 168
a30942cc 169 mp_drop(q);
052b36d0 170 mp_drop(t);
171 mp_drop(s);
172 dstr_destroy(&dn);
173 return (rr);
174
175 /* --- Tidy up if something failed --- */
176
a30942cc 177fail_r:
a30942cc 178 mp_drop(t);
179fail_t:
180 mp_drop(s);
181fail_s:
182 mp_drop(rr);
183 dstr_destroy(&dn);
052b36d0 184 return (0);
a30942cc 185
186#undef BITSLOP
187}
188
052b36d0 189/* --- @strongprime@ --- *
190 *
191 * Arguments: @const char *name@ = pointer to name root
192 * @mp *d@ = destination integer
193 * @unsigned nbits@ = number of bits wanted
194 * @grand *r@ = random number source
195 * @unsigned n@ = number of attempts to make
196 * @pgen_proc *event@ = event handler function
197 * @void *ectx@ = argument for the event handler
198 *
199 * Returns: A `strong' prime, or zero.
200 *
201 * Use: Finds `strong' primes. A strong prime %$p$% is such that
202 *
203 * * %$p - 1$% has a large prime factor %$r$%,
204 * * %$p + 1$% has a large prime factor %$s$%, and
205 * * %$r - 1$% has a large prime factor %$t$%.
206 *
207 * The numbers produced may be slightly larger than requested,
208 * by a few bits.
209 */
210
211mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
212 unsigned n, pgen_proc *event, void *ectx)
213{
214 pfilt f;
215 pgen_jumpctx j;
216 rabin rb;
217
218 d = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
219 j.j = &f;
220 d = pgen(name, d, d, event, ectx, n, pgen_jump, &j,
221 rabin_iters(nbits), pgen_test, &rb);
222 pfilt_destroy(&f);
223 return (d);
224}
225
a30942cc 226/*----- That's all, folks -------------------------------------------------*/