{"record":{"id":"6c17c637dd445e5d","repo":"OpenNHP/opennhp","slug":"failed-to-create-sm4-gcm-w","errorCode":null,"errorMessage":"failed to create SM4-GCM: %w","messagePattern":"failed to create SM4-GCM: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/crypto.go","lineNumber":171,"sourceCode":"\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\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","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/crypto.go#L153-L189","documentation":"AeadFromKey wraps cipher.NewGCM(sm4Block) failure as this message. As with AES, cipher.NewGCM only errors when the underlying block size is not 16 bytes; gmsm's SM4 block cipher always uses a 16-byte block, making this error practically unreachable and purely defensive. It would indicate a non-conforming SM4 implementation in the dependency graph.","triggerScenarios":"cipher.NewGCM receiving an SM4 block cipher whose BlockSize() != 16 — only feasible with a modified/forked emmansun/gmsm package.","commonSituations":"Forked or vendored gmsm libraries, corrupted modules, or custom builds replacing the SM4 implementation. Not expected in standard OpenNHP deployments.","solutions":["Run 'go mod verify' and restore the canonical github.com/emmansun/gmsm dependency","Check go.mod replace directives for gmsm forks","Log the wrapped error from the %w chain for the underlying message","Rebuild with a current Go toolchain and rerun the core cipher tests"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"block, err := sm4.NewCipher(key[:16])\nif err == nil && block.BlockSize() != 16 {\n    return fmt.Errorf(\"unexpected SM4 block size %d\", block.BlockSize())\n}","typeGuard":null,"tryCatchPattern":"aead, err := core.AeadFromKey(core.GCM_SM4, &key)\nif err != nil {\n    return fmt.Errorf(\"SM4-GCM init failed: %w\", err)\n}","preventionTips":["Keep github.com/emmansun/gmsm unmodified; verify with go mod verify","Avoid forks replacing the SM4 block implementation","Treat occurrences as dependency integrity failures, not data problems"],"tags":["crypto","sm4","gcm","gmsm"],"backgroundTag":"internal-invariant-violation","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"}