golang/go · error

pbkdf2: keyLength must be larger than 0

Error message

pbkdf2: keyLength must be larger than 0

What it means

The PBKDF2 key derivation function requires a strictly positive keyLength. A keyLength of zero or negative makes no sense for deriving a key and would produce an empty output, so it is rejected before any computation. This is the first validation check in the Key() function.

Source

Thrown at src/crypto/internal/fips140/pbkdf2/pbkdf2.go:27

	"crypto/internal/fips140/hmac"
	"errors"
	"hash"
)

// divRoundUp divides x+y-1 by y, rounding up if the result is not whole.
// This function casts x and y to int64 in order to avoid cases where
// x+y would overflow int on systems where int is an int32. The result
// is an int, which is safe as (x+y-1)/y should always fit, regardless
// of the integer size.
func divRoundUp(x, y int) int {
	return int((int64(x) + int64(y) - 1) / int64(y))
}

func Key[Hash hash.Hash](h func() Hash, password string, salt []byte, iter, keyLength int) ([]byte, error) {
	setServiceIndicator(salt, keyLength)

	if keyLength <= 0 {
		return nil, errors.New("pbkdf2: keyLength must be larger than 0")
	}

	prf := hmac.New(h, []byte(password))
	hmac.MarkAsUsedInKDF(prf)
	hashLen := prf.Size()
	numBlocks := divRoundUp(keyLength, hashLen)
	const maxBlocks = int64(1<<32 - 1)
	if keyLength+hashLen < keyLength || int64(numBlocks) > maxBlocks {
		return nil, errors.New("pbkdf2: keyLength too long")
	}

	var buf [4]byte
	dk := make([]byte, 0, numBlocks*hashLen)
	U := make([]byte, hashLen)
	for block := 1; block <= numBlocks; block++ {
		// N.B.: || means concatenation, ^ means XOR
		// for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
		// U_1 = PRF(password, salt || uint(i))

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Validate keyLength > 0 before calling Key()
  2. Set a sensible default key length (e.g., 32 for AES-256) in configuration
  3. Check that the configuration source for key length is populated

Example fix

// before
dk, err := pbkdf2.Key(sha256.New, password, salt, iter, keyLen)

// after
if keyLen <= 0 {
    return nil, fmt.Errorf("keyLength must be positive, got %d", keyLen)
}
dk, err := pbkdf2.Key(sha256.New, password, salt, iter, keyLen)
Defensive patterns

Strategy: validation

Validate before calling

func validateKeyLength(keyLen int) error {
    if keyLen <= 0 {
        return fmt.Errorf("keyLength must be positive, got %d", keyLen)
    }
    return nil
}

if err := validateKeyLength(keyLen); err != nil { return err }
dk, err := pbkdf2.Key(sha256.New, password, salt, iter, keyLen)

Try / catch

dk, err := pbkdf2.Key(h, password, salt, iter, keyLen)
if err != nil {
    return fmt.Errorf("PBKDF2 key derivation failed: %w", err)
}

Prevention

When it happens

Trigger: Calling pbkdf2.Key(...) with keyLength <= 0 — typically keyLength == 0.

Common situations: keyLength computed from a configuration value that defaults to 0 when unset; integer underflow in a derived length calculation; a bug where a key-size constant is read from a struct field that was never initialized.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/b72fbde324c84134. Report an issue: GitHub.