[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freewrt-commits
Subject:    [FreeWRT-commits] r3977 - branches/freewrt_1_0/package/rdate
From:       tg () freewrt ! org
Date:       2011-12-27 18:48:31
Message-ID: 20111227184831.67EF81C003 () frozenfish ! freewrt ! org
[Download RAW message or body]

Author: tg
Date: 2011-12-27 19:48:29 +0100 (Tue, 27 Dec 2011)
New Revision: 3977

Modified:
   branches/freewrt_1_0/package/rdate/Makefile
   branches/freewrt_1_0/package/rdate/arc4random.c
   trunk/freewrt/package/rdate/Makefile
   trunk/freewrt/toolchain/uClibc/files/arc4random.c
Log:
another long overdue update from MirBSD


Modified: branches/freewrt_1_0/package/rdate/Makefile
===================================================================
--- branches/freewrt_1_0/package/rdate/Makefile	2011-12-27 18:39:32 UTC (rev 3976)
+++ branches/freewrt_1_0/package/rdate/Makefile	2011-12-27 18:48:29 UTC (rev 3977)
@@ -7,11 +7,13 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME=		rdate
-PKG_VERSION=		20090802
+PKG_VERSION=		20100805
 PKG_RELEASE=		1
-PKG_MD5SUM=		a8fa4550b5a77cff6db1ed0a9d8aa357
+PKG_MD5SUM=		69f5e1e0f08c300d8a211acc2b2cee5f
 PKG_SOURCE_URL=		http://www.mirbsd.org/MirOS/dist/mir/rdate/
 
+WRKSRC=			${WRKDIR}/rdate
+
 include $(TOPDIR)/mk/package.mk
 
 $(eval $(call PKG_template,RDATE,rdate,$(PKG_VERSION)-$(PKG_RELEASE),$(ARCH)))

Modified: branches/freewrt_1_0/package/rdate/arc4random.c
===================================================================
--- branches/freewrt_1_0/package/rdate/arc4random.c	2011-12-27 18:39:32 UTC (rev \
                3976)
+++ branches/freewrt_1_0/package/rdate/arc4random.c	2011-12-27 18:48:29 UTC (rev \
3977) @@ -1,5 +1,5 @@
 static const char __vcsid[] = "@(#) MirOS contributed arc4random.c (old)"
-    "\n	@(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.19 \
2009/09/27 10:45:56 tg Exp $" +    "\n	@(#)rcsid_master: $MirOS: \
contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $"  ;
 
 /*-
@@ -32,7 +32,7 @@
  */
 
 /*-
- * Copyright (c) 2008, 2009
+ * Copyright (c) 2008, 2009, 2010
  *	Thorsten Glaser <tg@mirbsd.org>
  * This is arc4random(3) made more portable,
  * as well as arc4random_pushb(3) for Cygwin.
@@ -116,7 +116,7 @@
 static const char __randomdev[] = _PATH_URANDOM;
 
 static uint8_t arc4_getbyte(void);
-static void stir_finish(int);
+static void stir_finish(uint8_t);
 static void arc4_atexit(void);
 static char arc4_writeback(uint8_t *, size_t, char);
 
@@ -124,11 +124,23 @@
 u_int32_t arc4random(void);
 void arc4random_addrandom(u_char *, int);
 void arc4random_stir(void);
-#ifdef USE_MS_CRYPTOAPI
+#if defined(USE_MS_CRYPTOAPI) || defined(OPPORTUNISTIC_ROOT_PUSHB)
 uint32_t arc4random_pushb(const void *, size_t);
 #endif
 #endif
 
+#define NEED_UNIFORM_BUF_PROTO
+#if defined(__OpenBSD__) && defined(OpenBSD) && (OpenBSD > 200805)
+#undef NEED_UNIFORM_BUF_PROTO
+#elif defined(__MirBSD__) && defined(MirBSD) && (MirBSD > 0x0AA4)
+#undef NEED_UNIFORM_BUF_PROTO
+#endif
+
+#ifdef NEED_UNIFORM_BUF_PROTO
+u_int32_t arc4random_uniform(u_int32_t);
+void arc4random_buf(void *, size_t);
+#endif
+
 static void
 arc4_init(void)
 {
@@ -141,22 +153,39 @@
 }
 
 static void
-arc4_addrandom(u_char *dat, int datlen)
+arc4_addrandom(const u_char *dat, size_t datlen)
 {
-	int n;
+	size_t n = 0;
 	uint8_t si;
 
 	arc4_ctx.i--;
-	for (n = 0; n < 256; n++) {
+	while (n < 256) {
 		arc4_ctx.i++;
 		si = arc4_ctx.s[arc4_ctx.i];
-		arc4_ctx.j = (uint8_t)(arc4_ctx.j + si + dat[n % datlen]);
+		arc4_ctx.j = (uint8_t)(arc4_ctx.j + si + dat[n++ % datlen]);
 		arc4_ctx.s[arc4_ctx.i] = arc4_ctx.s[arc4_ctx.j];
 		arc4_ctx.s[arc4_ctx.j] = si;
 	}
+	arc4_ctx.i++;
 	arc4_ctx.j = arc4_ctx.i;
 }
 
+#if defined(USE_MS_CRYPTOAPI)
+#define RNDEV_BYTES	128
+#elif defined(__INTERIX)
+#define RNDEV_BYTES	4	/* slow /dev/urandom */
+#elif defined(__OpenBSD__)
+#define RNDEV_BYTES	(256 - (sizeof(struct timeval) + sizeof(pid_t)))
+#elif defined(__CYGWIN__)
+#define RNDEV_BYTES	64	/* /dev/urandom probably CryptoAPI */
+#elif defined(__FreeBSD__)
+#define RNDEV_BYTES	16	/* Yarrow has few state */
+#elif defined(__GLIBC__)
+#define RNDEV_BYTES	16	/* requested by maintainers */
+#else
+#define RNDEV_BYTES	8	/* unknown OS? */
+#endif
+
 static void
 arc4_stir(void)
 {
@@ -164,7 +193,7 @@
 	struct {
 		struct timeval tv;
 		pid_t pid;
-		u_int rnd[(128 - (sizeof(struct timeval) + sizeof(pid_t))) / sizeof(u_int)];
+		u_int rnd[(RNDEV_BYTES + sizeof(u_int) - 1) / sizeof(u_int)];
 	} rdat;
 	size_t sz = 0;
 
@@ -232,7 +261,7 @@
 }
 
 static void
-stir_finish(int av)
+stir_finish(uint8_t av)
 {
 	size_t n;
 	uint8_t tb[16];
@@ -245,18 +274,17 @@
 	 * We discard 256 words. A long word is 4 bytes.
 	 * We also discard a randomly fuzzed amount.
 	 */
-	n = 256 * 4 + (arc4_getbyte() & 0x0FU);
-	while (av) {
-		n += (av & 0x0F);
-		av >>= 4;
-	}
+	n = 256 * 4 + (arc4_getbyte() & 0x0FU) + (av & 0xF0U);
+	av &= 0x0FU;
 	while (n--)
 		arc4_getbyte();
-	while (n < sizeof(tb))
-		tb[n++] = arc4_getbyte();
+	while (++n < sizeof(tb))
+		tb[n] = arc4_getbyte();
 	if (arc4_writeback(tb, sizeof(tb), 0))
 		arc4_getbyte();
-	arc4_count = 400000;
+	while (av--)
+		arc4_getbyte();
+	arc4_count = 1600000;
 }
 
 static uint8_t
@@ -306,7 +334,8 @@
 u_int32_t
 arc4random(void)
 {
-	if (--arc4_count == 0 || !rs_initialized || arc4_stir_pid != getpid())
+	arc4_count -= 4;
+	if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid())
 		arc4random_stir();
 	return arc4_getword();
 }
@@ -453,17 +482,22 @@
 #endif
 }
 
-#if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk)
+#if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk) || \
+    defined(OPPORTUNISTIC_ROOT_PUSHB)
 uint32_t
 arc4random_pushb(const void *src, size_t len)
 {
 	size_t rlen;
 	union {
 		uint8_t buf[256];
-		struct timeval tv;
+		struct {
+			struct timeval tv;
+			const void *sp, *dp;
+			size_t sz;
+			uint32_t vu;
+		} s;
 		uint32_t xbuf;
 	} idat;
-	const uint8_t *cbuf = (const uint8_t *)src;
 	uint32_t res = 1;
 
 	if (!rs_initialized) {
@@ -471,19 +505,29 @@
 		rs_initialized = 1;
 	}
 
-	gettimeofday(&idat.tv, NULL);
-	for (rlen = 0; rlen < len; ++rlen)
-		idat.buf[rlen % sizeof(idat)] ^= cbuf[rlen];
-	rlen = MIN(sizeof(idat), MAX(sizeof(struct timeval), len));
+	idat.s.sp = &idat;
+	idat.s.dp = src;
+	idat.s.sz = len;
+	idat.s.vu = arc4_getword();
+	gettimeofday(&idat.s.tv, NULL);
 
-	if (arc4_writeback(&idat.buf[0], rlen, 1))
+	rlen = MAX(sizeof(idat.s), len);
+	while (rlen--)
+		idat.buf[rlen % sizeof(idat.buf)] ^=
+		    ((const uint8_t *)src)[rlen % len];
+	rlen = MIN(sizeof(idat), MAX(sizeof(idat.s), len));
+
+	if (arc4_writeback((void *)&idat, rlen, 1))
 		res = 0;
-	arc4_addrandom(&idat.buf[0], rlen);
+	arc4_addrandom((void *)&idat, rlen);
+	rlen = arc4_getbyte() & 1;
 	if (res)
 		res = idat.xbuf;
 	else
 		/* we got entropy from the kernel, so consider us stirred */
 		stir_finish(idat.buf[5]);
+	if (rlen)
+		(void)arc4_getbyte();
 	return (res ^ arc4_getword());
 }
 #endif
@@ -505,3 +549,74 @@
 
 	arc4_writeback((uint8_t *)&buf, sizeof(buf), 0);
 }
+
+void
+arc4random_buf(void *_buf, size_t n)
+{
+	uint8_t *buf = (uint8_t *)_buf;
+
+	if (!rs_initialized || arc4_stir_pid != getpid())
+		arc4random_stir();
+	buf[0] = arc4_getbyte() % 3;
+	while (buf[0]--)
+		(void)arc4_getbyte();
+	while (n--) {
+		if (--arc4_count <= 0)
+			arc4_stir();
+		buf[n] = arc4_getbyte();
+	}
+}
+
+/*-
+ * Written by Damien Miller.
+ * With simplifications by Jinmei Tatuya.
+ */
+
+/*
+ * Calculate a uniformly distributed random number less than
+ * upper_bound avoiding "modulo bias".
+ *
+ * Uniformity is achieved by generating new random numbers
+ * until the one returned is outside the range
+ * [0, 2^32 % upper_bound[. This guarantees the selected
+ * random number will be inside the range
+ * [2^32 % upper_bound, 2^32[ which maps back to
+ * [0, upper_bound[ after reduction modulo upper_bound.
+ */
+uint32_t
+arc4random_uniform(uint32_t upper_bound)
+{
+	uint32_t r, min;
+
+	if (upper_bound < 2)
+		return (0);
+
+#if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL)
+	min = 0x100000000UL % upper_bound;
+#else
+	/* calculate (2^32 % upper_bound) avoiding 64-bit math */
+	if (upper_bound > 0x80000000U)
+		/* 2^32 - upper_bound (only one "value area") */
+		min = 1 + ~upper_bound;
+	else
+		/* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
+		min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
+#endif
+
+	/*
+	 * This could theoretically loop forever but each retry has
+	 * p > 0.5 (worst case, usually far better) of selecting a
+	 * number inside the range we need, so it should rarely need
+	 * to re-roll (at all).
+	 */
+	arc4_count -= 4;
+	if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0)
+		arc4random_stir();
+	if (arc4_getbyte() & 1)
+		(void)arc4_getbyte();
+	do {
+		r = arc4_getword();
+	} while (r < min);
+
+	return (r % upper_bound);
+}

Modified: trunk/freewrt/package/rdate/Makefile
===================================================================
--- trunk/freewrt/package/rdate/Makefile	2011-12-27 18:39:32 UTC (rev 3976)
+++ trunk/freewrt/package/rdate/Makefile	2011-12-27 18:48:29 UTC (rev 3977)
@@ -7,9 +7,9 @@
 include ${TOPDIR}/rules.mk
 
 PKG_NAME=		rdate
-PKG_VERSION=		20091122
+PKG_VERSION=		20100805
 PKG_RELEASE=		1
-PKG_MD5SUM=		4914cdcf82cc6e8b30248fd6ea9e827f
+PKG_MD5SUM=		69f5e1e0f08c300d8a211acc2b2cee5f
 MASTER_SITES=		${MASTER_SITE_MIRBSD:distfiles/=dist/mir/rdate/}
 WRKDIST=		${WRKDIR}/${PKG_NAME}
 

Modified: trunk/freewrt/toolchain/uClibc/files/arc4random.c
===================================================================
--- trunk/freewrt/toolchain/uClibc/files/arc4random.c	2011-12-27 18:39:32 UTC (rev \
                3976)
+++ trunk/freewrt/toolchain/uClibc/files/arc4random.c	2011-12-27 18:48:29 UTC (rev \
3977) @@ -1,5 +1,5 @@
 static const char __vcsid[] = "@(#) MirOS contributed arc4random.c (old)"
-    "\n	@(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.24 \
2009/11/29 18:24:21 tg Exp $" +    "\n	@(#)rcsid_master: $MirOS: \
contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $"  ;
 
 /*-
@@ -32,7 +32,7 @@
  */
 
 /*-
- * Copyright (c) 2008, 2009
+ * Copyright (c) 2008, 2009, 2010
  *	Thorsten Glaser <tg@mirbsd.org>
  * This is arc4random(3) made more portable,
  * as well as arc4random_pushb(3) for Cygwin.
@@ -166,6 +166,7 @@
 		arc4_ctx.s[arc4_ctx.i] = arc4_ctx.s[arc4_ctx.j];
 		arc4_ctx.s[arc4_ctx.j] = si;
 	}
+	arc4_ctx.i++;
 	arc4_ctx.j = arc4_ctx.i;
 }
 
@@ -277,8 +278,8 @@
 	av &= 0x0FU;
 	while (n--)
 		arc4_getbyte();
-	while (n < sizeof(tb))
-		tb[n++] = arc4_getbyte();
+	while (++n < sizeof(tb))
+		tb[n] = arc4_getbyte();
 	if (arc4_writeback(tb, sizeof(tb), 0))
 		arc4_getbyte();
 	while (av--)
@@ -566,54 +567,56 @@
 	}
 }
 
+/*-
+ * Written by Damien Miller.
+ * With simplifications by Jinmei Tatuya.
+ */
+
 /*
- * Calculate a uniformly distributed random number less than upper_bound
- * avoiding "modulo bias".
+ * Calculate a uniformly distributed random number less than
+ * upper_bound avoiding "modulo bias".
  *
- * Uniformity is achieved by generating new random numbers until the one
- * returned is outside the range [0, 2**32 % upper_bound).  This
- * guarantees the selected random number will be inside
- * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
- * after reduction modulo upper_bound.
+ * Uniformity is achieved by generating new random numbers
+ * until the one returned is outside the range
+ * [0, 2^32 % upper_bound[. This guarantees the selected
+ * random number will be inside the range
+ * [2^32 % upper_bound, 2^32[ which maps back to
+ * [0, upper_bound[ after reduction modulo upper_bound.
  */
-u_int32_t
-arc4random_uniform(u_int32_t upper_bound)
+uint32_t
+arc4random_uniform(uint32_t upper_bound)
 {
-	u_int32_t r, min;
+	uint32_t r, min;
 
 	if (upper_bound < 2)
 		return (0);
 
-#if defined(ULONG_MAX) && (ULONG_MAX > 0xffffffffUL)
+#if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL)
 	min = 0x100000000UL % upper_bound;
 #else
-	/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
-	if (upper_bound > 0x80000000)
-		min = 1 + ~upper_bound;		/* 2**32 - upper_bound */
-	else {
-		/* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
-		min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
-	}
+	/* calculate (2^32 % upper_bound) avoiding 64-bit math */
+	if (upper_bound > 0x80000000U)
+		/* 2^32 - upper_bound (only one "value area") */
+		min = 1 + ~upper_bound;
+	else
+		/* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
+		min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
 #endif
 
 	/*
 	 * This could theoretically loop forever but each retry has
 	 * p > 0.5 (worst case, usually far better) of selecting a
 	 * number inside the range we need, so it should rarely need
-	 * to re-roll.
+	 * to re-roll (at all).
 	 */
-	if (!rs_initialized || arc4_stir_pid != getpid())
+	arc4_count -= 4;
+	if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0)
 		arc4random_stir();
 	if (arc4_getbyte() & 1)
 		(void)arc4_getbyte();
-	for (;;) {
-		arc4_count -= 4;
-		if (arc4_count <= 0)
-			arc4random_stir();
+	do {
 		r = arc4_getword();
-		if (r >= min)
-			break;
-	}
+	} while (r < min);
 
 	return (r % upper_bound);
 }

_______________________________________________
freewrt-commits mailing list
freewrt-commits@freewrt.org
https://www.freewrt.org/lists/listinfo/freewrt-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic