← Back to section · Software & Developer Tools
Testing Key Handling and Randomness: Testing What Cannot Be Seen or Touched
Keys and random numbers are the foundation of cryptographic software, but their defects show up not as crashes but as predictability and leakage. This article surveys the testing methods for this domain: entropy assessment and statistical testing of random sources, lifecycle state checks for keys, memory-zeroisation verification, and why such testing can only ever give probabilistic assurance.
The most dangerous defects in cryptographic software make no sound at all: the program does not crash, every function works, every test is green — it is just that the keys it generates can be predicted, or keys that should have been destroyed are still sitting in memory. Several famous cryptographic failures in history — from a game console’s signing key being reverse-engineered because of repeated random values, to wallet applications drained in bulk because of weak randomness — had their roots not in algorithms but in randomness and key handling. Testing in this domain therefore has its own methodology: what is tested is not functional correctness but statistical properties, state transitions, and memory traces.
Randomness: What Statistical Tests Can and Cannot Do
Running statistical tests over a random generator’s output is the most obvious and most easily misused layer. Suites such as NIST SP 800-22 apply a dozen-plus statistical tests — frequency, runs, spectral, and others — to check whether an output sequence “looks random”, and they can filter out generators that are obviously biased, too short in period, or structurally exposed. But their conclusions have strict boundaries: passing statistical tests only means no anomaly was detected; a beautifully structured deterministic algorithm (say, a good stream cipher run from a fixed seed) passes them all — and predictability is precisely what statistical tests cannot see. The engineeringly sound path is therefore two-stage: the underlying entropy source is assessed along the lines of SP 800-90B, verifying the noise source’s entropy rate and that health tests (start-up self-tests, continuous value monitoring) actually alarm on anomalies; the upper layer uses a standardised deterministic random bit generator (DRBG) to expand entropy into output, whose implementation correctness is assured by known-answer tests — given seed material, the output must match reference values byte for byte. The testing focus then falls on concrete, scenario-constructible, assertable points: does the code really seed from the operating system or a hardware entropy source; does anything use guessable values such as timestamps or process IDs as seeds; and does random state repeat after virtual-machine snapshot restore or process forking?
Key Handling: Checking Every Link of the Lifecycle
Key testing targets the entire lifecycle. At generation: are keys produced by a compliant random source, and do parameters (curve, modulus length, labels) come from configuration rather than hard-coding? At storage: are private keys stored encrypted, are access permissions minimised, and does anything write keys into logs or error messages? At transfer and backup: does the export format carry integrity protection, and are keys on backup media protected as strongly as the primary copies? At use: are key usages separated (one key, one job — signing and encryption never mixed), and is plaintext key material cached at layers where it does not belong? Destruction is the link most easily overlooked and hardest to test: language runtimes and compilers optimise away zero-overwrite assignments that look useless, and data may already have been copied into other buffers; verification methods include checking that the compiled artefact retains the zeroisation calls, using explicit optimisation-resistant zeroisation primitives, and deliberately crashing a test process after key use to check whether key material can still be found in the memory dump. Rotation is a process test rather than a code test: are expired keys actually retired, and does the overlap period between old and new keys have explicit boundaries?
One further testing surface is often missed: the key hygiene of the test environment itself. Keys, seeds, and certificates circulating in test and staging environments are usually managed to a far lower standard than in production: written into configuration files, protected by weak passwords, travelling with the code repository. Testing should specifically check two classes of problem. The first is isolation: can test keys leak into production (identically named configuration files, copied database snapshots), and have production keys ever been borrowed into a test environment for convenience — once the two are mixed, the test environment’s low standard becomes production’s actual standard. The second is the containment of test fixtures: fixed test vectors for known-answer tests are legitimate, but fixed seeds for testing, debug backdoors, and switches that bypass the random source must not survive into release builds; such switches should be backstopped by build-time assertions and pre-release scanning. More than one historical incident came from an “for testing only” shortcut shipping as-is into users’ hands — a lesson worth writing into every cryptographic project’s checklist.
Probabilistic Assurance: The Place of Testing
The epistemic boundary of this testing must be admitted: what it provides is “conforms to the specification” and “no known bad pattern found”, not a mathematical proof of unpredictability. An implementation can pass every statistical and known-answer test and still become predictable because its entropy source degrades in some hardware state — such problems are prevented by continuous entropy health monitoring and design review, not by a one-off test before shipping. The right engineering posture layers the testing: known-answer tests lock down implementation correctness; negative tests (network loss, entropy-source fault injection, clock rollback) verify that anomaly paths do not degrade into weak randomness; code audit watches seed sources and key flows; runtime monitoring backstops entropy degradation. For keys and randomness, the value of testing is not declaring “tested, therefore secure”, but turning every class of failure that has actually happened in history into a test case that fails — so none of them gets the chance to repeat itself in the same way. This also means the test suite itself must be continuously updated with newly disclosed failure cases: every publicised randomness incident should trigger a self-examination — does the same failure pattern exist in our own code, and would the existing cases catch it? Turning other people’s incidents into one’s own regression cases is the best-value testing investment in this domain.
Tags: RandomnessKey ManagementEntropy