1139826Simp/*- 2120639Sume * Copyright (C) 2003 WIDE Project. 3120639Sume * All rights reserved. 4120639Sume * 5120639Sume * Redistribution and use in source and binary forms, with or without 6120639Sume * modification, are permitted provided that the following conditions 7120639Sume * are met: 8120639Sume * 1. Redistributions of source code must retain the above copyright 9120639Sume * notice, this list of conditions and the following disclaimer. 10120639Sume * 2. Redistributions in binary form must reproduce the above copyright 11120639Sume * notice, this list of conditions and the following disclaimer in the 12120639Sume * documentation and/or other materials provided with the distribution. 13120639Sume * 3. Neither the name of the project nor the names of its contributors 14120639Sume * may be used to endorse or promote products derived from this software 15120639Sume * without specific prior written permission. 16120639Sume * 17120639Sume * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18120639Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19120639Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20120639Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21120639Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22120639Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23120639Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24120639Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25120639Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26120639Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27120639Sume * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: ip6_id.c,v 1.13 2003/09/16 09:11:19 itojun Exp $ 30120639Sume */ 31120639Sume 32139826Simp/*- 33120639Sume * Copyright 1998 Niels Provos <provos@citi.umich.edu> 34120639Sume * All rights reserved. 35120639Sume * 36120639Sume * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using 37120639Sume * such a mathematical system to generate more random (yet non-repeating) 38120639Sume * ids to solve the resolver/named problem. But Niels designed the 39120639Sume * actual system based on the constraints. 40120639Sume * 41120639Sume * Redistribution and use in source and binary forms, with or without 42120639Sume * modification, are permitted provided that the following conditions 43120639Sume * are met: 44120639Sume * 1. Redistributions of source code must retain the above copyright 45120639Sume * notice, this list of conditions and the following disclaimer. 46120639Sume * 2. Redistributions in binary form must reproduce the above copyright 47120639Sume * notice, this list of conditions and the following disclaimer in the 48120639Sume * documentation and/or other materials provided with the distribution. 49120639Sume * 3. All advertising materials mentioning features or use of this software 50120639Sume * must display the following acknowledgement: 51120639Sume * This product includes software developed by Niels Provos. 52120639Sume * 4. The name of the author may not be used to endorse or promote products 53120639Sume * derived from this software without specific prior written permission. 54120639Sume * 55120639Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 56120639Sume * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 57120639Sume * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 58120639Sume * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 59120639Sume * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 60120639Sume * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 61120639Sume * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 62120639Sume * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 63120639Sume * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 64120639Sume * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65174510Sobrien * 66327550Spfg * $OpenBSD: ip6_id.c,v 1.2 2003/12/10 07:21:01 itojun Exp $ 67120639Sume */ 68120639Sume 69174510Sobrien#include <sys/cdefs.h> 70174510Sobrien__FBSDID("$FreeBSD: stable/10/sys/netinet6/ip6_id.c 327550 2018-01-04 15:57:49Z pfg $"); 71174510Sobrien 72120639Sume/* 73120639Sume * seed = random (bits - 1) bit 74120639Sume * n = prime, g0 = generator to n, 75120639Sume * j = random so that gcd(j,n-1) == 1 76120639Sume * g = g0^j mod n will be a generator again. 77120639Sume * 78120639Sume * X[0] = random seed. 79120639Sume * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator 80120639Sume * with a = 7^(even random) mod m, 81120639Sume * b = random with gcd(b,m) == 1 82120639Sume * m = constant and a maximal period of m-1. 83120639Sume * 84120639Sume * The transaction id is determined by: 85120639Sume * id[n] = seed xor (g^X[n] mod n) 86120639Sume * 87120639Sume * Effectivly the id is restricted to the lower (bits - 1) bits, thus 88120639Sume * yielding two different cycles by toggling the msb on and off. 89120639Sume * This avoids reuse issues caused by reseeding. 90120639Sume */ 91120639Sume 92120639Sume#include <sys/types.h> 93120639Sume#include <sys/param.h> 94120639Sume#include <sys/kernel.h> 95120639Sume#include <sys/socket.h> 96120639Sume#include <sys/libkern.h> 97120639Sume 98120639Sume#include <net/if.h> 99120639Sume#include <net/route.h> 100120639Sume#include <netinet/in.h> 101120639Sume#include <netinet/ip6.h> 102120639Sume#include <netinet6/ip6_var.h> 103120639Sume 104120639Sume#ifndef INT32_MAX 105120639Sume#define INT32_MAX 0x7fffffffU 106120639Sume#endif 107120639Sume 108120639Sumestruct randomtab { 109120639Sume const int ru_bits; /* resulting bits */ 110120639Sume const long ru_out; /* Time after wich will be reseeded */ 111120639Sume const u_int32_t ru_max; /* Uniq cycle, avoid blackjack prediction */ 112120639Sume const u_int32_t ru_gen; /* Starting generator */ 113120639Sume const u_int32_t ru_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */ 114120639Sume const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */ 115120639Sume const u_int32_t ru_m; /* ru_m = 2^x*3^y */ 116120639Sume const u_int32_t pfacts[4]; /* factors of ru_n */ 117120639Sume 118120639Sume u_int32_t ru_counter; 119120639Sume u_int32_t ru_msb; 120120639Sume 121120639Sume u_int32_t ru_x; 122120639Sume u_int32_t ru_seed, ru_seed2; 123120639Sume u_int32_t ru_a, ru_b; 124120639Sume u_int32_t ru_g; 125120639Sume long ru_reseed; 126120639Sume}; 127120639Sume 128120639Sumestatic struct randomtab randomtab_32 = { 129120639Sume 32, /* resulting bits */ 130120639Sume 180, /* Time after wich will be reseeded */ 131120639Sume 1000000000, /* Uniq cycle, avoid blackjack prediction */ 132120639Sume 2, /* Starting generator */ 133120639Sume 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */ 134120639Sume 7, /* determine ru_a as RU_AGEN^(2*rand) */ 135120639Sume 1836660096, /* RU_M = 2^7*3^15 - don't change */ 136120639Sume { 2, 3, 59652323, 0 }, /* factors of ru_n */ 137120639Sume}; 138120639Sume 139120652Sumestatic struct randomtab randomtab_20 = { 140120652Sume 20, /* resulting bits */ 141120652Sume 180, /* Time after wich will be reseeded */ 142120652Sume 200000, /* Uniq cycle, avoid blackjack prediction */ 143120652Sume 2, /* Starting generator */ 144120652Sume 524269, /* RU_N-1 = 2^2*3^2*14563 */ 145120652Sume 7, /* determine ru_a as RU_AGEN^(2*rand) */ 146120652Sume 279936, /* RU_M = 2^7*3^7 - don't change */ 147120652Sume { 2, 3, 14563, 0 }, /* factors of ru_n */ 148120652Sume}; 149120652Sume 150120639Sumestatic u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t); 151120639Sumestatic void initid(struct randomtab *); 152120639Sumestatic u_int32_t randomid(struct randomtab *); 153120639Sume 154120639Sume/* 155120639Sume * Do a fast modular exponation, returned value will be in the range 156120639Sume * of 0 - (mod-1) 157120639Sume */ 158120639Sumestatic u_int32_t 159120639Sumepmod(u_int32_t gen, u_int32_t expo, u_int32_t mod) 160120639Sume{ 161120639Sume u_int64_t s, t, u; 162120639Sume 163120639Sume s = 1; 164120639Sume t = gen; 165120639Sume u = expo; 166120639Sume 167120639Sume while (u) { 168120639Sume if (u & 1) 169120639Sume s = (s * t) % mod; 170120639Sume u >>= 1; 171120639Sume t = (t * t) % mod; 172120639Sume } 173120639Sume return (s); 174120639Sume} 175120639Sume 176120639Sume/* 177120639Sume * Initalizes the seed and chooses a suitable generator. Also toggles 178120639Sume * the msb flag. The msb flag is used to generate two distinct 179120639Sume * cycles of random numbers and thus avoiding reuse of ids. 180120639Sume * 181120639Sume * This function is called from id_randomid() when needed, an 182120639Sume * application does not have to worry about it. 183120639Sume */ 184120639Sumestatic void 185120639Sumeinitid(struct randomtab *p) 186120639Sume{ 187120639Sume u_int32_t j, i; 188120639Sume int noprime = 1; 189120639Sume 190120639Sume p->ru_x = arc4random() % p->ru_m; 191120639Sume 192120639Sume /* (bits - 1) bits of random seed */ 193120639Sume p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1)); 194120639Sume p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1)); 195120639Sume 196120639Sume /* Determine the LCG we use */ 197120639Sume p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1; 198120639Sume p->ru_a = pmod(p->ru_agen, 199120639Sume (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m); 200120639Sume while (p->ru_b % 3 == 0) 201120639Sume p->ru_b += 2; 202120639Sume 203120639Sume j = arc4random() % p->ru_n; 204120639Sume 205120639Sume /* 206120639Sume * Do a fast gcd(j, RU_N - 1), so we can find a j with 207120639Sume * gcd(j, RU_N - 1) == 1, giving a new generator for 208120639Sume * RU_GEN^j mod RU_N 209120639Sume */ 210120639Sume while (noprime) { 211120639Sume for (i = 0; p->pfacts[i] > 0; i++) 212120639Sume if (j % p->pfacts[i] == 0) 213120639Sume break; 214120639Sume 215120639Sume if (p->pfacts[i] == 0) 216120639Sume noprime = 0; 217120639Sume else 218120639Sume j = (j + 1) % p->ru_n; 219120639Sume } 220120639Sume 221120639Sume p->ru_g = pmod(p->ru_gen, j, p->ru_n); 222120639Sume p->ru_counter = 0; 223120639Sume 224253970Shrs p->ru_reseed = time_uptime + p->ru_out; 225120639Sume p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1)); 226120639Sume} 227120639Sume 228120639Sumestatic u_int32_t 229120639Sumerandomid(struct randomtab *p) 230120639Sume{ 231120639Sume int i, n; 232120639Sume 233253970Shrs if (p->ru_counter >= p->ru_max || time_uptime > p->ru_reseed) 234120639Sume initid(p); 235120639Sume 236120639Sume /* Skip a random number of ids */ 237327550Spfg n = arc4random() & 0x3; 238120639Sume if (p->ru_counter + n >= p->ru_max) 239120639Sume initid(p); 240120639Sume 241120639Sume for (i = 0; i <= n; i++) { 242120639Sume /* Linear Congruential Generator */ 243120639Sume p->ru_x = (u_int32_t)((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m; 244120639Sume } 245120639Sume 246120639Sume p->ru_counter += i; 247120639Sume 248327550Spfg return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) | 249120639Sume p->ru_msb; 250120639Sume} 251120639Sume 252120639Sumeu_int32_t 253120639Sumeip6_randomid(void) 254120639Sume{ 255120639Sume 256120639Sume return randomid(&randomtab_32); 257120639Sume} 258120641Sume 259120649Sumeu_int32_t 260120649Sumeip6_randomflowlabel(void) 261120649Sume{ 262120649Sume 263120649Sume return randomid(&randomtab_20) & 0xfffff; 264120649Sume} 265