AlexxIT/go2rtc · error

ErrInvalidParams

ErrInvalidParams

Error message

chacha20poly1305: invalid params

What it means

ErrInvalidParams is the package's sentinel error for malformed cryptographic inputs. DecryptAndVerify and EncryptAndSeal require exactly a 32-byte (chacha20poly1305.KeySize) key and an 8-byte nonce; Signature has the same key requirement. Anything else is rejected before any crypto runs.

Solutions

  1. Verify the key is exactly 32 bytes (use chacha20poly1305.KeySize as the constant) and the nonce exactly 8 bytes before calling.
  2. Check where the key is derived: use hkdf.Sha512 with the correct HAP info labels and slice to the right length.
  3. Truncate or regenerate an oversized nonce to 8 bytes; do not pass standard 12-byte nonces.
  4. Compare with errors.Is(err, chacha20poly1305.ErrInvalidParams) to distinguish this from crypto failures.

Example fix

// before: 12-byte nonce from generic AEAD code
nonce := make([]byte, 12)
out, err := hapchacha.EncryptAndSeal(key, dst, nonce, plaintext, verify)
// after: HAP requires an 8-byte nonce and 32-byte key
key := hkdfOut[:chacha20poly1305.KeySize]
nonce := hkdfNonce[:8]
if len(key) != 32 || len(nonce) != 8 {
    return fmt.Errorf("bad key/nonce sizes: %d/%d", len(key), len(nonce))
}
out, err := hapchacha.EncryptAndSeal(key, dst, nonce, plaintext, verify)
Defensive patterns

Strategy: validation

Validate before calling

if len(key32) != chacha20poly1305.KeySize || len(nonce8) != 8 {
    return fmt.Errorf("need 32-byte key and 8-byte nonce, got %d/%d", len(key32), len(nonce8))
}

Try / catch

out, err := hapchacha.DecryptAndVerify(key, dst, nonce, ct, verify)
if err != nil {
    if errors.Is(err, hapchacha.ErrInvalidParams) {
        return fmt.Errorf("bad key/nonce sizes: key=%d nonce=%d", len(key), len(nonce))
    }
    return err // genuine crypto/authentication failure
}

Prevention

When it happens

Trigger: Passing an ed25519 private key (64 bytes) or a seed (32 bytes vs 32 needed, but truncated) where a chacha20 key is expected; passing a 12-byte (RFC 8439) or 16-byte nonce instead of the HAP 8-byte nonce; passing nil or empty slices.

Common situations: Mixing HAP key material with keys from another protocol; copying code that uses standard chacha20poly1305 with a 12-byte nonce; deriving keys with a wrong-length HKDF output and not checking the length.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/2b6c8716d72ac4d9. Report an issue: GitHub.

Appendix: source

Thrown at pkg/hap/chacha20poly1305/chacha20poly1305.go:9

package chacha20poly1305

import (
	"errors"

	"golang.org/x/crypto/chacha20poly1305"
)

var ErrInvalidParams = errors.New("chacha20poly1305: invalid params")

// Decrypt - decrypt without verify
func Decrypt(key32 []byte, nonce8 string, ciphertext []byte) ([]byte, error) {
	return DecryptAndVerify(key32, nil, []byte(nonce8), ciphertext, nil)
}

// Encrypt - encrypt without seal
func Encrypt(key32 []byte, nonce8 string, plaintext []byte) ([]byte, error) {
	return EncryptAndSeal(key32, nil, []byte(nonce8), plaintext, nil)
}

func DecryptAndVerify(key32, dst, nonce8, ciphertext, verify []byte) ([]byte, error) {
	if len(key32) != chacha20poly1305.KeySize || len(nonce8) != 8 {
		return nil, ErrInvalidParams
	}

	aead, err := chacha20poly1305.New(key32)
	if err != nil {

View on GitHub (pinned to c245815e75)