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