Encrypt user data
With productKey your app encrypts a user's data in the browser, and your server stores only ciphertext. Neither your server, nor a leak of your database, nor Novren ID can read it.
Encrypt and decrypt text
const encoder = new TextEncoder()
const decoder = new TextDecoder()
/**
* `productKey` is the AES-GCM 256 key from completeLogin / embedLogin. `label` binds the
* ciphertext to its place (user + record), so it can't be moved to another record unnoticed.
*/
export async function encryptText(productKey: CryptoKey, text: string, label: string): Promise<string> {
const iv = crypto.getRandomValues(new Uint8Array(12)) // new for EVERY encryption
const sealed = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: encoder.encode(label) }, productKey, encoder.encode(text))
const out = new Uint8Array(12 + sealed.byteLength)
out.set(iv)
out.set(new Uint8Array(sealed), 12)
return btoa(String.fromCharCode(...out))
}
export async function decryptText(productKey: CryptoKey, stored: string, label: string): Promise<string> {
const bytes = Uint8Array.from(atob(stored), (c) => c.charCodeAt(0))
const plain = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: bytes.subarray(0, 12), additionalData: encoder.encode(label) },
productKey,
bytes.subarray(12),
) // throws if the data or the label were changed
return decoder.decode(plain)
}
// Usage: the label names the user and the record.
// const body = await encryptText(login.productKey, note.text, `notes:${login.accountId}:${note.id}`)The rules that matter
- A new IV for every encryption. AES-GCM with the same key and the same 12-byte IV twice breaks confidentiality.
crypto.getRandomValuesfor every call, as above. - Bind ciphertext to its place with
additionalData(thelabel): user, kind of record, record id. A ciphertext moved to another record (by a bug or by someone with access to your database) won't decrypt. - Encrypt before the request, decrypt after the response. The plain text exists only in the browser.
- Version your format. Add a version byte or a field next to the ciphertext, so you can change algorithm or layout later.
Large files
For files, don't encrypt everything in one call: split into chunks (for example 1 MiB), encrypt each with its own IV and a label containing the chunk index and a final-chunk flag, so chunks can't be reordered or truncated.
Search and sorting
Your server can't search or sort encrypted fields. Common choices: keep non-sensitive metadata in clear (dates, sizes), or search on the client after decrypting.
Sharing between users
To send data to another user of your app, use their public identity from the directory, checked with verifyDirectoryEntry. The encryption functions for sealing to an identity (X-Wing) arrive with the public packages; until then, write to us if you need them.
When decryption fails
crypto.subtle.decrypt throws OperationError when the ciphertext was changed, the label is different, or the key is different. The last case happens after an account recovery by email, which creates new keys. See The app key.