arc4random.c revision 1.9.40.1 1 1.9.40.1 bouyer /* $NetBSD: arc4random.c,v 1.9.40.1 2011/02/08 16:18:59 bouyer 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.9.40.1 bouyer __RCSID("$NetBSD: arc4random.c,v 1.9.40.1 2011/02/08 16:18:59 bouyer Exp $");
31 1.8 lukem #endif /* LIBC_SCCS and not lint */
32 1.8 lukem
33 1.7 kleink #include "namespace.h"
34 1.1 itojun #include <fcntl.h>
35 1.1 itojun #include <stdlib.h>
36 1.1 itojun #include <unistd.h>
37 1.1 itojun #include <sys/types.h>
38 1.1 itojun #include <sys/param.h>
39 1.1 itojun #include <sys/time.h>
40 1.1 itojun #include <sys/sysctl.h>
41 1.1 itojun
42 1.7 kleink #ifdef __weak_alias
43 1.7 kleink __weak_alias(arc4random,_arc4random)
44 1.7 kleink #endif
45 1.7 kleink
46 1.1 itojun struct arc4_stream {
47 1.9.40.1 bouyer uint8_t i;
48 1.9.40.1 bouyer uint8_t j;
49 1.9.40.1 bouyer uint8_t s[256];
50 1.1 itojun };
51 1.1 itojun
52 1.6 kleink static int rs_initialized;
53 1.1 itojun static struct arc4_stream rs;
54 1.1 itojun
55 1.1 itojun static inline void arc4_init(struct arc4_stream *);
56 1.1 itojun static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
57 1.1 itojun static void arc4_stir(struct arc4_stream *);
58 1.9.40.1 bouyer static inline uint8_t arc4_getbyte(struct arc4_stream *);
59 1.9.40.1 bouyer static inline uint32_t arc4_getword(struct arc4_stream *);
60 1.1 itojun
61 1.1 itojun static inline void
62 1.9.40.1 bouyer arc4_init(struct arc4_stream *as)
63 1.1 itojun {
64 1.1 itojun int n;
65 1.1 itojun
66 1.1 itojun for (n = 0; n < 256; n++)
67 1.1 itojun as->s[n] = n;
68 1.1 itojun as->i = 0;
69 1.1 itojun as->j = 0;
70 1.1 itojun }
71 1.1 itojun
72 1.1 itojun static inline void
73 1.9.40.1 bouyer arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
74 1.1 itojun {
75 1.1 itojun int n;
76 1.9.40.1 bouyer uint8_t si;
77 1.1 itojun
78 1.1 itojun as->i--;
79 1.1 itojun for (n = 0; n < 256; n++) {
80 1.1 itojun as->i = (as->i + 1);
81 1.1 itojun si = as->s[as->i];
82 1.1 itojun as->j = (as->j + si + dat[n % datlen]);
83 1.1 itojun as->s[as->i] = as->s[as->j];
84 1.1 itojun as->s[as->j] = si;
85 1.1 itojun }
86 1.1 itojun as->j = as->i;
87 1.1 itojun }
88 1.1 itojun
89 1.1 itojun static void
90 1.9.40.1 bouyer arc4_stir(struct arc4_stream *as)
91 1.1 itojun {
92 1.1 itojun int fd;
93 1.1 itojun struct {
94 1.1 itojun struct timeval tv;
95 1.1 itojun u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)];
96 1.1 itojun } rdat;
97 1.3 itojun int n;
98 1.1 itojun
99 1.1 itojun gettimeofday(&rdat.tv, NULL);
100 1.2 itojun fd = open("/dev/urandom", O_RDONLY);
101 1.1 itojun if (fd != -1) {
102 1.1 itojun read(fd, rdat.rnd, sizeof(rdat.rnd));
103 1.1 itojun close(fd);
104 1.1 itojun }
105 1.4 itojun #ifdef KERN_URND
106 1.1 itojun else {
107 1.5 thorpej int mib[2];
108 1.5 thorpej u_int i;
109 1.1 itojun size_t len;
110 1.1 itojun
111 1.1 itojun /* Device could not be opened, we might be chrooted, take
112 1.1 itojun * randomness from sysctl. */
113 1.1 itojun
114 1.1 itojun mib[0] = CTL_KERN;
115 1.4 itojun mib[1] = KERN_URND;
116 1.1 itojun
117 1.5 thorpej for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i++) {
118 1.1 itojun len = sizeof(u_int);
119 1.1 itojun if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1)
120 1.1 itojun break;
121 1.1 itojun }
122 1.1 itojun }
123 1.1 itojun #endif
124 1.1 itojun /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take
125 1.1 itojun * whatever was on the stack... */
126 1.1 itojun
127 1.1 itojun arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
128 1.3 itojun
129 1.3 itojun /*
130 1.3 itojun * Throw away the first N words of output, as suggested in the
131 1.3 itojun * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
132 1.3 itojun * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
133 1.3 itojun */
134 1.3 itojun for (n = 0; n < 256 * 4; n++)
135 1.3 itojun arc4_getbyte(as);
136 1.1 itojun }
137 1.1 itojun
138 1.9.40.1 bouyer static inline uint8_t
139 1.9.40.1 bouyer arc4_getbyte(struct arc4_stream *as)
140 1.1 itojun {
141 1.9.40.1 bouyer uint8_t si, sj;
142 1.1 itojun
143 1.1 itojun as->i = (as->i + 1);
144 1.1 itojun si = as->s[as->i];
145 1.1 itojun as->j = (as->j + si);
146 1.1 itojun sj = as->s[as->j];
147 1.1 itojun as->s[as->i] = sj;
148 1.1 itojun as->s[as->j] = si;
149 1.1 itojun return (as->s[(si + sj) & 0xff]);
150 1.1 itojun }
151 1.1 itojun
152 1.9.40.1 bouyer static inline uint32_t
153 1.9.40.1 bouyer arc4_getword(struct arc4_stream *as)
154 1.1 itojun {
155 1.9.40.1 bouyer uint32_t val;
156 1.1 itojun val = arc4_getbyte(as) << 24;
157 1.1 itojun val |= arc4_getbyte(as) << 16;
158 1.1 itojun val |= arc4_getbyte(as) << 8;
159 1.1 itojun val |= arc4_getbyte(as);
160 1.1 itojun return val;
161 1.1 itojun }
162 1.1 itojun
163 1.1 itojun void
164 1.9.40.1 bouyer arc4random_stir(void)
165 1.1 itojun {
166 1.1 itojun if (!rs_initialized) {
167 1.1 itojun arc4_init(&rs);
168 1.1 itojun rs_initialized = 1;
169 1.1 itojun }
170 1.1 itojun arc4_stir(&rs);
171 1.1 itojun }
172 1.1 itojun
173 1.1 itojun void
174 1.9.40.1 bouyer arc4random_addrandom(u_char *dat, int datlen)
175 1.1 itojun {
176 1.1 itojun if (!rs_initialized)
177 1.1 itojun arc4random_stir();
178 1.1 itojun arc4_addrandom(&rs, dat, datlen);
179 1.1 itojun }
180 1.1 itojun
181 1.9.40.1 bouyer uint32_t
182 1.9.40.1 bouyer arc4random(void)
183 1.1 itojun {
184 1.1 itojun if (!rs_initialized)
185 1.1 itojun arc4random_stir();
186 1.1 itojun return arc4_getword(&rs);
187 1.1 itojun }
188 1.1 itojun
189 1.9.40.1 bouyer void
190 1.9.40.1 bouyer arc4random_buf(void *buf, size_t len)
191 1.9.40.1 bouyer {
192 1.9.40.1 bouyer uint8_t *bp = buf;
193 1.9.40.1 bouyer uint8_t *ep = bp + len;
194 1.9.40.1 bouyer
195 1.9.40.1 bouyer bp[0] = arc4_getbyte(&rs) % 3;
196 1.9.40.1 bouyer while (bp[0]--)
197 1.9.40.1 bouyer (void)arc4_getbyte(&rs);
198 1.9.40.1 bouyer
199 1.9.40.1 bouyer while (bp < ep)
200 1.9.40.1 bouyer *bp++ = arc4_getbyte(&rs);
201 1.9.40.1 bouyer }
202 1.9.40.1 bouyer
203 1.9.40.1 bouyer /*-
204 1.9.40.1 bouyer * Written by Damien Miller.
205 1.9.40.1 bouyer * With simplifications by Jinmei Tatuya.
206 1.9.40.1 bouyer */
207 1.9.40.1 bouyer
208 1.9.40.1 bouyer /*
209 1.9.40.1 bouyer * Calculate a uniformly distributed random number less than
210 1.9.40.1 bouyer * upper_bound avoiding "modulo bias".
211 1.9.40.1 bouyer *
212 1.9.40.1 bouyer * Uniformity is achieved by generating new random numbers
213 1.9.40.1 bouyer * until the one returned is outside the range
214 1.9.40.1 bouyer * [0, 2^32 % upper_bound[. This guarantees the selected
215 1.9.40.1 bouyer * random number will be inside the range
216 1.9.40.1 bouyer * [2^32 % upper_bound, 2^32[ which maps back to
217 1.9.40.1 bouyer * [0, upper_bound[ after reduction modulo upper_bound.
218 1.9.40.1 bouyer */
219 1.9.40.1 bouyer uint32_t
220 1.9.40.1 bouyer arc4random_uniform(uint32_t upper_bound)
221 1.9.40.1 bouyer {
222 1.9.40.1 bouyer uint32_t r, min;
223 1.9.40.1 bouyer
224 1.9.40.1 bouyer if (upper_bound < 2)
225 1.9.40.1 bouyer return 0;
226 1.9.40.1 bouyer
227 1.9.40.1 bouyer #if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL)
228 1.9.40.1 bouyer min = 0x100000000UL % upper_bound;
229 1.9.40.1 bouyer #else
230 1.9.40.1 bouyer /* calculate (2^32 % upper_bound) avoiding 64-bit math */
231 1.9.40.1 bouyer if (upper_bound > 0x80000000U)
232 1.9.40.1 bouyer /* 2^32 - upper_bound (only one "value area") */
233 1.9.40.1 bouyer min = 1 + ~upper_bound;
234 1.9.40.1 bouyer else
235 1.9.40.1 bouyer /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
236 1.9.40.1 bouyer min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
237 1.9.40.1 bouyer #endif
238 1.9.40.1 bouyer
239 1.9.40.1 bouyer /*
240 1.9.40.1 bouyer * This could theoretically loop forever but each retry has
241 1.9.40.1 bouyer * p > 0.5 (worst case, usually far better) of selecting a
242 1.9.40.1 bouyer * number inside the range we need, so it should rarely need
243 1.9.40.1 bouyer * to re-roll (at all).
244 1.9.40.1 bouyer */
245 1.9.40.1 bouyer if (!rs_initialized)
246 1.9.40.1 bouyer arc4random_stir();
247 1.9.40.1 bouyer if (arc4_getbyte(&rs) & 1)
248 1.9.40.1 bouyer (void)arc4_getbyte(&rs);
249 1.9.40.1 bouyer do
250 1.9.40.1 bouyer r = arc4_getword(&rs);
251 1.9.40.1 bouyer while (r < min);
252 1.9.40.1 bouyer
253 1.9.40.1 bouyer return r % upper_bound;
254 1.9.40.1 bouyer }
255 1.9.40.1 bouyer
256 1.9.40.1 bouyer
257 1.1 itojun #if 0
258 1.1 itojun /*-------- Test code for i386 --------*/
259 1.1 itojun #include <stdio.h>
260 1.1 itojun #include <machine/pctr.h>
261 1.1 itojun int
262 1.1 itojun main(int argc, char **argv)
263 1.1 itojun {
264 1.1 itojun const int iter = 1000000;
265 1.1 itojun int i;
266 1.1 itojun pctrval v;
267 1.1 itojun
268 1.1 itojun v = rdtsc();
269 1.1 itojun for (i = 0; i < iter; i++)
270 1.1 itojun arc4random();
271 1.1 itojun v = rdtsc() - v;
272 1.1 itojun v /= iter;
273 1.1 itojun
274 1.1 itojun printf("%qd cycles\n", v);
275 1.1 itojun }
276 1.1 itojun #endif
277