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.

The second output is also saved and is then compared to the third block. This process<br />

continues for all generator outputs.<br />

The following (non-thread-safe) code adds a FIPS-compliant wrapper to the spc_<br />

entropy( ) function from Recipe 11.2 (note that this assumes that spc_entropy( )<br />

does not cryptographically postprocess its data, because otherwise the test is all but<br />

worthless).<br />

#include <br />

#include <br />

#define RNG_BLOCK_SZ 16<br />

char *spc_fips_entropy(char *outbuf, int n) {<br />

static int i, bufsz = -1;<br />

static char b1[RNG_BLOCK_SZ], b2[RNG_BLOCK_SZ];<br />

static char *last = b1, *next = b2;<br />

char *p = outbuf;<br />

if (bufsz = = -1) {<br />

spc_entropy(next, RNG_BLOCK_SZ);<br />

bufsz = 0;<br />

}<br />

while (bufsz && n--)<br />

*p++ = last[RNG_BLOCK_SZ - bufsz--];<br />

while (n >= RNG_BLOCK_SZ) {<br />

/* Old next becomes last here */<br />

*next ^= *last;<br />

*last ^= *next;<br />

*next ^= *last;<br />

spc_entropy(next, RNG_BLOCK_SZ);<br />

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

if (next[i] != last[i]) goto okay;<br />

abort( );<br />

okay:<br />

memcpy(p, next, RNG_BLOCK_SZ);<br />

p += RNG_BLOCK_SZ;<br />

n -= RNG_BLOCK_SZ;<br />

}<br />

if (n) {<br />

*next ^= *last;<br />

*last ^= *next;<br />

*next ^= *last;<br />

spc_entropy(next, RNG_BLOCK_SZ);<br />

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

if (next[i] != last[i])<br />

goto okay2;<br />

abort( );<br />

okay2:<br />

memcpy(p, next, n);<br />

bufsz = RNG_BLOCK_SZ - n;<br />

}<br />

return outbuf;<br />

}<br />

620 | 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!