svn commit: r268486 - projects/random_number_generator/sys/dev/random

Mark Murray markm at FreeBSD.org
Thu Jul 10 09:17:48 UTC 2014


Author: markm
Date: Thu Jul 10 09:17:46 2014
New Revision: 268486
URL: http://svnweb.freebsd.org/changeset/base/268486

Log:
  Some DES-induced tidy-ups:
  
  o Remove unneeded (void *) casts. I can't remember all that well why they were there in the first place (probably misguided lint-fixing).
  
  o Fix sizeof(type) -> sizeof(variable). There are still one-or-two to be fixed in a follow-up.

Modified:
  projects/random_number_generator/sys/dev/random/dummy_rng.c
  projects/random_number_generator/sys/dev/random/fortuna.c
  projects/random_number_generator/sys/dev/random/ivy.c
  projects/random_number_generator/sys/dev/random/live_entropy_sources.c
  projects/random_number_generator/sys/dev/random/random_adaptors.c
  projects/random_number_generator/sys/dev/random/random_harvestq.c
  projects/random_number_generator/sys/dev/random/unit_test.c
  projects/random_number_generator/sys/dev/random/yarrow.c

Modified: projects/random_number_generator/sys/dev/random/dummy_rng.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/dummy_rng.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/dummy_rng.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -98,9 +98,9 @@ dummy_random_read_phony(uint8_t *buf, u_
 	/* srandom() is called in kern/init_main.c:proc0_post() */
 
 	/* Fill buf[] with random(9) output */
-	for (i = 0; i < count; i += sizeof(u_long)) {
+	for (i = 0; i < count; i += sizeof(randval)) {
 		randval = random();
-		size = MIN(count - i, sizeof(u_long));
+		size = MIN(count - i, sizeof(randval));
 		memcpy(buf + i, &randval, (size_t)size);
 	}
 

Modified: projects/random_number_generator/sys/dev/random/fortuna.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/fortuna.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/fortuna.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -143,7 +143,7 @@ random_fortuna_init_alg(void)
 	struct sysctl_oid *random_fortuna_o;
 #endif
 
-	memset((void *)(fortuna_start_cache.junk), 0, sizeof(fortuna_start_cache.junk));
+	memset(fortuna_start_cache.junk, 0, sizeof(fortuna_start_cache.junk));
 	fortuna_start_cache.length = 0U;
 	randomdev_hash_init(&fortuna_start_cache.hash);
 
@@ -192,7 +192,7 @@ random_fortuna_init_alg(void)
 	uint128_clear(&fortuna_state.counter.whole);
 
 	/* F&S - K = 0 */
-	memset((void *)(&fortuna_state.key), 0, sizeof(struct randomdev_key));
+	memset(&fortuna_state.key, 0, sizeof(fortuna_state.key));
 }
 
 void
@@ -200,7 +200,7 @@ random_fortuna_deinit_alg(void)
 {
 
 	mtx_destroy(&random_reseed_mtx);
-	memset((void *)(&fortuna_state), 0, sizeof(struct fortuna_state));
+	memset(&fortuna_state, 0, sizeof(fortuna_state));
 }
 
 /* F&S - AddRandomEvent() */
@@ -243,7 +243,7 @@ reseed(uint8_t *junk, u_int length)
 
 	/* F&S - temp = H(K|s) */
 	randomdev_hash_init(&context);
-	randomdev_hash_iterate(&context, &fortuna_state.key, sizeof(struct randomdev_key));
+	randomdev_hash_iterate(&context, &fortuna_state.key, sizeof(fortuna_state.key));
 	randomdev_hash_iterate(&context, junk, length);
 	randomdev_hash_finish(&context, temp);
 
@@ -254,8 +254,8 @@ reseed(uint8_t *junk, u_int length)
 
 	/* F&S - K = hash */
 	randomdev_encrypt_init(&fortuna_state.key, temp);
-	memset((void *)temp, 0, sizeof(temp));
-	memset((void *)hash, 0, sizeof(hash));
+	memset(temp, 0, sizeof(temp));
+	memset(hash, 0, sizeof(hash));
 
 	/* Unblock the device if it was blocked due to being unseeded */
 	if (uint128_is_zero(fortuna_state.counter.whole))
@@ -299,7 +299,7 @@ random_fortuna_genrandom(uint8_t *buf, u
 	/* F&S - K = GenerateBlocks(2) */
 	random_fortuna_genblocks(temp, KEYSIZE/BLOCKSIZE);
 	randomdev_encrypt_init(&fortuna_state.key, temp);
-	memset((void *)temp, 0, sizeof(temp));
+	memset(temp, 0, sizeof(temp));
 }
 
 /* F&S - RandomData() */
@@ -364,10 +364,10 @@ random_fortuna_read(uint8_t *buf, u_int 
 				reseed(s, seedlength);
 
 				/* Clean up */
-				memset((void *)s, 0, seedlength);
+				memset(s, 0, seedlength);
 				seedlength = 0U;
-				memset((void *)temp, 0, sizeof(temp));
-				memset((void *)&context, 0, sizeof(context));
+				memset(temp, 0, sizeof(temp));
+				memset(&context, 0, sizeof(context));
 			}
 		}
 	}
@@ -406,11 +406,11 @@ random_fortuna_write(uint8_t *buf, u_int
 	printf("\n");
 #endif
 
-	memset((void *)(temp), 0, KEYSIZE);
+	memset(temp, 0, KEYSIZE);
 	randomdev_hash_init(&fortuna_start_cache.hash);
 
 	reseed(fortuna_start_cache.junk, MIN(PAGE_SIZE, fortuna_start_cache.length));
-	memset((void *)(fortuna_start_cache.junk), 0, sizeof(fortuna_start_cache.junk));
+	memset(fortuna_start_cache.junk, 0, sizeof(fortuna_start_cache.junk));
 
 	mtx_unlock(&random_reseed_mtx);
 }

Modified: projects/random_number_generator/sys/dev/random/ivy.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/ivy.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/ivy.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -92,9 +92,9 @@ random_ivy_read(void *buf, u_int c)
 	long *b;
 	u_int count;
 
-	KASSERT(c % sizeof(long) == 0, ("partial read %d", c));
+	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
 	b = buf;
-	for (count = c; count > 0; count -= sizeof(long)) {
+	for (count = c; count > 0; count -= sizeof(*b)) {
 		if (ivy_rng_store(b++) == 0)
 			break;
 	}

Modified: projects/random_number_generator/sys/dev/random/live_entropy_sources.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/live_entropy_sources.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/live_entropy_sources.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -67,7 +67,7 @@ live_entropy_source_register(struct live
 
 	KASSERT(rsource != NULL, ("invalid input to %s", __func__));
 
-	lles = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK);
+	lles = malloc(sizeof(*lles), M_ENTROPY, M_WAITOK);
 	lles->lles_rsource = rsource;
 
 	sx_xlock(&les_lock);

Modified: projects/random_number_generator/sys/dev/random/random_adaptors.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/random_adaptors.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/random_adaptors.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -98,6 +98,14 @@ random_adaptor_choose(void)
 	if (TUNABLE_STR_FETCH("kern.random.active_adaptor", rngs, sizeof(rngs))) {
 		cp = rngs;
 
+		/* XXX: FIX!! (DES):
+		 * - fetch tunable once, at boot
+		 * - make sysctl r/w
+		 * - when fetching tunable or processing a sysctl
+		 *   write, parse into list of strings so we don't
+		 *   have to do it here again and again
+		 * - sysctl read should return a reconstructed string
+		 */
 		while ((token = strsep(&cp, ",")) != NULL) {
 			LIST_FOREACH(rra, &random_adaptors_list, rra_entries)
 				if (strcmp(rra->rra_name, token) == 0) {
@@ -156,7 +164,7 @@ random_adaptor_register(const char *name
 
 	KASSERT(name != NULL && ra != NULL, ("invalid input to %s", __func__));
 
-	rra = malloc(sizeof(struct random_adaptors), M_ENTROPY, M_WAITOK);
+	rra = malloc(sizeof(*rra), M_ENTROPY, M_WAITOK);
 	rra->rra_name = name;
 	rra->rra_ra = ra;
 
@@ -237,7 +245,7 @@ random_adaptor_read(struct cdev *dev __u
 
 		/* The actual read */
 
-		random_buf = (void *)malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
+		random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
 
 		while (uio->uio_resid && !error) {
 			c = MIN(uio->uio_resid, PAGE_SIZE);
@@ -288,7 +296,7 @@ random_adaptor_write(struct cdev *dev __
 
 	sx_slock(&random_adaptors_lock);
 
-	random_buf = (void *)malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
+	random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
 
 	while (uio->uio_resid > 0) {
 		c = MIN(uio->uio_resid, PAGE_SIZE);

Modified: projects/random_number_generator/sys/dev/random/random_harvestq.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/random_harvestq.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/random_harvestq.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -324,7 +324,7 @@ random_harvestq_deinit(void)
 	 * Command the hash/reseed thread to end and wait for it to finish
 	 */
 	random_kthread_control = -1;
-	tsleep((void *)&random_kthread_control, 0, "term", 0);
+	tsleep(&random_kthread_control, 0, "term", 0);
 
 	mtx_destroy(&harvest_mtx);
 

Modified: projects/random_number_generator/sys/dev/random/unit_test.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/unit_test.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/unit_test.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -228,7 +228,7 @@ main(int argc, char *argv[])
 
 	for (t = 0; t < NUM_THREADS; t++) {
 		printf("In main: creating thread %ld\n", t);
-		rc = thrd_create(&threads[t], (t == 0 ? RunHarvester : (t == 1 ? WriteCSPRNG : ReadCSPRNG)), (void *)t);
+		rc = thrd_create(&threads[t], (t == 0 ? RunHarvester : (t == 1 ? WriteCSPRNG : ReadCSPRNG)), t);
 		if (rc != thrd_success) {
 			printf("ERROR; return code from thrd_create() is %d\n", rc);
 			exit(-1);

Modified: projects/random_number_generator/sys/dev/random/yarrow.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/yarrow.c	Thu Jul 10 09:11:39 2014	(r268485)
+++ projects/random_number_generator/sys/dev/random/yarrow.c	Thu Jul 10 09:17:46 2014	(r268486)
@@ -142,7 +142,7 @@ random_yarrow_init_alg(void)
 	struct sysctl_oid *random_yarrow_o;
 #endif /* _KERNEL */
 
-	memset((void *)(yarrow_state.start_cache.junk), 0, KEYSIZE);
+	memset(yarrow_state.start_cache.junk, 0, KEYSIZE);
 	randomdev_hash_init(&yarrow_state.start_cache.hash);
 
 	/* Set up the lock for the reseed/gate state */
@@ -225,7 +225,7 @@ random_yarrow_deinit_alg(void)
 {
 
 	mtx_destroy(&random_reseed_mtx);
-	memset((void *)(&yarrow_state), 0, sizeof(struct yarrow_state));
+	memset(&yarrow_state, 0, sizeof(yarrow_state));
 
 #ifdef _KERNEL
 	sysctl_ctx_free(&random_clist);
@@ -376,7 +376,7 @@ reseed(u_int fastslow)
 		/* v[i] #= h(v[0]) */
 		randomdev_hash_iterate(&context, v[0], KEYSIZE);
 		/* v[i] #= h(i) */
-		randomdev_hash_iterate(&context, &i, sizeof(u_int));
+		randomdev_hash_iterate(&context, &i, sizeof(i));
 		/* Return the hashval */
 		randomdev_hash_finish(&context, v[i]);
 	}
@@ -406,10 +406,10 @@ reseed(u_int fastslow)
 
 	/* 6. Wipe memory of intermediate values */
 
-	memset((void *)v, 0, sizeof(v));
-	memset((void *)temp, 0, sizeof(temp));
-	memset((void *)hash, 0, sizeof(hash));
-	memset((void *)&context, 0, sizeof(context));
+	memset(v, 0, sizeof(v));
+	memset(temp, 0, sizeof(temp));
+	memset(hash, 0, sizeof(hash));
+	memset(&context, 0, sizeof(context));
 
 #ifdef RANDOM_RWFILE_WRITE_IS_OK /* Not defined so writes ain't gonna happen */
 	/* 7. Dump to seed file */
@@ -484,7 +484,7 @@ random_yarrow_write(uint8_t *buf, u_int 
 #endif
 
 	random_yarrow_process_buffer(yarrow_state.start_cache.junk, KEYSIZE);
-	memset((void *)(yarrow_state.start_cache.junk), 0, KEYSIZE);
+	memset(yarrow_state.start_cache.junk, 0, KEYSIZE);
 
 	mtx_unlock(&random_reseed_mtx);
 }
@@ -501,7 +501,7 @@ generator_gate(void)
 	}
 
 	randomdev_encrypt_init(&yarrow_state.key, temp);
-	memset((void *)temp, 0, KEYSIZE);
+	memset(temp, 0, KEYSIZE);
 }
 
 void


More information about the svn-src-projects mailing list