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