diff options
author | Mark Murray <markm@FreeBSD.org> | 2002-03-03 19:44:22 +0000 |
---|---|---|
committer | Mark Murray <markm@FreeBSD.org> | 2002-03-03 19:44:22 +0000 |
commit | e119960112f35b3ec1179e262cccde05b59a0309 (patch) | |
tree | 24e2d1fcb04c65adc9cb948dfed4b2a10ffe1d1b | |
parent | a3b69bbaa65e0c1212dca1ffd5531b58b1467dd7 (diff) | |
download | src-e119960112f35b3ec1179e262cccde05b59a0309.tar.gz src-e119960112f35b3ec1179e262cccde05b59a0309.zip |
Notes
-rw-r--r-- | sys/dev/random/harvest.c | 22 | ||||
-rw-r--r-- | sys/dev/random/hash.c | 11 | ||||
-rw-r--r-- | sys/dev/random/hash.h | 2 | ||||
-rw-r--r-- | sys/dev/random/randomdev.c | 71 | ||||
-rw-r--r-- | sys/dev/random/randomdev.h | 12 | ||||
-rw-r--r-- | sys/dev/random/yarrow.c | 36 | ||||
-rw-r--r-- | sys/sys/random.h | 15 |
7 files changed, 95 insertions, 74 deletions
diff --git a/sys/dev/random/harvest.c b/sys/dev/random/harvest.c index e9ac7f9220d6..1e2275d60744 100644 --- a/sys/dev/random/harvest.c +++ b/sys/dev/random/harvest.c @@ -42,7 +42,7 @@ #include <dev/random/randomdev.h> -static u_int read_random_phony(void *, u_int); +static int read_random_phony(void *, int); /* Structure holding the desired entropy sources */ struct harvest_select harvest = { 0, 0, 0 }; @@ -52,12 +52,12 @@ struct harvest_select harvest = { 0, 0, 0 }; */ static void (*reap_func)(u_int64_t, void *, u_int, u_int, u_int, enum esource) = NULL; -static u_int (*read_func)(void *, u_int) = read_random_phony; +static int (*read_func)(void *, int) = read_random_phony; /* Initialise the harvester at load time */ void random_init_harvester(void (*reaper)(u_int64_t, void *, u_int, u_int, u_int, - enum esource), u_int (*reader)(void *, u_int)) + enum esource), int (*reader)(void *, int)) { reap_func = reaper; read_func = reader; @@ -86,8 +86,8 @@ random_harvest(void *entropy, u_int count, u_int bits, u_int frac, } /* Userland-visible version of read_random */ -u_int -read_random(void *buf, u_int count) +int +read_random(void *buf, int count) { return (*read_func)(buf, count); } @@ -96,8 +96,8 @@ read_random(void *buf, u_int count) * provide _some_ kind of randomness. This should only be used * inside other RNG's, like arc4random(9). */ -static u_int -read_random_phony(void *buf, u_int count) +static int +read_random_phony(void *buf, int count) { u_long randval; int size, i; @@ -110,10 +110,12 @@ read_random_phony(void *buf, u_int count) srandom((u_long)get_cyclecount()); /* Fill buf[] with random(9) output */ - for (i = 0; i < count; i+= sizeof(u_long)) { + for (i = 0; i < count; i+= (size_t)sizeof(u_long)) { randval = random(); - size = (count - i) < sizeof(u_long) ? (count - i) : sizeof(u_long); - memcpy(&((char *)buf)[i], &randval, size); + size = (count - i) < (int)sizeof(u_long) + ? (count - i) + : sizeof(u_long); + memcpy(&((char *)buf)[i], &randval, (size_t)size); } return count; diff --git a/sys/dev/random/hash.c b/sys/dev/random/hash.c index ed796f13d2ba..a9246c34db7c 100644 --- a/sys/dev/random/hash.c +++ b/sys/dev/random/hash.c @@ -28,10 +28,6 @@ #include <sys/param.h> #include <sys/systm.h> -#include <sys/queue.h> -#include <sys/libkern.h> -#include <sys/random.h> -#include <sys/types.h> #include <crypto/rijndael/rijndael.h> @@ -55,9 +51,14 @@ yarrow_hash_iterate(struct yarrowhash *context, void *data, size_t size) { u_char temp[KEYSIZE]; u_int i, j; + union { + void *pv; + char *pc; + } trans; + trans.pv = data; for (i = 0; i < size; i++) { - context->accum[context->partial++] = ((u_char *)(data))[i]; + context->accum[context->partial++] = trans.pc[i]; if (context->partial == (KEYSIZE - 1)) { rijndael_makeKey(&context->hashkey, DIR_ENCRYPT, KEYSIZE*8, context->accum); diff --git a/sys/dev/random/hash.h b/sys/dev/random/hash.h index 99f0b4849b83..5a308210d690 100644 --- a/sys/dev/random/hash.h +++ b/sys/dev/random/hash.h @@ -32,7 +32,7 @@ struct yarrowhash { /* Big! Make static! */ keyInstance hashkey; /* Data cycles through here */ cipherInstance cipher; /* Rijndael internal */ u_char hash[KEYSIZE]; /* Repeatedly encrypted */ - u_char accum[KEYSIZE]; /* Accumulate partial chunks */ + char accum[KEYSIZE]; /* Accumulate partial chunks */ u_int partial; /* Keep track of < KEYSIZE chunks */ }; diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index fe73b0fe7551..8587200a727e 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -36,11 +36,9 @@ #include <sys/kthread.h> #include <sys/lock.h> #include <sys/malloc.h> -#include <sys/module.h> #include <sys/mutex.h> #include <sys/poll.h> #include <sys/proc.h> -#include <sys/queue.h> #include <sys/random.h> #include <sys/selinfo.h> #include <sys/sysctl.h> @@ -50,7 +48,6 @@ #include <machine/bus.h> #include <machine/cpu.h> -#include <machine/resource.h> #include <dev/random/randomdev.h> @@ -78,11 +75,12 @@ static struct cdevsw random_cdevsw = { /* dump */ nodump, /* psize */ nopsize, /* flags */ 0, + /* kqfilter */ NULL }; static void random_kthread(void *); static void random_harvest_internal(u_int64_t, void *, u_int, u_int, u_int, enum esource); -static void random_write_internal(void *, u_int); +static void random_write_internal(void *, int); /* Ring buffer holding harvested entropy */ static struct harvestring { @@ -106,6 +104,7 @@ static struct proc *random_kthread_proc; static dev_t random_dev; static dev_t urandom_dev; +/* ARGSUSED */ static int random_check_boolean(SYSCTL_HANDLER_ARGS) { @@ -138,8 +137,9 @@ SYSCTL_PROC(_kern_random_sys_harvest, OID_AUTO, interrupt, CTLTYPE_INT|CTLFLAG_RW, &harvest.interrupt, 0, random_check_boolean, "I", "Harvest IRQ entropy"); +/* ARGSUSED */ static int -random_open(dev_t dev, int flags, int fmt, struct thread *td) +random_open(dev_t dev __unused, int flags, int fmt __unused, struct thread *td) { int error; @@ -154,8 +154,9 @@ random_open(dev_t dev, int flags, int fmt, struct thread *td) return 0; } +/* ARGSUSED */ static int -random_close(dev_t dev, int flags, int fmt, struct thread *td) +random_close(dev_t dev __unused, int flags, int fmt __unused, struct thread *td) { if (flags & FWRITE) { if (!(suser(td->td_proc) || @@ -165,10 +166,11 @@ random_close(dev_t dev, int flags, int fmt, struct thread *td) return 0; } +/* ARGSUSED */ static int -random_read(dev_t dev, struct uio *uio, int flag) +random_read(dev_t dev __unused, struct uio *uio, int flag) { - u_int c, ret; + int c, ret; int error = 0; void *random_buf; @@ -181,8 +183,8 @@ random_read(dev_t dev, struct uio *uio, int flag) if (error != 0) return error; } - c = min(uio->uio_resid, PAGE_SIZE); - random_buf = (void *)malloc(c, M_TEMP, M_WAITOK); + c = uio->uio_resid < PAGE_SIZE ? uio->uio_resid : PAGE_SIZE; + random_buf = (void *)malloc((u_long)c, M_TEMP, M_WAITOK); while (uio->uio_resid > 0 && error == 0) { ret = read_random_real(random_buf, c); error = uiomove(random_buf, ret, uio); @@ -191,17 +193,20 @@ random_read(dev_t dev, struct uio *uio, int flag) return error; } +/* ARGSUSED */ static int -random_write(dev_t dev, struct uio *uio, int flag) +random_write(dev_t dev __unused, struct uio *uio, int flag __unused) { - u_int c; + int c; int error; void *random_buf; error = 0; random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); while (uio->uio_resid > 0) { - c = min(uio->uio_resid, PAGE_SIZE); + c = (int)(uio->uio_resid < PAGE_SIZE + ? uio->uio_resid + : PAGE_SIZE); error = uiomove(random_buf, c, uio); if (error) break; @@ -211,8 +216,10 @@ random_write(dev_t dev, struct uio *uio, int flag) return error; } +/* ARGSUSED */ static int -random_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) +random_ioctl(dev_t dev __unused, u_long cmd, caddr_t addr __unused, + int flags __unused, struct thread *td __unused) { switch (cmd) { /* Really handled in upper layer */ @@ -224,8 +231,9 @@ random_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) } } +/* ARGSUSED */ static int -random_poll(dev_t dev, int events, struct thread *td) +random_poll(dev_t dev __unused, int events, struct thread *td) { int revents; @@ -239,8 +247,9 @@ random_poll(dev_t dev, int events, struct thread *td) return revents; } +/* ARGSUSED */ static int -random_modevent(module_t mod, int type, void *data) +random_modevent(module_t mod __unused, int type, void *data __unused) { int error; @@ -306,11 +315,12 @@ random_modevent(module_t mod, int type, void *data) DEV_MODULE(random, random_modevent, NULL); +/* ARGSUSED */ static void -random_kthread(void *arg /* NOTUSED */) +random_kthread(void *arg __unused) { struct harvest *event; - int newtail, burst; + u_int newtail, burst; /* Drain the harvest queue (in 'burst' size chunks, * if 'burst' > 0. If 'burst' == 0, then completely @@ -365,8 +375,8 @@ static void random_harvest_internal(u_int64_t somecounter, void *entropy, u_int count, u_int bits, u_int frac, enum esource origin) { - struct harvest *harvest; - int newhead; + struct harvest *pharvest; + u_int newhead; newhead = (harvestring.head + 1) & HARVEST_RING_MASK; @@ -374,16 +384,17 @@ random_harvest_internal(u_int64_t somecounter, void *entropy, u_int count, /* Add the harvested data to the ring buffer */ - harvest = &harvestring.data[harvestring.head]; + pharvest = &harvestring.data[harvestring.head]; /* Stuff the harvested data into the ring */ - harvest->somecounter = somecounter; + pharvest->somecounter = somecounter; count = count > HARVESTSIZE ? HARVESTSIZE : count; - memcpy(harvest->entropy, entropy, count); - harvest->size = count; - harvest->bits = bits; - harvest->frac = frac; - harvest->source = origin < ENTROPYSOURCE ? origin : 0; + memcpy(pharvest->entropy, entropy, count); + pharvest->size = count; + pharvest->bits = bits; + pharvest->frac = frac; + pharvest->source = + origin < ENTROPYSOURCE ? origin : RANDOM_START; /* Bump the ring counter. This action is assumed * to be atomic. @@ -395,9 +406,9 @@ random_harvest_internal(u_int64_t somecounter, void *entropy, u_int count, } static void -random_write_internal(void *buf, u_int count) +random_write_internal(void *buf, int count) { - u_int i; + int i; /* Break the input up into HARVESTSIZE chunks. * The writer has too much control here, so "estimate" the @@ -418,7 +429,7 @@ random_write_internal(void *buf, u_int count) count %= HARVESTSIZE; if (count) { random_harvest_internal(get_cyclecount(), (char *)buf + i, - count, 0, 0, RANDOM_WRITE); + (u_int)count, 0, 0, RANDOM_WRITE); } } diff --git a/sys/dev/random/randomdev.h b/sys/dev/random/randomdev.h index 80a61ba18a7d..359dc269d50c 100644 --- a/sys/dev/random/randomdev.h +++ b/sys/dev/random/randomdev.h @@ -58,14 +58,14 @@ struct harvest { void random_init(void); void random_deinit(void); -void random_init_harvester(void (*)(u_int64_t, void *, u_int, u_int, u_int, enum esource), u_int (*)(void *, u_int)); +void random_init_harvester(void (*)(u_int64_t, void *, u_int, u_int, u_int, enum esource), int (*)(void *, int)); void random_deinit_harvester(void); void random_set_wakeup_exit(void *); void random_process_event(struct harvest *event); void random_reseed(void); void random_unblock(void); -u_int read_random_real(void *, u_int); +int read_random_real(void *, int); /* If this was c++, this would be a template */ #define RANDOM_CHECK_UINT(name, min, max) \ @@ -73,10 +73,10 @@ static int \ random_check_uint_##name(SYSCTL_HANDLER_ARGS) \ { \ if (oidp->oid_arg1 != NULL) { \ - if (*(u_int *)(oidp->oid_arg1) < min) \ - *(u_int *)(oidp->oid_arg1) = min; \ - else if (*(u_int *)(oidp->oid_arg1) > max) \ - *(u_int *)(oidp->oid_arg1) = max; \ + if (*(u_int *)(oidp->oid_arg1) <= (min)) \ + *(u_int *)(oidp->oid_arg1) = (min); \ + else if (*(u_int *)(oidp->oid_arg1) > (max)) \ + *(u_int *)(oidp->oid_arg1) = (max); \ } \ return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, \ req); \ diff --git a/sys/dev/random/yarrow.c b/sys/dev/random/yarrow.c index 1554f06e3e92..8ab16b245e3b 100644 --- a/sys/dev/random/yarrow.c +++ b/sys/dev/random/yarrow.c @@ -29,13 +29,10 @@ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> -#include <sys/libkern.h> #include <sys/lock.h> #include <sys/mutex.h> -#include <sys/selinfo.h> #include <sys/random.h> #include <sys/sysctl.h> -#include <sys/types.h> #include <crypto/rijndael/rijndael.h> @@ -81,8 +78,9 @@ static struct mtx random_reseed_mtx; void random_process_event(struct harvest *event) { - u_int pl, src, overthreshhold[2]; + u_int pl, overthreshhold[2]; struct source *source; + enum esource src; /* Unpack the event into the appropriate source accumulator */ pl = random_state.which; @@ -98,7 +96,7 @@ random_process_event(struct harvest *event) /* Count the over-threshold sources in each pool */ for (pl = 0; pl < 2; pl++) { overthreshhold[pl] = 0; - for (src = 0; src < ENTROPYSOURCE; src++) { + for (src = RANDOM_START; src < ENTROPYSOURCE; src++) { if (random_state.pool[pl].source[src].bits > random_state.pool[pl].thresh) overthreshhold[pl]++; @@ -160,7 +158,8 @@ reseed(u_int fastslow) static struct yarrowhash context; u_char hash[KEYSIZE]; /* h' */ u_char temp[KEYSIZE]; - u_int i, j; + u_int i; + enum esource j; #ifdef DEBUG mtx_lock(&Giant); @@ -222,7 +221,7 @@ reseed(u_int fastslow) /* 5. Reset entropy estimate accumulators to zero */ for (i = 0; i <= fastslow; i++) { - for (j = 0; j < ENTROPYSOURCE; j++) { + for (j = RANDOM_START; j < ENTROPYSOURCE; j++) { random_state.pool[i].source[j].bits = 0; random_state.pool[i].source[j].frac = 0; } @@ -253,14 +252,14 @@ reseed(u_int fastslow) /* Internal function to do return processed entropy from the * Yarrow PRNG */ -u_int -read_random_real(void *buf, u_int count) +int +read_random_real(void *buf, int count) { static int cur = 0; static int gate = 1; static u_char genval[KEYSIZE]; - u_int i; - u_int retval; + int i; + int retval; /* The reseed task must not be jumped on */ mtx_lock(&random_reseed_mtx); @@ -270,9 +269,9 @@ read_random_real(void *buf, u_int count) random_state.outputblocks = 0; gate = 0; } - if (count >= sizeof(random_state.counter)) { + if (count > 0 && (size_t)count >= sizeof(random_state.counter)) { retval = 0; - for (i = 0; i < count; i += sizeof(random_state.counter)) { + for (i = 0; i < count; i += (int)sizeof(random_state.counter)) { random_state.counter[0]++; yarrow_encrypt(&random_state.key, random_state.counter, genval); @@ -283,7 +282,7 @@ read_random_real(void *buf, u_int count) generator_gate(); random_state.outputblocks = 0; } - retval += sizeof(random_state.counter); + retval += (int)sizeof(random_state.counter); } } else { @@ -291,8 +290,8 @@ read_random_real(void *buf, u_int count) random_state.counter[0]++; yarrow_encrypt(&random_state.key, random_state.counter, genval); - memcpy(buf, genval, count); - cur = sizeof(random_state.counter) - count; + memcpy(buf, genval, (size_t)count); + cur = (int)sizeof(random_state.counter) - count; if (++random_state.outputblocks >= random_state.gengateinterval) { generator_gate(); @@ -302,8 +301,9 @@ read_random_real(void *buf, u_int count) } else { retval = cur < count ? cur : count; - memcpy(buf, &genval[sizeof(random_state.counter) - cur], - retval); + memcpy(buf, + &genval[(int)sizeof(random_state.counter) - cur], + (size_t)retval); cur -= retval; } } diff --git a/sys/sys/random.h b/sys/sys/random.h index f2c89e65ead4..3cd147124f39 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -31,10 +31,17 @@ #ifdef _KERNEL -u_int read_random(void *, u_int); - -enum esource { RANDOM_WRITE, RANDOM_KEYBOARD, RANDOM_MOUSE, RANDOM_NET, - RANDOM_INTERRUPT, ENTROPYSOURCE }; +int read_random(void *, int); + +enum esource { + RANDOM_START = 0, + RANDOM_WRITE = 0, + RANDOM_KEYBOARD, + RANDOM_MOUSE, + RANDOM_NET, + RANDOM_INTERRUPT, + ENTROPYSOURCE +}; void random_harvest(void *, u_int, u_int, u_int, enum esource); /* Allow the sysadmin to select the broad category of |