In a law firm's client-portal app with offline case documents, how should they be encrypted at rest, and what does a hardware-backed key defend against?
answer
- use the key, never read it
- a data key wrapped by a device key
- never the key beside the data
- copied files and backups stay dark
- on the phone, the key still works
basics
~20 sEncrypt documents and the database under a data key that a non-exportable, hardware-backed key protects, never stored beside them. That defeats copied files and backups read elsewhere, not code running as the app on an unlocked or compromised phone.
solid answer
~50 sEach document and the local database are encrypted with a symmetric **data key**, and the data key is stored only in wrapped form, encrypted by a key created inside the hardware-backed key store that the app can use but never read. At unlock the app asks the store to unwrap the data key and keeps it in memory for the session, so disk and backups hold only ciphertext and a wrapped key. Storing the data key in the database or preferences beside the data undoes all of it. What this buys: files copied off the phone, or a backup opened on another device, are unreadable, because the wrapping key never leaves this phone. What it does not: code running as the app on an unlocked phone can ask the store to unwrap, and an attacker with code on a rooted or jailbroken phone can use the key there, though not carry it away.
go deeper
Remember the slogan: use the key, never read it. The documents are encrypted with a key the app cannot export, and that key is never stored next to them.
Explain the envelope: a data key encrypts the files, a non-exportable device key wraps the data key, and the disk holds only ciphertext and the wrapped key.
Be exact about the threat table: copies and backups are defeated, code running as the app and a compromised phone are not, though the key never leaves the device.
Decide where this layer earns its cost: which data is encrypted under device keys, which relies on platform encryption, and what the server refuses regardless of what the phone holds.
## Two different stores on one phone A phone app has two system-protected places for secrets, and they do different jobs: - The **OS-protected credential store** holds small secrets, such as a refresh token, encrypted under keys the app cannot read, and hands the plaintext back to the app when asked. On one platform it is a system store for such items; on the other it is built from the key store, by keeping the secret encrypted under a key-store key. - The **hardware-backed key store** holds **keys** that the app *uses* but does not *read*. The app sends data in and gets ciphertext, plaintext or a signature back; the key material stays in a system process or in separate secure hardware. The platforms draw this line slightly differently. One describes key material that never enters the app's process at all and, when bound to secure hardware, never leaves that hardware. The other describes a key created inside a separate hardware key manager that the app never handles in plain text, while a key kept as an ordinary credential-store item is briefly copied into memory in plain text whenever it is used. The working rule is the same on both: **use the key, never read it**. ## The envelope design Encrypting every document directly with the hardware key is rarely practical: each operation is a call into the store, and one platform's secure hardware handles only one elliptic-curve key type, meant for signatures and key agreement. The portal therefore uses an envelope: 1. Generate a random symmetric **data key** for the document cache and the local database. 2. Protect it with a **wrapping key** created inside the hardware-backed store: non-exportable, bound to this device, usable only while the phone is unlocked. 3. Store only the **wrapped** data key on disk, beside the ciphertext. Without the wrapping key it is noise. 4. When the client opens the app, ask the store to unwrap the data key, hold it in memory for the session, and encrypt or decrypt each document with it. 5. To rotate or destroy access, touch only the small wrapped key, never gigabytes of files. Cipher modes, nonces and integrity checks have their own rules; the device design decides where each key lives and what the disk holds. ## What it defends against | Threat | Outcome with the envelope | |---|---| | Files copied off the phone and opened on a laptop | Ciphertext and a wrapped key: unreadable | | A device backup restored onto another phone | The same: the wrapping key never left the original phone | | Another app on the same phone | Blocked by the sandbox, and the data is ciphertext anyway | | Code running as the app on an unlocked phone | Can ask the store to unwrap: **not defended** | | Attacker code on a rooted or jailbroken phone | Can use the key on that phone while it runs there; cannot copy the key off | | Someone holding the unlocked phone with the app open | Sees what the app shows: encryption at rest does not apply | The first two rows are the reason to do this at all. The middle rows are what a strong candidate volunteers before being asked. ## What rooting or jailbreaking exposes A compromised operating system removes the sandbox, so the honest list is: - Every plain file and preferences entry is readable. - Credential-store secrets can be read the way the app reads them, by code that runs as the app or with more rights than it. - Hardware-backed keys can be **used** on that phone but not **extracted**; once the attacker loses access to the phone, any ciphertext they copied is worthless. - A key that requires the user to authenticate still demands it where secure hardware enforces the requirement; one platform states that compromising the app's process after the key was created cannot bypass it. - Whatever the app decrypts while the attacker is present is exposed; encryption at rest does not protect data in use. Spotting a compromised device and reporting it to the server is a separate design. This one limits the damage on the assumption that the spotting fails. ## Mistakes that undo it - Keeping the data key, or a password it is derived from, in the database or preferences beside the data. - Relying on the platform's own disk encryption alone: it protects a locked or powered-off phone, but it gives the app no way to make every copy unreadable on demand, or to cut off access by destroying one key. - Sharing one data key across accounts on a shared phone, so one client's sign-out cannot erase only their documents. - Treating "encrypted" as "safe from the app's own code".
- Why wrap a data key instead of encrypting every document with the hardware key directly?Every hardware operation is a call into the key store, which is slow for bulk data, and one platform's secure hardware supports only an elliptic-curve key used for signatures and key agreement. A symmetric data key encrypts files fast; the hardware key is used once per unlock to unwrap it; and rotating or destroying access means replacing one small wrapped key instead of re-encrypting the whole cache.
- Should the app also use the platform's own file encryption, where the app can choose when a file is readable?Yes, as an extra layer: it keeps files closed while the phone is locked at almost no cost. It does not replace the app's own key, because the app cannot make every copy unreadable on demand with it, and how a platform-protected file travels in a backup is the platform's choice. The app's device-bound key covers both.
- Keep the encryption key in the database next to the data it protects.
- The OS disk encryption alone keeps a copied file unreadable elsewhere.
- A hardware-backed key cannot be used by an attacker on a rooted phone.
- Root access lets an attacker copy a hardware-bound key off the device.
- Data encrypted at rest is safe from the app's own compromised code.