diff options
| author | Andrey A. Chernov <ache@FreeBSD.org> | 1997-03-23 22:40:20 +0000 |
|---|---|---|
| committer | Andrey A. Chernov <ache@FreeBSD.org> | 1997-03-23 22:40:20 +0000 |
| commit | 301cf5d3e41f0a6795709da16ed53c5a2eda9fb9 (patch) | |
| tree | ba7ac761d6d1a87af553d22c77fc8863efa638e9 /lib/libc/stdlib/random.c | |
| parent | 3e9c55a0635c4b19e55fcfd12f4d8b24c3bd1d32 (diff) | |
Notes
Diffstat (limited to 'lib/libc/stdlib/random.c')
| -rw-r--r-- | lib/libc/stdlib/random.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c index 9f5bd2a7400f..a3698a557774 100644 --- a/lib/libc/stdlib/random.c +++ b/lib/libc/stdlib/random.c @@ -43,8 +43,10 @@ static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95"; #define setstate osetstate #endif +#include <fcntl.h> /* for srandomdev() */ #include <stdio.h> #include <stdlib.h> +#include <unistd.h> /* for srandomdev() */ /* * random.c: @@ -278,6 +280,44 @@ srandom(x) } /* + * srandomdev: + * + * Many programs choose the seed value in a totally predictable manner. + * This often causes problems. We seed the generator using the much more + * secure `/dev/random' interface. Note that this particular seeding + * procedure can generate states which are impossible to reproduce by + * calling srandom() with any value, since the succeeding terms in the + * state buffer are no longer derived from the LC algorithm applied to + * a fixed seed. + */ +int +srandomdev() +{ + int fd; + size_t len; + + if (rand_type == TYPE_0) + len = sizeof state[0]; + else + len = rand_deg * sizeof state[0]; + + fd = open("/dev/urandom", O_RDONLY, 0); + if (fd < 0) + return -1; + if (read(fd, (void *) state, len) < (ssize_t) len) { + close(fd); + return -1; + } + close(fd); + + if (rand_type != TYPE_0) { + fptr = &state[rand_sep]; + rptr = &state[0]; + } + return 0; +} + +/* * initstate: * * Initialize the state information in the given array of n bytes for future |
