From owner-freebsd-hackers Mon Nov 23 13:05:16 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id NAA16896 for freebsd-hackers-outgoing; Mon, 23 Nov 1998 13:05:16 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from alcanet.com.au (border.alcanet.com.au [203.62.196.10]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id NAA16890 for ; Mon, 23 Nov 1998 13:05:14 -0800 (PST) (envelope-from peter.jeremy@auss2.alcatel.com.au) Received: by border.alcanet.com.au id <40327>; Tue, 24 Nov 1998 08:04:35 +1100 Date: Tue, 24 Nov 1998 08:05:01 +1100 From: Peter Jeremy Subject: /dev/random usage To: joelh@gnu.org Cc: hackers@FreeBSD.ORG Message-Id: <98Nov24.080435est.40327@border.alcanet.com.au> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Joel Ray Holveck 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