Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Nov 1998 08:05:01 +1100
From:      Peter Jeremy <peter.jeremy@auss2.alcatel.com.au>
To:        joelh@gnu.org
Cc:        hackers@FreeBSD.ORG
Subject:   /dev/random usage
Message-ID:  <98Nov24.080435est.40327@border.alcanet.com.au>

next in thread | raw e-mail | index | archive | help
Joel Ray Holveck <joelh@gnu.org> wrote:
>Speaking of such things, what are some apps that use /dev/random, or
>at least have hooks to use a random device?

Going thru the 3.0-RELEASE source tree, the following programs all use
srandomdev(3) - which gives you a pseudo-random sequence starting from
a random point within the sequence: games/adventure, games/arithmetic,
games/atc, games/backgammon, games/battlestar, games/bs,
games/canfield, games/cribbage, games/fish, games/fortune, games/hack,
games/hangman, games/larn, games/mille, games/phantasia, games/quiz,
games/rain, games/random, games/robots, games/rogue, games/sail,
games/snake, games/trek, games/worm, games/wump, sbin/fsirand,
sbin/newfs, usr.bin/jot, usr.bin/passwd, usr.sbin/ppp, usr.sbin/pw

Also arc4random(3) uses /dev/urandom for seeding it - and arc4random(3)
is used by mktemp(3).

Modifying an application to use /dev/random is not that difficult:
1) If pseudo-random numbers starting from a fairly random point within
   the sequence are adequate, and the application already uses random(3),
   just add a call to srandomdev(3) in place of srandom(3).

2) For a slightly better random starting point, it would be possible to
   rewrite srandomdev(3) to use /dev/random, waiting until sufficient
   key information became available.  Having read the comments in the
   random(4) code, this is unlikely to provide any benefit over 1) above.

3) For truely random numbers, roll your own random(3) (or rand(3) or
   drand48(3) etc - the main differences are the type and range of the
   return value) implementation that uses /dev/random:

long random()
{
    static int rand_fd = -1;
    int wanted;
    long rnd;

    if (rand_fd < 0 &&
        (rand_fd = open("/dev/random", O_RDONLY)) < 0)
    {
        perror("Failed to open /dev/random");
        abort();
    }

    wanted = sizeof(rnd);
    do {
        int len;
        len = read(rand_fd, ((char *)&rnd) + sizeof(rnd) - wanted, wanted);
	if (len < 0) {
	    perror("read /dev/random failed");
	    abort();
        }
	wanted -= len;
	if (wanted > 0)
	    sleep(1);		/* adjust as appropriate */
	else
	    return (rnd & 0x7fffffffL);
    } while (1);
}

Peter
--
Peter Jeremy (VK2PJ)                    peter.jeremy@alcatel.com.au
Alcatel Australia Limited
41 Mandible St                          Phone: +61 2 9690 5019
ALEXANDRIA  NSW  2015                   Fax:   +61 2 9690 5247

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?98Nov24.080435est.40327>