Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

linux

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.

like image 752
hookenz Avatar asked Dec 06 '11 03:12

hookenz


People also ask

Why does Dev random block?

In those older kernels, /dev/random would block because the blocking entropy pool had been depleted. However, changes to the algorithms used in ealier kernels meant that there was no longer any useful distinction between the blocking and non-blocking entropy pools.

Does urandom block?

The /dev/urandom device typically was never a blocking device, even if the pseudorandom number generator seed was not fully initialized with entropy since boot. Not all operating systems implement the same methods for /dev/random and /dev/urandom.

Should I use Dev urandom?

Applications should read from /dev/urandom when they need randomly generated data, e.g. cryptographic keys or seeds for simulations.

What is the purpose of Dev random?

The /dev/random device is intended to provide high quality, cryptographically secure random output and will only return output for which sufficient (an equal or greater amount) random input is available to generate the output.


2 Answers

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.

like image 140
David Schwartz Avatar answered Nov 15 '22 23:11

David Schwartz


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

like image 23
Basile Starynkevitch Avatar answered Nov 15 '22 22:11

Basile Starynkevitch