encryption

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 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 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

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

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

View Source
var ErrInvalidKeySlot = errors.New("invalid LUKS keyslot")

ErrInvalidKeySlot is returned for a slot index outside [LuksMinKeySlot, LuksMaxKeySlot].

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

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

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

func GeneratePassphrase(minWords int) (exec.Secret, error)

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

func HashPassphrase(passphrase string) string

HashPassphrase returns the SHA-512 hex hash of a passphrase. Used only for local passphrase reuse detection, not for authentication.

func IsRecentlyUsed

func IsRecentlyUsed(passphrase string, recentHashes []string) bool

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.

const LUKS Backend = iota + 1

LUKS is the cryptsetup/LUKS2 implementation.

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.

func New

func New(b Backend, runner exec.Runner, _ ...Option) (Manager, error)

New returns a Manager for the named backend, driven by runner. Pure: validates the backend is known; it does not probe the host. The zero value and any unimplemented backend are rejected with ErrUnknownBackend.

type Option

type Option func(*luks)

Option is the functional-option type for backend-specific knobs (none today).

type TPMEnroller

type TPMEnroller interface {
	Available(ctx context.Context) (bool, error)
	Enroll(ctx context.Context, dev string, existing exec.Secret) error
	Wipe(ctx context.Context, dev string, existing exec.Secret) error
}

TPMEnroller enrolls/removes a TPM2-bound key for a volume.

type Volume

type Volume struct {
	DevicePath string // e.g. "/dev/sda2"
	MapperName string // e.g. "luks-…" (empty if locked)
	MountPoint string // mount point of the unlocked mapper (empty if locked)
}

Volume is a detected LUKS volume.

Jump to

Keyboard shortcuts

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