From c12b74dad5391f65e0a69a8fe30f768a523f6d99 Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Wed, 3 Aug 2005 15:22:41 +0000 Subject: [PATCH] Random is now a separate driver. --- commands/scripts/MAKEDEV.sh | 4 +- drivers/Makefile | 1 + drivers/memory/Makefile | 4 +- drivers/memory/memory.c | 74 +---- drivers/random/Makefile | 53 ++++ drivers/{memory => random}/aes/boxes.dat | 0 drivers/{memory => random}/aes/rijndael-alg.h | 0 drivers/{memory => random}/aes/rijndael-api.h | 0 drivers/{memory => random}/aes/rijndael.h | 0 drivers/{memory => random}/aes/rijndael_alg.c | 0 drivers/{memory => random}/aes/rijndael_api.c | 0 drivers/{memory => random}/aes/word_i386.h | 0 drivers/random/main.c | 260 ++++++++++++++++++ drivers/{memory => random}/random.c | 0 drivers/{memory => random}/random.h | 0 drivers/{memory => random}/sha2.c | 0 drivers/{memory => random}/sha2.h | 0 etc/usr/rc | 4 + include/minix/com.h | 2 - servers/fs/const.h | 2 +- servers/fs/dmap.c | 1 + 21 files changed, 325 insertions(+), 80 deletions(-) create mode 100644 drivers/random/Makefile rename drivers/{memory => random}/aes/boxes.dat (100%) rename drivers/{memory => random}/aes/rijndael-alg.h (100%) rename drivers/{memory => random}/aes/rijndael-api.h (100%) rename drivers/{memory => random}/aes/rijndael.h (100%) rename drivers/{memory => random}/aes/rijndael_alg.c (100%) rename drivers/{memory => random}/aes/rijndael_api.c (100%) rename drivers/{memory => random}/aes/word_i386.h (100%) create mode 100644 drivers/random/main.c rename drivers/{memory => random}/random.c (100%) rename drivers/{memory => random}/random.h (100%) rename drivers/{memory => random}/sha2.c (100%) rename drivers/{memory => random}/sha2.h (100%) diff --git a/commands/scripts/MAKEDEV.sh b/commands/scripts/MAKEDEV.sh index cb62494d3..893bcda30 100755 --- a/commands/scripts/MAKEDEV.sh +++ b/commands/scripts/MAKEDEV.sh @@ -59,8 +59,8 @@ do $e mknod kmem c 1 2; $e chmod 640 kmem $e mknod null c 1 3; $e chmod 666 null $e mknod boot b 1 4; $e chmod 600 ram - $e mknod random c 1 5; $e chmod 644 random - $e mknod urandom c 1 5; $e chmod 644 urandom + $e mknod random c 16 0; $e chmod 644 random + $e mknod urandom c 16 0; $e chmod 644 urandom $e mknod zero c 1 6; $e chmod 644 zero $e chgrp kmem ram mem kmem null boot random urandom zero ;; diff --git a/drivers/Makefile b/drivers/Makefile index f3f0d7c98..d59007568 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -26,3 +26,4 @@ all install depend clean: cd ./dpeth && $(MAKE) $@ cd ./log && $(MAKE) $@ cd ./bios_wini && $(MAKE) $@ + cd ./random && $(MAKE) $@ diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile index c9576a127..e401ccc7b 100644 --- a/drivers/memory/Makefile +++ b/drivers/memory/Makefile @@ -16,7 +16,7 @@ CFLAGS = -I$i LDFLAGS = -i LIBS = -lsys -lsysutil -OBJ = memory.o random.o sha2.o aes/rijndael_api.o aes/rijndael_alg.o +OBJ = memory.o LIBDRIVER = $d/libdriver/driver.o @@ -46,7 +46,7 @@ clean: depend: - /usr/bin/mkdep "$(CC) -E $(CPPFLAGS)" *.c ../libdriver/*.c aes/*.c > .depend + /usr/bin/mkdep "$(CC) -E $(CPPFLAGS)" *.c ../libdriver/*.c > .depend # Include generated dependencies. include .depend diff --git a/drivers/memory/memory.c b/drivers/memory/memory.c index 60f89633c..553f1a2f1 100644 --- a/drivers/memory/memory.c +++ b/drivers/memory/memory.c @@ -5,7 +5,6 @@ * /dev/kmem - kernel virtual memory * /dev/null - null device (data sink) * /dev/boot - boot device loaded from boot image - * /dev/random - random number generator * /dev/zero - null byte stream generator * * Changes: @@ -24,17 +23,14 @@ #include "../../kernel/type.h" #include "assert.h" -#include "random.h" #define NR_DEVS 7 /* number of minor devices */ -#define KRANDOM_PERIOD 1 /* ticks between krandom calls */ PRIVATE struct device m_geom[NR_DEVS]; /* base and size of each device */ PRIVATE int m_seg[NR_DEVS]; /* segment index of each device */ PRIVATE int m_device; /* current device */ PRIVATE struct kinfo kinfo; /* kernel information */ PRIVATE struct machine machine; /* machine information */ -PRIVATE struct randomness krandom; /* randomness from the kernel */ extern int errno; /* error number for PM calls */ @@ -46,7 +42,6 @@ FORWARD _PROTOTYPE( int m_do_open, (struct driver *dp, message *m_ptr) ); FORWARD _PROTOTYPE( void m_init, (void) ); FORWARD _PROTOTYPE( int m_ioctl, (struct driver *dp, message *m_ptr) ); FORWARD _PROTOTYPE( void m_geometry, (struct partition *entry) ); -FORWARD _PROTOTYPE( void m_random, (struct driver *dp, message *m_ptr) ); /* Entry points to this driver. */ PRIVATE struct driver m_dtab = { @@ -59,7 +54,7 @@ PRIVATE struct driver m_dtab = { nop_cleanup, /* no need to clean up */ m_geometry, /* memory device "geometry" */ nop_signal, /* system signals */ - m_random, /* get randomness from kernel (alarm) */ + nop_alarm, nop_cancel, nop_select, NULL @@ -69,11 +64,6 @@ PRIVATE struct driver m_dtab = { #define ZERO_BUF_SIZE 1024 PRIVATE char dev_zero[ZERO_BUF_SIZE]; -/* Buffer for the /dev/random number generator. */ -#define RANDOM_BUF_SIZE 1024 -PRIVATE char random_buf[RANDOM_BUF_SIZE]; - - #define click_to_round_k(n) \ ((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024)) @@ -180,27 +170,6 @@ unsigned nr_req; /* length of request vector */ } break; - /* Random number generator. Character instead of block device. */ - case RANDOM_DEV: - if (opcode == DEV_GATHER && !random_isseeded()) - return(EAGAIN); - left = count; - while (left > 0) { - chunk = (left > RANDOM_BUF_SIZE) ? RANDOM_BUF_SIZE : left; - if (opcode == DEV_GATHER) { - random_getbytes(random_buf, chunk); - sys_vircopy(SELF, D, (vir_bytes) random_buf, - proc_nr, D, user_vir, chunk); - } else if (opcode == DEV_SCATTER) { - sys_vircopy(proc_nr, D, user_vir, - SELF, D, (vir_bytes) random_buf, chunk); - random_putbytes(random_buf, chunk); - } - user_vir += chunk; - left -= chunk; - } - break; - /* Null byte stream generator. */ case ZERO_DEV: if (opcode == DEV_GATHER) { @@ -283,9 +252,6 @@ PRIVATE void m_init() dev_zero[i] = '\0'; } - random_init(); - m_random(NULL, NULL); /* also set periodic timer */ - /* Set up memory ranges for /dev/mem. */ #if (CHIP == INTEL) if (OK != (s=sys_getmachine(&machine))) { @@ -359,44 +325,6 @@ message *m_ptr; /* pointer to control message */ } -/*============================================================================* - * m_random * - *============================================================================*/ -PRIVATE void m_random(dp, m_ptr) -struct driver *dp; /* pointer to driver structure */ -message *m_ptr; /* pointer to alarm message */ -{ - /* Fetch random information from the kernel to update /dev/random. */ - int i, s, r_next, r_size, r_high; - struct randomness krandom; - - if (OK != (s=sys_getrandomness(&krandom))) - report("MEM", "sys_getrandomness failed", s); - - for (i= 0; i .depend + +# Include generated dependencies. +include .depend + diff --git a/drivers/memory/aes/boxes.dat b/drivers/random/aes/boxes.dat similarity index 100% rename from drivers/memory/aes/boxes.dat rename to drivers/random/aes/boxes.dat diff --git a/drivers/memory/aes/rijndael-alg.h b/drivers/random/aes/rijndael-alg.h similarity index 100% rename from drivers/memory/aes/rijndael-alg.h rename to drivers/random/aes/rijndael-alg.h diff --git a/drivers/memory/aes/rijndael-api.h b/drivers/random/aes/rijndael-api.h similarity index 100% rename from drivers/memory/aes/rijndael-api.h rename to drivers/random/aes/rijndael-api.h diff --git a/drivers/memory/aes/rijndael.h b/drivers/random/aes/rijndael.h similarity index 100% rename from drivers/memory/aes/rijndael.h rename to drivers/random/aes/rijndael.h diff --git a/drivers/memory/aes/rijndael_alg.c b/drivers/random/aes/rijndael_alg.c similarity index 100% rename from drivers/memory/aes/rijndael_alg.c rename to drivers/random/aes/rijndael_alg.c diff --git a/drivers/memory/aes/rijndael_api.c b/drivers/random/aes/rijndael_api.c similarity index 100% rename from drivers/memory/aes/rijndael_api.c rename to drivers/random/aes/rijndael_api.c diff --git a/drivers/memory/aes/word_i386.h b/drivers/random/aes/word_i386.h similarity index 100% rename from drivers/memory/aes/word_i386.h rename to drivers/random/aes/word_i386.h diff --git a/drivers/random/main.c b/drivers/random/main.c new file mode 100644 index 000000000..5714b0751 --- /dev/null +++ b/drivers/random/main.c @@ -0,0 +1,260 @@ +/* This file contains the device dependent part of the drivers for the + * following special files: + * /dev/random - random number generator + */ + +#include "../drivers.h" +#include "../libdriver/driver.h" +#include +#include "../../kernel/const.h" +#include "../../kernel/config.h" +#include "../../kernel/type.h" + +#include "assert.h" +#include "random.h" + +#define NR_DEVS 1 /* number of minor devices */ +# define RANDOM_DEV 0 /* minor device for /dev/random */ + +#define KRANDOM_PERIOD 1 /* ticks between krandom calls */ + +PRIVATE struct device m_geom[NR_DEVS]; /* base and size of each device */ +PRIVATE int m_device; /* current device */ +PRIVATE struct randomness krandom; /* randomness from the kernel */ + +extern int errno; /* error number for PM calls */ + +FORWARD _PROTOTYPE( char *r_name, (void) ); +FORWARD _PROTOTYPE( struct device *r_prepare, (int device) ); +FORWARD _PROTOTYPE( int r_transfer, (int proc_nr, int opcode, off_t position, + iovec_t *iov, unsigned nr_req) ); +FORWARD _PROTOTYPE( int r_do_open, (struct driver *dp, message *m_ptr) ); +FORWARD _PROTOTYPE( void r_init, (void) ); +FORWARD _PROTOTYPE( int r_ioctl, (struct driver *dp, message *m_ptr) ); +FORWARD _PROTOTYPE( void r_geometry, (struct partition *entry) ); +FORWARD _PROTOTYPE( void r_random, (struct driver *dp, message *m_ptr) ); + +/* Entry points to this driver. */ +PRIVATE struct driver r_dtab = { + r_name, /* current device's name */ + r_do_open, /* open or mount */ + do_nop, /* nothing on a close */ + r_ioctl, /* specify ram disk geometry */ + r_prepare, /* prepare for I/O on a given minor device */ + r_transfer, /* do the I/O */ + nop_cleanup, /* no need to clean up */ + r_geometry, /* device "geometry" */ + nop_signal, /* system signals */ + r_random, /* get randomness from kernel (alarm) */ + nop_cancel, + nop_select, + NULL +}; + +/* Buffer for the /dev/random number generator. */ +#define RANDOM_BUF_SIZE 1024 +PRIVATE char random_buf[RANDOM_BUF_SIZE]; + + +/*===========================================================================* + * main * + *===========================================================================*/ +PUBLIC void main(void) +{ + r_init(); /* initialize the memory driver */ + driver_task(&r_dtab); /* start driver's main loop */ +} + + +/*===========================================================================* + * r_name * + *===========================================================================*/ +PRIVATE char *r_name() +{ +/* Return a name for the current device. */ + static char name[] = "random"; + return name; +} + + +/*===========================================================================* + * r_prepare * + *===========================================================================*/ +PRIVATE struct device *r_prepare(device) +int device; +{ +/* Prepare for I/O on a device: check if the minor device number is ok. */ + + if (device < 0 || device >= NR_DEVS) return(NIL_DEV); + m_device = device; + + return(&m_geom[device]); +} + + +/*===========================================================================* + * r_transfer * + *===========================================================================*/ +PRIVATE int r_transfer(proc_nr, opcode, position, iov, nr_req) +int proc_nr; /* process doing the request */ +int opcode; /* DEV_GATHER or DEV_SCATTER */ +off_t position; /* offset on device to read or write */ +iovec_t *iov; /* pointer to read or write request vector */ +unsigned nr_req; /* length of request vector */ +{ +/* Read or write one the driver's minor devices. */ + phys_bytes mem_phys, user_phys; + int seg; + unsigned count, left, chunk; + vir_bytes user_vir; + struct device *dv; + unsigned long dv_size; + int s; + + /* Get minor device number and check for /dev/null. */ + dv = &m_geom[m_device]; + dv_size = cv64ul(dv->dv_size); + + while (nr_req > 0) { + + /* How much to transfer and where to / from. */ + count = iov->iov_size; + user_vir = iov->iov_addr; + + switch (m_device) { + + /* Random number generator. Character instead of block device. */ + case RANDOM_DEV: + if (opcode == DEV_GATHER && !random_isseeded()) + return(EAGAIN); + left = count; + while (left > 0) { + chunk = (left > RANDOM_BUF_SIZE) ? RANDOM_BUF_SIZE : left; + if (opcode == DEV_GATHER) { + random_getbytes(random_buf, chunk); + sys_vircopy(SELF, D, (vir_bytes) random_buf, + proc_nr, D, user_vir, chunk); + } else if (opcode == DEV_SCATTER) { + sys_vircopy(proc_nr, D, user_vir, + SELF, D, (vir_bytes) random_buf, chunk); + random_putbytes(random_buf, chunk); + } + user_vir += chunk; + left -= chunk; + } + break; + + + /* Unknown (illegal) minor device. */ + default: + return(EINVAL); + } + + /* Book the number of bytes transferred. */ + position += count; + iov->iov_addr += count; + if ((iov->iov_size -= count) == 0) { iov++; nr_req--; } + + } + return(OK); +} + + +/*============================================================================* + * r_do_open * + *============================================================================*/ +PRIVATE int r_do_open(dp, m_ptr) +struct driver *dp; +message *m_ptr; +{ +/* Check device number on open. + */ + if (r_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO); + + return(OK); +} + + +/*===========================================================================* + * r_init * + *===========================================================================*/ +PRIVATE void r_init() +{ + /* Initialize this task. All minor devices are initialized one by one. */ + int i, s; + + random_init(); + r_random(NULL, NULL); /* also set periodic timer */ +} + + +/*===========================================================================* + * r_ioctl * + *===========================================================================*/ +PRIVATE int r_ioctl(dp, m_ptr) +struct driver *dp; /* pointer to driver structure */ +message *m_ptr; /* pointer to control message */ +{ + struct device *dv; + if ((dv = r_prepare(m_ptr->DEVICE)) == NIL_DEV) return(ENXIO); + + switch (m_ptr->REQUEST) { + + default: + return(do_diocntl(&r_dtab, m_ptr)); + } + return(OK); +} + + +/*============================================================================* + * r_random * + *============================================================================*/ +PRIVATE void r_random(dp, m_ptr) +struct driver *dp; /* pointer to driver structure */ +message *m_ptr; /* pointer to alarm message */ +{ + /* Fetch random information from the kernel to update /dev/random. */ + int i, s, r_next, r_size, r_high; + struct randomness krandom; + + if (OK != (s=sys_getrandomness(&krandom))) + report("RANDOM", "sys_getrandomness failed", s); + + for (i= 0; icylinders = div64u(m_geom[m_device].dv_size, SECTOR_SIZE) / (64 * 32); + entry->heads = 64; + entry->sectors = 32; +} + + diff --git a/drivers/memory/random.c b/drivers/random/random.c similarity index 100% rename from drivers/memory/random.c rename to drivers/random/random.c diff --git a/drivers/memory/random.h b/drivers/random/random.h similarity index 100% rename from drivers/memory/random.h rename to drivers/random/random.h diff --git a/drivers/memory/sha2.c b/drivers/random/sha2.c similarity index 100% rename from drivers/memory/sha2.c rename to drivers/random/sha2.c diff --git a/drivers/memory/sha2.h b/drivers/random/sha2.h similarity index 100% rename from drivers/memory/sha2.h rename to drivers/random/sha2.h diff --git a/etc/usr/rc b/etc/usr/rc index be366762c..db863e99c 100644 --- a/etc/usr/rc +++ b/etc/usr/rc @@ -62,6 +62,10 @@ start) # Cleanup. rm -rf /tmp/. /usr/run/. /usr/spool/lpd/. /usr/spool/locks/. + echo -n "Starting " + up random "" /dev/random + echo . + # load random number generator if [ -f $RANDOM_FILE ] then diff --git a/include/minix/com.h b/include/minix/com.h index 843e2733f..60b66f0e0 100755 --- a/include/minix/com.h +++ b/include/minix/com.h @@ -60,8 +60,6 @@ # define KMEM_DEV 2 /* minor device for /dev/kmem */ # define NULL_DEV 3 /* minor device for /dev/null */ # define BOOT_DEV 4 /* minor device for /dev/boot */ -# define RANDOM_DEV 5 /* minor device for /dev/(u)random */ -# define URANDOM_DEV RANDOM_DEV # define ZERO_DEV 6 /* minor device for /dev/zero */ /* Full device numbers that are special to the boot monitor and FS. */ diff --git a/servers/fs/const.h b/servers/fs/const.h index fd68904fd..fc46429f4 100644 --- a/servers/fs/const.h +++ b/servers/fs/const.h @@ -9,7 +9,7 @@ #define NR_SUPERS 8 /* # slots in super block table */ #define NR_LOCKS 8 /* # slots in the file locking table */ -#define NR_DEVICES 16 /* # slots in the device <-> driver table */ +#define NR_DEVICES 17 /* # slots in the device <-> driver table */ /* The type of sizeof may be (unsigned) long. Use the following macro for * taking the sizes of small objects so that there are no surprises like diff --git a/servers/fs/dmap.c b/servers/fs/dmap.c index 8fa820eb7..44982b1a0 100644 --- a/servers/fs/dmap.c +++ b/servers/fs/dmap.c @@ -47,6 +47,7 @@ struct dmap dmap[NR_DEVICES] = { DT(0, gen_opcl, gen_io, NONE, DMAP_MUTABLE) /*13 = /dev/audio */ DT(0, gen_opcl, gen_io, NONE, DMAP_MUTABLE) /*14 = /dev/mixer */ DT(1, gen_opcl, gen_io, LOG_PROC_NR, 0) /*15 = /dev/klog */ + DT(0, gen_opcl, gen_io, NONE, DMAP_MUTABLE) /*16 = /dev/random */ #endif /* IBM_PC */ }; -- 2.44.0