Feature notes
DRBG and entropy source (SP 800-90) notes
================================================================================
DRBG & ENTROPY SOURCE
The SP 800-90A/B/C random-generation stack (stRandom / stBaseDRBG /
stEntropySource and friends)
(StreamSec Security Library for Embarcadero Delphi)
================================================================================
Generated: 2026-07-30 (First edition. Documents the DRBG framework, the
TSC-jitter noise source, the per-sample min-entropy
claim, the SP 800-90B health tests, and the new
install-time calibration path. Written alongside two
collection changes: Windows cCollectIterations raised
32 -> 128 (uniform with POSIX), and the per-round
collection size made runtime-tunable via
gCollectIterations, with stEntropyCalibration added to
all 14 StrSecIV runtime packages. Section 1 gives
when-to-use guidance: primary vs secondary DRBG vs the
system CSPRNG vs an external hardware RNG.)
--------------------------------------------------------------------------------
0. SUMMARY
--------------------------------------------------------------------------------
The library generates random data with an SP 800-90A DRBG seeded from an
SP 800-90B entropy source, wired together per SP 800-90C:
application -> stRandom (registered generator, global handle)
-> stBaseDRBG / stDrbgAesCtr (CTR_DRBG, AES)
-> stEntropySource (tEntropy: the noise source + conditioning)
The noise source is CPU timestamp-counter (TSC) jitter: several collector
threads each run SHA-256 chains and time every iteration; the low byte of the
timing delta is the raw sample. Samples are health-tested continuously
(SP 800-90B 4.4) and credited conservatively before the pool releases entropy.
Two knobs are read once per tEntropy construction (set them at startup, before
the first instance) and can be calibrated per machine at install time:
EntropyHealthMinEntropy : Double -- claimed min-entropy PER SAMPLE
gCollectIterations : Cardinal -- timed iterations PER CHAIN PER ROUND
================================================================================
1. CHOOSING A RANDOM SOURCE (PRIMARY / SECONDARY / SYSTEM / HARDWARE)
================================================================================
Consumer entry point: stRandom.GetRawRandom(aSecondary: Boolean): iRawRandomEx.
GetRawRandom(False) PRIMARY - shared singleton DRBG, seeded and reseeded
from the SP 800-90B noise source (section 5).
GetRawRandom(True) SECONDARY - a fresh DRBG instance, seeded FROM the primary
(tSecondaryEntropy), not from the physical
source. Independent per call.
PRIMARY DRBG -- the root of trust. Use for anything whose compromise is
catastrophic and whose volume is modest:
- long-term key generation and key-encryption keys
- seeds for other DRBGs (including every secondary)
- salts/nonces where predictability breaks the scheme
It is a shared singleton (lock-guarded) and its reseeds draw on the physical
noise source, which is deliberately rate-limited. Do NOT hammer it for bulk or
high-frequency output: that serialises threads on one lock and drains the source.
SECONDARY DRBG -- cheap, isolated, still cryptographically strong (it inherits
the primary's strength via its seed). Use for high-volume or per-context random
where you want speed and no contention on the primary:
- one per connection / per thread / per session
- protocol wire values that must be unpredictable but are not themselves the
secret: TLS client/server randoms, session IDs, ticket age masks, IVs,
padding, blinding jitter
Take a secondary once per context and reuse it; it reseeds from the primary on
its own interval. This is the normal choice for everything except the items in
the PRIMARY list above.
SYSTEM RANDOM SOURCE (OS CSPRNG) -- Windows CryptGenRandom / BCryptGenRandom,
POSIX getrandom(2) / /dev/urandom. Appropriate when:
- a policy or certification mandates the platform generator
- you are outside a context where this library's DRBG is initialised
- you want the OS to own reseeding and entropy management
Trade-off: it is a black box -- you cannot run the SP 800-90B assessment or the
per-machine calibration (sections 8-9) against it, and you inherit the OS pool's
provenance. This library defaults to its own jitter source precisely so the
entropy is assessable end to end.
EXTERNAL HARDWARE RNG -- a dedicated entropy device (TPM, RDRAND/RDSEED, HSM,
smartcard). Appropriate when policy/certification requires a validated hardware
source, when you need a higher assured entropy rate than jitter provides, or when
an HSM already holds the keys. Prefer wiring it in as the ENTROPY SOURCE that
feeds the primary DRBG (an iSecretKey-returning entropy source, or a registered
RawRandom factory) rather than consuming raw hardware output directly: that keeps
the SP 800-90C construction -- conditioning, the S + 64 oversampling margin, and
the health tests -- around the hardware output. Only bypass the DRBG if the
device is itself a validated DRBG you are required to use verbatim.
Quick guide:
key material / seeds / low volume -> PRIMARY GetRawRandom(False)
per-connection or bulk, must be strong -> SECONDARY GetRawRandom(True)
platform mandate / uninitialised context -> SYSTEM CSPRNG
certified HW / high assured rate -> HARDWARE, feeding the primary DRBG
================================================================================
2. UNIT INVENTORY
================================================================================
stRandom Registered-generator registry + global handle.
stBaseDRBG SP 800-90A DRBG base (instantiate / reseed / generate).
stDrbgAesCtr CTR_DRBG over AES.
stBaseEntropySource iSecretKey-returning entropy-source interface.
stEntropySource tEntropy: TSC-jitter noise source, collectors, the
controller thread, conditioning, credit accounting.
stEntropySourcePosix POSIX flavour of tEntropy (clock_gettime timing).
stEntropyHealth SP 800-90B 4.4 health monitor (RCT + APT) and, in
assessment/calibration builds, the raw-sample recorder.
stEntropyEstimators SP 800-90B 6.3 non-IID estimator suite (offline
assessment; bit-exact vs the NIST ea_non_iid oracle).
stEntropyCalibration Install-time loader/saver for the two runtime knobs.
================================================================================
3. STANDARDS MAP
================================================================================
SP 800-90A DRBG mechanism (CTR_DRBG) stBaseDRBG / stDrbgAesCtr
SP 800-90B Entropy source + health tests stEntropySource / stEntropyHealth
SP 800-90B 6.3 non-IID min-entropy estimate stEntropyEstimators (offline)
SP 800-90C DRBG-from-entropy construction the S + 64 oversampling rule
================================================================================
4. DRBG FRAMEWORK
================================================================================
An entropy request carries a security strength S. Per SP 800-90C the pool must
hold S + cEntropyOversample (= 64) bits of credited entropy before it is
released; a request blocks until that is met, then the conditioned output is
returned and the counter reset.
Instantiation asks for 3/2 * S (a nonce is drawn at the same time as the seed);
reseed asks for S. So a 256-bit instantiate needs 384 + 64 = 448 credited bits,
a 256-bit reseed needs 256 + 64 = 320. These accumulate over as many collection
rounds as the per-round yield requires (see sections 5 and 6).
Output is inhibited until the SP 800-90B startup tests pass, and again whenever
a health test fails (fail-closed: credited entropy is discarded and the startup
tests re-run).
================================================================================
5. NOISE SOURCE - TSC JITTER
================================================================================
Structure (constants in stEntropySource):
cCollectors = 4 collector threads = independent noise-source instances
cCollectChains = 4 SHA-256 chains per collector
gCollectIterations timed iterations per chain per round (default 128)
Each round every chain is timed over gCollectIterations iterations; the raw
sample is (delta mod 256). Samples per round = cCollectors * cCollectChains *
gCollectIterations (4 * 4 * 128 = 2048 at the default).
Why 128 everywhere. How much jitter lands in the low byte depends on the
timestamp UNIT -- Windows times with rdtsc (cycles, ~0.3 ns), every POSIX
target with clock_gettime (nanoseconds, ~3 cycles) -- but ALSO, and less
predictably, on the specific silicon. It is tempting to collect fewer samples
on Windows on the theory that the finer timer buys enough per-sample entropy
to pay for it. Real-hardware SP 800-90B 6.3 assessments (the full
ten-estimator suite, cross-checked bit-exact against the NIST ea_non_iid tool)
show that theory does not hold across machines: assessed worst-channel
min-entropy on Windows was ~2.22 bits/sample on one box but only ~0.35 on
another -- squarely in the POSIX range (Linux ~0.50, macOS ~0.78). A
"~3.2 Windows bits/sample" figure is an MCV upper bound, not the assessed
value. Because a low-entropy Windows box is a real possibility, the source
collects the same larger budget on every platform, so a round carries a
comparable amount of entropy regardless of OS or luck of the CPU.
This does NOT license reusing one machine's entropy claim on another: SP 800-90B
treats each platform (and really each box) as its own entropy source. Assess the
target and, if needed, calibrate it (section 8).
================================================================================
6. PER-SAMPLE MIN-ENTROPY CLAIM
================================================================================
EntropyHealthMinEntropy : Double = 0.25;
This is the claimed min-entropy PER 8-bit reduced sample. It drives two things:
the SP 800-90B 4.4 health-test cutoffs, and a cap on the per-round credit
(credit/round <= samples/round * claim). It is a rate, NOT a per-request total:
a request for S bits is satisfied by accumulating rounds until S + 64 bits are
credited.
0.25 is a conservative FLOOR meant to hold across all deployment hardware, not a
per-machine tuning. The honest measure of headroom is the SP 800-90B 6.3
ASSESSED min-entropy (the minimum over all ten estimators, both passes) -- NOT
the MCV upper bound, which overstates it. Assessed worst-channel figures seen on
real hardware: Windows 2.22 and 0.35, Linux ~0.50, macOS ~0.78 -- so 0.25 holds
everywhere measured, but with as little as ~1.4x to spare on some Windows
silicon. Do not raise it toward any single machine's figure.
At the default 128 iterations and 0.25 claim, a round credits up to 2048 * 0.25
= 512 bits -- enough to satisfy a full 384+64 instantiate in one round.
================================================================================
7. HEALTH TESTS (SP 800-90B 4.4)
================================================================================
stEntropyHealth runs, per collector channel and in collection order:
- Repetition Count Test (RCT, 4.4.1)
- Adaptive Proportion Test (APT, 4.4.2)
Cutoffs are computed from EntropyHealthMinEntropy. Startup withholds output
until 1024 samples/channel have passed; a run-time failure fails closed (discard
credit, restart the startup tests). The monitor runs on the internal controller
thread; consumer code only reads its state for diagnostics.
================================================================================
8. INSTALL-TIME CALIBRATION
================================================================================
Both knobs are runtime globals, re-read per tEntropy construction, so a
deployment can size the source to the target box's MEASURED entropy. Two rules,
each moving its knob only toward MORE conservative:
EntropyHealthMinEntropy only ever LOWERED (claim less per sample)
gCollectIterations only ever RAISED (collect more per round)
(clamped to [default 128, cMaxCollectIterations 4096])
Because both are one-way, a tampered or corrupt calibration file can at worst
make the source slower or over-collect (a denial of service); it can never
weaken the entropy claim.
Consumer API (stEntropyCalibration):
LoadCalibration(const aPath: string): Boolean; -- call ONCE at startup,
before the first tEntropy
The file is plain text, invariant decimals, one key=value per line:
claim=0.174222
iterations=184
Producing the file. The calibration run is stEntropyCalibration.
RunInstallerCalibration, compiled only under the ENTROPY_CALIBRATE define. The
installer builds a single-line console program that calls it and runs that once
on the target machine:
program CalibrateEntropy;
{$APPTYPE CONSOLE}
uses stEntropyCalibration;
begin RunInstallerCalibration; end.
It drives the real source, assesses every channel with the 6.3 suite, takes the
worst channel, and writes claim = min(worstH / 2, default) and iterations sized
so a round carries ~512 bits at that claim. Jitter is noisy run-to-run, so pass
"-runs N" to repeat the capture and drive the claim from the MINIMUM worst-channel
over N runs (the most conservative sample); any stalled/health-failing run aborts
without writing. This build taps the raw noise source; it is an installer/lab
tool and must not ship as part of the runtime.
Calibration gives operational conservatism per box; a SUBMITTED SP 800-90B claim
additionally needs the restart dataset and independent review.
================================================================================
9. OFFLINE ASSESSMENT TOOLING
================================================================================
stEntropyEstimators implements the full SP 800-90B 6.3 non-IID suite (all ten
estimators, literal + bitstring passes, the H = min(H_original, 8*H_bitstring)
rollup) and is bit-exact against the NIST ea_non_iid reference. The assessment
programs (one to capture a dataset from the real source, one to score datasets,
one for the restart test) build with the ENTROPY_ASSESSMENT define; the
installer's calibration program builds with ENTROPY_CALIBRATE (section 8). Both
defines tap the raw noise source and are for lab / installer use only -- NEVER
build a shipping product with either defined.