Add a RANDOM_RWFILE option and hide the entropy cache code behind it.

Rename YARROW_RNG and FORTUNA_RNG to RANDOM_YARROW and RANDOM_FORTUNA.
Add the RANDOM_* options to LINT.
This commit is contained in:
Dag-Erling Smørgrav 2013-10-09 20:14:16 +00:00
parent 6916062296
commit 997b0a641d
6 changed files with 44 additions and 21 deletions

View File

@ -2962,3 +2962,8 @@ options RCTL
options BROOKTREE_ALLOC_PAGES=(217*4+1) options BROOKTREE_ALLOC_PAGES=(217*4+1)
options MAXFILES=999 options MAXFILES=999
# Random number generator
options RANDOM_YARROW # Yarrow RNG
##options RANDOM_FORTUNA # Fortuna RNG - not yet implemented
options RANDOM_DEBUG # Debugging messages
options RANDOM_RWFILE # Read and write entropy cache

View File

@ -906,6 +906,7 @@ RACCT opt_global.h
RCTL opt_global.h RCTL opt_global.h
# Random number generator(s) # Random number generator(s)
YARROW_RNG opt_random.h RANDOM_YARROW opt_random.h
FORTUNA_RNG opt_random.h RANDOM_FORTUNA opt_random.h
RANDOM_DEBUG opt_random.h RANDOM_DEBUG opt_random.h
RANDOM_RWFILE opt_random.h

View File

@ -30,6 +30,8 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include "opt_random.h"
#include <sys/param.h> #include <sys/param.h>
#include <sys/systm.h> #include <sys/systm.h>
#include <sys/eventhandler.h> #include <sys/eventhandler.h>
@ -79,11 +81,12 @@ int random_kthread_control = 0;
static struct proc *random_kthread_proc; static struct proc *random_kthread_proc;
#ifdef NOTYET /* This is full of policy stuff, needs further discussion */ #ifdef RANDOM_RWFILE
static const char *entropy_files[] = { static const char *entropy_files[] = {
"/entropy", "/entropy",
NULL NULL
}; };
#endif
/* Deal with entropy cached externally if this is present. /* Deal with entropy cached externally if this is present.
* Lots of policy may eventually arrive in this function. * Lots of policy may eventually arrive in this function.
@ -92,10 +95,13 @@ static const char *entropy_files[] = {
static void static void
random_harvestq_cache(void *arg __unused) random_harvestq_cache(void *arg __unused)
{ {
const char **entropy_file; uint8_t *keyfile, *data;
uint8_t *keyfile, *data, *zbuf;
size_t size, i; size_t size, i;
#ifdef RANDOM_RWFILE
const char **entropy_file;
uint8_t *zbuf;
int error; int error;
#endif
/* Get stuff that may have been preloaded by loader(8) */ /* Get stuff that may have been preloaded by loader(8) */
keyfile = preload_search_by_type("/boot/entropy"); keyfile = preload_search_by_type("/boot/entropy");
@ -112,6 +118,7 @@ random_harvestq_cache(void *arg __unused)
printf("random: no preloaded entropy cache available\n"); printf("random: no preloaded entropy cache available\n");
} }
#ifdef RANDOM_RWFILE
/* Read and attempt to overwrite the entropy cache files. /* Read and attempt to overwrite the entropy cache files.
* If the file exists, can be read and then overwritten, * If the file exists, can be read and then overwritten,
* then use it. Ignore it otherwise, but print out what is * then use it. Ignore it otherwise, but print out what is
@ -137,9 +144,9 @@ random_harvestq_cache(void *arg __unused)
} }
bzero(data, PAGE_SIZE); bzero(data, PAGE_SIZE);
free(data, M_ENTROPY); free(data, M_ENTROPY);
#endif
} }
EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0); EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0);
#endif /* NOTYET */
static void static void
random_kthread(void *arg) random_kthread(void *arg)

View File

@ -28,12 +28,12 @@
#include "opt_random.h" #include "opt_random.h"
#if !defined(YARROW_RNG) && !defined(FORTUNA_RNG) #if !defined(RANDOM_YARROW) && !defined(RANDOM_FORTUNA)
#define YARROW_RNG #define RANDOM_YARROW
#elif defined(YARROW_RNG) && defined(FORTUNA_RNG) #elif defined(RANDOM_YARROW) && defined(RANDOM_FORTUNA)
#error "Must define either YARROW_RNG or FORTUNA_RNG" #error "Must define either RANDOM_YARROW or RANDOM_FORTUNA"
#endif #endif
#if defined(FORTUNA_RNG) #if defined(RANDOM_FORTUNA)
#error "Fortuna is not yet implemented" #error "Fortuna is not yet implemented"
#endif #endif
@ -62,10 +62,10 @@ __FBSDID("$FreeBSD$");
#include <dev/random/randomdev_soft.h> #include <dev/random/randomdev_soft.h>
#include <dev/random/random_harvestq.h> #include <dev/random/random_harvestq.h>
#include <dev/random/random_adaptors.h> #include <dev/random/random_adaptors.h>
#if defined(YARROW_RNG) #if defined(RANDOM_YARROW)
#include <dev/random/yarrow.h> #include <dev/random/yarrow.h>
#endif #endif
#if defined(FORTUNA_RNG) #if defined(RANDOM_FORTUNA)
#include <dev/random/fortuna.h> #include <dev/random/fortuna.h>
#endif #endif
@ -74,7 +74,7 @@ static int randomdev_poll(int event, struct thread *td);
static int randomdev_block(int flag); static int randomdev_block(int flag);
static void randomdev_flush_reseed(void); static void randomdev_flush_reseed(void);
#if defined(YARROW_RNG) #if defined(RANDOM_YARROW)
static struct random_adaptor random_context = { static struct random_adaptor random_context = {
.ident = "Software, Yarrow", .ident = "Software, Yarrow",
.init = randomdev_init, .init = randomdev_init,
@ -89,7 +89,7 @@ static struct random_adaptor random_context = {
#define RANDOM_CSPRNG_NAME "yarrow" #define RANDOM_CSPRNG_NAME "yarrow"
#endif #endif
#if defined(FORTUNA_RNG) #if defined(RANDOM_FORTUNA)
static struct random_adaptor random_context = { static struct random_adaptor random_context = {
.ident = "Software, Fortuna", .ident = "Software, Fortuna",
.init = randomdev_init, .init = randomdev_init,
@ -123,10 +123,10 @@ randomdev_init(void)
{ {
struct sysctl_oid *random_sys_o, *random_sys_harvest_o; struct sysctl_oid *random_sys_o, *random_sys_harvest_o;
#if defined(YARROW_RNG) #if defined(RANDOM_YARROW)
random_yarrow_init_alg(&random_clist); random_yarrow_init_alg(&random_clist);
#endif #endif
#if defined(FORTUNA_RNG) #if defined(RANDOM_FORTUNA)
random_fortuna_init_alg(&random_clist); random_fortuna_init_alg(&random_clist);
#endif #endif
@ -186,10 +186,10 @@ randomdev_deinit(void)
random_kthread_control = -1; random_kthread_control = -1;
tsleep((void *)&random_kthread_control, 0, "term", 0); tsleep((void *)&random_kthread_control, 0, "term", 0);
#if defined(YARROW_RNG) #if defined(RANDOM_YARROW)
random_yarrow_deinit_alg(); random_yarrow_deinit_alg();
#endif #endif
#if defined(FORTUNA_RNG) #if defined(RANDOM_FORTUNA)
random_fortuna_deinit_alg(); random_fortuna_deinit_alg();
#endif #endif
@ -258,11 +258,11 @@ randomdev_flush_reseed(void)
while (random_kthread_control) while (random_kthread_control)
pause("-", hz / 10); pause("-", hz / 10);
#if defined(YARROW_RNG) #if defined(RANDOM_YARROW)
/* This ultimately calls randomdev_unblock() */ /* This ultimately calls randomdev_unblock() */
random_yarrow_reseed(); random_yarrow_reseed();
#endif #endif
#if defined(FORTUNA_RNG) #if defined(RANDOM_FORTUNA)
/* This ultimately calls randomdev_unblock() */ /* This ultimately calls randomdev_unblock() */
random_fortuna_reseed(); random_fortuna_reseed();
#endif #endif

View File

@ -28,6 +28,10 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include "opt_random.h"
#ifdef RANDOM_RWFILE
#include <sys/param.h> #include <sys/param.h>
#include <sys/systm.h> #include <sys/systm.h>
#include <sys/kernel.h> #include <sys/kernel.h>
@ -88,3 +92,5 @@ randomdev_write_file(const char *filename, void *buf, size_t length)
return (error); return (error);
} }
#endif

View File

@ -29,7 +29,11 @@
#ifndef SYS_DEV_RANDOM_RWFILE_H_INCLUDED #ifndef SYS_DEV_RANDOM_RWFILE_H_INCLUDED
#define SYS_DEV_RANDOM_RWFILE_H_INCLUDED #define SYS_DEV_RANDOM_RWFILE_H_INCLUDED
#ifdef RANDOM_RWFILE
int randomdev_read_file(const char *filename, void *buf, size_t); int randomdev_read_file(const char *filename, void *buf, size_t);
int randomdev_write_file(const char *filename, void *buf, size_t); int randomdev_write_file(const char *filename, void *buf, size_t);
#endif #endif
#endif