Skip to content

Commit

Permalink
In srandomdev(), if we can't access /dev/arandom, use the sysctl() in…
Browse files Browse the repository at this point in the history
…stead.

We don't want to use the sysctl() by default since we are reading more
than just a few bytes of entropy when setting up the state.
  • Loading branch information
millert committed Dec 6, 2002
1 parent e4bb27c commit 99d815f
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/libc/stdlib/random.c
Expand Up @@ -32,10 +32,11 @@
*/

#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: random.c,v 1.9 2000/04/04 14:27:00 millert Exp $";
static char *rcsid = "$OpenBSD: random.c,v 1.10 2002/12/06 17:43:34 millert Exp $";
#endif /* LIBC_SCCS and not lint */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>
Expand Down Expand Up @@ -237,27 +238,43 @@ srandom(x)
void
srandomdev()
{
int fd;
int fd, i, mib[2], n;
size_t len;

if (rand_type == TYPE_0)
len = sizeof(state[0]);
else
len = rand_deg * sizeof(state[0]);

/*
* To get seed data, first try reading from /dev/arandom.
* If that fails, try the KERN_ARND sysctl() (one int at a time).
* As a last resort, call srandom().
*/
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);
if (fd != -1)
close(fd);
return;
mib[0] = CTL_KERN;
mib[1] = KERN_ARND;
n = len / sizeof(int);
len = sizeof(int);
for (i = 0; i < n; i++) {
if (sysctl(mib, 2, (char *)((int *)state + i), &len,
NULL, 0) == -1)
break;
}
if (i != n) {
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) {
Expand Down

0 comments on commit 99d815f

Please sign in to comment.