2 * THE BEER-WARE LICENSE
4 * <dan@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you
6 * think this stuff is worth it, you can buy me a beer in return.
13 #include <sys/libkern.h>
16 static u_int8_t arc4_i, arc4_j;
17 static int arc4_initialized = 0;
18 static u_int8_t arc4_sbox[256];
21 arc4_swap(u_int8_t *a, u_int8_t *b)
31 * Initialize our S-box to its beginning defaults.
40 for (n = 0; n < 256; n++)
41 arc4_sbox[n] = (u_int8_t) n;
44 srandom(ts.tv_sec ^ ts.tv_nsec);
45 for (n = 0; n < 256; n++)
46 key[n] = random() % 256;
49 for (n = 0; n < 256; n++)
51 arc4_j = arc4_j + arc4_sbox[n] + key[n];
52 arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]);
58 * Generate a random byte.
65 arc4_i = (arc4_i + 1) % 256;
66 arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256;
68 arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]);
70 arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256;
71 return arc4_sbox[arc4_t];
79 /* Initialize array if needed. */
80 if (!arc4_initialized)
83 ret = arc4_randbyte();
84 ret |= arc4_randbyte() << 8;
85 ret |= arc4_randbyte() << 16;
86 ret |= arc4_randbyte() << 24;