Pollard's rho algorithm for computing discrete logs.
[u/mdw/catacomb] / dsarand.c
1 /* -*-c-*-
2 *
3 * $Id: dsarand.c,v 1.2 2000/06/17 10:54:00 mdw Exp $
4 *
5 * Random number generator for DSA
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: dsarand.c,v $
33 * Revision 1.2 2000/06/17 10:54:00 mdw
34 * Typesetting fixes. Arena support.
35 *
36 * Revision 1.1 1999/12/22 15:53:12 mdw
37 * Random number generator for finding DSA parameters.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <stdarg.h>
44 #include <string.h>
45
46 #include <mLib/alloc.h>
47 #include <mLib/bits.h>
48 #include <mLib/sub.h>
49
50 #include "dsarand.h"
51 #include "grand.h"
52 #include "sha.h"
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- @STEP@ --- *
57 *
58 * Arguments: @dsarand *d@ = pointer to context
59 *
60 * Use: Increments the buffer by one, interpreting it as a big-endian
61 * integer. Carries outside the integer are discarded.
62 */
63
64 #define STEP(d) do { \
65 dsarand *_d = (d); \
66 octet *_p = _d->p; \
67 octet *_q = _p + _d->sz; \
68 unsigned _c = 1; \
69 while (_c && _q > _p) { \
70 _c += *--_q; \
71 *_q = U8(_c); \
72 _c >>= 8; \
73 } \
74 } while (0)
75
76 /* --- @dsarand_init@ --- *
77 *
78 * Arguments: @dsarand *d@ = pointer to context
79 * @const void *p@ = pointer to seed buffer
80 * @size_t sz@ = size of the buffer
81 *
82 * Returns: ---
83 *
84 * Use: Initializes a DSA random number generator.
85 */
86
87 void dsarand_init(dsarand *d, const void *p, size_t sz)
88 {
89 d->p = xmalloc(sz);
90 d->sz = sz;
91 d->passes = 1;
92 if (p)
93 memcpy(d->p, p, sz);
94 }
95
96 /* --- @dsarand_reseed@ --- *
97 *
98 * Arguments: @dsarand *d@ = pointer to context
99 * @const void *p@ = pointer to seed buffer
100 * @size_t sz@ = size of the buffer
101 *
102 * Returns: ---
103 *
104 * Use: Initializes a DSA random number generator.
105 */
106
107 void dsarand_reseed(dsarand *d, const void *p, size_t sz)
108 {
109 xfree(d->p);
110 d->p = xmalloc(sz);
111 d->sz = sz;
112 d->passes = 1;
113 if (p)
114 memcpy(d->p, p, sz);
115 }
116
117 /* --- @dsarand_destroy@ --- *
118 *
119 * Arguments: @dsarand *d@ = pointer to context
120 *
121 * Returns: ---
122 *
123 * Use: Disposes of a DSA random number generation context.
124 */
125
126 void dsarand_destroy(dsarand *d)
127 {
128 xfree(d->p);
129 }
130
131 /* --- @dsarand_fill@ --- *
132 *
133 * Arguments: @dsarand *d@ = pointer to context
134 * @void *p@ = pointer to output buffer
135 * @size_t sz@ = size of output buffer
136 *
137 * Returns: ---
138 *
139 * Use: Fills an output buffer with pseudorandom data.
140 *
141 * Let %$p$% be the numerical value of the input buffer, and let
142 * %$b$% be the number of bytes required. Let
143 * %$z = \lceil b / 20 \rceil$% be the number of SHA outputs
144 * required. Then the output of pass %$n$% is
145 *
146 * %$P_n = \sum_{0 \le i < z} 2^{160i} SHA(p + nz + i)$%
147 * %${} \bmod 2^{8b}$%
148 *
149 * and the actual result in the output buffer is the XOR of all
150 * of the output passes.
151 *
152 * The DSA procedure for choosing @q@ involves two passes with
153 * %$z = 1$%; the procedure for choosing @p@ involves one pass
154 * with larger %$z$%. This generalization of the DSA generation
155 * procedure is my own invention but it seems relatively sound.
156 */
157
158 void dsarand_fill(dsarand *d, void *p, size_t sz)
159 {
160 octet *q = p;
161 unsigned n = d->passes;
162
163 /* --- Write out the first pass --- *
164 *
165 * This can write directly to the output buffer, so it's done differently
166 * from the latter passes.
167 */
168
169 {
170 size_t o = sz;
171
172 while (o) {
173 sha_ctx h;
174
175 /* --- Hash the input buffer --- */
176
177 sha_init(&h);
178 sha_hash(&h, d->p, d->sz);
179
180 /* --- If enough space, extract the hash output directly --- */
181
182 if (o >= SHA_HASHSZ) {
183 o -= SHA_HASHSZ;
184 sha_done(&h, q + o);
185 }
186
187 /* --- Otherwise take the hash result out of line and copy it --- */
188
189 else {
190 octet hash[SHA_HASHSZ];
191 sha_done(&h, hash);
192 memcpy(q, hash + (SHA_HASHSZ - o), o);
193 o = 0;
194 }
195
196 /* --- Step the input buffer --- */
197
198 STEP(d);
199 }
200
201 /* --- Another pass has been done --- */
202
203 n--;
204 }
205
206 /* --- Write out subsequent passes --- *
207 *
208 * The hash output has to be done offline, so this is slightly easier.
209 */
210
211 while (n) {
212 size_t o = sz;
213
214 while (o) {
215 sha_ctx h;
216 octet hash[SHA_HASHSZ];
217 size_t n;
218 octet *pp, *qq;
219
220 /* --- Hash the input buffer --- */
221
222 sha_init(&h);
223 sha_hash(&h, d->p, d->sz);
224 sha_done(&h, hash);
225
226 /* --- Work out how much output is wanted --- */
227
228 n = SHA_HASHSZ;
229 if (n > o)
230 n = o;
231 o -= n;
232
233 /* --- XOR the data out --- */
234
235 for (pp = hash + (SHA_HASHSZ - n), qq = q + o;
236 pp < hash + SHA_HASHSZ; pp++, qq++)
237 *qq ^= *pp;
238
239 /* --- Step the input buffer --- */
240
241 STEP(d);
242 }
243
244 /* --- Another pass is done --- */
245
246 n--;
247 }
248 }
249
250 /*----- Generic pseudorandom-number generator interface -------------------*/
251
252 static const grand_ops gops;
253
254 typedef struct gctx {
255 grand r;
256 dsarand d;
257 } gctx;
258
259 static void gdestroy(grand *r)
260 {
261 gctx *g = (gctx *)r;
262 dsarand_destroy(&g->d);
263 DESTROY(g);
264 }
265
266 static int gmisc(grand *r, unsigned op, ...)
267 {
268 gctx *g = (gctx *)r;
269 va_list ap;
270 int rc = 0;
271 va_start(ap, op);
272
273 switch (op) {
274 case GRAND_CHECK:
275 switch (va_arg(ap, unsigned)) {
276 case GRAND_CHECK:
277 case GRAND_SEEDBLOCK:
278 case GRAND_SEEDRAND:
279 case DSARAND_PASSES:
280 rc = 1;
281 break;
282 default:
283 rc = 0;
284 break;
285 }
286 break;
287 case GRAND_SEEDBLOCK: {
288 const void *p = va_arg(ap, const void *);
289 size_t sz = va_arg(ap, size_t);
290 dsarand_reseed(&g->d, p, sz);
291 } break;
292 case GRAND_SEEDRAND: {
293 grand *rr = va_arg(ap, grand *);
294 rr->ops->fill(rr, g->d.p, g->d.sz);
295 } break;
296 case DSARAND_PASSES:
297 g->d.passes = va_arg(ap, unsigned);
298 break;
299 default:
300 GRAND_BADOP;
301 break;
302 }
303
304 va_end(ap);
305 return (rc);
306 }
307
308 static void gfill(grand *r, void *p, size_t sz)
309 {
310 gctx *g = (gctx *)r;
311 dsarand_fill(&g->d, p, sz);
312 }
313
314 static const grand_ops gops = {
315 "dsarand",
316 0, 0,
317 gmisc, gdestroy,
318 grand_word, grand_byte, grand_word, grand_range, gfill
319 };
320
321 /* --- @dsarand_create@ --- *
322 *
323 * Arguments: @const void *p@ = pointer to seed buffer
324 * @size_t sz@ = size of seed buffer
325 *
326 * Returns: Pointer to a generic generator.
327 *
328 * Use: Constructs a generic generator interface over a Catacomb
329 * entropy pool generator.
330 */
331
332 grand *dsarand_create(const void *p, size_t sz)
333 {
334 gctx *g = CREATE(gctx);
335 g->r.ops = &gops;
336 dsarand_init(&g->d, p, sz);
337 return (&g->r);
338 }
339
340 /*----- That's all, folks -------------------------------------------------*/