Documentation
¶
Overview ¶
Package crypt is the encryption-at-rest key hierarchy for a ClassAd database.
A random 256-bit DB master key roots the hierarchy. It is never stored in the clear: it is wrapped once per available pool key (a KEK), so any one pool key opens the DB and the DB survives key rotation (add a row per new key). Purpose-specific subkeys are derived from the master via HKDF -- the data key (wraps each segment's DEK) and the backup key (wraps a snapshot's decryption key). Each segment gets its own random DEK, wrapped by the data key and stored with the segment; the DEK encrypts that segment's encrypted attributes with AES-256-GCM. A stolen database is useless without a pool key.
This mirrors golang-htcondor's session-cache envelope, generalized for the DB.
Index ¶
- Constants
- Variables
- func NewDEK() ([]byte, error)
- func NewMaster() ([]byte, error)
- func Open(key, nonce, ciphertext []byte) ([]byte, error)
- func OpenMaster(rows []MasterKeyRow, keys []KEK) ([]byte, error)
- func Seal(key, plaintext []byte) (nonce, ciphertext []byte, err error)
- func Subkey(master []byte, info string) ([]byte, error)
- func UnwrapDEK(key, nonce, wrapped []byte) ([]byte, error)
- func WrapDEK(dek, key []byte) (nonce, wrapped []byte, err error)
- type AEAD
- type KEK
- type MasterKeyRow
Constants ¶
const ( // DataInfo derives the data key, which wraps each per-segment DEK. DataInfo = "classad-db-data-v1" // BackupInfo derives the backup key, which wraps a snapshot's decryption key. BackupInfo = "classad-db-backups-v1" )
Purpose labels for master-key subkeys (HKDF context). Distinct labels keep the derived keys independent, so the same master protects multiple uses without key reuse.
const (
// KeySize is the AES-256 / master-key / DEK size in bytes.
KeySize = 32
)
Variables ¶
var ErrNoKey = errors.New("crypt: no available pool key can decrypt the database")
ErrNoKey reports that none of the available pool keys could unwrap the master key.
Functions ¶
func Open ¶
Open decrypts a Seal result, verifying the GCM tag (returns an error on tampering or a wrong key). Like Seal it builds the cipher for this one call.
func OpenMaster ¶
func OpenMaster(rows []MasterKeyRow, keys []KEK) ([]byte, error)
OpenMaster recovers the master key from the first row whose KeyID matches an available KEK and whose wrapping decrypts. Returns ErrNoKey if none match.
func Seal ¶
Seal encrypts plaintext with AES-256-GCM under key, returning a fresh nonce and the ciphertext (with the GCM tag appended). It builds the cipher for this one call; a caller sealing repeatedly under one key should hold an AEAD instead.
func Subkey ¶
Subkey derives a 256-bit purpose-specific key from master via HKDF-SHA256 with the given context label (use DataInfo / BackupInfo).
Types ¶
type AEAD ¶ added in v0.37.0
type AEAD struct {
// contains filtered or unexported fields
}
AEAD is AES-256-GCM bound to one key: Seal and Open with the same results and the same format as the package-level Seal and Open under that key, without rebuilding the cipher on every call. Building it -- the AES key schedule and the GHASH table -- costs more than sealing a short value, and a store seals or opens every encrypted attribute of every record it writes or reads.
An AEAD holds its own copy of the key it was built with and never uses any other, and there is no cache shared between AEADs: a holder whose key changes builds a new AEAD, and the old one cannot be handed back for the new key.
It is safe for concurrent use. crypto/cipher does not document whether a cipher.AEAD may be shared between goroutines -- only the counter-nonce GCM variants document that they may not -- and although the AES-GCM implementation in the Go 1.26 standard library keeps no per-call state in the cipher (Seal and Open read the key schedule and GHASH table and work on stack locals), that is not a promise a later release must keep. So no cipher is shared: each call takes one from a pool and returns it after, and a cipher the garbage collector drops from the pool is rebuilt from the key on demand.
func NewAEAD ¶ added in v0.37.0
NewAEAD returns an AEAD for key, which must be KeySize bytes (any AES key size is accepted, as by Seal). The key is copied.
type KEK ¶
KEK is a key-encryption key: a pool / HTCondor signing key. ID names it (e.g. "POOL"); Material is the raw key bytes.
type MasterKeyRow ¶
type MasterKeyRow struct {
KeyID string
Salt []byte
Nonce []byte
Wrapped []byte // AES-GCM(KEK, master)
}
MasterKeyRow is the DB master key wrapped by one KEK, persisted so any available pool key can recover the master.
func WrapMaster ¶
func WrapMaster(master []byte, k KEK) (MasterKeyRow, error)
WrapMaster wraps master with the KEK derived from k (via a fresh salt), producing a persistable row.