1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions * Copyright (c) 2009-2019 Apple Inc. All rights reserved.
3*699cd480SApple OSS Distributions *
4*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions *
6*699cd480SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions *
15*699cd480SApple OSS Distributions * Please obtain a copy of the License at
16*699cd480SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions *
18*699cd480SApple OSS Distributions * The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions * Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions * limitations under the License.
25*699cd480SApple OSS Distributions *
26*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions */
28*699cd480SApple OSS Distributions
29*699cd480SApple OSS Distributions /*-
30*699cd480SApple OSS Distributions * Copyright (C) 2003 WIDE Project.
31*699cd480SApple OSS Distributions * All rights reserved.
32*699cd480SApple OSS Distributions *
33*699cd480SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
34*699cd480SApple OSS Distributions * modification, are permitted provided that the following conditions
35*699cd480SApple OSS Distributions * are met:
36*699cd480SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
37*699cd480SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
38*699cd480SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
39*699cd480SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
40*699cd480SApple OSS Distributions * documentation and/or other materials provided with the distribution.
41*699cd480SApple OSS Distributions * 3. Neither the name of the project nor the names of its contributors
42*699cd480SApple OSS Distributions * may be used to endorse or promote products derived from this software
43*699cd480SApple OSS Distributions * without specific prior written permission.
44*699cd480SApple OSS Distributions *
45*699cd480SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46*699cd480SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47*699cd480SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48*699cd480SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49*699cd480SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50*699cd480SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51*699cd480SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52*699cd480SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53*699cd480SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54*699cd480SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55*699cd480SApple OSS Distributions * SUCH DAMAGE.
56*699cd480SApple OSS Distributions */
57*699cd480SApple OSS Distributions
58*699cd480SApple OSS Distributions /*-
59*699cd480SApple OSS Distributions * Copyright 1998 Niels Provos <[email protected]>
60*699cd480SApple OSS Distributions * All rights reserved.
61*699cd480SApple OSS Distributions *
62*699cd480SApple OSS Distributions * Theo de Raadt <[email protected]> came up with the idea of using
63*699cd480SApple OSS Distributions * such a mathematical system to generate more random (yet non-repeating)
64*699cd480SApple OSS Distributions * ids to solve the resolver/named problem. But Niels designed the
65*699cd480SApple OSS Distributions * actual system based on the constraints.
66*699cd480SApple OSS Distributions *
67*699cd480SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
68*699cd480SApple OSS Distributions * modification, are permitted provided that the following conditions
69*699cd480SApple OSS Distributions * are met:
70*699cd480SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
71*699cd480SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
72*699cd480SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
73*699cd480SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
74*699cd480SApple OSS Distributions * documentation and/or other materials provided with the distribution.
75*699cd480SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
76*699cd480SApple OSS Distributions * must display the following acknowledgement:
77*699cd480SApple OSS Distributions * This product includes software developed by Niels Provos.
78*699cd480SApple OSS Distributions * 4. The name of the author may not be used to endorse or promote products
79*699cd480SApple OSS Distributions * derived from this software without specific prior written permission.
80*699cd480SApple OSS Distributions *
81*699cd480SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
82*699cd480SApple OSS Distributions * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83*699cd480SApple OSS Distributions * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84*699cd480SApple OSS Distributions * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
85*699cd480SApple OSS Distributions * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
86*699cd480SApple OSS Distributions * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
87*699cd480SApple OSS Distributions * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
88*699cd480SApple OSS Distributions * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
89*699cd480SApple OSS Distributions * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
90*699cd480SApple OSS Distributions * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91*699cd480SApple OSS Distributions */
92*699cd480SApple OSS Distributions
93*699cd480SApple OSS Distributions #include <sys/cdefs.h>
94*699cd480SApple OSS Distributions
95*699cd480SApple OSS Distributions /*
96*699cd480SApple OSS Distributions * seed = random (bits - 1) bit
97*699cd480SApple OSS Distributions * n = prime, g0 = generator to n,
98*699cd480SApple OSS Distributions * j = random so that gcd(j,n-1) == 1
99*699cd480SApple OSS Distributions * g = g0^j mod n will be a generator again.
100*699cd480SApple OSS Distributions *
101*699cd480SApple OSS Distributions * X[0] = random seed.
102*699cd480SApple OSS Distributions * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
103*699cd480SApple OSS Distributions * with a = 7^(even random) mod m,
104*699cd480SApple OSS Distributions * b = random with gcd(b,m) == 1
105*699cd480SApple OSS Distributions * m = constant and a maximal period of m-1.
106*699cd480SApple OSS Distributions *
107*699cd480SApple OSS Distributions * The transaction id is determined by:
108*699cd480SApple OSS Distributions * id[n] = seed xor (g^X[n] mod n)
109*699cd480SApple OSS Distributions *
110*699cd480SApple OSS Distributions * Effectivly the id is restricted to the lower (bits - 1) bits, thus
111*699cd480SApple OSS Distributions * yielding two different cycles by toggling the msb on and off.
112*699cd480SApple OSS Distributions * This avoids reuse issues caused by reseeding.
113*699cd480SApple OSS Distributions */
114*699cd480SApple OSS Distributions
115*699cd480SApple OSS Distributions #include <sys/types.h>
116*699cd480SApple OSS Distributions #include <sys/socket.h>
117*699cd480SApple OSS Distributions #include <sys/param.h>
118*699cd480SApple OSS Distributions #include <sys/time.h>
119*699cd480SApple OSS Distributions #include <sys/kernel.h>
120*699cd480SApple OSS Distributions #include <sys/random.h>
121*699cd480SApple OSS Distributions #include <sys/protosw.h>
122*699cd480SApple OSS Distributions #include <libkern/libkern.h>
123*699cd480SApple OSS Distributions #include <dev/random/randomdev.h>
124*699cd480SApple OSS Distributions
125*699cd480SApple OSS Distributions #include <net/if.h>
126*699cd480SApple OSS Distributions #include <net/route.h>
127*699cd480SApple OSS Distributions #include <netinet/in.h>
128*699cd480SApple OSS Distributions #include <netinet/ip6.h>
129*699cd480SApple OSS Distributions #include <netinet6/ip6_var.h>
130*699cd480SApple OSS Distributions
131*699cd480SApple OSS Distributions struct randomtab {
132*699cd480SApple OSS Distributions const int ru_bits; /* resulting bits */
133*699cd480SApple OSS Distributions const long ru_out; /* Time after wich will be reseeded */
134*699cd480SApple OSS Distributions const u_int32_t ru_max; /* Uniq cycle, avoid blackjack prediction */
135*699cd480SApple OSS Distributions const u_int32_t ru_gen; /* Starting generator */
136*699cd480SApple OSS Distributions const u_int32_t ru_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */
137*699cd480SApple OSS Distributions const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */
138*699cd480SApple OSS Distributions const u_int32_t ru_m; /* ru_m = 2^x*3^y */
139*699cd480SApple OSS Distributions const u_int32_t pfacts[4]; /* factors of ru_n */
140*699cd480SApple OSS Distributions
141*699cd480SApple OSS Distributions u_int32_t ru_counter;
142*699cd480SApple OSS Distributions u_int32_t ru_msb;
143*699cd480SApple OSS Distributions
144*699cd480SApple OSS Distributions u_int32_t ru_x;
145*699cd480SApple OSS Distributions u_int32_t ru_seed, ru_seed2;
146*699cd480SApple OSS Distributions u_int32_t ru_a, ru_b;
147*699cd480SApple OSS Distributions u_int32_t ru_g;
148*699cd480SApple OSS Distributions long ru_reseed;
149*699cd480SApple OSS Distributions };
150*699cd480SApple OSS Distributions
151*699cd480SApple OSS Distributions static struct randomtab randomtab_32 = {
152*699cd480SApple OSS Distributions .ru_bits = 32, /* resulting bits */
153*699cd480SApple OSS Distributions .ru_out = 180, /* Time after wich will be reseeded */
154*699cd480SApple OSS Distributions .ru_max = 1000000000, /* Uniq cycle, avoid blackjack prediction */
155*699cd480SApple OSS Distributions .ru_gen = 2, /* Starting generator */
156*699cd480SApple OSS Distributions .ru_n = 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */
157*699cd480SApple OSS Distributions .ru_agen = 7, /* determine ru_a as RU_AGEN^(2*rand) */
158*699cd480SApple OSS Distributions .ru_m = 1836660096, /* RU_M = 2^7*3^15 - don't change */
159*699cd480SApple OSS Distributions .pfacts = { 2, 3, 59652323, 0 }, /* factors of ru_n */
160*699cd480SApple OSS Distributions .ru_counter = 0,
161*699cd480SApple OSS Distributions .ru_msb = 0,
162*699cd480SApple OSS Distributions .ru_x = 0,
163*699cd480SApple OSS Distributions .ru_seed = 0,
164*699cd480SApple OSS Distributions .ru_seed2 = 0,
165*699cd480SApple OSS Distributions .ru_a = 0,
166*699cd480SApple OSS Distributions .ru_b = 0,
167*699cd480SApple OSS Distributions .ru_g = 0,
168*699cd480SApple OSS Distributions .ru_reseed = 0
169*699cd480SApple OSS Distributions };
170*699cd480SApple OSS Distributions
171*699cd480SApple OSS Distributions static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
172*699cd480SApple OSS Distributions static void initid(struct randomtab *);
173*699cd480SApple OSS Distributions static u_int32_t randomid(struct randomtab *);
174*699cd480SApple OSS Distributions
175*699cd480SApple OSS Distributions /*
176*699cd480SApple OSS Distributions * Do a fast modular exponation, returned value will be in the range
177*699cd480SApple OSS Distributions * of 0 - (mod-1)
178*699cd480SApple OSS Distributions */
179*699cd480SApple OSS Distributions static u_int32_t
pmod(u_int32_t gen,u_int32_t expo,u_int32_t mod)180*699cd480SApple OSS Distributions pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
181*699cd480SApple OSS Distributions {
182*699cd480SApple OSS Distributions u_int64_t s, t, u;
183*699cd480SApple OSS Distributions
184*699cd480SApple OSS Distributions s = 1;
185*699cd480SApple OSS Distributions t = gen;
186*699cd480SApple OSS Distributions u = expo;
187*699cd480SApple OSS Distributions
188*699cd480SApple OSS Distributions while (u) {
189*699cd480SApple OSS Distributions if (u & 1) {
190*699cd480SApple OSS Distributions s = (s * t) % mod;
191*699cd480SApple OSS Distributions }
192*699cd480SApple OSS Distributions u >>= 1;
193*699cd480SApple OSS Distributions t = (t * t) % mod;
194*699cd480SApple OSS Distributions }
195*699cd480SApple OSS Distributions return (u_int32_t)s;
196*699cd480SApple OSS Distributions }
197*699cd480SApple OSS Distributions
198*699cd480SApple OSS Distributions /*
199*699cd480SApple OSS Distributions * Initalizes the seed and chooses a suitable generator. Also toggles
200*699cd480SApple OSS Distributions * the msb flag. The msb flag is used to generate two distinct
201*699cd480SApple OSS Distributions * cycles of random numbers and thus avoiding reuse of ids.
202*699cd480SApple OSS Distributions *
203*699cd480SApple OSS Distributions * This function is called from id_randomid() when needed, an
204*699cd480SApple OSS Distributions * application does not have to worry about it.
205*699cd480SApple OSS Distributions */
206*699cd480SApple OSS Distributions static void
initid(struct randomtab * p)207*699cd480SApple OSS Distributions initid(struct randomtab *p)
208*699cd480SApple OSS Distributions {
209*699cd480SApple OSS Distributions time_t curtime = (time_t)net_uptime();
210*699cd480SApple OSS Distributions u_int32_t j, i;
211*699cd480SApple OSS Distributions int noprime = 1;
212*699cd480SApple OSS Distributions
213*699cd480SApple OSS Distributions p->ru_x = RandomULong() % p->ru_m;
214*699cd480SApple OSS Distributions
215*699cd480SApple OSS Distributions /* (bits - 1) bits of random seed */
216*699cd480SApple OSS Distributions p->ru_seed = RandomULong() & (~0U >> (32 - p->ru_bits + 1));
217*699cd480SApple OSS Distributions p->ru_seed2 = RandomULong() & (~0U >> (32 - p->ru_bits + 1));
218*699cd480SApple OSS Distributions
219*699cd480SApple OSS Distributions /* Determine the LCG we use */
220*699cd480SApple OSS Distributions p->ru_b = (RandomULong() & (~0U >> (32 - p->ru_bits))) | 1;
221*699cd480SApple OSS Distributions p->ru_a = pmod(p->ru_agen,
222*699cd480SApple OSS Distributions (RandomULong() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
223*699cd480SApple OSS Distributions while (p->ru_b % 3 == 0) {
224*699cd480SApple OSS Distributions p->ru_b += 2;
225*699cd480SApple OSS Distributions }
226*699cd480SApple OSS Distributions
227*699cd480SApple OSS Distributions j = RandomULong() % p->ru_n;
228*699cd480SApple OSS Distributions
229*699cd480SApple OSS Distributions /*
230*699cd480SApple OSS Distributions * Do a fast gcd(j, RU_N - 1), so we can find a j with
231*699cd480SApple OSS Distributions * gcd(j, RU_N - 1) == 1, giving a new generator for
232*699cd480SApple OSS Distributions * RU_GEN^j mod RU_N
233*699cd480SApple OSS Distributions */
234*699cd480SApple OSS Distributions while (noprime) {
235*699cd480SApple OSS Distributions for (i = 0; p->pfacts[i] > 0; i++) {
236*699cd480SApple OSS Distributions if (j % p->pfacts[i] == 0) {
237*699cd480SApple OSS Distributions break;
238*699cd480SApple OSS Distributions }
239*699cd480SApple OSS Distributions }
240*699cd480SApple OSS Distributions
241*699cd480SApple OSS Distributions if (p->pfacts[i] == 0) {
242*699cd480SApple OSS Distributions noprime = 0;
243*699cd480SApple OSS Distributions } else {
244*699cd480SApple OSS Distributions j = (j + 1) % p->ru_n;
245*699cd480SApple OSS Distributions }
246*699cd480SApple OSS Distributions }
247*699cd480SApple OSS Distributions
248*699cd480SApple OSS Distributions p->ru_g = pmod(p->ru_gen, j, p->ru_n);
249*699cd480SApple OSS Distributions p->ru_counter = 0;
250*699cd480SApple OSS Distributions
251*699cd480SApple OSS Distributions p->ru_reseed = curtime + p->ru_out;
252*699cd480SApple OSS Distributions p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
253*699cd480SApple OSS Distributions }
254*699cd480SApple OSS Distributions
255*699cd480SApple OSS Distributions static u_int32_t
randomid(struct randomtab * p)256*699cd480SApple OSS Distributions randomid(struct randomtab *p)
257*699cd480SApple OSS Distributions {
258*699cd480SApple OSS Distributions time_t curtime = (time_t)net_uptime();
259*699cd480SApple OSS Distributions int i, n;
260*699cd480SApple OSS Distributions u_int32_t tmp;
261*699cd480SApple OSS Distributions
262*699cd480SApple OSS Distributions if (p->ru_counter >= p->ru_max || curtime > p->ru_reseed) {
263*699cd480SApple OSS Distributions initid(p);
264*699cd480SApple OSS Distributions }
265*699cd480SApple OSS Distributions
266*699cd480SApple OSS Distributions tmp = RandomULong();
267*699cd480SApple OSS Distributions
268*699cd480SApple OSS Distributions /* Skip a random number of ids */
269*699cd480SApple OSS Distributions n = tmp & 0x3; tmp = tmp >> 2;
270*699cd480SApple OSS Distributions if (p->ru_counter + n >= p->ru_max) {
271*699cd480SApple OSS Distributions initid(p);
272*699cd480SApple OSS Distributions }
273*699cd480SApple OSS Distributions
274*699cd480SApple OSS Distributions for (i = 0; i <= n; i++) {
275*699cd480SApple OSS Distributions /* Linear Congruential Generator */
276*699cd480SApple OSS Distributions p->ru_x = ((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
277*699cd480SApple OSS Distributions }
278*699cd480SApple OSS Distributions
279*699cd480SApple OSS Distributions p->ru_counter += i;
280*699cd480SApple OSS Distributions
281*699cd480SApple OSS Distributions return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) |
282*699cd480SApple OSS Distributions p->ru_msb;
283*699cd480SApple OSS Distributions }
284*699cd480SApple OSS Distributions
285*699cd480SApple OSS Distributions u_int32_t
ip6_randomid(void)286*699cd480SApple OSS Distributions ip6_randomid(void)
287*699cd480SApple OSS Distributions {
288*699cd480SApple OSS Distributions return randomid(&randomtab_32);
289*699cd480SApple OSS Distributions }
290*699cd480SApple OSS Distributions
291*699cd480SApple OSS Distributions u_int32_t
ip6_randomflowlabel(void)292*699cd480SApple OSS Distributions ip6_randomflowlabel(void)
293*699cd480SApple OSS Distributions {
294*699cd480SApple OSS Distributions return RandomULong() & IPV6_FLOWLABEL_MASK;
295*699cd480SApple OSS Distributions }
296