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