chiark / gitweb /
prime generation: Deploy the new Baillie--PSW testers.
[catacomb] / pub / dsa-gen.c
1 /* -*-c-*-
2  *
3  * Generate DSA shared 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 <stdio.h>
31 #include <stdlib.h>
32
33 #include "dsa.h"
34 #include "dsarand.h"
35 #include "fibrand.h"
36 #include "mp.h"
37 #include "mprand.h"
38 #include "pgen.h"
39 #include "prim.h"
40 #include "sha.h"
41
42 /*----- The DSA stepper ---------------------------------------------------*/
43
44 /* --- @next@ --- *
45  *
46  * Arguments:   @pgen_event *ev@ = pointer to event block
47  *              @dsa_stepctx *d@ = pointer to stepping context
48  *
49  * Returns:     A @PGEN@ result code.
50  *
51  * Use:         Steps the generator once, reads the result, and tests it.
52  */
53
54 static int next(pgen_event *ev, dsa_stepctx *d)
55 {
56   mp *m;
57   int rc;
58
59   /* --- Load the new candidate --- */
60
61   if (d->seedbuf)
62     d->r->ops->misc(d->r, DSARAND_GETSEED, d->seedbuf);
63   m = mprand(ev->m, d->bits, d->r, 0);
64
65   /* --- Force to be a multiple of @q@ --- */
66
67   if (d->q) {
68     mp *r = MP_NEW;
69     mp_div(0, &r, m, d->q);
70     m = mp_sub(m, m, r);
71     mp_drop(r);
72   }
73   m->v[0] |= d->or;
74   ev->m = m;
75
76   /* --- Do the trial division --- */
77
78   rc = pfilt_smallfactor(m);
79   d->count++;
80
81   /* --- Return the result --- */
82
83   return (rc);
84 }
85
86 /* --- @dsa_step@ --- */
87
88 int dsa_step(int rq, pgen_event *ev, void *p)
89 {
90   dsa_stepctx *d = p;
91
92   switch (rq) {
93     case PGEN_BEGIN:
94     case PGEN_TRY:
95       return (next(ev, d));
96     case PGEN_DONE:
97       return (PGEN_DONE);
98   }
99   return (PGEN_ABORT);
100 }
101
102 /*----- Glue code ---------------------------------------------------------*/
103
104 /* --- @dsa_gen@ --- *
105  *
106  * Arguments:   @dsa_param *dp@ = where to store parameters
107  *              @unsigned ql@ = length of @q@ in bits
108  *              @unsigned pl@ = length of @p@ in bits
109  *              @unsigned steps@ = number of steps to find @q@
110  *              @const void *k@ = pointer to key material
111  *              @size_t sz@ = size of key material
112  *              @dsa_seed *ds@ = optional pointer for output seed information
113  *              @pgen_proc *event@ = event handler function
114  *              @void *ectx@ = argument for event handler
115  *
116  * Returns:     @PGEN_DONE@ if everything worked ok; @PGEN_ABORT@ otherwise.
117  *
118  * Use:         Generates the DSA shared parameters from a given seed value.
119  *
120  *              The parameters are a prime %$q$%, relatively small, and a
121  *              large prime %$p = kq + 1$% for some %$k$%, together with a
122  *              generator %$g$% of the cyclic subgroup of order %$q$%.  These
123  *              are actually the same as the Diffie-Hellman parameter set,
124  *              but the generation algorithm is different.
125  *
126  *              The algorithm used is a compatible extension of the method
127  *              described in the DSA standard, FIPS 186.  The standard
128  *              requires that %$q$% be 160 bits in size (i.e., @ql == 160@)
129  *              and that the length of %$p$% be %$L = 512 + 64l$% for some
130  *              %$l$%.  Neither limitation applies to this implementation.
131  */
132
133 int dsa_gen(dsa_param *dp, unsigned ql, unsigned pl, unsigned steps,
134             const void *k, size_t sz, dsa_seed *ds,
135             pgen_proc *event, void *ectx)
136 {
137   dsa_stepctx s;
138   prim_ctx p;
139   int i;
140   mp *qc;
141
142   /* --- Initialize the stepping context --- */
143
144   s.r = dsarand_create(k, sz);
145
146   /* --- Find @q@ --- */
147
148   s.q = 0;
149   s.r->ops->misc(s.r, DSARAND_PASSES, 2);
150   s.bits = ql;
151   s.count = 0;
152   s.or = 1;
153   if (!ds)
154     s.seedbuf = 0;
155   else {
156     ds->sz = sz;
157     ds->p = s.seedbuf = xmalloc(sz);
158   }
159   if ((dp->q = pgen("q", MP_NEW, MP_NEW, event, ectx, steps, dsa_step, &s,
160                     PGEN_BAILLIEPSWNTESTS, pgen_bailliepswtest, 0)) == 0)
161     goto fail_q;
162
163   /* --- Find @p@ --- */
164
165   s.count = ~0;
166   s.q = mp_lsl(MP_NEW, dp->q, 1);
167   s.r->ops->misc(s.r, DSARAND_PASSES, 1);
168   s.bits = pl;
169   s.seedbuf = 0;
170   if ((dp->p = pgen("p", MP_NEW, MP_NEW, event, ectx, 4096, dsa_step, &s,
171                     PGEN_BAILLIEPSWNTESTS, pgen_bailliepswtest, 0)) == 0)
172     goto fail_p;
173   mp_drop(s.q);
174   if (ds)
175     ds->count = s.count;
176
177   /* --- Find @g@ --- *
178    *
179    * The division returns remainder 1.  This doesn't matter.
180    */
181
182   mpmont_create(&p.mm, dp->p);
183   qc = MP_NEW; mp_div(&qc, 0, dp->p, dp->q);
184   i = 0;
185   p.exp = qc;
186   p.n = 0;
187   if ((dp->g = pgen("g", MP_NEW, MP_NEW, event, ectx, 0, prim_step, &i,
188                     1, prim_test, &p)) == 0)
189     goto fail_g;
190
191   /* --- Done --- */
192
193   mp_drop(qc);
194   mpmont_destroy(&p.mm);
195   s.r->ops->destroy(s.r);
196   return (PGEN_DONE);
197
198   /* --- Tidy up when things go wrong --- */
199
200 fail_g:
201   mp_drop(qc);
202   mpmont_destroy(&p.mm);
203 fail_p:
204   mp_drop(dp->q);
205   mp_drop(s.q);
206 fail_q:
207   s.r->ops->destroy(s.r);
208   if (ds)
209     xfree(ds->p);
210   return (PGEN_ABORT);
211 }
212
213 /*----- Test rig ----------------------------------------------------------*/
214
215 #ifdef TEST_RIG
216
217 #include <mLib/macros.h>
218
219 static int verify(dstr *v)
220 {
221   mp *q = *(mp **)v[4].buf;
222   mp *p = *(mp **)v[5].buf;
223   mp *g = *(mp **)v[6].buf;
224   dsa_param dp;
225   dsa_seed ds;
226   unsigned long l = *(unsigned long *)v[1].buf;
227   unsigned long n = *(unsigned long *)v[3].buf;
228   int ok = 1;
229   int rc;
230   keycheck kc;
231   keycheck_reportctx kcr;
232
233   rc = dsa_gen(&dp, 160, l, 16, v[0].buf, v[0].len, &ds, pgen_evspin, 0);
234   if (rc || ds.count != n || ds.sz != v[2].len ||
235       MEMCMP(ds.p, !=, v[2].buf, v[2].len) ||
236       !MP_EQ(q, dp.q) || !MP_EQ(p, dp.p) || !MP_EQ(g, dp.g)) {
237     fputs("\n*** gen failed", stderr);
238     fputs("\nseed_in = ", stderr); type_hex.dump(&v[0], stderr);
239     fprintf(stderr, "\nl = %lu", l);
240     fputs("\nseed_out = ", stderr); type_hex.dump(&v[2], stderr);
241     fprintf(stderr, "\ncount = %lu", n);
242     fputs("\n   q = ", stderr); mp_writefile(q, stderr, 16);
243     fputs("\n   p = ", stderr); mp_writefile(p, stderr, 16);
244     fputs("\n   g = ", stderr); mp_writefile(g, stderr, 16);
245     if (!rc) {
246       dstr d;
247       d.buf = ds.p; d.len = ds.sz;
248       fputs("\nds.seed = ", stderr); type_hex.dump(&d, stderr);
249       fprintf(stderr, "\nds.count = %u", ds.count);
250       fputs("\ndp.q = ", stderr); mp_writefile(dp.q, stderr, 16);
251       fputs("\ndp.p = ", stderr); mp_writefile(dp.p, stderr, 16);
252       fputs("\ndp.g = ", stderr); mp_writefile(dp.g, stderr, 16);
253     }
254     fputc('\n', stderr);
255     ok = 0;
256   }
257
258   kcr.fp = stderr;
259   kcr.sev = KCSEV_ERR;
260   keycheck_init(&kc, keycheck_stdreport, &kcr);
261   if (!rc)
262     dsa_checkparam(&kc, &dp, &ds);
263   if (!keycheck_allclear(&kc, KCSEV_ERR)) {
264     fputs("\n*** gen failed check", stderr);
265     fputs("\nseed_in = ", stderr); type_hex.dump(&v[0], stderr);
266     fprintf(stderr, "\nl = %lu", l);
267     fputs("\nseed_out = ", stderr); type_hex.dump(&v[2], stderr);
268     fprintf(stderr, "\ncount = %lu", n);
269     fputs("\n   q = ", stderr); mp_writefile(q, stderr, 16);
270     fputs("\n   p = ", stderr); mp_writefile(p, stderr, 16);
271     fputs("\n   g = ", stderr); mp_writefile(g, stderr, 16);
272     fputc('\n', stderr);
273     ok = 0;
274   }
275
276   mp_drop(q); mp_drop(p); mp_drop(g);
277   if (!rc) {
278     mp_drop(dp.q); mp_drop(dp.p); mp_drop(dp.g); xfree(ds.p);
279   }
280   assert(mparena_count(MPARENA_GLOBAL) == 0);
281   return (ok);
282 }
283
284 static test_chunk tests[] = {
285   { "gen", verify,
286     { &type_hex, &type_ulong, &type_hex, &type_ulong,
287       &type_mp, &type_mp, &type_mp, 0 }  },
288   { 0, 0, { 0 } }
289 };
290
291 int main(int argc, char *argv[])
292 {
293   sub_init();
294   test_run(argc, argv, tests, SRCDIR "/t/dsa");
295   return (0);
296 }
297
298 #endif
299
300 /*----- That's all, folks -------------------------------------------------*/