# Health data encrypted at rest — `MMO\core\helpers\FieldCipher`
The properties classified `sante` (mmo.core's `#[Classification]`) that a project lists are encrypted by the
application before the repository writes them, and decrypted when it reads them (ISO/IEC 27001 A.8.24). What the
database, its dumps, its copies and its journal hold of them is ciphertext. **Disabled by default.**
```json
"MMO\\core\\helpers\\FieldCipher": {
"enabled": false,
"key": "1",
"keyFolder": "{configs}/keys",
"fields": {"Acme\\entities\\Visit": ["lesions", "comments"]}
}
```
- `fields`: per entity class, top-level properties (a parent's apply to its children). Each must be classified
`sante`, else the configuration is refused at the first write.
- libsodium `sodium_crypto_secretbox` (XSalsa20-Poly1305), a random nonce per value. The value, of any type, is
json-encoded and stored as one string, `mmo:enc::`. null stays null. A value
changed, or read with another key, is refused.
- Keys: one file per version, `{keyFolder}/fields-.key` (32 random bytes, base64, mode 600), outside the database
and outside git. `key` names the one that encrypts; every key found decrypts. A missing key is an error, never a new
key (`createKey: true` writes one, for a new installation only). **A lost key loses the data**: back it up apart
from the database.
- Rotation: a new file `fields-2.key`, `"key": "2"`, then the project re-encrypts what is stored (my.oviepro:
`scripts/patch/encrypt-sante-32.php --apply --rotate`), then the old file can be removed.
- Reading decrypts whatever is encrypted, enabled or not: turning it off leaves the data readable. The values stored
before it was turned on stay in clear until the project's migration encrypts them.
The repository calls it: mmo.mongodb's `MongoRepository` encrypts what it inserts and updates (the log of the write
holds the ciphertext too), and decrypts what it loads. mmo.pdo does not yet.
## What it costs
A field encrypted can no longer be **filtered, sorted, grouped, indexed nor searched** by the database: the
database sees one random string. List only what is read, not searched (a lesion, a comment, a medical restriction),
and check the project's queries, statistics and exports first. A value holding references or dates is stored as json:
list plain values only.