# 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 | 20 (≈ 0.75 s per unlock on the dev machine; `examples/kdf_timing.rs` measures it) | | 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) | ## Device key wrap (not part of the file) Quick unlock on one machine (the desktop app uses Windows Hello) keeps a separate 108-byte blob **on that machine**, never in the vault file: ``` "CDW1" (4) ‖ salt (32) ‖ nonce (24) ‖ XChaCha20-Poly1305(KEK, vault key, aad = "CDW1" ‖ salt ‖ nonce) (48) KEK = HKDF-SHA256(ikm = device material, salt, info = "custos device wrap v1") ``` The device material is something only that device can produce on request (for Windows Hello: the RSA signature of a fixed challenge by a TPM-held key, which only Hello can unlock). A wrong material or a changed blob gives `DeviceKeyRejected`; a blob from another vault unwraps a key that fails the body tag (`Corrupted`). Changing the master password does not change the vault key, so the app deletes the blob itself when the password changes. ## Saving `custos-vault::save_atomic` never leaves a half-written vault in place of the old one: 1. The new bytes go to `.custos.tmp` in the same folder; the file is flushed with `fsync`. 2. If `.custos` exists, it is copied to `.custos.bak.tmp`, flushed, and renamed over `.custos.bak`. 3. `.custos.tmp` is renamed over `.custos` (`std::fs::rename`, which replaces the target; on Unix the folder is flushed afterwards). A rename refused because a sync client or virus scanner holds the file open is retried for about two seconds. 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. ## Share links Shares are not vault files; their blob and link format is in `docs/SHARE.md`.