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 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/bus.h> 40 #include <sys/callout.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/random.h> 45 #include <sys/sysctl.h> 46 #include <machine/stdarg.h> 47 48 #include <dev/rndtest/rndtest.h> 49 50 static void rndtest_test(struct rndtest_state *); 51 static void rndtest_timeout(void *); 52 53 /* The tests themselves */ 54 static int rndtest_monobit(struct rndtest_state *); 55 static int rndtest_runs(struct rndtest_state *); 56 static int rndtest_longruns(struct rndtest_state *); 57 static int rndtest_chi_4(struct rndtest_state *); 58 59 static int rndtest_runs_check(struct rndtest_state *, int, int *); 60 static void rndtest_runs_record(struct rndtest_state *, int, int *); 61 62 static const struct rndtest_testfunc { 63 int (*test)(struct rndtest_state *); 64 } rndtest_funcs[] = { 65 { rndtest_monobit }, 66 { rndtest_runs }, 67 { rndtest_chi_4 }, 68 { rndtest_longruns }, 69 }; 70 71 #define RNDTEST_NTESTS nitems(rndtest_funcs) 72 73 static SYSCTL_NODE(_kern, OID_AUTO, rndtest, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 74 "RNG test parameters"); 75 static int rndtest_retest = 120; /* interval in seconds */ 76 SYSCTL_INT(_kern_rndtest, OID_AUTO, retest, CTLFLAG_RW, &rndtest_retest, 77 0, "retest interval (seconds)"); 78 static struct rndtest_stats rndstats; 79 SYSCTL_STRUCT(_kern_rndtest, OID_AUTO, stats, CTLFLAG_RD, &rndstats, 80 rndtest_stats, "RNG test statistics"); 81 static int rndtest_verbose = 1; /* report only failures */ 82 SYSCTL_INT(_kern_rndtest, OID_AUTO, verbose, CTLFLAG_RW, &rndtest_verbose, 83 0, "display results on console"); 84 85 struct rndtest_state * 86 rndtest_attach(device_t dev) 87 { 88 struct rndtest_state *rsp; 89 90 rsp = malloc(sizeof (*rsp), M_DEVBUF, M_NOWAIT); 91 if (rsp != NULL) { 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; 95 rsp->rs_discard = 1; 96 rsp->rs_collect = 1; 97 rsp->rs_parent = dev; 98 callout_init(&rsp->rs_to, 1); 99 } else 100 device_printf(dev, "rndtest_init: no memory for state block\n"); 101 return (rsp); 102 } 103 104 void 105 rndtest_detach(struct rndtest_state *rsp) 106 { 107 callout_stop(&rsp->rs_to); 108 free(rsp, M_DEVBUF); 109 } 110 111 void 112 rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len) 113 { 114 size_t i; 115 /* 116 * If enabled, collect data and run tests when we have enough. 117 */ 118 if (rsp->rs_collect) { 119 for (i = 0; i < len; i++) { 120 *rsp->rs_current = ((u_char *) buf)[i]; 121 if (++rsp->rs_current == rsp->rs_end) { 122 rndtest_test(rsp); 123 rsp->rs_current = rsp->rs_begin; 124 /* 125 * If tests passed, turn off collection and 126 * schedule another test. Otherwise we keep 127 * testing until the data looks ok. 128 */ 129 if (!rsp->rs_discard && rndtest_retest != 0) { 130 rsp->rs_collect = 0; 131 callout_reset(&rsp->rs_to, 132 hz * rndtest_retest, 133 rndtest_timeout, rsp); 134 break; 135 } 136 } 137 } 138 } 139 /* 140 * Only stir entropy that passes muster into the pool. 141 */ 142 if (rsp->rs_discard) 143 rndstats.rst_discard += len; 144 else 145 /* MarkM: FIX!! Check that this does not swamp the harvester! */ 146 random_harvest_queue(buf, len, RANDOM_PURE_RNDTEST); 147 } 148 149 static void 150 rndtest_test(struct rndtest_state *rsp) 151 { 152 int i, rv = 0; 153 154 rndstats.rst_tests++; 155 for (i = 0; i < RNDTEST_NTESTS; i++) 156 rv |= (*rndtest_funcs[i].test)(rsp); 157 rsp->rs_discard = (rv != 0); 158 } 159 160 static void 161 rndtest_report(struct rndtest_state *rsp, int failure, const char *fmt, ...) 162 { 163 char buf[80]; 164 va_list ap; 165 166 if (rndtest_verbose == 0) 167 return; 168 if (!failure && rndtest_verbose == 1) /* don't report successes */ 169 return; 170 va_start(ap, fmt); 171 vsnprintf(buf, sizeof (buf), fmt, ap); 172 va_end(ap); 173 device_printf(rsp->rs_parent, "rndtest: %s\n", buf); 174 } 175 176 #define RNDTEST_MONOBIT_MINONES 9725 177 #define RNDTEST_MONOBIT_MAXONES 10275 178 179 static int 180 rndtest_monobit(struct rndtest_state *rsp) 181 { 182 int i, ones = 0, j; 183 u_int8_t r; 184 185 for (i = 0; i < RNDTEST_NBYTES; i++) { 186 r = rsp->rs_buf[i]; 187 for (j = 0; j < 8; j++, r <<= 1) 188 if (r & 0x80) 189 ones++; 190 } 191 if (ones > RNDTEST_MONOBIT_MINONES && 192 ones < RNDTEST_MONOBIT_MAXONES) { 193 if (rndtest_verbose > 1) 194 rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)", 195 RNDTEST_MONOBIT_MINONES, ones, 196 RNDTEST_MONOBIT_MAXONES); 197 return (0); 198 } else { 199 if (rndtest_verbose) 200 rndtest_report(rsp, 1, 201 "monobit failed (%d ones)", ones); 202 rndstats.rst_monobit++; 203 return (-1); 204 } 205 } 206 207 #define RNDTEST_RUNS_NINTERVAL 6 208 209 static const struct rndtest_runs_tabs { 210 u_int16_t min, max; 211 } rndtest_runs_tab[] = { 212 { 2343, 2657 }, 213 { 1135, 1365 }, 214 { 542, 708 }, 215 { 251, 373 }, 216 { 111, 201 }, 217 { 111, 201 }, 218 }; 219 220 static int 221 rndtest_runs(struct rndtest_state *rsp) 222 { 223 int i, j, ones, zeros, rv = 0; 224 int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL]; 225 u_int8_t c; 226 227 bzero(onei, sizeof(onei)); 228 bzero(zeroi, sizeof(zeroi)); 229 ones = zeros = 0; 230 for (i = 0; i < RNDTEST_NBYTES; i++) { 231 c = rsp->rs_buf[i]; 232 for (j = 0; j < 8; j++, c <<= 1) { 233 if (c & 0x80) { 234 ones++; 235 rndtest_runs_record(rsp, zeros, zeroi); 236 zeros = 0; 237 } else { 238 zeros++; 239 rndtest_runs_record(rsp, ones, onei); 240 ones = 0; 241 } 242 } 243 } 244 rndtest_runs_record(rsp, ones, onei); 245 rndtest_runs_record(rsp, zeros, zeroi); 246 247 rv |= rndtest_runs_check(rsp, 0, zeroi); 248 rv |= rndtest_runs_check(rsp, 1, onei); 249 250 if (rv) 251 rndstats.rst_runs++; 252 253 return (rv); 254 } 255 256 static void 257 rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv) 258 { 259 if (len == 0) 260 return; 261 if (len > RNDTEST_RUNS_NINTERVAL) 262 len = RNDTEST_RUNS_NINTERVAL; 263 len -= 1; 264 intrv[len]++; 265 } 266 267 static int 268 rndtest_runs_check(struct rndtest_state *rsp, int val, int *src) 269 { 270 int i, rv = 0; 271 272 for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) { 273 if (src[i] < rndtest_runs_tab[i].min || 274 src[i] > rndtest_runs_tab[i].max) { 275 rndtest_report(rsp, 1, 276 "%s interval %d failed (%d, %d-%d)", 277 val ? "ones" : "zeros", 278 i + 1, src[i], rndtest_runs_tab[i].min, 279 rndtest_runs_tab[i].max); 280 rv = -1; 281 } else { 282 rndtest_report(rsp, 0, 283 "runs pass %s interval %d (%d < %d < %d)", 284 val ? "ones" : "zeros", 285 i + 1, rndtest_runs_tab[i].min, src[i], 286 rndtest_runs_tab[i].max); 287 } 288 } 289 return (rv); 290 } 291 292 static int 293 rndtest_longruns(struct rndtest_state *rsp) 294 { 295 int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0; 296 u_int8_t c; 297 298 for (i = 0; i < RNDTEST_NBYTES; i++) { 299 c = rsp->rs_buf[i]; 300 for (j = 0; j < 8; j++, c <<= 1) { 301 if (c & 0x80) { 302 zeros = 0; 303 ones++; 304 if (ones > maxones) 305 maxones = ones; 306 } else { 307 ones = 0; 308 zeros++; 309 if (zeros > maxzeros) 310 maxzeros = zeros; 311 } 312 } 313 } 314 315 if (maxones < 26 && maxzeros < 26) { 316 rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)", 317 maxones, maxzeros); 318 return (0); 319 } else { 320 rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)", 321 maxones, maxzeros); 322 rndstats.rst_longruns++; 323 return (-1); 324 } 325 } 326 327 /* 328 * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2, 329 * but it is really the chi^2 test over 4 bits (the poker test as described 330 * by Knuth vol 2 is something different, and I take him as authoritative 331 * on nomenclature over NIST). 332 */ 333 #define RNDTEST_CHI4_K 16 334 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1) 335 336 /* 337 * The unnormalized values are used so that we don't have to worry about 338 * fractional precision. The "real" value is found by: 339 * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value) 340 */ 341 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */ 342 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */ 343 344 static int 345 rndtest_chi_4(struct rndtest_state *rsp) 346 { 347 unsigned int freq[RNDTEST_CHI4_K], i, sum; 348 349 for (i = 0; i < RNDTEST_CHI4_K; i++) 350 freq[i] = 0; 351 352 /* Get number of occurrences of each 4 bit pattern */ 353 for (i = 0; i < RNDTEST_NBYTES; i++) { 354 freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++; 355 freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++; 356 } 357 358 for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++) 359 sum += freq[i] * freq[i]; 360 361 if (sum >= 1563181 && sum <= 1576929) { 362 rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum); 363 return (0); 364 } else { 365 rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum); 366 rndstats.rst_chi++; 367 return (-1); 368 } 369 } 370 371 static void 372 rndtest_timeout(void *xrsp) 373 { 374 struct rndtest_state *rsp = xrsp; 375 376 rsp->rs_collect = 1; 377 } 378 379 static int 380 rndtest_modevent(module_t mod, int type, void *unused) 381 { 382 switch (type) { 383 case MOD_LOAD: 384 return 0; 385 case MOD_UNLOAD: 386 return 0; 387 } 388 return EINVAL; 389 } 390 391 static moduledata_t rndtest_mod = { 392 "rndtest", 393 rndtest_modevent, 394 0 395 }; 396 DECLARE_MODULE(rndtest, rndtest_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 397 MODULE_VERSION(rndtest, 1); 398