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