Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/arc4random-fbsd.c

1/*
2 * Copyright (c) 1996, David Mazieres <dm@uun.org>
3 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
4 *
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.
8 *
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.
16 */
17
18/*
19 * Arc4 random number generator for OpenBSD.
20 *
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.
26 *
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.
31 *
32 * RC4 is a registered trademark of RSA Laboratories.
33 */
34
35
36#include "libsaio.h"
37
38
39struct arc4_stream {
40u_int8_t i;
41u_int8_t j;
42u_int8_t s[256];
43};
44
45
46#define KEYSIZE128
47
48static struct arc4_stream rs = {
49.i = 0,
50.j = 0,
51.s = {
52 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
53 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
54 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
55 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
56 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
57 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
58 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
59112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
60128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
61144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
62160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
63176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
64192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
65208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
66224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
67240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
68}
69};
70//static int rs_initialized;
71static int rs_stired;
72static int arc4_count;
73
74static inline u_int8_t arc4_getbyte(void);
75static void arc4_stir(void);
76__private_extern__ void _arc4_fork_child(void);
77
78static struct {
79struct timevaltv;
80u_int8_trnd[KEYSIZE];
81} rdat;
82static volatile int rs_data_available = 0;
83
84static inline void
85arc4_addrandom(u_char *dat, int datlen)
86{
87int n;
88u_int8_t si;
89
90rs.i--;
91for (n = 0; n < 256; n++) {
92rs.i = (rs.i + 1);
93si = rs.s[rs.i];
94rs.j = (rs.j + si + dat[n % datlen]);
95rs.s[rs.i] = rs.s[rs.j];
96rs.s[rs.j] = si;
97}
98rs.j = rs.i;
99}
100
101static void
102arc4_fetch(void)
103{
104
105(void)gettimeofday(&rdat.tv, NULL);
106
107}
108
109static void
110arc4_stir(void)
111{
112int n;
113/*
114 * If we don't have data, we need some now before we can integrate
115 * it into the static buffers
116 */
117if (!rs_data_available)
118{
119arc4_fetch();
120}
121rs_data_available = 0;
122__sync_synchronize();
123
124arc4_addrandom((u_char *)&rdat, KEYSIZE);
125
126/*
127 * Throw away the first N bytes of output, as suggested in the
128 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
129 * by Fluher, Mantin, and Shamir. N=1024 is based on
130 * suggestions in the paper "(Not So) Random Shuffles of RC4"
131 * by Ilya Mironov.
132 */
133for (n = 0; n < 1024; n++)
134(void) arc4_getbyte();
135arc4_count = 1600000;
136rs_stired = 1;
137}
138
139static inline u_int8_t
140arc4_getbyte(void)
141{
142u_int8_t si, sj;
143
144rs.i = (rs.i + 1);
145si = rs.s[rs.i];
146rs.j = (rs.j + si);
147sj = rs.s[rs.j];
148rs.s[rs.i] = sj;
149rs.s[rs.j] = si;
150
151return (rs.s[(si + sj) & 0xff]);
152}
153
154static inline u_int32_t
155arc4_getword(void)
156{
157u_int32_t val;
158
159val = arc4_getbyte() << 24;
160val |= arc4_getbyte() << 16;
161val |= arc4_getbyte() << 8;
162val |= arc4_getbyte();
163
164return (val);
165}
166
167/* 7944700: force restir in child */
168__private_extern__ void
169_arc4_fork_child(void)
170{
171rs_stired = 0;
172rs_data_available = 0;
173}
174
175static inline int
176arc4_check_stir(void)
177{
178if (!rs_stired || arc4_count <= 0) {
179arc4_stir();
180return 1;
181}
182return 0;
183}
184
185void
186arc4random_stir(void)
187{
188arc4_stir();
189}
190
191void
192arc4random_addrandom(u_char *dat, int datlen)
193{
194arc4_check_stir();
195arc4_addrandom(dat, datlen);
196}
197
198u_int32_t
199arc4random(void)
200{
201u_int32_t rnd;
202
203int did_stir = arc4_check_stir();
204rnd = arc4_getword();
205arc4_count -= 4;
206
207if (did_stir)
208{
209/* stirring used up our data pool, we need to read in new data outside of the lock */
210arc4_fetch();
211rs_data_available = 1;
212__sync_synchronize();
213}
214
215return (rnd);
216}
217
218void
219arc4random_buf(void *_buf, size_t n)
220{
221u_char *buf = (u_char *)_buf;
222int did_stir = 0;
223
224while (n--) {
225if (arc4_check_stir())
226{
227did_stir = 1;
228}
229buf[n] = arc4_getbyte();
230arc4_count--;
231}
232
233if (did_stir)
234{
235/* stirring used up our data pool, we need to read in new data outside of the lock */
236arc4_fetch();
237rs_data_available = 1;
238__sync_synchronize();
239}
240}
241
242/*
243 * Calculate a uniformly distributed random number less than upper_bound
244 * avoiding "modulo bias".
245 *
246 * Uniformity is achieved by generating new random numbers until the one
247 * returned is outside the range [0, 2**32 % upper_bound). This
248 * guarantees the selected random number will be inside
249 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
250 * after reduction modulo upper_bound.
251 */
252u_int32_t
253arc4random_uniform(u_int32_t upper_bound)
254{
255u_int32_t r, min;
256
257if (upper_bound < 2)
258return (0);
259
260#if (ULONG_MAX > 0xffffffffUL)
261min = 0x100000000UL % upper_bound;
262#else
263/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
264if (upper_bound > 0x80000000)
265min = 1 + ~upper_bound;/* 2**32 - upper_bound */
266else {
267/* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
268min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
269}
270#endif
271
272/*
273 * This could theoretically loop forever but each retry has
274 * p > 0.5 (worst case, usually far better) of selecting a
275 * number inside the range we need, so it should rarely need
276 * to re-roll.
277 */
278for (;;) {
279r = arc4random();
280if (r >= min)
281break;
282}
283
284return (r % upper_bound);
285}
286
287#if 0
288void
289arc4_init(void)
290{
291}
292
293/*-------- Test code for i386 --------*/
294#include <stdio.h>
295#include <machine/pctr.h>
296int
297main(int argc, char **argv)
298{
299const int iter = 1000000;
300int i;
301pctrval v;
302
303v = rdtsc();
304for (i = 0; i < iter; i++)
305arc4random();
306v = rdtsc() - v;
307v /= iter;
308
309printf("%qd cycles\n", v);
310}
311#endif
312

Archive Download this file

Revision: HEAD