arc4random.c revision 1.8 1 1.8 lukem /* $NetBSD: arc4random.c,v 1.8 2005/06/12 05:21:27 lukem 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.8 lukem __RCSID("$NetBSD: arc4random.c,v 1.8 2005/06/12 05:21:27 lukem 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 #ifdef __GNUC__
47 1.1 itojun #define inline __inline
48 1.1 itojun #else /* !__GNUC__ */
49 1.1 itojun #define inline
50 1.1 itojun #endif /* !__GNUC__ */
51 1.1 itojun
52 1.1 itojun struct arc4_stream {
53 1.1 itojun u_int8_t i;
54 1.1 itojun u_int8_t j;
55 1.1 itojun u_int8_t s[256];
56 1.1 itojun };
57 1.1 itojun
58 1.6 kleink static int rs_initialized;
59 1.1 itojun static struct arc4_stream rs;
60 1.1 itojun
61 1.1 itojun static inline void arc4_init(struct arc4_stream *);
62 1.1 itojun static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
63 1.1 itojun static void arc4_stir(struct arc4_stream *);
64 1.1 itojun static inline u_int8_t arc4_getbyte(struct arc4_stream *);
65 1.1 itojun static inline u_int32_t arc4_getword(struct arc4_stream *);
66 1.1 itojun
67 1.1 itojun static inline void
68 1.1 itojun arc4_init(as)
69 1.1 itojun struct arc4_stream *as;
70 1.1 itojun {
71 1.1 itojun int n;
72 1.1 itojun
73 1.1 itojun for (n = 0; n < 256; n++)
74 1.1 itojun as->s[n] = n;
75 1.1 itojun as->i = 0;
76 1.1 itojun as->j = 0;
77 1.1 itojun }
78 1.1 itojun
79 1.1 itojun static inline void
80 1.1 itojun arc4_addrandom(as, dat, datlen)
81 1.1 itojun struct arc4_stream *as;
82 1.1 itojun u_char *dat;
83 1.1 itojun int datlen;
84 1.1 itojun {
85 1.1 itojun int n;
86 1.1 itojun u_int8_t si;
87 1.1 itojun
88 1.1 itojun as->i--;
89 1.1 itojun for (n = 0; n < 256; n++) {
90 1.1 itojun as->i = (as->i + 1);
91 1.1 itojun si = as->s[as->i];
92 1.1 itojun as->j = (as->j + si + dat[n % datlen]);
93 1.1 itojun as->s[as->i] = as->s[as->j];
94 1.1 itojun as->s[as->j] = si;
95 1.1 itojun }
96 1.1 itojun as->j = as->i;
97 1.1 itojun }
98 1.1 itojun
99 1.1 itojun static void
100 1.1 itojun arc4_stir(as)
101 1.1 itojun struct arc4_stream *as;
102 1.1 itojun {
103 1.1 itojun int fd;
104 1.1 itojun struct {
105 1.1 itojun struct timeval tv;
106 1.1 itojun u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)];
107 1.1 itojun } rdat;
108 1.3 itojun int n;
109 1.1 itojun
110 1.1 itojun gettimeofday(&rdat.tv, NULL);
111 1.2 itojun fd = open("/dev/urandom", O_RDONLY);
112 1.1 itojun if (fd != -1) {
113 1.1 itojun read(fd, rdat.rnd, sizeof(rdat.rnd));
114 1.1 itojun close(fd);
115 1.1 itojun }
116 1.4 itojun #ifdef KERN_URND
117 1.1 itojun else {
118 1.5 thorpej int mib[2];
119 1.5 thorpej u_int i;
120 1.1 itojun size_t len;
121 1.1 itojun
122 1.1 itojun /* Device could not be opened, we might be chrooted, take
123 1.1 itojun * randomness from sysctl. */
124 1.1 itojun
125 1.1 itojun mib[0] = CTL_KERN;
126 1.4 itojun mib[1] = KERN_URND;
127 1.1 itojun
128 1.5 thorpej for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i++) {
129 1.1 itojun len = sizeof(u_int);
130 1.1 itojun if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1)
131 1.1 itojun break;
132 1.1 itojun }
133 1.1 itojun }
134 1.1 itojun #endif
135 1.1 itojun /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take
136 1.1 itojun * whatever was on the stack... */
137 1.1 itojun
138 1.1 itojun arc4_addrandom(as, (void *) &rdat, 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.3 itojun for (n = 0; n < 256 * 4; n++)
146 1.3 itojun arc4_getbyte(as);
147 1.1 itojun }
148 1.1 itojun
149 1.1 itojun static inline u_int8_t
150 1.1 itojun arc4_getbyte(as)
151 1.1 itojun struct arc4_stream *as;
152 1.1 itojun {
153 1.1 itojun u_int8_t si, sj;
154 1.1 itojun
155 1.1 itojun as->i = (as->i + 1);
156 1.1 itojun si = as->s[as->i];
157 1.1 itojun as->j = (as->j + si);
158 1.1 itojun sj = as->s[as->j];
159 1.1 itojun as->s[as->i] = sj;
160 1.1 itojun as->s[as->j] = si;
161 1.1 itojun return (as->s[(si + sj) & 0xff]);
162 1.1 itojun }
163 1.1 itojun
164 1.1 itojun static inline u_int32_t
165 1.1 itojun arc4_getword(as)
166 1.1 itojun struct arc4_stream *as;
167 1.1 itojun {
168 1.1 itojun u_int32_t val;
169 1.1 itojun val = arc4_getbyte(as) << 24;
170 1.1 itojun val |= arc4_getbyte(as) << 16;
171 1.1 itojun val |= arc4_getbyte(as) << 8;
172 1.1 itojun val |= arc4_getbyte(as);
173 1.1 itojun return val;
174 1.1 itojun }
175 1.1 itojun
176 1.1 itojun void
177 1.1 itojun arc4random_stir()
178 1.1 itojun {
179 1.1 itojun if (!rs_initialized) {
180 1.1 itojun arc4_init(&rs);
181 1.1 itojun rs_initialized = 1;
182 1.1 itojun }
183 1.1 itojun arc4_stir(&rs);
184 1.1 itojun }
185 1.1 itojun
186 1.1 itojun void
187 1.1 itojun arc4random_addrandom(dat, datlen)
188 1.1 itojun u_char *dat;
189 1.1 itojun int datlen;
190 1.1 itojun {
191 1.1 itojun if (!rs_initialized)
192 1.1 itojun arc4random_stir();
193 1.1 itojun arc4_addrandom(&rs, dat, datlen);
194 1.1 itojun }
195 1.1 itojun
196 1.1 itojun u_int32_t
197 1.1 itojun arc4random()
198 1.1 itojun {
199 1.1 itojun if (!rs_initialized)
200 1.1 itojun arc4random_stir();
201 1.1 itojun return arc4_getword(&rs);
202 1.1 itojun }
203 1.1 itojun
204 1.1 itojun #if 0
205 1.1 itojun /*-------- Test code for i386 --------*/
206 1.1 itojun #include <stdio.h>
207 1.1 itojun #include <machine/pctr.h>
208 1.1 itojun int
209 1.1 itojun main(int argc, char **argv)
210 1.1 itojun {
211 1.1 itojun const int iter = 1000000;
212 1.1 itojun int i;
213 1.1 itojun pctrval v;
214 1.1 itojun
215 1.1 itojun v = rdtsc();
216 1.1 itojun for (i = 0; i < iter; i++)
217 1.1 itojun arc4random();
218 1.1 itojun v = rdtsc() - v;
219 1.1 itojun v /= iter;
220 1.1 itojun
221 1.1 itojun printf("%qd cycles\n", v);
222 1.1 itojun }
223 1.1 itojun #endif
224