WebCrypto and non-extractable keys
A key the page cannot read
crypto.subtle is cryptography built into the browser: AES-GCM, ECDH, signatures, HKDF and PBKDF2 are
computed by the browser, not by the page’s JavaScript. A key here is not an array of bytes but a
CryptoKey object — an opaque handle to material the browser manages. At generateKey you fix, once and
for all, whether the key can be exported (extractable) and what it may be used for (usages). A key with
extractable: false encrypts and decrypts, but exportKey and wrapKey reject it with
InvalidAccessError.
You can store a CryptoKey in IndexedDB like any other object. The browser keeps the key itself together
with its extractable flag and its usages, and after a reload it hands back a working, still
non-extractable key. In the toy you encrypt a message (AES-GCM with a fresh IV),
reload the page and decrypt it with a key whose bytes the page has never seen. Turn extractable on and
generate a new key, and “Export the key” hands out its 32 bytes. “Delete the key” removes the key from
IndexedDB.
A key the page cannot read
You decide once, at generateKey. A non-extractable key cannot be “unlocked” later.
Limit 1 KB. We count UTF-8 bytes — “é” is 2 bytes.
The message is empty. Type at least one character.
Checking IndexedDB for a key from your last visit…
A script on the page and a reused IV
Key theft via XSS — resistant, but only to taking the key out. In the toy’s “Break it” mode above you
are a script that got onto the page. It runs in the page’s origin, so it opens the same IndexedDB database
and gets the same CryptoKey. exportKey refuses, but decrypt and encrypt work: while the script runs
on the page, the key is its oracle — it reads every message within reach and forges new ones with a valid
tag. All extractable: false buys is that once the page is closed, the attacker has no key. The Web
Crypto API specification likens script injection to remote code execution and names CSP and TLS as the
mitigations.
Reused IV — vulnerable if misused. encrypt accepts any IV of an allowed length and does not remember
which ones you have used. A common mistake looks innocent: the IV comes from a cryptographic generator, but only once, at
start-up, instead of on every call.
// ✕ IV drawn once, then the same in every call
const iv = crypto.getRandomValues(new Uint8Array(12));
const save = (data) => crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data);// ✓ a new IV in every call, stored next to the ciphertext
async function save(data) {
const iv = crypto.getRandomValues(new Uint8Array(12));
return { iv, ciphertext: await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data) };
}No call throws, and each ciphertext on its own looks random. Only two side by side show what happened: the same key and IV give the same keystream, so wherever the messages share bytes the ciphertexts share them too, and C₁ ⊕ C₂ = P₁ ⊕ P₂. How to read the whole second message from that, and why tags can then be forged, is shown at the AES-GCM station. A non-extractable key does not help here — the attacker needs only the ciphertexts.
Two encrypt calls, one key
Each encrypt call gets its own IV: crypto.getRandomValues(new Uint8Array(12)).
The message is empty. Type at least one character.
The message is empty. Type at least one character.
Two messages, one non-extractable key. Encrypt both and compare the ciphertexts.
Where non-extractable keys live
- Web Cryptography APIThe W3C specification requires only that key material is not exposed to script except through
exportKeyandwrapKey. It states plainly that it does not guarantee the material will not be persisted to disk, possibly unencrypted, nor that it is inaccessible to users or other applications with the same privileges as the browser.Source (Web Cryptography API) - Element (Matrix)The web client generates a non-extractable AES-256-GCM key and stores it in IndexedDB next to the “pickle” key it encrypts, which then decrypts, among other things, access tokens — the same pattern as in the toy.Source (Element (Matrix))
- OAuth 2.0 DPoPThe client binds tokens to its signing key, which in a browser can be non-extractable. RFC 9449 says it plainly: an attacker running code in the client cannot exfiltrate such a key, but can create new proofs of possession with it as long as the client is online.Source (OAuth 2.0 DPoP)
- Safari (ITP)Since 2020 Safari deletes all of a website’s script-writable storage, IndexedDB included, after seven days of Safari use without user interaction on that site; web apps added to the home screen keep their own count. A key in IndexedDB is not a vault forever.Source (Safari (ITP))
No export for the key, a new IV for every call
Generate keys in the browser with extractable: false and the narrowest usages you need — a key that only
wraps other keys gets ['wrapKey', 'unwrapKey']. Keep the CryptoKey object in IndexedDB, not its bytes.
Draw the IV on every encrypt and store it next to the ciphertext. The only thing that stops a foreign script
from using the key is keeping such scripts off the page: CSP, Trusted Types, care with third-party code.
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
false, // extractable: the key bytes never reach JavaScript
['encrypt', 'decrypt'],
);
db.transaction('keys', 'readwrite').objectStore('keys').put(key, 'main'); // the CryptoKey object
const iv = crypto.getRandomValues(new Uint8Array(12)); // on every encryption
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data);The Web Crypto API standard has no Argon2id, ChaCha20-Poly1305 or post-quantum algorithms — the WICG draft “Modern Algorithms in the Web Cryptography API” adds them, and it is not a W3C standard. Turn passwords into keys with an Argon2id library (e.g. in WebAssembly) or with WebCrypto’s PBKDF2 at a high iteration count, never with a single hash.
Do not set extractable: true “just in case”, and do not store the raw key in localStorage or a cookie —
any script on the page can read it. A non-extractable key cannot be wrapped with wrapKey either, so if you
need a backup, plan it from the start. Do not treat extractable: false as a hardware key store: the key
material may sit in the browser profile on disk, and clearing site data deletes IndexedDB, key included.
// ✕ the key as bytes in localStorage: any script on the page can read it
const raw = await crypto.subtle.exportKey('raw', key);
localStorage.setItem('key', btoa(String.fromCharCode(...new Uint8Array(raw))));