hookenz
hookenz

Reputation: 38907

Why does reading from /dev/random nearly always block?

I'm using kubuntu with kernel 2.6.38-12-generic

I want to read 16 random numbers from /dev/random at the start of my program. However, it blocks after a relatively short time.

How long does it take for the /dev/random buffer to fill? why is it taking so long to fill.

I'm using this as a uuid generator with other sources of randomness added to seed my mersenne twister. It's critical that I don't get duplicates or a duplicate seed.

If I change to /dev/urandom it works ok. Any view on using /dev/random over /dev/urandom.

Upvotes: 7

Views: 2771

Answers (3)

The man page of man 4 random answers the question:

When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. /dev/random should be suitable for uses that need very high quality randomness such as one-time pad or key generation. When the entropy pool is empty, reads from /dev/random will block until additional environmental noise is gathered.

I'm so surprised people prefer asking than reading the man pages! You don't even need Internet to read the man pages of your system.

BTW, as I commented, the entropy pool is fed by physical phenomena (depends of the hardware), like e.g. mouse movements, key presses, ethernet packets, etc. Some few processors have a hardware random noise generator (e.g. the RDRAND machine instruction), and you can buy random USB devices (see also this list), etc.... Hence reading from /dev/random could be expansive (or even blocking). You'll use it for high quality randomness (e.g. required by cryptographic keys) or, at initialization, for seeding your PRNG. You should expect /dev/random to have a relatively small bandwidth (e.g. a few kilobytes or at most a megabyte per second at most) and it could have a lot of latency (dozens of milliseconds, or even more). Details are of course computer specific.

Read also Thomas Hühn's Myths about /dev/urandom

Upvotes: 6

moshbear
moshbear

Reputation: 3322

Reading from /dev/random is non-determinstic, because all it does is fetch the requested number of bits from the random pool. It will block until it can read the requested number of bits.

/dev/urandom, however, is the kernel's PRNG, and can supply a near-infinite stream of pseudo-random numbers.

Upvotes: 4

David Schwartz
David Schwartz

Reputation: 182779

You really should never use /dev/random. There are no known circumstances where the advantages of /dev/random over /dev/urandom matter, and the disadvantages are pretty obvious.

The difference is that /dev/urandom provides 'merely' cryptographically-secure random numbers while /dev/random provides truly random numbers (at least, that is what we believe). But there is no known situation where this difference matters and no known test that can distinguish "true" randomness from merely cryptographically-secure randomness.

I usually joke that /dev/urandom provides water and /dev/random provides holy water.

Upvotes: 19

Related Questions