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.

1. Figure out how big a seed you need. At the very least, you need a seed that is as<br />

many bits in length as bits of entropy you think are in the generator. Generally,<br />

this will be at least as large as the key size of the underlying primitive (or the output<br />

size when using a one-way hash function instead of a cipher).<br />

2. If you need to introduce new entropy, properly compress the data containing<br />

entropy. In particular, you must transform the data into a seed of the proper<br />

size, with minimal loss of entropy. One easy way to do that is to process the<br />

string with a cryptographic hash function (truncating the hash output to the<br />

desired length, if necessary). Then XOR the compressed entropy with the seed<br />

output by the generator.<br />

3. Take the value and use it to reseed the generator. If you are using a counterbased<br />

generator, you can either reset the counter or choose not to do so. In fact,<br />

it is preferable to take a bit of extra output from the generator so that the<br />

counter can be set to a random value.<br />

For example, using the block cipher–based PRNG from Recipe 11.5, here is a function<br />

that reseeds the generator, given new, uncompressed data containing entropy:<br />

void spc_bcprng_reseed(SPC_BCPRNG_CTX *prng, unsigned char *new_data, size_t l) {<br />

size_t i;<br />

unsigned char m[SPC_MAX_KEYLEN + SPC_BLOCK_SZ];<br />

SPC_BCPRNG_LOCK( );<br />

if (prng->kl > SPC_MAX_KEYLEN) prng->kl = SPC_MAX_KEYLEN;<br />

spc_bcprng_rand(prng, m, prng->kl + SPC_BLOCK_SZ);<br />

while (l > prng->kl) {<br />

for (i = 0; i < prng->kl; i++) m[i] ^= *new_data++;<br />

l -= prng->kl;<br />

spc_bcprng_init(prng, m, prng->kl, m + prng->kl, SPC_BLOCK_SZ);<br />

spc_bcprng_rand(prng, m, prng->kl + SPC_BLOCK_SZ);<br />

}<br />

for (i = 0; i kl, m + prng->kl, SPC_BLOCK_SZ);<br />

SPC_BCPRNG_UNLOCK( );<br />

}<br />

To handle compression of the data that contains entropy, we avoid using a hash<br />

function. Instead, we break the data up into chunks no larger than the required seed<br />

size, and reseed multiple times until we have run out of data. This is an entropy-preserving<br />

way of processing the data that does not require the use of a cryptographic<br />

hash function.<br />

See Also<br />

Recipes 11.5, 11.16<br />

Reseeding a Pseudo-Random Number Generator | 593<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!