1ab073b32Sdr146992 /* 2ab073b32Sdr146992 * CDDL HEADER START 3ab073b32Sdr146992 * 4ab073b32Sdr146992 * The contents of this file are subject to the terms of the 5ab073b32Sdr146992 * Common Development and Distribution License (the "License"). 6ab073b32Sdr146992 * You may not use this file except in compliance with the License. 7ab073b32Sdr146992 * 8ab073b32Sdr146992 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9ab073b32Sdr146992 * or http://www.opensolaris.org/os/licensing. 10ab073b32Sdr146992 * See the License for the specific language governing permissions 11ab073b32Sdr146992 * and limitations under the License. 12ab073b32Sdr146992 * 13ab073b32Sdr146992 * When distributing Covered Code, include this CDDL HEADER in each 14ab073b32Sdr146992 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15ab073b32Sdr146992 * If applicable, add the following below this CDDL HEADER, with the 16ab073b32Sdr146992 * fields enclosed by brackets "[]" replaced with your own identifying 17ab073b32Sdr146992 * information: Portions Copyright [yyyy] [name of copyright owner] 18ab073b32Sdr146992 * 19ab073b32Sdr146992 * CDDL HEADER END 20ab073b32Sdr146992 */ 21ab073b32Sdr146992 22ab073b32Sdr146992 /* 23*62d17c5fSDarren Reed * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24ab073b32Sdr146992 * Use is subject to license terms. 25ab073b32Sdr146992 */ 26ab073b32Sdr146992 27ab073b32Sdr146992 /* Copyright (c) 1988 AT&T */ 28ab073b32Sdr146992 /* All Rights Reserved */ 29ab073b32Sdr146992 30ab073b32Sdr146992 /* 31ab073b32Sdr146992 * drand48, etc. pseudo-random number generator 32ab073b32Sdr146992 * This implementation assumes unsigned short integers of at least 33ab073b32Sdr146992 * 16 bits, long integers of at least 32 bits, and ignores 34ab073b32Sdr146992 * overflows on adding or multiplying two unsigned integers. 35ab073b32Sdr146992 * Two's-complement representation is assumed in a few places. 36ab073b32Sdr146992 * Some extra masking is done if unsigneds are exactly 16 bits 37ab073b32Sdr146992 * or longs are exactly 32 bits, but so what? 38ab073b32Sdr146992 * An assembly-language implementation would run significantly faster. 39ab073b32Sdr146992 */ 40ab073b32Sdr146992 /* 41ab073b32Sdr146992 * New assumptions (supercede those stated above) for 64-bit work. 42ab073b32Sdr146992 * Longs are now 64 bits, and we are bound by standards to return 43ab073b32Sdr146992 * type long, hovever all internal calculations where long was 44ab073b32Sdr146992 * previously used (32 bit precision) are now using the int32_t 45ab073b32Sdr146992 * type (32 bit precision in both ILP32 and LP64 worlds). 46ab073b32Sdr146992 */ 47ab073b32Sdr146992 48ab073b32Sdr146992 #include <sys/mutex.h> 49ab073b32Sdr146992 50ab073b32Sdr146992 static kmutex_t seed_lock; 51ab073b32Sdr146992 static int init48done = 0; 52ab073b32Sdr146992 53ab073b32Sdr146992 #define EXPORT0(TYPE, fn, fnu) TYPE fn() { \ 54ab073b32Sdr146992 TYPE res; \ 55ab073b32Sdr146992 mutex_enter(&seed_lock); \ 56ab073b32Sdr146992 res = fnu(); \ 57ab073b32Sdr146992 mutex_exit(&seed_lock); \ 58ab073b32Sdr146992 return (res); } 59ab073b32Sdr146992 #define EXPORT1(TYPE, fn, fnu) TYPE fn(unsigned short xsubi[3]) { \ 60ab073b32Sdr146992 TYPE res; \ 61ab073b32Sdr146992 mutex_enter(&seed_lock); \ 62ab073b32Sdr146992 res = fnu(xsubi); \ 63ab073b32Sdr146992 mutex_exit(&seed_lock); \ 64ab073b32Sdr146992 return (res); } 65ab073b32Sdr146992 66ab073b32Sdr146992 #define N 16 67ab073b32Sdr146992 #define MASK ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1) 68ab073b32Sdr146992 #define LOW(x) ((unsigned)(x) & MASK) 69ab073b32Sdr146992 #define HIGH(x) LOW((x) >> N) 70ab073b32Sdr146992 #define MUL(x, y, z) { int32_t l = (int32_t)(x) * (int32_t)(y); \ 71ab073b32Sdr146992 (z)[0] = LOW(l); (z)[1] = HIGH(l); } 72ab073b32Sdr146992 #define CARRY(x, y) ((int32_t)(x) + (int32_t)(y) > MASK) 73ab073b32Sdr146992 #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y))) 74ab073b32Sdr146992 #define X0 0x330E 75ab073b32Sdr146992 #define X1 0xABCD 76ab073b32Sdr146992 #define X2 0x1234 77ab073b32Sdr146992 #define A0 0xE66D 78ab073b32Sdr146992 #define A1 0xDEEC 79ab073b32Sdr146992 #define A2 0x5 80ab073b32Sdr146992 #define C 0xB 81ab073b32Sdr146992 #define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2)) 82ab073b32Sdr146992 #define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2])) 83ab073b32Sdr146992 #define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C) 84ab073b32Sdr146992 #define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \ 85ab073b32Sdr146992 return (v) 86ab073b32Sdr146992 #define NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \ 87ab073b32Sdr146992 int i; TYPE v; unsigned temp[3]; \ 88ab073b32Sdr146992 for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); } \ 89ab073b32Sdr146992 v = F(); REST(v); } 90ab073b32Sdr146992 91ab073b32Sdr146992 /* Way ugly solution to problem names, but it works */ 92ab073b32Sdr146992 #define x _drand48_x 93ab073b32Sdr146992 #define a _drand48_a 94ab073b32Sdr146992 #define c _drand48_c 95ab073b32Sdr146992 /* End way ugly */ 96ab073b32Sdr146992 static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C; 97ab073b32Sdr146992 static unsigned short lastx[3]; 98ab073b32Sdr146992 static void next(void); 99ab073b32Sdr146992 100ab073b32Sdr146992 static long 101ab073b32Sdr146992 ipf_r_lrand48_u(void) 102ab073b32Sdr146992 { 103ab073b32Sdr146992 next(); 104ab073b32Sdr146992 return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1)); 105ab073b32Sdr146992 } 106ab073b32Sdr146992 107ab073b32Sdr146992 static void 108ab073b32Sdr146992 init48(void) 109ab073b32Sdr146992 { 110ab073b32Sdr146992 mutex_init(&seed_lock, 0L, MUTEX_DRIVER, 0L); 111ab073b32Sdr146992 init48done = 1; 112ab073b32Sdr146992 } 113ab073b32Sdr146992 114ab073b32Sdr146992 static long 115ab073b32Sdr146992 ipf_r_mrand48_u(void) 116ab073b32Sdr146992 { 117ab073b32Sdr146992 next(); 118ab073b32Sdr146992 return ((long)((int32_t)x[2] << N) + x[1]); 119ab073b32Sdr146992 } 120ab073b32Sdr146992 121ab073b32Sdr146992 static void 122ab073b32Sdr146992 next(void) 123ab073b32Sdr146992 { 124ab073b32Sdr146992 unsigned p[2], q[2], r[2], carry0, carry1; 125ab073b32Sdr146992 126ab073b32Sdr146992 MUL(a[0], x[0], p); 127ab073b32Sdr146992 ADDEQU(p[0], c, carry0); 128ab073b32Sdr146992 ADDEQU(p[1], carry0, carry1); 129ab073b32Sdr146992 MUL(a[0], x[1], q); 130ab073b32Sdr146992 ADDEQU(p[1], q[0], carry0); 131ab073b32Sdr146992 MUL(a[1], x[0], r); 132ab073b32Sdr146992 x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] + 133ab073b32Sdr146992 a[0] * x[2] + a[1] * x[1] + a[2] * x[0]); 134ab073b32Sdr146992 x[1] = LOW(p[1] + r[0]); 135ab073b32Sdr146992 x[0] = LOW(p[0]); 136ab073b32Sdr146992 } 137ab073b32Sdr146992 138ab073b32Sdr146992 void 139ab073b32Sdr146992 ipf_r_srand48(long seedval) 140ab073b32Sdr146992 { 141ab073b32Sdr146992 int32_t fixseed = (int32_t)seedval; /* limit to 32 bits */ 142ab073b32Sdr146992 143ab073b32Sdr146992 if (init48done == 0) 144ab073b32Sdr146992 init48(); 145ab073b32Sdr146992 mutex_enter(&seed_lock); 146ab073b32Sdr146992 SEED(X0, LOW(fixseed), HIGH(fixseed)); 147ab073b32Sdr146992 mutex_exit(&seed_lock); 148ab073b32Sdr146992 } 149ab073b32Sdr146992 150ab073b32Sdr146992 EXPORT0(long, ipf_r_lrand48, ipf_r_lrand48_u) 151ab073b32Sdr146992 152ab073b32Sdr146992 #include <sys/random.h> 153ab073b32Sdr146992 154ab073b32Sdr146992 unsigned 155ab073b32Sdr146992 ipf_random() 156ab073b32Sdr146992 { 157ab073b32Sdr146992 static int seeded = 0; 158ab073b32Sdr146992 159ab073b32Sdr146992 if (seeded == 0) { 160ab073b32Sdr146992 long seed; 161ab073b32Sdr146992 162ab073b32Sdr146992 /* 163ab073b32Sdr146992 * Keep reseeding until some good randomness comes from the 164ab073b32Sdr146992 * kernel. One of two things will happen: it will succeed or 165ab073b32Sdr146992 * it will fail (with poor randomness), thus creating NAT 166ab073b32Sdr146992 * sessions will be "slow" until enough randomness is gained 167ab073b32Sdr146992 * to not need to get more. It isn't necessary to initialise 168ab073b32Sdr146992 * seed as it will just pickup whatever random garbage has 169ab073b32Sdr146992 * been left on the heap and that's good enough until we 170ab073b32Sdr146992 * get some good garbage. 171ab073b32Sdr146992 */ 172ab073b32Sdr146992 if (random_get_bytes((uint8_t *)&seed, sizeof (seed)) == 0) 173ab073b32Sdr146992 seeded = 1; 174ab073b32Sdr146992 ipf_r_srand48(seed); 175ab073b32Sdr146992 } 176ab073b32Sdr146992 177ab073b32Sdr146992 return (unsigned)ipf_r_lrand48(); 178ab073b32Sdr146992 } 179