The format/crypto/storage core of Madstone Cipher as a standalone Go module.
- Module:
github.com/madstone-tech/cipher-core - License: BSL 1.1 (converts to Apache 2.0 three years after each release) — see LICENSE
| Package | Contents | Importable |
|---|---|---|
pkg/cipher |
The public SDK — NewLocal/New, Client, aliased types |
yes |
pkg/cipherports |
Public port interfaces + auth-context types; PlanFeatureResolver hook |
yes |
internal/core/entities |
Domain types: IdentityKey, KeyMetadata, EncryptedFile, RecipientBlock, sentinels |
no — architecture |
internal/core/usecases |
Application logic (encrypt/decrypt/keys/CKD/recovery) + ports.go port interfaces |
no — architecture |
internal/adapters/crypto |
The crypto engine: X25519/HKDF/AES-256-GCM, the header-AEAD binding, .mdstn format codec, fuzz + tamper-matrix tests |
no — architecture |
internal/adapters/sqlite |
SQLite repositories (identity keys, CKD cache, recovery, shared-key policies) | no — architecture |
internal/adapters/ckd |
CKD discovery client | no — architecture |
internal/adapters/cli |
File handler (staged writes: random exclusive temp file, published only on success) | no — architecture |
internal/adapters/logging |
SimpleLogger |
no — architecture |
internal/mcp |
MCP server on stdio (7 tools; no key creation or deletion, no caller-chosen write paths) | no — architecture |
cmd/ |
The CLI command surface (mdstn-cipher entrypoint) |
no — build the binary |
internal/testdata/ |
Canonical format test vectors (vectors.json + .mdstn fixtures) — normative per docs/format-spec.md |
no — architecture |
Only pkg/cipher and pkg/cipherports are importable by other modules. The internal/ packages
are the implementation: read them to understand the design, but code against the public surface.
- SDK guide — the 7 SDK tasks, each snippet tied to a
compiling example in
pkg/cipher. - CLI guide — every command, the exit codes and non-interactive secret handling.
- MCP guide — start the server, client configuration and the tool list.
This repository is the canonical documentation; docs.mdstn.dev/cipher mirrors it.
The .mdstn container has one format, version byte 0x03. The body starts with a header MAC
that commits the header to the file key, so every recipient gets the same plaintext or an error.
Each stream chunk is authenticated with its index and a final-chunk flag, so truncation and
reordering fail. Recipient blocks bind both public keys into the key derivation and store no
recipient identifier. File passwords use Argon2id. Version bytes 0x01 and 0x02 are retired:
the tool rejects them with "unsupported version". The reimplementation-grade specification is
docs/format-spec.md. §Error Classes, §Tamper Contract, and §Governance
are normative.
CKD (email-based public-key discovery) is documented in docs/ckd-protocol.md.
Publish to the hosted directory (keys.mdstn.dev) so others can encrypt to your email without out-of-band key exchange:
# one-time: verify your email at https://keys.mdstn.dev and copy the token
mdstn-cipher keys publish --email alice@example.com --kd-token "$KD_TOKEN"
mdstn-cipher keys status # is my key published?
mdstn-cipher keys unpublish --email alice@example.com --kd-token "$KD_TOKEN"--server-url / CIPHER_SERVER_URL retarget these commands; the production
directory is the default. A changed binding requires the server's
current_fingerprint plus --fingerprint (the CLI surfaces it on the 409).
CIPHER_CKD_URL retargets the lookup side for non-production directories.
The CLI runs without a terminal:
mdstn-cipher encrypt snap.db --recipient-key "$PUB" -o snap.mdstn # raw X25519 public key
mdstn-cipher encrypt big.tar --password-file /run/secrets/pw # file password from a file
mdstn-cipher decrypt snap.mdstn --key-passphrase-file - -o snap.db < pw # key passphrase from stdinExit codes (docs/format-spec.md §Error Classes): 0 success, 1 usage or parse error,
2 tampered file, 3 not a recipient, 4 wrong file password. A failed or cancelled operation
leaves no file at the output path. With --json, stdout holds exactly one JSON value, and logs
and progress go to stderr. Without -o, decrypt writes to the input path without .mdstn and never
replaces an existing file (--force allows it). It never uses the sender's file name from the
header as a path, and it never replaces the input. encrypt never replaces <file>.mdstn without --force. backup writes to
~/.mdstn/backups/ and never replaces a file. The format does not authenticate the sender.
mdstn-cipher mcp runs an MCP server on stdio (protocol versions 2025-06-18 and 2024-11-05). Its tools never replace a file, and none of them writes to a path that the caller
chooses, except encrypt_file and decrypt_file with their no-replace default paths.
Homebrew (macOS and Linux):
brew install madstone-tech/tap/mdstn-cipherThe cask serves binaries built from this repo's releases
(github.com/madstone-tech/cipher-core); the binary reports the release
version in mdstn-cipher --version. Other paths: a checksums-verified
direct download from the release page, the GHCR image, or
go install github.com/madstone-tech/cipher-core/cmd/mdstn-cipher@vX.Y.Z.
go get github.com/madstone-tech/cipher-coreThe SDK surface is pkg/cipher (cipher.NewLocal, cipher.New, Client.EncryptStream,
Client.DecryptStream). Use Config{DBPath: cipher.InMemory} to encrypt to a public key without a
key database on disk. Port interfaces for wiring consuming apps are in pkg/cipherports.
A complete round trip (in-memory key database, default key, stream encrypt/decrypt):
package main
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/madstone-tech/cipher-core/pkg/cipher"
)
func main() {
ctx := context.Background()
client, err := cipher.New(cipher.Config{DBPath: cipher.InMemory})
if err != nil {
log.Fatal(err)
}
defer func() {
if err := client.Close(); err != nil {
log.Fatal(err)
}
}()
dir, err := os.MkdirTemp("", "cipher-stream-*")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
log.Fatal(err)
}
}()
seed := filepath.Join(dir, "seed.txt")
if err := os.WriteFile(seed, []byte("seed"), 0o600); err != nil {
log.Fatal(err)
}
// Encrypt with no intent creates the default key and returns its
// recovery phrase; EncryptStream cannot create one.
if _, err := client.Encrypt(ctx, cipher.EncryptInput{
FilePath: seed,
KeyPassphrase: "default-key-passphrase",
OutputPath: filepath.Join(dir, "seed.txt.mdstn"),
}); err != nil {
log.Fatal(err)
}
var sealed bytes.Buffer
if err := client.EncryptStream(ctx, strings.NewReader("streamed secret"), &sealed, cipher.EncryptStreamInput{
KeyPassphrase: "default-key-passphrase",
}); err != nil {
log.Fatal(err)
}
var opened bytes.Buffer
if _, err := client.DecryptStream(ctx, &sealed, &opened, cipher.DecryptStreamInput{
RecoveryPassphrase: "default-key-passphrase",
}); err != nil {
log.Fatal(err)
}
fmt.Println(opened.String())
}The SDK guide covers the other tasks: keyless encryption to a public key, key recovery, directory lookups and error classes.
task ci # fmt + lint + vet + test + security + build + boundary
task verify-sdk # prove pkg/cipher works from a throwaway external module
task verify-facade # assert no internal type leaks through pkg/cipherRequires Go 1.27.1+, golangci-lint, gofumpt, task.
Tag vX.Y.Z and push — .github/workflows/release.yml runs GoReleaser:
cross-compiled mdstn-cipher binaries (linux/darwin/windows, amd64/arm64 +
macOS universal), tar.gz/zip archives, checksums, SBOM, keyless cosign
signing (.sigstore.json bundles), GHCR multi-arch Docker images
(ghcr.io/madstone-tech/mdstn-cipher-core), and SLSA build-provenance attestations
(auto-enabled on public repos).
Local snapshot (no publish): task release-snapshot.
After tagging:
- Confirm the
context7-refreshworkflow run for the tag succeeded. It is tag-triggered, and it can be dispatched from the Actions tab. Before theCONTEXT7_API_KEYrepository secret exists, the run prints a "not set" notice and succeeds. - When a minor line is superseded, add its last tag to
previousVersionsincontext7.jsonas{ "tag": "vX.Y.Z" }, in the next commit. Never list a tag earlier than v0.5.1. - Spot-check one SDK question on Context7 against the new tag.
- Tags are cut as-needed when this module's code changes.
v0.1.0= the initial cut. Patches bump patch; additive surface bumps minor. A format change needs a new container version byte and is governed bydocs/format-spec.md§Governance.
Feature branches, conventional commits — see AGENTS.md.