arc4random.c revision 1.10.4.1 1 1.10.4.1 yamt /* $NetBSD: arc4random.c,v 1.10.4.1 2012/04/17 00:05:18 yamt Exp $ */
2 1.1 itojun /* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Arc4 random number generator for OpenBSD.
6 1.1 itojun * Copyright 1996 David Mazieres <dm (at) lcs.mit.edu>.
7 1.1 itojun *
8 1.1 itojun * Modification and redistribution in source and binary forms is
9 1.1 itojun * permitted provided that due credit is given to the author and the
10 1.1 itojun * OpenBSD project by leaving this copyright notice intact.
11 1.1 itojun */
12 1.1 itojun
13 1.1 itojun /*
14 1.1 itojun * This code is derived from section 17.1 of Applied Cryptography,
15 1.1 itojun * second edition, which describes a stream cipher allegedly
16 1.1 itojun * compatible with RSA Labs "RC4" cipher (the actual description of
17 1.1 itojun * which is a trade secret). The same algorithm is used as a stream
18 1.1 itojun * cipher called "arcfour" in Tatu Ylonen's ssh package.
19 1.1 itojun *
20 1.1 itojun * Here the stream cipher has been modified always to include the time
21 1.1 itojun * when initializing the state. That makes it impossible to
22 1.1 itojun * regenerate the same random sequence twice, so this can't be used
23 1.1 itojun * for encryption, but will generate good random numbers.
24 1.1 itojun *
25 1.1 itojun * RC4 is a registered trademark of RSA Laboratories.
26 1.1 itojun */
27 1.1 itojun
28 1.8 lukem #include <sys/cdefs.h>
29 1.8 lukem #if defined(LIBC_SCCS) && !defined(lint)
30 1.10.4.1 yamt __RCSID("$NetBSD: arc4random.c,v 1.10.4.1 2012/04/17 00:05:18 yamt Exp $");
31 1.8 lukem #endif /* LIBC_SCCS and not lint */
32 1.8 lukem
33 1.7 kleink #include "namespace.h"
34 1.10.4.1 yamt #include "reentrant.h"
35 1.1 itojun #include <fcntl.h>
36 1.1 itojun #include <stdlib.h>
37 1.1 itojun #include <unistd.h>
38 1.1 itojun #include <sys/types.h>
39 1.1 itojun #include <sys/param.h>
40 1.1 itojun #include <sys/time.h>
41 1.1 itojun #include <sys/sysctl.h>
42 1.1 itojun
43 1.7 kleink #ifdef __weak_alias
44 1.7 kleink __weak_alias(arc4random,_arc4random)
45 1.7 kleink #endif
46 1.7 kleink
47 1.10.4.1 yamt #define RSIZE 256
48 1.1 itojun struct arc4_stream {
49 1.10.4.1 yamt mutex_t mtx;
50 1.10.4.1 yamt int initialized;
51 1.10 christos uint8_t i;
52 1.10 christos uint8_t j;
53 1.10.4.1 yamt uint8_t s[RSIZE];
54 1.1 itojun };
55 1.1 itojun
56 1.10.4.1 yamt /* XXX lint explodes with an internal error if only mtx is initialized! */
57 1.10.4.1 yamt static struct arc4_stream rs = { .i = 0, .mtx = MUTEX_INITIALIZER };
58 1.1 itojun
59 1.1 itojun static inline void arc4_init(struct arc4_stream *);
60 1.1 itojun static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
61 1.1 itojun static void arc4_stir(struct arc4_stream *);
62 1.10 christos static inline uint8_t arc4_getbyte(struct arc4_stream *);
63 1.10 christos static inline uint32_t arc4_getword(struct arc4_stream *);
64 1.1 itojun
65 1.1 itojun static inline void
66 1.10 christos arc4_init(struct arc4_stream *as)
67 1.1 itojun {
68 1.10.4.1 yamt for (int n = 0; n < RSIZE; n++)
69 1.1 itojun as->s[n] = n;
70 1.1 itojun as->i = 0;
71 1.1 itojun as->j = 0;
72 1.10.4.1 yamt
73 1.10.4.1 yamt as->initialized = 1;
74 1.10.4.1 yamt arc4_stir(as);
75 1.1 itojun }
76 1.1 itojun
77 1.1 itojun static inline void
78 1.10 christos arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
79 1.1 itojun {
80 1.10 christos uint8_t si;
81 1.1 itojun
82 1.1 itojun as->i--;
83 1.10.4.1 yamt for (int n = 0; n < RSIZE; n++) {
84 1.1 itojun as->i = (as->i + 1);
85 1.1 itojun si = as->s[as->i];
86 1.1 itojun as->j = (as->j + si + dat[n % datlen]);
87 1.1 itojun as->s[as->i] = as->s[as->j];
88 1.1 itojun as->s[as->j] = si;
89 1.1 itojun }
90 1.1 itojun as->j = as->i;
91 1.1 itojun }
92 1.1 itojun
93 1.1 itojun static void
94 1.10 christos arc4_stir(struct arc4_stream *as)
95 1.1 itojun {
96 1.10.4.1 yamt int rdat[32];
97 1.10.4.1 yamt static const int mib[] = { CTL_KERN, KERN_URND };
98 1.10.4.1 yamt size_t len;
99 1.10.4.1 yamt
100 1.10.4.1 yamt /*
101 1.10.4.1 yamt * This code once opened and read /dev/urandom on each
102 1.10.4.1 yamt * call. That causes repeated rekeying of the kernel stream
103 1.10.4.1 yamt * generator, which is very wasteful. Because of application
104 1.10.4.1 yamt * behavior, caching the fd doesn't really help. So we just
105 1.10.4.1 yamt * fill up the tank from sysctl, which is a tiny bit slower
106 1.10.4.1 yamt * for us but much friendlier to other entropy consumers.
107 1.10.4.1 yamt */
108 1.10.4.1 yamt
109 1.10.4.1 yamt for (size_t i = 0; i < __arraycount(rdat); i++) {
110 1.10.4.1 yamt len = sizeof(rdat[i]);
111 1.10.4.1 yamt if (sysctl(mib, 2, &rdat[i], &len, NULL, 0) == -1)
112 1.10.4.1 yamt abort();
113 1.1 itojun }
114 1.1 itojun
115 1.10.4.1 yamt arc4_addrandom(as, (void *) &rdat, (int)sizeof(rdat));
116 1.3 itojun
117 1.3 itojun /*
118 1.3 itojun * Throw away the first N words of output, as suggested in the
119 1.3 itojun * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
120 1.3 itojun * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
121 1.3 itojun */
122 1.10.4.1 yamt for (size_t j = 0; j < RSIZE * 4; j++)
123 1.3 itojun arc4_getbyte(as);
124 1.1 itojun }
125 1.1 itojun
126 1.10 christos static inline uint8_t
127 1.10 christos arc4_getbyte(struct arc4_stream *as)
128 1.1 itojun {
129 1.10 christos uint8_t si, sj;
130 1.1 itojun
131 1.1 itojun as->i = (as->i + 1);
132 1.1 itojun si = as->s[as->i];
133 1.1 itojun as->j = (as->j + si);
134 1.1 itojun sj = as->s[as->j];
135 1.1 itojun as->s[as->i] = sj;
136 1.1 itojun as->s[as->j] = si;
137 1.1 itojun return (as->s[(si + sj) & 0xff]);
138 1.1 itojun }
139 1.1 itojun
140 1.10 christos static inline uint32_t
141 1.10 christos arc4_getword(struct arc4_stream *as)
142 1.1 itojun {
143 1.10 christos uint32_t val;
144 1.1 itojun val = arc4_getbyte(as) << 24;
145 1.1 itojun val |= arc4_getbyte(as) << 16;
146 1.1 itojun val |= arc4_getbyte(as) << 8;
147 1.1 itojun val |= arc4_getbyte(as);
148 1.1 itojun return val;
149 1.1 itojun }
150 1.1 itojun
151 1.10.4.1 yamt static inline void
152 1.10.4.1 yamt _arc4random_stir_unlocked(void)
153 1.10.4.1 yamt {
154 1.10.4.1 yamt if (__predict_false(!rs.initialized)) {
155 1.10.4.1 yamt arc4_init(&rs); /* stirs */
156 1.10.4.1 yamt } else {
157 1.10.4.1 yamt arc4_stir(&rs);
158 1.10.4.1 yamt }
159 1.10.4.1 yamt }
160 1.10.4.1 yamt
161 1.1 itojun void
162 1.10 christos arc4random_stir(void)
163 1.1 itojun {
164 1.10.4.1 yamt #ifdef _REENTRANT
165 1.10.4.1 yamt if (__isthreaded) {
166 1.10.4.1 yamt mutex_lock(&rs.mtx);
167 1.10.4.1 yamt _arc4random_stir_unlocked();
168 1.10.4.1 yamt mutex_unlock(&rs.mtx);
169 1.10.4.1 yamt return;
170 1.10.4.1 yamt }
171 1.10.4.1 yamt #endif
172 1.10.4.1 yamt _arc4random_stir_unlocked();
173 1.10.4.1 yamt }
174 1.10.4.1 yamt
175 1.10.4.1 yamt static inline void
176 1.10.4.1 yamt _arc4random_addrandom_unlocked(u_char *dat, int datlen)
177 1.10.4.1 yamt {
178 1.10.4.1 yamt if (__predict_false(rs.initialized)) {
179 1.1 itojun arc4_init(&rs);
180 1.1 itojun }
181 1.10.4.1 yamt arc4_addrandom(&rs, dat, datlen);
182 1.1 itojun }
183 1.1 itojun
184 1.1 itojun void
185 1.10 christos arc4random_addrandom(u_char *dat, int datlen)
186 1.1 itojun {
187 1.10.4.1 yamt #ifdef _REENTRANT
188 1.10.4.1 yamt if (__isthreaded) {
189 1.10.4.1 yamt mutex_lock(&rs.mtx);
190 1.10.4.1 yamt _arc4random_addrandom_unlocked(dat, datlen);
191 1.10.4.1 yamt mutex_unlock(&rs.mtx);
192 1.10.4.1 yamt return;
193 1.10.4.1 yamt }
194 1.10.4.1 yamt #endif
195 1.10.4.1 yamt _arc4random_addrandom_unlocked(dat, datlen);
196 1.10.4.1 yamt }
197 1.10.4.1 yamt
198 1.10.4.1 yamt static inline uint32_t
199 1.10.4.1 yamt _arc4random_unlocked(void)
200 1.10.4.1 yamt {
201 1.10.4.1 yamt if (__predict_false(!rs.initialized)) {
202 1.10.4.1 yamt arc4_init(&rs);
203 1.10.4.1 yamt }
204 1.10.4.1 yamt return arc4_getword(&rs);
205 1.1 itojun }
206 1.1 itojun
207 1.10 christos uint32_t
208 1.10 christos arc4random(void)
209 1.1 itojun {
210 1.10.4.1 yamt uint32_t v;
211 1.10.4.1 yamt #ifdef _REENTRANT
212 1.10.4.1 yamt if (__isthreaded) {
213 1.10.4.1 yamt mutex_lock(&rs.mtx);
214 1.10.4.1 yamt v = _arc4random_unlocked();
215 1.10.4.1 yamt mutex_unlock(&rs.mtx);
216 1.10.4.1 yamt return v;
217 1.10.4.1 yamt }
218 1.10.4.1 yamt #endif
219 1.10.4.1 yamt v = _arc4random_unlocked();
220 1.10.4.1 yamt return v;
221 1.1 itojun }
222 1.1 itojun
223 1.10.4.1 yamt static void
224 1.10.4.1 yamt _arc4random_buf_unlocked(void *buf, size_t len)
225 1.10 christos {
226 1.10 christos uint8_t *bp = buf;
227 1.10 christos uint8_t *ep = bp + len;
228 1.10 christos
229 1.10.4.1 yamt if (__predict_false(!rs.initialized)) {
230 1.10.4.1 yamt arc4_init(&rs);
231 1.10.4.1 yamt }
232 1.10.4.1 yamt
233 1.10 christos bp[0] = arc4_getbyte(&rs) % 3;
234 1.10 christos while (bp[0]--)
235 1.10 christos (void)arc4_getbyte(&rs);
236 1.10 christos
237 1.10 christos while (bp < ep)
238 1.10 christos *bp++ = arc4_getbyte(&rs);
239 1.10 christos }
240 1.10 christos
241 1.10.4.1 yamt void
242 1.10.4.1 yamt arc4random_buf(void *buf, size_t len)
243 1.10.4.1 yamt {
244 1.10.4.1 yamt #ifdef _REENTRANT
245 1.10.4.1 yamt if (__isthreaded) {
246 1.10.4.1 yamt mutex_lock(&rs.mtx);
247 1.10.4.1 yamt _arc4random_buf_unlocked(buf, len);
248 1.10.4.1 yamt mutex_unlock(&rs.mtx);
249 1.10.4.1 yamt return;
250 1.10.4.1 yamt } else
251 1.10.4.1 yamt #endif
252 1.10.4.1 yamt _arc4random_buf_unlocked(buf, len);
253 1.10.4.1 yamt }
254 1.10.4.1 yamt
255 1.10 christos /*-
256 1.10 christos * Written by Damien Miller.
257 1.10 christos * With simplifications by Jinmei Tatuya.
258 1.10 christos */
259 1.10 christos
260 1.10 christos /*
261 1.10 christos * Calculate a uniformly distributed random number less than
262 1.10 christos * upper_bound avoiding "modulo bias".
263 1.10 christos *
264 1.10 christos * Uniformity is achieved by generating new random numbers
265 1.10 christos * until the one returned is outside the range
266 1.10 christos * [0, 2^32 % upper_bound[. This guarantees the selected
267 1.10 christos * random number will be inside the range
268 1.10 christos * [2^32 % upper_bound, 2^32[ which maps back to
269 1.10 christos * [0, upper_bound[ after reduction modulo upper_bound.
270 1.10 christos */
271 1.10.4.1 yamt static uint32_t
272 1.10.4.1 yamt _arc4random_uniform_unlocked(uint32_t upper_bound)
273 1.10 christos {
274 1.10 christos uint32_t r, min;
275 1.10 christos
276 1.10 christos if (upper_bound < 2)
277 1.10 christos return 0;
278 1.10 christos
279 1.10 christos #if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL)
280 1.10.4.1 yamt min = (uint32_t)(0x100000000U % upper_bound);
281 1.10 christos #else
282 1.10 christos /* calculate (2^32 % upper_bound) avoiding 64-bit math */
283 1.10 christos if (upper_bound > 0x80000000U)
284 1.10 christos /* 2^32 - upper_bound (only one "value area") */
285 1.10 christos min = 1 + ~upper_bound;
286 1.10 christos else
287 1.10 christos /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
288 1.10 christos min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
289 1.10 christos #endif
290 1.10 christos
291 1.10 christos /*
292 1.10 christos * This could theoretically loop forever but each retry has
293 1.10 christos * p > 0.5 (worst case, usually far better) of selecting a
294 1.10 christos * number inside the range we need, so it should rarely need
295 1.10 christos * to re-roll (at all).
296 1.10 christos */
297 1.10.4.1 yamt if (__predict_false(!rs.initialized)) {
298 1.10.4.1 yamt arc4_init(&rs);
299 1.10.4.1 yamt }
300 1.10 christos if (arc4_getbyte(&rs) & 1)
301 1.10 christos (void)arc4_getbyte(&rs);
302 1.10 christos do
303 1.10 christos r = arc4_getword(&rs);
304 1.10 christos while (r < min);
305 1.10 christos
306 1.10 christos return r % upper_bound;
307 1.10 christos }
308 1.10 christos
309 1.10.4.1 yamt uint32_t
310 1.10.4.1 yamt arc4random_uniform(uint32_t upper_bound)
311 1.10.4.1 yamt {
312 1.10.4.1 yamt uint32_t v;
313 1.10.4.1 yamt #ifdef _REENTRANT
314 1.10.4.1 yamt if (__isthreaded) {
315 1.10.4.1 yamt mutex_lock(&rs.mtx);
316 1.10.4.1 yamt v = _arc4random_uniform_unlocked(upper_bound);
317 1.10.4.1 yamt mutex_unlock(&rs.mtx);
318 1.10.4.1 yamt return v;
319 1.10.4.1 yamt }
320 1.1 itojun #endif
321 1.10.4.1 yamt v = _arc4random_uniform_unlocked(upper_bound);
322 1.10.4.1 yamt return v;
323 1.10.4.1 yamt }
324