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