4
* statistical tests for randomness (FIPS 140-2, Section 4.9)
12
debug_module_t mod_stat = {
13
0, /* debugging is off by default */
14
(char *)"stat test" /* printable module name */
18
* each test assumes that 20,000 bits (2500 octets) of data is
22
#define STAT_TEST_DATA_LEN 2500
25
stat_test_monobit(uint8_t *data) {
26
uint8_t *data_end = data + STAT_TEST_DATA_LEN;
30
while (data < data_end) {
31
ones_count += octet_get_weight(*data);
35
debug_print(mod_stat, "bit count: %d", ones_count);
37
if ((ones_count < 9725) || (ones_count > 10275))
38
return err_status_algo_fail;
44
stat_test_poker(uint8_t *data) {
46
uint8_t *data_end = data + STAT_TEST_DATA_LEN;
49
0, 0, 0, 0, 0, 0, 0, 0,
50
0, 0, 0, 0, 0, 0, 0, 0
53
while (data < data_end) {
54
f[*data & 0x0f]++; /* increment freq. count for low nibble */
55
f[(*data) >> 4]++; /* increment freq. count for high nibble */
60
for (i=0; i < 16; i++)
61
poker += (double) f[i] * f[i];
63
poker *= (16.0 / 5000.0);
66
debug_print(mod_stat, "poker test: %f\n", poker);
68
if ((poker < 2.16) || (poker > 46.17))
69
return err_status_algo_fail;
76
* runs[i] holds the number of runs of size (i-1)
80
stat_test_runs(uint8_t *data) {
81
uint8_t *data_end = data + STAT_TEST_DATA_LEN;
82
uint16_t runs[6] = { 0, 0, 0, 0, 0, 0 };
83
uint16_t gaps[6] = { 0, 0, 0, 0, 0, 0 };
84
uint16_t lo_value[6] = { 2315, 1114, 527, 240, 103, 103 };
85
uint16_t hi_value[6] = { 2685, 1386, 723, 384, 209, 209 };
91
* the state variable holds the number of bits in the
92
* current run (or gap, if negative)
95
while (data < data_end) {
97
/* loop over the bits of this byte */
98
for (mask = 1; mask < 256; mask <<= 1) {
101
/* next bit is a one */
104
/* prefix is a run, so increment the run-count */
107
/* check for long runs */
109
debug_print(mod_stat, ">25 runs: %d", state);
110
return err_status_algo_fail;
113
} else if (state < 0) {
115
/* prefix is a gap */
117
debug_print(mod_stat, ">25 gaps: %d", state);
118
return err_status_algo_fail; /* long-runs test failed */
121
state = -6; /* group together gaps > 5 */
123
gaps[-1-state]++; /* increment gap count */
124
state = 1; /* set state at one set bit */
127
/* state is zero; this happens only at initialization */
132
/* next bit is a zero */
135
/* prefix is a run */
137
debug_print(mod_stat, ">25 runs (2): %d", state);
138
return err_status_algo_fail; /* long-runs test failed */
141
state = 6; /* group together runs > 5 */
143
runs[state-1]++; /* increment run count */
144
state = -1; /* set state at one zero bit */
145
} else if (state < 0) {
147
/* prefix is a gap, so increment gap-count (decrement state) */
150
/* check for long gaps */
152
debug_print(mod_stat, ">25 gaps (2): %d", state);
153
return err_status_algo_fail;
158
/* state is zero; this happens only at initialization */
164
/* move along to next octet */
169
debug_print(mod_stat, "runs test", NULL);
170
for (i=0; i < 6; i++)
171
debug_print(mod_stat, " runs[]: %d", runs[i]);
172
for (i=0; i < 6; i++)
173
debug_print(mod_stat, " gaps[]: %d", gaps[i]);
176
/* check run and gap counts against the fixed limits */
177
for (i=0; i < 6; i++)
178
if ( (runs[i] < lo_value[i] ) || (runs[i] > hi_value[i])
179
|| (gaps[i] < lo_value[i] ) || (gaps[i] > hi_value[i]))
180
return err_status_algo_fail;
183
return err_status_ok;
188
* the function stat_test_rand_source applys the FIPS-140-2 statistical
189
* tests to the random source defined by rs
193
#define RAND_SRC_BUF_OCTETS 50 /* this value MUST divide 2500! */
196
stat_test_rand_source(rand_source_func_t get_rand_bytes) {
199
uint8_t *data, *data_end;
201
0, 0, 0, 0, 0, 0, 0, 0,
202
0, 0, 0, 0, 0, 0, 0, 0
204
uint8_t buffer[RAND_SRC_BUF_OCTETS];
207
uint16_t runs[6] = { 0, 0, 0, 0, 0, 0 };
208
uint16_t gaps[6] = { 0, 0, 0, 0, 0, 0 };
209
uint16_t lo_value[6] = { 2315, 1114, 527, 240, 103, 103 };
210
uint16_t hi_value[6] = { 2685, 1386, 723, 384, 209, 209 };
214
/* counters for monobit, poker, and runs tests are initialized above */
216
/* main loop: fill buffer, update counters for stat tests */
217
for (i=0; i < 2500; i+=RAND_SRC_BUF_OCTETS) {
219
/* fill data buffer */
220
status = get_rand_bytes(buffer, RAND_SRC_BUF_OCTETS);
222
debug_print(mod_stat, "couldn't get rand bytes: %d",status);
227
debug_print(mod_stat, "%s",
228
octet_string_hex_string(buffer, RAND_SRC_BUF_OCTETS));
232
data_end = data + RAND_SRC_BUF_OCTETS;
233
while (data < data_end) {
235
/* update monobit test counter */
236
ones_count += octet_get_weight(*data);
238
/* update poker test counters */
239
f[*data & 0x0f]++; /* increment freq. count for low nibble */
240
f[(*data) >> 4]++; /* increment freq. count for high nibble */
242
/* update runs test counters */
243
/* loop over the bits of this byte */
244
for (mask = 1; mask < 256; mask <<= 1) {
247
/* next bit is a one */
250
/* prefix is a run, so increment the run-count */
253
/* check for long runs */
255
debug_print(mod_stat, ">25 runs (3): %d", state);
256
return err_status_algo_fail;
259
} else if (state < 0) {
261
/* prefix is a gap */
263
debug_print(mod_stat, ">25 gaps (3): %d", state);
264
return err_status_algo_fail; /* long-runs test failed */
267
state = -6; /* group together gaps > 5 */
269
gaps[-1-state]++; /* increment gap count */
270
state = 1; /* set state at one set bit */
273
/* state is zero; this happens only at initialization */
278
/* next bit is a zero */
281
/* prefix is a run */
283
debug_print(mod_stat, ">25 runs (4): %d", state);
284
return err_status_algo_fail; /* long-runs test failed */
287
state = 6; /* group together runs > 5 */
289
runs[state-1]++; /* increment run count */
290
state = -1; /* set state at one zero bit */
291
} else if (state < 0) {
293
/* prefix is a gap, so increment gap-count (decrement state) */
296
/* check for long gaps */
298
debug_print(mod_stat, ">25 gaps (4): %d", state);
299
return err_status_algo_fail;
304
/* state is zero; this happens only at initialization */
310
/* advance data pointer */
315
/* check to see if test data is within bounds */
317
/* check monobit test data */
319
debug_print(mod_stat, "stat: bit count: %d", ones_count);
321
if ((ones_count < 9725) || (ones_count > 10275)) {
322
debug_print(mod_stat, "stat: failed monobit test %d", ones_count);
323
return err_status_algo_fail;
326
/* check poker test data */
328
for (i=0; i < 16; i++)
329
poker += (double) f[i] * f[i];
331
poker *= (16.0 / 5000.0);
334
debug_print(mod_stat, "stat: poker test: %f", poker);
336
if ((poker < 2.16) || (poker > 46.17)) {
337
debug_print(mod_stat, "stat: failed poker test", NULL);
338
return err_status_algo_fail;
341
/* check run and gap counts against the fixed limits */
342
for (i=0; i < 6; i++)
343
if ((runs[i] < lo_value[i] ) || (runs[i] > hi_value[i])
344
|| (gaps[i] < lo_value[i] ) || (gaps[i] > hi_value[i])) {
345
debug_print(mod_stat, "stat: failed run/gap test", NULL);
346
return err_status_algo_fail;
349
debug_print(mod_stat, "passed random stat test", NULL);
350
return err_status_ok;
354
stat_test_rand_source_with_repetition(rand_source_func_t source, unsigned num_trials) {
356
err_status_t err = err_status_algo_fail;
358
for (i=0; i < num_trials; i++) {
359
err = stat_test_rand_source(source);
360
if (err == err_status_ok) {
361
return err_status_ok;
363
debug_print(mod_stat, "failed stat test (try number %d)\n", i);