Passphrase key wrapping
PBKDF2-SHA256 key derivation and AES-GCM key wrapping. Use this to protect an AEK with a user passphrase, optionally hardened with a Secret Key.
Signatures
Section titled “Signatures”wrapKey(passphrase: string, key: CryptoKey, secretKey?: Uint8Array): Promise<{ wrappedKey: ArrayBuffer, salt: Uint8Array }>unwrapKey(passphrase: string, wrappedKey: ArrayBuffer, salt: Uint8Array, secretKey?: Uint8Array, extractable?: boolean): Promise<CryptoKey>rekey(oldPassphrase: string, newPassphrase: string, wrappedKey: ArrayBuffer, salt: Uint8Array, secretKey?: Uint8Array): Promise<{ wrappedKey: ArrayBuffer, salt: Uint8Array }>rekeySecretKey(passphrase: string, oldSecretKey: Uint8Array, newSecretKey: Uint8Array, wrappedKey: ArrayBuffer, salt: Uint8Array): Promise<{ wrappedKey: ArrayBuffer, salt: Uint8Array }>Example: account setup
Section titled “Example: account setup”import { wrapKey, unwrapKey, rekey } from '@consealdev/conseal'
// Account setup: generate AEK, wrap with passphrase, store wrapped key + salt on serverconst aek = await crypto.subtle.generateKey( { name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt'])const { wrappedKey, salt } = await wrapKey('my-strong-passphrase', aek)// POST { wrappedKey: btoa(...), salt: btoa(...) } to server
// New device: fetch wrappedKey + salt, unwrap with passphraseconst recovered = await unwrapKey('my-strong-passphrase', wrappedKey, salt)
// Passphrase change — no files need re-encryptionconst { wrappedKey: newWrapped, salt: newSalt } = await rekey('old-pass', 'new-pass', wrappedKey, salt)Example: with Secret Key (recommended)
Section titled “Example: with Secret Key (recommended)”import { generateSecretKey, wrapKey, unwrapKey, rekey, rekeySecretKey } from '@consealdev/conseal'
const sk = generateSecretKey()// store sk in localStorage and keep an offline copy
const aek = await crypto.subtle.generateKey( { name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt'])const { wrappedKey, salt } = await wrapKey('my-passphrase', aek, sk)const recovered = await unwrapKey('my-passphrase', wrappedKey, salt, sk)
// Passphrase change — Secret Key stays the sameconst { wrappedKey: newWrapped, salt: newSalt } = await rekey('old-pass', 'new-pass', wrappedKey, salt, sk)
// Secret Key rotation (compromise event only) — passphrase stays the sameconst newSk = generateSecretKey()const { wrappedKey: rotated, salt: rotatedSalt } = await rekeySecretKey('my-passphrase', sk, newSk, wrappedKey, salt)wrapKey()requires the key to haveextractable: true. Throws ifkey.extractableis false.unwrapKey()returnsextractable: falseunlessextractableistrue— safe for IndexedDB storage. Ask for an extractable key only to transfer it, as the Circle ceremony does.- PBKDF2 at 600,000 iterations takes ~200–500ms — this is intentional offline brute-force protection.
- The
saltis not secret. Store it alongsidewrappedKey. wrappedKeyis the 12-byte AES-GCM IV followed by the ciphertext and its tag. A wrong passphrase or Secret Key fails the tag and throws.- When a
secretKeyis provided, it is combined with the passphrase by HMAC-SHA-256, keyed by the Secret Key, before PBKDF2 runs. An attacker who stealswrappedKeyandsaltcannot brute-force without also knowing the Secret Key. - Wrapping is AES-GCM, not AES-KW (RFC 3394), the algorithm made for it. WebKit doesn’t enforce AES-KW’s integrity check, so a wrong passphrase could unwrap to garbage instead of throwing. AES-GCM’s tag is checked on every engine.
rekeySecretKeyis a rare, heavyweight operation — use it only when the Secret Key is compromised. It requires re-enrolling all devices.