{"record":{"id":"dbf9f461d84d96ef","repo":"OpenNHP/opennhp","slug":"failed-to-create-chacha20-poly1305-w","errorCode":null,"errorMessage":"failed to create ChaCha20-Poly1305: %w","messagePattern":"failed to create ChaCha20-Poly1305: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/crypto.go","lineNumber":178,"sourceCode":"\t\t\treturn nil, fmt.Errorf(\"failed to create AES-GCM: %w\", err)\n\t\t}\n\t\treturn aead, nil\n\n\tcase GCM_SM4:\n\t\tsm4Block, err := sm4.NewCipher(key[:16])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create SM4 cipher: %w\", err)\n\t\t}\n\t\taead, err := cipher.NewGCM(sm4Block)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create SM4-GCM: %w\", err)\n\t\t}\n\t\treturn aead, nil\n\n\tcase GCM_CHACHA20POLY1305:\n\t\taead, err := chacha20poly1305.New(key[:])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create ChaCha20-Poly1305: %w\", err)\n\t\t}\n\t\treturn aead, nil\n\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported GCM type: %d\", t)\n\t}\n}\n\nfunc CBCEncryption(t GcmTypeEnum, key *[SymmetricKeySize]byte, plaintext []byte, inPlace bool) ([]byte, error) {\n\tvar block cipher.Block\n\tvar iv []byte\n\tvar err error\n\tswitch t {\n\tcase GCM_AES256:\n\t\tblock, err = aes.NewCipher(key[:])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create AES cipher for CBC: %w\", err)\n\t\t}","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/crypto.go#L160-L196","documentation":"AeadFromKey(GCM_CHACHA20POLY1305, key) wraps chacha20poly1305.New(key[:]) errors with this message. golang.org/x/crypto/chacha20poly1305.New fails only when the key is not exactly 32 bytes (chacha20poly1305.KeySize); OpenNHP passes the full 32-byte SymmetricKeySize array, so failure implies nil/mis-sized key material or dependency issues. The ChaCha20-Poly1305 suite is an alternative AEAD available in the GcmTypeEnum.","triggerScenarios":"Calling AeadFromKey with GCM_CHACHA20POLY1305 and a nil *[SymmetricKeySize]byte, an array not fully populated by the KDF, or a x/crypto build where chacha20poly1305 constants differ.","commonSituations":"Custom code selecting the ChaCha20-Poly1305 GCM type before the ECDH key derivation completes; unsafe casting of shorter slices into the fixed-size array; dependency pinning problems.","solutions":["Ensure the 32-byte key array is fully derived (ECDH shared secret + KDF) before calling AeadFromKey","Log the wrapped error — it will state the expected 32-byte key size","Verify SymmetricKeySize equals 32 and chacha20poly1305.KeySize matches in your build","Run go mod verify to ensure unmodified golang.org/x/crypto","Test with go test ./nhp/core/... to reproduce against known-good keys"],"exampleFix":"// before\nkey := make([]byte, 16) // wrong length, cast into fixed array\naead, _ := core.AeadFromKey(core.GCM_CHACHA20POLY1305, (*[core.SymmetricKeySize]byte)(unsafe.Pointer(&key[0])))\n// after\nshared := ecdh.SharedSecret(peerPub)\nif len(shared) != core.SymmetricKeySize {\n    return fmt.Errorf(\"derived key length %d, want %d\", len(shared), core.SymmetricKeySize)\n}\nvar key [core.SymmetricKeySize]byte\ncopy(key[:], shared)\naead, err := core.AeadFromKey(core.GCM_CHACHA20POLY1305, &key)\nif err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"if key == nil {\n    return errors.New(\"ChaCha20-Poly1305 key not initialized\")\n}\nif core.SymmetricKeySize != chacha20poly1305.KeySize {\n    return fmt.Errorf(\"SymmetricKeySize %d != required %d\", core.SymmetricKeySize, chacha20poly1305.KeySize)\n}","typeGuard":null,"tryCatchPattern":"aead, err := core.AeadFromKey(core.GCM_CHACHA20POLY1305, &key)\nif err != nil {\n    return fmt.Errorf(\"ChaCha20-Poly1305 setup failed: %w\", err)\n}","preventionTips":["Derive the full 32-byte key before selecting the ChaCha20-Poly1305 GCM type","Never cast arbitrary-length slices into the fixed-size key array","Verify golang.org/x/crypto integrity with go mod verify"],"tags":["crypto","chacha20poly1305","aead","key-length"],"backgroundTag":"invalid-argument-value","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}