4 * OCF/Linux port done by David McCullough <david_mccullough@securecomputing.com>
5 * Copyright (C) 2006-2007 David McCullough
6 * Copyright (C) 2004-2005 Intel Corporation.
7 * The license and original author are listed below.
9 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
39 #ifndef AUTOCONF_INCLUDED
40 #include <linux/config.h>
42 #include <linux/module.h>
43 #include <linux/list.h>
44 #include <linux/wait.h>
45 #include <linux/time.h>
46 #include <linux/version.h>
47 #include <linux/unistd.h>
48 #include <linux/kernel.h>
49 #include <linux/string.h>
50 #include <linux/time.h>
51 #include <cryptodev.h>
54 static struct rndtest_stats rndstats
;
56 static void rndtest_test(struct rndtest_state
*);
58 /* The tests themselves */
59 static int rndtest_monobit(struct rndtest_state
*);
60 static int rndtest_runs(struct rndtest_state
*);
61 static int rndtest_longruns(struct rndtest_state
*);
62 static int rndtest_chi_4(struct rndtest_state
*);
64 static int rndtest_runs_check(struct rndtest_state
*, int, int *);
65 static void rndtest_runs_record(struct rndtest_state
*, int, int *);
67 static const struct rndtest_testfunc
{
68 int (*test
)(struct rndtest_state
*);
76 #define RNDTEST_NTESTS (sizeof(rndtest_funcs)/sizeof(rndtest_funcs[0]))
79 rndtest_test(struct rndtest_state
*rsp
)
84 for (i
= 0; i
< RNDTEST_NTESTS
; i
++)
85 rv
|= (*rndtest_funcs
[i
].test
)(rsp
);
86 rsp
->rs_discard
= (rv
!= 0);
90 extern int crypto_debug
;
91 #define rndtest_verbose 2
92 #define rndtest_report(rsp, failure, fmt, a...) \
93 { if (failure || crypto_debug) { printk("rng_test: " fmt "\n", a); } else; }
95 #define RNDTEST_MONOBIT_MINONES 9725
96 #define RNDTEST_MONOBIT_MAXONES 10275
99 rndtest_monobit(struct rndtest_state
*rsp
)
104 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
106 for (j
= 0; j
< 8; j
++, r
<<= 1)
110 if (ones
> RNDTEST_MONOBIT_MINONES
&&
111 ones
< RNDTEST_MONOBIT_MAXONES
) {
112 if (rndtest_verbose
> 1)
113 rndtest_report(rsp
, 0, "monobit pass (%d < %d < %d)",
114 RNDTEST_MONOBIT_MINONES
, ones
,
115 RNDTEST_MONOBIT_MAXONES
);
119 rndtest_report(rsp
, 1,
120 "monobit failed (%d ones)", ones
);
121 rndstats
.rst_monobit
++;
126 #define RNDTEST_RUNS_NINTERVAL 6
128 static const struct rndtest_runs_tabs
{
130 } rndtest_runs_tab
[] = {
140 rndtest_runs(struct rndtest_state
*rsp
)
142 int i
, j
, ones
, zeros
, rv
= 0;
143 int onei
[RNDTEST_RUNS_NINTERVAL
], zeroi
[RNDTEST_RUNS_NINTERVAL
];
146 bzero(onei
, sizeof(onei
));
147 bzero(zeroi
, sizeof(zeroi
));
149 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
151 for (j
= 0; j
< 8; j
++, c
<<= 1) {
154 rndtest_runs_record(rsp
, zeros
, zeroi
);
158 rndtest_runs_record(rsp
, ones
, onei
);
163 rndtest_runs_record(rsp
, ones
, onei
);
164 rndtest_runs_record(rsp
, zeros
, zeroi
);
166 rv
|= rndtest_runs_check(rsp
, 0, zeroi
);
167 rv
|= rndtest_runs_check(rsp
, 1, onei
);
176 rndtest_runs_record(struct rndtest_state
*rsp
, int len
, int *intrv
)
180 if (len
> RNDTEST_RUNS_NINTERVAL
)
181 len
= RNDTEST_RUNS_NINTERVAL
;
187 rndtest_runs_check(struct rndtest_state
*rsp
, int val
, int *src
)
191 for (i
= 0; i
< RNDTEST_RUNS_NINTERVAL
; i
++) {
192 if (src
[i
] < rndtest_runs_tab
[i
].min
||
193 src
[i
] > rndtest_runs_tab
[i
].max
) {
194 rndtest_report(rsp
, 1,
195 "%s interval %d failed (%d, %d-%d)",
196 val
? "ones" : "zeros",
197 i
+ 1, src
[i
], rndtest_runs_tab
[i
].min
,
198 rndtest_runs_tab
[i
].max
);
201 rndtest_report(rsp
, 0,
202 "runs pass %s interval %d (%d < %d < %d)",
203 val
? "ones" : "zeros",
204 i
+ 1, rndtest_runs_tab
[i
].min
, src
[i
],
205 rndtest_runs_tab
[i
].max
);
212 rndtest_longruns(struct rndtest_state
*rsp
)
214 int i
, j
, ones
= 0, zeros
= 0, maxones
= 0, maxzeros
= 0;
217 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
219 for (j
= 0; j
< 8; j
++, c
<<= 1) {
228 if (zeros
> maxzeros
)
234 if (maxones
< 26 && maxzeros
< 26) {
235 rndtest_report(rsp
, 0, "longruns pass (%d ones, %d zeros)",
239 rndtest_report(rsp
, 1, "longruns fail (%d ones, %d zeros)",
241 rndstats
.rst_longruns
++;
247 * chi^2 test over 4 bits: (this is called the poker test in FIPS 140-2,
248 * but it is really the chi^2 test over 4 bits (the poker test as described
249 * by Knuth vol 2 is something different, and I take him as authoritative
250 * on nomenclature over NIST).
252 #define RNDTEST_CHI4_K 16
253 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
256 * The unnormalized values are used so that we don't have to worry about
257 * fractional precision. The "real" value is found by:
258 * (V - 1562500) * (16 / 5000) = Vn (where V is the unnormalized value)
260 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
261 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
264 rndtest_chi_4(struct rndtest_state
*rsp
)
266 unsigned int freq
[RNDTEST_CHI4_K
], i
, sum
;
268 for (i
= 0; i
< RNDTEST_CHI4_K
; i
++)
271 /* Get number of occurances of each 4 bit pattern */
272 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
273 freq
[(rsp
->rs_buf
[i
] >> 4) & RNDTEST_CHI4_K_MASK
]++;
274 freq
[(rsp
->rs_buf
[i
] >> 0) & RNDTEST_CHI4_K_MASK
]++;
277 for (i
= 0, sum
= 0; i
< RNDTEST_CHI4_K
; i
++)
278 sum
+= freq
[i
] * freq
[i
];
280 if (sum
>= 1563181 && sum
<= 1576929) {
281 rndtest_report(rsp
, 0, "chi^2(4): pass (sum %u)", sum
);
284 rndtest_report(rsp
, 1, "chi^2(4): failed (sum %u)", sum
);
291 rndtest_buf(unsigned char *buf
)
293 struct rndtest_state rsp
;
295 memset(&rsp
, 0, sizeof(rsp
));
298 return(rsp
.rs_discard
);
This page took 0.060043 seconds and 5 git commands to generate.