crypt

package
v0.46.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 9 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const (
	// KeySize is the AES-256 / master-key / DEK size in bytes.
	KeySize = 32
)

Variables

View Source
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 NewDEK

func NewDEK() ([]byte, error)

NewDEK returns a fresh random 256-bit data-encryption key (one per segment).

func NewMaster

func NewMaster() ([]byte, error)

NewMaster returns a fresh random 256-bit master key.

func Open

func Open(key, nonce, ciphertext []byte) ([]byte, error)

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

func Seal(key, plaintext []byte) (nonce, ciphertext []byte, err error)

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

func Subkey(master []byte, info string) ([]byte, error)

Subkey derives a 256-bit purpose-specific key from master via HKDF-SHA256 with the given context label (use DataInfo / BackupInfo).

func UnwrapDEK

func UnwrapDEK(key, nonce, wrapped []byte) ([]byte, error)

UnwrapDEK recovers a DEK wrapped by WrapDEK.

func WrapDEK

func WrapDEK(dek, key []byte) (nonce, wrapped []byte, err error)

WrapDEK wraps a DEK under key (the data key); UnwrapDEK reverses it.

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

func NewAEAD(key []byte) (*AEAD, error)

NewAEAD returns an AEAD for key, which must be KeySize bytes (any AES key size is accepted, as by Seal). The key is copied.

func (*AEAD) Open added in v0.37.0

func (a *AEAD) Open(nonce, ciphertext []byte) ([]byte, error)

Open is the package-level Open under a's key.

func (*AEAD) Seal added in v0.37.0

func (a *AEAD) Seal(plaintext []byte) (nonce, ciphertext []byte, err error)

Seal is the package-level Seal under a's key.

type KEK

type KEK struct {
	ID       string
	Material []byte
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL