Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 3 min read

Why Standard AES-GCM Still Leaks Your Secret Length (And How to Fix It With WebCrypto)

Most "zero-knowledge" secret-sharing web apps stop at crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, plaintext) and call it a day. That protects the contents of your secret, but it still leaks two subtle things to

Most "zero-knowledge" secret-sharing web apps stop at crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, plaintext) and call it a day.

That protects the contents of your secret, but it still leaks two subtle things to any database observer or network sniffer:

  1. Exact plaintext length β€” AES-GCM is a stream cipher (CTR mode + GHASH). A 4-digit PIN produces a ~20-byte ciphertext, an API key produces ~50 bytes, and a 24-word recovery phrase produces ~180 bytes. Anyone inspecting your DB or network traffic can immediately classify what kind of secret was sent.
  2. Cross-context ciphertext substitution β€” without domain-separated Authenticated Associated Data (AAD), a ciphertext encrypted in one context (e.g., a file blob or metadata record) could be replayed in another context if keys were ever reused.

When building BurnNote (a client-side encrypted, burn-after-reading tool for passwords, API keys, and .env files), I wanted to fix both β€” and let skeptical developers verify the cryptography themselves without trusting a single line of our code.

1. Hiding Exact Length with CSPRNG Buckets

Instead of encrypting raw UTF-8 bytes directly, we pack every note into a deterministic binary frame:

  • First 4 bytes: big-endian uint32 exact plaintext byte length
  • Next N bytes: UTF-8 plaintext
  • Remaining bytes up to the bucket ceiling (512 B, 2048 B, 4096 B, 6144 B, ...): filled with crypto.getRandomValues()
function computeBucketSize(plainByteLength: number): number {
  const minFrame = 4 + plainByteLength;
  if (minFrame <= 512) return 512;
  if (minFrame <= 2048) return 2048;
  if (minFrame <= 4096) return 4096;
  if (minFrame <= 6144) return 6144;
  return Math.ceil(minFrame / 4096) * 4096;
}

function packLengthHiddenFrame(plainBytes: Uint8Array): Uint8Array {
  const targetSize = computeBucketSize(plainBytes.byteLength);
  const frame = new Uint8Array(targetSize);
  new DataView(frame.buffer).setUint32(0, plainBytes.byteLength, false);
  frame.set(plainBytes, 4);
  if (targetSize > 4 + plainBytes.byteLength) {
    crypto.getRandomValues(frame.subarray(4 + plainBytes.byteLength));
  }
  return frame;
}

Now a 4-digit PIN and a 24-word seed phrase produce the exact same 512-byte encrypted payload on the wire and in PostgreSQL.

2. Binding Domain-Separated AAD + Keeping the Key in #fragment

When calling WebCrypto, we pass an explicit additionalData tag (burnnote:v4:note:zk-envelope for notes, burnnote:v4:file:zk-blob for binary files):

const encryptedBuffer = await crypto.subtle.encrypt(
  {
    name: 'AES-GCM',
    iv,
    additionalData: new TextEncoder().encode('burnnote:v4:note:zk-envelope'),
  },
  aesKey,
  paddedFrame
);

And the 256-bit key is placed strictly inside the URL #key=... fragment. Per RFC 3986, web browsers never send the #fragment to the server in HTTP requests β€” so our server only ever stores v4:<iv_b64>:<cipher_b64>, and deletes it via an atomic PostgreSQL DELETE (FOR UPDATE) on the first read.

3. Don't Trust Claims β€” Verify Offline in 12 Lines of Vanilla WebCrypto

Anyone can write "military-grade encryption" on a landing page. That's why we built an interactive Trust & Verification Center where you can:

  1. Disconnect your Wi-Fi (or toggle Offline in DevTools) and encrypt/decrypt a test secret locally β€” proving 0% server involvement.
  2. Flip 1 bit of the ciphertext to watch AES-GCM reject the tampered payload immediately.
  3. Decrypt it yourself with zero BurnNote code by pasting 12 lines of pure browser WebCrypto into DevTools Console:
(async () => {
  const b64 = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
  const [, ivB64, ctB64] = CIPHERTEXT_V4.split(':');
  const key = await crypto.subtle.importKey('raw', b64(KEY_B64), 'AES-GCM', false, ['decrypt']);
  const frame = new Uint8Array(await crypto.subtle.decrypt(
    { name: 'AES-GCM', iv: b64(ivB64), additionalData: new TextEncoder().encode('burnnote:v4:note:zk-envelope') },
    key,
    b64(ctB64)
  ));
  const len = new DataView(frame.buffer).getUint32(0, false);
  console.log('Verified plaintext:', new TextDecoder().decode(frame.subarray(4, 4 + len)));
})();

Try It / Break It

If you spot any edge case in the cryptography, padding scheme, or browser hardening, I'd love to hear it in the comments!

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.