xref: /freebsd/lib/libc/gen/arc4random.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
1 /* $FreeBSD$ */
2 
3 /*
4  * Arc4 random number generator for OpenBSD.
5  * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
6  *
7  * Modification and redistribution in source and binary forms is
8  * permitted provided that due credit is given to the author and the
9  * OpenBSD project (for instance by leaving this copyright notice
10  * intact).
11  */
12 
13 /*
14  * This code is derived from section 17.1 of Applied Cryptography,
15  * second edition, which describes a stream cipher allegedly
16  * compatible with RSA Labs "RC4" cipher (the actual description of
17  * which is a trade secret).  The same algorithm is used as a stream
18  * cipher called "arcfour" in Tatu Ylonen's ssh package.
19  *
20  * Here the stream cipher has been modified always to include the time
21  * when initializing the state.  That makes it impossible to
22  * regenerate the same random sequence twice, so this can't be used
23  * for encryption, but will generate good random numbers.
24  *
25  * RC4 is a registered trademark of RSA Laboratories.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "namespace.h"
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include "un-namespace.h"
38 
39 struct arc4_stream {
40 	u_int8_t i;
41 	u_int8_t j;
42 	u_int8_t s[256];
43 };
44 
45 static int rs_initialized;
46 static struct arc4_stream rs;
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;
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 static inline u_int8_t
104 arc4_getbyte(as)
105 	struct arc4_stream *as;
106 {
107 	u_int8_t si, sj;
108 
109 	as->i = (as->i + 1);
110 	si = as->s[as->i];
111 	as->j = (as->j + si);
112 	sj = as->s[as->j];
113 	as->s[as->i] = sj;
114 	as->s[as->j] = si;
115 	return (as->s[(si + sj) & 0xff]);
116 }
117 
118 static inline u_int32_t
119 arc4_getword(as)
120 	struct arc4_stream *as;
121 {
122 	u_int32_t val;
123 	val = arc4_getbyte(as) << 24;
124 	val |= arc4_getbyte(as) << 16;
125 	val |= arc4_getbyte(as) << 8;
126 	val |= arc4_getbyte(as);
127 	return val;
128 }
129 
130 void
131 arc4random_stir()
132 {
133 	if (!rs_initialized) {
134 		arc4_init(&rs);
135 		rs_initialized = 1;
136 	}
137 	arc4_stir(&rs);
138 }
139 
140 void
141 arc4random_addrandom(dat, datlen)
142 	u_char *dat;
143 	int     datlen;
144 {
145 	if (!rs_initialized)
146 		arc4random_stir();
147 	arc4_addrandom(&rs, dat, datlen);
148 }
149 
150 u_int32_t
151 arc4random()
152 {
153 	if (!rs_initialized)
154 		arc4random_stir();
155 	return arc4_getword(&rs);
156 }
157 
158 #if 0
159 /*-------- Test code for i386 --------*/
160 #include <stdio.h>
161 #include <machine/pctr.h>
162 int
163 main(int argc, char **argv)
164 {
165 	const int iter = 1000000;
166 	int     i;
167 	pctrval v;
168 
169 	v = rdtsc();
170 	for (i = 0; i < iter; i++)
171 		arc4random();
172 	v = rdtsc() - v;
173 	v /= iter;
174 
175 	printf("%qd cycles\n", v);
176 }
177 #endif
178