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>
46 #include <dev/rndtest/rndtest.h>
48 static void rndtest_test(struct rndtest_state *);
49 static void rndtest_timeout(void *);
51 /* The tests themselves */
52 static int rndtest_monobit(struct rndtest_state *);
53 static int rndtest_runs(struct rndtest_state *);
54 static int rndtest_longruns(struct rndtest_state *);
55 static int rndtest_chi_4(struct rndtest_state *);
57 static int rndtest_runs_check(struct rndtest_state *, int, int *);
58 static void rndtest_runs_record(struct rndtest_state *, int, int *);
60 static const struct rndtest_testfunc {
61 int (*test)(struct rndtest_state *);
69 #define RNDTEST_NTESTS (sizeof(rndtest_funcs)/sizeof(rndtest_funcs[0]))
71 SYSCTL_NODE(_kern, OID_AUTO, rndtest, CTLFLAG_RD, 0, "RNG test parameters");
73 static int rndtest_retest = 120; /* interval in seconds */
74 SYSCTL_INT(_kern_rndtest, OID_AUTO, retest, CTLFLAG_RW, &rndtest_retest,
75 0, "retest interval (seconds)");
76 static struct rndtest_stats rndstats;
77 SYSCTL_STRUCT(_kern_rndtest, OID_AUTO, stats, CTLFLAG_RD, &rndstats,
78 rndtest_stats, "RNG test statistics");
79 static int rndtest_verbose = 1; /* report only failures */
80 SYSCTL_INT(_kern_rndtest, OID_AUTO, verbose, CTLFLAG_RW, &rndtest_verbose,
81 0, "display results on console");
83 struct rndtest_state *
84 rndtest_attach(device_t dev)
86 struct rndtest_state *rsp;
88 rsp = malloc(sizeof (*rsp), M_DEVBUF, M_NOWAIT);
90 rsp->rs_begin = rsp->rs_buf;
91 rsp->rs_end = rsp->rs_buf + sizeof(rsp->rs_buf);
92 rsp->rs_current = rsp->rs_begin;
96 #if __FreeBSD_version < 500000
97 callout_init(&rsp->rs_to);
99 /* NB: 1 means the callout runs w/o Giant locked */
100 callout_init(&rsp->rs_to, 1);
103 device_printf(dev, "rndtest_init: no memory for state block\n");
108 rndtest_detach(struct rndtest_state *rsp)
110 callout_stop(&rsp->rs_to);
115 rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len)
119 * If enabled, collect data and run tests when we have enough.
121 if (rsp->rs_collect) {
122 for (i = 0; i < len; i++) {
123 *rsp->rs_current = ((u_char *) buf)[i];
124 if (++rsp->rs_current == rsp->rs_end) {
126 rsp->rs_current = rsp->rs_begin;
128 * If tests passed, turn off collection and
129 * schedule another test. Otherwise we keep
130 * testing until the data looks ok.
132 if (!rsp->rs_discard && rndtest_retest != 0) {
134 callout_reset(&rsp->rs_to,
136 rndtest_timeout, rsp);
143 * Only stir entropy that passes muster into the pool.
146 rndstats.rst_discard += len;
148 #if __FreeBSD_version < 500000
149 /* XXX verify buffer is word aligned */
151 for (len /= sizeof (u_int32_t); len; len--)
152 add_true_randomness(*p++);
154 random_harvest(buf, len, len*NBBY, 0, RANDOM_PURE);
160 rndtest_test(struct rndtest_state *rsp)
164 rndstats.rst_tests++;
165 for (i = 0; i < RNDTEST_NTESTS; i++)
166 rv |= (*rndtest_funcs[i].test)(rsp);
167 rsp->rs_discard = (rv != 0);
171 rndtest_report(struct rndtest_state *rsp, int failure, const char *fmt, ...)
176 if (rndtest_verbose == 0)
178 if (!failure && rndtest_verbose == 1) /* don't report successes */
181 vsnprintf(buf, sizeof (buf), fmt, ap);
183 device_printf(rsp->rs_parent, "rndtest: %s\n", buf);
186 #define RNDTEST_MONOBIT_MINONES 9725
187 #define RNDTEST_MONOBIT_MAXONES 10275
190 rndtest_monobit(struct rndtest_state *rsp)
195 for (i = 0; i < RNDTEST_NBYTES; i++) {
197 for (j = 0; j < 8; j++, r <<= 1)
201 if (ones > RNDTEST_MONOBIT_MINONES &&
202 ones < RNDTEST_MONOBIT_MAXONES) {
203 if (rndtest_verbose > 1)
204 rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)",
205 RNDTEST_MONOBIT_MINONES, ones,
206 RNDTEST_MONOBIT_MAXONES);
210 rndtest_report(rsp, 1,
211 "monobit failed (%d ones)", ones);
212 rndstats.rst_monobit++;
217 #define RNDTEST_RUNS_NINTERVAL 6
219 static const struct rndtest_runs_tabs {
221 } rndtest_runs_tab[] = {
231 rndtest_runs(struct rndtest_state *rsp)
233 int i, j, ones, zeros, rv = 0;
234 int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL];
237 bzero(onei, sizeof(onei));
238 bzero(zeroi, sizeof(zeroi));
240 for (i = 0; i < RNDTEST_NBYTES; i++) {
242 for (j = 0; j < 8; j++, c <<= 1) {
245 rndtest_runs_record(rsp, zeros, zeroi);
249 rndtest_runs_record(rsp, ones, onei);
254 rndtest_runs_record(rsp, ones, onei);
255 rndtest_runs_record(rsp, zeros, zeroi);
257 rv |= rndtest_runs_check(rsp, 0, zeroi);
258 rv |= rndtest_runs_check(rsp, 1, onei);
267 rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv)
271 if (len > RNDTEST_RUNS_NINTERVAL)
272 len = RNDTEST_RUNS_NINTERVAL;
278 rndtest_runs_check(struct rndtest_state *rsp, int val, int *src)
282 for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) {
283 if (src[i] < rndtest_runs_tab[i].min ||
284 src[i] > rndtest_runs_tab[i].max) {
285 rndtest_report(rsp, 1,
286 "%s interval %d failed (%d, %d-%d)",
287 val ? "ones" : "zeros",
288 i + 1, src[i], rndtest_runs_tab[i].min,
289 rndtest_runs_tab[i].max);
292 rndtest_report(rsp, 0,
293 "runs pass %s interval %d (%d < %d < %d)",
294 val ? "ones" : "zeros",
295 i + 1, rndtest_runs_tab[i].min, src[i],
296 rndtest_runs_tab[i].max);
303 rndtest_longruns(struct rndtest_state *rsp)
305 int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0;
308 for (i = 0; i < RNDTEST_NBYTES; i++) {
310 for (j = 0; j < 8; j++, c <<= 1) {
319 if (zeros > maxzeros)
325 if (maxones < 26 && maxzeros < 26) {
326 rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)",
330 rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)",
332 rndstats.rst_longruns++;
338 * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
339 * but it is really the chi^2 test over 4 bits (the poker test as described
340 * by Knuth vol 2 is something different, and I take him as authoritative
341 * on nomenclature over NIST).
343 #define RNDTEST_CHI4_K 16
344 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
347 * The unnormalized values are used so that we don't have to worry about
348 * fractional precision. The "real" value is found by:
349 * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
351 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
352 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
355 rndtest_chi_4(struct rndtest_state *rsp)
357 unsigned int freq[RNDTEST_CHI4_K], i, sum;
359 for (i = 0; i < RNDTEST_CHI4_K; i++)
362 /* Get number of occurances of each 4 bit pattern */
363 for (i = 0; i < RNDTEST_NBYTES; i++) {
364 freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
365 freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;
368 for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++)
369 sum += freq[i] * freq[i];
371 if (sum >= 1563181 && sum <= 1576929) {
372 rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum);
375 rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum);
382 rndtest_timeout(void *xrsp)
384 struct rndtest_state *rsp = xrsp;
390 rndtest_modevent(module_t mod, int type, void *unused)
401 static moduledata_t rndtest_mod = {
406 DECLARE_MODULE(rndtest, rndtest_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
407 MODULE_VERSION(rndtest, 1);