# Vaultbook encrypted journals: the file format

Written so that an encrypted journal can be read without Vaultbook. `Tools/vaultbook-decrypt.swift`
is an independent reading of this document, and `Checks/run.sh crypto` checks it against files the
app writes. Anyone with the folder and either the passphrase or the recovery code can decrypt it.

## The key file: `.vaultbook/encryption.json`

JSON. Binary fields are Base64; dates are seconds since 1 January 2001 (Foundation's default).

| Field | Meaning |
|---|---|
| `format` | 1 |
| `keyID` | 8 bytes: the first 8 of SHA-256("vaultbook key id" ‖ vault key) |
| `passphrase` | a wrap: `salt` (16 bytes), `iterations`, `sealedKey` |
| `recovery` | the same, for the recovery code |

To unwrap the vault key from a wrap:

1. Wrapping key = PBKDF2-HMAC-SHA256(secret, salt, iterations, 32 bytes).
   - For the passphrase, the secret is the passphrase as UTF-8 in Unicode NFC.
   - For the recovery code, it is the code in its canonical form: upper case, the letters
     O, I and L read as 0, 1 and 1, grouped as six groups of five joined by `-`
     (for example `7K2QX-9MWDP-...`).
2. `sealedKey` is ChaCha20-Poly1305 combined: nonce (12) ‖ ciphertext (32) ‖ tag (16), with
   associated data `vaultbook key` (UTF-8). Opening it gives the 32-byte vault key.

The recovery code is 28 random characters and 2 check characters in Crockford Base32
(`0123456789ABCDEFGHJKMNPQRSTVWXYZ`). Check: s = Σ (i+1)·value(cᵢ) mod 1021 over the first 28,
then the two characters are value s/32 mod 32 and s mod 32.

## Encrypted files: `*.vbe`

An encrypted journal's files are in its folder under random names: 32 hex characters and `.vbe`,
for example `3f9a…c2e1.vbe`. The real name is inside, so the folder shows neither the dates of
entries nor what photos are called. `.vaultbook-journal.json` in the folder says the journal is
encrypted and with which key id. Nothing outside a journal's folder is ever encrypted.

Header, 29 bytes:

| Bytes | |
|---|---|
| 0–3 | `VBE1` |
| 4 | version, 1 |
| 5–12 | key id (as in the key file) |
| 13–16 | chunk size, little-endian (1 048 576) |
| 17–28 | nonce base, random per file |

Then the chunks: each is ciphertext (up to the chunk size) followed by a 16-byte tag. Every chunk
but the last is exactly chunk size + 16 bytes; the last is whatever is left (at least 16 bytes,
even for an empty file).

Chunk *n* (from 0) is ChaCha20-Poly1305 with:

- key: the vault key;
- nonce: the nonce base with bytes 4–11 XORed with *n* as a big-endian 64-bit number;
- associated data: the 29-byte header ‖ *n* as big-endian 64 bits ‖ one byte, 1 for the last
  chunk and 0 otherwise.

The last-chunk byte means a file cut short at a chunk boundary fails to open, and nothing can be
added after the last chunk. The plaintext is the chunks' plaintexts in order, and is:

| | |
|---|---|
| 4 bytes | length of the name, little-endian |
| name | UTF-8: the file's path inside the journal folder, such as `2026-11-05 094003.md` or `attachments/beach.jpg` |
| the rest | the file itself |
