diff options
author | ache <ache@FreeBSD.org> | 2001-04-23 02:29:10 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 2001-04-23 02:29:10 +0000 |
commit | fb76083ae2c2c518374309576a3a0fe4f9d1697c (patch) | |
tree | ddd3457b20766f3f41f7e95a9e7ff98af932912a /lib/libc/stdlib/rand.c | |
parent | 350bc3b6117560aae429d3ffd1085e99f9877ef9 (diff) | |
download | FreeBSD-src-fb76083ae2c2c518374309576a3a0fe4f9d1697c.zip FreeBSD-src-fb76083ae2c2c518374309576a3a0fe4f9d1697c.tar.gz |
Add sranddev() since srand() is not vary much with seed, typical time
Diffstat (limited to 'lib/libc/stdlib/rand.c')
-rw-r--r-- | lib/libc/stdlib/rand.c | 34 |
1 files changed, 34 insertions, 0 deletions
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 <sys/time.h> /* for sranddev() */ #include <sys/types.h> +#include <fcntl.h> /* for sranddev() */ #include <stdlib.h> +#include <unistd.h> /* for sranddev() */ #ifdef TEST #include <stdio.h> @@ -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() |