1 /* $FreeBSD: src/sys/dev/rndtest/rndtest.c,v 1.1.4.1 2003/06/04 17:10:30 sam Exp $ */
5 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jason L. Wright
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/callout.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/random.h>
43 #include <sys/sysctl.h>
44 #include <machine/stdarg.h>
48 static int rndtest_modevent(module_t, int, void *);
49 static void rndtest_report(struct rndtest_state *, int, const char *, ...)
51 static void rndtest_test(struct rndtest_state *);
52 static void rndtest_timeout(void *);
54 /* The tests themselves */
55 static int rndtest_monobit(struct rndtest_state *);
56 static int rndtest_runs(struct rndtest_state *);
57 static int rndtest_longruns(struct rndtest_state *);
58 static int rndtest_chi_4(struct rndtest_state *);
60 static int rndtest_runs_check(struct rndtest_state *, int, int *);
61 static void rndtest_runs_record(struct rndtest_state *, int, int *);
63 static const struct rndtest_testfunc {
64 int (*test)(struct rndtest_state *);
72 #define RNDTEST_NTESTS NELEM(rndtest_funcs)
74 SYSCTL_NODE(_kern, OID_AUTO, rndtest, CTLFLAG_RD, 0, "RNG test parameters");
76 static int rndtest_retest = 120; /* interval in seconds */
77 SYSCTL_INT(_kern_rndtest, OID_AUTO, retest, CTLFLAG_RW, &rndtest_retest,
78 0, "retest interval (seconds)");
79 static struct rndtest_stats rndstats;
80 SYSCTL_STRUCT(_kern_rndtest, OID_AUTO, stats, CTLFLAG_RD, &rndstats,
81 rndtest_stats, "RNG test statistics");
82 static int rndtest_verbose = 1; /* report only failures */
83 SYSCTL_INT(_kern_rndtest, OID_AUTO, verbose, CTLFLAG_RW, &rndtest_verbose,
84 0, "display results on console");
86 struct rndtest_state *
87 rndtest_attach(device_t dev)
89 struct rndtest_state *rsp;
91 rsp = kmalloc(sizeof (*rsp), M_DEVBUF, M_INTWAIT);
92 rsp->rs_begin = rsp->rs_buf;
93 rsp->rs_end = rsp->rs_buf + sizeof(rsp->rs_buf);
94 rsp->rs_current = rsp->rs_begin;
98 /* NB: 1 means the callout runs w/o Giant locked */
99 callout_init_mp(&rsp->rs_to);
104 rndtest_detach(struct rndtest_state *rsp)
106 callout_stop(&rsp->rs_to);
107 kfree(rsp, M_DEVBUF);
111 rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len)
115 * If enabled, collect data and run tests when we have enough.
117 if (rsp->rs_collect) {
118 for (i = 0; i < len; i++) {
119 *rsp->rs_current = ((u_char *) buf)[i];
120 if (++rsp->rs_current == rsp->rs_end) {
122 rsp->rs_current = rsp->rs_begin;
124 * If tests passed, turn off collection and
125 * schedule another test. Otherwise we keep
126 * testing until the data looks ok.
128 if (!rsp->rs_discard && rndtest_retest != 0) {
130 callout_reset(&rsp->rs_to,
132 rndtest_timeout, rsp);
139 * Only stir entropy that passes muster into the pool.
142 rndstats.rst_discard += len;
144 #if defined(__DragonFly__) || __FreeBSD_version < 500000
145 /* XXX verify buffer is word aligned */
147 for (len /= sizeof (u_int32_t); len; len--)
148 add_true_randomness(*p++);
150 random_harvest(buf, len, len*NBBY, 0, RANDOM_PURE);
156 rndtest_test(struct rndtest_state *rsp)
160 rndstats.rst_tests++;
161 for (i = 0; i < RNDTEST_NTESTS; i++)
162 rv |= (*rndtest_funcs[i].test)(rsp);
163 rsp->rs_discard = (rv != 0);
167 rndtest_report(struct rndtest_state *rsp, int failure, const char *fmt, ...)
172 if (rndtest_verbose == 0)
174 if (!failure && rndtest_verbose == 1) /* don't report successes */
177 kvsnprintf(buf, sizeof (buf), fmt, ap);
179 device_printf(rsp->rs_parent, "rndtest: %s\n", buf);
182 #define RNDTEST_MONOBIT_MINONES 9725
183 #define RNDTEST_MONOBIT_MAXONES 10275
186 rndtest_monobit(struct rndtest_state *rsp)
191 for (i = 0; i < RNDTEST_NBYTES; i++) {
193 for (j = 0; j < 8; j++, r <<= 1)
197 if (ones > RNDTEST_MONOBIT_MINONES &&
198 ones < RNDTEST_MONOBIT_MAXONES) {
199 if (rndtest_verbose > 1)
200 rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)",
201 RNDTEST_MONOBIT_MINONES, ones,
202 RNDTEST_MONOBIT_MAXONES);
206 rndtest_report(rsp, 1,
207 "monobit failed (%d ones)", ones);
208 rndstats.rst_monobit++;
213 #define RNDTEST_RUNS_NINTERVAL 6
215 static const struct rndtest_runs_tabs {
217 } rndtest_runs_tab[] = {
227 rndtest_runs(struct rndtest_state *rsp)
229 int i, j, ones, zeros, rv = 0;
230 int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL];
233 bzero(onei, sizeof(onei));
234 bzero(zeroi, sizeof(zeroi));
236 for (i = 0; i < RNDTEST_NBYTES; i++) {
238 for (j = 0; j < 8; j++, c <<= 1) {
241 rndtest_runs_record(rsp, zeros, zeroi);
245 rndtest_runs_record(rsp, ones, onei);
250 rndtest_runs_record(rsp, ones, onei);
251 rndtest_runs_record(rsp, zeros, zeroi);
253 rv |= rndtest_runs_check(rsp, 0, zeroi);
254 rv |= rndtest_runs_check(rsp, 1, onei);
263 rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv)
267 if (len > RNDTEST_RUNS_NINTERVAL)
268 len = RNDTEST_RUNS_NINTERVAL;
274 rndtest_runs_check(struct rndtest_state *rsp, int val, int *src)
278 for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) {
279 if (src[i] < rndtest_runs_tab[i].min ||
280 src[i] > rndtest_runs_tab[i].max) {
281 rndtest_report(rsp, 1,
282 "%s interval %d failed (%d, %d-%d)",
283 val ? "ones" : "zeros",
284 i + 1, src[i], rndtest_runs_tab[i].min,
285 rndtest_runs_tab[i].max);
288 rndtest_report(rsp, 0,
289 "runs pass %s interval %d (%d < %d < %d)",
290 val ? "ones" : "zeros",
291 i + 1, rndtest_runs_tab[i].min, src[i],
292 rndtest_runs_tab[i].max);
299 rndtest_longruns(struct rndtest_state *rsp)
301 int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0;
304 for (i = 0; i < RNDTEST_NBYTES; i++) {
306 for (j = 0; j < 8; j++, c <<= 1) {
315 if (zeros > maxzeros)
321 if (maxones < 26 && maxzeros < 26) {
322 rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)",
326 rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)",
328 rndstats.rst_longruns++;
334 * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
335 * but it is really the chi^2 test over 4 bits (the poker test as described
336 * by Knuth vol 2 is something different, and I take him as authoritative
337 * on nomenclature over NIST).
339 #define RNDTEST_CHI4_K 16
340 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
343 * The unnormalized values are used so that we don't have to worry about
344 * fractional precision. The "real" value is found by:
345 * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
347 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
348 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
351 rndtest_chi_4(struct rndtest_state *rsp)
353 unsigned int freq[RNDTEST_CHI4_K], i, sum;
355 for (i = 0; i < RNDTEST_CHI4_K; i++)
358 /* Get number of occurances of each 4 bit pattern */
359 for (i = 0; i < RNDTEST_NBYTES; i++) {
360 freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
361 freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;
364 for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++)
365 sum += freq[i] * freq[i];
367 if (sum >= 1563181 && sum <= 1576929) {
368 rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum);
371 rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum);
378 rndtest_timeout(void *xrsp)
380 struct rndtest_state *rsp = xrsp;
386 rndtest_modevent(module_t mod, int type, void *unused)
397 static moduledata_t rndtest_mod = {
402 DECLARE_MODULE(rndtest, rndtest_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
403 MODULE_VERSION(rndtest, 1);