{"record":{"id":"788b029be631845f","repo":"OpenNHP/opennhp","slug":"invalid-key-length-for-aes-256-gcm","errorCode":null,"errorMessage":"invalid key length for AES-256-GCM","messagePattern":"invalid key length for AES-256-GCM","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/ztdo/noise.go","lineNumber":111,"sourceCode":"\t\treturn AES256GCM112Tag, nil\n\tcase \"AES-256-GCM-120\":\n\t\treturn AES256GCM120Tag, nil\n\tcase \"AES-256-GCM-128\":\n\t\treturn AES256GCM128Tag, nil\n\tcase \"SM4-GCM-64\":\n\t\treturn SM4GCM64Tag, nil\n\tcase \"SM4-GCM-128\":\n\t\treturn SM4GCM128Tag, nil\n\tdefault:\n\t\treturn 0, fmt.Errorf(\"unknown symmetric mode name: %s\", mode)\n\t}\n}\nfunc (mode SymmetricCipherMode) newCipherBlock(key []byte) (cipher.Block, error) {\n\tswitch mode {\n\tcase AES256GCM64Tag, AES256GCM96Tag, AES256GCM104Tag,\n\t\tAES256GCM112Tag, AES256GCM120Tag, AES256GCM128Tag:\n\t\tif len(key) != 32 {\n\t\t\treturn nil, fmt.Errorf(\"invalid key length for AES-256-GCM\")\n\t\t}\n\t\treturn aes.NewCipher(key)\n\tcase SM4GCM64Tag, SM4GCM128Tag:\n\t\tif len(key) < 16 {\n\t\t\treturn nil, fmt.Errorf(\"invalid key length for SM4-GCM\")\n\t\t} else {\n\t\t\tkey = key[:16]\n\t\t}\n\t\treturn sm4.NewCipher(key)\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported mode: %v\", mode)\n\t}\n}\n\nfunc (mode SymmetricCipherMode) Encrypt(key, nonce, plaintext, ad []byte) ([]byte, error) {\n\ttagSize := mode.TagSize()\n\n\tcipherBlock, err := mode.newCipherBlock(key)","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/ztdo/noise.go#L93-L129","documentation":"newCipherBlock validates the key length before constructing the underlying cipher block. AES-256-GCM modes require exactly a 32-byte (256-bit) key; any other length returns this error. Called from Encrypt/Decrypt whenever an AES mode is used with a wrongly sized key.","triggerScenarios":"Encrypt or Decrypt invoked on an AES256GCM* SymmetricCipherMode with len(key) != 32 (nhp/core/ztdo/noise.go:111).","commonSituations":"Keys derived with the wrong hash output size, base64/hex keys not decoded before use, truncating or padding keys during key derivation, mixing SM4 (16-byte) keys with AES modes.","solutions":["Ensure the key is exactly 32 bytes before calling Encrypt/Decrypt","Decode base64/hex-encoded keys to raw bytes; don't pass the encoded string","Check your key-derivation function's output length (e.g. HKDF expand to 32 bytes)","Verify you selected the correct cipher mode for your key size"],"exampleFix":"// before\nblock, err := mode.newCipherBlock([]byte(base64Key)) // wrong length/encoding\n// after\nraw, _ := base64.StdEncoding.DecodeString(base64Key) // must be 32 bytes\nblock, err := mode.newCipherBlock(raw)","handlingStrategy":"validation","validationCode":"if len(key) != 32 {\n\treturn fmt.Errorf(\"AES-256-GCM requires a 32-byte key, got %d\", len(key))\n}","typeGuard":null,"tryCatchPattern":"ct, err := mode.Encrypt(key, nonce, plaintext, ad)\nif err != nil {\n\tif strings.Contains(err.Error(), \"invalid key length for AES-256-GCM\") {\n\t\t// re-derive or decode the key to 32 bytes and retry\n\t}\n\treturn err\n}","preventionTips":["Always derive AES keys to exactly 32 bytes (HKDF/SHA-256)","Decode base64/hex keys before use","Add key-length assertions in tests for every cipher path"],"tags":["go","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"}