1 /* $NetBSD: arc4random.c,v 1.19 2007/07/19 22:00:04 dsl Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Thor Lancelot Simon.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * THE BEER-WARE LICENSE
35 * <dan@FreeBSD.ORG> wrote this file. As long as you retain this notice you
36 * can do whatever you want with this stuff. If we meet some day, and you
37 * think this stuff is worth it, you can buy me a beer in return.
41 * $FreeBSD: src/sys/libkern/arc4random.c,v 1.9 2001/08/30 12:30:58 bde Exp $
44 #include <sys/cdefs.h>
52 #include <sys/types.h>
54 #include <sys/param.h>
56 #include <sys/kernel.h>
58 #include <sys/systm.h>
60 #include <lib/libkern/libkern.h>
66 #define ARC4_MAXRUNS 16384
67 #define ARC4_RESEED_SECONDS 300
68 #define ARC4_KEYBYTES 32 /* 256 bit key */
71 #define time_uptime 1 /* XXX ugly! */
72 #endif /* _STANDALONE */
74 static u_int8_t arc4_i
, arc4_j
;
75 static int arc4_initialized
= 0;
76 static int arc4_numruns
= 0;
77 static u_int8_t arc4_sbox
[256];
78 static time_t arc4_nextreseed
;
80 static inline u_int8_t
arc4_randbyte(void);
83 arc4_swap(u_int8_t
*a
, u_int8_t
*b
)
99 static int cur_keybytes
;
105 if(!arc4_initialized
)
106 /* The first time through, we must take what we can get */
109 /* Don't rekey with less entropy than we already have */
110 byteswanted
= cur_keybytes
;
112 #if NRND > 0 /* XXX without rnd, we will key from the stack, ouch! */
113 r
= rnd_extract_data(key
, ARC4_KEYBYTES
, RND_EXTRACT_GOOD
);
115 if (r
< ARC4_KEYBYTES
) {
116 if (r
>= byteswanted
) {
117 (void)rnd_extract_data(key
+ r
,
121 /* don't replace a good key with a bad one! */
122 arc4_nextreseed
= time_uptime
+ ARC4_RESEED_SECONDS
;
124 /* we should just ask rnd(4) to rekey us when
125 it can, but for now, we'll just try later. */
132 for (n
= ARC4_KEYBYTES
; n
< sizeof(key
); n
++)
133 key
[n
] = key
[n
% ARC4_KEYBYTES
];
135 for (n
= 0; n
< 256; n
++) {
136 arc4_j
= (arc4_j
+ arc4_sbox
[n
] + key
[n
]) % 256;
137 arc4_swap(&arc4_sbox
[n
], &arc4_sbox
[arc4_j
]);
140 /* Reset for next reseed cycle. */
141 arc4_nextreseed
= time_uptime
+ ARC4_RESEED_SECONDS
;
145 * Throw away the first N words of output, as suggested in the
146 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
147 * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
149 for (n
= 0; n
< 256 * 4; n
++)
154 * Initialize our S-box to its beginning defaults.
162 for (n
= 0; n
< 256; n
++)
163 arc4_sbox
[n
] = (u_int8_t
) n
;
166 arc4_initialized
= 1;
170 * Generate a random byte.
172 static inline u_int8_t
177 arc4_i
= (arc4_i
+ 1) % 256;
178 arc4_j
= (arc4_j
+ arc4_sbox
[arc4_i
]) % 256;
180 arc4_swap(&arc4_sbox
[arc4_i
], &arc4_sbox
[arc4_j
]);
182 arc4_t
= (arc4_sbox
[arc4_i
] + arc4_sbox
[arc4_j
]) % 256;
183 return arc4_sbox
[arc4_t
];
192 /* Initialize array if needed. */
193 if (!arc4_initialized
)
196 if ((++arc4_numruns
> ARC4_MAXRUNS
) ||
197 (time_uptime
> arc4_nextreseed
)) {
201 for (i
= 0, ret
= 0; i
<= 24; ret
|= arc4_randbyte() << i
, i
+= 8)
207 arc4randbytes(void *p
, size_t len
)
212 /* Initialize array if needed. */
213 if (!arc4_initialized
)
218 for (i
= 0; i
< len
; buf
[i
] = arc4_randbyte(), i
++)
220 arc4_numruns
+= len
/ sizeof(u_int32_t
);
221 if ((arc4_numruns
> ARC4_MAXRUNS
) ||
222 (time_uptime
> arc4_nextreseed
)) {