rndtest.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* $OpenBSD$ */
  2. /*
  3. * OCF/Linux port done by David McCullough <david_mccullough@mcafee.com>
  4. * Copyright (C) 2006-2010 David McCullough
  5. * Copyright (C) 2004-2005 Intel Corporation.
  6. * The license and original author are listed below.
  7. *
  8. * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. All advertising materials mentioning features or use of this software
  20. * must display the following acknowledgement:
  21. * This product includes software developed by Jason L. Wright
  22. * 4. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  26. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/version.h>
  38. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
  39. #include <linux/config.h>
  40. #endif
  41. #include <linux/module.h>
  42. #include <linux/list.h>
  43. #include <linux/wait.h>
  44. #include <linux/time.h>
  45. #include <linux/unistd.h>
  46. #include <linux/kernel.h>
  47. #include <linux/string.h>
  48. #include <linux/time.h>
  49. #include <cryptodev.h>
  50. #include "rndtest.h"
  51. static struct rndtest_stats rndstats;
  52. static void rndtest_test(struct rndtest_state *);
  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. static int rndtest_runs_check(struct rndtest_state *, int, int *);
  59. static void rndtest_runs_record(struct rndtest_state *, int, int *);
  60. static const struct rndtest_testfunc {
  61. int (*test)(struct rndtest_state *);
  62. } rndtest_funcs[] = {
  63. { rndtest_monobit },
  64. { rndtest_runs },
  65. { rndtest_chi_4 },
  66. { rndtest_longruns },
  67. };
  68. #define RNDTEST_NTESTS (sizeof(rndtest_funcs)/sizeof(rndtest_funcs[0]))
  69. static void
  70. rndtest_test(struct rndtest_state *rsp)
  71. {
  72. int i, rv = 0;
  73. rndstats.rst_tests++;
  74. for (i = 0; i < RNDTEST_NTESTS; i++)
  75. rv |= (*rndtest_funcs[i].test)(rsp);
  76. rsp->rs_discard = (rv != 0);
  77. }
  78. extern int crypto_debug;
  79. #define rndtest_verbose 2
  80. #define rndtest_report(rsp, failure, fmt, a...) \
  81. { if (failure || crypto_debug) { printk("rng_test: " fmt "\n", a); } else; }
  82. #define RNDTEST_MONOBIT_MINONES 9725
  83. #define RNDTEST_MONOBIT_MAXONES 10275
  84. static int
  85. rndtest_monobit(struct rndtest_state *rsp)
  86. {
  87. int i, ones = 0, j;
  88. u_int8_t r;
  89. for (i = 0; i < RNDTEST_NBYTES; i++) {
  90. r = rsp->rs_buf[i];
  91. for (j = 0; j < 8; j++, r <<= 1)
  92. if (r & 0x80)
  93. ones++;
  94. }
  95. if (ones > RNDTEST_MONOBIT_MINONES &&
  96. ones < RNDTEST_MONOBIT_MAXONES) {
  97. if (rndtest_verbose > 1)
  98. rndtest_report(rsp, 0, "monobit pass (%d < %d < %d)",
  99. RNDTEST_MONOBIT_MINONES, ones,
  100. RNDTEST_MONOBIT_MAXONES);
  101. return (0);
  102. } else {
  103. if (rndtest_verbose)
  104. rndtest_report(rsp, 1,
  105. "monobit failed (%d ones)", ones);
  106. rndstats.rst_monobit++;
  107. return (-1);
  108. }
  109. }
  110. #define RNDTEST_RUNS_NINTERVAL 6
  111. static const struct rndtest_runs_tabs {
  112. u_int16_t min, max;
  113. } rndtest_runs_tab[] = {
  114. { 2343, 2657 },
  115. { 1135, 1365 },
  116. { 542, 708 },
  117. { 251, 373 },
  118. { 111, 201 },
  119. { 111, 201 },
  120. };
  121. static int
  122. rndtest_runs(struct rndtest_state *rsp)
  123. {
  124. int i, j, ones, zeros, rv = 0;
  125. int onei[RNDTEST_RUNS_NINTERVAL], zeroi[RNDTEST_RUNS_NINTERVAL];
  126. u_int8_t c;
  127. bzero(onei, sizeof(onei));
  128. bzero(zeroi, sizeof(zeroi));
  129. ones = zeros = 0;
  130. for (i = 0; i < RNDTEST_NBYTES; i++) {
  131. c = rsp->rs_buf[i];
  132. for (j = 0; j < 8; j++, c <<= 1) {
  133. if (c & 0x80) {
  134. ones++;
  135. rndtest_runs_record(rsp, zeros, zeroi);
  136. zeros = 0;
  137. } else {
  138. zeros++;
  139. rndtest_runs_record(rsp, ones, onei);
  140. ones = 0;
  141. }
  142. }
  143. }
  144. rndtest_runs_record(rsp, ones, onei);
  145. rndtest_runs_record(rsp, zeros, zeroi);
  146. rv |= rndtest_runs_check(rsp, 0, zeroi);
  147. rv |= rndtest_runs_check(rsp, 1, onei);
  148. if (rv)
  149. rndstats.rst_runs++;
  150. return (rv);
  151. }
  152. static void
  153. rndtest_runs_record(struct rndtest_state *rsp, int len, int *intrv)
  154. {
  155. if (len == 0)
  156. return;
  157. if (len > RNDTEST_RUNS_NINTERVAL)
  158. len = RNDTEST_RUNS_NINTERVAL;
  159. len -= 1;
  160. intrv[len]++;
  161. }
  162. static int
  163. rndtest_runs_check(struct rndtest_state *rsp, int val, int *src)
  164. {
  165. int i, rv = 0;
  166. for (i = 0; i < RNDTEST_RUNS_NINTERVAL; i++) {
  167. if (src[i] < rndtest_runs_tab[i].min ||
  168. src[i] > rndtest_runs_tab[i].max) {
  169. rndtest_report(rsp, 1,
  170. "%s interval %d failed (%d, %d-%d)",
  171. val ? "ones" : "zeros",
  172. i + 1, src[i], rndtest_runs_tab[i].min,
  173. rndtest_runs_tab[i].max);
  174. rv = -1;
  175. } else {
  176. rndtest_report(rsp, 0,
  177. "runs pass %s interval %d (%d < %d < %d)",
  178. val ? "ones" : "zeros",
  179. i + 1, rndtest_runs_tab[i].min, src[i],
  180. rndtest_runs_tab[i].max);
  181. }
  182. }
  183. return (rv);
  184. }
  185. static int
  186. rndtest_longruns(struct rndtest_state *rsp)
  187. {
  188. int i, j, ones = 0, zeros = 0, maxones = 0, maxzeros = 0;
  189. u_int8_t c;
  190. for (i = 0; i < RNDTEST_NBYTES; i++) {
  191. c = rsp->rs_buf[i];
  192. for (j = 0; j < 8; j++, c <<= 1) {
  193. if (c & 0x80) {
  194. zeros = 0;
  195. ones++;
  196. if (ones > maxones)
  197. maxones = ones;
  198. } else {
  199. ones = 0;
  200. zeros++;
  201. if (zeros > maxzeros)
  202. maxzeros = zeros;
  203. }
  204. }
  205. }
  206. if (maxones < 26 && maxzeros < 26) {
  207. rndtest_report(rsp, 0, "longruns pass (%d ones, %d zeros)",
  208. maxones, maxzeros);
  209. return (0);
  210. } else {
  211. rndtest_report(rsp, 1, "longruns fail (%d ones, %d zeros)",
  212. maxones, maxzeros);
  213. rndstats.rst_longruns++;
  214. return (-1);
  215. }
  216. }
  217. /*
  218. * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
  219. * but it is really the chi^2 test over 4 bits (the poker test as described
  220. * by Knuth vol 2 is something different, and I take him as authoritative
  221. * on nomenclature over NIST).
  222. */
  223. #define RNDTEST_CHI4_K 16
  224. #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
  225. /*
  226. * The unnormalized values are used so that we don't have to worry about
  227. * fractional precision. The "real" value is found by:
  228. * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
  229. */
  230. #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
  231. #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
  232. static int
  233. rndtest_chi_4(struct rndtest_state *rsp)
  234. {
  235. unsigned int freq[RNDTEST_CHI4_K], i, sum;
  236. for (i = 0; i < RNDTEST_CHI4_K; i++)
  237. freq[i] = 0;
  238. /* Get number of occurances of each 4 bit pattern */
  239. for (i = 0; i < RNDTEST_NBYTES; i++) {
  240. freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
  241. freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;
  242. }
  243. for (i = 0, sum = 0; i < RNDTEST_CHI4_K; i++)
  244. sum += freq[i] * freq[i];
  245. if (sum >= 1563181 && sum <= 1576929) {
  246. rndtest_report(rsp, 0, "chi^2(4): pass (sum %u)", sum);
  247. return (0);
  248. } else {
  249. rndtest_report(rsp, 1, "chi^2(4): failed (sum %u)", sum);
  250. rndstats.rst_chi++;
  251. return (-1);
  252. }
  253. }
  254. int
  255. rndtest_buf(unsigned char *buf)
  256. {
  257. struct rndtest_state rsp;
  258. memset(&rsp, 0, sizeof(rsp));
  259. rsp.rs_buf = buf;
  260. rndtest_test(&rsp);
  261. return(rsp.rs_discard);
  262. }