kdfbudget

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: BSD-3-Clause Imports: 6 Imported by: 6 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.

README

kdfbudget

One shared bound on the TOTAL key-derivation work spent opening a volume or key.

A header read from an image is untrusted. Limiting each KDF run is not enough: a hostile header can hold many runs (key slots times digests, keybag lockers, every KDF a format tries in turn), each one within its limit, and the sum is unbounded. Measured on the fleet: 3155 runs for any password in LUKS (go-fde/luks#32), 888 with no passphrase in APFS (go-fde/apfs#35), pbkdf2iters of 2^62 in zfscrypt, and a facade that cannot pass limits (go-fde/fde#41).

A Budget is made for one unlock. Every run is charged BEFORE it is spent; a charge that does not fit returns ErrLimit and spends nothing.

b := kdfbudget.Default() // or Unlimited() for a header you trust

for _, slot := range slots {
	if err := b.ChargePBKDF2(kdfbudget.CostSHA512, slot.Iterations); err != nil {
		return err // errors.Is(err, kdfbudget.ErrLimit)
	}
	key := pbkdf2(slot) // only now
}
if err := b.ChargeArgon2(memKiB, passes, threads); err != nil { ... }
if err := b.ChargeCompressions(kdfbudget.CostSHA256, 2*0x100000); err != nil { ... } // BitLocker stretch

API

Budget{MaxMemory, MaxThreads, MaxWork, Deadline} limits for one unlock; safe for concurrent use; the zero value and a nil pointer refuse everything
Default(), Unlimited() the defaults below; structural bounds only
ChargePBKDF2(hashBlockCost, iters) one output block
ChargeArgon2(memKiB, passes, threads) also refuses what argon2 cannot take (0 passes, 0 or more than 255 threads, under 8 KiB per thread)
ChargeCompressions(hashBlockCost, n) any other hash chain
Spent(), ErrLimit ErrLimit can be aliased by a package that had its own

Parameters are uint64, so a value above 2^32 is refused rather than truncated (an APFS locker with 256 threads became 0 as a uint8).

Cost model

One unit is the compression of one 64-byte block by a plain hash.

  • PBKDF2-HMAC: 2 * iterations * hashBlockCost (inner and outer hash per iteration). Costs: SHA-1, SHA-256, RIPEMD-160, BLAKE2s 1; SHA-512 2 (128-byte block); Whirlpool, Streebog 4.
  • Argon2: passes * memKiB * 8. Threads change elapsed time, not work.
  • Memory is a peak: each Argon2 run is checked against MaxMemory, never summed.

The constants are round, conservative estimates, not measurements of a machine: the budget is a denial-of-service bound accurate to a small factor, not a timer.

Defaults

MaxMemory 4 GiB (cryptsetup's Argon2 ceiling), MaxThreads 255 (what x/crypto/argon2 takes; cryptsetup writes 1 to 4), MaxWork 2^32 units, about three minutes of one core. The tests assert that these accept: a 4 GiB Argon2 with 4 threads; 32 LUKS2 slots each 1 GiB x 4 passes; 8 LUKS1 slots of 2^26 PBKDF2 iterations; OpenZFS's minimum pbkdf2iters (100000, no maximum) and 2^31; BitLocker's fixed 0x100000-round stretch; every VeraCrypt variant at PIM 2000 (15000 + 1000 x PIM iterations). A hostile header is refused at the run that crosses the budget: the 17th LUKS slot of 2^26 SHA-512 iterations, the 129th 4 GiB Argon2 locker. Use Unlimited() for a volume you trust.

Tests

100% statement coverage, race detector, a fuzz target on the charge arithmetic (overflow, thread truncation, exact cost), a committed seed corpus, and a 32-bit (386) lane.

Documentation

Overview

Package kdfbudget bounds the TOTAL key-derivation work spent opening one volume or key.

A header read from an image is untrusted. Checking each KDF run against its own limit is not enough: a hostile header can hold many runs (key slots times digests, lockers, every KDF a format tries in turn), each one within its limit, and the sum is unbounded. A Budget is created for one unlock and every run is charged to it BEFORE it is spent; a charge that does not fit is refused with ErrLimit and spends nothing.

b := kdfbudget.Default()
for _, slot := range slots {
	if err := b.ChargePBKDF2(kdfbudget.CostSHA512, slot.Iterations); err != nil {
		return err // errors.Is(err, kdfbudget.ErrLimit)
	}
	key := pbkdf2(slot) // only now
}

Cost model

Work is counted in units, one unit being the compression of one 64-byte block by a plain hash (SHA-1, SHA-256, RIPEMD-160, BLAKE2s):

  • PBKDF2-HMAC runs the hash twice per iteration (inner and outer) for each output block: 2 * iterations * hashBlockCost. The Cost constants give hashBlockCost; SHA-512 is 2 because its block is 128 bytes. Charge once per output block when the derived key is longer than the hash output.
  • Argon2 fills passes * memKiB blocks of 1 KiB; one block fill is charged Argon2BlockCost (8) units, which puts Argon2 and PBKDF2 within a small factor of each other in time on one core. Threads do not change the work, only the elapsed time.

The constants are round, conservative estimates, not measurements of one machine: the budget is a denial-of-service bound, accurate to a small factor, not a timer.

Memory is a peak, not a sum: runs are sequential, so each Argon2 run is checked against MaxMemory and none is added to another.

Index

Constants

View Source
const (
	CostSHA1      uint64 = 1
	CostRIPEMD160 uint64 = 1
	CostSHA256    uint64 = 1
	CostBLAKE2s   uint64 = 1
	CostSHA512    uint64 = 2
	CostWhirlpool uint64 = 4
	CostStreebog  uint64 = 4
)

Relative cost of one compression of the named hash, for ChargePBKDF2. The unit is a 64-byte block of a plain hash. SHA-512 takes 128 bytes per block, hence 2; Whirlpool and Streebog are table or S-box heavy and rounded up to 4. A hash not listed takes the cost of the nearest one, rounded up.

View Source
const (
	// DefaultMaxMemory is 4 GiB: cryptsetup's ceiling for Argon2 memory
	// (crypt_pbkdf_get_limits).
	DefaultMaxMemory uint64 = 4 << 30
	// DefaultMaxThreads is 255, what golang.org/x/crypto/argon2 takes (its
	// parallelism is a uint8); cryptsetup writes 1 to 4.
	DefaultMaxThreads uint8 = math.MaxUint8
	// DefaultMaxWork is 2^32 units, about three minutes of one core. It
	// covers a LUKS2 volume with its 32 key slots each calibrated to about
	// two seconds, tried in turn with a wrong passphrase, with margin; and
	// 2^31 PBKDF2-SHA1 iterations, far more than OpenZFS writes (its minimum
	// is 100000 and it sets no maximum). A header that wants more is refused:
	// use Unlimited for a volume you trust.
	DefaultMaxWork uint64 = 1 << 32
)

Defaults, from the maxima of the tools that write volumes.

View Source
const Argon2BlockCost uint64 = 8

Argon2BlockCost is the cost of filling one 1 KiB Argon2 block.

Variables

View Source
var ErrLimit = errors.New("kdfbudget: the key derivation asks for more than the budget allows")

ErrLimit is returned, wrapped, by every refused charge. Packages that had their own ErrLimit can alias it: var ErrLimit = kdfbudget.ErrLimit.

Functions

This section is empty.

Types

type Budget

type Budget struct {
	// MaxMemory bounds the memory of one Argon2 run, in bytes.
	MaxMemory uint64
	// MaxThreads bounds the parallelism of one Argon2 run.
	MaxThreads uint8
	// MaxWork bounds the sum of every charge, in units (see the package
	// comment).
	MaxWork uint64
	// Deadline, when not zero, refuses every charge made after it.
	Deadline time.Time
	// contains filtered or unexported fields
}

A Budget bounds the key-derivation work of one unlock. It is safe for concurrent use, but its limit fields must not change after the first charge. Create it with Default or Unlimited, or fill the fields; the zero Budget refuses every charge, and so does a nil *Budget.

func Default

func Default() *Budget

Default returns a Budget with the Default constants and no deadline.

func Unlimited

func Unlimited() *Budget

Unlimited returns a Budget that only keeps the structural bounds (Argon2 needs 1 to 255 threads and 8 KiB per thread) and counts the work spent. Use it for a header you wrote or trust.

func (*Budget) ChargeArgon2

func (b *Budget) ChargeArgon2(memKiB, passes, threads uint64) error

ChargeArgon2 charges an Argon2 run of memKiB KiB, passes passes and threads lanes. Besides the budget it refuses parameters argon2 itself cannot take (no pass, no thread, fewer than 8 KiB per thread), which would otherwise panic or be truncated, and memory above MaxMemory. Call it before deriving.

func (*Budget) ChargeCompressions

func (b *Budget) ChargeCompressions(hashBlockCost, n uint64) error

ChargeCompressions charges n compressions of a hash of relative cost hashBlockCost, for a derivation that is not PBKDF2: BitLocker's key stretch hashes 0x100000 times an 88-byte structure, 2 blocks each, which is ChargeCompressions(CostSHA256, 2*0x100000).

func (*Budget) ChargePBKDF2

func (b *Budget) ChargePBKDF2(hashBlockCost, iters uint64) error

ChargePBKDF2 charges a PBKDF2-HMAC run of iters iterations with a hash of relative cost hashBlockCost (the Cost constants), for one output block. Call it before deriving, once per output block.

func (*Budget) Spent

func (b *Budget) Spent() uint64

Spent returns the work charged so far.

Jump to

Keyboard shortcuts

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