Documentation
¶
Overview ¶
Package crypto implements Harbor's client-side, end-to-end note encryption — the part the server never sees. Harbor is zero-knowledge: the server stores opaque ciphertext and an opaque per-user keystore and never holds, derives, or validates any key. All key derivation, wrapping, and field encryption happens here, in the client.
This package is the canonical CLI implementation of the interop contract. Two formats must match byte-for-byte across every Harbor client:
- HRBK1 keystore — a JSON blob holding the KEK salt, Argon2id parameters, and the wrapped master key. It is synced as one opaque per-user record.
- HRBC2 field envelope — the per-field ciphertext string stored in a note's title and content: "HRBC2" "." base64url(iv12) "." base64url(ct‖tag16).
Key model (envelope encryption): a single passphrase + a per-user random salt derive a Key-Encryption-Key (KEK) via Argon2id; a random 256-bit master key is generated once and wrapped with the KEK in the keystore; every note field is encrypted with the master key (AES-256-GCM, fresh 12-byte nonce per op). This is why a passphrase change is cheap — re-wrap the master key, no note re-encrypt.
All base64url is UNPADDED (RawURLEncoding), matching the server's structural validator. The AEAD additional-authenticated-data (AAD) for a field is the UTF-8 bytes of "<noteID>:<fieldName>" — the note id, a literal colon, then "title" or "content" — so an envelope cannot be transplanted to another note or field. That exact byte sequence is the cross-client interop contract shared with web, macOS/iOS, Android and Windows; see fieldAAD and crypto/README.md.
Index ¶
- Constants
- Variables
- func DeriveKEK(passphrase string, salt []byte, p Argon2Params) []byte
- func IsEnvelope(s string) bool
- func NewKeystore(passphrase string, p Argon2Params) (blob string, masterKey []byte, err error)
- func NewUUIDv4() (string, error)
- func OpenField(masterKey []byte, recordID, fieldName, envelope string) (string, error)
- func RewrapMasterKey(k *Keystore, oldPass, newPass string, p Argon2Params) (string, error)
- func SealField(masterKey []byte, recordID, fieldName, plaintext string) (string, error)
- func UnwrapMasterKey(k *Keystore, passphrase string) ([]byte, error)
- type Argon2Params
- type Keystore
Constants ¶
const ( // KeystoreVersion tags the HRBK1 keystore blob. KeystoreVersion = "HRBK1" // EnvelopeVersion tags an HRBC2 per-field ciphertext envelope. EnvelopeVersion = "HRBC2" )
Canonical format constants. These are part of the cross-client wire contract; do not change them without coordinating every Harbor client.
Variables ¶
var ( // ErrBadPassphrase means the wrapped master key would not unwrap — almost // always a wrong passphrase (GCM authentication failed). ErrBadPassphrase = errors.New("incorrect passphrase: could not unwrap the master key") // ErrNotEnvelope means a string is not a structurally-valid HRBC2 envelope. ErrNotEnvelope = errors.New("not an HRBC2 ciphertext envelope") // ErrDecrypt means an envelope failed to decrypt or authenticate (wrong key, // wrong AAD, or corrupt data). ErrDecrypt = errors.New("decryption failed: wrong key or corrupt data") // ErrBadKeystore means the keystore blob is malformed or an unknown version. ErrBadKeystore = errors.New("malformed or unsupported keystore") )
Sentinel errors callers branch on to render friendly messages.
var DefaultArgon2Params = Argon2Params{MemKiB: 65536, Iterations: 3, Parallelism: 1}
DefaultArgon2Params is the canonical profile from docs/encryption.md (RFC 9106): m = 64 MiB, t = 3, p = 1.
Functions ¶
func DeriveKEK ¶
func DeriveKEK(passphrase string, salt []byte, p Argon2Params) []byte
DeriveKEK derives the 32-byte Key-Encryption-Key from the passphrase and salt using Argon2id with the given parameters. The same (passphrase, salt, params) always yields the same KEK, which is what lets any device unwrap the keystore.
func IsEnvelope ¶
IsEnvelope reports whether s is a structurally well-formed HRBC2 envelope, mirroring the server's looksLikeEnvelope check (3 dot parts, "HRBC2" tag, base64url-decodable, 12-byte IV, ≥16-byte ciphertext). It never decrypts.
func NewKeystore ¶
func NewKeystore(passphrase string, p Argon2Params) (blob string, masterKey []byte, err error)
NewKeystore performs first-time setup: it generates a random master key and a random salt, derives the KEK from the passphrase, wraps the master key, and returns the serialized HRBK1 blob together with the in-memory master key. The master key is the long-lived secret; losing the passphrase that wraps it makes the key (and all ciphertext) unrecoverable — there is no escrow.
func NewUUIDv4 ¶
NewUUIDv4 returns a random RFC 4122 version-4 UUID. Encrypted notes must be created with a client-supplied id because the field AAD binds to that id, so the id has to exist before the title/content are sealed.
func OpenField ¶
OpenField decrypts an HRBC2 envelope back to plaintext under the master key, using AAD bound to (recordID, fieldName). It returns ErrNotEnvelope for a non-envelope string and ErrDecrypt when authentication fails (wrong key/AAD or corrupt data).
func RewrapMasterKey ¶
func RewrapMasterKey(k *Keystore, oldPass, newPass string, p Argon2Params) (string, error)
RewrapMasterKey rotates the passphrase: it unwraps the master key with the old passphrase, then re-wraps the SAME master key under a new KEK derived from the new passphrase and a fresh salt. No note is re-encrypted. Returns the new blob.
Types ¶
type Argon2Params ¶
type Argon2Params struct {
MemKiB uint32 // m — memory in KiB
Iterations uint32 // t — passes
Parallelism uint8 // p — lanes
}
Argon2Params are the Argon2id cost parameters carried in the keystore so every device derives the same KEK from the passphrase.
type Keystore ¶
type Keystore struct {
Version string `json:"version"`
KDF string `json:"kdf"`
KDFMemKiB uint32 `json:"kdf_mem_kib"`
KDFIter uint32 `json:"kdf_iterations"`
KDFParallel uint8 `json:"kdf_parallelism"`
Salt string `json:"salt"`
WrappedKey string `json:"wrapped_key"`
}
Keystore is the parsed HRBK1 blob. The on-disk/on-wire form is exactly this JSON object (snake_case field names are part of the contract). Salt and WrappedKey are unpadded base64url; WrappedKey decodes to iv(12)‖ct‖tag(16).
func ParseKeystore ¶
ParseKeystore decodes and structurally validates an HRBK1 keystore blob, rejecting unknown versions or KDFs so a client never silently mis-derives.