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.

• Get entropy if it is available, and if it is not, fall back on cryptographically strong<br />

random numbers (using any available entropy).<br />

• Get data that should be highly entropic that has never passed through a pseudorandom<br />

number generator. Note that this function is not always available and<br />

that it will hang until enough entropy is available.<br />

The first function, which always produces cryptographically strong randomness, has<br />

the following signature:<br />

unsigned char *spc_rand(unsigned char *buf, size_t b);<br />

It places b bytes into memory, starting at the location buf, and returns buf (this is<br />

done to minimize the chance of someone misusing the API). This function always<br />

returns unless it causes your program to abort, which it does only if spc_rand_init( )<br />

has never successfully returned.<br />

The second function, which returns entropy if it is available, and otherwise produces<br />

cryptographically strong randomness, has the following signature:<br />

unsigned char *spc_keygen(unsigned char *buf, size_t b);<br />

The arguments are the same as for spc_rand( ). The name change reflects the fact<br />

that this is meant to be the function you will generally use for generating long-term<br />

key material, unless you want to insist that key material come directly from entropy,<br />

in which case you should use the spc_entropy( ) function. For all other uses, we recommend<br />

using spc_rand( ).<br />

The spc_entropy( ) function mimics the first two functions:<br />

unsigned char *spc_entropy(unsigned char *buf, size_t b);<br />

However, note that this function will block until it has enough entropy collected to<br />

fill the buffer. For Windows, this function is only usable using the code in this book<br />

if you use EGADS, as discussed in Recipe 11.8.<br />

See Also<br />

The functions spc_keygen( ) and spc_entropy( ) should cryptographically<br />

postprocess (whiten) any entropy they use before outputting it, if<br />

that’s not already done by the underlying entropy sources. Often, it<br />

will be done for you, but it will not hurt to do it again if you are not<br />

sure. (See Recipe 11.16 for how to do it.)<br />

Recipes 11.8, 11.10, 11.11, 11.12, 11.13, 11.14, 11.16<br />

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