{"record":{"id":"8626c6ab83368955","repo":"golang/go","slug":"crypto-sha512-invalid-hash-state-identifier","errorCode":null,"errorMessage":"crypto/sha512: invalid hash state identifier","messagePattern":"crypto/sha512: invalid hash state identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/boring/sha.go","lineNumber":518,"sourceCode":"\td := (*sha512Ctx)(unsafe.Pointer(&h.ctx))\n\tb = append(b, magic512...)\n\tb = byteorder.BEAppendUint64(b, d.h[0])\n\tb = byteorder.BEAppendUint64(b, d.h[1])\n\tb = byteorder.BEAppendUint64(b, d.h[2])\n\tb = byteorder.BEAppendUint64(b, d.h[3])\n\tb = byteorder.BEAppendUint64(b, d.h[4])\n\tb = byteorder.BEAppendUint64(b, d.h[5])\n\tb = byteorder.BEAppendUint64(b, d.h[6])\n\tb = byteorder.BEAppendUint64(b, d.h[7])\n\tb = append(b, d.x[:d.nx]...)\n\tb = append(b, make([]byte, len(d.x)-int(d.nx))...)\n\tb = byteorder.BEAppendUint64(b, d.nl>>3|d.nh<<61)\n\treturn b, nil\n}\n\nfunc (h *sha384Hash) UnmarshalBinary(b []byte) error {\n\tif len(b) < len(magic512) {\n\t\treturn errors.New(\"crypto/sha512: invalid hash state identifier\")\n\t}\n\tif string(b[:len(magic384)]) != magic384 {\n\t\treturn errors.New(\"crypto/sha512: invalid hash state identifier\")\n\t}\n\tif len(b) != marshaledSize512 {\n\t\treturn errors.New(\"crypto/sha512: invalid hash state size\")\n\t}\n\td := (*sha512Ctx)(unsafe.Pointer(&h.ctx))\n\tb = b[len(magic512):]\n\tb, d.h[0] = consumeUint64(b)\n\tb, d.h[1] = consumeUint64(b)\n\tb, d.h[2] = consumeUint64(b)\n\tb, d.h[3] = consumeUint64(b)\n\tb, d.h[4] = consumeUint64(b)\n\tb, d.h[5] = consumeUint64(b)\n\tb, d.h[6] = consumeUint64(b)\n\tb, d.h[7] = consumeUint64(b)\n\tb = b[copy(d.x[:], b):]","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/boring/sha.go#L500-L536","documentation":"Returned by sha384Hash.UnmarshalBinary in the BoringCrypto SHA-512 backend when the blob is shorter than magic512, so the prefix cannot even be read safely. SHA-384 and SHA-512 share the 512-bit block context and the magic512 prefix, but the SHA-384 method additionally requires the prefix to equal magic384.","triggerScenarios":"Calling UnmarshalBinary on a sha512.New384() hash with a blob shorter than len(magic512).","commonSituations":"Empty or near-empty state blob; truncated persisted state; feeding a SHA-256 blob into a SHA-384 hasher.","solutions":["Validate len(state) >= len(magic512) before unmarshalling.","Regenerate the blob from the current Go build.","Re-hash instead of persisting state."],"exampleFix":"// before\nh := sha512.New384()\nh.(encoding.BinaryUnmarshalser).UnmarshalBinary(shortState)\n// after\nif len(state) < 6 { return errors.New(\"state too short\") }\nh.(encoding.BinaryUnmarshalser).UnmarshalBinary(state)","handlingStrategy":"validation","validationCode":"func validSHA512FamilyState(s []byte) bool {\n    return len(s) >= 6 // len(magic512)\n}","typeGuard":"// n/a","tryCatchPattern":"if err := h384.(encoding.BinaryUnmarshalser).UnmarshalBinary(state); err != nil {\n    return err\n}","preventionTips":["Reject blobs shorter than the family magic before unmarshalling.","Re-hash instead of restoring state across builds.","Tag blobs by algorithm externally."],"tags":["crypto","sha512","sha384","boringcrypto","serialization","hash"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}