{"record":{"id":"2b6c8716d72ac4d9","repo":"AlexxIT/go2rtc","slug":"errinvalidparams","errorCode":"ErrInvalidParams","errorMessage":"chacha20poly1305: invalid params","messagePattern":"chacha20poly1305: invalid params","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/hap/chacha20poly1305/chacha20poly1305.go","lineNumber":9,"sourceCode":"package chacha20poly1305\n\nimport (\n\t\"errors\"\n\n\t\"golang.org/x/crypto/chacha20poly1305\"\n)\n\nvar ErrInvalidParams = errors.New(\"chacha20poly1305: invalid params\")\n\n// Decrypt - decrypt without verify\nfunc Decrypt(key32 []byte, nonce8 string, ciphertext []byte) ([]byte, error) {\n\treturn DecryptAndVerify(key32, nil, []byte(nonce8), ciphertext, nil)\n}\n\n// Encrypt - encrypt without seal\nfunc Encrypt(key32 []byte, nonce8 string, plaintext []byte) ([]byte, error) {\n\treturn EncryptAndSeal(key32, nil, []byte(nonce8), plaintext, nil)\n}\n\nfunc DecryptAndVerify(key32, dst, nonce8, ciphertext, verify []byte) ([]byte, error) {\n\tif len(key32) != chacha20poly1305.KeySize || len(nonce8) != 8 {\n\t\treturn nil, ErrInvalidParams\n\t}\n\n\taead, err := chacha20poly1305.New(key32)\n\tif err != nil {","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/hap/chacha20poly1305/chacha20poly1305.go#L1-L27","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the key is exactly 32 bytes (use chacha20poly1305.KeySize as the constant) and the nonce exactly 8 bytes before calling.","Check where the key is derived: use hkdf.Sha512 with the correct HAP info labels and slice to the right length.","Truncate or regenerate an oversized nonce to 8 bytes; do not pass standard 12-byte nonces.","Compare with errors.Is(err, chacha20poly1305.ErrInvalidParams) to distinguish this from crypto failures."],"exampleFix":"// before: 12-byte nonce from generic AEAD code\nnonce := make([]byte, 12)\nout, err := hapchacha.EncryptAndSeal(key, dst, nonce, plaintext, verify)\n// after: HAP requires an 8-byte nonce and 32-byte key\nkey := hkdfOut[:chacha20poly1305.KeySize]\nnonce := hkdfNonce[:8]\nif len(key) != 32 || len(nonce) != 8 {\n    return fmt.Errorf(\"bad key/nonce sizes: %d/%d\", len(key), len(nonce))\n}\nout, err := hapchacha.EncryptAndSeal(key, dst, nonce, plaintext, verify)","handlingStrategy":"validation","validationCode":"if len(key32) != chacha20poly1305.KeySize || len(nonce8) != 8 {\n    return fmt.Errorf(\"need 32-byte key and 8-byte nonce, got %d/%d\", len(key32), len(nonce8))\n}","typeGuard":null,"tryCatchPattern":"out, err := hapchacha.DecryptAndVerify(key, dst, nonce, ct, verify)\nif err != nil {\n    if errors.Is(err, hapchacha.ErrInvalidParams) {\n        return fmt.Errorf(\"bad key/nonce sizes: key=%d nonce=%d\", len(key), len(nonce))\n    }\n    return err // genuine crypto/authentication failure\n}","preventionTips":["Slice HKDF output explicitly to chacha20poly1305.KeySize (32) and the nonce to 8 bytes.","Never reuse nonces from generic AEAD code (12-byte RFC 8439 nonces) with the HAP package.","Keep HAP key material separate from ed25519 keys — they are not interchangeable."],"tags":["crypto","chacha20poly1305","hap","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}