Skip to content

Keys and recovery

GitHub →

Cipher keys are X25519 key pairs stored in the local database (~/.mdstn/cipher.db). A private key is never stored raw: it is wrapped with your key passphrase, and it can always be rebuilt from its 24-word recovery phrase.

Create a key

mdstn-cipher keys create --alias work
mdstn-cipher keys create --alias project --expires-in 90d
mdstn-cipher keys create --alias project --expires-at 2027-06-30T00:00:00Z

keys create shows the 24-word recovery phrase once and asks you to confirm three of the words. Write the phrase down and keep it offline. Without a terminal, pass --yes to skip the confirmation, and supply the passphrase with --key-passphrase-file (or - for stdin) or the MDSTN_KEY_PASSPHRASE environment variable.

--type aes256 creates a symmetric key instead of an X25519 pair.

Your default key

“Encrypt for yourself” (encrypt with no recipient or password) uses your default key. If you have none, encrypt creates one, with the alias default, and shows its recovery phrase. A key made with keys create does not become the default.

Without a terminal, encrypt can’t ask you to confirm the new default key’s recovery phrase, so it stops with an error after it has saved the key. For a first run in a script, add --json: the phrase is then returned in the recovery_mnemonic field of the result, and encryption completes.

List, rename and delete

mdstn-cipher keys list
mdstn-cipher keys alias <key-id> new-name
mdstn-cipher keys delete <key-id>

A key can be named by its 16-character ID or by its full fingerprint (64 hex characters, either case). keys list shows the ID, type, alias, creation date, usage count, which key is the default, and the expiry.

Key expiry

Keys made with --expires-in or --expires-at carry an expiry date. check-expiry scans every key and records a notification for each key that has expired or expires within 1 or 7 days. It records each notification once, so you can run it from cron:

mdstn-cipher check-expiry

Export a key

mdstn-cipher keys export <key-id> --output key.pem
mdstn-cipher keys export <key-id> --format raw --output key.bin

The export holds the wrapped private key, still encrypted with your key passphrase, not the raw key. Its PEM label is MDSTN WRAPPED X25519 PRIVATE KEY, and the file is written with mode 0600. To give someone your public key, use keys list --json (the public_key field) or publish it.

Back up and restore

mdstn-cipher backup                      # ~/.mdstn/backups/cipher-backup-YYYYMMDD-HHMMSS.json
mdstn-cipher backup -o /secure/cipher.json
mdstn-cipher restore /secure/cipher.json

A backup holds every key’s wrapped private key and salt, plus any shared-key policies. Store it as carefully as the keys themselves. backup never replaces a file (the folder has mode 0700, the file 0600). restore keeps the keys already in the database unless you pass --force.

Recover from the recovery phrase

If you lose the database and have no backup, rebuild the key from its 24 words:

mdstn-cipher recover from-mnemonic

It asks for the phrase and a new key passphrase. The same 24 words always give the same key pair, so files encrypted to that key open again. The derivation is documented, so any tool that implements it can rebuild the key:

seed       = PBKDF2-HMAC-SHA512(NFKD(mnemonic), salt="mnemonic", 2048 iterations, 64 bytes)
scalar     = HKDF-SHA256(seed, salt=32 zero bytes, info="cipher-x25519-v1", 32 bytes)
public_key = X25519(scalar, 9)

How a key is protected at rest

kek                 = Argon2id(key_passphrase, salt(16), m=64 MiB, t=3, p=4, 32 bytes)
wrapped_private_key = nonce(12) || AES-256-GCM(kek, nonce, scalar, aad="mdstn-wrapped-key:" + key_id)

The additional data binds each wrapped key to its key ID, so a wrapped key can’t be moved to another key’s record.