xref: /freebsd/sys/libkern/arc4random.c (revision e0c27215058b5786c78fcfb3963eebe61a989511)
1 /*-
2  * THE BEER-WARE LICENSE
3  *
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.
7  *
8  * Dan Moschuk
9  */
10 
11 #include <sys/cdefs.h>
12 __FBSDID("$FreeBSD$");
13 
14 #include <sys/types.h>
15 #include <sys/random.h>
16 #include <sys/libkern.h>
17 #include <sys/time.h>
18 
19 #define	ARC4_RESEED_BYTES 65536
20 #define	ARC4_RESEED_SECONDS 300
21 #define	ARC4_KEYBYTES (256 / 8)
22 
23 static u_int8_t arc4_i, arc4_j;
24 static int arc4_initialized = 0;
25 static int arc4_numruns = 0;
26 static u_int8_t arc4_sbox[256];
27 static time_t arc4_t_reseed;
28 
29 static u_int8_t arc4_randbyte(void);
30 
31 static __inline void
32 arc4_swap(u_int8_t *a, u_int8_t *b)
33 {
34 	u_int8_t c;
35 
36 	c = *a;
37 	*a = *b;
38 	*b = c;
39 }
40 
41 /*
42  * Stir our S-box.
43  */
44 static void
45 arc4_randomstir (void)
46 {
47 	u_int8_t key[256];
48 	int r, n;
49 	struct timeval tv_now;
50 
51 	/*
52 	 * XXX read_random() returns unsafe numbers if the entropy
53 	 * device is not loaded -- MarkM.
54 	 */
55 	r = read_random(key, ARC4_KEYBYTES);
56 	/* If r == 0 || -1, just use what was on the stack. */
57 	if (r > 0) {
58 		for (n = r; n < sizeof(key); n++)
59 			key[n] = key[n % r];
60 	}
61 
62 	for (n = 0; n < 256; n++) {
63 		arc4_j = (arc4_j + arc4_sbox[n] + key[n]) % 256;
64 		arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]);
65 	}
66 
67 	/* Reset for next reseed cycle. */
68 	getmicrouptime(&tv_now);
69 	arc4_t_reseed = tv_now.tv_sec + ARC4_RESEED_SECONDS;
70 	arc4_numruns = 0;
71 }
72 
73 /*
74  * Initialize our S-box to its beginning defaults.
75  */
76 static void
77 arc4_init(void)
78 {
79 	int n;
80 
81 	arc4_i = arc4_j = 0;
82 	for (n = 0; n < 256; n++)
83 		arc4_sbox[n] = (u_int8_t) n;
84 
85 	arc4_randomstir();
86 	arc4_initialized = 1;
87 
88 	/*
89 	 * Throw away the first N words of output, as suggested in the
90 	 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
91 	 * by Fluher, Mantin, and Shamir.  (N = 256 in our case.)
92 	 */
93 	for (n = 0; n < 256*4; n++)
94 		arc4_randbyte();
95 }
96 
97 /*
98  * Generate a random byte.
99  */
100 static u_int8_t
101 arc4_randbyte(void)
102 {
103 	u_int8_t arc4_t;
104 
105 	arc4_i = (arc4_i + 1) % 256;
106 	arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256;
107 
108 	arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]);
109 
110 	arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256;
111 	return arc4_sbox[arc4_t];
112 }
113 
114 void
115 arc4rand(void *ptr, u_int len, int reseed)
116 {
117 	u_char *p;
118 	struct timeval tv;
119 
120 	/* Initialize array if needed. */
121 	if (!arc4_initialized)
122 		arc4_init();
123 
124 	getmicrouptime(&tv);
125 	arc4_numruns += len;
126 	if (reseed ||
127 	   (arc4_numruns > ARC4_RESEED_BYTES) ||
128 	   (tv.tv_sec > arc4_t_reseed))
129 		arc4_randomstir();
130 
131 	p = ptr;
132 	while (len--)
133 		*p++ = arc4_randbyte();
134 }
135 
136 uint32_t
137 arc4random(void)
138 {
139 	uint32_t ret;
140 
141 	arc4rand(&ret, sizeof ret, 0);
142 	return ret;
143 }
144