From b9d8a7cc98831a4396c35c4044a140f14799eeff Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 23 Mar 1997 22:40:20 +0000 Subject: Add srandomdev() function (use "/dev/urandom" now) Submitted by: wollman & me (add type casts and remove unneded loop) --- lib/libc/stdlib/random.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'lib/libc/stdlib/random.c') diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c index 9f5bd2a..a3698a5 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 /* for srandomdev() */ #include #include +#include /* 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 -- cgit v1.1