3 * $Id: dsarand.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
5 * Random number generator for DSA
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/alloc.h>
36 #include <mLib/bits.h>
43 /*----- Main code ---------------------------------------------------------*/
47 * Arguments: @dsarand *d@ = pointer to context
49 * Use: Increments the buffer by one, interpreting it as a big-endian
50 * integer. Carries outside the integer are discarded.
53 #define STEP(d) do { \
56 octet *_q = _p + _d->sz; \
58 while (_c && _q > _p) { \
65 /* --- @dsarand_init@ --- *
67 * Arguments: @dsarand *d@ = pointer to context
68 * @const void *p@ = pointer to seed buffer
69 * @size_t sz@ = size of the buffer
73 * Use: Initializes a DSA random number generator.
76 void dsarand_init(dsarand
*d
, const void *p
, size_t sz
)
85 /* --- @dsarand_reseed@ --- *
87 * Arguments: @dsarand *d@ = pointer to context
88 * @const void *p@ = pointer to seed buffer
89 * @size_t sz@ = size of the buffer
93 * Use: Initializes a DSA random number generator.
96 void dsarand_reseed(dsarand
*d
, const void *p
, size_t sz
)
106 /* --- @dsarand_destroy@ --- *
108 * Arguments: @dsarand *d@ = pointer to context
112 * Use: Disposes of a DSA random number generation context.
115 void dsarand_destroy(dsarand
*d
)
120 /* --- @dsarand_fill@ --- *
122 * Arguments: @dsarand *d@ = pointer to context
123 * @void *p@ = pointer to output buffer
124 * @size_t sz@ = size of output buffer
128 * Use: Fills an output buffer with pseudorandom data.
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
135 * %$P_n = \sum_{0 \le i < z} 2^{160i} SHA(p + nz + i)$%
136 * %${} \bmod 2^{8b}$%
138 * and the actual result in the output buffer is the XOR of all
139 * of the output passes.
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.
147 void dsarand_fill(dsarand
*d
, void *p
, size_t sz
)
150 unsigned n
= d
->passes
;
152 /* --- Write out the first pass --- *
154 * This can write directly to the output buffer, so it's done differently
155 * from the latter passes.
164 /* --- Hash the input buffer --- */
167 sha_hash(&h
, d
->p
, d
->sz
);
169 /* --- If enough space, extract the hash output directly --- */
171 if (o
>= SHA_HASHSZ
) {
176 /* --- Otherwise take the hash result out of line and copy it --- */
179 octet hash
[SHA_HASHSZ
];
181 memcpy(q
, hash
+ (SHA_HASHSZ
- o
), o
);
185 /* --- Step the input buffer --- */
190 /* --- Another pass has been done --- */
195 /* --- Write out subsequent passes --- *
197 * The hash output has to be done offline, so this is slightly easier.
205 octet hash
[SHA_HASHSZ
];
209 /* --- Hash the input buffer --- */
212 sha_hash(&h
, d
->p
, d
->sz
);
215 /* --- Work out how much output is wanted --- */
222 /* --- XOR the data out --- */
224 for (pp
= hash
+ (SHA_HASHSZ
- n
), qq
= q
+ o
;
225 pp
< hash
+ SHA_HASHSZ
; pp
++, qq
++)
228 /* --- Step the input buffer --- */
233 /* --- Another pass is done --- */
239 /*----- Generic pseudorandom-number generator interface -------------------*/
241 static const grand_ops gops
;
243 typedef struct gctx
{
248 static void gdestroy(grand
*r
)
251 dsarand_destroy(&g
->d
);
255 static int gmisc(grand
*r
, unsigned op
, ...)
264 switch (va_arg(ap
, unsigned)) {
266 case GRAND_SEEDBLOCK
:
270 case DSARAND_GETSEED
:
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
);
283 case GRAND_SEEDRAND
: {
284 grand
*rr
= va_arg(ap
, grand
*);
285 rr
->ops
->fill(rr
, g
->d
.p
, g
->d
.sz
);
288 g
->d
.passes
= va_arg(ap
, unsigned);
293 case DSARAND_GETSEED
:
294 memcpy(va_arg(ap
, void *), g
->d
.p
, g
->d
.sz
);
305 static void gfill(grand
*r
, void *p
, size_t sz
)
308 dsarand_fill(&g
->d
, p
, sz
);
311 static const grand_ops gops
= {
315 grand_word
, grand_byte
, grand_word
, grand_range
, gfill
318 /* --- @dsarand_create@ --- *
320 * Arguments: @const void *p@ = pointer to seed buffer
321 * @size_t sz@ = size of seed buffer
323 * Returns: Pointer to a generic generator.
325 * Use: Constructs a generic generator interface over a Catacomb
326 * entropy pool generator.
329 grand
*dsarand_create(const void *p
, size_t sz
)
331 gctx
*g
= CREATE(gctx
);
333 dsarand_init(&g
->d
, p
, sz
);
337 /*----- That's all, folks -------------------------------------------------*/