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.

11.3 Using the Standard Unix Randomness<br />

Infrastructure<br />

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

You want to use random numbers on a modern-day Unix machine.<br />

Solution<br />

On most modern Unix systems, there are two devices from which you can read: /dev/<br />

random, which is expected to produce entropy, and /dev/urandom, which is expected<br />

to provide cryptographically secure pseudo-random values. In reality, these expectations<br />

may not always be met, but in practice, it seems reasonably safe to assume that<br />

they are.<br />

We strongly recommend accessing these devices through the API we present in Recipe<br />

11.2.<br />

Discussion<br />

If you need a cryptographically strong random number source that is<br />

nonetheless reproducible, /dev/random will not suit your purposes.<br />

Use one of the other PRNGs discussed in this chapter.<br />

Most modern Unix operating systems have two devices that produce random numbers:<br />

/dev/random and /dev/urandom. In theory, /dev/random may block and should<br />

produce data that is statistically close to pure entropy, while /dev/urandom should<br />

return immediately, providing only cryptographic randomness.<br />

The real world is somewhat messy, though. First, your application may need to run<br />

on a system that does not have these devices. (In that case, see Recipe 11.19, where<br />

we discuss solutions to this problem. *) Any reasonable version of Linux, FreeBSD,<br />

OpenBSD, or NetBSD will have these devices. They are also present on Mac OS X<br />

10.1 or later, Solaris 9 or later, AIX 5.2 or later, HP-UX 11i or later, and IRIX 6.5.19<br />

or later. As of this writing, only dead or officially “about to die” Unix variants, such<br />

as Tru64 and Ultrix, lack these devices. Note that each operating system tends to<br />

have its own implementation of these devices. We haven’t looked at them all, so we<br />

cannot, in general, vouch for how strong and efficient these generators are, but we<br />

* If you want to interoperate with such platforms (there are still plenty of systems without /dev/random and<br />

/dev/urandom), that reinforces the utility of using our API; simply link against code that implements our API<br />

using the solution from Recipe 11.8 instead of the solution from this recipe.<br />

Using the Standard Unix Randomness Infrastructure | 575<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!