{"record":{"id":"e4f17620bfac8a62","repo":"OpenNHP/opennhp","slug":"failed-to-create-aes-cipher-for-cbc-decryption-w","errorCode":null,"errorMessage":"failed to create AES cipher for CBC decryption: %w","messagePattern":"failed to create AES cipher for CBC decryption: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/crypto.go","lineNumber":244,"sourceCode":"\t\tciphertext = make([]byte, 0, len(plaintext))\n\t}\n\n\tmode := cipher.NewCBCEncrypter(block, iv)\n\t// CryptBlocks can work in-place if the two arguments are the same.\n\tmode.CryptBlocks(ciphertext, paddedPlainText)\n\n\treturn ciphertext, nil\n}\n\nfunc CBCDecryption(t GcmTypeEnum, key *[SymmetricKeySize]byte, ciphertext []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 decryption: %w\", err)\n\t\t}\n\t\tiv = key[8:24]\n\n\tcase GCM_SM4:\n\t\tblock, err = sm4.NewCipher(key[:16])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create SM4 cipher for CBC decryption: %w\", err)\n\t\t}\n\t\tiv = key[16:]\n\n\tcase GCM_CHACHA20POLY1305:\n\t\treturn nil, ErrNotApplicable\n\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported cipher type for CBC decryption: %d\", t)\n\t}\n\n\t// Validate ciphertext: must be at least one block and a multiple of block size","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/crypto.go#L226-L262","documentation":"In CBCDecryption's GCM_AES256 branch, aes.NewCipher(key[:]) failed. Go's AES implementation only accepts 16/24/32-byte keys, so this wraps the underlying key-length error. With SymmetricKeySize keys this should never occur, meaning the caller effectively passed a wrong-sized or corrupted key buffer.","triggerScenarios":"Calling CBCDecryption with GCM_AES256 and a *[SymmetricKeySize]byte whose backing bytes were constructed from a key of invalid AES length (not 16/24/32 bytes), typically a truncated, mis-decoded, or wrong-format key.","commonSituations":"Key loaded from a hex/base64 string decoded to a non-standard length; key material from an older config version with a different key size; hand-built byte arrays instead of the library's keygen output.","solutions":["Generate keys with the daemon's `keygen --curve` output or the library's key derivation so the AES key is exactly 32 bytes.","Check that the key was fully decoded (base64/hex) and not truncated before wrapping it in the [SymmetricKeySize]byte array.","Confirm the config.toml private key matches the curve/scheme expected (curve25519/AES vs SM2/SM4).","Log/inspect the wrapped %w error; it names the exact key-length violation."],"exampleFix":"// before\nkeyBytes, _ := hex.DecodeString(cfg.Key) // may be 24 or 40 bytes\nvar key [core.SymmetricKeySize]byte\ncopy(key[:], keyBytes)\n// after\nif len(keyBytes) != core.SymmetricKeySize {\n    return fmt.Errorf(\"key must be %d bytes, got %d\", core.SymmetricKeySize, len(keyBytes))\n}\ncopy(key[:], keyBytes)","handlingStrategy":"validation","validationCode":"if len(keyBytes) != core.SymmetricKeySize {\n    return fmt.Errorf(\"AES key must be %d bytes, got %d\", core.SymmetricKeySize, len(keyBytes))\n}","typeGuard":null,"tryCatchPattern":"plain, err := core.CBCDecryption(core.GCM_AES256, key, ct, false)\nif err != nil {\n    return fmt.Errorf(\"CBC decrypt failed (check key material): %w\", err)\n}","preventionTips":["Generate keys with the daemon keygen command instead of hand-built byte arrays.","Assert key length immediately after decoding from config or network.","Keep AES and SM4 key material in separate, clearly named config fields."],"tags":["go","crypto","aes","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"}