summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/random.c
diff options
context:
space:
mode:
authormillert <>2000-04-03 23:23:48 +0000
committermillert <>2000-04-03 23:23:48 +0000
commitdc35e9219fa6088544a9e0fe13b3baf01ae24d1c (patch)
tree9db8e9f80ab7396b931d742b3f82d4f1fa718c93 /src/lib/libc/stdlib/random.c
parentb741f7cef95c2485e6eaf92dc465eb8dee2d8c51 (diff)
downloadopenbsd-dc35e9219fa6088544a9e0fe13b3baf01ae24d1c.tar.gz
openbsd-dc35e9219fa6088544a9e0fe13b3baf01ae24d1c.tar.bz2
openbsd-dc35e9219fa6088544a9e0fe13b3baf01ae24d1c.zip
Add srandomdev() from FreeBSD for use by sendmail and others.
Diffstat (limited to 'src/lib/libc/stdlib/random.c')
-rw-r--r--src/lib/libc/stdlib/random.c47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c
index 79344f30f1..7c6e5f7eb9 100644
--- a/src/lib/libc/stdlib/random.c
+++ b/src/lib/libc/stdlib/random.c
@@ -32,11 +32,15 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: random.c,v 1.6 1998/02/07 02:16:25 millert Exp $"; 35static char *rcsid = "$OpenBSD: random.c,v 1.7 2000/04/03 23:23:48 millert Exp $";
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include <sys/types.h>
39#include <sys/time.h>
40#include <fcntl.h>
38#include <stdio.h> 41#include <stdio.h>
39#include <stdlib.h> 42#include <stdlib.h>
43#include <unistd.h>
40 44
41/* 45/*
42 * random.c: 46 * random.c:
@@ -220,6 +224,47 @@ srandom(x)
220} 224}
221 225
222/* 226/*
227 * srandomdev:
228 *
229 * Many programs choose the seed value in a totally predictable manner.
230 * This often causes problems. We seed the generator using the much more
231 * secure arandom(4) interface. Note that this particular seeding
232 * procedure can generate states which are impossible to reproduce by
233 * calling srandom() with any value, since the succeeding terms in the
234 * state buffer are no longer derived from the LC algorithm applied to
235 * a fixed seed.
236 */
237void
238srandomdev()
239{
240 int fd;
241 size_t len;
242
243 if (rand_type == TYPE_0)
244 len = sizeof(state[0]);
245 else
246 len = rand_deg * sizeof(state[0]);
247
248 if ((fd = open("/dev/arandom", O_RDONLY, 0)) != -1 &&
249 read(fd, (void *) state, len) == (ssize_t) len) {
250 close(fd);
251 } else {
252 struct timeval tv;
253 u_int junk;
254
255 /* XXX - this could be better */
256 gettimeofday(&tv, NULL);
257 srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk);
258 return;
259 }
260
261 if (rand_type != TYPE_0) {
262 fptr = &state[rand_sep];
263 rptr = &state[0];
264 }
265}
266
267/*
223 * initstate: 268 * initstate:
224 * 269 *
225 * Initialize the state information in the given array of n bytes for future 270 * Initialize the state information in the given array of n bytes for future