3 * $Id: dsarand.c,v 1.2 2000/06/17 10:54:00 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/06/17 10:54:00 mdw
34 * Typesetting fixes. Arena support.
36 * Revision 1.1 1999/12/22 15:53:12 mdw
37 * Random number generator for finding DSA parameters.
41 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/alloc.h>
47 #include <mLib/bits.h>
54 /*----- Main code ---------------------------------------------------------*/
58 * Arguments: @dsarand *d@ = pointer to context
60 * Use: Increments the buffer by one, interpreting it as a big-endian
61 * integer. Carries outside the integer are discarded.
64 #define STEP(d) do { \
67 octet *_q = _p + _d->sz; \
69 while (_c && _q > _p) { \
76 /* --- @dsarand_init@ --- *
78 * Arguments: @dsarand *d@ = pointer to context
79 * @const void *p@ = pointer to seed buffer
80 * @size_t sz@ = size of the buffer
84 * Use: Initializes a DSA random number generator.
87 void dsarand_init(dsarand
*d
, const void *p
, size_t sz
)
96 /* --- @dsarand_reseed@ --- *
98 * Arguments: @dsarand *d@ = pointer to context
99 * @const void *p@ = pointer to seed buffer
100 * @size_t sz@ = size of the buffer
104 * Use: Initializes a DSA random number generator.
107 void dsarand_reseed(dsarand
*d
, const void *p
, size_t sz
)
117 /* --- @dsarand_destroy@ --- *
119 * Arguments: @dsarand *d@ = pointer to context
123 * Use: Disposes of a DSA random number generation context.
126 void dsarand_destroy(dsarand
*d
)
131 /* --- @dsarand_fill@ --- *
133 * Arguments: @dsarand *d@ = pointer to context
134 * @void *p@ = pointer to output buffer
135 * @size_t sz@ = size of output buffer
139 * Use: Fills an output buffer with pseudorandom data.
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
146 * %$P_n = \sum_{0 \le i < z} 2^{160i} SHA(p + nz + i)$%
147 * %${} \bmod 2^{8b}$%
149 * and the actual result in the output buffer is the XOR of all
150 * of the output passes.
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.
158 void dsarand_fill(dsarand
*d
, void *p
, size_t sz
)
161 unsigned n
= d
->passes
;
163 /* --- Write out the first pass --- *
165 * This can write directly to the output buffer, so it's done differently
166 * from the latter passes.
175 /* --- Hash the input buffer --- */
178 sha_hash(&h
, d
->p
, d
->sz
);
180 /* --- If enough space, extract the hash output directly --- */
182 if (o
>= SHA_HASHSZ
) {
187 /* --- Otherwise take the hash result out of line and copy it --- */
190 octet hash
[SHA_HASHSZ
];
192 memcpy(q
, hash
+ (SHA_HASHSZ
- o
), o
);
196 /* --- Step the input buffer --- */
201 /* --- Another pass has been done --- */
206 /* --- Write out subsequent passes --- *
208 * The hash output has to be done offline, so this is slightly easier.
216 octet hash
[SHA_HASHSZ
];
220 /* --- Hash the input buffer --- */
223 sha_hash(&h
, d
->p
, d
->sz
);
226 /* --- Work out how much output is wanted --- */
233 /* --- XOR the data out --- */
235 for (pp
= hash
+ (SHA_HASHSZ
- n
), qq
= q
+ o
;
236 pp
< hash
+ SHA_HASHSZ
; pp
++, qq
++)
239 /* --- Step the input buffer --- */
244 /* --- Another pass is done --- */
250 /*----- Generic pseudorandom-number generator interface -------------------*/
252 static const grand_ops gops
;
254 typedef struct gctx
{
259 static void gdestroy(grand
*r
)
262 dsarand_destroy(&g
->d
);
266 static int gmisc(grand
*r
, unsigned op
, ...)
275 switch (va_arg(ap
, unsigned)) {
277 case GRAND_SEEDBLOCK
:
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
);
292 case GRAND_SEEDRAND
: {
293 grand
*rr
= va_arg(ap
, grand
*);
294 rr
->ops
->fill(rr
, g
->d
.p
, g
->d
.sz
);
297 g
->d
.passes
= va_arg(ap
, unsigned);
308 static void gfill(grand
*r
, void *p
, size_t sz
)
311 dsarand_fill(&g
->d
, p
, sz
);
314 static const grand_ops gops
= {
318 grand_word
, grand_byte
, grand_word
, grand_range
, gfill
321 /* --- @dsarand_create@ --- *
323 * Arguments: @const void *p@ = pointer to seed buffer
324 * @size_t sz@ = size of seed buffer
326 * Returns: Pointer to a generic generator.
328 * Use: Constructs a generic generator interface over a Catacomb
329 * entropy pool generator.
332 grand
*dsarand_create(const void *p
, size_t sz
)
334 gctx
*g
= CREATE(gctx
);
336 dsarand_init(&g
->d
, p
, sz
);
340 /*----- That's all, folks -------------------------------------------------*/