From owner-freebsd-hackers Sun Oct 15 12:19:48 1995 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.6.12/8.6.6) id MAA05998 for hackers-outgoing; Sun, 15 Oct 1995 12:19:48 -0700 Received: from grunt.grondar.za (grunt.grondar.za [196.7.18.129]) by freefall.freebsd.org (8.6.12/8.6.6) with ESMTP id MAA05993 for ; Sun, 15 Oct 1995 12:19:21 -0700 Received: from grumble.grondar.za (grumble.grondar.za [196.7.18.130]) by grunt.grondar.za (8.6.12/8.6.9) with ESMTP id VAA22709 for ; Sun, 15 Oct 1995 21:19:04 +0200 Received: from localhost (localhost [127.0.0.1]) by grumble.grondar.za (8.6.12/8.6.9) with SMTP id VAA23932 for ; Sun, 15 Oct 1995 21:19:03 +0200 Message-Id: <199510151919.VAA23932@grumble.grondar.za> X-Authentication-Warning: grumble.grondar.za: Host localhost didn't use HELO protocol To: hackers@freebsd.org Subject: Creating a /dev/random Date: Sun, 15 Oct 1995 21:19:03 +0200 From: Mark Murray Sender: owner-hackers@freebsd.org Precedence: bulk Hi I am slowly hacking my way through the kernel, figuring out how to do things. So far, I am doing OK, but with lots of help. Thanks guys! I am building devices, /dev/random and /dev/urandom that when read give random noise generated in and by the kernel. So far it is going pretty well, but I have some unexplained hangs. I believe these are due to my complete lack of knowledge of what the uiomove() function is supposed to do. Please have a look at my mods to mem.c (the home of /dev/null) and make some suggestions: Notes: 1) read_random() only returns the number of random bytes it thinks are truly random, and should cause /dev/random to return EOF when this "pool of entropy" is empty. 2) it is this /dev/random that dies. If I continually open/read/close the kernel will wedge. No reboot, syscons is alive, but everything else stops. 3) read_random_unlimited always returns the number of bytes requested, even if they are dubious. This is central to /dev/urandom, which I tried to model on /dev/zero (except for the return value :-]) 4) /dev/urandom never seems to kill the system. 5) the kernel is 'hooked' to provide a contunual source of randomness. (interrupts, keystrokes etc) diff --exclude=CVS -udr sys.ORG/i386/i386/mem.c sys/i386/i386/mem.c --- sys.ORG/i386/i386/mem.c Wed Sep 20 15:01:17 1995 +++ sys/i386/i386/mem.c Mon Oct 2 22:59:44 1995 @@ -135,6 +139,10 @@ register struct iovec *iov; int error = 0; caddr_t zbuf = NULL; +#ifdef DEVRANDOM + caddr_t rbuf = NULL; + int poolsize; +#endif while (uio->uio_resid > 0 && error == 0) { iov = uio->uio_iov; @@ -190,6 +198,40 @@ return (0); c = iov->iov_len; break; + +#ifdef DEVRANDOM +/* minor device 3 is filth generator /dev/random - rathole on write */ + case 3: + if (uio->uio_rw == UIO_WRITE) { + c = iov->iov_len; + break; + } + if (rbuf == NULL) { + rbuf = (caddr_t) + malloc(CLBYTES, M_TEMP, M_WAITOK); + } + poolsize = read_random(rbuf, CLBYTES); + c = min(iov->iov_len, CLBYTES); + c = min(c, poolsize); + error = uiomove(rbuf, (int)c, uio); + continue; + +/* minor device 4 is dirt generator /dev/urandom - rathole on write */ + case 4: + if (uio->uio_rw == UIO_WRITE) { + c = iov->iov_len; + break; + } + if (rbuf == NULL) { + rbuf = (caddr_t) + malloc(CLBYTES, M_TEMP, M_WAITOK); + } + poolsize = read_random_unlimited(rbuf, CLBYTES); + c = min(iov->iov_len, CLBYTES); + c = min(c, poolsize); + error = uiomove(rbuf, (int)c, uio); + continue; +#endif /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */ case 12: -- Mark Murray 46 Harvey Rd, Claremont, Cape Town 7700, South Africa +27 21 61-3768 GMT+0200 Finger mark@grumble.grondar.za for PGP key