summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorprovos <>1997-04-13 21:30:47 +0000
committerprovos <>1997-04-13 21:30:47 +0000
commit8748c1e58b42a18ddefc3a3c64dcef04f063fd73 (patch)
tree0b205c7047810284d55a96e138cbef206efe861c
parentd4f803e80d72c0e5d6d296286646ccc5dc01c32d (diff)
downloadopenbsd-8748c1e58b42a18ddefc3a3c64dcef04f063fd73.tar.gz
openbsd-8748c1e58b42a18ddefc3a3c64dcef04f063fd73.tar.bz2
openbsd-8748c1e58b42a18ddefc3a3c64dcef04f063fd73.zip
the before mentioned random number generator.
-rw-r--r--src/lib/libc/net/res_init.c17
-rw-r--r--src/lib/libc/net/res_mkquery.c11
-rw-r--r--src/lib/libc/net/res_random.c192
3 files changed, 202 insertions, 18 deletions
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c
index a16c47807f..973fdd43be 100644
--- a/src/lib/libc/net/res_init.c
+++ b/src/lib/libc/net/res_init.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: res_init.c,v 1.13 1997/04/05 21:13:17 millert Exp $ */ 1/* $OpenBSD: res_init.c,v 1.14 1997/04/13 21:30:43 provos Exp $ */
2 2
3/* 3/*
4 * ++Copyright++ 1985, 1989, 1993 4 * ++Copyright++ 1985, 1989, 1993
@@ -60,7 +60,7 @@
60static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 60static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
61static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $"; 61static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $";
62#else 62#else
63static char rcsid[] = "$OpenBSD: res_init.c,v 1.13 1997/04/05 21:13:17 millert Exp $"; 63static char rcsid[] = "$OpenBSD: res_init.c,v 1.14 1997/04/13 21:30:43 provos Exp $";
64#endif 64#endif
65#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
66 66
@@ -185,13 +185,6 @@ res_init()
185 if (!(_res.options & RES_INIT)) 185 if (!(_res.options & RES_INIT))
186 _res.options = RES_DEFAULT; 186 _res.options = RES_DEFAULT;
187 187
188 /*
189 * This one used to initialize implicitly to zero, so unless the app
190 * has set it to something in particular, we can randomize it now.
191 */
192 if (!_res.id)
193 _res.id = res_randomid();
194
195#ifdef USELOOPBACK 188#ifdef USELOOPBACK
196 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 189 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
197#else 190#else
@@ -510,9 +503,3 @@ net_mask(in) /* XXX - should really use system's version of this */
510 return (htonl(IN_CLASSC_NET)); 503 return (htonl(IN_CLASSC_NET));
511} 504}
512#endif 505#endif
513
514u_int
515res_randomid()
516{
517 return (0xffff & arc4random());
518}
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c
index 5faba01748..2c244c24fc 100644
--- a/src/lib/libc/net/res_mkquery.c
+++ b/src/lib/libc/net/res_mkquery.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: res_mkquery.c,v 1.6 1997/03/13 19:07:39 downsj Exp $ */ 1/* $OpenBSD: res_mkquery.c,v 1.7 1997/04/13 21:30:46 provos Exp $ */
2 2
3/* 3/*
4 * ++Copyright++ 1985, 1993 4 * ++Copyright++ 1985, 1993
@@ -60,7 +60,7 @@
60static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
61static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $"; 61static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $";
62#else 62#else
63static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.6 1997/03/13 19:07:39 downsj Exp $"; 63static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.7 1997/04/13 21:30:46 provos Exp $";
64#endif 64#endif
65#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
66 66
@@ -106,12 +106,17 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen)
106#endif 106#endif
107 /* 107 /*
108 * Initialize header fields. 108 * Initialize header fields.
109 *
110 * A special random number generator is used to create non predictable
111 * and non repeating ids over a long period. It also avoids reuse
112 * by switching between two distinct number cycles.
109 */ 113 */
114
110 if ((buf == NULL) || (buflen < HFIXEDSZ)) 115 if ((buf == NULL) || (buflen < HFIXEDSZ))
111 return (-1); 116 return (-1);
112 bzero(buf, HFIXEDSZ); 117 bzero(buf, HFIXEDSZ);
113 hp = (HEADER *) buf; 118 hp = (HEADER *) buf;
114 hp->id = htons(++_res.id); 119 hp->id = htons(_res.id=res_randomid());
115 hp->opcode = op; 120 hp->opcode = op;
116 hp->rd = (_res.options & RES_RECURSE) != 0; 121 hp->rd = (_res.options & RES_RECURSE) != 0;
117 hp->rcode = NOERROR; 122 hp->rcode = NOERROR;
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c
new file mode 100644
index 0000000000..d6d3a9269c
--- /dev/null
+++ b/src/lib/libc/net/res_random.c
@@ -0,0 +1,192 @@
1/* $OpenBSD: res_random.c,v 1.1 1997/04/13 21:30:47 provos 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 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Niels Provos.
23 * 4. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * seed = random 15bit
40 * n = prime, g0 = generator to n,
41 * j = random so that gcd(j,n-1) == 1
42 * g = g0^j mod n will be a generator again.
43 *
44 * X[0] = random seed.
45 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
46 * with a = 625, b = 6571, m = 31104 and a maximal period of m-1.
47 *
48 * The transaction id is determined by:
49 * id[n] = seed xor (g^X[n] mod n)
50 *
51 * Effectivly the id is restricted to the lower 15 bits, thus
52 * yielding two different cycles by toggling the msb on and off.
53 * This avoids reuse issues caused by reseeding.
54 */
55
56#include <sys/types.h>
57#include <netinet/in.h>
58#include <sys/time.h>
59#include <resolv.h>
60
61#include <unistd.h>
62#include <stdlib.h>
63#include <string.h>
64
65#define RU_MAX 20000 /* Uniq cycle, avoid blackjack prediction */
66#define RU_GEN 2 /* Starting generator */
67#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */
68#define RU_A 625
69#define RU_B 6571
70#define RU_M 31104
71
72#define PFAC_N 3
73const static u_int16_t pfacts[PFAC_N] = {
74 2,
75 3,
76 2729
77};
78
79static u_int16_t ru_x;
80static u_int16_t ru_seed;
81static u_int16_t ru_g;
82static u_int16_t ru_counter = 0;
83static u_int16_t ru_msb = 0;
84
85static u_int32_t pmod __P((u_int32_t, u_int32_t, u_int32_t));
86static void res_initid __P((void));
87
88/*
89 * Do a fast modular exponation, returned value will be in the range
90 * of 0 - (mod-1)
91 */
92
93static u_int32_t
94pmod(gen, exp, mod)
95 u_int32_t gen, exp, mod;
96{
97 u_int32_t s, t, u;
98
99 s = 1;
100 t = gen;
101 u = exp;
102
103 while (u) {
104 if (u & 1)
105 s = (s*t) % mod;
106 u >>= 1;
107 t = (t*t) % mod;
108 }
109 return (s);
110}
111
112/*
113 * Initalizes the seed and choosed a suitable generator. Also toggles
114 * the msb flag. The msb flag is used to generate two distinct
115 * cycles of random numbers and thus avoiding reuse of ids.
116 *
117 * This function is called from res_randomid() when needed, an
118 * application does not have to worry about it.
119 */
120static void
121res_initid()
122{
123 u_int16_t j, i;
124 u_int32_t tmp;
125 int noprime = 1;
126
127 tmp = arc4random();
128 ru_x = (tmp & 0xFFFF) % RU_M;
129
130 /* 15 bits of random seed */
131 ru_seed = (tmp >> 16) & 0x7FFF;
132
133 j = arc4random() % RU_N;
134
135 /*
136 * Do a fast gcd(j,RU_N-1), so we can find a j with
137 * gcd(j, RU_N-1) == 1, giving a new generator for
138 * RU_GEN^j mod RU_N
139 */
140
141 while (noprime) {
142 for (i=0; i<PFAC_N; i++)
143 if (j%pfacts[i] == 0)
144 break;
145
146 if (i>=PFAC_N)
147 noprime = 0;
148 else
149 j = (j+1) % RU_N;
150 }
151
152 ru_g = pmod(RU_GEN,j,RU_N);
153 ru_counter = 0;
154
155 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
156}
157
158u_int
159res_randomid()
160{
161 if (ru_counter % RU_MAX == 0)
162 res_initid();
163
164 ru_counter++;
165
166 /* Linear Congruential Generator */
167 ru_x = (RU_A*ru_x + RU_B) % RU_M;
168
169 return (ru_seed ^ pmod(ru_g,ru_x,RU_N)) | ru_msb;
170}
171
172#if 0
173void
174main(int argc, char **argv)
175{
176 int i, n;
177 u_int16_t wert;
178
179 res_initid();
180
181 printf("Generator: %d\n", ru_g);
182 printf("Seed: %d\n", ru_seed);
183 printf("Ru_X: %d\n", ru_x);
184
185 n = atoi(argv[1]);
186 for (i=0;i<n;i++) {
187 wert = res_randomid();
188 printf("%06d\n", wert);
189 }
190}
191#endif
192