{"record":{"id":"c0b776629253a26f","repo":"golang/go","slug":"crypto-sha256-invalid-hash-state-size-c0b776","errorCode":null,"errorMessage":"crypto/sha256: invalid hash state size","messagePattern":"crypto/sha256: invalid hash state size","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/fips140/sha256/sha256.go","lineNumber":94,"sourceCode":"\tb = byteorder.BEAppendUint32(b, d.h[1])\n\tb = byteorder.BEAppendUint32(b, d.h[2])\n\tb = byteorder.BEAppendUint32(b, d.h[3])\n\tb = byteorder.BEAppendUint32(b, d.h[4])\n\tb = byteorder.BEAppendUint32(b, d.h[5])\n\tb = byteorder.BEAppendUint32(b, d.h[6])\n\tb = byteorder.BEAppendUint32(b, d.h[7])\n\tb = append(b, d.x[:d.nx]...)\n\tb = append(b, make([]byte, len(d.x)-d.nx)...)\n\tb = byteorder.BEAppendUint64(b, d.len)\n\treturn b, nil\n}\n\nfunc (d *Digest) UnmarshalBinary(b []byte) error {\n\tif len(b) < len(magic224) || (d.is224 && string(b[:len(magic224)]) != magic224) || (!d.is224 && string(b[:len(magic256)]) != magic256) {\n\t\treturn errors.New(\"crypto/sha256: invalid hash state identifier\")\n\t}\n\tif len(b) != marshaledSize {\n\t\treturn errors.New(\"crypto/sha256: invalid hash state size\")\n\t}\n\tb = b[len(magic224):]\n\tb, d.h[0] = consumeUint32(b)\n\tb, d.h[1] = consumeUint32(b)\n\tb, d.h[2] = consumeUint32(b)\n\tb, d.h[3] = consumeUint32(b)\n\tb, d.h[4] = consumeUint32(b)\n\tb, d.h[5] = consumeUint32(b)\n\tb, d.h[6] = consumeUint32(b)\n\tb, d.h[7] = consumeUint32(b)\n\tb = b[copy(d.x[:], b):]\n\tb, d.len = consumeUint64(b)\n\td.nx = int(d.len % chunk)\n\treturn nil\n}\n\nfunc consumeUint64(b []byte) ([]byte, uint64) {\n\treturn b[8:], byteorder.BEUint64(b)","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/fips140/sha256/sha256.go#L76-L112","documentation":"Returned by sha256 Digest.UnmarshalBinary when the magic prefix is correct but the total byte length is not marshaledSize. The serialized layout is fixed-length (magic + 8 x uint32 state + 64-byte block buffer + uint64 length counter); any truncation or extra trailing bytes fail this check.","triggerScenarios":"Passing bytes whose magic is valid but length differs from the fixed marshaledSize constant: a buffer truncated by a length-prefixed framer, a buffer with extra bytes appended by a buggy serializer, or a buffer from a different Go version whose marshaled layout differs.","commonSituations":"Database BLOB column sized too small and silently truncating; a copy/paste that lost trailing bytes; version skew between the Go build that marshaled the state and the one unmarshaling it (the marshaled size has historically been stable but is not an API contract).","solutions":["Ensure the full marshaledSize bytes are stored and retrieved unchanged (verify byte-for-byte length).","If the data passes through a length-prefixed protocol, check the length prefix matches the expected marshaledSize before forwarding.","Re-hash from the original input if the state is unrecoverable.","Pin the producer and consumer to the same Go major version when persisting hash state long-term."],"exampleFix":"// before\nbuf := stored[:len(stored)-4] // accidentally truncated\nd.UnmarshalBinary(buf) // valid magic, wrong size -> error\n// after\nd.UnmarshalBinary(stored) // full marshaledSize bytes","handlingStrategy":"validation","validationCode":"if len(b) != marshaledSize {\n    return fmt.Errorf(\"sha256 state size mismatch: got %d want %d\", len(b), marshaledSize)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify byte-for-byte length of persisted hash state on read and write.","Use io.ReadFull and check the error when loading the buffer.","Pin producer and consumer to the same Go major version for long-lived persisted state."],"tags":["crypto","hash","sha256","serialization","go-stdlib"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}