Mining/pkg/ueps/reader.go
Claude 41761823d3
ax(ueps): expand ReadAndVerify comment to show error sentinel branches
AX Principle 2 — comments as usage examples: the ReadAndVerify doc
comment only showed the success path; both errMissingHMAC and
errIntegrityViolation sentinel branches are now illustrated so callers
can copy-paste correct dispatch logic.

Co-Authored-By: Charon <charon@lethean.io>
2026-04-02 13:28:59 +01:00

112 lines
3.2 KiB
Go

package ueps
import (
"bufio"
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"io"
)
// packet, err := ReadAndVerify(bufio.NewReader(conn), secret)
// if err == errMissingHMAC { /* no HMAC tag found in frame */ }
var errMissingHMAC = sentinelError("UEPS packet missing HMAC signature")
// packet, err := ReadAndVerify(bufio.NewReader(conn), wrongSecret)
// if err == errIntegrityViolation { header.ThreatScore += 100; /* reject packet */ }
var errIntegrityViolation = sentinelError("integrity violation: HMAC mismatch")
// dispatch(packet.Header.IntentID, packet.Header.ThreatScore, packet.Payload)
type ParsedPacket struct {
Header UEPSHeader // packet.Header.Version == 0x09; packet.Header.IntentID == 0x01; packet.Header.ThreatScore == 0
Payload []byte // packet.Payload == []byte("hello world")
}
// packet, err := ueps.ReadAndVerify(bufio.NewReader(conn), []byte("my-shared-secret"))
// if err == errMissingHMAC { return } // unauthenticated: no HMAC tag in stream
// if err == errIntegrityViolation { return } // tampered: HMAC mismatch; reject and raise threat score
// if err == nil { dispatch(packet.Header.IntentID, packet.Header.ThreatScore, packet.Payload) }
func ReadAndVerify(reader *bufio.Reader, sharedSecret []byte) (*ParsedPacket, error) {
var hmacInputBuffer bytes.Buffer
var header UEPSHeader
var hmacSignature []byte
var payload []byte
for {
tagType, err := reader.ReadByte()
if err != nil {
return nil, err
}
if tagType == TagPayload {
payload, err = io.ReadAll(reader)
if err != nil {
return nil, err
}
break
}
tagValueLength, err := reader.ReadByte()
if err != nil {
return nil, err
}
tagValue := make([]byte, tagValueLength)
if _, err := io.ReadFull(reader, tagValue); err != nil {
return nil, err
}
switch tagType {
case TagVersion:
header.Version = tagValue[0]
hmacInputBuffer.WriteByte(tagType)
hmacInputBuffer.WriteByte(tagValueLength)
hmacInputBuffer.Write(tagValue)
case TagCurrentLayer:
header.CurrentLayer = tagValue[0]
hmacInputBuffer.WriteByte(tagType)
hmacInputBuffer.WriteByte(tagValueLength)
hmacInputBuffer.Write(tagValue)
case TagTargetLayer:
header.TargetLayer = tagValue[0]
hmacInputBuffer.WriteByte(tagType)
hmacInputBuffer.WriteByte(tagValueLength)
hmacInputBuffer.Write(tagValue)
case TagIntent:
header.IntentID = tagValue[0]
hmacInputBuffer.WriteByte(tagType)
hmacInputBuffer.WriteByte(tagValueLength)
hmacInputBuffer.Write(tagValue)
case TagThreatScore:
header.ThreatScore = binary.BigEndian.Uint16(tagValue)
hmacInputBuffer.WriteByte(tagType)
hmacInputBuffer.WriteByte(tagValueLength)
hmacInputBuffer.Write(tagValue)
case TagHMAC:
hmacSignature = tagValue
default:
hmacInputBuffer.WriteByte(tagType)
hmacInputBuffer.WriteByte(tagValueLength)
hmacInputBuffer.Write(tagValue)
}
}
if len(hmacSignature) == 0 {
return nil, errMissingHMAC
}
messageAuthCode := hmac.New(sha256.New, sharedSecret)
messageAuthCode.Write(hmacInputBuffer.Bytes())
messageAuthCode.Write(payload)
expectedMessageAuthCode := messageAuthCode.Sum(nil)
if !hmac.Equal(hmacSignature, expectedMessageAuthCode) {
return nil, errIntegrityViolation
}
return &ParsedPacket{
Header: header,
Payload: payload,
}, nil
}