1 /* $OpenBSD$ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 2002 Jason L. Wright (jason@thought.net) 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 22 * 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. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/bus.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> 49 50 #include <dev/rndtest/rndtest.h> 51 52 static void rndtest_test(struct rndtest_state *); 53 static void rndtest_timeout(void *); 54 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 *); 60 61 static int rndtest_runs_check(struct rndtest_state *, int, int *); 62 static void rndtest_runs_record(struct rndtest_state *, int, int *); 63 64 static const struct rndtest_testfunc { 65 int (*test)(struct rndtest_state *); 66 } rndtest_funcs[] = { 67 { rndtest_monobit }, 68 { rndtest_runs }, 69 { rndtest_chi_4 }, 70 { rndtest_longruns }, 71 }; 72 73 #define RNDTEST_NTESTS nitems(rndtest_funcs) 74 75 static SYSCTL_NODE(_kern, OID_AUTO, rndtest, CTLFLAG_RD, 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"); 86 87 struct rndtest_state * 88 rndtest_attach(device_t dev) 89 { 90 struct rndtest_state *rsp; 91 92 rsp = malloc(sizeof (*rsp), M_DEVBUF, M_NOWAIT); 93 if (rsp != NULL) { 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; 97 rsp->rs_discard = 1; 98 rsp->rs_collect = 1; 99 rsp->rs_parent = dev; 100 #if __FreeBSD_version < 500000 101 callout_init(&rsp->rs_to); 102 #else 103 callout_init(&rsp->rs_to, 1); 104 #endif 105 } else 106 device_printf(dev, "rndtest_init: no memory for state block\n"); 107 return (rsp); 108 } 109 110 void 111 rndtest_detach(struct rndtest_state *rsp) 112 { 113 callout_stop(&rsp->rs_to); 114 free(rsp, M_DEVBUF); 115 } 116 117 void 118 rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len) 119 { 120 size_t i; 121 /* 122 * If enabled, collect data and run tests when we have enough. 123 */ 124 if (rsp->rs_collect) { 125 for (i = 0; i < len; i++) { 126 *rsp->rs_current = ((u_char *) buf)[i]; 127 if (++rsp->rs_current == rsp->rs_end) { 128 rndtest_test(rsp); 129 rsp->rs_current = rsp->rs_begin; 130 /* 131 * If tests passed, turn off collection and 132 * schedule another test. Otherwise we keep 133 * testing until the data looks ok. 134 */ 135 if (!rsp->rs_discard && rndtest_retest != 0) { 136 rsp->rs_collect = 0; 137 callout_reset(&rsp->rs_to, 138 hz * rndtest_retest, 139 rndtest_timeout, rsp); 140 break; 141 } 142 } 143 } 144 } 145 /* 146 * Only stir entropy that passes muster into the pool. 147 */ 148 if (rsp->rs_discard) 149 rndstats.rst_discard += len; 150 else 151 /* MarkM: FIX!! Check that this does not swamp the harvester! */ 152 random_harvest_queue(buf, len, RANDOM_PURE_RNDTEST); 153 } 154 155 static void 156 rndtest_test(struct rndtest_state *rsp) 157 { 158 int i, rv = 0; 159 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); 164 } 165 166 static void 167 rndtest_report(struct rndtest_state *rsp, int failure, const char *fmt, ...) 168 { 169 char buf[80]; 170 va_list ap; 171 172 if (rndtest_verbose == 0) 173 return; 174 if (!failure && rndtest_verbose == 1) /* don't report successes */ 175 return; 176 va_start(ap, fmt); 177 vsnprintf(buf, sizeof (buf), fmt, ap); 178 va_end(ap); 179 device_printf(rsp->rs_parent, "rndtest: %s\n", buf); 180 } 181 182 #define RNDTEST_MONOBIT_MINONES 9725 183 #define RNDTEST_MONOBIT_MAXONES 10275 184 185 static int 186 rndtest_monobit(struct rndtest_state *rsp) 187 { 188 int i, ones = 0, j; 189 u_int8_t r; 190 191 for (i = 0; i < RNDTEST_NBYTES; i++) { 192 r = rsp->rs_buf[i]; 193 for (j = 0; j < 8; j++, r <<= 1) 194 if (r & 0x80) 195 ones++; 196 } 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); 203 return (0); 204 } else { 205 if (rndtest_verbose) 206 rndtest_report(rsp, 1, 207 "monobit failed (%d ones)", ones); 208 rndstats.rst_monobit++; 209 return (-1); 210 } 211 } 212 213 #define RNDTEST_RUNS_NINTERVAL 6 214 215 static const struct rndtest_runs_tabs { 216 u_int16_t min, max; 217 } rndtest_runs_tab[] = { 218 { 2343, 2657 }, 219 { 1135, 1365 }, 220 { 542, 708 }, 221 { 251, 373 }, 222 { 111, 201 }, 223 { 111, 201 }, 224 }; 225 226 static int 227 rndtest_runs(struct rndtest_state *rsp) 228 { 229 int i, j, ones, zeros, rv = 0; 230 int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL]; 231 u_int8_t c; 232 233 bzero(onei, sizeof(onei)); 234 bzero(zeroi, sizeof(zeroi)); 235 ones = zeros = 0; 236 for (i = 0; i < RNDTEST_NBYTES; i++) { 237 c = rsp->rs_buf[i]; 238 for (j = 0; j < 8; j++, c <<= 1) { 239 if (c & 0x80) { 240 ones++; 241 rndtest_runs_record(rsp, zeros, zeroi); 242 zeros = 0; 243 } else { 244 zeros++; 245 rndtest_runs_record(rsp, ones, onei); 246 ones = 0; 247 } 248 } 249 } 250 rndtest_runs_record(rsp, ones, onei); 251 rndtest_runs_record(rsp, zeros, zeroi); 252 253 rv |= rndtest_runs_check(rsp, 0, zeroi); 254 rv |= rndtest_runs_check(rsp, 1, onei); 255 256 if (rv) 257 rndstats.rst_runs++; 258 259 return (rv); 260 } 261 262 static void 263 rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv) 264 { 265 if (len == 0) 266 return; 267 if (len > RNDTEST_RUNS_NINTERVAL) 268 len = RNDTEST_RUNS_NINTERVAL; 269 len -= 1; 270 intrv[len]++; 271 } 272 273 static int 274 rndtest_runs_check(struct rndtest_state *rsp, int val, int *src) 275 { 276 int i, rv = 0; 277 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); 286 rv = -1; 287 } else { 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); 293 } 294 } 295 return (rv); 296 } 297 298 static int 299 rndtest_longruns(struct rndtest_state *rsp) 300 { 301 int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0; 302 u_int8_t c; 303 304 for (i = 0; i < RNDTEST_NBYTES; i++) { 305 c = rsp->rs_buf[i]; 306 for (j = 0; j < 8; j++, c <<= 1) { 307 if (c & 0x80) { 308 zeros = 0; 309 ones++; 310 if (ones > maxones) 311 maxones = ones; 312 } else { 313 ones = 0; 314 zeros++; 315 if (zeros > maxzeros) 316 maxzeros = zeros; 317 } 318 } 319 } 320 321 if (maxones < 26 && maxzeros < 26) { 322 rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)", 323 maxones, maxzeros); 324 return (0); 325 } else { 326 rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)", 327 maxones, maxzeros); 328 rndstats.rst_longruns++; 329 return (-1); 330 } 331 } 332 333 /* 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). 338 */ 339 #define RNDTEST_CHI4_K 16 340 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1) 341 342 /* 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) 346 */ 347 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */ 348 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */ 349 350 static int 351 rndtest_chi_4(struct rndtest_state *rsp) 352 { 353 unsigned int freq[RNDTEST_CHI4_K], i, sum; 354 355 for (i = 0; i < RNDTEST_CHI4_K; i++) 356 freq[i] = 0; 357 358 /* Get number of occurrences 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]++; 362 } 363 364 for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++) 365 sum += freq[i] * freq[i]; 366 367 if (sum >= 1563181 && sum <= 1576929) { 368 rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum); 369 return (0); 370 } else { 371 rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum); 372 rndstats.rst_chi++; 373 return (-1); 374 } 375 } 376 377 static void 378 rndtest_timeout(void *xrsp) 379 { 380 struct rndtest_state *rsp = xrsp; 381 382 rsp->rs_collect = 1; 383 } 384 385 static int 386 rndtest_modevent(module_t mod, int type, void *unused) 387 { 388 switch (type) { 389 case MOD_LOAD: 390 return 0; 391 case MOD_UNLOAD: 392 return 0; 393 } 394 return EINVAL; 395 } 396 397 static moduledata_t rndtest_mod = { 398 "rndtest", 399 rndtest_modevent, 400 0 401 }; 402 DECLARE_MODULE(rndtest, rndtest_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 403 MODULE_VERSION(rndtest, 1); 404