- Security
- Web Crypto
- TypeScript
A document vault even we can’t open: client-side encryption in RightUni
RightUni students keep passports and bank statements in a vault that only they can read. Files are encrypted in the browser with AES-256-GCM, so the server only ever stores ciphertext.
Toukir Ahmed Rony · · 3 min read
RightUni is a free study-abroad organiser I led the build of for SETS Tech. Students search universities, track their applications and follow the visa steps. Most of them are from Bangladesh, India and Pakistan, and before RightUni many of them sent their passports and bank statements around by email and WhatsApp.
We wanted to give them one safe place for those documents. But "safe" can mean two very different things.
Two kinds of "encrypted"
Most systems encrypt files on the server. The file arrives readable, the server encrypts it and stores the key. That protects you if someone steals the disk, but the company running the server can still read every file.
For RightUni I wanted something stronger: files that nobody except the student can read, including us. That means encrypting in the browser, before the file is uploaded. This is often called client-side or zero-knowledge encryption.
The key structure
Everything happens in the browser with the Web Crypto API:
- The student chooses a passphrase. In the browser, it goes through PBKDF2 (SHA-256) with 600,000 iterations to make a passphrase key.
- The browser creates a random vault key. The passphrase key locks it.
- The student is shown a recovery key once. It can also unlock the vault key.
- Every file, and every new version of it, gets its own key, which is locked by the vault key.
- Each file is encrypted with AES-256-GCM before upload.
This is the code that creates a new vault, in the browser:
/** Creates a vault: random vault key, wrapped by the passphrase and by a new recovery key. */
export async function createVault(passphrase: string, iterations = KDF_ITERATIONS): Promise<NewVault> {
const raw = random(32);
const salt = random(16);
const recovery = random(32);
const vaultKey = await toVaultKey(raw);
return {
vaultKey,
recoveryKey: formatRecoveryKey(recovery),
material: {
kdf: KDF,
kdfIterations: iterations,
salt: toB64u(salt),
wrappedKeyByPassphrase: await seal(await derivePassphraseKey(passphrase, salt, iterations), raw, AAD.vaultKey),
wrappedKeyByRecovery: await seal(await aesKey(recovery), raw, AAD.vaultKey),
keyCheck: await seal(vaultKey.key, enc.encode(KEY_CHECK_TEXT), AAD.check),
},
};
}Only the material part is ever sent to the server. The vault key itself stays in the browser, and the recovery key is shown to the student once.
The server stores only encrypted files, encrypted document details and the locked keys. It never sees the passphrase, a readable key or a readable file.
The layers are there for practical reasons. Because each file has its own key, files can be handled one at a time. And because the vault key sits in the middle, a student can change their passphrase without re-encrypting every document.
Why PBKDF2 and not Argon2id
On the server, passwords are hashed with Argon2id, which is the stronger choice today. So why not use it for the vault too?
Because the vault key is made in the browser. Using Argon2id there would mean compiling it to WebAssembly and shipping a crypto library to every student. PBKDF2 is built into the Web Crypto API, so no extra crypto code is shipped at all.
The trade-off is real: PBKDF2 isn't memory-hard, so it resists GPU guessing less well than Argon2id. The high iteration count is there to make up for that as far as possible, and it is stored with each vault, so it can be raised later.
The hard trade-off
Zero-knowledge has a cost that you have to explain to users honestly. If a student loses both their passphrase and their recovery key, nobody can get their files back. Not the student, not the support team, not me. The only option left is "Reset vault", which deletes the old files and starts an empty vault. There is no reset that opens the old one, because that would mean we could read it.
There are smaller costs too. The server can't scan a file or show a preview, because it can't read it.
For RightUni I think the trade is right. These are passports, transcripts, offer letters and bank statements. Students are trusting a new service with them, and "not even RightUni can read your documents" is a promise we can actually keep.
What I took from it
Security choices are product choices. Encrypting in the browser changed the sign-up flow, the support process and what features are possible. It was worth it here, because the whole point of the vault is that students control their own documents.
