Skip to content

Integrating a QRNG: C, Python, Rust and the Linux entropy path

On Linux a hardware entropy source is normally exposed through the kernel random subsystem, so applications keep calling getrandom() and inherit the better seed without changing. Direct access through a device interface or a library is available where an application needs entropy that has not passed through the kernel pool.

The default path: through the kernel

The right integration for most systems changes no application code. The device feeds the kernel entropy pool, and everything that already calls getrandom(), reads /dev/urandom, or uses a TLS library's RNG benefits without knowing anything happened.

This is the correct default for a reason beyond convenience. The kernel pool mixes several sources, so adding a strong one improves the seed without creating a new single point of failure. An application reading a device directly loses that mixing, and gains a dependency on one piece of hardware being present and healthy.

When to bypass the pool

  • A compliance regime requires entropy from a specifically evaluated source, and a mixed pool cannot demonstrate that.
  • Consumption is high enough that going through the kernel adds meaningful overhead.
  • The application needs the source's health status alongside its output, which the pool does not carry.

Language notes

In C, treat the source as a file descriptor and read from it, checking short reads. Do not assume a read returns the number of bytes requested. In Python, prefer os.urandom() and the secrets module once the kernel is seeded from the device; reach for a direct binding only if you need health telemetry. In Rust, the getrandom crate already routes to the kernel interface, so a kernel-level integration is transparent to it.

Across all three the guidance is the same: read from the operating system unless you have a stated reason not to, and when you do bypass it, handle the failure path explicitly. A generator that silently falls back to a weaker source on error is worse than one that fails.

First boot

The case worth engineering carefully is the first one. A device generating a key before its entropy source has produced enough output is how identical keys end up across a product line. If the platform has a hardware source, block on it during provisioning rather than proceeding with whatever the pool happens to hold. Key generation in an HSM treats the same problem where the stakes are highest.