You've done this before. Someone sends you a link and a short phrase made of ordinary words, you paste both into a browser tab, and a chat box opens with no install and no account. You type the sentence you'd never put in email, press send, and assume the sealed box arrives intact.
Most explanations of AES-GCM encryption stop at "AES-256, so it's unbreakable." That's the least interesting part of the system. What decides whether your sentence stays private is the nonce, the tag, the associated data, and the browser you're typing in. Take them one at a time and the whole thing stops feeling like magic.
What AES-GCM Guarantees in Plain Terms
Picture a letter sealed in an envelope, with a pattern pressed into the wax that was computed from the letter's own words. The recipient doesn't open first and check later. They check the seal and the contents together, and if the pattern doesn't match the envelope goes in the bin unopened. Verify before you read. That single design decision separates authenticated encryption from older modes that only hid data.

AES-GCM is that envelope in digital form. It combines AES in counter mode, which generates a keystream and XORs it against your plaintext, with GHASH, a polynomial hash that authenticates both the ciphertext and any optional associated data. The NIST report on AES-GCM usage describes the result as confidentiality and integrity in one construction, where decryption must reject the ciphertext if the authentication tag does not validate, preventing undetected modification of messages, file metadata, or protocol fields.
Six parts make up the operation, and each has a job you can name out loud.
| Component | Secret? | Typical size | Role |
|---|---|---|---|
| Key | Yes | 128, 192 or 256 bits | Makes the transform secret; the same key must never be reused with a repeated nonce |
| Nonce (IV) | No | 96 bits recommended | Makes each encryption unique under the same key |
| Plaintext | Yes | Any length | The data being protected |
| Associated data | No, authenticated only | Any length | Binds context such as channel ID or message type to the record |
| Ciphertext | No | Same length as plaintext | Encrypted output of the counter-mode stream |
| Authentication tag | No | 128 bits typical | Proves the ciphertext and associated data are unchanged before any plaintext is returned |
Read that table as a contract rather than a menu. The key makes the transform secret. The nonce makes each encryption unique even though the key stays fixed. The associated data is the piece people skip: a channel identifier, a message type, a chunk index, sent in the clear but folded into the tag, so a record encrypted for one context can't be replayed into another. The same primitive appears well beyond chat, and if you want to see how authenticated modes sit beside the key models used in distributed ledgers, this overview of blockchain encryption methods is a useful comparison.
The promise in one line: if a single bit of ciphertext, associated data, or tag has changed, decryption returns an error instead of plaintext.
That's the guarantee. Not "the attacker can't read it" alone, but "the attacker can't change it without you noticing." This matters more in a chat than many expect, because a modified message that decrypts cleanly is worse than one that fails loudly.
AES-GCM protects data only when the key, nonce, associated data, and tag are handled as one set. Pull any one of them out of the set and the guarantee disappears.
How Encryption and Tag Checks Work Step by Step
Follow one message from keystroke to relay. Six steps, three to seal it and three to open it, and each exists for a reason you can trace back to the envelope.
- Generate a fresh nonce for this key. The nonce isn't secret and isn't random for its own sake. It exists so the keystream used for this message never repeats one used before under the same key. Uniqueness is the entire job.
- Encrypt in counter mode and compute the tag over the ciphertext plus the associated data. The tag doesn't cover the plaintext directly. It covers what will travel, so any alteration in transit gets caught.
- Send the nonce, ciphertext, and tag together, with the context supplied as associated data. The receiver needs all four to reconstruct the check. Drop one and the record becomes unverifiable.
- Recompute the tag with the same key, nonce, and associated data. The receiver runs GHASH over what arrived and compares it against the tag that was claimed.
- Compare in constant time. A comparison that exits early on the first mismatched byte leaks how much of a forged tag was correct, which is enough to grind a forgery out one byte at a time.
- Decrypt only after the tags match, and return nothing at all if they don't. No partial output, no best-effort plaintext. A rejected record produces an error, not a fragment.
Why does this stay affordable? The cost is smaller than most developers assume. A 2026 study published by the Association for Computing Machinery measured AES-GCM encryption of 50 MB at 26.711 ms and decryption at 27.830 ms on an Intel Xeon W7-3465X test platform. Your message is measured in bytes, so the tag check disappears next to the network round trip.
What the relay actually holds
The boundary matters more than the benchmark. In Ciphar the relay stores only the ciphertext, the initialization vector, the authentication tag, a salt, and an expiry timestamp for each message, with no keys, plaintext, or logs. Everything in steps one to six happens in your browser, on your device, before a packet leaves it.
The same arithmetic scales up when it has to. A 2026 GPU implementation study on arXiv reported AES-GCM GHASH throughput of 664 Gbps on an NVIDIA L40S, which is the identical construction that verifies your 200-byte message, just spread across far more parallel lanes.

Run three questions against your own code before you trust it:
- Is the tag verified before any plaintext exists? If your code decrypts first and checks afterwards, the plaintext already reached memory and possibly the UI.
- Is the nonce stored or transmitted with the ciphertext? A nonce you can't retrieve at decryption time means the record can't be verified later.
- Is the same associated data supplied on both sides? Encrypt with a channel ID and decrypt without it, and every record fails. Encrypt without it and decrypt with it, and binding is gone.
Choosing Key Sizes and Deriving Keys Safely
Key length is the easiest question here and it gets the most attention. AES comes in three sizes, and the extra work is modest: AES-128 runs ten rounds, AES-192 twelve, and AES-256 fourteen. More rounds means more CPU per block, which is why some throughput-bound systems still pick 128. For anything that has to stay confidential for years, default to 256 and stop deliberating.
Length isn't the hard part. Uniform randomness is. A human-chosen word is not a key no matter how long it looks, because the search space an attacker faces is the dictionary they'd guess from, not the theoretical one.
That's what PBKDF2 exists for. Three steps turn a readable passphrase into a real key.
- Combine the passphrase with a unique per-channel salt. The salt defeats precomputation and stops two channels that share a passphrase from producing the same key.
- Run PBKDF2-HMAC-SHA-256 for 100,000 iterations. Each iteration is deliberate work, paid on every guess an attacker makes, including the ones made on a GPU farm.
- Emit a 256-bit AES key. Fixed length, uniformly distributed, and short enough for the browser to handle without a delay you'd notice.
In Ciphar, the access key and the per-channel salt produce that 256-bit AES key in the browser, and the key stays in memory on your device. Nothing about it crosses the network, which is why the relay can hold ciphertext it has no way to open.
The lifecycle rules around a key matter as much as its bits. Keep one key per channel or purpose so a compromise in one conversation doesn't reach another. Draw random keys from the browser's native crypto API rather than anything you assemble by hand. Rotate when exposure is possible, not after it's confirmed. And plan for the lost-key case in advance, because a system without escrow cannot recover what you forget. If you want the longer version of this, the guide to how encryption keys work covers generation, storage, and rotation in more detail.
Why Nonce Reuse Breaks AES-GCM in Real Chats
Nonce uniqueness is the constraint everything else rests on, and it's the one chat implementations get wrong. Reuse a nonce under the same key and GCM's counter blocks come around again, so two ciphertexts can be XORed to expose exactly how the plaintexts differ. That alone is bad. The worse consequence, set out in NIST's revision material on GCM, is that an attacker can recover GCM's authentication subkey and forge ciphertexts with valid tags. AES-256's key never falls. Your integrity guarantee does.

Put that in chat terms. Someone who can forge a valid tag can inject a message your client accepts without a whisper of warning, or alter one in flight. The lock wasn't picked. The seal was copied.
Four strategies get used in practice, and they fail in different places.
| Strategy | How it works | Limit or risk |
|---|---|---|
| Random 96-bit per message | Fresh random value for each encryption | NIST limit of at most 2^32 invocations per key; collisions grow with volume |
| Strictly increasing counter | Increment a stored value for every encryption | Reuse after a crash, reload or second tab unless the counter is persisted and shared |
| Fixed prefix plus counter | Per-key, per-device or per-session prefix with a counter | Two devices with the same prefix collide unless the prefix is unique |
| Random per record, rekey often | Random nonce with key rotation before the limit | Rotation must happen before 2^32 invocations |
Random nonces have a ceiling
Random values are the convenient choice, and they run into a hard wall. NIST's revision material notes that random-nonce deployments are limited to at most 2^32 authenticated-encryption invocations per key. For text messages that's a number you'd struggle to reach inside one hour-long conversation. For voice it's less comfortable, because a call generates frames continuously for as long as someone is talking, and every frame needs its own nonce.
The fix isn't exotic. Use a deterministic construction, either a per-key fixed value combined with a strictly increasing counter, or rekey before the limit approaches. Deterministic construction makes collisions impossible rather than improbable, which is a better property to reason about at two in the morning.
Six objects, six rules
A chat has more encryption surfaces than a message box, and each one needs its own answer.
- Messages take the next persisted counter value, written down before the encryption, not after.
- File chunks bind a file identifier and a chunk index to a fresh counter value, so a chunk can't be reordered into another file.
- Edits and replies get a new counter value and never reuse the nonce of the message they modify.
- Voice frames use a per-stream sequence number, because a high frame rate plus random values is where collisions stop being theoretical.
- Retries resend the stored ciphertext, nonce, and tag. Encrypting again after a failed send is how one plaintext ends up under two nonces you can't track.
- Reconnects, reloads, a second tab, or a worker load and share the counter before the first encryption, not lazily after it.
The last rule is where real deployments break. A counter held in a variable that resets on reload is a counter that starts over.
The underlying requirement is simple to state. MDN's AES-GCM parameter reference says the IV must be unique for every encryption operation under the same key and need not be secret. Where implementations drift is in treating "unique" as a synonym for "random."
If you want the GHASH forgery argument built up visually, the walkthrough below is worth the time.
One more point that trips people up. A server-side deletion timer does not repair a nonce that was already reused. Once ciphertext exists under a repeated nonce, expiry deletes a file. It doesn't undo the exposure. Nonce discipline has to be correct at the moment of encryption, in the browser, which is the part you control. If the mechanics of IV construction are still fuzzy, this explanation of how an IV works builds it from the ground up.
Common Implementation Mistakes and How to Fix Them
With the key size settled and a nonce strategy chosen, what remains is library calls, and library calls are where most real bugs live. Six failures cover the majority of what you'll find in a review, ordered from forgery risk down to test coverage.
| Mistake | What it looks like | Fix |
|---|---|---|
| Truncating the tag | Tags shorter than 128 bits in a bandwidth-saving design | Keep the full 128-bit tag unless a named standard requires shorter |
| Assuming the library nonce length | Calls fail or uniqueness weakens when the default is not 12 bytes | Set a 96-bit nonce explicitly and assert its byte length |
| Writing a custom GHASH | Polynomial bugs still produce ciphertext that looks valid | Use a standard library AES-GCM implementation |
| Non-constant-time tag compare | Early reject leaks how many tag bytes matched | Use the library decrypt call and return plaintext only on success |
| Leaving out associated data | Records can be moved between channels or message types | Include channel ID and message type as associated data on both sides |
| No known-answer tests | A swapped nonce, tag or associated-data field ships unnoticed | Keep encrypt and decrypt test vectors in the suite |

The two that hide in plain sight
Tag truncation looks harmless because the ciphertext is unchanged. It isn't. A shorter tag means fewer bits an attacker has to guess, so a forged record gets accepted with higher probability, and the failure is silent. Keep 128 bits unless a named standard you're implementing explicitly specifies something shorter.
The constant-time comparison is the subtler one. Writing your own byte loop feels careful. It usually isn't, because returning early on the first mismatch turns a search over the whole tag space into a search one byte at a time. Let the library's decrypt call own that comparison and never produce plaintext except on success.
A useful habit: write a test that flips one bit in the ciphertext, one in the tag, and one in the associated data, and assert that all three throw. If any of them returns bytes, stop and fix the call site before anything else.
Start your audit with three checks on existing code: the tag length, an explicit 96-bit nonce with an asserted byte count, and a decrypt path that verifies before it outputs. Those three catch most of the damage, and none of them requires reading the rest of the codebase.
Decide Where Your Trust Boundary Sits
Everything above assumes an honest browser. That assumption carries more weight than the key length does, and it is the one nobody documents. A 256-bit key buys you nothing against a nonce that repeats or an endpoint that leaks plaintext. The Web Crypto API is available only in secure contexts such as HTTPS, and AES-GCM checks ciphertext integrity with real rigor. Neither property stops a compromised browser, a malicious extension, an injected script, or a hostile local device from reading plaintext before encryption or after decryption.
Two different promises
Confidentiality against the relay is one promise. Confidentiality against the endpoint is another, and marketing copy often treats them as the same thing. A ciphertext-only relay cannot decrypt what it stores. The browser that composed the message can still observe it while you type, and files and voice frames spend time in memory on their way to being encrypted.
AES-GCM is an authenticated-encryption primitive. A chat security model is the larger claim built around it.
Self-destruction lives on the relay side of that line. Deleting server-held ciphertext cannot erase screenshots, copied text, downloads, browser caches, memory remnants, or a recording made by the other participant. That limit belongs to the medium itself, and it is worth saying plainly to anyone weighing what to send.
Within a channel, admission is a cryptographic test rather than a password prompt. A new participant enters only if their browser derives the same key and successfully decrypts the channel's test blob; otherwise they never get in. Ciphar keeps no escrow, no recovery mechanism, and no backdoor, so a lost access key means the conversation is gone for everyone.
Working out what your code can prove about where it runs is a longer exercise, and MANDATE's guide to browser verification works through the client-versus-server question in practical terms.
Four checks before you trust a system
- Define the threat model. Are you protecting against the operator, a network observer, a colleague with your device, or all three? The answers point at different controls.
- Confirm the nonce strategy and where the counter lives. Ask how nonces are assigned across messages, file chunks, voice frames, and retries, and ask what happens on reload, in a second tab, and after a failed send.
- Confirm that tags are verified and associated data is bound. "We use AES-256-GCM" says nothing about whether a rejected record ever reaches the UI.
- Confirm which endpoint risks you accept. Extensions, shared machines, and update channels are decisions rather than accidents.
Run those four questions against any system before you hand it something sensitive. A short guide to what a threat model is helps you write the answers down instead of keeping them in your head.
If you want to watch these decisions in practice rather than read about them, Ciphar is a browser-based chat where every message, file, reply, and voice frame is encrypted with AES-256-GCM on your device before it touches the network, with the key derived locally from an access key and a per-channel salt through PBKDF2 at 100,000 SHA-256 iterations. That key stays in browser memory and never reaches the server. Channels are identity-free and self-destruct sixty minutes after creation, and the relay holds only ciphertext, IVs, auth tags, a salt, and an expiry timestamp. Open Ciphar and run the four checks above against it yourself.



