diff options
author | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 2003-12-10 07:21:02 +0000 |
---|---|---|
committer | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 2003-12-10 07:21:02 +0000 |
commit | 519d21e04cd9ed70834f541ccb7586f24cac0e95 (patch) | |
tree | 39728d60867607f979023f16933319769c683ecf | |
parent | 9b3c0e63410db24dc1cc8431f9634da692fcab4d (diff) |
correct non-repetitive ID code, based on comments from niels provos.
- seed2 is necessary, but use it as "seed2 + x" not "seed2 ^ x".
- skipping number is not needed, so disable it for 16bit generator (makes
the repetition period to 30000)
-rw-r--r-- | lib/libc/net/res_random.c | 28 | ||||
-rw-r--r-- | sys/netinet/ip_id.c | 8 | ||||
-rw-r--r-- | sys/netinet6/ip6_id.c | 9 |
3 files changed, 25 insertions, 20 deletions
diff --git a/lib/libc/net/res_random.c b/lib/libc/net/res_random.c index c739e4a9521..82d81e038a1 100644 --- a/lib/libc/net/res_random.c +++ b/lib/libc/net/res_random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_random.c,v 1.12 2002/06/27 10:14:02 itojun Exp $ */ +/* $OpenBSD: res_random.c,v 1.13 2003/12/10 07:21:01 itojun Exp $ */ /* * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> @@ -111,9 +111,9 @@ pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod) while (u) { if (u & 1) - s = (s*t) % mod; + s = (s * t) % mod; u >>= 1; - t = (t*t) % mod; + t = (t * t) % mod; } return (s); } @@ -146,7 +146,7 @@ res_initid() ru_b = (tmp & 0xfffe) | 1; ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); while (ru_b % 3 == 0) - ru_b += 2; + ru_b += 2; tmp = arc4random(); j = tmp % RU_N; @@ -159,17 +159,17 @@ res_initid() */ 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 - j = (j+1) % RU_N; + j = (j + 1) % RU_N; } - ru_g = pmod(RU_GEN,j,RU_N); + ru_g = pmod(RU_GEN, j, RU_N); ru_counter = 0; gettimeofday(&tv, NULL); @@ -189,18 +189,22 @@ res_randomid() if (!tmp) tmp = arc4random(); +#if 0 /* Skip a random number of ids */ n = tmp & 0x7; tmp = tmp >> 3; if (ru_counter + n >= RU_MAX) res_initid(); +#else + n = 0; +#endif - for (i=0; i<=n; i++) + for (i = 0; i <= n; i++) /* Linear Congruential Generator */ - ru_x = (ru_a*ru_x + ru_b) % RU_M; + ru_x = (ru_a * ru_x + ru_b) % RU_M; ru_counter += i; - return (ru_seed ^ pmod(ru_g,ru_seed2 ^ ru_x,RU_N)) | ru_msb; + return (ru_seed ^ pmod(ru_g, ru_seed2 + ru_x, RU_N)) | ru_msb; } #if 0 diff --git a/sys/netinet/ip_id.c b/sys/netinet/ip_id.c index 0534d406e33..67208b89de9 100644 --- a/sys/netinet/ip_id.c +++ b/sys/netinet/ip_id.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_id.c,v 1.7 2003/09/21 04:06:39 itojun Exp $ */ +/* $OpenBSD: ip_id.c,v 1.8 2003/12/10 07:21:00 itojun Exp $ */ /* * Copyright 1998 Niels Provos <provos@citi.umich.edu> @@ -174,10 +174,14 @@ ip_randomid(void) if (!tmp) tmp = arc4random(); +#if 0 /* Skip a random number of ids */ n = tmp & 0x3; tmp = tmp >> 2; if (ru_counter + n >= RU_MAX) ip_initid(); +#else + n = 0; +#endif for (i = 0; i <= n; i++) /* Linear Congruential Generator */ @@ -185,5 +189,5 @@ ip_randomid(void) ru_counter += i; - return (ru_seed ^ pmod(ru_g,ru_seed2 ^ ru_x,RU_N)) | ru_msb; + return (ru_seed ^ pmod(ru_g,ru_seed2 + ru_x, RU_N)) | ru_msb; } diff --git a/sys/netinet6/ip6_id.c b/sys/netinet6/ip6_id.c index e509a6364a6..5b6151a246a 100644 --- a/sys/netinet6/ip6_id.c +++ b/sys/netinet6/ip6_id.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip6_id.c,v 1.1 2003/10/01 21:41:05 itojun Exp $ */ +/* $OpenBSD: ip6_id.c,v 1.2 2003/12/10 07:21:01 itojun Exp $ */ /* $NetBSD: ip6_id.c,v 1.7 2003/09/13 21:32:59 itojun Exp $ */ /* $KAME: ip6_id.c,v 1.8 2003/09/06 13:41:06 itojun Exp $ */ @@ -223,15 +223,12 @@ static u_int32_t randomid(struct randomtab *p) { int i, n; - u_int32_t tmp; if (p->ru_counter >= p->ru_max || time.tv_sec > p->ru_reseed) initid(p); - tmp = arc4random(); - /* Skip a random number of ids */ - n = tmp & 0x3; tmp = tmp >> 2; + n = arc4random() & 0x3; if (p->ru_counter + n >= p->ru_max) initid(p); @@ -242,7 +239,7 @@ randomid(struct randomtab *p) p->ru_counter += i; - return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) | + return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) | p->ru_msb; } |