{"record":{"id":"b54fd115401c348c","repo":"OpenNHP/opennhp","slug":"failed-to-create-aes-cipher-w","errorCode":null,"errorMessage":"failed to create AES cipher: %w","messagePattern":"failed to create AES cipher: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/crypto.go","lineNumber":156,"sourceCode":"\nfunc NewECDH(t EccTypeEnum) (e Ecdh) {\n\tswitch t {\n\tcase ECC_CURVE25519:\n\t\te = curve.NewECDH()\n\n\tcase ECC_SM2:\n\t\te = gmsm.NewECDH()\n\t}\n\n\treturn e\n}\n\nfunc AeadFromKey(t GcmTypeEnum, key *[SymmetricKeySize]byte) (cipher.AEAD, error) {\n\tswitch t {\n\tcase GCM_AES256:\n\t\taesBlock, err := aes.NewCipher(key[:])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create AES cipher: %w\", err)\n\t\t}\n\t\taead, err := cipher.NewGCM(aesBlock)\n\t\tif err != nil {\n\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","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/crypto.go#L138-L174","documentation":"AeadFromKey(GCM_AES256, key) wraps the error from aes.NewCipher(key[:]) when the key material cannot form an AES block cipher. Go's crypto/aes only fails when the key length is not 16, 24, or 32 bytes; since key is a *[SymmetricKeySize]byte (32 bytes), this error indicates the caller supplied a nil pointer, mis-sized buffer, or corrupted key derivation. It is a programming/key-derivation bug signal, not a runtime network condition.","triggerScenarios":"Calling AeadFromKey with a *[SymmetricKeySize]byte that was never filled (all code paths normally pass exactly 32 bytes), or future refactors that change SymmetricKeySize to a non-AES-legal length, or passing a key derived from an ECDH shared-secret routine that returned garbage/empty data.","commonSituations":"A key-exchange step silently failed earlier and left the symmetric key zero-length or mis-sized; custom code constructs the array via unsafe casting of a shorter byte slice; tests exercising GCM with a stubbed key.","solutions":["Ensure the key array is fully populated by a successful ECDH/SharedSecret + KDF before calling AeadFromKey","Check the wrapped error — with a fixed-size array it points to dependency-level or pointer-level problems","Verify SymmetricKeySize is 32 (AES-256) and unchanged in your build","Add a caller-side assert that the key is non-zero before use","Run the cipher-suite tests (go test ./nhp/core/...) to reproduce with known-good keys"],"exampleFix":"// before\nkey := &[core.SymmetricKeySize]byte{}\naead, err := core.AeadFromKey(core.GCM_AES256, key)\n// after\nshared := ecdh.SharedSecret(peerPub)\nif len(shared) == 0 {\n    return errors.New(\"key exchange produced empty shared secret\")\n}\nvar key [core.SymmetricKeySize]byte\nutils.Memcpy(key[:], kdf(shared))\naead, err := core.AeadFromKey(core.GCM_AES256, &key)\nif err != nil {\n    return fmt.Errorf(\"aead init: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if key == nil {\n    return errors.New(\"symmetric key not initialized\")\n}\nif core.SymmetricKeySize != 16 && core.SymmetricKeySize != 24 && core.SymmetricKeySize != 32 {\n    return fmt.Errorf(\"SymmetricKeySize %d is not a valid AES key length\", core.SymmetricKeySize)\n}","typeGuard":null,"tryCatchPattern":"aead, err := core.AeadFromKey(core.GCM_AES256, &key)\nif err != nil {\n    return fmt.Errorf(\"AES-GCM setup failed: %w\", err)\n}\n// use aead.Seal / aead.Open only after this succeeds","preventionTips":["Complete ECDH + KDF before constructing any AEAD","Never construct the key array from arbitrary-length slices","Keep SymmetricKeySize at 32 and check after dependency upgrades","Cover AeadFromKey with unit tests using known-good keys"],"tags":["crypto","aes","gcm","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"}