The proper interface to use on Linux is getrandom
, which is available in most Linux distros these days. The interface will block until the CSPRNG is initialized on startup (unless GRND_NONBLOCK
is specified) and never again.
That ensures that you have at least 256 bits of entropy on the system, which is enough to generate all the keys you could possibly need. The system will continually reseed the CSPRNG with new entropy as it goes along as well. Note that making sure the CSPRNG is seeded on Linux is very easy if you have a hardware RNG like RDRAND or RDSEED and you've configured CONFIG_RANDOM_TRUST_CPU
in your kernel (most distros do).
On FreeBSD, getrandom
is also available, and you can use getentropy
or arc4random
on any of the BSDs. (Technically, those are also available on Linux, but only in more recent versions of glibc.) To my knowledge, the BSDs by default ensure the OS CSPRNG is seeded appropriately on startup, provided writable media exist. See the random(4)
page for your BSD for details.
On most varieties of Unix, /dev/urandom
can be used as well, but on Linux it doesn't guarantee that the CSPRNG has been appropriately seeded. That's why using getrandom
or getentropy
is preferred.
On Windows, RtlGenRandom
is the right interface.
Note that if your system is hanging after the machine has been up and serving TLS requests for a while, then it isn't a lack of entropy, because all major operating systems continue to serve data from the system CSPRNG after it's initially seeded. As mentioned, 256 bits of entropy is sufficient to generate a virtually unlimited amount of random data on the system (especially considering the fact that you'll reboot now and again for kernel security updates), so there's no need to block once correctly seeded.