d03ab969 |
1 | /* -*-c-*- |
2 | * |
b3f05084 |
3 | * $Id: rand.h,v 1.4 1999/12/10 23:29:48 mdw Exp $ |
d03ab969 |
4 | * |
5 | * Secure random number generator |
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: rand.h,v $ |
b3f05084 |
33 | * Revision 1.4 1999/12/10 23:29:48 mdw |
34 | * Change header file guard names. |
35 | * |
dd985e0f |
36 | * Revision 1.3 1999/10/15 21:04:30 mdw |
37 | * Increase output buffer a bit for performance. |
38 | * |
ba044e65 |
39 | * Revision 1.2 1999/10/12 21:00:15 mdw |
40 | * Make pool and buffer sizes more sensible. |
41 | * |
d03ab969 |
42 | * Revision 1.1 1999/09/03 08:41:12 mdw |
43 | * Initial import. |
44 | * |
45 | */ |
46 | |
47 | /*----- Notes on the random number generator ------------------------------* |
48 | * |
49 | * The algorithm is one of the author's own devising. It may therefore be |
50 | * worth a certain amount of skepticism. However, I've thought about this |
51 | * method for over a year before actually considering it worth implementing. |
52 | * With a little bit of luck, it should have received some peer review by the |
53 | * time this code is actually properly released, and it'll be worth a bit |
54 | * more confidence. My earlier generator was very similar in structure to |
55 | * the Linux /dev/random device. This generator is intended to address |
56 | * concerns I expressed about the Linux generator in a Usenet article to |
57 | * sci.crypt. |
58 | * |
b3f05084 |
59 | * The generator is divided into two parts: an input pool and an output |
d03ab969 |
60 | * buffer. New random data is placed into the pool in the way described |
61 | * below, which is shamelessly stolen from the Linux /dev/random generator. |
62 | * The only interaction that the pool has on the output buffer is through the |
63 | * keyed `gating' operation, which mixes up and redistributes all of the |
64 | * generator's state in an irreversible manner. Random bytes, when |
65 | * requested, are extracted from the output buffer in a linear fashion. |
66 | * |
67 | * The input pool is best seen as being eight shift registers in parallel. |
68 | * Data is added to the pool one octet at a time. Each bit of a new octet is |
69 | * added to a different shift register, by adding it (mod 2) with other bits |
70 | * according to the coefficients of a primitive polynomial. Each new byte is |
71 | * rotated before being added into the pool, in a half-hearted attempt to |
72 | * protect against biases in the input data (e.g., top bits being clear on |
73 | * ASCII text). |
74 | * |
75 | * The gating operation takes a keyed hash of the entire generator state, |
76 | * uses it as the key for a symmetric cipher, and encrypts the state. The |
77 | * key is then discarded. The result is that every ouptut bit of the |
78 | * operation depends in a complex way on every input bit, but the operation |
79 | * cannot be reversed. |
80 | * |
81 | * As an added wrinkle, 160 bits of the output buffer are never actually |
82 | * output. They are used in the gating operation only, as an extra item that |
83 | * an adversary has to guess before predicting generator output. |
84 | */ |
85 | |
b3f05084 |
86 | #ifndef CATACOMB_RAND_H |
87 | #define CATACOMB_RAND_H |
d03ab969 |
88 | |
89 | #ifdef __cplusplus |
90 | extern "C" { |
91 | #endif |
92 | |
93 | /*----- Header files ------------------------------------------------------*/ |
94 | |
95 | #include <stddef.h> |
96 | |
b3f05084 |
97 | #ifndef CATACOMB_GRAND_H |
98 | # include "grand.h" |
99 | #endif |
100 | |
101 | #ifndef CATACOMB_RMD160_HMAC_H |
102 | # include "rmd160-hmac.h" |
103 | #endif |
d03ab969 |
104 | |
105 | /*----- Magic numbers -----------------------------------------------------*/ |
106 | |
ba044e65 |
107 | #define RAND_POOLSZ 128 /* Input pool size in bytes */ |
dd985e0f |
108 | #define RAND_BUFSZ 512 /* Output buffer size in bytes */ |
d03ab969 |
109 | #define RAND_SECSZ 20 /* Secret octets in output buffer */ |
110 | |
111 | #define RAND_IBITS (RAND_POOLSZ * 8) |
112 | #define RAND_OBITS (RAND_BUFSZ * 8) |
113 | |
114 | /*----- Data structures ---------------------------------------------------*/ |
115 | |
116 | /* --- A random number generator pool --- */ |
117 | |
118 | typedef struct rand_pool { |
119 | octet pool[RAND_POOLSZ]; /* Actual contents of the pool */ |
120 | unsigned i; /* Current index into pool */ |
121 | unsigned irot; /* Current rotation applied */ |
122 | unsigned ibits; /* Number of good bits in pool */ |
123 | octet buf[RAND_BUFSZ]; /* Random octet output buffer */ |
124 | unsigned o; /* Current index into buffer */ |
125 | unsigned obits; /* Number of good bits in buffer */ |
126 | rmd160_mackey k; /* Secret key for this pool */ |
127 | const struct rand_source *s; /* System-specific noise source */ |
128 | } rand_pool; |
129 | |
130 | #define RAND_GLOBAL ((rand_pool *)0) /* The global randomness pool */ |
131 | |
132 | /* --- A noise source --- */ |
133 | |
134 | typedef struct rand_source { |
135 | void (*getnoise)(rand_pool */*r*/); /* Acquire more noise */ |
136 | int (*timer)(rand_pool */*r*/); /* Get noise from current time */ |
137 | } rand_source; |
138 | |
139 | /*----- Functions provided ------------------------------------------------*/ |
140 | |
141 | /* --- @rand_init@ --- * |
142 | * |
143 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
144 | * |
145 | * Returns: --- |
146 | * |
147 | * Use: Initializes a randomness pool. The pool doesn't start out |
148 | * very random: that's your job to sort out. |
149 | */ |
150 | |
151 | extern void rand_init(rand_pool */*r*/); |
152 | |
153 | /* --- @rand_noisesrc@ --- * |
154 | * |
155 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
156 | * @const rand_source *s@ = pointer to source definition |
157 | * |
158 | * Returns: --- |
159 | * |
160 | * Use: Sets a noise source for a randomness pool. When the pool's |
161 | * estimate of good random bits falls to zero, the @getnoise@ |
162 | * function is called, passing the pool handle as an argument. |
163 | * It is expected to increase the number of good bits by at |
164 | * least one, because it'll be called over and over again until |
165 | * there are enough bits to satisfy the caller. The @timer@ |
166 | * function is called frequently throughout the generator's |
167 | * operation. |
168 | */ |
169 | |
170 | extern void rand_noisesrc(rand_pool */*r*/, const rand_source */*s*/); |
171 | |
172 | /* --- @rand_key@ --- * |
173 | * |
174 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
175 | * @const void *k@ = pointer to key data |
176 | * @size_t sz@ = size of key data |
177 | * |
178 | * Returns: --- |
179 | * |
180 | * Use: Sets the secret key for a randomness pool. The key is used |
181 | * when mixing in new random bits. |
182 | */ |
183 | |
184 | extern void rand_key(rand_pool */*r*/, const void */*k*/, size_t /*sz*/); |
185 | |
186 | /* --- @rand_add@ --- * |
187 | * |
188 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
189 | * @const void *p@ = pointer a buffer of data to add |
190 | * @size_t sz@ = size of the data buffer |
191 | * @unsigned goodbits@ = number of good bits estimated in buffer |
192 | * |
193 | * Returns: --- |
194 | * |
195 | * Use: Mixes the data in the buffer with the contents of the |
196 | * pool. The estimate of the number of good bits is added to |
197 | * the pool's own count. The mixing operation is not |
198 | * cryptographically strong. However, data in the input pool |
199 | * isn't output directly, only through the one-way gating |
200 | * operation, so that shouldn't matter. |
201 | */ |
202 | |
203 | extern void rand_add(rand_pool */*r*/, |
204 | const void */*p*/, size_t /*sz*/, |
205 | unsigned /*goodbits*/); |
206 | |
207 | /* --- @rand_goodbits@ --- * |
208 | * |
209 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
210 | * |
211 | * Returns: Estimate of the number of good bits remaining in the pool. |
212 | */ |
213 | |
214 | extern unsigned rand_goodbits(rand_pool */*r*/); |
215 | |
216 | /* --- @rand_gate@ --- * |
217 | * |
218 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
219 | * |
220 | * Returns: --- |
221 | * |
222 | * Use: Mixes up the entire state of the generator in a nonreversible |
223 | * way. |
224 | */ |
225 | |
226 | extern void rand_gate(rand_pool */*r*/); |
227 | |
228 | /* --- @rand_stretch@ --- * |
229 | * |
230 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
231 | * |
232 | * Returns: --- |
233 | * |
234 | * Use: Stretches the contents of the output buffer by transforming |
235 | * it in a nonreversible way. This doesn't add any entropy |
236 | * worth speaking about, but it works well enough when the |
237 | * caller doesn't care about that sort of thing. |
238 | */ |
239 | |
240 | extern void rand_stretch(rand_pool */*r*/); |
241 | |
242 | /* --- @rand_get@ --- * |
243 | * |
244 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
245 | * @void *p@ = pointer to output buffer |
246 | * @size_t sz@ = size of output buffer |
247 | * |
248 | * Returns: --- |
249 | * |
250 | * Use: Gets random data from the pool. The pool's contents can't be |
251 | * determined from the output of this function; nor can the |
252 | * output data be determined from a knowledge of the data input |
253 | * to the pool wihtout also having knowledge of the secret key. |
254 | * The good bits counter is decremented, although no special |
255 | * action is taken if it reaches zero. |
256 | */ |
257 | |
258 | extern void rand_get(rand_pool */*r*/, void */*p*/, size_t /*sz*/); |
259 | |
260 | /* --- @rand_getgood@ --- * |
261 | * |
262 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
263 | * @void *p@ = pointer to output buffer |
264 | * @size_t sz@ = size of output buffer |
265 | * |
266 | * Returns: --- |
267 | * |
268 | * Use: Gets random data from the pool. The pool's contents can't be |
269 | * determined from the output of this function; nor can the |
270 | * output data be determined from a knowledge of the data input |
271 | * to the pool wihtout also having knowledge of the secret key. |
272 | * If a noise source is attached to the pool in question, it is |
273 | * called to replenish the supply of good bits in the pool; |
274 | * otherwise this call is equivalent to @rand_get@. |
275 | */ |
276 | |
277 | extern void rand_getgood(rand_pool */*r*/, void */*p*/, size_t /*sz*/); |
278 | |
b3f05084 |
279 | /*----- Generic random number generator interface -------------------------*/ |
280 | |
281 | /* --- Miscellaneous operations --- */ |
282 | |
283 | enum { |
284 | RAND_GATE = GRAND_SPECIFIC, /* No args */ |
285 | RAND_STRETCH, /* No args */ |
286 | RAND_KEY, /* @const void *k, size_t sz@ */ |
287 | RAND_NOISESRC /* @const rand_source *s@ */ |
288 | }; |
289 | |
290 | /* --- Default random number generator --- */ |
291 | |
292 | extern grand rand_global; |
293 | |
294 | /* --- @rand_create@ --- * |
295 | * |
296 | * Arguments: --- |
297 | * |
298 | * Returns: Pointer to a generic generator. |
299 | * |
300 | * Use: Constructs a generic generator interface over a Catacomb |
301 | * entropy pool generator. |
302 | */ |
303 | |
304 | extern grand *rand_create(void); |
305 | |
d03ab969 |
306 | /*----- That's all, folks -------------------------------------------------*/ |
307 | |
308 | #ifdef __cplusplus |
309 | } |
310 | #endif |
311 | |
312 | #endif |