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.

If the generator is not seeded with enough entropy, this function could produce output<br />

that may be insecure. In such a case, the function will return 0. Make sure that<br />

you always check for this condition!<br />

Do not, under any circumstances, use the API function, RAND_pseudo_<br />

bytes( ). It is not a cryptographically strong PRNG and therefore is<br />

not worth using for anything that has even a remote possibility of<br />

being security-relevant.<br />

You can implement spc_rand( ), the cryptographic pseudo-randomness function<br />

from Recipe 11.2, by simply calling RAND_bytes( ) and aborting if that function<br />

returns 0.<br />

#include <br />

#include <br />

#include <br />

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

if (!RAND_bytes(buf, l)) {<br />

fprintf(stderr, "The PRNG is not seeded!\n");<br />

abort( );<br />

}<br />

return buf;<br />

}<br />

See Also<br />

Recipe 11.2<br />

11.10 Getting Random Integers<br />

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

Given a pseudo-random number generation interface that returns an array of bytes,<br />

you need to get random values in the various integer data types.<br />

Solution<br />

For dealing with an integer that can contain any value, you may simply write bytes<br />

directly into every byte of the integer.<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Getting Random Integers | 605

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

Saved successfully!

Ooh no, something went wrong!