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