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.

don’t think you should worry about this issue in practice. (There are almost always<br />

bigger fish to fry.)<br />

Second, depending on the operating system, the entropy produced by /dev/random<br />

may be reused by /dev/urandom. While few (if any) Unix platforms try to guarantee a<br />

clean separation of entropy, this is more of a theoretical problem than a practical<br />

problem; it is not something about which we personally would worry. Conversely,<br />

depending on the operating system, use of /dev/urandom can drain entropy, denying<br />

service to the /dev/random device.<br />

Finally, most operating systems do not actually guarantee that /dev/urandom is properly<br />

seeded. To understand why, you need to know something about what generally<br />

goes on under the hood. Basically, the randomness infrastructure tries to cull randomness<br />

from user input. For example, tiny bits of entropy can be derived from the<br />

time between console keystrokes. Unfortunately, the system may start up with very<br />

little entropy, particularly if the system boots without user intervention.<br />

To avoid this problem, most cryptographic pseudo-random number generators<br />

stash away output before the system shuts down, which is used as a seed for the<br />

pseudo-random number generator when it starts back up. If the system can reboot<br />

without the seed being compromised (a reasonable assumption unless physical<br />

attacks are in your threat model, in which case you have to mitigate risk at the physical<br />

level), /dev/urandom will produce good results.<br />

The only time to get really paranoid about a lack of entropy is before you are sure the<br />

infrastructure has been seeded well. In particular, a freshly installed system may not<br />

have any entropy at all. Many people choose to ignore such a threat, and it is reasonable<br />

to do so because it is a problem that the operating system should be responsible<br />

for fixing.<br />

However, if you want to deal with this problem yourself, be aware that all of the<br />

operating systems that have a /dev/random device (as far as we can determine) monitor<br />

all keyboard events, adding those events to their internal collection of entropy.<br />

Therefore, you can use code such as that presented in Recipe 11.20 to gather sufficient<br />

entropy from the keyboard, then immediately throw it away (because the operating<br />

system will also be collecting it). Alternatively, you can collect entropy yourself<br />

using the techniques discussed in Recipes 11.22 and 11.23, then run your own cryptographic<br />

pseudo-random number generator (see Recipe 11.5).<br />

The /dev/random and /dev/urandom devices behave just like files. You should read<br />

from these devices by opening the files and reading data from them. There are a few<br />

common “gotchas” when using that approach, however. First, the call to read data<br />

may fail. If you do not check for failure, you may think you got a random number<br />

when, in reality, you did not.<br />

Second, people will occasionally use the API functions improperly. In particular, we<br />

have seen people who assume that the read( ) or fread( ) functions return a value or<br />

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