pg: Add missing dummy stack frames for mcount for x86_64.
[dragonfly.git] / sys / libkern / arc4random.c
blobf81c137b17dee1445b293d90a58d044da5918fc4
1 /*-
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.
8 * Dan Moschuk
10 * $FreeBSD: src/sys/libkern/arc4random.c,v 1.3.2.2 2001/09/17 07:06:50 silby Exp $
11 * $DragonFly: src/sys/libkern/arc4random.c,v 1.3 2006/09/03 17:31:55 dillon Exp $
14 #include <sys/types.h>
15 #include <sys/random.h>
16 #include <sys/libkern.h>
17 #include <sys/time.h>
19 #define ARC4_MAXRUNS 16384
20 #define ARC4_RESEED_SECONDS 300
21 #define ARC4_KEYBYTES 32 /* 256 bit key */
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 struct timeval arc4_tv_nextreseed;
29 static u_int8_t arc4_randbyte(void);
31 static __inline void
32 arc4_swap(u_int8_t *a, u_int8_t *b)
34 u_int8_t c;
36 c = *a;
37 *a = *b;
38 *b = c;
42 * Stir our S-box.
44 static void
45 arc4_randomstir (void)
47 u_int8_t key[256];
48 int r, n;
51 * XXX read_random() returns unsafe numbers if the entropy
52 * device is not loaded -- MarkM.
54 r = read_random_unlimited(key, ARC4_KEYBYTES);
55 /* If r == 0 || -1, just use what was on the stack. */
56 if (r > 0)
58 for (n = r; n < sizeof(key); n++)
59 key[n] = key[n % r];
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]);
68 * Discard early keystream, as per recommendations in:
69 * "(Not So) Random Shuffles of RC4" by Ilya Mironov.
71 for (n = 0; n < 768*4; n++)
72 arc4_randbyte();
74 /* Reset for next reseed cycle. */
75 getmicrotime(&arc4_tv_nextreseed);
76 arc4_tv_nextreseed.tv_sec += ARC4_RESEED_SECONDS;
77 arc4_numruns = 0;
81 * Initialize our S-box to its beginning defaults.
83 static void
84 arc4_init(void)
86 int n;
88 arc4_i = arc4_j = 0;
89 for (n = 0; n < 256; n++)
90 arc4_sbox[n] = (u_int8_t) n;
92 arc4_randomstir();
93 arc4_initialized = 1;
96 * Discard early keystream, as per recommendations in:
97 * "(Not So) Random Shuffles of RC4" by Ilya Mironov.
99 for (n = 0; n < 768*4; n++)
100 arc4_randbyte();
104 * Generate a random byte.
106 static u_int8_t
107 arc4_randbyte(void)
109 u_int8_t arc4_t;
111 arc4_i = (arc4_i + 1) % 256;
112 arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256;
114 arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]);
116 arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256;
117 return arc4_sbox[arc4_t];
120 u_int32_t
121 karc4random(void)
123 u_int32_t ret;
124 struct timeval tv_now;
126 /* Initialize array if needed. */
127 if (!arc4_initialized)
128 arc4_init();
130 getmicrotime(&tv_now);
131 if ((++arc4_numruns > ARC4_MAXRUNS) ||
132 (tv_now.tv_sec > arc4_tv_nextreseed.tv_sec))
134 arc4_randomstir();
137 ret = arc4_randbyte();
138 ret |= arc4_randbyte() << 8;
139 ret |= arc4_randbyte() << 16;
140 ret |= arc4_randbyte() << 24;
142 return ret;
145 void
146 karc4rand(void *ptr, size_t len)
148 uint8_t *p;
149 struct timeval tv_now;
151 p = ptr;
153 /* Initialize array if needed. */
154 if (!arc4_initialized)
155 arc4_init();
157 getmicrotime(&tv_now);
158 if ((++arc4_numruns > ARC4_MAXRUNS) ||
159 (tv_now.tv_sec > arc4_tv_nextreseed.tv_sec))
161 arc4_randomstir();
164 while (len--)
165 *p++ = arc4_randbyte();