MCPcopy Create free account
hub / github.com/F-Stack/f-stack / initid

Function initid

freebsd/netinet6/ip6_id.c:183–225  ·  view source on GitHub ↗

* Initializes 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 * application does not have to worry about it. */

Source from the content-addressed store, hash-verified

181 * application does not have to worry about it.
182 */
183static void
184initid(struct randomtab *p)
185{
186 u_int32_t j, i;
187 int noprime = 1;
188
189 p->ru_x = arc4random() % p->ru_m;
190
191 /* (bits - 1) bits of random seed */
192 p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1));
193 p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1));
194
195 /* Determine the LCG we use */
196 p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1;
197 p->ru_a = pmod(p->ru_agen,
198 (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
199 while (p->ru_b % 3 == 0)
200 p->ru_b += 2;
201
202 j = arc4random() % p->ru_n;
203
204 /*
205 * Do a fast gcd(j, RU_N - 1), so we can find a j with
206 * gcd(j, RU_N - 1) == 1, giving a new generator for
207 * RU_GEN^j mod RU_N
208 */
209 while (noprime) {
210 for (i = 0; p->pfacts[i] > 0; i++)
211 if (j % p->pfacts[i] == 0)
212 break;
213
214 if (p->pfacts[i] == 0)
215 noprime = 0;
216 else
217 j = (j + 1) % p->ru_n;
218 }
219
220 p->ru_g = pmod(p->ru_gen, j, p->ru_n);
221 p->ru_counter = 0;
222
223 p->ru_reseed = time_uptime + p->ru_out;
224 p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
225}
226
227static u_int32_t
228randomid(struct randomtab *p)

Callers 1

randomidFunction · 0.85

Calls 2

pmodFunction · 0.85
arc4randomFunction · 0.50

Tested by

no test coverage detected