3 * $Id: dsarand.c,v 1.1 1999/12/22 15:53:12 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.1 1999/12/22 15:53:12 mdw
34 * Random number generator for finding DSA parameters.
38 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
50 /*----- Main code ---------------------------------------------------------*/
54 * Arguments: @dsarand *d@ = pointer to context
56 * Use: Increments the buffer by one, interpreting it as a big-endian
57 * integer. Carries outside the integer are discarded.
60 #define STEP(d) do { \
63 octet *_q = _p + _d->sz; \
65 while (_c && _q > _p) { \
72 /* --- @dsarand_init@ --- *
74 * Arguments: @dsarand *d@ = pointer to context
75 * @const void *p@ = pointer to seed buffer
76 * @size_t sz@ = size of the buffer
80 * Use: Initializes a DSA random number generator.
83 void dsarand_init(dsarand
*d
, const void *p
, size_t sz
)
91 /* --- @dsarand_reseed@ --- *
93 * Arguments: @dsarand *d@ = pointer to context
94 * @const void *p@ = pointer to seed buffer
95 * @size_t sz@ = size of the buffer
99 * Use: Initializes a DSA random number generator.
102 void dsarand_reseed(dsarand
*d
, const void *p
, size_t sz
)
112 /* --- @dsarand_destroy@ --- *
114 * Arguments: @dsarand *d@ = pointer to context
118 * Use: Disposes of a DSA random number generation context.
121 void dsarand_destroy(dsarand
*d
)
126 /* --- @dsarand_fill@ --- *
128 * Arguments: @dsarand *d@ = pointer to context
129 * @void *p@ = pointer to output buffer
130 * @size_t sz@ = size of output buffer
134 * Use: Fills an output buffer with pseudorandom data.
136 * Let %$p$% be the numerical value of the input buffer, and let
137 * %$b$% be the number of bytes required. Let
138 * %$z = \lceil b / 20 \rceil%$ be the number of SHA outputs
139 * required. Then the output of pass %$n$% is
141 * %$P_n = \sum_{0 \le i < z} 2^{160i} SHA(p + nz + i)$%
142 * %${} \bmod 2^{8b}$%
144 * and the actual result in the output buffer is the XOR of all
145 * of the output passes.
147 * The DSA procedure for choosing @q@ involves two passes with
148 * %$z = 1$%; the procedure for choosing @p@ involves one pass
149 * with larger %$z$%. This generalization of the DSA generation
150 * procedure is my own invention but it seems relatively sound.
153 void dsarand_fill(dsarand
*d
, void *p
, size_t sz
)
156 unsigned n
= d
->passes
;
158 /* --- Write out the first pass --- *
160 * This can write directly to the output buffer, so it's done differently
161 * from the latter passes.
170 /* --- Hash the input buffer --- */
173 sha_hash(&h
, d
->p
, d
->sz
);
175 /* --- If enough space, extract the hash output directly --- */
177 if (o
>= SHA_HASHSZ
) {
182 /* --- Otherwise take the hash result out of line and copy it --- */
185 octet hash
[SHA_HASHSZ
];
187 memcpy(q
, hash
+ (SHA_HASHSZ
- o
), o
);
191 /* --- Step the input buffer --- */
196 /* --- Another pass has been done --- */
201 /* --- Write out subsequent passes --- *
203 * The hash output has to be done offline, so this is slightly easier.
211 octet hash
[SHA_HASHSZ
];
215 /* --- Hash the input buffer --- */
218 sha_hash(&h
, d
->p
, d
->sz
);
221 /* --- Work out how much output is wanted --- */
228 /* --- XOR the data out --- */
230 for (pp
= hash
+ (SHA_HASHSZ
- n
), qq
= q
+ o
;
231 pp
< hash
+ SHA_HASHSZ
; pp
++, qq
++)
234 /* --- Step the input buffer --- */
239 /* --- Another pass is done --- */
245 /*----- Generic pseudorandom-number generator interface -------------------*/
247 static const grand_ops gops
;
249 typedef struct gctx
{
254 static void gdestroy(grand
*r
)
257 dsarand_destroy(&g
->d
);
261 static int gmisc(grand
*r
, unsigned op
, ...)
270 switch (va_arg(ap
, unsigned)) {
272 case GRAND_SEEDBLOCK
:
282 case GRAND_SEEDBLOCK
: {
283 const void *p
= va_arg(ap
, const void *);
284 size_t sz
= va_arg(ap
, size_t);
285 dsarand_reseed(&g
->d
, p
, sz
);
287 case GRAND_SEEDRAND
: {
288 grand
*rr
= va_arg(ap
, grand
*);
289 rr
->ops
->fill(rr
, g
->d
.p
, g
->d
.sz
);
292 g
->d
.passes
= va_arg(ap
, unsigned);
303 static void gfill(grand
*r
, void *p
, size_t sz
)
306 dsarand_fill(&g
->d
, p
, sz
);
309 static const grand_ops gops
= {
313 grand_word
, grand_byte
, grand_word
, grand_range
, gfill
316 /* --- @dsarand_create@ --- *
318 * Arguments: @const void *p@ = pointer to seed buffer
319 * @size_t sz@ = size of seed buffer
321 * Returns: Pointer to a generic generator.
323 * Use: Constructs a generic generator interface over a Catacomb
324 * entropy pool generator.
327 grand
*dsarand_create(const void *p
, size_t sz
)
329 gctx
*g
= CREATE(gctx
);
331 dsarand_init(&g
->d
, p
, sz
);
335 /*----- That's all, folks -------------------------------------------------*/