arc4random.c revision 1.25 1 1.25 roy /* $NetBSD: arc4random.c,v 1.25 2014/07/19 14:53:22 roy 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.25 roy __RCSID("$NetBSD: arc4random.c,v 1.25 2014/07/19 14:53:22 roy Exp $");
31 1.8 lukem #endif /* LIBC_SCCS and not lint */
32 1.8 lukem
33 1.7 kleink #include "namespace.h"
34 1.11 tls #include "reentrant.h"
35 1.1 itojun #include <fcntl.h>
36 1.22 roy #include <pthread.h>
37 1.22 roy #include <stdbool.h>
38 1.1 itojun #include <stdlib.h>
39 1.1 itojun #include <unistd.h>
40 1.1 itojun #include <sys/types.h>
41 1.1 itojun #include <sys/param.h>
42 1.1 itojun #include <sys/time.h>
43 1.1 itojun #include <sys/sysctl.h>
44 1.1 itojun
45 1.7 kleink #ifdef __weak_alias
46 1.7 kleink __weak_alias(arc4random,_arc4random)
47 1.20 dsl __weak_alias(arc4random_addrandom,_arc4random_addrandom)
48 1.20 dsl __weak_alias(arc4random_buf,_arc4random_buf)
49 1.20 dsl __weak_alias(arc4random_stir,_arc4random_stir)
50 1.20 dsl __weak_alias(arc4random_uniform,_arc4random_uniform)
51 1.7 kleink #endif
52 1.7 kleink
53 1.23 apb #define REKEY_BYTES 1600000
54 1.23 apb
55 1.1 itojun struct arc4_stream {
56 1.22 roy bool inited;
57 1.10 christos uint8_t i;
58 1.10 christos uint8_t j;
59 1.18 dsl uint8_t s[(uint8_t)~0u + 1u]; /* 256 to you and me */
60 1.22 roy size_t count;
61 1.18 dsl mutex_t mtx;
62 1.1 itojun };
63 1.1 itojun
64 1.16 dsl #ifdef _REENTRANT
65 1.23 apb #define LOCK(rs) do { \
66 1.24 apb if (__isthreaded) mutex_lock(&(rs)->mtx); \
67 1.23 apb } while (/*CONSTCOND*/ 0)
68 1.23 apb #define UNLOCK(rs) do { \
69 1.23 apb if (__isthreaded) mutex_unlock(&(rs)->mtx); \
70 1.23 apb } while (/*CONSTCOND*/ 0)
71 1.16 dsl #else
72 1.22 roy #define LOCK(rs)
73 1.16 dsl #define UNLOCK(rs)
74 1.16 dsl #endif
75 1.16 dsl
76 1.18 dsl #define S(n) (n)
77 1.18 dsl #define S4(n) S(n), S(n + 1), S(n + 2), S(n + 3)
78 1.18 dsl #define S16(n) S4(n), S4(n + 4), S4(n + 8), S4(n + 12)
79 1.18 dsl #define S64(n) S16(n), S16(n + 16), S16(n + 32), S16(n + 48)
80 1.18 dsl #define S256 S64(0), S64(64), S64(128), S64(192)
81 1.18 dsl
82 1.22 roy static struct arc4_stream rs = { .inited = false,
83 1.22 roy .i = 0xff, .j = 0, .s = { S256 },
84 1.22 roy .count = 0, .mtx = MUTEX_INITIALIZER };
85 1.18 dsl
86 1.18 dsl #undef S
87 1.18 dsl #undef S4
88 1.18 dsl #undef S16
89 1.18 dsl #undef S64
90 1.18 dsl #undef S256
91 1.1 itojun
92 1.1 itojun static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
93 1.18 dsl static __noinline void arc4_stir(struct arc4_stream *);
94 1.10 christos static inline uint8_t arc4_getbyte(struct arc4_stream *);
95 1.10 christos static inline uint32_t arc4_getword(struct arc4_stream *);
96 1.1 itojun
97 1.22 roy #ifdef _REENTRANT
98 1.22 roy static void
99 1.22 roy arc4_fork_prepare(void)
100 1.22 roy {
101 1.22 roy
102 1.22 roy LOCK(&rs);
103 1.22 roy }
104 1.22 roy
105 1.22 roy static void
106 1.22 roy arc4_fork_parent(void)
107 1.22 roy {
108 1.22 roy
109 1.22 roy UNLOCK(&rs);
110 1.22 roy }
111 1.22 roy #else
112 1.22 roy #define arc4_fork_prepare NULL
113 1.22 roy #define arc4_fork_parent NULL
114 1.22 roy #endif
115 1.22 roy
116 1.22 roy static void
117 1.22 roy arc4_fork_child(void)
118 1.22 roy {
119 1.22 roy
120 1.22 roy /* Reset the counter to a force new stir after forking */
121 1.22 roy rs.count = 0;
122 1.22 roy UNLOCK(&rs);
123 1.22 roy }
124 1.22 roy
125 1.22 roy static inline void
126 1.15 dsl arc4_check_init(struct arc4_stream *as)
127 1.15 dsl {
128 1.25 roy /*
129 1.25 roy * pthread_atfork(3) only allows async-signal-safe functions in
130 1.25 roy * the child handler.
131 1.25 roy * NetBSD's mutex_unlock is async-signal safe, other implementations
132 1.25 roy * may not be.
133 1.25 roy */
134 1.15 dsl
135 1.22 roy if (__predict_false(!as->inited)) {
136 1.22 roy as->inited = true;
137 1.22 roy pthread_atfork(arc4_fork_prepare,
138 1.22 roy arc4_fork_parent, arc4_fork_child);
139 1.22 roy }
140 1.15 dsl }
141 1.15 dsl
142 1.1 itojun static inline void
143 1.10 christos arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
144 1.1 itojun {
145 1.10 christos uint8_t si;
146 1.18 dsl size_t n;
147 1.1 itojun
148 1.18 dsl for (n = 0; n < __arraycount(as->s); n++) {
149 1.1 itojun as->i = (as->i + 1);
150 1.1 itojun si = as->s[as->i];
151 1.1 itojun as->j = (as->j + si + dat[n % datlen]);
152 1.1 itojun as->s[as->i] = as->s[as->j];
153 1.1 itojun as->s[as->j] = si;
154 1.1 itojun }
155 1.1 itojun }
156 1.1 itojun
157 1.18 dsl static __noinline void
158 1.10 christos arc4_stir(struct arc4_stream *as)
159 1.1 itojun {
160 1.13 christos int rdat[32];
161 1.14 dsl int mib[] = { CTL_KERN, KERN_URND };
162 1.11 tls size_t len;
163 1.14 dsl size_t i, j;
164 1.1 itojun
165 1.22 roy arc4_check_init(as);
166 1.22 roy
167 1.11 tls /*
168 1.11 tls * This code once opened and read /dev/urandom on each
169 1.11 tls * call. That causes repeated rekeying of the kernel stream
170 1.11 tls * generator, which is very wasteful. Because of application
171 1.11 tls * behavior, caching the fd doesn't really help. So we just
172 1.11 tls * fill up the tank from sysctl, which is a tiny bit slower
173 1.11 tls * for us but much friendlier to other entropy consumers.
174 1.11 tls */
175 1.11 tls
176 1.14 dsl for (i = 0; i < __arraycount(rdat); i++) {
177 1.11 tls len = sizeof(rdat[i]);
178 1.11 tls if (sysctl(mib, 2, &rdat[i], &len, NULL, 0) == -1)
179 1.11 tls abort();
180 1.1 itojun }
181 1.1 itojun
182 1.13 christos arc4_addrandom(as, (void *) &rdat, (int)sizeof(rdat));
183 1.3 itojun
184 1.3 itojun /*
185 1.3 itojun * Throw away the first N words of output, as suggested in the
186 1.3 itojun * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
187 1.3 itojun * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
188 1.3 itojun */
189 1.22 roy for (j = 0; j < __arraycount(as->s) * sizeof(uint32_t); j++)
190 1.3 itojun arc4_getbyte(as);
191 1.18 dsl
192 1.23 apb /* Stir again after REKEY_BYTES bytes, or if the pid changes */
193 1.23 apb as->count = REKEY_BYTES;
194 1.22 roy }
195 1.22 roy
196 1.22 roy static inline void
197 1.22 roy arc4_stir_if_needed(struct arc4_stream *as, size_t len)
198 1.22 roy {
199 1.22 roy
200 1.22 roy if (__predict_false(as->count <= len))
201 1.22 roy arc4_stir(as);
202 1.22 roy else
203 1.22 roy as->count -= len;
204 1.1 itojun }
205 1.1 itojun
206 1.21 christos static __inline uint8_t
207 1.17 dsl arc4_getbyte_ij(struct arc4_stream *as, uint8_t *i, uint8_t *j)
208 1.1 itojun {
209 1.10 christos uint8_t si, sj;
210 1.1 itojun
211 1.17 dsl *i = *i + 1;
212 1.17 dsl si = as->s[*i];
213 1.17 dsl *j = *j + si;
214 1.17 dsl sj = as->s[*j];
215 1.17 dsl as->s[*i] = sj;
216 1.17 dsl as->s[*j] = si;
217 1.1 itojun return (as->s[(si + sj) & 0xff]);
218 1.1 itojun }
219 1.1 itojun
220 1.17 dsl static inline uint8_t
221 1.17 dsl arc4_getbyte(struct arc4_stream *as)
222 1.17 dsl {
223 1.22 roy
224 1.17 dsl return arc4_getbyte_ij(as, &as->i, &as->j);
225 1.17 dsl }
226 1.17 dsl
227 1.10 christos static inline uint32_t
228 1.10 christos arc4_getword(struct arc4_stream *as)
229 1.1 itojun {
230 1.10 christos uint32_t val;
231 1.22 roy
232 1.1 itojun val = arc4_getbyte(as) << 24;
233 1.1 itojun val |= arc4_getbyte(as) << 16;
234 1.1 itojun val |= arc4_getbyte(as) << 8;
235 1.1 itojun val |= arc4_getbyte(as);
236 1.1 itojun return val;
237 1.1 itojun }
238 1.1 itojun
239 1.16 dsl void
240 1.16 dsl arc4random_stir(void)
241 1.1 itojun {
242 1.22 roy
243 1.16 dsl LOCK(&rs);
244 1.18 dsl arc4_stir(&rs);
245 1.16 dsl UNLOCK(&rs);
246 1.1 itojun }
247 1.1 itojun
248 1.1 itojun void
249 1.16 dsl arc4random_addrandom(u_char *dat, int datlen)
250 1.11 tls {
251 1.22 roy
252 1.16 dsl LOCK(&rs);
253 1.22 roy arc4_stir_if_needed(&rs, datlen);
254 1.11 tls arc4_addrandom(&rs, dat, datlen);
255 1.16 dsl UNLOCK(&rs);
256 1.1 itojun }
257 1.1 itojun
258 1.10 christos uint32_t
259 1.10 christos arc4random(void)
260 1.1 itojun {
261 1.11 tls uint32_t v;
262 1.16 dsl
263 1.16 dsl LOCK(&rs);
264 1.22 roy arc4_stir_if_needed(&rs, sizeof(v));
265 1.16 dsl v = arc4_getword(&rs);
266 1.16 dsl UNLOCK(&rs);
267 1.11 tls return v;
268 1.1 itojun }
269 1.1 itojun
270 1.16 dsl void
271 1.16 dsl arc4random_buf(void *buf, size_t len)
272 1.10 christos {
273 1.10 christos uint8_t *bp = buf;
274 1.10 christos uint8_t *ep = bp + len;
275 1.17 dsl uint8_t i, j;
276 1.10 christos
277 1.16 dsl LOCK(&rs);
278 1.22 roy arc4_stir_if_needed(&rs, len);
279 1.12 tls
280 1.17 dsl /* cache i and j - compiler can't know 'buf' doesn't alias them */
281 1.17 dsl i = rs.i;
282 1.17 dsl j = rs.j;
283 1.17 dsl
284 1.10 christos while (bp < ep)
285 1.17 dsl *bp++ = arc4_getbyte_ij(&rs, &i, &j);
286 1.17 dsl rs.i = i;
287 1.17 dsl rs.j = j;
288 1.17 dsl
289 1.16 dsl UNLOCK(&rs);
290 1.11 tls }
291 1.11 tls
292 1.10 christos /*-
293 1.10 christos * Written by Damien Miller.
294 1.10 christos * With simplifications by Jinmei Tatuya.
295 1.10 christos */
296 1.10 christos
297 1.10 christos /*
298 1.10 christos * Calculate a uniformly distributed random number less than
299 1.10 christos * upper_bound avoiding "modulo bias".
300 1.10 christos *
301 1.10 christos * Uniformity is achieved by generating new random numbers
302 1.10 christos * until the one returned is outside the range
303 1.10 christos * [0, 2^32 % upper_bound[. This guarantees the selected
304 1.10 christos * random number will be inside the range
305 1.10 christos * [2^32 % upper_bound, 2^32[ which maps back to
306 1.10 christos * [0, upper_bound[ after reduction modulo upper_bound.
307 1.10 christos */
308 1.16 dsl uint32_t
309 1.16 dsl arc4random_uniform(uint32_t upper_bound)
310 1.10 christos {
311 1.10 christos uint32_t r, min;
312 1.10 christos
313 1.10 christos if (upper_bound < 2)
314 1.10 christos return 0;
315 1.10 christos
316 1.10 christos /* calculate (2^32 % upper_bound) avoiding 64-bit math */
317 1.16 dsl /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
318 1.16 dsl min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
319 1.16 dsl
320 1.16 dsl LOCK(&rs);
321 1.22 roy arc4_stir_if_needed(&rs, sizeof(r));
322 1.16 dsl
323 1.10 christos /*
324 1.10 christos * This could theoretically loop forever but each retry has
325 1.10 christos * p > 0.5 (worst case, usually far better) of selecting a
326 1.10 christos * number inside the range we need, so it should rarely need
327 1.10 christos * to re-roll (at all).
328 1.10 christos */
329 1.10 christos do
330 1.10 christos r = arc4_getword(&rs);
331 1.10 christos while (r < min);
332 1.16 dsl UNLOCK(&rs);
333 1.10 christos
334 1.10 christos return r % upper_bound;
335 1.10 christos }
336