Files
CustosPublic/docs/FORMAT.md
T

182 lines
7.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# The `.custos` vault file, format version 1
One file holds one vault. Everything after the header is one XChaCha20-Poly1305 ciphertext.
The header is not secret, but it is authenticated: changing any byte of it makes the body fail to open.
All integers are little-endian. The code is `crates/custos-vault/src/format.rs`.
## Keys
```
master password ──Argon2id(salt, m, t, p)──► KEK (32 bytes) ──unwraps──► vault key (32 random bytes)
│
body = XChaCha20-Poly1305(vault key, body nonce,
JSON, aad = whole header)
```
- The **vault key** is 32 bytes from the OS CSPRNG, made once when the vault is created.
- A **key slot** wraps the vault key. Version 1 has one kind: a password slot (Argon2id → KEK →
XChaCha20-Poly1305). Changing the password writes a new slot (new salt, new nonce) and leaves the vault
key alone. Windows Hello (desktop app) wraps the same vault key outside this file, on the one machine.
- **Nonces** are 24 random bytes, fresh for every slot write and every save. With 192-bit nonces random
generation is safe for any realistic number of saves.
## Layout
```
offset size field
0 6 magic "CUSTOS" (43 55 53 54 4F 53)
6 2 version u16 = 1
8 1 slot count u8, 1..=8
9 … slots repeated `slot count` times, see below
… 24 body nonce
──────── end of header ─────────────────────────────────────────────
… n+16 body ciphertext of n bytes of JSON + 16-byte Poly1305 tag, to end of file
```
Each slot:
```
size field
1 kind 1 = password; other values are skipped by readers that do not know them
2 length u16, bytes of payload that follow
len payload
```
Password slot payload (117 bytes):
```
size field
1 kdf 1 = Argon2id, version 0x13
4 m_cost u32, memory in KiB
4 t_cost u32, iterations
4 p_cost u32, lanes
32 salt
24 wrap nonce
48 wrapped key 32-byte vault key encrypted with the KEK + 16-byte tag
```
- **Slot AAD**: `magic ‖ version ‖ kind ‖ payload[0..69]` (everything in the slot before the wrapped key).
So the KDF parameters and salt cannot be swapped without the unwrap failing.
- **Body AAD**: the whole header, from offset 0 to the end of the body nonce.
### Limits a reader enforces before running Argon2
A tampered header must not make the reader allocate 16 GiB. Outside these ranges the file is refused
with "unsupported KDF parameters", before any work:
| Parameter | Allowed | Default written by Custos |
|:--|:--|:--|
| m_cost | 8·p ..= 2 097 152 KiB (2 GiB) | 65 536 KiB (64 MiB) |
| t_cost | 1 ..= 64 | 3 (measured ≈ 0.5–1 s on the dev machine, see `custos-vault` `KdfParams::DEFAULT`) |
| p_cost | 1 ..= 16 | 1 |
The parameters are stored per file, so a later version can raise the defaults; a vault keeps its
parameters until its password is changed.
### Errors
| Situation | Error |
|:--|:--|
| Wrong magic, or the file is shorter than a header | `NotAVault` |
| Version other than 1 | `UnsupportedVersion(n)` |
| No password slot, or a slot runs past the end | `Malformed` |
| KDF parameters outside the limits | `UnsupportedKdf` |
| No slot unwraps with the password | `WrongPassword` (a wrong password and a tampered slot look the same) |
| Slot unwraps but the body tag fails | `Corrupted` (header or body changed after it was written) |
No error carries any part of the password, the key or the plaintext.
## Body (the JSON inside)
UTF-8 JSON, held in zeroizing buffers while being encrypted or parsed:
```json
{
"format": 1,
"items": [
{
"id": "6f1c…(32 hex)",
"type": "web-login",
"title": "GitHub",
"fields": [
{ "name": "username", "value": "GitHub.Lantern.47", "secret": false },
{ "name": "password", "value": "Fable7-Orbit3-…", "secret": true },
{ "name": "url", "value": "https://github.com", "secret": false }
],
"tags": ["dev"],
"notes": "",
"created": 1793000000000,
"modified": 1793000000000,
"history": [ { "field": "password", "value": "old…", "changed": 1792000000000 } ]
}
]
}
```
- Times are Unix milliseconds (UTC).
- `fields` is an ordered list; a name may repeat (several `url`s). Unknown names are kept as custom fields.
- `history` keeps the previous value of a secret field each time it changes (newest last, at most 20 per item).
- Readers ignore JSON members they do not know, so later versions can add them.
### Item types and their fields (v1)
`*` = secret (`"secret": true`: hidden until revealed, copied with clipboard protection).
| `type` | Fields, in order |
|:--|:--|
| `web-login` | `username`, `password`*, `url` (repeatable), `totp`* (otpauth URI or base32 secret), `autotype-match`, `autotype-sequence` |
| `api-credential` | `service`, `key-id`, `secret`*, `endpoint`, `expires` (YYYY-MM-DD) |
| `local-app` | `target` (app or Credential Manager target), `username`, `password`* |
| `wifi` | `ssid`, `password`*, `security` (`WPA` / `WEP` / `nopass`), `hidden` (`true` / `false`) |
| `secure-note` | none: the text is `notes` |
| `card` | `cardholder`, `number`*, `expiry` (MM/YY), `cvv`*, `pin`* |
`notes` is secret for every type (it is shown only on reveal).
## Planned KDBX 4 mapping (export comes in v1.2)
KeePassXC-compatible: standard strings where KeePass has them, custom strings otherwise. Secret fields
become *protected* strings.
| Custos | KDBX 4 entry |
|:--|:--|
| `title` | `Title` |
| `username` (web-login, local-app) | `UserName` |
| `password`* (web-login, local-app, wifi) | `Password` (protected) |
| first `url` | `URL` |
| 2nd, 3rd … `url` | `KP2A_URL_1`, `KP2A_URL_2`, … (KeePassXC reads these) |
| `notes` | `Notes` |
| `totp` | `otp` (protected), as an `otpauth://` URI (KeePassXC's format) |
| `autotype-sequence` | `AutoType/DefaultSequence` |
| `autotype-match` | `AutoType/Association/Window` |
| `type` | custom string `custos-type` |
| `api-credential`: `service`, `key-id`, `endpoint`, `expires` | custom strings `service`, `key-id`, `endpoint`; `expires` → entry `Times/Expires` + `ExpiryTime` |
| `api-credential`: `secret`* | `Password` (protected) |
| `local-app`: `target` | custom string `target` |
| `wifi`: `ssid`, `security`, `hidden` | `UserName` = ssid, custom strings `security`, `hidden` |
| `card`: `number`* | `Password` (protected) |
| `card`: `cardholder`, `expiry` | `UserName` = cardholder, custom string `expiry` |
| `card`: `cvv`*, `pin`* | protected custom strings `cvv`, `pin` |
| other custom fields | custom strings of the same name, protected when secret |
| `tags` | `Tags` (`;`-separated) |
| `created`, `modified` | `Times/CreationTime`, `Times/LastModificationTime` |
| `history` entries | `History` entries (one per change, with that old password) |
## Saving
`custos-vault::save_atomic` never leaves a half-written vault in place of the old one:
1. The new bytes go to `<name>.custos.tmp` in the same folder; the file is flushed with `fsync`.
2. If `<name>.custos` exists, it is copied to `<name>.custos.bak.tmp`, flushed, and renamed over
`<name>.custos.bak`.
3. `<name>.custos.tmp` is renamed over `<name>.custos` (`MoveFileEx` with `REPLACE_EXISTING` on Windows,
`rename` on Unix, then the folder is flushed on Unix).
A crash in step 1 or 2 leaves the old vault untouched; a crash between 2 and 3 leaves both the old vault
and an identical `.bak`. Leftover `.tmp` files are ignored and overwritten by the next save.
## Versioning
A change that old readers would misread bumps `version`. Readers refuse versions they do not know rather
than guess. Adding JSON members or new slot kinds does not need a bump.