Random is now a separate driver.
This commit is contained in:
parent
43f89d7eff
commit
c12b74dad5
21 changed files with 325 additions and 80 deletions
|
@ -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
|
||||
;;
|
||||
|
|
|
@ -26,3 +26,4 @@ all install depend clean:
|
|||
cd ./dpeth && $(MAKE) $@
|
||||
cd ./log && $(MAKE) $@
|
||||
cd ./bios_wini && $(MAKE) $@
|
||||
cd ./random && $(MAKE) $@
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<RANDOM_SOURCES; i++)
|
||||
{
|
||||
r_next= krandom.bin[i].r_next;
|
||||
r_size= krandom.bin[i].r_size;
|
||||
r_high= r_next+r_size;
|
||||
if (r_high <= RANDOM_ELEMENTS)
|
||||
{
|
||||
random_update(i, &krandom.bin[i].r_buf[r_next], r_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(r_next < RANDOM_ELEMENTS);
|
||||
random_update(i, &krandom.bin[i].r_buf[r_next],
|
||||
RANDOM_ELEMENTS-r_next);
|
||||
random_update(i, &krandom.bin[i].r_buf[0],
|
||||
r_high-RANDOM_ELEMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
/* Schedule new alarm for next m_random call. */
|
||||
if (OK != (s=sys_setalarm(KRANDOM_PERIOD, 0)))
|
||||
report("MEM", "sys_setalarm failed", s);
|
||||
}
|
||||
|
||||
/*============================================================================*
|
||||
* m_geometry *
|
||||
*============================================================================*/
|
||||
|
|
53
drivers/random/Makefile
Normal file
53
drivers/random/Makefile
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Makefile for random driver (RANDOM)
|
||||
DRIVER = random
|
||||
|
||||
# directories
|
||||
u = /usr
|
||||
i = $u/include
|
||||
s = $i/sys
|
||||
m = $i/minix
|
||||
b = $i/ibm
|
||||
d = ..
|
||||
|
||||
# programs, flags, etc.
|
||||
MAKE = exec make
|
||||
CC = exec cc
|
||||
CFLAGS = -I$i
|
||||
LDFLAGS = -i
|
||||
LIBS = -lsys -lsysutil
|
||||
|
||||
OBJ = main.o random.o sha2.o aes/rijndael_api.o aes/rijndael_alg.o
|
||||
LIBDRIVER = $d/libdriver/driver.o
|
||||
|
||||
|
||||
# build local binary
|
||||
all build: $(DRIVER)
|
||||
$(DRIVER): $(OBJ) $(LIBDRIVER)
|
||||
$(CC) -o $@ $(LDFLAGS) $(OBJ) $(LIBDRIVER) $(LIBS)
|
||||
install -S 1024w $(DRIVER)
|
||||
|
||||
$(LIBDRIVER):
|
||||
cd $d/libdriver && $(MAKE)
|
||||
|
||||
aes/rijndael_api.o:
|
||||
$(CC) -c -o $@ aes/rijndael_api.c
|
||||
|
||||
aes/rijndael_alg.o:
|
||||
$(CC) -c -o $@ aes/rijndael_alg.c
|
||||
|
||||
# install with other drivers
|
||||
install: /usr/sbin/$(DRIVER)
|
||||
/usr/sbin/$(DRIVER): $(DRIVER)
|
||||
install -o root -cs $? $@
|
||||
|
||||
# clean up local files
|
||||
clean:
|
||||
rm -f $(DRIVER) *.o *.bak
|
||||
|
||||
|
||||
depend:
|
||||
/usr/bin/mkdep "$(CC) -E $(CPPFLAGS)" *.c ../libdriver/*.c aes/*.c > .depend
|
||||
|
||||
# Include generated dependencies.
|
||||
include .depend
|
||||
|
260
drivers/random/main.c
Normal file
260
drivers/random/main.c
Normal file
|
@ -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 <sys/ioc_memory.h>
|
||||
#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; i<RANDOM_SOURCES; i++)
|
||||
{
|
||||
r_next= krandom.bin[i].r_next;
|
||||
r_size= krandom.bin[i].r_size;
|
||||
r_high= r_next+r_size;
|
||||
if (r_high <= RANDOM_ELEMENTS)
|
||||
{
|
||||
random_update(i, &krandom.bin[i].r_buf[r_next], r_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(r_next < RANDOM_ELEMENTS);
|
||||
random_update(i, &krandom.bin[i].r_buf[r_next],
|
||||
RANDOM_ELEMENTS-r_next);
|
||||
random_update(i, &krandom.bin[i].r_buf[0],
|
||||
r_high-RANDOM_ELEMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
/* Schedule new alarm for next m_random call. */
|
||||
if (OK != (s=sys_setalarm(KRANDOM_PERIOD, 0)))
|
||||
report("RANDOM", "sys_setalarm failed", s);
|
||||
}
|
||||
|
||||
/*============================================================================*
|
||||
* r_geometry *
|
||||
*============================================================================*/
|
||||
PRIVATE void r_geometry(entry)
|
||||
struct partition *entry;
|
||||
{
|
||||
/* Memory devices don't have a geometry, but the outside world insists. */
|
||||
entry->cylinders = div64u(m_geom[m_device].dv_size, SECTOR_SIZE) / (64 * 32);
|
||||
entry->heads = 64;
|
||||
entry->sectors = 32;
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
@ -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. */
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue