Files
pixa/internal/seal/crypto.go
sneak 23506df609
All checks were successful
check / check (push) Successful in 2m3s
chore: update golangci-lint to v2.12.2 with canonical config
Replace .golangci.yml with the canonical v2-schema config
(default: all minus six disabled linters, lll 88, tests included)
and bump every golangci-lint pin to v2.12.2:

- Dockerfile: golangci/golangci-lint:v2.12.2-alpine (hash-pinned)
- script/bootstrap: GOLANGCI_LINT_VERSION 2.12.2 with new
  linux-amd64/arm64 release-archive sha256 pins

Fix all 747 findings the stricter config surfaces, with no behavior
changes: t.Parallel() throughout the test suite, static sentinel
errors and errors.Is comparisons, checked error returns, context
propagation (contextcheck/noctx), 88-column wrapping, extracted
constants and helpers for goconst/dupl/funlen/cyclop, exhaustive
switch cases replicating existing defaults, and white-box test files
renamed to *_internal_test.go for testpackage. Three
nolint:tagliatelle directives preserve the existing snake_case JSON
wire and on-disk metadata formats.
2026-08-07 17:10:27 +00:00

88 lines
2.3 KiB
Go

// Package seal provides authenticated encryption utilities for pixa.
// It uses NaCl secretbox (XSalsa20-Poly1305) for sealing data
// and HKDF-SHA256 for key derivation.
package seal
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"io"
"golang.org/x/crypto/hkdf"
"golang.org/x/crypto/nacl/secretbox"
)
// Key sizes for NaCl secretbox.
const (
KeySize = 32 // NaCl secretbox key size
NonceSize = 24 // NaCl secretbox nonce size
)
// Errors returned by crypto operations.
var (
ErrDecryptionFailed = errors.New("decryption failed: invalid ciphertext or key")
ErrInvalidPayload = errors.New("invalid encrypted payload")
ErrKeyDerivation = errors.New("key derivation failed")
)
// DeriveKey uses HKDF-SHA256 to derive a key from master key material.
// The salt parameter provides domain separation between different key usages.
func DeriveKey(masterKey []byte, salt string) ([KeySize]byte, error) {
var key [KeySize]byte
hkdfReader := hkdf.New(sha256.New, masterKey, []byte(salt), nil)
_, err := io.ReadFull(hkdfReader, key[:])
if err != nil {
return key, ErrKeyDerivation
}
return key, nil
}
// Encrypt encrypts plaintext using NaCl secretbox (XSalsa20-Poly1305).
// Returns base64url-encoded ciphertext with the nonce prepended.
func Encrypt(key [KeySize]byte, plaintext []byte) (string, error) {
// Generate random nonce
var nonce [NonceSize]byte
_, err := rand.Read(nonce[:])
if err != nil {
return "", err
}
// Encrypt: nonce + secretbox.Seal(plaintext)
encrypted := secretbox.Seal(nonce[:], plaintext, &nonce, &key)
return base64.RawURLEncoding.EncodeToString(encrypted), nil
}
// Decrypt decrypts base64url-encoded ciphertext using NaCl secretbox.
// Expects the nonce to be prepended to the ciphertext.
func Decrypt(key [KeySize]byte, ciphertext string) ([]byte, error) {
// Decode base64url
data, err := base64.RawURLEncoding.DecodeString(ciphertext)
if err != nil {
return nil, ErrInvalidPayload
}
// Check minimum length (nonce + at least some ciphertext + auth tag)
if len(data) < NonceSize+secretbox.Overhead {
return nil, ErrInvalidPayload
}
// Extract nonce
var nonce [NonceSize]byte
copy(nonce[:], data[:NonceSize])
// Decrypt
plaintext, ok := secretbox.Open(nil, data[NonceSize:], &nonce, &key)
if !ok {
return nil, ErrDecryptionFailed
}
return plaintext, nil
}