21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

out<br />

Buffer into which the random data will be placed.<br />

l<br />

Number of random bytes to be placed into the output buffer.<br />

If your hash function produces n-bit outputs and has no practical weaknesses, do not<br />

use the generator after you run the MAC more than 2n/2 times. For example, with<br />

SHA1, this generator should be not be a problem for at least 280 × 20 bytes. In practice,<br />

you probably will not have to worry about this issue.<br />

To bind this cryptographic pseudo-random number generator to the API in Recipe<br />

11.2, you can use a single global generator context that you seed in spc_rand_<br />

init( ), requiring you to get a secure seed. Once that is done (assuming the generator<br />

variable is a statically allocated global variable named spc_prng), you can simply<br />

implement spc_rand( ) as follows:<br />

unsigned char *spc_rand(unsigned char *buf, size_t l) {<br />

return spc_bcprng_rand(&spc_prng, buf, l);<br />

}<br />

Note that, although we don’t show it in the previous code, you should ensure that<br />

the generator is initialized before giving output.<br />

See Also<br />

Recipes 5.2, 5.5, 5.9, 5.23, 6.10, 11.2, 11.6, 11.8, 11.16<br />

11.6 Reseeding a Pseudo-Random Number<br />

Generator<br />

<strong>Problem</strong><br />

You have an application-level pseudo-random number generator such as the ones<br />

presented in Recipe 11.5, and you want to reseed it, either because you have new<br />

entropy to mix in or because you would like to prevent against backtracking attacks.<br />

Solution<br />

Create a new seed by getting a sufficient number of bytes from the generator to seed<br />

the generator. If mixing in entropy, compress the entropy down to the seed size if<br />

necessary, as discussed in Recipe 11.16, then XOR the compressed seed with the<br />

generator output. Finally, reseed the generator with the resulting value.<br />

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