1 /* Generate buffers of random data.
3 Copyright (C) 2006, 2008-2010 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
35 #define _(msgid) gettext (msgid)
37 #include "rand-isaac.h"
38 #include "stdio-safer.h"
39 #include "unlocked-io.h"
43 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
44 # define __attribute__(x) /* empty */
48 #ifndef ATTRIBUTE_NORETURN
49 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
53 # define MIN(a, b) ((a) < (b) ? (a) : (b))
56 #if _STRING_ARCH_unaligned
57 # define ALIGNED_POINTER(ptr, type) true
59 # define alignof(type) offsetof (struct { char c; type x; }, x)
60 # define ALIGNED_POINTER(ptr, type) ((size_t) (ptr) % alignof (type) == 0)
63 /* The maximum buffer size used for reads of random data. Using the
64 value 2 * ISAAC_BYTES makes this the largest power of two that
65 would not otherwise cause struct randread_source to grow. */
66 #define RANDREAD_BUFFER_SIZE (2 * ISAAC_BYTES)
68 /* A source of random data for generating random buffers. */
69 struct randread_source
71 /* Stream to read random bytes from. If null, the current
72 implementation uses an internal PRNG (ISAAC). */
75 /* Function to call, and its argument, if there is an input error or
76 end of file when reading from the stream; errno is nonzero if
77 there was an error. If this function returns, it should fix the
78 problem before returning. The default handler assumes that
79 handler_arg is the file name of the source. */
80 void (*handler
) (void const *);
81 void const *handler_arg
;
83 /* The buffer for SOURCE. It's kept here to simplify storage
84 allocation and to make it easier to clear out buffered random
88 /* The stream buffer, if SOURCE is not null. */
89 char c
[RANDREAD_BUFFER_SIZE
];
91 /* The buffered ISAAC pseudorandom buffer, if SOURCE is null. */
94 /* The number of bytes that are buffered at the end of data.b. */
97 /* State of the ISAAC generator. */
98 struct isaac_state state
;
100 /* Up to a buffer's worth of pseudorandom data. */
103 uint32_t w
[ISAAC_WORDS
];
104 unsigned char b
[ISAAC_BYTES
];
111 /* The default error handler. */
113 static void ATTRIBUTE_NORETURN
114 randread_error (void const *file_name
)
117 error (exit_failure
, errno
,
118 _(errno
== 0 ? "%s: end of file" : "%s: read error"),
119 quotearg_colon (file_name
));
123 /* Simply return a new randread_source object with the default error
126 static struct randread_source
*
127 simple_new (FILE *source
, void const *handler_arg
)
129 struct randread_source
*s
= xmalloc (sizeof *s
);
131 s
->handler
= randread_error
;
132 s
->handler_arg
= handler_arg
;
136 /* Create and initialize a random data source from NAME, or use a
137 reasonable default source if NAME is null. BYTES_BOUND is an upper
138 bound on the number of bytes that will be needed. If zero, it is a
139 hard bound; otherwise it is just an estimate.
141 If NAME is not null, NAME is saved for use as the argument of the
142 default handler. Unless a non-default handler is used, NAME's
143 lifetime should be at least that of the returned value.
145 Return NULL (setting errno) on failure. */
147 struct randread_source
*
148 randread_new (char const *name
, size_t bytes_bound
)
150 if (bytes_bound
== 0)
151 return simple_new (NULL
, NULL
);
155 struct randread_source
*s
;
158 if (! (source
= fopen_safer (name
, "rb")))
161 s
= simple_new (source
, name
);
164 setvbuf (source
, s
->buf
.c
, _IOFBF
, MIN (sizeof s
->buf
.c
, bytes_bound
));
167 s
->buf
.isaac
.buffered
= 0;
168 isaac_seed (&s
->buf
.isaac
.state
);
176 /* Set S's handler and its argument. HANDLER (HANDLER_ARG) is called
177 when there is a read error or end of file from the random data
178 source; errno is nonzero if there was an error. If HANDLER
179 returns, it should fix the problem before returning. The default
180 handler assumes that handler_arg is the file name of the source; it
184 randread_set_handler (struct randread_source
*s
, void (*handler
) (void const *))
186 s
->handler
= handler
;
190 randread_set_handler_arg (struct randread_source
*s
, void const *handler_arg
)
192 s
->handler_arg
= handler_arg
;
196 /* Place SIZE random bytes into the buffer beginning at P, using
200 readsource (struct randread_source
*s
, unsigned char *p
, size_t size
)
204 size_t inbytes
= fread (p
, sizeof *p
, size
, s
->source
);
205 int fread_errno
= errno
;
210 errno
= (ferror (s
->source
) ? fread_errno
: 0);
211 s
->handler (s
->handler_arg
);
216 /* Place SIZE pseudorandom bytes into the buffer beginning at P, using
217 the buffered ISAAC generator in ISAAC. */
220 readisaac (struct isaac
*isaac
, unsigned char *p
, size_t size
)
222 size_t inbytes
= isaac
->buffered
;
228 memcpy (p
, isaac
->data
.b
+ ISAAC_BYTES
- inbytes
, size
);
229 isaac
->buffered
= inbytes
- size
;
233 memcpy (p
, isaac
->data
.b
+ ISAAC_BYTES
- inbytes
, inbytes
);
237 /* If P is aligned, write to *P directly to avoid the overhead
238 of copying from the buffer. */
239 if (ALIGNED_POINTER (p
, uint32_t))
241 uint32_t *wp
= (uint32_t *) p
;
242 while (ISAAC_BYTES
<= size
)
244 isaac_refill (&isaac
->state
, wp
);
253 p
= (unsigned char *) wp
;
256 isaac_refill (&isaac
->state
, isaac
->data
.w
);
257 inbytes
= ISAAC_BYTES
;
262 /* Consume random data from *S to generate a random buffer BUF of size
266 randread (struct randread_source
*s
, void *buf
, size_t size
)
269 readsource (s
, buf
, size
);
271 readisaac (&s
->buf
.isaac
, buf
, size
);
275 /* Clear *S so that it no longer contains undelivered random data, and
276 deallocate any system resources associated with *S. Return 0 if
277 successful, a negative number (setting errno) if not (this is rare,
278 but can occur in theory if there is an input error). */
281 randread_free (struct randread_source
*s
)
283 FILE *source
= s
->source
;
284 memset (s
, 0, sizeof *s
);
286 return (source
? fclose (source
) : 0);