summaryrefslogtreecommitdiff
path: root/sys/netinet/ip_id.c
diff options
context:
space:
mode:
authorMichael Shalayeff <mickey@cvs.openbsd.org>2001-06-04 19:57:23 +0000
committerMichael Shalayeff <mickey@cvs.openbsd.org>2001-06-04 19:57:23 +0000
commit589854a6adba054640dbf7ca08c5b3239813eaa6 (patch)
tree478ec5de5beb5ba69045e7d82e30683b9f0a0419 /sys/netinet/ip_id.c
parentf865dd3621cdaa5d9c4266ba12affc94f4d2b67f (diff)
use faster arc4random() for small amounts fo data, some spaces; niels ok
Diffstat (limited to 'sys/netinet/ip_id.c')
-rw-r--r--sys/netinet/ip_id.c63
1 files changed, 29 insertions, 34 deletions
diff --git a/sys/netinet/ip_id.c b/sys/netinet/ip_id.c
index 4f03a48d1a1..63fdf033485 100644
--- a/sys/netinet/ip_id.c
+++ b/sys/netinet/ip_id.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ip_id.c,v 1.2 1999/08/26 13:37:01 provos Exp $ */
+/* $OpenBSD: ip_id.c,v 1.3 2001/06/04 19:57:22 mickey Exp $ */
/*
* Copyright 1998 Niels Provos <provos@citi.umich.edu>
@@ -35,7 +35,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-/*
+/*
* seed = random 15bit
* n = prime, g0 = generator to n,
* j = random so that gcd(j,n-1) == 1
@@ -43,7 +43,7 @@
*
* X[0] = random seed.
* X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
- * with a = 7^(even random) mod m,
+ * with a = 7^(even random) mod m,
* b = random with gcd(b,m) == 1
* m = 31104 and a maximal period of m-1.
*
@@ -61,16 +61,16 @@
#include <dev/rndvar.h>
-#define RU_OUT 180 /* Time after wich will be reseeded */
+#define RU_OUT 180 /* Time after wich will be reseeded */
#define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */
#define RU_GEN 2 /* Starting generator */
#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */
-#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */
-#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */
+#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */
+#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */
#define PFAC_N 3
const static u_int16_t pfacts[PFAC_N] = {
- 2,
+ 2,
3,
2729
};
@@ -82,7 +82,7 @@ static u_int16_t ru_g;
static u_int16_t ru_counter = 0;
static u_int16_t ru_msb = 0;
static long ru_reseed;
-static u_int32_t tmp; /* Storage for unused random */
+static u_int32_t tmp; /* Storage for unused random */
static u_int16_t pmod __P((u_int16_t, u_int16_t, u_int16_t));
static void ip_initid __P((void));
@@ -117,54 +117,49 @@ pmod(gen, exp, mod)
return (s);
}
-/*
- * Initalizes the seed and chooses a suitable generator. Also toggles
+/*
+ * Initalizes the seed and chooses a suitable generator. Also toggles
* the msb flag. The msb flag is used to generate two distinct
* cycles of random numbers and thus avoiding reuse of ids.
*
- * This function is called from id_randomid() when needed, an
+ * This function is called from id_randomid() when needed, an
* application does not have to worry about it.
*/
-static void
+static void
ip_initid(void)
{
u_int16_t j, i;
int noprime = 1;
- get_random_bytes((void *) &tmp, sizeof(tmp));
- ru_x = (tmp & 0xFFFF) % RU_M;
+ ru_x = ((tmp = arc4random()) & 0xFFFF) % RU_M;
/* 15 bits of random seed */
ru_seed = (tmp >> 16) & 0x7FFF;
- get_random_bytes((void *) &tmp, sizeof(tmp));
- ru_seed2 = tmp & 0x7FFF;
-
- get_random_bytes((void *) &tmp, sizeof(tmp));
+ ru_seed2 = arc4random() & 0x7FFF;
/* Determine the LCG we use */
- ru_b = (tmp & 0xfffe) | 1;
+ ru_b = ((tmp = arc4random()) & 0xfffe) | 1;
ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
while (ru_b % 3 == 0)
- ru_b += 2;
-
- get_random_bytes((void *) &tmp, sizeof(tmp));
- j = tmp % RU_N;
+ ru_b += 2;
+
+ j = (tmp = arc4random()) % RU_N;
tmp = tmp >> 16;
- /*
+ /*
* Do a fast gcd(j,RU_N-1), so we can find a j with
* gcd(j, RU_N-1) == 1, giving a new generator for
* RU_GEN^j mod RU_N
*/
while (noprime) {
- for (i=0; i<PFAC_N; i++)
- if (j%pfacts[i] == 0)
+ for (i = 0; i < PFAC_N; i++)
+ if (j % pfacts[i] == 0)
break;
- if (i>=PFAC_N)
+ if (i >= PFAC_N)
noprime = 0;
- else
+ else
j = (j+1) % RU_N;
}
@@ -172,28 +167,28 @@ ip_initid(void)
ru_counter = 0;
ru_reseed = time.tv_sec + RU_OUT;
- ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
+ ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
}
u_int16_t
ip_randomid(void)
{
- int i, n;
+ int i, n;
if (ru_counter >= RU_MAX || time.tv_sec > ru_reseed)
ip_initid();
if (!tmp)
- get_random_bytes((void *) &tmp, sizeof(tmp));
+ tmp = arc4random();
/* Skip a random number of ids */
n = tmp & 0x3; tmp = tmp >> 2;
if (ru_counter + n >= RU_MAX)
- ip_initid();
+ ip_initid();
for (i = 0; i <= n; i++)
- /* Linear Congruential Generator */
- ru_x = (ru_a*ru_x + ru_b) % RU_M;
+ /* Linear Congruential Generator */
+ ru_x = (ru_a * ru_x + ru_b) % RU_M;
ru_counter += i;