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