Blocking vs Non-Blocking Randomness
What you'll learn: How Linux's /dev/random and /dev/urandom differ in blocking behavior and why boot-time entropy matters for security.
The Two Sources of Randomness
On Unix-like systems, two special files provide randomness:
/dev/random– The "blocking" source/dev/urandom– The "non-blocking" source
Both draw from the system's entropy pool, a collection of unpredictable data gathered from hardware events (keyboard timing, disk I/O, network packets, etc.).
When /dev/random Blocks
Think of the entropy pool as a water reservoir. /dev/random acts like a cautious dispenser: if it estimates there isn't enough "true randomness" available, it blocks (waits) until more entropy is collected. This can pause your program indefinitely during low-activity periods.
/dev/urandom is the pragmatic choice: once the pool is initialized at boot, it uses a CSPRNG to stretch available entropy and never blocks. For cryptographic purposes, this is almost always what you want.
The Boot-Time Problem
Here's the critical issue: on system boot, especially on embedded devices or virtual machines, the entropy pool might not be initialized yet. If you generate keys or seeds before sufficient entropy exists, you might get predictable values—a catastrophic security failure.
Modern systems signal entropy readiness through mechanisms like getrandom() with the GRND_RANDOM flag, which blocks until the pool is properly seeded at boot, then behaves like /dev/urandom thereafter.
Practical Guidance
For applications needing cryptographic randomness:
- Use
getrandom()or/dev/urandom(not/dev/random) - On boot, ensure the entropy pool is initialized before generating keys
- Never assume randomness is available immediately after system start
Key Takeaway: /dev/random blocks when entropy runs low; /dev/urandom doesn't block after initialization. The real danger is generating keys before the entropy pool is seeded at boot—use modern APIs that wait for proper initialization.