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.
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 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33))
41 #include <generated/autoconf.h>
43 #include <linux/autoconf.h>
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>
57 static struct rndtest_stats rndstats
;
59 static void rndtest_test(struct rndtest_state
*);
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
*);
67 static int rndtest_runs_check(struct rndtest_state
*, int, int *);
68 static void rndtest_runs_record(struct rndtest_state
*, int, int *);
70 static const struct rndtest_testfunc
{
71 int (*test
)(struct rndtest_state
*);
79 #define RNDTEST_NTESTS (sizeof(rndtest_funcs)/sizeof(rndtest_funcs[0]))
82 rndtest_test(struct rndtest_state
*rsp
)
87 for (i
= 0; i
< RNDTEST_NTESTS
; i
++)
88 rv
|= (*rndtest_funcs
[i
].test
)(rsp
);
89 rsp
->rs_discard
= (rv
!= 0);
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; }
98 #define RNDTEST_MONOBIT_MINONES 9725
99 #define RNDTEST_MONOBIT_MAXONES 10275
102 rndtest_monobit(struct rndtest_state
*rsp
)
107 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
109 for (j
= 0; j
< 8; j
++, r
<<= 1)
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
);
122 rndtest_report(rsp
, 1,
123 "monobit failed (%d ones)", ones
);
124 rndstats
.rst_monobit
++;
129 #define RNDTEST_RUNS_NINTERVAL 6
131 static const struct rndtest_runs_tabs
{
133 } rndtest_runs_tab
[] = {
143 rndtest_runs(struct rndtest_state
*rsp
)
145 int i
, j
, ones
, zeros
, rv
= 0;
146 int onei
[RNDTEST_RUNS_NINTERVAL
], zeroi
[RNDTEST_RUNS_NINTERVAL
];
149 bzero(onei
, sizeof(onei
));
150 bzero(zeroi
, sizeof(zeroi
));
152 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
154 for (j
= 0; j
< 8; j
++, c
<<= 1) {
157 rndtest_runs_record(rsp
, zeros
, zeroi
);
161 rndtest_runs_record(rsp
, ones
, onei
);
166 rndtest_runs_record(rsp
, ones
, onei
);
167 rndtest_runs_record(rsp
, zeros
, zeroi
);
169 rv
|= rndtest_runs_check(rsp
, 0, zeroi
);
170 rv
|= rndtest_runs_check(rsp
, 1, onei
);
179 rndtest_runs_record(struct rndtest_state
*rsp
, int len
, int *intrv
)
183 if (len
> RNDTEST_RUNS_NINTERVAL
)
184 len
= RNDTEST_RUNS_NINTERVAL
;
190 rndtest_runs_check(struct rndtest_state
*rsp
, int val
, int *src
)
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
);
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
);
215 rndtest_longruns(struct rndtest_state
*rsp
)
217 int i
, j
, ones
= 0, zeros
= 0, maxones
= 0, maxzeros
= 0;
220 for (i
= 0; i
< RNDTEST_NBYTES
; i
++) {
222 for (j
= 0; j
< 8; j
++, c
<<= 1) {
231 if (zeros
> maxzeros
)
237 if (maxones
< 26 && maxzeros
< 26) {
238 rndtest_report(rsp
, 0, "longruns pass (%d ones, %d zeros)",
242 rndtest_report(rsp
, 1, "longruns fail (%d ones, %d zeros)",
244 rndstats
.rst_longruns
++;
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).
255 #define RNDTEST_CHI4_K 16
256 #define RNDTEST_CHI4_K_MASK (RNDTEST_CHI4_K - 1)
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)
263 #define RNDTEST_CHI4_VMIN 1563181 /* 2.1792 */
264 #define RNDTEST_CHI4_VMAX 1576929 /* 46.1728 */
267 rndtest_chi_4(struct rndtest_state
*rsp
)
269 unsigned int freq
[RNDTEST_CHI4_K
], i
, sum
;
271 for (i
= 0; i
< RNDTEST_CHI4_K
; i
++)
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
]++;
280 for (i
= 0, sum
= 0; i
< RNDTEST_CHI4_K
; i
++)
281 sum
+= freq
[i
] * freq
[i
];
283 if (sum
>= 1563181 && sum
<= 1576929) {
284 rndtest_report(rsp
, 0, "chi^2(4): pass (sum %u)", sum
);
287 rndtest_report(rsp
, 1, "chi^2(4): failed (sum %u)", sum
);
294 rndtest_buf(unsigned char *buf
)
296 struct rndtest_state rsp
;
298 memset(&rsp
, 0, sizeof(rsp
));
301 return(rsp
.rs_discard
);
This page took 0.054148 seconds and 5 git commands to generate.