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