1 /* $OpenBSD: res_random.c,v 1.16 2005/03/25 13:24:12 otto Exp $ */ 2 3 /* 4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 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 * Effectivly 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 * The 16 bit space is very small and brute force attempts are 53 * entirly feasible, we skip a random number of transaction ids 54 * so that an attacker will not get sequential ids. 55 */ 56 57 #include <sys/types.h> 58 #include <netinet/in.h> 59 #include <sys/time.h> 60 #include <resolv.h> 61 62 #include <unistd.h> 63 #include <stdlib.h> 64 #include <string.h> 65 66 #define RU_OUT 180 /* Time after wich will be reseeded */ 67 #define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ 68 #define RU_GEN 2 /* Starting generator */ 69 #define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ 70 #define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ 71 #define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ 72 73 #define PFAC_N 3 74 const static u_int16_t pfacts[PFAC_N] = { 75 2, 76 3, 77 2729 78 }; 79 80 static u_int16_t ru_x; 81 static u_int16_t ru_seed, ru_seed2; 82 static u_int16_t ru_a, ru_b; 83 static u_int16_t ru_g; 84 static u_int16_t ru_counter = 0; 85 static u_int16_t ru_msb = 0; 86 static long ru_reseed; 87 static u_int32_t tmp; /* Storage for unused random */ 88 static struct timeval tv; 89 90 static u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t); 91 static void res_initid(void); 92 93 /* 94 * Do a fast modular exponation, returned value will be in the range 95 * of 0 - (mod-1) 96 */ 97 98 static u_int16_t 99 pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod) 100 { 101 u_int16_t s, t, u; 102 103 s = 1; 104 t = gen; 105 u = exp; 106 107 while (u) { 108 if (u & 1) 109 s = (s * t) % mod; 110 u >>= 1; 111 t = (t * t) % mod; 112 } 113 return (s); 114 } 115 116 /* 117 * Initializes the seed and chooses a suitable generator. Also toggles 118 * the msb flag. The msb flag is used to generate two distinct 119 * cycles of random numbers and thus avoiding reuse of ids. 120 * 121 * This function is called from res_randomid() when needed, an 122 * application does not have to worry about it. 123 */ 124 static void 125 res_initid(void) 126 { 127 u_int16_t j, i; 128 int noprime = 1; 129 130 tmp = arc4random(); 131 ru_x = (tmp & 0xFFFF) % RU_M; 132 133 /* 15 bits of random seed */ 134 ru_seed = (tmp >> 16) & 0x7FFF; 135 tmp = arc4random(); 136 ru_seed2 = tmp & 0x7FFF; 137 138 tmp = arc4random(); 139 140 /* Determine the LCG we use */ 141 ru_b = (tmp & 0xfffe) | 1; 142 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); 143 while (ru_b % 3 == 0) 144 ru_b += 2; 145 146 tmp = arc4random(); 147 j = tmp % RU_N; 148 tmp = tmp >> 16; 149 150 /* 151 * Do a fast gcd(j,RU_N-1), so we can find a j with 152 * gcd(j, RU_N-1) == 1, giving a new generator for 153 * RU_GEN^j mod RU_N 154 */ 155 156 while (noprime) { 157 for (i = 0; i < PFAC_N; i++) 158 if (j % pfacts[i] == 0) 159 break; 160 161 if (i >= PFAC_N) 162 noprime = 0; 163 else 164 j = (j + 1) % RU_N; 165 } 166 167 ru_g = pmod(RU_GEN, j, RU_N); 168 ru_counter = 0; 169 170 gettimeofday(&tv, NULL); 171 ru_reseed = tv.tv_sec + RU_OUT; 172 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; 173 } 174 175 u_int 176 res_randomid(void) 177 { 178 int i, n; 179 180 gettimeofday(&tv, NULL); 181 if (ru_counter >= RU_MAX || tv.tv_sec > ru_reseed) 182 res_initid(); 183 184 #if 0 185 if (!tmp) 186 tmp = arc4random(); 187 188 /* Skip a random number of ids */ 189 n = tmp & 0x7; tmp = tmp >> 3; 190 if (ru_counter + n >= RU_MAX) 191 res_initid(); 192 #else 193 n = 0; 194 #endif 195 196 for (i = 0; i <= n; i++) 197 /* Linear Congruential Generator */ 198 ru_x = (ru_a * ru_x + ru_b) % RU_M; 199 200 ru_counter += i; 201 202 return (ru_seed ^ pmod(ru_g, ru_seed2 + ru_x, RU_N)) | ru_msb; 203 } 204 205 #if 0 206 void 207 main(int argc, char **argv) 208 { 209 int i, n; 210 u_int16_t wert; 211 212 res_initid(); 213 214 printf("Generator: %u\n", ru_g); 215 printf("Seed: %u\n", ru_seed); 216 printf("Reseed at %ld\n", ru_reseed); 217 printf("Ru_X: %u\n", ru_x); 218 printf("Ru_A: %u\n", ru_a); 219 printf("Ru_B: %u\n", ru_b); 220 221 n = atoi(argv[1]); 222 for (i=0;i<n;i++) { 223 wert = res_randomid(); 224 printf("%06d\n", wert); 225 } 226 } 227 #endif 228 229