Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 27 Apr 2014 19:00:14 +0000 (UTC)
From:      Mark Murray <markm@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r265022 - projects/random_number_generator/sys/dev/random
Message-ID:  <201404271900.s3RJ0EkC090479@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markm
Date: Sun Apr 27 19:00:14 2014
New Revision: 265022
URL: http://svnweb.freebsd.org/changeset/base/265022

Log:
  Remove some debugging printfs, refactor for architectures that have __uint128_t.

Added:
  projects/random_number_generator/sys/dev/random/uint128.h   (contents, props changed)
Modified:
  projects/random_number_generator/sys/dev/random/fortuna.c
  projects/random_number_generator/sys/dev/random/randomdev_soft.c
  projects/random_number_generator/sys/dev/random/yarrow.c

Modified: projects/random_number_generator/sys/dev/random/fortuna.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/fortuna.c	Sun Apr 27 18:57:56 2014	(r265021)
+++ projects/random_number_generator/sys/dev/random/fortuna.c	Sun Apr 27 19:00:14 2014	(r265022)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2013 Mark R V Murray
+ * Copyright (c) 2013-2014 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -49,7 +49,7 @@ __FBSDID("$FreeBSD$");
 #include <dev/random/randomdev.h>
 #include <dev/random/random_adaptors.h>
 #include <dev/random/random_harvestq.h>
-// #include <dev/random/randomdev_soft.h>
+#include <dev/random/uint128.h>
 #include <dev/random/fortuna.h>
 #else /* !_KERNEL */
 #include <sys/param.h>
@@ -66,7 +66,8 @@ __FBSDID("$FreeBSD$");
 #include <crypto/sha2/sha2.h>
 
 #include <dev/random/hash.h>
-#include <dev/random/yarrow.h>
+#include <dev/random/uint128.h>
+#include <dev/random/fortuna.h>
 #endif /* _KERNEL */
 
 #if !defined(RANDOM_YARROW) && !defined(RANDOM_FORTUNA)
@@ -74,6 +75,7 @@ __FBSDID("$FreeBSD$");
 #elif defined(RANDOM_YARROW) && defined(RANDOM_FORTUNA)
 #error "Must define either RANDOM_YARROW or RANDOM_FORTUNA"
 #endif
+
 #if defined(RANDOM_FORTUNA)
 
 #define NPOOLS 32
@@ -82,7 +84,7 @@ __FBSDID("$FreeBSD$");
 #define MAXPOOLSIZE 65536
 
 /* This algorithm (and code) presumes that KEYSIZE is twice as large as BLOCKSIZE */
-CTASSERT(BLOCKSIZE == sizeof(__uint128_t));
+CTASSERT(BLOCKSIZE == sizeof(uint128_t));
 CTASSERT(KEYSIZE == 2*BLOCKSIZE);
 
 /* This is the beastie that needs protecting. It contains all of the
@@ -102,7 +104,7 @@ static struct fortuna_state {
 	/* C - 128 bits */
 	union {
 		uint8_t byte[BLOCKSIZE];
-		__uint128_t whole;
+		uint128_t whole;
 	} counter;
 
 	/* K */
@@ -141,10 +143,6 @@ random_fortuna_init_alg(void)
 	struct sysctl_oid *random_fortuna_o;
 #endif
 
-#ifdef RANDOM_DEBUG
-	printf("random: %s\n", __func__);
-#endif
-
 	memset((void *)(fortuna_start_cache.junk), 0, sizeof(fortuna_start_cache.junk));
 	fortuna_start_cache.length = 0U;
 	randomdev_hash_init(&fortuna_start_cache.hash);
@@ -187,7 +185,7 @@ random_fortuna_init_alg(void)
 	/* F&S - InitializeGenerator() */
 
 	/* F&S - C = 0 */
-	fortuna_state.counter.whole = 0ULL;
+	uint128_clear(&fortuna_state.counter.whole);
 
 	/* F&S - K = 0 */
 	memset((void *)(&fortuna_state.key), 0, sizeof(struct randomdev_key));
@@ -197,9 +195,6 @@ void
 random_fortuna_deinit_alg(void)
 {
 
-#ifdef RANDOM_DEBUG
-	printf("random: %s\n", __func__);
-#endif
 	mtx_destroy(&random_reseed_mtx);
 	memset((void *)(&fortuna_state), 0, sizeof(struct fortuna_state));
 }
@@ -238,9 +233,6 @@ reseed(uint8_t *junk, u_int length)
 	uint8_t hash[KEYSIZE], temp[KEYSIZE];
 
 	KASSERT(fortuna_state.minpoolsize > 0, ("random: Fortuna threshold = 0"));
-#ifdef RANDOM_DEBUG
-	printf("random: %s %d %u\n", __func__, (fortuna_state.counter.whole != 0ULL), length);
-#endif
 #ifdef _KERNEL
 	mtx_assert(&random_reseed_mtx, MA_OWNED);
 #endif
@@ -262,11 +254,10 @@ reseed(uint8_t *junk, u_int length)
 	memset((void *)hash, 0, sizeof(hash));
 
 	/* Unblock the device if it was blocked due to being unseeded */
-	if (fortuna_state.counter.whole == 0ULL)
+	if (uint128_is_zero(fortuna_state.counter.whole))
 		random_adaptor_unblock();
-
 	/* F&S - C = C + 1 */
-	fortuna_state.counter.whole++;
+	uint128_increment(&fortuna_state.counter.whole);
 }
 
 /* F&S - GenerateBlocks() */
@@ -282,7 +273,7 @@ random_fortuna_genblocks(uint8_t *buf, u
 		buf += BLOCKSIZE;
 
 		/* F&S - C = C + 1 */
-		fortuna_state.counter.whole++;
+		uint128_increment(&fortuna_state.counter.whole);
 	}
 }
 
@@ -342,15 +333,9 @@ random_fortuna_read(uint8_t *buf, u_int 
 				/* F&S - ReseedCNT = ReseedCNT + 1 */
 				fortuna_state.reseedcount++;
 				/* s = \epsilon by default */
-#ifdef RANDOM_DEBUG
-				printf("random: active reseed: reseedcount [%d] [", fortuna_state.reseedcount);
-#endif
 				for (i = 0; i < NPOOLS; i++) {
 					/* F&S - if Divides(ReseedCnt, 2^i) ... */
 					if ((fortuna_state.reseedcount % (1 << i)) == 0U) {
-#ifdef RANDOM_DEBUG
-						printf(" %d", i);
-#endif
 						seedlength += KEYSIZE;
 						/* F&S - temp = (P_i) */
 						randomdev_hash_finish(&fortuna_state.pool[i].hash, temp);
@@ -366,8 +351,7 @@ random_fortuna_read(uint8_t *buf, u_int 
 						break;
 				}
 #ifdef RANDOM_DEBUG
-				printf(" ]\n");
-				printf("random: active reseed: ");
+				printf("random: active reseed: reseedcount [%d] ", fortuna_state.reseedcount);
 				for (i = 0; i < NPOOLS; i++)
 					printf(" %d", fortuna_state.pool[i].length);
 				printf("\n");
@@ -438,7 +422,7 @@ int
 random_fortuna_seeded(void)
 {
 
-	return (fortuna_state.counter.whole != 0ULL);
+	return (uint128_is_zero(fortuna_state.counter.whole));
 }
 
 #endif /* RANDOM_FORTUNA */

Modified: projects/random_number_generator/sys/dev/random/randomdev_soft.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/randomdev_soft.c	Sun Apr 27 18:57:56 2014	(r265021)
+++ projects/random_number_generator/sys/dev/random/randomdev_soft.c	Sun Apr 27 19:00:14 2014	(r265022)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2000-2013 Mark R V Murray
+ * Copyright (c) 2000-2014 Mark R V Murray
  * Copyright (c) 2004 Robert N. M. Watson
  * All rights reserved.
  *

Added: projects/random_number_generator/sys/dev/random/uint128.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ projects/random_number_generator/sys/dev/random/uint128.h	Sun Apr 27 19:00:14 2014	(r265022)
@@ -0,0 +1,82 @@
+/*-
+ * Copyright (c) 2014 Mark R V Murray
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef SYS_DEV_RANDOM_UINT128_H_INCLUDED
+#define SYS_DEV_RANDOM_UINT128_H_INCLUDED
+
+/* This whole thing is a crock :-(
+ *
+ * Everyone knows you always need the __uint128_t types!
+ */
+
+#if !defined(__arm__) && !defined(__mips__) && !defined(__i386__) && !defined(__pc98__)
+/* We do have an inbuilt __uint128_t type */
+
+typedef __uint128_t uint128_t;
+#define USE_128_BIT
+
+#else
+
+/* There is no inbuilt __uint128_t type */
+typedef uint64_t uint128_t[2];
+
+#endif
+
+static __inline void
+uint128_clear(uint128_t *big_uint)
+{
+#ifdef USE_128_BIT
+	(*big_uint) = 0ULL;
+#else
+	(*big_uint)[0] = (*big_uint)[1] = 0UL;
+#endif
+}
+
+static __inline void
+uint128_increment(uint128_t *big_uint)
+{
+#ifdef USE_128_BIT
+	(*big_uint)++;
+#else
+	(*big_uint)[0]++;
+	if ((*big_uint)[0] == 0UL)
+		(*big_uint)[1]++;
+#endif
+}
+
+static __inline int
+uint128_is_zero(uint128_t big_uint)
+{
+#ifdef USE_128_BIT
+	return (big_uint == 0ULL);
+#else
+	return (big_uint[0] == 0UL && big_uint[1] == 0UL);
+#endif
+}
+
+#endif /* SYS_DEV_RANDOM_UINT128_H_INCLUDED */

Modified: projects/random_number_generator/sys/dev/random/yarrow.c
==============================================================================
--- projects/random_number_generator/sys/dev/random/yarrow.c	Sun Apr 27 18:57:56 2014	(r265021)
+++ projects/random_number_generator/sys/dev/random/yarrow.c	Sun Apr 27 19:00:14 2014	(r265022)
@@ -49,6 +49,7 @@ __FBSDID("$FreeBSD$");
 #include <dev/random/randomdev.h>
 #include <dev/random/random_adaptors.h>
 #include <dev/random/random_harvestq.h>
+#include <dev/random/uint128.h>
 #include <dev/random/yarrow.h>
 #else /* !_KERNEL */
 #include <sys/param.h>
@@ -66,6 +67,7 @@ __FBSDID("$FreeBSD$");
 
 #include <dev/random/hash.h>
 #include <dev/random/randomdev.h>
+#include <dev/random/uint128.h>
 #include <dev/random/yarrow.h>
 #endif /* _KERNEL */
 
@@ -74,6 +76,7 @@ __FBSDID("$FreeBSD$");
 #elif defined(RANDOM_YARROW) && defined(RANDOM_FORTUNA)
 #error "Must define either RANDOM_YARROW or RANDOM_FORTUNA"
 #endif
+
 #if defined(RANDOM_YARROW)
 
 #define TIMEBIN		16	/* max value for Pt/t */
@@ -82,7 +85,7 @@ __FBSDID("$FreeBSD$");
 #define SLOW		1
 
 /* This algorithm (and code) presumes that KEYSIZE is twice as large as BLOCKSIZE */
-CTASSERT(BLOCKSIZE == sizeof(__uint128_t));
+CTASSERT(BLOCKSIZE == sizeof(uint128_t));
 CTASSERT(KEYSIZE == 2*BLOCKSIZE);
 
 /* This is the beastie that needs protecting. It contains all of the
@@ -92,7 +95,7 @@ CTASSERT(KEYSIZE == 2*BLOCKSIZE);
 static struct yarrow_state {
 	union {
 		uint8_t byte[BLOCKSIZE];
-		__uint128_t whole;
+		uint128_t whole;
 	} counter;			/* C */
 	struct randomdev_key key;	/* K */
 	u_int gengateinterval;		/* Pg */
@@ -139,10 +142,6 @@ random_yarrow_init_alg(void)
 	struct sysctl_oid *random_yarrow_o;
 #endif /* _KERNEL */
 
-#ifdef RANDOM_DEBUG
-	printf("random: %s\n", __func__);
-#endif
-
 	memset((void *)(yarrow_state.start_cache.junk), 0, KEYSIZE);
 	randomdev_hash_init(&yarrow_state.start_cache.hash);
 
@@ -214,16 +213,13 @@ random_yarrow_init_alg(void)
 	}
 
 	/* Clear the counter */
-	yarrow_state.counter.whole = 0ULL;
+	uint128_clear(&yarrow_state.counter.whole);
 }
 
 void
 random_yarrow_deinit_alg(void)
 {
 
-#ifdef RANDOM_DEBUG
-	printf("random: %s\n", __func__);
-#endif
 	mtx_destroy(&random_reseed_mtx);
 	memset((void *)(&yarrow_state), 0, sizeof(struct yarrow_state));
 
@@ -307,22 +303,7 @@ random_yarrow_process_buffer(uint8_t *bu
 		/* Don't do this here - do it in bulk at the end */
 		yarrow_state.pool[pl].source[RANDOM_CACHED].bits += bits;
 #endif
-#ifdef RANDOM_DEBUG_VERBOSE
-		printf("random: %s - ", __func__);
-		printf(" %jX", event.he_somecounter);
-		printf(" %u", event.he_bits);
-		printf(" %u", event.he_source);
-		printf(" %u", event.he_destination);
-		printf(" %u", event.he_size);
-		printf(" %X", *((uint32_t *)(&event.he_entropy)));
-		printf("\n");
-#endif
-
 	}
-#ifdef RANDOM_DEBUG_VERBOSE
-	printf("random: %s - ", __func__);
-	printf(" bit contribution magical guess is %u\n", length >> 4);
-#endif
 	for (pl = FAST; pl <= SLOW; pl++)
 		yarrow_state.pool[pl].source[RANDOM_CACHED].bits += (length >> 4);
 
@@ -407,7 +388,7 @@ reseed(u_int fastslow)
 
 	/* 4. Recompute the counter */
 
-	yarrow_state.counter.whole = 0ULL;
+	uint128_clear(&yarrow_state.counter.whole);
 	randomdev_encrypt(&yarrow_state.key, yarrow_state.counter.byte, temp, BLOCKSIZE);
 	memcpy(yarrow_state.counter.byte, temp, BLOCKSIZE);
 
@@ -460,7 +441,7 @@ random_yarrow_read(uint8_t *buf, u_int b
 			generator_gate();
 			yarrow_state.outputblocks = 0;
 		}
-		yarrow_state.counter.whole++;
+		uint128_increment(&yarrow_state.counter.whole);
 		randomdev_encrypt(&yarrow_state.key, yarrow_state.counter.byte, buf, BLOCKSIZE);
 		buf += BLOCKSIZE;
 	}
@@ -508,12 +489,8 @@ generator_gate(void)
 	u_int i;
 	uint8_t temp[KEYSIZE];
 
-#ifdef RANDOM_DEBUG_VERBOSE
-	printf("random: %s\n", __func__);
-#endif
-
 	for (i = 0; i < KEYSIZE; i += BLOCKSIZE) {
-		yarrow_state.counter.whole++;
+		uint128_increment(&yarrow_state.counter.whole);
 		randomdev_encrypt(&yarrow_state.key, yarrow_state.counter.byte, temp + i, BLOCKSIZE);
 	}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201404271900.s3RJ0EkC090479>