2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the Licenseor (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be usefulbut
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <crypto/crypto_tester.h>
28 monobit_t monobit_all
= {
33 static bool test_monobit(monobit_t
*param
, chunk_t data
)
37 for (i
= 0; i
< data
.len
; i
++)
39 for (j
= 0; j
< 8; j
++)
41 if (data
.ptr
[i
] & (1<<j
))
47 DBG2(" Monobit: %d/%d bits set", bits
, data
.len
* 8);
48 if (bits
> param
->lower
&& bits
< param
->upper
)
55 rng_test_vector_t rng_monobit_1
= {
56 RNG_WEAK
, .len
= 2500,
57 .test
= (void*)test_monobit
,
61 rng_test_vector_t rng_monobit_2
= {
62 RNG_STRONG
, .len
= 2500,
63 .test
= (void*)test_monobit
,
67 rng_test_vector_t rng_monobit_3
= {
68 RNG_TRUE
, .len
= 2500,
69 .test
= (void*)test_monobit
,
86 static bool test_poker(poker_t
*param
, chunk_t data
)
91 memset(counter
, 0, sizeof(counter
));
93 for (i
= 0; i
< data
.len
; i
++)
95 counter
[data
.ptr
[i
] & 0x0F]++;
96 counter
[(data
.ptr
[i
] & 0xF0) >> 4]++;
99 for (i
= 0; i
< countof(counter
); i
++)
101 sum
+= (counter
[i
] * counter
[i
]) / 5000.0 * 16.0;
104 DBG2(" Poker: %f", sum
);
105 if (sum
> param
->lower
&& sum
< param
->upper
)
112 rng_test_vector_t rng_poker_1
= {
113 RNG_WEAK
, .len
= 2500,
114 .test
= (void*)test_poker
,
118 rng_test_vector_t rng_poker_2
= {
119 RNG_STRONG
, .len
= 2500,
120 .test
= (void*)test_poker
,
124 rng_test_vector_t rng_poker_3
= {
125 RNG_TRUE
, .len
= 2500,
126 .test
= (void*)test_poker
,
141 .lower
= {-1, 2267, 1079, 502, 223, 90, 90},
142 .upper
= {-1, 2733, 1421, 748, 402, 223, 223},
145 static bool test_runs(runs_t
*param
, chunk_t data
)
147 int i
, j
, zero_runs
[7], one_runs
[7], zero
= 0, one
= 0, longrun
= 0;
149 memset(one_runs
, 0, sizeof(zero_runs
));
150 memset(zero_runs
, 0, sizeof(one_runs
));
152 for (i
= 0; i
< data
.len
; i
++)
154 for (j
= 0; j
< 8; j
++)
156 if (data
.ptr
[i
] & (1<<j
))
160 if (++one
>= param
->longrun
)
168 zero_runs
[min(6, zero
)]++;
177 if (++zero
>= param
->longrun
)
185 one_runs
[min(6, one
)]++;
193 DBG2(" Runs: zero: %d/%d/%d/%d/%d/%d, one: %d/%d/%d/%d/%d/%d, "
195 zero_runs
[1], zero_runs
[2], zero_runs
[3],
196 zero_runs
[4], zero_runs
[5], zero_runs
[6],
197 one_runs
[1], one_runs
[2], one_runs
[3],
198 one_runs
[4], one_runs
[5], one_runs
[6],
206 for (i
= 1; i
< countof(zero_runs
); i
++)
208 if (zero_runs
[i
] <= param
->lower
[i
] ||
209 zero_runs
[i
] >= param
->upper
[i
] ||
210 one_runs
[i
] <= param
->lower
[i
] ||
211 one_runs
[i
] >= param
->upper
[i
])
219 rng_test_vector_t rng_runs_1
= {
220 RNG_WEAK
, .len
= 2500,
221 .test
= (void*)test_runs
,
225 rng_test_vector_t rng_runs_2
= {
226 RNG_STRONG
, .len
= 2500,
227 .test
= (void*)test_runs
,
231 rng_test_vector_t rng_runs_3
= {
232 RNG_TRUE
, .len
= 2500,
233 .test
= (void*)test_runs
,