# Encryption of stored data

> What the realm seals under its own storage key, what it keeps only as a one-way hash, and how to seal a field of your own.

The realm seals its most sensitive stored data under a key of its own, the **storage key**, so a copy of the database
alone gives none of it away. Passwords and client secrets are never stored at all, only one-way hashes of them. And
any field you add to an object type can be sealed the same way.

The storage key is the realm's own, kept beside its [signing keys](/platform/keys/signing-keys/), and no API returns
it.

## Before you begin

- **Nothing to do** for the data the realm seals itself: every realm seals it from birth.
- **To seal a field of your own:** `MODIFY` on that object type, which `RealmOwner` gives, through the API at
  `https://foobar.cvyuh.com/api/v0/am/types/v1/<type>`. Your session is in `$CVYUH_SESSION`
  ([Call the API](/develop/api/#get-a-session)).

## How it works

The realm seals with ChaCha20-Poly1305 under its storage key, `store/encryption`. Each sealed value records the key
version that sealed it and is bound to its realm and field, so it can't be moved to another realm or field and still
open.

| Data | How it is kept |
|---|---|
| A sign-in in progress, and its deadline | Sealed |
| Tokens a session holds for other services | Sealed, bound to the session and its holder |
| The private half of the realm's signing keys | Sealed |
| A signal stream's `authorizationHeader`, a device's seed, a directory's `bindPassword` | Sealed |
| Sign-in codes, refresh tokens and challenges that travel through the browser | Sealed, and not stored at all |
| A session | Stored under a hash of its id, never the id itself |
| A user's password, a client's secret, a registration token's secret | One-way hash only (bcrypt or PBKDF2) |

A value that can't be sealed is refused, never stored in the clear.

## Set it up

To seal a field of your own, mark it in its object type with `x-encrypt`, naming the realm key that seals it, and
make it private:

```json
"apiKey": {
  "type": "string",
  "scope": "private",
  "x-encrypt": { "key": "store/encryption" }
}
```

For a value you only ever need to check, never read back, use `"x-hash": { "algorithm": "bcrypt" }` instead. Send
the changed type with `PUT https://foobar.cvyuh.com/api/v0/am/types/v1/<type>`. From then on, every write seals or
hashes the field; values written before stay as they were until they're written again.

## What happens when it fails

A type change is refused with `400`, naming the field, when:

| The field | What to do |
|---|---|
| Isn't a string | Seal or hash only text fields |
| Is `x-encrypt` without `"scope": "private"` | Make it private: a sealed value is never returned |
| Names a key that isn't one of the realm's, or no key | Name `store/encryption` |
| Is marked both `x-encrypt` and `x-hash` | Choose one |

What users see: nothing. A sealed field never appears in a read; a hashed one is checked, never shown.

## Limits and defaults

| What | Default | Can it change |
|---|---|---|
| Cipher | ChaCha20-Poly1305 | No |
| One-way hashes | bcrypt or PBKDF2 | Choose per field with `x-hash` |
| Fields that can be sealed or hashed | Text fields only | No |
| Storage key versions | Kept while anything sealed under them may exist | No |

## Troubleshooting

- **A field you sealed comes back empty in reads.** It's private, so no read returns it. That's the point: read the
  value only where your app needs it, through the feature that uses it.
- **Older values aren't sealed.** Sealing applies from the type change on. Write each value again to seal it.

## Audit record

A change to an object type is recorded on the audit trail as an `UPDATED` record for the type, naming who made it; no
API returns type changes yet. Sealed and hashed fields never reach the audit trail: a record of a change to an object
leaves them out.

## Good to know

- **The storage key rotates by version.** A new version seals from then on; older versions are kept to open what they
  sealed. `GET https://foobar.cvyuh.com/api/v0/am/keys/v1` lists its versions, the newest `valid` and the rest
  `retained`.
- **It is never destroyed while anything sealed under it may exist.** A realm's storage key goes only when the realm
  itself is purged.
- **Secrets in a vault are sealed separately**, under keys per owner: see
  [Sealing per owner](/secrets-store/keys-and-protection/sealing/).
- **You can't hold the key that wraps your realm's stored data** in a key store of your own; see
  [Not offered](/reference/not-offered/#platform).

## Related

- [Signing keys](/platform/keys/signing-keys/): the listing that shows the storage key's versions.
- [Crypto-shredding](/secrets-store/keys-and-protection/crypto-shredding/): destroy the realm's secrets key, and every
  secret with it.
- [Retention and clean-up](/platform/data/retention/): how long sealed data is kept.
