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