Documentation
¶
Overview ¶
Package encryption manages disk encryption through an injected exec.Runner.
Build a Manager for an explicit backend (LUKS is the only one today) and a Runner, then call its methods. Passphrases and keys are exec.Secret values: they are written to an ephemeral /dev/shm key file (or piped via stdin for TPM enrollment) and never appear in a command's argv.
r, _ := exec.NewRunner(exec.Direct) enc, _ := encryption.New(encryption.LUKS, r) ok, _ := enc.VerifyPassphrase(ctx, "/dev/sda2", pass)
Pure helpers (passphrase generation/validation/hashing) are package-level.
Index ¶
- Constants
- Variables
- func GeneratePassphrase(minWords int) (exec.Secret, error)
- func HashPassphrase(passphrase string) string
- func IsRecentlyUsed(passphrase string, recentHashes []string) bool
- func ValidatePassphrase(passphrase string, minLength int, complexity Complexity) string
- type AddKeyOptions
- type Backend
- type Complexity
- type Manager
- type Option
- type TPMEnroller
- type Volume
Constants ¶
const ( LuksMinKeySlot = 0 LuksMaxKeySlot = 7 )
LUKS2 (and LUKS1) support eight keyslots, 0..7. Rejecting out-of-range slots at the SDK boundary surfaces a clear reason instead of cryptsetup's opaque one.
Variables ¶
var ErrEmptyKeyMaterial = errors.New("encryption: empty key material not permitted")
ErrEmptyKeyMaterial is returned by a mutating LUKS/TPM operation given an empty passphrase/key. It is refused before any cryptsetup/cryptenroll exec: an empty NEW key would enroll a slot that unlocks with no passphrase, and an empty AUTHENTICATING key for a mutating op is never a legitimate request. (VerifyPassphrase is intentionally exempt — probing an empty passphrase is a legitimate read-only query.)
var ErrInvalidKeySlot = errors.New("invalid LUKS keyslot")
ErrInvalidKeySlot is returned for a slot index outside [LuksMinKeySlot, LuksMaxKeySlot].
var ErrKeyFileStaging = errors.New("encryption: unsafe LUKS key file staging directory")
ErrKeyFileStaging is returned when the private directory key files are staged in cannot be established, or fails its owner/mode check. Owning that directory is enough to replace a key file regardless of the file's own ownership, so anything unexpected about it is a hard failure.
var ErrKeyFileTampered = errors.New("encryption: LUKS key file changed between staging and use")
ErrKeyFileTampered is returned when a staged key file no longer names the file this process wrote. cryptsetup re-resolves the path it is handed, so a swapped entry would enrol a passphrase the attacker chose.
var ErrUnknownBackend = fmt.Errorf("encryption: unknown backend")
ErrUnknownBackend is returned by New for the zero value or any Backend the SDK does not implement (fail-closed).
Functions ¶
func GeneratePassphrase ¶
GeneratePassphrase generates a word-based passphrase as a Secret: at least minWords (minimum 3) capitalized words joined by '-', and at least 32 chars total. Example: "Apple-Tower-Kitchen-Forest".
func HashPassphrase ¶
HashPassphrase returns the SHA-512 hex hash of a passphrase. Used only for local passphrase reuse detection, not for authentication.
func IsRecentlyUsed ¶
IsRecentlyUsed checks if the SHA-256 hash of a passphrase matches any in the provided list of recent hashes.
func ValidatePassphrase ¶
func ValidatePassphrase(passphrase string, minLength int, complexity Complexity) string
ValidatePassphrase checks a passphrase against length and complexity requirements. Returns a human-readable error message if validation fails, or "" if valid.
Types ¶
type AddKeyOptions ¶
type AddKeyOptions struct {
Slot *int
}
AddKeyOptions configures AddKey. The zero value (Slot nil) lets cryptsetup auto-assign a free keyslot; set Slot to target a specific one (0..7) — note the explicit slot 0 is distinct from auto.
type Backend ¶
type Backend int
Backend selects the disk-encryption implementation. Passed explicitly even though LUKS is the only value today; the zero value is invalid (New → ErrUnknownBackend). The never-implemented GELI/CGD scaffolds are not ported.
type Complexity ¶
type Complexity int
Complexity represents password complexity requirements.
const ( ComplexityNone Complexity = 0 ComplexityAlphanumeric Complexity = 1 // letters + digits ComplexityComplex Complexity = 2 // letters + digits + special chars )
type Manager ¶
type Manager interface {
IsEncrypted(ctx context.Context, dev string) (bool, error)
AddKey(ctx context.Context, dev string, existing, newKey exec.Secret, opts AddKeyOptions) error
RemoveKey(ctx context.Context, dev string, key exec.Secret) error
KillSlot(ctx context.Context, dev string, slot int, existing exec.Secret) error
VerifyPassphrase(ctx context.Context, dev string, p exec.Secret) (bool, error)
DetectVolume(ctx context.Context) (Volume, error)
DetectVolumeByKey(ctx context.Context, p exec.Secret) (Volume, error)
DetectAllVolumes(ctx context.Context) ([]Volume, error)
// TPM returns the TPM enroller for this backend; ok is false when the
// backend has no TPM support.
TPM() (TPMEnroller, bool)
}
Manager is the disk-encryption contract.
type Option ¶
type Option func(*luks)
Option is the functional-option type for backend-specific knobs (none today).