ip_id.c (133720) | ip_id.c (133874) |
---|---|
1/* $OpenBSD: ip_id.c,v 1.2 1999/08/26 13:37:01 provos Exp $ */ 2 3/* 4 * Copyright 1998 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 6 * 7 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using 8 * such a mathematical system to generate more random (yet non-repeating) --- 20 unchanged lines hidden (view full) --- 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * | 1/* $OpenBSD: ip_id.c,v 1.2 1999/08/26 13:37:01 provos Exp $ */ 2 3/* 4 * Copyright 1998 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 6 * 7 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using 8 * such a mathematical system to generate more random (yet non-repeating) --- 20 unchanged lines hidden (view full) --- 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * |
37 * $FreeBSD: head/sys/netinet/ip_id.c 133720 2004-08-14 15:32:40Z dwmalone $ | 37 * $FreeBSD: head/sys/netinet/ip_id.c 133874 2004-08-16 18:32:07Z rwatson $ |
38 */ 39 | 38 */ 39 |
40/* | 40/* |
41 * seed = random 15bit 42 * n = prime, g0 = generator to n, 43 * j = random so that gcd(j,n-1) == 1 44 * g = g0^j mod n will be a generator again. 45 * 46 * X[0] = random seed. 47 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator | 41 * seed = random 15bit 42 * n = prime, g0 = generator to n, 43 * j = random so that gcd(j,n-1) == 1 44 * g = g0^j mod n will be a generator again. 45 * 46 * X[0] = random seed. 47 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator |
48 * with a = 7^(even random) mod m, | 48 * with a = 7^(even random) mod m, |
49 * b = random with gcd(b,m) == 1 50 * m = 31104 and a maximal period of m-1. 51 * 52 * The transaction id is determined by: 53 * id[n] = seed xor (g^X[n] mod n) 54 * 55 * Effectivly the id is restricted to the lower 15 bits, thus 56 * yielding two different cycles by toggling the msb on and off. --- 10 unchanged lines hidden (view full) --- 67#define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ 68#define RU_GEN 2 /* Starting generator */ 69#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ 70#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ 71#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ 72 73#define PFAC_N 3 74const static u_int16_t pfacts[PFAC_N] = { | 49 * b = random with gcd(b,m) == 1 50 * m = 31104 and a maximal period of m-1. 51 * 52 * The transaction id is determined by: 53 * id[n] = seed xor (g^X[n] mod n) 54 * 55 * Effectivly the id is restricted to the lower 15 bits, thus 56 * yielding two different cycles by toggling the msb on and off. --- 10 unchanged lines hidden (view full) --- 67#define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ 68#define RU_GEN 2 /* Starting generator */ 69#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ 70#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ 71#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ 72 73#define PFAC_N 3 74const static u_int16_t pfacts[PFAC_N] = { |
75 2, | 75 2, |
76 3, 77 2729 78}; 79 80static u_int16_t ru_x; 81static u_int16_t ru_seed, ru_seed2; 82static u_int16_t ru_a, ru_b; 83static u_int16_t ru_g; --- 30 unchanged lines hidden (view full) --- 114 if (u & 1) 115 s = (s*t) % mod; 116 u >>= 1; 117 t = (t*t) % mod; 118 } 119 return (s); 120} 121 | 76 3, 77 2729 78}; 79 80static u_int16_t ru_x; 81static u_int16_t ru_seed, ru_seed2; 82static u_int16_t ru_a, ru_b; 83static u_int16_t ru_g; --- 30 unchanged lines hidden (view full) --- 114 if (u & 1) 115 s = (s*t) % mod; 116 u >>= 1; 117 t = (t*t) % mod; 118 } 119 return (s); 120} 121 |
122/* 123 * Initalizes the seed and chooses a suitable generator. Also toggles | 122/* 123 * Initalizes the seed and chooses a suitable generator. Also toggles |
124 * the msb flag. The msb flag is used to generate two distinct 125 * cycles of random numbers and thus avoiding reuse of ids. 126 * | 124 * the msb flag. The msb flag is used to generate two distinct 125 * cycles of random numbers and thus avoiding reuse of ids. 126 * |
127 * This function is called from id_randomid() when needed, an | 127 * This function is called from id_randomid() when needed, an |
128 * application does not have to worry about it. 129 */ | 128 * application does not have to worry about it. 129 */ |
130static void | 130static void |
131ip_initid(void) 132{ 133 u_int16_t j, i; 134 int noprime = 1; 135 struct timeval time; 136 137 getmicrotime(&time); 138 read_random((void *) &tmp, sizeof(tmp)); --- 6 unchanged lines hidden (view full) --- 145 146 read_random((void *) &tmp, sizeof(tmp)); 147 148 /* Determine the LCG we use */ 149 ru_b = (tmp & 0xfffe) | 1; 150 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); 151 while (ru_b % 3 == 0) 152 ru_b += 2; | 131ip_initid(void) 132{ 133 u_int16_t j, i; 134 int noprime = 1; 135 struct timeval time; 136 137 getmicrotime(&time); 138 read_random((void *) &tmp, sizeof(tmp)); --- 6 unchanged lines hidden (view full) --- 145 146 read_random((void *) &tmp, sizeof(tmp)); 147 148 /* Determine the LCG we use */ 149 ru_b = (tmp & 0xfffe) | 1; 150 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); 151 while (ru_b % 3 == 0) 152 ru_b += 2; |
153 | 153 |
154 read_random((void *) &tmp, sizeof(tmp)); 155 j = tmp % RU_N; 156 tmp = tmp >> 16; 157 | 154 read_random((void *) &tmp, sizeof(tmp)); 155 j = tmp % RU_N; 156 tmp = tmp >> 16; 157 |
158 /* | 158 /* |
159 * Do a fast gcd(j,RU_N-1), so we can find a j with 160 * gcd(j, RU_N-1) == 1, giving a new generator for 161 * RU_GEN^j mod RU_N 162 */ 163 164 while (noprime) { 165 for (i=0; i<PFAC_N; i++) 166 if (j%pfacts[i] == 0) 167 break; 168 169 if (i>=PFAC_N) 170 noprime = 0; | 159 * Do a fast gcd(j,RU_N-1), so we can find a j with 160 * gcd(j, RU_N-1) == 1, giving a new generator for 161 * RU_GEN^j mod RU_N 162 */ 163 164 while (noprime) { 165 for (i=0; i<PFAC_N; i++) 166 if (j%pfacts[i] == 0) 167 break; 168 169 if (i>=PFAC_N) 170 noprime = 0; |
171 else | 171 else |
172 j = (j+1) % RU_N; 173 } 174 175 ru_g = pmod(RU_GEN,j,RU_N); 176 ru_counter = 0; 177 178 ru_reseed = time.tv_sec + RU_OUT; | 172 j = (j+1) % RU_N; 173 } 174 175 ru_g = pmod(RU_GEN,j,RU_N); 176 ru_counter = 0; 177 178 ru_reseed = time.tv_sec + RU_OUT; |
179 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; | 179 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; |
180} 181 182u_int16_t 183ip_randomid(void) 184{ 185 int i, n; 186 struct timeval time; 187 --- 22 unchanged lines hidden --- | 180} 181 182u_int16_t 183ip_randomid(void) 184{ 185 int i, n; 186 struct timeval time; 187 --- 22 unchanged lines hidden --- |