From dc35e9219fa6088544a9e0fe13b3baf01ae24d1c Mon Sep 17 00:00:00 2001 From: millert <> Date: Mon, 3 Apr 2000 23:23:48 +0000 Subject: Add srandomdev() from FreeBSD for use by sendmail and others. --- src/lib/libc/stdlib/random.c | 47 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/lib/libc/stdlib/random.c') 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 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: random.c,v 1.6 1998/02/07 02:16:25 millert Exp $"; +static char *rcsid = "$OpenBSD: random.c,v 1.7 2000/04/03 23:23:48 millert Exp $"; #endif /* LIBC_SCCS and not lint */ +#include +#include +#include #include #include +#include /* * random.c: @@ -219,6 +223,47 @@ 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 arandom(4) 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. + */ +void +srandomdev() +{ + int fd; + size_t len; + + if (rand_type == TYPE_0) + len = sizeof(state[0]); + else + len = rand_deg * sizeof(state[0]); + + if ((fd = open("/dev/arandom", O_RDONLY, 0)) != -1 && + read(fd, (void *) state, len) == (ssize_t) len) { + close(fd); + } else { + struct timeval tv; + u_int junk; + + /* XXX - this could be better */ + gettimeofday(&tv, NULL); + srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk); + return; + } + + if (rand_type != TYPE_0) { + fptr = &state[rand_sep]; + rptr = &state[0]; + } +} + /* * initstate: * -- cgit v1.2.3-55-g6feb