arc4random.c revision 1.5.2.2 1 1.5.2.2 jmc /* $NetBSD: arc4random.c,v 1.5.2.2 2004/03/26 22:52:50 jmc Exp $ */
2 1.5.2.2 jmc /* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */
3 1.5.2.2 jmc
4 1.5.2.2 jmc /*
5 1.5.2.2 jmc * Arc4 random number generator for OpenBSD.
6 1.5.2.2 jmc * Copyright 1996 David Mazieres <dm (at) lcs.mit.edu>.
7 1.5.2.2 jmc *
8 1.5.2.2 jmc * Modification and redistribution in source and binary forms is
9 1.5.2.2 jmc * permitted provided that due credit is given to the author and the
10 1.5.2.2 jmc * OpenBSD project by leaving this copyright notice intact.
11 1.5.2.2 jmc */
12 1.5.2.2 jmc
13 1.5.2.2 jmc /*
14 1.5.2.2 jmc * This code is derived from section 17.1 of Applied Cryptography,
15 1.5.2.2 jmc * second edition, which describes a stream cipher allegedly
16 1.5.2.2 jmc * compatible with RSA Labs "RC4" cipher (the actual description of
17 1.5.2.2 jmc * which is a trade secret). The same algorithm is used as a stream
18 1.5.2.2 jmc * cipher called "arcfour" in Tatu Ylonen's ssh package.
19 1.5.2.2 jmc *
20 1.5.2.2 jmc * Here the stream cipher has been modified always to include the time
21 1.5.2.2 jmc * when initializing the state. That makes it impossible to
22 1.5.2.2 jmc * regenerate the same random sequence twice, so this can't be used
23 1.5.2.2 jmc * for encryption, but will generate good random numbers.
24 1.5.2.2 jmc *
25 1.5.2.2 jmc * RC4 is a registered trademark of RSA Laboratories.
26 1.5.2.2 jmc */
27 1.5.2.2 jmc
28 1.5.2.2 jmc #include <fcntl.h>
29 1.5.2.2 jmc #include <stdlib.h>
30 1.5.2.2 jmc #include <unistd.h>
31 1.5.2.2 jmc #include <sys/types.h>
32 1.5.2.2 jmc #include <sys/param.h>
33 1.5.2.2 jmc #include <sys/time.h>
34 1.5.2.2 jmc #include <sys/sysctl.h>
35 1.5.2.2 jmc
36 1.5.2.2 jmc #ifdef __GNUC__
37 1.5.2.2 jmc #define inline __inline
38 1.5.2.2 jmc #else /* !__GNUC__ */
39 1.5.2.2 jmc #define inline
40 1.5.2.2 jmc #endif /* !__GNUC__ */
41 1.5.2.2 jmc
42 1.5.2.2 jmc struct arc4_stream {
43 1.5.2.2 jmc u_int8_t i;
44 1.5.2.2 jmc u_int8_t j;
45 1.5.2.2 jmc u_int8_t s[256];
46 1.5.2.2 jmc };
47 1.5.2.2 jmc
48 1.5.2.2 jmc int rs_initialized;
49 1.5.2.2 jmc static struct arc4_stream rs;
50 1.5.2.2 jmc
51 1.5.2.2 jmc static inline void arc4_init(struct arc4_stream *);
52 1.5.2.2 jmc static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
53 1.5.2.2 jmc static void arc4_stir(struct arc4_stream *);
54 1.5.2.2 jmc static inline u_int8_t arc4_getbyte(struct arc4_stream *);
55 1.5.2.2 jmc static inline u_int32_t arc4_getword(struct arc4_stream *);
56 1.5.2.2 jmc
57 1.5.2.2 jmc static inline void
58 1.5.2.2 jmc arc4_init(as)
59 1.5.2.2 jmc struct arc4_stream *as;
60 1.5.2.2 jmc {
61 1.5.2.2 jmc int n;
62 1.5.2.2 jmc
63 1.5.2.2 jmc for (n = 0; n < 256; n++)
64 1.5.2.2 jmc as->s[n] = n;
65 1.5.2.2 jmc as->i = 0;
66 1.5.2.2 jmc as->j = 0;
67 1.5.2.2 jmc }
68 1.5.2.2 jmc
69 1.5.2.2 jmc static inline void
70 1.5.2.2 jmc arc4_addrandom(as, dat, datlen)
71 1.5.2.2 jmc struct arc4_stream *as;
72 1.5.2.2 jmc u_char *dat;
73 1.5.2.2 jmc int datlen;
74 1.5.2.2 jmc {
75 1.5.2.2 jmc int n;
76 1.5.2.2 jmc u_int8_t si;
77 1.5.2.2 jmc
78 1.5.2.2 jmc as->i--;
79 1.5.2.2 jmc for (n = 0; n < 256; n++) {
80 1.5.2.2 jmc as->i = (as->i + 1);
81 1.5.2.2 jmc si = as->s[as->i];
82 1.5.2.2 jmc as->j = (as->j + si + dat[n % datlen]);
83 1.5.2.2 jmc as->s[as->i] = as->s[as->j];
84 1.5.2.2 jmc as->s[as->j] = si;
85 1.5.2.2 jmc }
86 1.5.2.2 jmc as->j = as->i;
87 1.5.2.2 jmc }
88 1.5.2.2 jmc
89 1.5.2.2 jmc static void
90 1.5.2.2 jmc arc4_stir(as)
91 1.5.2.2 jmc struct arc4_stream *as;
92 1.5.2.2 jmc {
93 1.5.2.2 jmc int fd;
94 1.5.2.2 jmc struct {
95 1.5.2.2 jmc struct timeval tv;
96 1.5.2.2 jmc u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)];
97 1.5.2.2 jmc } rdat;
98 1.5.2.2 jmc int n;
99 1.5.2.2 jmc
100 1.5.2.2 jmc gettimeofday(&rdat.tv, NULL);
101 1.5.2.2 jmc fd = open("/dev/urandom", O_RDONLY);
102 1.5.2.2 jmc if (fd != -1) {
103 1.5.2.2 jmc read(fd, rdat.rnd, sizeof(rdat.rnd));
104 1.5.2.2 jmc close(fd);
105 1.5.2.2 jmc }
106 1.5.2.2 jmc #ifdef KERN_URND
107 1.5.2.2 jmc else {
108 1.5.2.2 jmc int mib[2];
109 1.5.2.2 jmc u_int i;
110 1.5.2.2 jmc size_t len;
111 1.5.2.2 jmc
112 1.5.2.2 jmc /* Device could not be opened, we might be chrooted, take
113 1.5.2.2 jmc * randomness from sysctl. */
114 1.5.2.2 jmc
115 1.5.2.2 jmc mib[0] = CTL_KERN;
116 1.5.2.2 jmc mib[1] = KERN_URND;
117 1.5.2.2 jmc
118 1.5.2.2 jmc for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i++) {
119 1.5.2.2 jmc len = sizeof(u_int);
120 1.5.2.2 jmc if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1)
121 1.5.2.2 jmc break;
122 1.5.2.2 jmc }
123 1.5.2.2 jmc }
124 1.5.2.2 jmc #endif
125 1.5.2.2 jmc /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take
126 1.5.2.2 jmc * whatever was on the stack... */
127 1.5.2.2 jmc
128 1.5.2.2 jmc arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
129 1.5.2.2 jmc
130 1.5.2.2 jmc /*
131 1.5.2.2 jmc * Throw away the first N words of output, as suggested in the
132 1.5.2.2 jmc * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
133 1.5.2.2 jmc * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
134 1.5.2.2 jmc */
135 1.5.2.2 jmc for (n = 0; n < 256 * 4; n++)
136 1.5.2.2 jmc arc4_getbyte(as);
137 1.5.2.2 jmc }
138 1.5.2.2 jmc
139 1.5.2.2 jmc static inline u_int8_t
140 1.5.2.2 jmc arc4_getbyte(as)
141 1.5.2.2 jmc struct arc4_stream *as;
142 1.5.2.2 jmc {
143 1.5.2.2 jmc u_int8_t si, sj;
144 1.5.2.2 jmc
145 1.5.2.2 jmc as->i = (as->i + 1);
146 1.5.2.2 jmc si = as->s[as->i];
147 1.5.2.2 jmc as->j = (as->j + si);
148 1.5.2.2 jmc sj = as->s[as->j];
149 1.5.2.2 jmc as->s[as->i] = sj;
150 1.5.2.2 jmc as->s[as->j] = si;
151 1.5.2.2 jmc return (as->s[(si + sj) & 0xff]);
152 1.5.2.2 jmc }
153 1.5.2.2 jmc
154 1.5.2.2 jmc static inline u_int32_t
155 1.5.2.2 jmc arc4_getword(as)
156 1.5.2.2 jmc struct arc4_stream *as;
157 1.5.2.2 jmc {
158 1.5.2.2 jmc u_int32_t val;
159 1.5.2.2 jmc val = arc4_getbyte(as) << 24;
160 1.5.2.2 jmc val |= arc4_getbyte(as) << 16;
161 1.5.2.2 jmc val |= arc4_getbyte(as) << 8;
162 1.5.2.2 jmc val |= arc4_getbyte(as);
163 1.5.2.2 jmc return val;
164 1.5.2.2 jmc }
165 1.5.2.2 jmc
166 1.5.2.2 jmc void
167 1.5.2.2 jmc arc4random_stir()
168 1.5.2.2 jmc {
169 1.5.2.2 jmc if (!rs_initialized) {
170 1.5.2.2 jmc arc4_init(&rs);
171 1.5.2.2 jmc rs_initialized = 1;
172 1.5.2.2 jmc }
173 1.5.2.2 jmc arc4_stir(&rs);
174 1.5.2.2 jmc }
175 1.5.2.2 jmc
176 1.5.2.2 jmc void
177 1.5.2.2 jmc arc4random_addrandom(dat, datlen)
178 1.5.2.2 jmc u_char *dat;
179 1.5.2.2 jmc int datlen;
180 1.5.2.2 jmc {
181 1.5.2.2 jmc if (!rs_initialized)
182 1.5.2.2 jmc arc4random_stir();
183 1.5.2.2 jmc arc4_addrandom(&rs, dat, datlen);
184 1.5.2.2 jmc }
185 1.5.2.2 jmc
186 1.5.2.2 jmc u_int32_t
187 1.5.2.2 jmc arc4random()
188 1.5.2.2 jmc {
189 1.5.2.2 jmc if (!rs_initialized)
190 1.5.2.2 jmc arc4random_stir();
191 1.5.2.2 jmc return arc4_getword(&rs);
192 1.5.2.2 jmc }
193 1.5.2.2 jmc
194 1.5.2.2 jmc #if 0
195 1.5.2.2 jmc /*-------- Test code for i386 --------*/
196 1.5.2.2 jmc #include <stdio.h>
197 1.5.2.2 jmc #include <machine/pctr.h>
198 1.5.2.2 jmc int
199 1.5.2.2 jmc main(int argc, char **argv)
200 1.5.2.2 jmc {
201 1.5.2.2 jmc const int iter = 1000000;
202 1.5.2.2 jmc int i;
203 1.5.2.2 jmc pctrval v;
204 1.5.2.2 jmc
205 1.5.2.2 jmc v = rdtsc();
206 1.5.2.2 jmc for (i = 0; i < iter; i++)
207 1.5.2.2 jmc arc4random();
208 1.5.2.2 jmc v = rdtsc() - v;
209 1.5.2.2 jmc v /= iter;
210 1.5.2.2 jmc
211 1.5.2.2 jmc printf("%qd cycles\n", v);
212 1.5.2.2 jmc }
213 1.5.2.2 jmc #endif
214