4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Jason L. Wright
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/callout.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/random.h>
47 #include <sys/sysctl.h>
48 #include <machine/stdarg.h>
50 #include <dev/rndtest/rndtest.h>
52 static void rndtest_test(struct rndtest_state *);
53 static void rndtest_timeout(void *);
55 /* The tests themselves */
56 static int rndtest_monobit(struct rndtest_state *);
57 static int rndtest_runs(struct rndtest_state *);
58 static int rndtest_longruns(struct rndtest_state *);
59 static int rndtest_chi_4(struct rndtest_state *);
61 static int rndtest_runs_check(struct rndtest_state *, int, int *);
62 static void rndtest_runs_record(struct rndtest_state *, int, int *);
64 static const struct rndtest_testfunc {
65 int (*test)(struct rndtest_state *);
73 #define RNDTEST_NTESTS nitems(rndtest_funcs)
75 static SYSCTL_NODE(_kern, OID_AUTO, rndtest, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
76 "RNG test parameters");
77 static int rndtest_retest = 120; /* interval in seconds */
78 SYSCTL_INT(_kern_rndtest, OID_AUTO, retest, CTLFLAG_RW, &rndtest_retest,
79 0, "retest interval (seconds)");
80 static struct rndtest_stats rndstats;
81 SYSCTL_STRUCT(_kern_rndtest, OID_AUTO, stats, CTLFLAG_RD, &rndstats,
82 rndtest_stats, "RNG test statistics");
83 static int rndtest_verbose = 1; /* report only failures */
84 SYSCTL_INT(_kern_rndtest, OID_AUTO, verbose, CTLFLAG_RW, &rndtest_verbose,
85 0, "display results on console");
87 struct rndtest_state *
88 rndtest_attach(device_t dev)
90 struct rndtest_state *rsp;
92 rsp = malloc(sizeof (*rsp), M_DEVBUF, M_NOWAIT);
94 rsp->rs_begin = rsp->rs_buf;
95 rsp->rs_end = rsp->rs_buf + sizeof(rsp->rs_buf);
96 rsp->rs_current = rsp->rs_begin;
100 callout_init(&rsp->rs_to, 1);
102 device_printf(dev, "rndtest_init: no memory for state block\n");
107 rndtest_detach(struct rndtest_state *rsp)
109 callout_stop(&rsp->rs_to);
114 rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len)
118 * If enabled, collect data and run tests when we have enough.
120 if (rsp->rs_collect) {
121 for (i = 0; i < len; i++) {
122 *rsp->rs_current = ((u_char *) buf)[i];
123 if (++rsp->rs_current == rsp->rs_end) {
125 rsp->rs_current = rsp->rs_begin;
127 * If tests passed, turn off collection and
128 * schedule another test. Otherwise we keep
129 * testing until the data looks ok.
131 if (!rsp->rs_discard && rndtest_retest != 0) {
133 callout_reset(&rsp->rs_to,
135 rndtest_timeout, rsp);
142 * Only stir entropy that passes muster into the pool.
145 rndstats.rst_discard += len;
147 /* MarkM: FIX!! Check that this does not swamp the harvester! */
148 random_harvest_queue(buf, len, RANDOM_PURE_RNDTEST);
152 rndtest_test(struct rndtest_state *rsp)
156 rndstats.rst_tests++;
157 for (i = 0; i < RNDTEST_NTESTS; i++)
158 rv |= (*rndtest_funcs[i].test)(rsp);
159 rsp->rs_discard = (rv != 0);
163 rndtest_report(struct rndtest_state *rsp, int failure, const char *fmt, ...)
168 if (rndtest_verbose == 0)
170 if (!failure && rndtest_verbose == 1) /* don't report successes */
173 vsnprintf(buf, sizeof (buf), fmt, ap);
175 device_printf(rsp->rs_parent, "rndtest: %s\n", buf);
178 #define RNDTEST_MONOBIT_MINONES 9725
179 #define RNDTEST_MONOBIT_MAXONES 10275
182 rndtest_monobit(struct rndtest_state *rsp)
187 for (i = 0; i < RNDTEST_NBYTES; i++) {
189 for (j = 0; j < 8; j++, r <<= 1)
193 if (ones > RNDTEST_MONOBIT_MINONES &&
194 ones < RNDTEST_MONOBIT_MAXONES) {
195 if (rndtest_verbose > 1)
196 rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)",
197 RNDTEST_MONOBIT_MINONES, ones,
198 RNDTEST_MONOBIT_MAXONES);
202 rndtest_report(rsp, 1,
203 "monobit failed (%d ones)", ones);
204 rndstats.rst_monobit++;
209 #define RNDTEST_RUNS_NINTERVAL 6
211 static const struct rndtest_runs_tabs {
213 } rndtest_runs_tab[] = {
223 rndtest_runs(struct rndtest_state *rsp)
225 int i, j, ones, zeros, rv = 0;
226 int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL];
229 bzero(onei, sizeof(onei));
230 bzero(zeroi, sizeof(zeroi));
232 for (i = 0; i < RNDTEST_NBYTES; i++) {
234 for (j = 0; j < 8; j++, c <<= 1) {
237 rndtest_runs_record(rsp, zeros, zeroi);
241 rndtest_runs_record(rsp, ones, onei);
246 rndtest_runs_record(rsp, ones, onei);
247 rndtest_runs_record(rsp, zeros, zeroi);
249 rv |= rndtest_runs_check(rsp, 0, zeroi);
250 rv |= rndtest_runs_check(rsp, 1, onei);
259 rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv)
263 if (len > RNDTEST_RUNS_NINTERVAL)
264 len = RNDTEST_RUNS_NINTERVAL;
270 rndtest_runs_check(struct rndtest_state *rsp, int val, int *src)
274 for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) {
275 if (src[i] < rndtest_runs_tab[i].min ||
276 src[i] > rndtest_runs_tab[i].max) {
277 rndtest_report(rsp, 1,
278 "%s interval %d failed (%d, %d-%d)",
279 val ? "ones" : "zeros",
280 i + 1, src[i], rndtest_runs_tab[i].min,
281 rndtest_runs_tab[i].max);
284 rndtest_report(rsp, 0,
285 "runs pass %s interval %d (%d < %d < %d)",
286 val ? "ones" : "zeros",
287 i + 1, rndtest_runs_tab[i].min, src[i],
288 rndtest_runs_tab[i].max);
295 rndtest_longruns(struct rndtest_state *rsp)
297 int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0;
300 for (i = 0; i < RNDTEST_NBYTES; i++) {
302 for (j = 0; j < 8; j++, c <<= 1) {
311 if (zeros > maxzeros)
317 if (maxones < 26 && maxzeros < 26) {
318 rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)",
322 rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)",
324 rndstats.rst_longruns++;
330 * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
331 * but it is really the chi^2 test over 4 bits (the poker test as described
332 * by Knuth vol 2 is something different, and I take him as authoritative
333 * on nomenclature over NIST).
335 #define RNDTEST_CHI4_K 16
336 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
339 * The unnormalized values are used so that we don't have to worry about
340 * fractional precision. The "real" value is found by:
341 * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
343 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
344 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
347 rndtest_chi_4(struct rndtest_state *rsp)
349 unsigned int freq[RNDTEST_CHI4_K], i, sum;
351 for (i = 0; i < RNDTEST_CHI4_K; i++)
354 /* Get number of occurrences of each 4 bit pattern */
355 for (i = 0; i < RNDTEST_NBYTES; i++) {
356 freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
357 freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;
360 for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++)
361 sum += freq[i] * freq[i];
363 if (sum >= 1563181 && sum <= 1576929) {
364 rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum);
367 rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum);
374 rndtest_timeout(void *xrsp)
376 struct rndtest_state *rsp = xrsp;
382 rndtest_modevent(module_t mod, int type, void *unused)
393 static moduledata_t rndtest_mod = {
398 DECLARE_MODULE(rndtest, rndtest_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
399 MODULE_VERSION(rndtest, 1);