Blog / A02:2021 Cryptographic Failures
A02:2021 · Cryptographic Failures

Your encryption is only as random as whoever calls the constructor

A cipher class took its randomness source as a constructor argument and trusted it. Hand it a predictable generator and the nonces repeat, which is the one thing AES-GCM cannot survive.

caller constructsthe cipher objectpasses in arandomness sourcesource generatesthe noncenonce repeatsacross messagesGCMconfidentiality…ATTACKER INPUTIMPACToutput is predictable
Attack path · caller constructs the cipher object → passes in a randomness source → source generates the nonce → nonce repeats across messages → GCM confidentiality collapses

A cipher class took its randomness source as a constructor argument and trusted it. Hand it a predictable generator and the nonces repeat, which is the one thing AES-GCM cannot survive.

What goes wrong

The class needed a source of randomness, so it accepted one as a constructor parameter and stored it. Reasonable on the surface, and clean by the usual standards: it is dependency injection, it is easy to test. The problem is that the nonce for every encryption now comes from whatever the caller handed over, and the class never checks what that was.

Feed it a generator seeded with a constant, or one that was already drained, and you get repeated nonces. Under AES-GCM a repeated nonce with the same key is catastrophic. An attacker who sees two ciphertexts under one nonce can recover the authentication key and forge messages, and the keystream reuse leaks the plaintext. This is a full break of the mode, not a slight weakening of it.

The wider point: a security primitive should own the ingredients that make it secure. Randomness is one of them. Accepting it from outside turns an internal invariant into a caller's promise, and callers break promises.

The pattern

Vulnerable
class SecureBox:
    def __init__(self, rng):
        self.rng = rng          # trusts whatever is passed in

    def seal(self, key, plaintext):
        nonce = self.rng.bytes(12)   # predictable if rng is
        return aes_gcm_encrypt(key, nonce, plaintext)
Hardened
class SecureBox:
    def __init__(self):
        self.rng = system_secure_random()   # owned internally

    def seal(self, key, plaintext):
        nonce = self.rng.bytes(12)
        return aes_gcm_encrypt(key, nonce, plaintext)
How the pipeline adjudicated itHELD
ABCDEFGH

Grounding and reachability were confirmed. The exploit-path gate would not conclude without the calling code, so the finding was held and an evidence request was raised instead of being reported as fact.

Ed25519 · per-tenant signing key · gate ledger A–H · verify offline against the published payload hash

Why it matters to you

If this ships in a wallet or a messaging path, one bad caller turns every message into a decryptable one. A test harness left wired into production. A well-meaning deterministic mode for reproducible builds. An attacker who reaches the injection point. You will not catch it in a unit test either, because the unit test is exactly the code that supplies the weak generator.

Check your own code