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.

anyway. You can use any of the sources we have discussed elsewhere in this chapter<br />

for seeding the OpenSSLPRNG. Multiple API functions are available that allow seed<br />

information to be passed to the PRNG.<br />

One such function is RAND_seed( ), which allows you to pass in arbitrary data that<br />

should be completely full of entropy. It has the following signature:<br />

void RAND_seed(const void *buf, int num);<br />

This function has the following arguments:<br />

buf<br />

Buffer containing the entropy to seed the PRNG.<br />

num<br />

Length of the seed buffer in bytes.<br />

If you have data that you believe contains entropy but does not come close to one bit<br />

of entropy per bit of data, you can call RAND_add( ), which is similar to RAND_seed( )<br />

except that it allows you to provide an indication of how many bits of entropy the<br />

data has:<br />

void RAND_add(const void *buf, int num, double entropy);<br />

If you want to seed from a device or some other file (usually, you only want to use a<br />

stored seed), you can use the function RAND_load_file( ), which will read the<br />

requested number of bytes from the file. Because there is no way to determine how<br />

much entropy is contained in the data, OpenSSLassumes that the data it reads from<br />

the file is purely entropic.<br />

int RAND_load_file(const char *filename, long max_bytes);<br />

If –1 is specified as the length parameter to this function, it reads the entire file. This<br />

function returns the number of bytes read. The function can be used to read from the<br />

/dev/random and /dev/urandom devices on Unix systems that have them, but you<br />

must make sure that you don’t specify –1 for the number of bytes to read from these<br />

files; otherwise, the function will never return!<br />

To implement PRNG state saving with OpenSSL, you can use RAND_write_file( ),<br />

which writes out a representation of the PRNG’s internal state that can be used to<br />

reseed the PRNG when needed (e.g., after a reboot):<br />

int RAND_write_file(const char *filename);<br />

If there is any sort of error, RAND_write_file( ) will return –1. Note that the system<br />

may write a seed file without enough entropy, in which case it will also return –1.<br />

Otherwise, this function returns the number of bytes written to the seed file.<br />

To obtain pseudo-random data from the PRNG, use the function RAND_bytes( ):<br />

int RAND_bytes(unsigned char *buf, int num);<br />

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