{"record":{"id":"8fa0bca4d75dee7f","repo":"OpenNHP/opennhp","slug":"failed-to-create-chain-hash-w","errorCode":null,"errorMessage":"failed to create chain hash: %w","messagePattern":"failed to create chain hash: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/initiator.go","lineNumber":136,"sourceCode":"\t\t}\n\n\t\t// create header and init device ecdh\n\t\tlog.Info(\"start encryption using CIPHER_SCHEME_%d(0: CURVE; 1: GMSM.)\", mad.CipherScheme)\n\t\tmad.header = mad.BasePacket.HeaderWithCipherScheme(mad.CipherScheme)\n\t\tmad.ciphers = NewCipherSuite(mad.CipherScheme)\n\t\tmad.deviceEcdh = d.GetEcdhByCipherScheme(mad.CipherScheme)\n\n\t\t// init version\n\t\tmad.header.SetVersion(ProtocolVersionMajor, ProtocolVersionMinor)\n\n\t\t// init header counter\n\t\tmad.header.SetCounter(mad.TransactionId)\n\t}\n\n\t// init chain hash -> ChainHash0\n\tmad.chainHash, err = NewHash(mad.ciphers.HashType)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create chain hash: %w\", err)\n\t}\n\tmad.chainHash.Write([]byte(InitialHashString))\n\n\t// init chain key -> ChainKey0\n\tmad.noise.HashType = mad.ciphers.HashType\n\tmad.noise.MixKey(&mad.chainKey, mad.chainHash.Sum(nil), []byte(InitialChainKeyString))\n\n\t// init timestamp\n\tmad.LocalInitTime = time.Now().UnixNano()\n\n\t// assign channel\n\tmad.ResponseMsgCh = md.ResponseMsgCh\n\n\t// init hmac hash -> HmacHash0\n\tmad.hmacHash, err = NewHash(mad.ciphers.HashType)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create HMAC hash: %w\", err)\n\t}","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/initiator.go#L118-L154","documentation":"createMsgAssemblerData initializes the per-message chain hash (ChainHash0) with NewHash for the negotiated cipher suite's hash type. If the hash algorithm cannot be instantiated, the wrapped error is returned. This indicates the configured ciphers.HashType is unsupported or crypto backend initialization failed.","triggerScenarios":"MsgToPacket or packet assembly calls createMsgAssemblerData with a MsgData whose ciphers.HashType is unknown/zero, or the underlying hash constructor fails (e.g. unregistered algorithm).","commonSituations":"Cipher-scheme mismatch after switching between CIPHER_SCHEME_CURVE and CIPHER_SCHEME_GMSM; manually built MsgData with an uninitialized Ciphers struct; missing SM3 support in a custom crypto build.","solutions":["Set md.Ciphers to a valid CipherSuites value (e.g. from the negotiated ConnData) before assembly","Ensure the cipher scheme constant is one of the supported values (CURVE or GMSM)","Test NewHash(hashType) directly to confirm the hash type is registered","Rebuild with full crypto backends if using a trimmed build"],"exampleFix":"// before\nmd := &MsgData{Msg: msg} // Ciphers zero-valued\n// after\nmd := &MsgData{Msg: msg, Ciphers: connData.Ciphers} // negotiated suite","handlingStrategy":"validation","validationCode":"if _, err := nhpcore.NewHash(md.Ciphers.HashType); err != nil {\n    return fmt.Errorf(\"unsupported hash type %v\", md.Ciphers.HashType)\n}","typeGuard":"func supportedSuite(c *nhpcore.CipherSuites) bool {\n    _, err := nhpcore.NewHash(c.HashType)\n    return err == nil\n}","tryCatchPattern":"mad, err := dev.MsgToPacket(md)\nif err != nil && strings.Contains(err.Error(), \"failed to create chain hash\") {\n    return fmt.Errorf(\"bad cipher config: %w\", err)\n}","preventionTips":["Use predefined cipher-suite constants, not hand-built structs","Verify crypto backends are compiled in (SM3 for GMSM)","Test crypto init at process startup"],"tags":["go","crypto","hash"],"backgroundTag":"invalid-enum-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"}