I maintain a small library that encrypts what a web app keeps in localStorage. It uses AES-256-GCM, so a stored value that has been modified is rejected rather than returned. For a year I read that sentence as "the stored data cannot be tampered with".
It can. Not by changing a value: by moving one.
The attack
Two values, same password, two keys:
import { SecureStorage, MemoryStorage, AesGcmEncryptor } from '@azemzemi/cryptobox';
const backend = new MemoryStorage();
const storage = new SecureStorage(backend, new AesGcmEncryptor(), { bindToKey: false });
await storage.setSecure('role:guest', { admin: false }, password);
await storage.setSecure('role:admin', { admin: true }, password);
Now play the attacker. You do not know the password. You cannot read either value, and you cannot forge a new one. You can only write to the store, so you copy one ciphertext over the other:
await backend.setItem('role:guest', await backend.getItem('role:admin'));
await storage.getSecure('role:guest', password); // { admin: true }
No error. The ciphertext is authentic: it was produced with the right key and no bit of it changed. GCM did its job. The library returned a value that is perfectly valid and sits in the wrong place.
{ bindToKey: false } above reproduces what version 1 did everywhere. Nothing in the ciphertext knew which storage key it belonged to.
Who can write to your storage without your password
This is the right question to ask before caring. An attacker who runs script in your page (XSS, a compromised dependency) does not need this trick: they see the password when it is typed and the plaintext when it is decrypted. No storage encryption helps there.
The swap matters for the cases storage encryption is actually for, where someone reaches the bytes but not the running application:
- a copied or restored browser profile,
- a synced storage area or a backup,
- a shared device,
- a server-side key-value store or a database dump, if you use the same format there.
In all of them the promise "they cannot read it and they cannot change it" was only half true. They could rearrange it.
The fix: tell the cipher where the value lives
AES-GCM takes a second input next to the plaintext, the additional authenticated data. It is not encrypted and not stored. It is mixed into the authentication tag, and decryption fails unless the reader supplies exactly the same bytes.
So the storage key goes in there:
// write
crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: header + 'role:admin' }, key, data);
// read, under the key the value was found at
crypto.subtle.decrypt({ name: 'AES-GCM', iv, additionalData: header + 'role:guest' }, key, data);
// throws: the tag does not match
The ciphertext is the same length as before. The binding costs nothing to store, because the reader already knows the key it is reading from.
With the default settings in version 2, the same swap ends differently:
const storage = new SecureStorage(backend, new AesGcmEncryptor());
// same writes, same copy
await storage.getSecure('role:guest', password); // CryptoBoxError: DECRYPTION_FAILED
The same mechanism authenticates the envelope header, which records the format version and the PBKDF2 iteration count. Lowering the count in a stored value, to make the password cheaper to guess later, now breaks the tag too.
What it still does not do
Binding to the key stops a value from being moved. It does not stop these:
- Rollback. Yesterday's value for the same key, put back in place, still decrypts. The format has no notion of freshness. If that matters, keep a counter or a timestamp inside the payload.
- Deletion. Whoever can write can also erase. Tampering is detected, not prevented.
- Old data. Values written by version 1 carry no binding. They stay movable until you rewrite them, and the library keeps accepting them until you tell it to stop. Migration is one call per key, then one option to close the door.
And the library has not been audited by a third party. What it has instead is a second implementation: the test suite writes and reads the same format with OpenSSL through node:crypto, in both directions, and a set of frozen ciphertexts that must keep decrypting forever.
Try it
There is a page where you can do this to your own browser: store a value, look at what localStorage holds, then read it back with the wrong password, after flipping one bit, and after moving it to another key.
- Demo: https://adelzemzemi.github.io/CryptoBox/
- Source: https://github.com/adelzemzemi/CryptoBox
-
npm install @azemzemi/cryptobox(zero dependencies, about 6.5 kB gzipped)
If you encrypt anything keyed by a name, an id or a path, check what your ciphertext knows about where it lives. Mine knew nothing.
Top comments (0)