2 * Copyright (c) 1996, David Mazieres <dm@uun.org>
3 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Arc4 random number generator for OpenBSD.
21 * This code is derived from section 17.1 of Applied Cryptography,
22 * second edition, which describes a stream cipher allegedly
23 * compatible with RSA Labs "RC4" cipher (the actual description of
24 * which is a trade secret). The same algorithm is used as a stream
25 * cipher called "arcfour" in Tatu Ylonen's ssh package.
27 * Here the stream cipher has been modified always to include the time
28 * when initializing the state. That makes it impossible to
29 * regenerate the same random sequence twice, so this can't be used
30 * for encryption, but will generate good random numbers.
32 * RC4 is a registered trademark of RSA Laboratories.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "namespace.h"
39 #include <sys/types.h>
46 #include "libc_private.h"
47 #include "un-namespace.h"
55 static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
57 #define RANDOMDEV "/dev/urandom"
58 #define THREAD_LOCK() \
61 _pthread_mutex_lock(&arc4random_mtx); \
64 #define THREAD_UNLOCK() \
67 _pthread_mutex_unlock(&arc4random_mtx); \
70 static struct arc4_stream rs;
71 static int rs_initialized;
73 static int arc4_count;
75 static inline u_int8_t arc4_getbyte(void);
76 static void arc4_stir(void);
83 for (n = 0; n < 256; n++)
90 arc4_addrandom(u_char *dat, int datlen)
96 for (n = 0; n < 256; n++) {
99 rs.j = (rs.j + si + dat[n % datlen]);
100 rs.s[rs.i] = rs.s[rs.j];
113 u_int8_t rnd[128 - sizeof(struct timeval) - sizeof(pid_t)];
116 fd = _open(RANDOMDEV, O_RDONLY, 0);
119 if (_read(fd, &rdat, sizeof(rdat)) == sizeof(rdat))
123 /* !done? Ah, what the heck. We'll just take whatever was on the
126 gettimeofday(&rdat.tv, NULL);
130 arc4_addrandom((u_char *)&rdat, sizeof(rdat));
133 * Throw away the first N bytes of output, as suggested in the
134 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
135 * by Fluher, Mantin, and Shamir. N=512 is based on
136 * suggestions in the paper "(Not So) Random Shuffles of RC4"
139 if (rs_initialized != 1) {
140 for (n = 0; n < 512; n++)
141 (void)arc4_getbyte();
144 arc4_count = 1600000;
147 static inline u_int8_t
159 return (rs.s[(si + sj) & 0xff]);
162 static inline u_int32_t
167 val = arc4_getbyte() << 24;
168 val |= arc4_getbyte() << 16;
169 val |= arc4_getbyte() << 8;
170 val |= arc4_getbyte();
176 arc4_check_init(void)
178 if (!rs_initialized) {
185 arc4_check_stir(void)
187 if (!rs_stired || arc4_count <= 0) {
194 arc4random_stir(void)
204 arc4random_addrandom(u_char *dat, int datlen)
209 arc4_addrandom(dat, datlen);
221 rnd = arc4_getword();
229 arc4random_buf(void *_buf, size_t n)
231 u_char *buf = (u_char *)_buf;
237 buf[n] = arc4_getbyte();
244 /*-------- Test code for i386 --------*/
246 #include <machine/pctr.h>
248 main(int argc, char **argv)
250 const int iter = 1000000;
255 for (i = 0; i < iter; i++)
260 printf("%qd cycles\n", v);