I know from experience that reading from /dev/random blocks when the Linux kernel entropy pool runs out of entropy. Also, I've seen many articles and blog entries stating that when running on Linux, java.security.SecureRandom uses /dev/random as its entropy source and thus blocks when the kernel entropy pool runs out of entropy.
However, I'm unable to produce an experiment which causes SecureRandom to block. Conversely, it seems easy to get a simple bash one-liner which reads from /dev/random to block.
Here's the java code I'm using for these experiments:
import java.security.SecureRandom;
public class A {
public static void main(String[] args) {
SecureRandom sr = new SecureRandom();
int out = 0;
for (int i = 0; i < 1<<20 ; i++) {
out ^= sr.nextInt();
}
System.out.println(out);
}
}
It generates just over 1,000,000 random 32-bit integers. That should be 2^(20 + log2(32)) = 2^25 bits or 2^22 (a little over 4 million) bytes of entropy, right? However, it never blocks. It always finishes in about 1.2 seconds no matter whether I wiggle the mouse or not.
The bash one-liner I used is:
head -c 100 /dev/random | xxd
This blocks easily. As long as I keep my hand off of the mouse and keyboard, it'll sit there doing nothing for several minutes. And I'm only asking for 100 bytes of entropy.
Surely I'm missing something here. Could someone explain what's going on?
Thanks!