From fb76083ae2c2c518374309576a3a0fe4f9d1697c Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 23 Apr 2001 02:29:10 +0000 Subject: Add sranddev() since srand() is not vary much with seed, typical time --- lib/libc/stdlib/rand.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lib/libc/stdlib/rand.c') diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index 70285bd..8de8ead 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -39,8 +39,11 @@ static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93"; #endif /* LIBC_SCCS and not lint */ +#include /* for sranddev() */ #include +#include /* for sranddev() */ #include +#include /* for sranddev() */ #ifdef TEST #include @@ -101,6 +104,37 @@ u_int seed; next = seed; } + +/* + * sranddev: + * + * Many programs choose the seed value in a totally predictable manner. + * This often causes problems. We seed the generator using the much more + * secure urandom(4) interface. + */ +void +sranddev() +{ + int fd, done; + + done = 0; + fd = _open("/dev/urandom", O_RDONLY, 0); + if (fd >= 0) { + if (_read(fd, (void *) &next, sizeof(next)) == sizeof(next)) + done = 1; + _close(fd); + } + + if (!done) { + struct timeval tv; + unsigned long junk; + + gettimeofday(&tv, NULL); + next = getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk; + } +} + + #ifdef TEST main() -- cgit v1.1