Skip to content

ConceptsHow the generator works

How the generator works

One HMAC_DRBG per processor, buffered, seeded and reseeded from the operating system, with every verified pulse mixed in.

ISecureRandom has to be fast, because a call such as NextInteger(1, 7) costs nanoseconds, never a network round trip, and many threads call one singleton at once. It also has to stay unpredictable. This page explains how the generator gets both: what it is built from, how it is split and buffered, when it reseeds, and where its secret state lives.

The generator is an HMAC_DRBG with HMAC-SHA-512, exactly as NIST SP 800-90A Rev. 1 section 10.1.2 specifies, at a security strength of 256 bits. The NIST CAVP test vectors for SHA-512 HMAC_DRBG pin it, so the output is checked against the published answers.

The library keeps one generator, called a shard, per processor, rounded up to a power of two and capped at 64. A thread picks its shard by its managed thread ID, and each shard has its own lock, so threads rarely contend.

Each shard generates 512 bytes at a time and serves small requests from that buffer. It zeroes each byte it serves, so a served byte is gone from memory. A request of 512 bytes or more bypasses the buffer. The HMAC work of a small request is therefore amortized over 512 bytes, and the request itself costs a copy and a lock.

Up to 511 generated bytes per shard wait in memory until they are served or discarded. That is the trade every buffered generator makes. Bytes that are already served stay safe, because the generator updates its state after each generate, so a later memory disclosure cannot recover them.

Every shard starts from 64 bytes of operating-system entropy and a 32-byte nonce. All of the entropy comes from the operating system.

A shard reseeds from 64 fresh bytes of operating-system entropy, with its index and the beacon pool’s epoch and digest as additional input, in any of three cases:

  • As soon as a new verified pulse is mixed in.
  • After ReseedInterval, one minute by default.
  • After ReseedAfterBytes, 1 MiB by default.

A reseed discards whatever the buffer still holds, so no byte generated before a reseed is served after it. A pulse is only ever additional input, never a replacement for the operating system’s entropy, which is why a missing or forged pulse cannot weaken the output. Secret and public randomness explains why.

The two limits are options. A service that wants a tighter bound than the defaults sets them:

services.AddRandomness(options =>
{
options.ReseedInterval = TimeSpan.FromSeconds(30);
options.ReseedAfterBytes = 256 * 1024;
});

No value waits on the network: a pulse is fetched in the background and picked up by the next reseed, and a beacon outage changes nothing about how fast or how secure a value is.

The generator’s key, value and output buffer are pinned arrays, and its seeds are stackalloc memory. Each is cleared with CryptographicOperations.ZeroMemory, and the generator zeroes its state when it is disposed. Secret material never goes in ArrayPool<T>.Shared or an unpinned array, because the garbage collector can move an unpinned array and leave a copy behind. The packages take no buffer library dependency: their members take and fill a Span<byte>, so a caller keeps a secret in the memory it chooses. ADR-1001 records the construction, and ADR-1012 records where secrets live.

The metric ncode.randomness.generator.reseeds counts reseeds, tagged with a reason: beacon for a new pulse, interval for ReseedInterval and volume for ReseedAfterBytes. A steady count of interval and volume reseeds with no beacon ones means no pulse is arriving.

Configuration lists every option, including the reseed limits. Secret and public randomness explains why a pulse never makes a secret, and Monitor the beacons shows how to watch the reseed metric and the health check.