2 * Copyright (C) 2009 Martin Willi
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the Licenseor (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be usefulbut
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include <crypto/crypto_tester.h>
19 #include <utils/debug.h>
29 monobit_t monobit_all
= {
34 static bool test_monobit(monobit_t
*param
, chunk_t data
)
38 for (i
= 0; i
< data
.len
; i
++)
40 for (j
= 0; j
< 8; j
++)
42 if (data
.ptr
[i
] & (1<<j
))
48 DBG2(DBG_LIB
, " Monobit: %d/%d bits set", bits
, data
.len
* 8);
49 if (bits
> param
->lower
&& bits
< param
->upper
)
56 rng_test_vector_t rng_monobit_1
= {
57 RNG_WEAK
, .len
= 2500,
58 .test
= (void*)test_monobit
,
62 rng_test_vector_t rng_monobit_2
= {
63 RNG_STRONG
, .len
= 2500,
64 .test
= (void*)test_monobit
,
68 rng_test_vector_t rng_monobit_3
= {
69 RNG_TRUE
, .len
= 2500,
70 .test
= (void*)test_monobit
,
87 static bool test_poker(poker_t
*param
, chunk_t data
)
92 memset(counter
, 0, sizeof(counter
));
94 for (i
= 0; i
< data
.len
; i
++)
96 counter
[data
.ptr
[i
] & 0x0F]++;
97 counter
[(data
.ptr
[i
] & 0xF0) >> 4]++;
100 for (i
= 0; i
< countof(counter
); i
++)
102 sum
+= (counter
[i
] * counter
[i
]) / 5000.0 * 16.0;
105 DBG2(DBG_LIB
, " Poker: %f", sum
);
106 if (sum
> param
->lower
&& sum
< param
->upper
)
113 rng_test_vector_t rng_poker_1
= {
114 RNG_WEAK
, .len
= 2500,
115 .test
= (void*)test_poker
,
119 rng_test_vector_t rng_poker_2
= {
120 RNG_STRONG
, .len
= 2500,
121 .test
= (void*)test_poker
,
125 rng_test_vector_t rng_poker_3
= {
126 RNG_TRUE
, .len
= 2500,
127 .test
= (void*)test_poker
,
142 .lower
= {-1, 2267, 1079, 502, 223, 90, 90},
143 .upper
= {-1, 2733, 1421, 748, 402, 223, 223},
146 static bool test_runs(runs_t
*param
, chunk_t data
)
148 int i
, j
, zero_runs
[7], one_runs
[7], zero
= 0, one
= 0, longrun
= 0;
150 memset(one_runs
, 0, sizeof(zero_runs
));
151 memset(zero_runs
, 0, sizeof(one_runs
));
153 for (i
= 0; i
< data
.len
; i
++)
155 for (j
= 0; j
< 8; j
++)
157 if (data
.ptr
[i
] & (1<<j
))
161 if (++one
>= param
->longrun
)
169 zero_runs
[min(6, zero
)]++;
178 if (++zero
>= param
->longrun
)
186 one_runs
[min(6, one
)]++;
194 DBG2(DBG_LIB
, " Runs: zero: %d/%d/%d/%d/%d/%d, one: %d/%d/%d/%d/%d/%d, "
196 zero_runs
[1], zero_runs
[2], zero_runs
[3],
197 zero_runs
[4], zero_runs
[5], zero_runs
[6],
198 one_runs
[1], one_runs
[2], one_runs
[3],
199 one_runs
[4], one_runs
[5], one_runs
[6],
207 for (i
= 1; i
< countof(zero_runs
); i
++)
209 if (zero_runs
[i
] <= param
->lower
[i
] ||
210 zero_runs
[i
] >= param
->upper
[i
] ||
211 one_runs
[i
] <= param
->lower
[i
] ||
212 one_runs
[i
] >= param
->upper
[i
])
220 rng_test_vector_t rng_runs_1
= {
221 RNG_WEAK
, .len
= 2500,
222 .test
= (void*)test_runs
,
226 rng_test_vector_t rng_runs_2
= {
227 RNG_STRONG
, .len
= 2500,
228 .test
= (void*)test_runs
,
232 rng_test_vector_t rng_runs_3
= {
233 RNG_TRUE
, .len
= 2500,
234 .test
= (void*)test_runs
,