1 /* $OpenBSD: ip_id.c,v 1.14 2007/05/27 19:59:11 dlg 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) 9 * ids to solve the resolver/named problem. But Niels designed the 10 * actual system based on the constraints. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * seed = random 15bit 35 * n = prime, g0 = generator to n, 36 * j = random so that gcd(j,n-1) == 1 37 * g = g0^j mod n will be a generator again. 38 * 39 * X[0] = random seed. 40 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator 41 * with a = 7^(even random) mod m, 42 * b = random with gcd(b,m) == 1 43 * m = 31104 and a maximal period of m-1. 44 * 45 * The transaction id is determined by: 46 * id[n] = seed xor (g^X[n] mod n) 47 * 48 * Effectively the id is restricted to the lower 15 bits, thus 49 * yielding two different cycles by toggling the msb on and off. 50 * This avoids reuse issues caused by reseeding. 51 */ 52 53 #include <sys/param.h> 54 #include <sys/kernel.h> 55 56 #include <dev/rndvar.h> 57 58 #define RU_OUT 180 /* Time after wich will be reseeded */ 59 #define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ 60 #define RU_GEN 2 /* Starting generator */ 61 #define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ 62 #define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ 63 #define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ 64 65 #define PFAC_N 3 66 const static u_int16_t pfacts[PFAC_N] = { 67 2, 68 3, 69 2729 70 }; 71 72 static u_int16_t ru_x; 73 static u_int16_t ru_seed, ru_seed2; 74 static u_int16_t ru_a, ru_b; 75 static u_int16_t ru_g; 76 static u_int16_t ru_counter = 0; 77 static u_int16_t ru_msb = 0; 78 static long ru_reseed; 79 static u_int32_t tmp; /* Storage for unused random */ 80 81 u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t); 82 void ip_initid(void); 83 u_int16_t ip_randomid(void); 84 85 /* 86 * Do a fast modular exponation, returned value will be in the range 87 * of 0 - (mod-1) 88 */ 89 90 u_int16_t 91 pmod(u_int16_t gen, u_int16_t expo, u_int16_t mod) 92 { 93 u_int16_t s, t, u; 94 95 s = 1; 96 t = gen; 97 u = expo; 98 99 while (u) { 100 if (u & 1) 101 s = (s*t) % mod; 102 u >>= 1; 103 t = (t*t) % mod; 104 } 105 return (s); 106 } 107 108 /* 109 * Initalizes the seed and chooses a suitable generator. Also toggles 110 * the msb flag. The msb flag is used to generate two distinct 111 * cycles of random numbers and thus avoiding reuse of ids. 112 * 113 * This function is called from id_randomid() when needed, an 114 * application does not have to worry about it. 115 */ 116 void 117 ip_initid(void) 118 { 119 u_int16_t j, i; 120 int noprime = 1; 121 122 ru_x = ((tmp = arc4random()) & 0xFFFF) % RU_M; 123 124 /* 15 bits of random seed */ 125 ru_seed = (tmp >> 16) & 0x7FFF; 126 ru_seed2 = arc4random() & 0x7FFF; 127 128 /* Determine the LCG we use */ 129 ru_b = ((tmp = arc4random()) & 0xfffe) | 1; 130 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); 131 while (ru_b % 3 == 0) 132 ru_b += 2; 133 134 j = (tmp = arc4random()) % RU_N; 135 tmp = tmp >> 16; 136 137 /* 138 * Do a fast gcd(j,RU_N-1), so we can find a j with 139 * gcd(j, RU_N-1) == 1, giving a new generator for 140 * RU_GEN^j mod RU_N 141 */ 142 143 while (noprime) { 144 for (i = 0; i < PFAC_N; i++) 145 if (j % pfacts[i] == 0) 146 break; 147 148 if (i >= PFAC_N) 149 noprime = 0; 150 else 151 j = (j+1) % RU_N; 152 } 153 154 ru_g = pmod(RU_GEN,j,RU_N); 155 ru_counter = 0; 156 157 ru_reseed = time_second + RU_OUT; 158 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; 159 } 160 161 u_int16_t 162 ip_randomid(void) 163 { 164 int i, n; 165 166 if (ru_counter >= RU_MAX || time_second > ru_reseed) 167 ip_initid(); 168 169 #if 0 170 if (!tmp) 171 tmp = arc4random(); 172 173 /* Skip a random number of ids */ 174 n = tmp & 0x3; tmp = tmp >> 2; 175 if (ru_counter + n >= RU_MAX) 176 ip_initid(); 177 #else 178 n = 0; 179 #endif 180 181 for (i = 0; i <= n; i++) 182 /* Linear Congruential Generator */ 183 ru_x = (ru_a * ru_x + ru_b) % RU_M; 184 185 ru_counter += i; 186 187 return (ru_seed ^ pmod(ru_g,ru_seed2 + ru_x, RU_N)) | ru_msb; 188 }