Skip to content

Commit

Permalink
rename payload type to source [squash]
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Feb 8, 2023
1 parent 3b52023 commit a8fce5c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
13 changes: 13 additions & 0 deletions attributable_error_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import (
"crypto/sha256"
)

type payloadSource byte

const (
// payloadIntermediateNode is a marker to signal that this attributable
// error payload is originating from a node between the payer and the error
// source.
payloadIntermediateNode payloadSource = 0

// payloadErrorNode is a marker to signal that this attributable error
// payload is originating from the error source.
payloadErrorNode payloadSource = 1
)

type AttributableErrorStructure struct {
HopCount int
FixedPayloadLen int
Expand Down
31 changes: 13 additions & 18 deletions attributable_error_decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (o *OnionAttributableErrorDecrypter) DecryptError(encryptedData []byte) (
}

// Extract the payload and exit with a nil message if it is invalid.
payloadType, payload, err := o.extractPayload(payloads)
source, payload, err := o.extractPayload(payloads)
if sender == 0 {
if err != nil {
sender = i + 1
Expand All @@ -118,7 +118,7 @@ func (o *OnionAttributableErrorDecrypter) DecryptError(encryptedData []byte) (

// If we are at the node that is the source of the error, we can now
// save the message in our return variable.
if payloadType == payloadFinal {
if source == payloadErrorNode {
sender = i + 1
msg = message
}
Expand Down Expand Up @@ -149,11 +149,6 @@ func (o *OnionAttributableErrorDecrypter) DecryptError(encryptedData []byte) (
}, nil
}

const (
payloadFinal = 1
payloadIntermediate = 0
)

func (o *OnionAttributableErrorDecrypter) shiftHmacsLeft(hmacs []byte) {
srcIdx := o.maxHops
destIdx := 1
Expand All @@ -174,21 +169,21 @@ func (o *OnionAttributableErrorDecrypter) shiftPayloadsLeft(payloads []byte) {
copy(payloads, payloads[o.payloadLen:o.maxHops*o.payloadLen])
}

func (o *OnionAttributableErrorDecrypter) extractPayload(payloads []byte) (
PayloadType, []byte, error) {

var payloadType PayloadType
// extractPayload extracts the payload and payload origin information from the
// given byte slice.
func (o *OnionAttributableErrorDecrypter) extractPayload(payloadBytes []byte) (
payloadSource, []byte, error) {

switch payloads[0] {
case payloadFinal, payloadIntermediate:
payloadType = PayloadType(payloads[0])
source := payloadSource(payloadBytes[0])

default:
return 0, nil, errors.New("invalid payload type")
// Validate source indicator.
if source != payloadErrorNode && source != payloadIntermediateNode {
return 0, nil, errors.New("invalid payload source indicator")
}

// Extract payload.
payload := make([]byte, o.payloadDataLen)
copy(payload, payloads[1:o.payloadLen])
copy(payload, payloadBytes[1:o.payloadLen])

return payloadType, payload, nil
return source, payload, nil
}
10 changes: 6 additions & 4 deletions attributable_error_encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (o *OnionAttributableErrorEncrypter) initializePayload(message []byte,
_, payloads, _ := o.getMsgComponents(data)

// Signal final hops in the payload.
addPayload(payloads, payloadFinal, payload)
addPayload(payloads, payloadErrorNode, payload)

return data
}
Expand All @@ -127,10 +127,12 @@ func (o *OnionAttributableErrorEncrypter) addIntermediatePayload(data []byte,
o.shiftHmacsRight(hmacs)

// Signal intermediate hop in the payload.
addPayload(payloads, payloadIntermediate, payload)
addPayload(payloads, payloadIntermediateNode, payload)
}

func addPayload(payloads []byte, payloadType PayloadType, payload []byte) {
payloads[0] = byte(payloadType)
func addPayload(payloads []byte, source payloadSource,
payload []byte) {

payloads[0] = byte(source)
copy(payloads[1:], payload)
}

0 comments on commit a8fce5c

Please sign in to comment.