diff --git a/README.md b/README.md index c2eebbd..bc192b9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Encryptoglyph is an open-source encryption-at-rest library and self-hosted servi ## Features -- **XChaCha20-Poly1305 AEAD.** New ciphertext uses a 192-bit nonce and a 256-bit key. The header carries a version, an algorithm id, and a key id. Legacy AES-256-GCM envelopes still decrypt, and `rewrap` upgrades them. Rust (`crates/encryptoglyph`) and Python (`api/eg_crypto`) share the wire format and the vectors in `testvectors/envelope.json`. +- **XChaCha20-Poly1305 AEAD.** New ciphertext uses a 192-bit nonce and a 256-bit key. The header carries a version, an algorithm id, and a key id. Legacy AES-256-GCM envelopes still decrypt, and `rewrap` upgrades them. Rust (`crates/encryptoglyph`), Python (`api/eg_crypto`), and Go (`bindings/go`) share the wire format and the vectors in `testvectors/envelope.json`. - **Key custody.** The compose file runs OpenBao on an internal network for Transit keys and KV. The library itself has no escrow key and no master override. Root keys are 32 bytes, derived with HKDF-SHA256 per tenant, purpose, and algorithm, or from a password with Argon2id. The console and `/v1` sidecar do not call OpenBao. The custody design is in [docs/plan.md](docs/plan.md) and [docs/SECURITY-HARDENING.md](docs/SECURITY-HARDENING.md). - **Encrypted backup.** `restic/rest-server` sits on the same internal network as the backup target. Host ports are not published. Backup scheduling and restore evidence are described in the plan and are not implemented in this tree. - **Console.** `console/` is the operator UI, served by the API at `/`. It seals fields, rotates and shreds keys, and runs a deterministic assistant. OpenBao and restic stay unwired until a deployment supplies them. The compose service builds the same image as the API. @@ -137,6 +137,29 @@ cd crates/encryptoglyph cargo test --locked ``` +### Go + +`bindings/go` matches the Rust wire format using `golang.org/x/crypto` (XChaCha20-Poly1305, HKDF-SHA256, Argon2id). `Encrypt` writes version 3. `Decrypt` still opens version 2 and legacy AES-256-GCM version 1. `Rewrap` writes a fresh version-3 blob. + +```bash +cd bindings/go +go test -count=1 +``` + +```go +import "github.com/GoDeskio/Encryptoglyph/bindings/go" + +kr, err := encryptoglyph.NewKeyring("k1", key) +ctx := encryptoglyph.RecordContext{ + Tenant: "acme", Purpose: "field", Table: "customers", + RecordID: "42", Field: "email", SchemaVersion: 1, +} +blob, err := encryptoglyph.Encrypt(kr, ctx, []byte("ada@example.com")) +plain, err := encryptoglyph.Decrypt(kr, ctx, blob, encryptoglyph.VersionLegacy) +``` + +Module path: `github.com/GoDeskio/Encryptoglyph/bindings/go`. + ### Python `api/eg_crypto` matches the Rust wire format. Tests run it directly. The API container image does not copy this package. diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 0c25d83..00fca16 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -39,6 +39,13 @@ Encryptoglyph runs the following third-party software unmodified. Attribute them - License: MIT - Source: https://git.distrust.co/public/blahaj +## golang.org/x/crypto + +- Project: Go cryptography packages +- Use: Go envelope (`bindings/go`) for XChaCha20-Poly1305, HKDF-SHA256, and Argon2id +- License: BSD 3-Clause License +- Source: https://cs.opensource.google/go/x/crypto + ## FastAPI - Project: FastAPI diff --git a/bindings/go/README.md b/bindings/go/README.md new file mode 100644 index 0000000..ea44ffa --- /dev/null +++ b/bindings/go/README.md @@ -0,0 +1,48 @@ +# bindings/go + +Go package for the Encryptoglyph envelope. Wire-compatible with `crates/encryptoglyph` and `api/eg_crypto`. + +Primitives, all from `golang.org/x/crypto` or the Go standard library: + +- XChaCha20-Poly1305 (`golang.org/x/crypto/chacha20poly1305`) +- AES-256-GCM for legacy version-1 decrypt (`crypto/aes`, `crypto/cipher`) +- HKDF-SHA256 (`golang.org/x/crypto/hkdf`) +- Argon2id (`golang.org/x/crypto/argon2`), m = 256 MiB, t = 3, p = 1 + +`Encrypt` writes version 3. `Decrypt` accepts version 3, version 2, and legacy version 1. `Rewrap` writes a fresh version-3 blob under the current key id. + +## Install + +```bash +go get github.com/GoDeskio/Encryptoglyph/bindings/go@ +``` + +Until that commit is the version you depend on, a replace directive points at a checkout: + +``` +require github.com/GoDeskio/Encryptoglyph/bindings/go v0.0.0 +replace github.com/GoDeskio/Encryptoglyph/bindings/go => ../Encryptoglyph/bindings/go +``` + +## Seal a credential + +The 32-byte root key stays outside the repo. + +```go +kr, err := encryptoglyph.NewKeyring("k1", rootKey) +ctx := encryptoglyph.RecordContext{ + Tenant: "railyard", Purpose: "credential", Table: "connections", + RecordID: id, Field: "secret", SchemaVersion: 1, +} +blob, err := encryptoglyph.Encrypt(kr, ctx, secret) +plain, err := encryptoglyph.Decrypt(kr, ctx, blob, encryptoglyph.VersionLegacy) +``` + +## Tests + +```bash +cd bindings/go +go test -count=1 +``` + +The tests decrypt `testvectors/envelope.json` and reseal the version-3 known answer. diff --git a/bindings/go/envelope.go b/bindings/go/envelope.go new file mode 100644 index 0000000..a35c343 --- /dev/null +++ b/bindings/go/envelope.go @@ -0,0 +1,525 @@ +// Package encryptoglyph seals records in the Encryptoglyph envelope. +// +// Wire format (docs/SPEC-ENVELOPE.md, testvectors/envelope.json): +// +// "EG" | version u8 | alg u8 | kid_len u8 | kid | nonce | body +// +// Version 3 is the default writer: XChaCha20-Poly1305 with a 32-byte key +// commitment (body = commitment || ciphertext || tag). Version 2 is +// XChaCha20-Poly1305 without the commitment. Version 1 is legacy AES-256-GCM. +// Both older versions decrypt, and Rewrap writes a fresh version-3 blob. +// +// XChaCha20-Poly1305, HKDF-SHA256, and Argon2id come from golang.org/x/crypto. +// There is no escrow key in this package. +package encryptoglyph + +import ( + "crypto/aes" + "crypto/cipher" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "crypto/subtle" + "encoding/binary" + "errors" + "fmt" + "io" + "strings" + "unicode/utf8" + + "golang.org/x/crypto/argon2" + "golang.org/x/crypto/chacha20poly1305" + "golang.org/x/crypto/hkdf" +) + +const ( + VersionLegacy = 1 + VersionV2 = 2 + VersionV3 = 3 + + AlgAES256GCM = 1 + AlgXChaCha20Poly1305 = 2 + AlgXChaCha20Poly1305Commit = 3 + + KeyLen = 32 + commitLen = 32 + tagLen = 16 + Argon2Time = 3 + Argon2MemoryKiB = 262144 + Argon2Parallelism = 1 +) + +var hkdfSalt = []byte("encryptoglyph/hkdf-salt/v2") + +// ErrDecrypt is the only failure callers see for a bad blob, tag, or key. +var ErrDecrypt = errors.New("decryption failed") + +// ErrDowngrade is returned when the envelope version is below minVersion. +// errors.Is(err, ErrDecrypt) is also true. +var ErrDowngrade = downgradeError{} + +// ErrInvalidKey means a root key was not 32 bytes. +var ErrInvalidKey = errors.New("invalid key") + +// ErrInvalidArgument means a caller passed a bad kid, salt, or nonce. +var ErrInvalidArgument = errors.New("invalid argument") + +type downgradeError struct{} + +func (downgradeError) Error() string { return "envelope version below minimum" } +func (downgradeError) Unwrap() error { return ErrDecrypt } + +// RecordContext is authenticated as additional data. A ciphertext cannot be +// moved to another tenant, table, row, or field. +type RecordContext struct { + Tenant string + Purpose string + Table string + RecordID string + Field string + SchemaVersion uint32 +} + +// AAD is the canonical record context, not including the envelope header. +func (c RecordContext) AAD() []byte { + out := append([]byte{}, []byte("EGAAD1")...) + for _, part := range []string{c.Tenant, c.Purpose, c.Table, c.RecordID, c.Field, fmt.Sprintf("%d", c.SchemaVersion)} { + out = lp(out, []byte(part)) + } + return out +} + +func lp(out, b []byte) []byte { + var lenb [4]byte + binary.BigEndian.PutUint32(lenb[:], uint32(len(b))) + out = append(out, lenb[:]...) + return append(out, b...) +} + +// Keyring holds 32-byte root keys. Encrypt uses the current id. Decrypt +// accepts any id still in the ring. +type Keyring struct { + keys map[string][]byte + current string +} + +// NewKeyring starts a ring with one current key. +func NewKeyring(kid string, key []byte) (*Keyring, error) { + kr := &Keyring{keys: map[string][]byte{}} + if err := kr.Add(kid, key, true); err != nil { + return nil, err + } + return kr, nil +} + +// GenerateKey returns 32 random bytes. +func GenerateKey() ([]byte, error) { + k := make([]byte, KeyLen) + if _, err := rand.Read(k); err != nil { + return nil, ErrInvalidArgument + } + return k, nil +} + +// Current is the key id used for new envelopes. +func (k *Keyring) Current() string { return k.current } + +// Add copies key into the ring. makeCurrent selects it for encryption. +func (k *Keyring) Add(kid string, key []byte, makeCurrent bool) error { + if err := checkKid(kid); err != nil { + return err + } + if len(key) != KeyLen { + return ErrInvalidKey + } + copied := make([]byte, KeyLen) + copy(copied, key) + k.keys[kid] = copied + if makeCurrent { + k.current = kid + } + return nil +} + +// Retire removes a key. The current key cannot be retired. +func (k *Keyring) Retire(kid string) error { + if kid == k.current { + return fmt.Errorf("%w: cannot retire current key", ErrInvalidArgument) + } + if prev, ok := k.keys[kid]; ok { + for i := range prev { + prev[i] = 0 + } + } + delete(k.keys, kid) + return nil +} + +// Has reports whether kid is still in the ring. +func (k *Keyring) Has(kid string) bool { + _, ok := k.keys[kid] + return ok +} + +// Derive is HKDF-SHA256 of the root key, separated by purpose, tenant, and algorithm. +func (k *Keyring) Derive(kid, purpose, tenant string, alg byte) ([]byte, error) { + root, ok := k.keys[kid] + if !ok { + return nil, ErrDecrypt + } + info := lp(lp(append([]byte{}, []byte("encryptoglyph/dek/v2")...), []byte(purpose)), []byte(tenant)) + info = append(info, alg) + return hkdf32(root, hkdfSalt, info) +} + +func hkdf32(ikm, salt, info []byte) ([]byte, error) { + r := hkdf.New(sha256.New, ikm, salt, info) + out := make([]byte, KeyLen) + if _, err := io.ReadFull(r, out); err != nil { + return nil, ErrInvalidKey + } + return out, nil +} + +func checkKid(kid string) error { + if !utf8.ValidString(kid) || kid == "" || len(kid) > 255 { + return ErrInvalidArgument + } + return nil +} + +// Header is a parsed envelope prefix. +type Header struct { + Version byte + Alg byte + Kid string + Nonce []byte + Header []byte + Body []byte +} + +func nonceLen(alg byte) (int, bool) { + switch alg { + case AlgAES256GCM: + return 12, true + case AlgXChaCha20Poly1305, AlgXChaCha20Poly1305Commit: + return 24, true + default: + return 0, false + } +} + +func versionAlgOK(version, alg byte) bool { + switch version { + case VersionLegacy: + return alg == AlgAES256GCM + case VersionV2: + return alg == AlgXChaCha20Poly1305 + case VersionV3: + return alg == AlgXChaCha20Poly1305Commit + default: + return false + } +} + +// ParseHeader splits an envelope. Malformed input returns ErrDecrypt. +func ParseHeader(blob []byte) (Header, error) { + if len(blob) < 5 || blob[0] != 'E' || blob[1] != 'G' { + return Header{}, ErrDecrypt + } + version, alg, klen := blob[2], blob[3], int(blob[4]) + if !versionAlgOK(version, alg) { + return Header{}, ErrDecrypt + } + n, ok := nonceLen(alg) + if !ok { + return Header{}, ErrDecrypt + } + off := 5 + klen + extra := 0 + if alg == AlgXChaCha20Poly1305Commit { + extra = commitLen + } + if len(blob) < off+n+extra+tagLen { + return Header{}, ErrDecrypt + } + kidBytes := blob[5:off] + if !utf8.Valid(kidBytes) { + return Header{}, ErrDecrypt + } + return Header{ + Version: version, + Alg: alg, + Kid: string(kidBytes), + Nonce: blob[off : off+n], + Header: blob[:off+n], + Body: blob[off+n:], + }, nil +} + +func writeHeader(version, alg byte, kid string, nonce []byte) ([]byte, error) { + if err := checkKid(kid); err != nil { + return nil, err + } + h := []byte{'E', 'G', version, alg, byte(len(kid))} + h = append(h, kid...) + h = append(h, nonce...) + return h, nil +} + +func commitKeys(dek, nonce []byte) (enc, commit []byte, err error) { + enc, err = hkdf32(dek, nonce, []byte("encryptoglyph/v3/enc")) + if err != nil { + return nil, nil, err + } + commit, err = hkdf32(dek, nonce, []byte("encryptoglyph/v3/commit")) + if err != nil { + return nil, nil, err + } + return enc, commit, nil +} + +func commitment(commitKey, aad []byte) []byte { + m := hmac.New(sha256.New, commitKey) + m.Write([]byte("EGCMT1")) + m.Write(aad) + return m.Sum(nil) +} + +func xchachaSeal(key, nonce, aad, plaintext []byte) ([]byte, error) { + aead, err := chacha20poly1305.NewX(key) + if err != nil { + return nil, ErrInvalidKey + } + return aead.Seal(nil, nonce, plaintext, aad), nil +} + +func xchachaOpen(key, nonce, aad, body []byte) ([]byte, error) { + aead, err := chacha20poly1305.NewX(key) + if err != nil { + return nil, ErrDecrypt + } + plain, err := aead.Open(nil, nonce, body, aad) + if err != nil { + return nil, ErrDecrypt + } + return plain, nil +} + +func aesSeal(key, nonce, aad, plaintext []byte) ([]byte, error) { + aead, err := newGCM(key) + if err != nil { + return nil, err + } + return aead.Seal(nil, nonce, plaintext, aad), nil +} + +func aesOpen(key, nonce, aad, body []byte) ([]byte, error) { + aead, err := newGCM(key) + if err != nil { + return nil, ErrDecrypt + } + plain, err := aead.Open(nil, nonce, body, aad) + if err != nil { + return nil, ErrDecrypt + } + return plain, nil +} + +func newGCM(key []byte) (cipher.AEAD, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, ErrInvalidKey + } + aead, err := cipher.NewGCM(block) + if err != nil { + return nil, ErrInvalidKey + } + return aead, nil +} + +// EncryptWithNonce writes a version-3 envelope using nonce. +// Nonce reuse under one key destroys confidentiality. Tests use this. +// Encrypt draws a fresh nonce. +func EncryptWithNonce(kr *Keyring, ctx RecordContext, plaintext, nonce []byte) ([]byte, error) { + if len(nonce) != 24 { + return nil, fmt.Errorf("%w: nonce must be 24 bytes", ErrInvalidArgument) + } + dek, err := kr.Derive(kr.current, ctx.Purpose, ctx.Tenant, AlgXChaCha20Poly1305Commit) + if err != nil { + return nil, err + } + hdr, err := writeHeader(VersionV3, AlgXChaCha20Poly1305Commit, kr.current, nonce) + if err != nil { + return nil, err + } + aad := append(append([]byte{}, hdr...), ctx.AAD()...) + enc, com, err := commitKeys(dek, nonce) + if err != nil { + return nil, err + } + ct, err := xchachaSeal(enc, nonce, aad, plaintext) + if err != nil { + return nil, err + } + out := append(hdr, commitment(com, aad)...) + return append(out, ct...), nil +} + +// Encrypt writes a version-3 envelope under the current key. +func Encrypt(kr *Keyring, ctx RecordContext, plaintext []byte) ([]byte, error) { + nonce := make([]byte, 24) + if _, err := rand.Read(nonce); err != nil { + return nil, ErrInvalidArgument + } + return EncryptWithNonce(kr, ctx, plaintext, nonce) +} + +// EncryptV2 writes a version-2 envelope (no key commitment) for tests and interop. +func EncryptV2(kr *Keyring, ctx RecordContext, plaintext []byte) ([]byte, error) { + key, err := kr.Derive(kr.current, ctx.Purpose, ctx.Tenant, AlgXChaCha20Poly1305) + if err != nil { + return nil, err + } + nonce := make([]byte, 24) + if _, err := rand.Read(nonce); err != nil { + return nil, ErrInvalidArgument + } + hdr, err := writeHeader(VersionV2, AlgXChaCha20Poly1305, kr.current, nonce) + if err != nil { + return nil, err + } + aad := append(append([]byte{}, hdr...), ctx.AAD()...) + ct, err := xchachaSeal(key, nonce, aad, plaintext) + if err != nil { + return nil, err + } + return append(hdr, ct...), nil +} + +// EncryptLegacyV1 writes a version-1 AES-256-GCM envelope for tests and interop. +func EncryptLegacyV1(kr *Keyring, ctx RecordContext, plaintext []byte) ([]byte, error) { + key, err := kr.Derive(kr.current, ctx.Purpose, ctx.Tenant, AlgAES256GCM) + if err != nil { + return nil, err + } + nonce := make([]byte, 12) + if _, err := rand.Read(nonce); err != nil { + return nil, ErrInvalidArgument + } + hdr, err := writeHeader(VersionLegacy, AlgAES256GCM, kr.current, nonce) + if err != nil { + return nil, err + } + aad := append(append([]byte{}, hdr...), ctx.AAD()...) + ct, err := aesSeal(key, nonce, aad, plaintext) + if err != nil { + return nil, err + } + return append(hdr, ct...), nil +} + +// Decrypt opens blob. minVersion rejects older envelopes with ErrDowngrade. +func Decrypt(kr *Keyring, ctx RecordContext, blob []byte, minVersion byte) ([]byte, error) { + h, err := ParseHeader(blob) + if err != nil { + return nil, ErrDecrypt + } + if h.Version < minVersion { + return nil, ErrDowngrade + } + key, err := kr.Derive(h.Kid, ctx.Purpose, ctx.Tenant, h.Alg) + if err != nil { + return nil, ErrDecrypt + } + aad := append(append([]byte{}, h.Header...), ctx.AAD()...) + switch h.Alg { + case AlgXChaCha20Poly1305Commit: + enc, com, err := commitKeys(key, h.Nonce) + if err != nil { + return nil, ErrDecrypt + } + expect := commitment(com, aad) + plain, openErr := xchachaOpen(enc, h.Nonce, aad, h.Body[commitLen:]) + commitOK := subtle.ConstantTimeCompare(h.Body[:commitLen], expect) == 1 + if openErr != nil || !commitOK { + return nil, ErrDecrypt + } + return plain, nil + case AlgXChaCha20Poly1305: + return xchachaOpen(key, h.Nonce, aad, h.Body) + default: + return aesOpen(key, h.Nonce, aad, h.Body) + } +} + +// NeedsRewrap reports whether blob is older than version 3 or under a retired current id. +func NeedsRewrap(kr *Keyring, blob []byte) (bool, error) { + h, err := ParseHeader(blob) + if err != nil { + return false, err + } + return h.Version < VersionV3 || h.Kid != kr.current, nil +} + +// Rewrap decrypts blob and encrypts it again as version 3 under the current key. +func Rewrap(kr *Keyring, ctx RecordContext, blob []byte) ([]byte, error) { + plain, err := Decrypt(kr, ctx, blob, VersionLegacy) + if err != nil { + return nil, err + } + return Encrypt(kr, ctx, plain) +} + +// BlindIndex is a truncated HMAC-SHA256 of the normalised value. +// Normalisation trims, lowercases, and collapses whitespace. +func BlindIndex(key []byte, tenant, field, value string, length int) []byte { + if length > 32 { + length = 32 + } + if length < 0 { + length = 0 + } + norm := normalize(value) + msg := lp(lp(append([]byte{}, []byte("EGBI1")...), []byte(tenant)), []byte(field)) + msg = append(msg, norm...) + m := hmac.New(sha256.New, key) + m.Write(msg) + return m.Sum(nil)[:length] +} + +func normalize(value string) string { + return strings.Join(strings.Fields(strings.ToLower(strings.TrimSpace(value))), " ") +} + +// Fingerprint is the first 16 bytes of SHA-256(label || key), hex encoded. +func Fingerprint(key []byte) string { + h := sha256.New() + h.Write([]byte("encryptoglyph/key-fingerprint/v1")) + h.Write(key) + sum := h.Sum(nil)[:16] + const hexdigits = "0123456789abcdef" + out := make([]byte, 32) + for i, b := range sum { + out[i*2] = hexdigits[b>>4] + out[i*2+1] = hexdigits[b&0x0f] + } + return string(out) +} + +// DeriveRootKeyFromPassword is Argon2id v1.3 with m=256 MiB, t=3, p=1. +// salt must be 16 bytes stored with the account. +func DeriveRootKeyFromPassword(password, salt []byte) ([]byte, error) { + return DeriveRootKeyWithParams(password, salt, Argon2Time, Argon2MemoryKiB, Argon2Parallelism) +} + +// DeriveRootKeyWithParams is Argon2id with caller-chosen cost. Tests use small memory. +func DeriveRootKeyWithParams(password, salt []byte, time, memoryKiB, parallelism uint32) ([]byte, error) { + if len(salt) != 16 { + return nil, fmt.Errorf("%w: salt must be 16 bytes", ErrInvalidArgument) + } + if time == 0 || memoryKiB == 0 || parallelism == 0 { + return nil, ErrInvalidArgument + } + return argon2.IDKey(password, salt, time, memoryKiB, uint8(parallelism), KeyLen), nil +} diff --git a/bindings/go/envelope_test.go b/bindings/go/envelope_test.go new file mode 100644 index 0000000..0d2a1b9 --- /dev/null +++ b/bindings/go/envelope_test.go @@ -0,0 +1,314 @@ +package encryptoglyph + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "errors" + "os" + "path/filepath" + "testing" +) + +type envelopeVectors struct { + KeyHex string `json:"key_hex"` + Kid string `json:"kid"` + Ctx []any `json:"ctx"` + Plaintext string `json:"plaintext"` + V3Hex string `json:"v3_hex"` + V2Hex string `json:"v2_hex"` + V1Hex string `json:"v1_hex"` + BlindIndexHex string `json:"blind_index_hex"` +} + +func loadVectors(t *testing.T) (envelopeVectors, []byte) { + t.Helper() + path := filepath.Join("..", "..", "testvectors", "envelope.json") + raw, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var v envelopeVectors + if err := json.Unmarshal(raw, &v); err != nil { + t.Fatal(err) + } + key, err := hex.DecodeString(v.KeyHex) + if err != nil { + t.Fatal(err) + } + return v, key +} + +func vectorContext(t *testing.T, v envelopeVectors) RecordContext { + t.Helper() + if len(v.Ctx) != 6 { + t.Fatalf("ctx len %d", len(v.Ctx)) + } + schema, ok := v.Ctx[5].(float64) + if !ok || schema != 1 { + t.Fatalf("schema %#v", v.Ctx[5]) + } + return RecordContext{ + Tenant: v.Ctx[0].(string), + Purpose: v.Ctx[1].(string), + Table: v.Ctx[2].(string), + RecordID: v.Ctx[3].(string), + Field: v.Ctx[4].(string), + SchemaVersion: uint32(schema), + } +} + +func TestSharedVectorsDecrypt(t *testing.T) { + v, key := loadVectors(t) + kr, err := NewKeyring(v.Kid, key) + if err != nil { + t.Fatal(err) + } + ctx := vectorContext(t, v) + for _, field := range []string{v.V3Hex, v.V2Hex, v.V1Hex} { + blob, err := hex.DecodeString(field) + if err != nil { + t.Fatal(err) + } + plain, err := Decrypt(kr, ctx, blob, VersionLegacy) + if err != nil { + t.Fatal(err) + } + if string(plain) != v.Plaintext { + t.Fatalf("got %q", plain) + } + } +} + +func TestV3KnownAnswerReseal(t *testing.T) { + v, key := loadVectors(t) + kr, err := NewKeyring(v.Kid, key) + if err != nil { + t.Fatal(err) + } + ctx := vectorContext(t, v) + blob, err := hex.DecodeString(v.V3Hex) + if err != nil { + t.Fatal(err) + } + h, err := ParseHeader(blob) + if err != nil { + t.Fatal(err) + } + if h.Version != VersionV3 || h.Kid != v.Kid || len(h.Nonce) != 24 { + t.Fatalf("header %+v", h) + } + again, err := EncryptWithNonce(kr, ctx, []byte(v.Plaintext), h.Nonce) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(again, blob) { + t.Fatalf("reseal\n got %x\nwant %x", again, blob) + } +} + +func TestBlindIndexVector(t *testing.T) { + v, key := loadVectors(t) + got := BlindIndex(key, "tenant-a", "email", "Foo@Example.com", 16) + if hex.EncodeToString(got) != v.BlindIndexHex { + t.Fatalf("got %x", got) + } +} + +func TestRoundTripRewrapAndDowngrade(t *testing.T) { + key, err := GenerateKey() + if err != nil { + t.Fatal(err) + } + kr, err := NewKeyring("k1", key) + if err != nil { + t.Fatal(err) + } + ctx := RecordContext{Tenant: "acme", Purpose: "credential", Table: "connections", RecordID: "42", Field: "secret", SchemaVersion: 1} + first, err := Encrypt(kr, ctx, []byte("super-secret")) + if err != nil { + t.Fatal(err) + } + second, err := Encrypt(kr, ctx, []byte("super-secret")) + if err != nil { + t.Fatal(err) + } + if bytes.Equal(first, second) { + t.Fatal("nonce reused") + } + h, err := ParseHeader(first) + if err != nil || h.Version != VersionV3 { + t.Fatal(err, h.Version) + } + plain, err := Decrypt(kr, ctx, first, VersionLegacy) + if err != nil || string(plain) != "super-secret" { + t.Fatal(err, string(plain)) + } + needs, err := NeedsRewrap(kr, first) + if err != nil || needs { + t.Fatal(err, needs) + } + legacy, err := EncryptLegacyV1(kr, ctx, []byte("legacy")) + if err != nil { + t.Fatal(err) + } + needs, err = NeedsRewrap(kr, legacy) + if err != nil || !needs { + t.Fatal(err, needs) + } + if _, err := Decrypt(kr, ctx, legacy, VersionV2); !errors.Is(err, ErrDowngrade) || !errors.Is(err, ErrDecrypt) { + t.Fatal(err) + } + up, err := Rewrap(kr, ctx, legacy) + if err != nil { + t.Fatal(err) + } + uh, err := ParseHeader(up) + if err != nil || uh.Version != VersionV3 { + t.Fatal(err, uh.Version) + } + needs, err = NeedsRewrap(kr, up) + if err != nil || needs { + t.Fatal(err, needs) + } +} + +func TestTamperWrongContextWrongKey(t *testing.T) { + key, err := GenerateKey() + if err != nil { + t.Fatal(err) + } + kr, err := NewKeyring("k1", key) + if err != nil { + t.Fatal(err) + } + ctx := RecordContext{Tenant: "acme", Purpose: "field", Table: "users", RecordID: "1", Field: "email", SchemaVersion: 1} + blob, err := Encrypt(kr, ctx, []byte("secret")) + if err != nil { + t.Fatal(err) + } + blob[len(blob)-1] ^= 0x01 + if _, err := Decrypt(kr, ctx, blob, VersionLegacy); !errors.Is(err, ErrDecrypt) { + t.Fatal(err) + } + fresh, err := Encrypt(kr, ctx, []byte("secret")) + if err != nil { + t.Fatal(err) + } + otherCtx := ctx + otherCtx.Field = "other" + if _, err := Decrypt(kr, otherCtx, fresh, VersionLegacy); !errors.Is(err, ErrDecrypt) { + t.Fatal(err) + } + otherKey, err := GenerateKey() + if err != nil { + t.Fatal(err) + } + other, err := NewKeyring("k1", otherKey) + if err != nil { + t.Fatal(err) + } + if _, err := Decrypt(other, ctx, fresh, VersionLegacy); !errors.Is(err, ErrDecrypt) { + t.Fatal(err) + } +} + +func TestRotation(t *testing.T) { + key, err := GenerateKey() + if err != nil { + t.Fatal(err) + } + kr, err := NewKeyring("k1", key) + if err != nil { + t.Fatal(err) + } + ctx := RecordContext{Tenant: "acme", Purpose: "tool-result", Table: "calls", RecordID: "9", Field: "body", SchemaVersion: 1} + old, err := Encrypt(kr, ctx, []byte("rotate me")) + if err != nil { + t.Fatal(err) + } + next, err := GenerateKey() + if err != nil { + t.Fatal(err) + } + if err := kr.Add("k2", next, true); err != nil { + t.Fatal(err) + } + needs, err := NeedsRewrap(kr, old) + if err != nil || !needs { + t.Fatal(err, needs) + } + plain, err := Decrypt(kr, ctx, old, VersionLegacy) + if err != nil || string(plain) != "rotate me" { + t.Fatal(err, string(plain)) + } + sealed, err := Rewrap(kr, ctx, old) + if err != nil { + t.Fatal(err) + } + h, err := ParseHeader(sealed) + if err != nil || h.Kid != "k2" { + t.Fatal(err, h.Kid) + } + if err := kr.Retire("k1"); err != nil { + t.Fatal(err) + } + plain, err = Decrypt(kr, ctx, sealed, VersionLegacy) + if err != nil || string(plain) != "rotate me" { + t.Fatal(err, string(plain)) + } + if _, err := Decrypt(kr, ctx, old, VersionLegacy); !errors.Is(err, ErrDecrypt) { + t.Fatal(err) + } + if err := kr.Retire("k2"); err == nil { + t.Fatal("retired current key") + } +} + +func TestArgon2MatchesNodeAndXCrypto(t *testing.T) { + if Argon2Time != 3 || Argon2MemoryKiB != 262144 || Argon2Parallelism != 1 { + t.Fatal("production Argon2 parameters drifted") + } + pw := bytes.Repeat([]byte{1}, 32) + salt := bytes.Repeat([]byte{2}, 16) + got, err := DeriveRootKeyWithParams(pw, salt, 3, 32, 1) + if err != nil { + t.Fatal(err) + } + const want = "30e0b44b7e23423b7282859ea36f02825b5828dde3c25f124d59d42d7235e132" + if hex.EncodeToString(got) != want { + t.Fatalf("got %x", got) + } + horse, err := DeriveRootKeyWithParams([]byte("correct horse"), []byte("0123456789abcdef"), 2, 64, 1) + if err != nil { + t.Fatal(err) + } + const horseWant = "74fb6b1d311ac0bb4c891f7efd79680245d9f84b72ad8f9bbe61c86e2b9f864d" + if hex.EncodeToString(horse) != horseWant { + t.Fatalf("got %x", horse) + } + if _, err := DeriveRootKeyWithParams([]byte("x"), []byte("short"), 1, 32, 1); err == nil { + t.Fatal("short salt accepted") + } +} + +func TestHKDFSeparation(t *testing.T) { + _, key := loadVectors(t) + kr, err := NewKeyring("k1", key) + if err != nil { + t.Fatal(err) + } + seen := map[string]struct{}{} + for _, purpose := range []string{"a", "b"} { + for _, tenant := range []string{"t1", "t2"} { + derived, err := kr.Derive("k1", purpose, tenant, AlgXChaCha20Poly1305) + if err != nil { + t.Fatal(err) + } + seen[hex.EncodeToString(derived)] = struct{}{} + } + } + if len(seen) != 4 { + t.Fatal(len(seen)) + } +} diff --git a/bindings/go/go.mod b/bindings/go/go.mod new file mode 100644 index 0000000..144d62a --- /dev/null +++ b/bindings/go/go.mod @@ -0,0 +1,7 @@ +module github.com/GoDeskio/Encryptoglyph/bindings/go + +go 1.22 + +require golang.org/x/crypto v0.31.0 + +require golang.org/x/sys v0.28.0 // indirect diff --git a/bindings/go/go.sum b/bindings/go/go.sum new file mode 100644 index 0000000..0b273f9 --- /dev/null +++ b/bindings/go/go.sum @@ -0,0 +1,4 @@ +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=