21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

#define FIPS_POKER_HIBOUND 57.4<br />

int spc_fips_poker(unsigned char data[FIPS_NUMBYTES]) {<br />

int i;<br />

long counts[16] = {0,}, sum = 0;<br />

double result;<br />

for (i = 0; i < FIPS_NUMBYTES; i++) {<br />

counts[data[i] & 0xf]++;<br />

counts[data[i] >> 4]++;<br />

}<br />

for (i = 0; i < 16; i++)<br />

sum += (counts[i] * counts[i]);<br />

result = (16.0 / 5000) * (double)sum - 5000.0;<br />

return (result > FIPS_POKER_LOBOUND && result < FIPS_POKER_HIBOUND);<br />

}<br />

The third and fourth FIPS 140-1 statistical tests are implemented as follows to run in<br />

parallel in a single routine. The third test, the “Runs” test, goes through the data<br />

stream and finds all the “runs” of consecutive bits that are identical. The test then<br />

counts the maximum length of each run. That is, if there are three consecutive zeros<br />

starting at the first position, that’s one run of length three, but it doesn’t count as<br />

any runs of length two or any runs of length one. Runs that are longer than six bits<br />

are counted as a six-bit run. At the end, for each length of run, the count for consecutive<br />

zeros of that run length and the count for consecutive ones are examined. If<br />

either fails to fall within a specified range, the test fails. If all of the results are in an<br />

appropriate range for the run length in question, the test passes.<br />

The fourth test, the “Long Runs” test, also calculates runs of bits. The test looks for<br />

runs of 34 bits or longer. Any such runs cause the test to fail; otherwise, it succeeds.<br />

#define FIPS_NUMBYTES 2500<br />

#define FIPS_LONGRUN 34<br />

#define FIPS_RUNS_1_LO 2267<br />

#define FIPS_RUNS_1_HI 2733<br />

#define FIPS_RUNS_2_LO 1079<br />

#define FIPS_RUNS_2_HI 1421<br />

#define FIPS_RUNS_3_LO 502<br />

#define FIPS_RUNS_3_HI 748<br />

#define FIPS_RUNS_4_LO 223<br />

#define FIPS_RUNS_4_HI 402<br />

#define FIPS_RUNS_5_LO 90<br />

#define FIPS_RUNS_5_HI 223<br />

#define FIPS_RUNS_6_LO 90<br />

#define FIPS_RUNS_6_HI 223<br />

/* Perform both the "Runs" test and the "Long Run" test */<br />

int spc_fips_runs(unsigned char data[FIPS_NUMBYTES]) {<br />

/* We allow a zero-length run size, mainly just to keep the array indexing less<br />

* confusing. It also allows us to set cur_val arbitrarily below (if the first<br />

* bit of the stream is a 1, then runs[0] will be 1; otherwise, it will be 0).<br />

*/<br />

int runs[2][7] = {{0,},{0,}};<br />

618 | Chapter 11: Random Numbers<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!