1 /* $FreeBSD: src/sys/dev/rndtest/rndtest.c,v 1.1.4.1 2003/06/04 17:10:30 sam Exp $ */
2 /* $DragonFly: src/sys/dev/crypto/rndtest/rndtest.c,v 1.6 2004/05/13 19:44:31 dillon Exp $ */
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/param.h>
37 #include <sys/systm.h>
39 #include <sys/callout.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/random.h>
44 #include <sys/sysctl.h>
45 #include <machine/stdarg.h>
49 static void rndtest_test(struct rndtest_state *);
50 static void rndtest_timeout(void *);
52 /* The tests themselves */
53 static int rndtest_monobit(struct rndtest_state *);
54 static int rndtest_runs(struct rndtest_state *);
55 static int rndtest_longruns(struct rndtest_state *);
56 static int rndtest_chi_4(struct rndtest_state *);
58 static int rndtest_runs_check(struct rndtest_state *, int, int *);
59 static void rndtest_runs_record(struct rndtest_state *, int, int *);
61 static const struct rndtest_testfunc {
62 int (*test)(struct rndtest_state *);
70 #define RNDTEST_NTESTS (sizeof(rndtest_funcs)/sizeof(rndtest_funcs[0]))
72 SYSCTL_NODE(_kern, OID_AUTO, rndtest, CTLFLAG_RD, 0, "RNG test parameters");
74 static int rndtest_retest = 120; /* interval in seconds */
75 SYSCTL_INT(_kern_rndtest, OID_AUTO, retest, CTLFLAG_RW, &rndtest_retest,
76 0, "retest interval (seconds)");
77 static struct rndtest_stats rndstats;
78 SYSCTL_STRUCT(_kern_rndtest, OID_AUTO, stats, CTLFLAG_RD, &rndstats,
79 rndtest_stats, "RNG test statistics");
80 static int rndtest_verbose = 1; /* report only failures */
81 SYSCTL_INT(_kern_rndtest, OID_AUTO, verbose, CTLFLAG_RW, &rndtest_verbose,
82 0, "display results on console");
84 struct rndtest_state *
85 rndtest_attach(device_t dev)
87 struct rndtest_state *rsp;
89 rsp = malloc(sizeof (*rsp), M_DEVBUF, M_INTWAIT);
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 defined(__DragonFly__) || __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);
106 rndtest_detach(struct rndtest_state *rsp)
108 callout_stop(&rsp->rs_to);
113 rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len)
117 * If enabled, collect data and run tests when we have enough.
119 if (rsp->rs_collect) {
120 for (i = 0; i < len; i++) {
121 *rsp->rs_current = ((u_char *) buf)[i];
122 if (++rsp->rs_current == rsp->rs_end) {
124 rsp->rs_current = rsp->rs_begin;
126 * If tests passed, turn off collection and
127 * schedule another test. Otherwise we keep
128 * testing until the data looks ok.
130 if (!rsp->rs_discard && rndtest_retest != 0) {
132 callout_reset(&rsp->rs_to,
134 rndtest_timeout, rsp);
141 * Only stir entropy that passes muster into the pool.
144 rndstats.rst_discard += len;
146 #if defined(__DragonFly__) || __FreeBSD_version < 500000
147 /* XXX verify buffer is word aligned */
149 for (len /= sizeof (u_int32_t); len; len--)
150 add_true_randomness(*p++);
152 random_harvest(buf, len, len*NBBY, 0, RANDOM_PURE);
158 rndtest_test(struct rndtest_state *rsp)
162 rndstats.rst_tests++;
163 for (i = 0; i < RNDTEST_NTESTS; i++)
164 rv |= (*rndtest_funcs[i].test)(rsp);
165 rsp->rs_discard = (rv != 0);
169 rndtest_report(struct rndtest_state *rsp, int failure, const char *fmt, ...)
174 if (rndtest_verbose == 0)
176 if (!failure && rndtest_verbose == 1) /* don't report successes */
179 vsnprintf(buf, sizeof (buf), fmt, ap);
181 device_printf(rsp->rs_parent, "rndtest: %s\n", buf);
184 #define RNDTEST_MONOBIT_MINONES 9725
185 #define RNDTEST_MONOBIT_MAXONES 10275
188 rndtest_monobit(struct rndtest_state *rsp)
193 for (i = 0; i < RNDTEST_NBYTES; i++) {
195 for (j = 0; j < 8; j++, r <<= 1)
199 if (ones > RNDTEST_MONOBIT_MINONES &&
200 ones < RNDTEST_MONOBIT_MAXONES) {
201 if (rndtest_verbose > 1)
202 rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)",
203 RNDTEST_MONOBIT_MINONES, ones,
204 RNDTEST_MONOBIT_MAXONES);
208 rndtest_report(rsp, 1,
209 "monobit failed (%d ones)", ones);
210 rndstats.rst_monobit++;
215 #define RNDTEST_RUNS_NINTERVAL 6
217 static const struct rndtest_runs_tabs {
219 } rndtest_runs_tab[] = {
229 rndtest_runs(struct rndtest_state *rsp)
231 int i, j, ones, zeros, rv = 0;
232 int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL];
235 bzero(onei, sizeof(onei));
236 bzero(zeroi, sizeof(zeroi));
238 for (i = 0; i < RNDTEST_NBYTES; i++) {
240 for (j = 0; j < 8; j++, c <<= 1) {
243 rndtest_runs_record(rsp, zeros, zeroi);
247 rndtest_runs_record(rsp, ones, onei);
252 rndtest_runs_record(rsp, ones, onei);
253 rndtest_runs_record(rsp, zeros, zeroi);
255 rv |= rndtest_runs_check(rsp, 0, zeroi);
256 rv |= rndtest_runs_check(rsp, 1, onei);
265 rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv)
269 if (len > RNDTEST_RUNS_NINTERVAL)
270 len = RNDTEST_RUNS_NINTERVAL;
276 rndtest_runs_check(struct rndtest_state *rsp, int val, int *src)
280 for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) {
281 if (src[i] < rndtest_runs_tab[i].min ||
282 src[i] > rndtest_runs_tab[i].max) {
283 rndtest_report(rsp, 1,
284 "%s interval %d failed (%d, %d-%d)",
285 val ? "ones" : "zeros",
286 i + 1, src[i], rndtest_runs_tab[i].min,
287 rndtest_runs_tab[i].max);
290 rndtest_report(rsp, 0,
291 "runs pass %s interval %d (%d < %d < %d)",
292 val ? "ones" : "zeros",
293 i + 1, rndtest_runs_tab[i].min, src[i],
294 rndtest_runs_tab[i].max);
301 rndtest_longruns(struct rndtest_state *rsp)
303 int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0;
306 for (i = 0; i < RNDTEST_NBYTES; i++) {
308 for (j = 0; j < 8; j++, c <<= 1) {
317 if (zeros > maxzeros)
323 if (maxones < 26 && maxzeros < 26) {
324 rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)",
328 rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)",
330 rndstats.rst_longruns++;
336 * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
337 * but it is really the chi^2 test over 4 bits (the poker test as described
338 * by Knuth vol 2 is something different, and I take him as authoritative
339 * on nomenclature over NIST).
341 #define RNDTEST_CHI4_K 16
342 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
345 * The unnormalized values are used so that we don't have to worry about
346 * fractional precision. The "real" value is found by:
347 * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
349 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
350 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
353 rndtest_chi_4(struct rndtest_state *rsp)
355 unsigned int freq[RNDTEST_CHI4_K], i, sum;
357 for (i = 0; i < RNDTEST_CHI4_K; i++)
360 /* Get number of occurances of each 4 bit pattern */
361 for (i = 0; i < RNDTEST_NBYTES; i++) {
362 freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
363 freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;
366 for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++)
367 sum += freq[i] * freq[i];
369 if (sum >= 1563181 && sum <= 1576929) {
370 rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum);
373 rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum);
380 rndtest_timeout(void *xrsp)
382 struct rndtest_state *rsp = xrsp;
388 rndtest_modevent(module_t mod, int type, void *unused)
399 static moduledata_t rndtest_mod = {
404 DECLARE_MODULE(rndtest, rndtest_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
405 MODULE_VERSION(rndtest, 1);