Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Oct 1995 21:19:03 +0200
From:      Mark Murray <mark@grondar.za>
To:        hackers@freebsd.org
Subject:   Creating a /dev/random
Message-ID:  <199510151919.VAA23932@grumble.grondar.za>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199510151919.VAA23932>