arc4random.c revision 1.19 1 1.19 dsl /* $NetBSD: arc4random.c,v 1.19 2012/08/20 20:32:09 dsl 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.19 dsl __RCSID("$NetBSD: arc4random.c,v 1.19 2012/08/20 20:32:09 dsl 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.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.1 itojun struct arc4_stream {
48 1.18 dsl uint8_t stirred;
49 1.18 dsl uint8_t pad;
50 1.10 christos uint8_t i;
51 1.10 christos uint8_t j;
52 1.18 dsl uint8_t s[(uint8_t)~0u + 1u]; /* 256 to you and me */
53 1.18 dsl mutex_t mtx;
54 1.1 itojun };
55 1.1 itojun
56 1.16 dsl #ifdef _REENTRANT
57 1.16 dsl #define LOCK(rs) { \
58 1.16 dsl int isthreaded = __isthreaded; \
59 1.16 dsl if (isthreaded) \
60 1.16 dsl mutex_lock(&(rs)->mtx);
61 1.16 dsl #define UNLOCK(rs) \
62 1.16 dsl if (isthreaded) \
63 1.16 dsl mutex_unlock(&(rs)->mtx); \
64 1.16 dsl }
65 1.16 dsl #else
66 1.16 dsl #define LOCK(rs)
67 1.16 dsl #define UNLOCK(rs)
68 1.16 dsl #endif
69 1.16 dsl
70 1.18 dsl #define S(n) (n)
71 1.18 dsl #define S4(n) S(n), S(n + 1), S(n + 2), S(n + 3)
72 1.18 dsl #define S16(n) S4(n), S4(n + 4), S4(n + 8), S4(n + 12)
73 1.18 dsl #define S64(n) S16(n), S16(n + 16), S16(n + 32), S16(n + 48)
74 1.18 dsl #define S256 S64(0), S64(64), S64(128), S64(192)
75 1.18 dsl
76 1.18 dsl static struct arc4_stream rs = { .i = 0xff, .j = 0, .s = { S256 },
77 1.18 dsl .stirred = 0, .mtx = MUTEX_INITIALIZER };
78 1.18 dsl
79 1.18 dsl #undef S
80 1.18 dsl #undef S4
81 1.18 dsl #undef S16
82 1.18 dsl #undef S64
83 1.18 dsl #undef S256
84 1.1 itojun
85 1.1 itojun static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
86 1.18 dsl static __noinline void arc4_stir(struct arc4_stream *);
87 1.10 christos static inline uint8_t arc4_getbyte(struct arc4_stream *);
88 1.10 christos static inline uint32_t arc4_getword(struct arc4_stream *);
89 1.1 itojun
90 1.15 dsl static inline int
91 1.15 dsl arc4_check_init(struct arc4_stream *as)
92 1.15 dsl {
93 1.18 dsl if (__predict_true(rs.stirred))
94 1.15 dsl return 0;
95 1.15 dsl
96 1.18 dsl arc4_stir(as);
97 1.15 dsl return 1;
98 1.15 dsl }
99 1.15 dsl
100 1.1 itojun static inline void
101 1.10 christos arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
102 1.1 itojun {
103 1.10 christos uint8_t si;
104 1.18 dsl size_t n;
105 1.1 itojun
106 1.18 dsl for (n = 0; n < __arraycount(as->s); n++) {
107 1.1 itojun as->i = (as->i + 1);
108 1.1 itojun si = as->s[as->i];
109 1.1 itojun as->j = (as->j + si + dat[n % datlen]);
110 1.1 itojun as->s[as->i] = as->s[as->j];
111 1.1 itojun as->s[as->j] = si;
112 1.1 itojun }
113 1.1 itojun }
114 1.1 itojun
115 1.18 dsl static __noinline void
116 1.10 christos arc4_stir(struct arc4_stream *as)
117 1.1 itojun {
118 1.13 christos int rdat[32];
119 1.14 dsl int mib[] = { CTL_KERN, KERN_URND };
120 1.11 tls size_t len;
121 1.14 dsl size_t i, j;
122 1.1 itojun
123 1.11 tls /*
124 1.11 tls * This code once opened and read /dev/urandom on each
125 1.11 tls * call. That causes repeated rekeying of the kernel stream
126 1.11 tls * generator, which is very wasteful. Because of application
127 1.11 tls * behavior, caching the fd doesn't really help. So we just
128 1.11 tls * fill up the tank from sysctl, which is a tiny bit slower
129 1.11 tls * for us but much friendlier to other entropy consumers.
130 1.11 tls */
131 1.11 tls
132 1.14 dsl for (i = 0; i < __arraycount(rdat); i++) {
133 1.11 tls len = sizeof(rdat[i]);
134 1.11 tls if (sysctl(mib, 2, &rdat[i], &len, NULL, 0) == -1)
135 1.11 tls abort();
136 1.1 itojun }
137 1.1 itojun
138 1.13 christos arc4_addrandom(as, (void *) &rdat, (int)sizeof(rdat));
139 1.3 itojun
140 1.3 itojun /*
141 1.3 itojun * Throw away the first N words of output, as suggested in the
142 1.3 itojun * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
143 1.3 itojun * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
144 1.3 itojun */
145 1.18 dsl for (j = 0; j < __arraycount(as->s) * 4; j++)
146 1.3 itojun arc4_getbyte(as);
147 1.18 dsl
148 1.18 dsl as->stirred = 1;
149 1.1 itojun }
150 1.1 itojun
151 1.17 dsl static __always_inline uint8_t
152 1.17 dsl arc4_getbyte_ij(struct arc4_stream *as, uint8_t *i, uint8_t *j)
153 1.1 itojun {
154 1.10 christos uint8_t si, sj;
155 1.1 itojun
156 1.17 dsl *i = *i + 1;
157 1.17 dsl si = as->s[*i];
158 1.17 dsl *j = *j + si;
159 1.17 dsl sj = as->s[*j];
160 1.17 dsl as->s[*i] = sj;
161 1.17 dsl as->s[*j] = si;
162 1.1 itojun return (as->s[(si + sj) & 0xff]);
163 1.1 itojun }
164 1.1 itojun
165 1.17 dsl static inline uint8_t
166 1.17 dsl arc4_getbyte(struct arc4_stream *as)
167 1.17 dsl {
168 1.17 dsl return arc4_getbyte_ij(as, &as->i, &as->j);
169 1.17 dsl }
170 1.17 dsl
171 1.10 christos static inline uint32_t
172 1.10 christos arc4_getword(struct arc4_stream *as)
173 1.1 itojun {
174 1.10 christos uint32_t val;
175 1.1 itojun val = arc4_getbyte(as) << 24;
176 1.1 itojun val |= arc4_getbyte(as) << 16;
177 1.1 itojun val |= arc4_getbyte(as) << 8;
178 1.1 itojun val |= arc4_getbyte(as);
179 1.1 itojun return val;
180 1.1 itojun }
181 1.1 itojun
182 1.16 dsl void
183 1.16 dsl arc4random_stir(void)
184 1.1 itojun {
185 1.16 dsl LOCK(&rs);
186 1.18 dsl arc4_stir(&rs);
187 1.16 dsl UNLOCK(&rs);
188 1.1 itojun }
189 1.1 itojun
190 1.1 itojun void
191 1.16 dsl arc4random_addrandom(u_char *dat, int datlen)
192 1.11 tls {
193 1.16 dsl LOCK(&rs);
194 1.15 dsl arc4_check_init(&rs);
195 1.11 tls arc4_addrandom(&rs, dat, datlen);
196 1.16 dsl UNLOCK(&rs);
197 1.1 itojun }
198 1.1 itojun
199 1.10 christos uint32_t
200 1.10 christos arc4random(void)
201 1.1 itojun {
202 1.11 tls uint32_t v;
203 1.16 dsl
204 1.16 dsl LOCK(&rs);
205 1.16 dsl arc4_check_init(&rs);
206 1.16 dsl v = arc4_getword(&rs);
207 1.16 dsl UNLOCK(&rs);
208 1.11 tls return v;
209 1.1 itojun }
210 1.1 itojun
211 1.16 dsl void
212 1.16 dsl arc4random_buf(void *buf, size_t len)
213 1.10 christos {
214 1.10 christos uint8_t *bp = buf;
215 1.10 christos uint8_t *ep = bp + len;
216 1.17 dsl uint8_t i, j;
217 1.10 christos
218 1.16 dsl LOCK(&rs);
219 1.15 dsl arc4_check_init(&rs);
220 1.12 tls
221 1.17 dsl /* cache i and j - compiler can't know 'buf' doesn't alias them */
222 1.17 dsl i = rs.i;
223 1.17 dsl j = rs.j;
224 1.17 dsl
225 1.10 christos while (bp < ep)
226 1.17 dsl *bp++ = arc4_getbyte_ij(&rs, &i, &j);
227 1.17 dsl rs.i = i;
228 1.17 dsl rs.j = j;
229 1.17 dsl
230 1.16 dsl UNLOCK(&rs);
231 1.11 tls }
232 1.11 tls
233 1.10 christos /*-
234 1.10 christos * Written by Damien Miller.
235 1.10 christos * With simplifications by Jinmei Tatuya.
236 1.10 christos */
237 1.10 christos
238 1.10 christos /*
239 1.10 christos * Calculate a uniformly distributed random number less than
240 1.10 christos * upper_bound avoiding "modulo bias".
241 1.10 christos *
242 1.10 christos * Uniformity is achieved by generating new random numbers
243 1.10 christos * until the one returned is outside the range
244 1.10 christos * [0, 2^32 % upper_bound[. This guarantees the selected
245 1.10 christos * random number will be inside the range
246 1.10 christos * [2^32 % upper_bound, 2^32[ which maps back to
247 1.10 christos * [0, upper_bound[ after reduction modulo upper_bound.
248 1.10 christos */
249 1.16 dsl uint32_t
250 1.16 dsl arc4random_uniform(uint32_t upper_bound)
251 1.10 christos {
252 1.10 christos uint32_t r, min;
253 1.10 christos
254 1.10 christos if (upper_bound < 2)
255 1.10 christos return 0;
256 1.10 christos
257 1.10 christos /* calculate (2^32 % upper_bound) avoiding 64-bit math */
258 1.16 dsl /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
259 1.16 dsl min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
260 1.16 dsl
261 1.16 dsl LOCK(&rs);
262 1.16 dsl arc4_check_init(&rs);
263 1.16 dsl
264 1.10 christos /*
265 1.10 christos * This could theoretically loop forever but each retry has
266 1.10 christos * p > 0.5 (worst case, usually far better) of selecting a
267 1.10 christos * number inside the range we need, so it should rarely need
268 1.10 christos * to re-roll (at all).
269 1.10 christos */
270 1.10 christos do
271 1.10 christos r = arc4_getword(&rs);
272 1.10 christos while (r < min);
273 1.16 dsl UNLOCK(&rs);
274 1.10 christos
275 1.10 christos return r % upper_bound;
276 1.10 christos }
277