1*2fe8fb19SBen Gras /* $NetBSD: randomid.c,v 1.13 2009/01/11 02:46:27 christos Exp $ */
2*2fe8fb19SBen Gras /* $KAME: ip6_id.c,v 1.8 2003/09/06 13:41:06 itojun Exp $ */
3*2fe8fb19SBen Gras /* $OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $ */
4*2fe8fb19SBen Gras
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras * Copyright (C) 2003 WIDE Project.
7*2fe8fb19SBen Gras * All rights reserved.
8*2fe8fb19SBen Gras *
9*2fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
10*2fe8fb19SBen Gras * modification, are permitted provided that the following conditions
11*2fe8fb19SBen Gras * are met:
12*2fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
13*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
14*2fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
15*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
16*2fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
17*2fe8fb19SBen Gras * 3. Neither the name of the project nor the names of its contributors
18*2fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
19*2fe8fb19SBen Gras * without specific prior written permission.
20*2fe8fb19SBen Gras *
21*2fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22*2fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*2fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*2fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25*2fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*2fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*2fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*2fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*2fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*2fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*2fe8fb19SBen Gras * SUCH DAMAGE.
32*2fe8fb19SBen Gras */
33*2fe8fb19SBen Gras
34*2fe8fb19SBen Gras /*
35*2fe8fb19SBen Gras * Copyright 1998 Niels Provos <provos@citi.umich.edu>
36*2fe8fb19SBen Gras * All rights reserved.
37*2fe8fb19SBen Gras *
38*2fe8fb19SBen Gras * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
39*2fe8fb19SBen Gras * such a mathematical system to generate more random (yet non-repeating)
40*2fe8fb19SBen Gras * ids to solve the resolver/named problem. But Niels designed the
41*2fe8fb19SBen Gras * actual system based on the constraints.
42*2fe8fb19SBen Gras *
43*2fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
44*2fe8fb19SBen Gras * modification, are permitted provided that the following conditions
45*2fe8fb19SBen Gras * are met:
46*2fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
47*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
48*2fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
49*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
50*2fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
51*2fe8fb19SBen Gras *
52*2fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53*2fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54*2fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55*2fe8fb19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56*2fe8fb19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57*2fe8fb19SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58*2fe8fb19SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59*2fe8fb19SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60*2fe8fb19SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61*2fe8fb19SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62*2fe8fb19SBen Gras */
63*2fe8fb19SBen Gras
64*2fe8fb19SBen Gras /*
65*2fe8fb19SBen Gras * seed = random (bits - 1) bit
66*2fe8fb19SBen Gras * n = prime, g0 = generator to n,
67*2fe8fb19SBen Gras * j = random so that gcd(j,n-1) == 1
68*2fe8fb19SBen Gras * g = g0^j mod n will be a generator again.
69*2fe8fb19SBen Gras *
70*2fe8fb19SBen Gras * X[0] = random seed.
71*2fe8fb19SBen Gras * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
72*2fe8fb19SBen Gras * with a = 7^(even random) mod m,
73*2fe8fb19SBen Gras * b = random with gcd(b,m) == 1
74*2fe8fb19SBen Gras * m = constant and a maximal period of m-1.
75*2fe8fb19SBen Gras *
76*2fe8fb19SBen Gras * The transaction id is determined by:
77*2fe8fb19SBen Gras * id[n] = seed xor (g^X[n] mod n)
78*2fe8fb19SBen Gras *
79*2fe8fb19SBen Gras * Effectivly the id is restricted to the lower (bits - 1) bits, thus
80*2fe8fb19SBen Gras * yielding two different cycles by toggling the msb on and off.
81*2fe8fb19SBen Gras * This avoids reuse issues caused by reseeding.
82*2fe8fb19SBen Gras */
83*2fe8fb19SBen Gras
84*2fe8fb19SBen Gras #include <sys/cdefs.h>
85*2fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
86*2fe8fb19SBen Gras __RCSID("$NetBSD: randomid.c,v 1.13 2009/01/11 02:46:27 christos Exp $");
87*2fe8fb19SBen Gras #endif
88*2fe8fb19SBen Gras
89*2fe8fb19SBen Gras #include "namespace.h"
90*2fe8fb19SBen Gras
91*2fe8fb19SBen Gras #include <sys/types.h>
92*2fe8fb19SBen Gras #include <sys/time.h>
93*2fe8fb19SBen Gras #include <stdlib.h>
94*2fe8fb19SBen Gras #include <string.h>
95*2fe8fb19SBen Gras #include <errno.h>
96*2fe8fb19SBen Gras #include <randomid.h>
97*2fe8fb19SBen Gras
98*2fe8fb19SBen Gras #ifdef __weak_alias
99*2fe8fb19SBen Gras __weak_alias(randomid,_randomid)
100*2fe8fb19SBen Gras __weak_alias(randomid_new,_randomid_new)
101*2fe8fb19SBen Gras __weak_alias(randomid_delete,_randomid_delete)
102*2fe8fb19SBen Gras #endif
103*2fe8fb19SBen Gras
104*2fe8fb19SBen Gras struct randomconf {
105*2fe8fb19SBen Gras const int rc_bits; /* resulting bits */
106*2fe8fb19SBen Gras const u_int32_t rc_max; /* Uniq cycle, avoid blackjack prediction */
107*2fe8fb19SBen Gras const u_int32_t rc_gen; /* Starting generator */
108*2fe8fb19SBen Gras const u_int32_t rc_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */
109*2fe8fb19SBen Gras const u_int32_t rc_agen; /* determine ru_a as ru_agen^(2*rand) */
110*2fe8fb19SBen Gras const u_int32_t rc_m; /* ru_m = 2^x*3^y */
111*2fe8fb19SBen Gras const u_int32_t rc_pfacts[4]; /* factors of ru_n */
112*2fe8fb19SBen Gras const int rc_skip; /* skip values */
113*2fe8fb19SBen Gras };
114*2fe8fb19SBen Gras
115*2fe8fb19SBen Gras struct randomid_ctx {
116*2fe8fb19SBen Gras struct randomconf *ru_conf;
117*2fe8fb19SBen Gras #define ru_bits ru_conf->rc_bits
118*2fe8fb19SBen Gras #define ru_max ru_conf->rc_max
119*2fe8fb19SBen Gras #define ru_gen ru_conf->rc_gen
120*2fe8fb19SBen Gras #define ru_n ru_conf->rc_n
121*2fe8fb19SBen Gras #define ru_agen ru_conf->rc_agen
122*2fe8fb19SBen Gras #define ru_m ru_conf->rc_m
123*2fe8fb19SBen Gras #define ru_pfacts ru_conf->rc_pfacts
124*2fe8fb19SBen Gras #define ru_skip ru_conf->rc_skip
125*2fe8fb19SBen Gras long ru_out; /* Time after wich will be reseeded */
126*2fe8fb19SBen Gras u_int32_t ru_counter;
127*2fe8fb19SBen Gras u_int32_t ru_msb;
128*2fe8fb19SBen Gras
129*2fe8fb19SBen Gras u_int32_t ru_x;
130*2fe8fb19SBen Gras u_int32_t ru_seed, ru_seed2;
131*2fe8fb19SBen Gras u_int32_t ru_a, ru_b;
132*2fe8fb19SBen Gras u_int32_t ru_g;
133*2fe8fb19SBen Gras time_t ru_reseed;
134*2fe8fb19SBen Gras };
135*2fe8fb19SBen Gras
136*2fe8fb19SBen Gras static struct randomconf randomconf[] = {
137*2fe8fb19SBen Gras {
138*2fe8fb19SBen Gras 32, /* resulting bits */
139*2fe8fb19SBen Gras 1000000000, /* Uniq cycle, avoid blackjack prediction */
140*2fe8fb19SBen Gras 2, /* Starting generator */
141*2fe8fb19SBen Gras 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */
142*2fe8fb19SBen Gras 7, /* determine ru_a as RU_AGEN^(2*rand) */
143*2fe8fb19SBen Gras 1836660096, /* RU_M = 2^7*3^15 - don't change */
144*2fe8fb19SBen Gras { 2, 3, 59652323, 0 }, /* factors of ru_n */
145*2fe8fb19SBen Gras 3, /* skip values */
146*2fe8fb19SBen Gras },
147*2fe8fb19SBen Gras {
148*2fe8fb19SBen Gras 20, /* resulting bits */
149*2fe8fb19SBen Gras 200000, /* Uniq cycle, avoid blackjack prediction */
150*2fe8fb19SBen Gras 2, /* Starting generator */
151*2fe8fb19SBen Gras 524269, /* RU_N-1 = 2^2*3^2*14563 */
152*2fe8fb19SBen Gras 7, /* determine ru_a as RU_AGEN^(2*rand) */
153*2fe8fb19SBen Gras 279936, /* RU_M = 2^7*3^7 - don't change */
154*2fe8fb19SBen Gras { 2, 3, 14563, 0 }, /* factors of ru_n */
155*2fe8fb19SBen Gras 3, /* skip values */
156*2fe8fb19SBen Gras },
157*2fe8fb19SBen Gras {
158*2fe8fb19SBen Gras 16, /* resulting bits */
159*2fe8fb19SBen Gras 30000, /* Uniq cycle, avoid blackjack prediction */
160*2fe8fb19SBen Gras 2, /* Starting generator */
161*2fe8fb19SBen Gras 32749, /* RU_N-1 = 2^2*3*2729 */
162*2fe8fb19SBen Gras 7, /* determine ru_a as RU_AGEN^(2*rand) */
163*2fe8fb19SBen Gras 31104, /* RU_M = 2^7*3^5 - don't change */
164*2fe8fb19SBen Gras { 2, 3, 2729, 0 }, /* factors of ru_n */
165*2fe8fb19SBen Gras 0, /* skip values */
166*2fe8fb19SBen Gras },
167*2fe8fb19SBen Gras {
168*2fe8fb19SBen Gras .rc_bits = -1, /* termination */
169*2fe8fb19SBen Gras },
170*2fe8fb19SBen Gras };
171*2fe8fb19SBen Gras
172*2fe8fb19SBen Gras static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
173*2fe8fb19SBen Gras static void initid(struct randomid_ctx *);
174*2fe8fb19SBen Gras
175*2fe8fb19SBen Gras struct randomid_ctx *randomid_new(int, long);
176*2fe8fb19SBen Gras void randomid_delete(struct randomid_ctx *);
177*2fe8fb19SBen Gras u_int32_t randomid(struct randomid_ctx *);
178*2fe8fb19SBen Gras
179*2fe8fb19SBen Gras /*
180*2fe8fb19SBen Gras * Do a fast modular exponation, returned value will be in the range
181*2fe8fb19SBen Gras * of 0 - (mod-1)
182*2fe8fb19SBen Gras */
183*2fe8fb19SBen Gras
184*2fe8fb19SBen Gras static u_int32_t
pmod(u_int32_t gen,u_int32_t expo,u_int32_t mod)185*2fe8fb19SBen Gras pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
186*2fe8fb19SBen Gras {
187*2fe8fb19SBen Gras u_int64_t s, t, u;
188*2fe8fb19SBen Gras
189*2fe8fb19SBen Gras s = 1;
190*2fe8fb19SBen Gras t = gen;
191*2fe8fb19SBen Gras u = expo;
192*2fe8fb19SBen Gras
193*2fe8fb19SBen Gras while (u) {
194*2fe8fb19SBen Gras if (u & 1)
195*2fe8fb19SBen Gras s = (s * t) % mod;
196*2fe8fb19SBen Gras u >>= 1;
197*2fe8fb19SBen Gras t = (t * t) % mod;
198*2fe8fb19SBen Gras }
199*2fe8fb19SBen Gras return ((u_int32_t)s & UINT32_MAX);
200*2fe8fb19SBen Gras }
201*2fe8fb19SBen Gras
202*2fe8fb19SBen Gras /*
203*2fe8fb19SBen Gras * Initalizes the seed and chooses a suitable generator. Also toggles
204*2fe8fb19SBen Gras * the msb flag. The msb flag is used to generate two distinct
205*2fe8fb19SBen Gras * cycles of random numbers and thus avoiding reuse of ids.
206*2fe8fb19SBen Gras *
207*2fe8fb19SBen Gras * This function is called from id_randomid() when needed, an
208*2fe8fb19SBen Gras * application does not have to worry about it.
209*2fe8fb19SBen Gras */
210*2fe8fb19SBen Gras static void
initid(struct randomid_ctx * p)211*2fe8fb19SBen Gras initid(struct randomid_ctx *p)
212*2fe8fb19SBen Gras {
213*2fe8fb19SBen Gras u_int32_t j, i;
214*2fe8fb19SBen Gras int noprime = 1;
215*2fe8fb19SBen Gras struct timeval tv;
216*2fe8fb19SBen Gras
217*2fe8fb19SBen Gras p->ru_x = arc4random() % p->ru_m;
218*2fe8fb19SBen Gras
219*2fe8fb19SBen Gras /* (bits - 1) bits of random seed */
220*2fe8fb19SBen Gras p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1));
221*2fe8fb19SBen Gras p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1));
222*2fe8fb19SBen Gras
223*2fe8fb19SBen Gras /* Determine the LCG we use */
224*2fe8fb19SBen Gras p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1;
225*2fe8fb19SBen Gras p->ru_a = pmod(p->ru_agen,
226*2fe8fb19SBen Gras (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
227*2fe8fb19SBen Gras while (p->ru_b % 3 == 0)
228*2fe8fb19SBen Gras p->ru_b += 2;
229*2fe8fb19SBen Gras
230*2fe8fb19SBen Gras j = arc4random() % p->ru_n;
231*2fe8fb19SBen Gras
232*2fe8fb19SBen Gras /*
233*2fe8fb19SBen Gras * Do a fast gcd(j, RU_N - 1), so we can find a j with
234*2fe8fb19SBen Gras * gcd(j, RU_N - 1) == 1, giving a new generator for
235*2fe8fb19SBen Gras * RU_GEN^j mod RU_N
236*2fe8fb19SBen Gras */
237*2fe8fb19SBen Gras while (noprime) {
238*2fe8fb19SBen Gras for (i = 0; p->ru_pfacts[i] > 0; i++)
239*2fe8fb19SBen Gras if (j % p->ru_pfacts[i] == 0)
240*2fe8fb19SBen Gras break;
241*2fe8fb19SBen Gras
242*2fe8fb19SBen Gras if (p->ru_pfacts[i] == 0)
243*2fe8fb19SBen Gras noprime = 0;
244*2fe8fb19SBen Gras else
245*2fe8fb19SBen Gras j = (j + 1) % p->ru_n;
246*2fe8fb19SBen Gras }
247*2fe8fb19SBen Gras
248*2fe8fb19SBen Gras p->ru_g = pmod(p->ru_gen, j, p->ru_n);
249*2fe8fb19SBen Gras p->ru_counter = 0;
250*2fe8fb19SBen Gras
251*2fe8fb19SBen Gras gettimeofday(&tv, NULL);
252*2fe8fb19SBen Gras p->ru_reseed = tv.tv_sec + p->ru_out;
253*2fe8fb19SBen Gras p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
254*2fe8fb19SBen Gras }
255*2fe8fb19SBen Gras
256*2fe8fb19SBen Gras struct randomid_ctx *
randomid_new(int bits,long timeo)257*2fe8fb19SBen Gras randomid_new(int bits, long timeo)
258*2fe8fb19SBen Gras {
259*2fe8fb19SBen Gras struct randomconf *conf;
260*2fe8fb19SBen Gras struct randomid_ctx *ctx;
261*2fe8fb19SBen Gras
262*2fe8fb19SBen Gras if (timeo < RANDOMID_TIMEO_MIN) {
263*2fe8fb19SBen Gras errno = EINVAL;
264*2fe8fb19SBen Gras return (NULL);
265*2fe8fb19SBen Gras }
266*2fe8fb19SBen Gras
267*2fe8fb19SBen Gras for (conf = randomconf; conf->rc_bits > 0; conf++) {
268*2fe8fb19SBen Gras if (bits == conf->rc_bits)
269*2fe8fb19SBen Gras break;
270*2fe8fb19SBen Gras }
271*2fe8fb19SBen Gras
272*2fe8fb19SBen Gras /* unsupported bits */
273*2fe8fb19SBen Gras if (bits != conf->rc_bits) {
274*2fe8fb19SBen Gras errno = ENOTSUP;
275*2fe8fb19SBen Gras return (NULL);
276*2fe8fb19SBen Gras }
277*2fe8fb19SBen Gras
278*2fe8fb19SBen Gras ctx = malloc(sizeof(*ctx));
279*2fe8fb19SBen Gras if (!ctx)
280*2fe8fb19SBen Gras return (NULL);
281*2fe8fb19SBen Gras
282*2fe8fb19SBen Gras memset(ctx, 0, sizeof(*ctx));
283*2fe8fb19SBen Gras ctx->ru_conf = conf;
284*2fe8fb19SBen Gras ctx->ru_out = timeo;
285*2fe8fb19SBen Gras
286*2fe8fb19SBen Gras return (ctx);
287*2fe8fb19SBen Gras }
288*2fe8fb19SBen Gras
289*2fe8fb19SBen Gras void
randomid_delete(struct randomid_ctx * ctx)290*2fe8fb19SBen Gras randomid_delete(struct randomid_ctx *ctx)
291*2fe8fb19SBen Gras {
292*2fe8fb19SBen Gras
293*2fe8fb19SBen Gras memset(ctx, 0, sizeof(*ctx));
294*2fe8fb19SBen Gras free(ctx);
295*2fe8fb19SBen Gras }
296*2fe8fb19SBen Gras
297*2fe8fb19SBen Gras u_int32_t
randomid(struct randomid_ctx * p)298*2fe8fb19SBen Gras randomid(struct randomid_ctx *p)
299*2fe8fb19SBen Gras {
300*2fe8fb19SBen Gras int i, n;
301*2fe8fb19SBen Gras struct timeval tv;
302*2fe8fb19SBen Gras
303*2fe8fb19SBen Gras gettimeofday(&tv, NULL);
304*2fe8fb19SBen Gras if (p->ru_counter >= p->ru_max || tv.tv_sec > p->ru_reseed)
305*2fe8fb19SBen Gras initid(p);
306*2fe8fb19SBen Gras
307*2fe8fb19SBen Gras /* Skip a random number of ids */
308*2fe8fb19SBen Gras if (p->ru_skip) {
309*2fe8fb19SBen Gras n = arc4random() & p->ru_skip;
310*2fe8fb19SBen Gras if (p->ru_counter + n >= p->ru_max)
311*2fe8fb19SBen Gras initid(p);
312*2fe8fb19SBen Gras } else
313*2fe8fb19SBen Gras n = 0;
314*2fe8fb19SBen Gras
315*2fe8fb19SBen Gras for (i = 0; i <= n; i++) {
316*2fe8fb19SBen Gras /* Linear Congruential Generator */
317*2fe8fb19SBen Gras p->ru_x = (u_int32_t)(((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m);
318*2fe8fb19SBen Gras }
319*2fe8fb19SBen Gras
320*2fe8fb19SBen Gras p->ru_counter += i;
321*2fe8fb19SBen Gras
322*2fe8fb19SBen Gras return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) |
323*2fe8fb19SBen Gras p->ru_msb;
324*2fe8fb19SBen Gras }
325