-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attributable errors #60
Open
joostjager
wants to merge
4
commits into
lightningnetwork:master
Choose a base branch
from
joostjager:fat-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,049
−32
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b0bf831
add minPaddedOnionErrorLength constant
joostjager 7b82733
initialize NewOnionErrorEncrypter with shared secret directly
joostjager 78880cd
export GenerateSharedSecret
joostjager 787ad3d
add attributable error encryption and decryption
joostjager File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package sphinx | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"io" | ||
) | ||
|
||
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 | ||
) | ||
|
||
// AttrErrorStructure contains the parameters that define the structure | ||
// of the error message that is passed back. | ||
type AttrErrorStructure struct { | ||
// hopCount is the assumed maximum number of hops in the path. | ||
hopCount int | ||
|
||
// fixedPayloadLen is the length of the payload data that each hop along | ||
// the route can add. | ||
fixedPayloadLen int | ||
|
||
// hmacSize is the number of bytes that is reserved for each hmac. | ||
hmacSize int | ||
|
||
zeroHmac []byte | ||
} | ||
|
||
func NewAttrErrorStructure(hopCount int, fixedPayloadLen int, | ||
hmacSize int) *AttrErrorStructure { | ||
|
||
return &AttrErrorStructure{ | ||
hopCount: hopCount, | ||
fixedPayloadLen: fixedPayloadLen, | ||
hmacSize: hmacSize, | ||
|
||
zeroHmac: make([]byte, hmacSize), | ||
} | ||
} | ||
|
||
// HopCount returns the assumed maximum number of hops in the path. | ||
func (o *AttrErrorStructure) HopCount() int { | ||
return o.hopCount | ||
} | ||
|
||
// FixedPayloadLen returns the length of the payload data that each hop along | ||
// the route can add. | ||
func (o *AttrErrorStructure) FixedPayloadLen() int { | ||
return o.fixedPayloadLen | ||
} | ||
|
||
// HmacSize returns the number of bytes that is reserved for each hmac. | ||
func (o *AttrErrorStructure) HmacSize() int { | ||
return o.hmacSize | ||
} | ||
|
||
// totalHmacs is the total number of hmacs that is present in the failure | ||
// message. Every hop adds HopCount hmacs to the message, but as the error | ||
// back-propagates, downstream hmacs can be pruned. This results in the number | ||
// of hmacs for each hop decreasing by one for each step that we move away from | ||
// the current node. | ||
func (o *AttrErrorStructure) totalHmacs() int { | ||
return (o.hopCount * (o.hopCount + 1)) / 2 | ||
} | ||
|
||
// allHmacsLen is the total length in the bytes of all hmacs in the failure | ||
// message. | ||
func (o *AttrErrorStructure) allHmacsLen() int { | ||
return o.totalHmacs() * o.hmacSize | ||
} | ||
|
||
// hmacsAndPayloadsLen is the total length in bytes of all hmacs and payloads | ||
// together. | ||
func (o *AttrErrorStructure) hmacsAndPayloadsLen() int { | ||
return o.allHmacsLen() + o.allPayloadsLen() | ||
} | ||
|
||
// allPayloadsLen is the total length in bytes of all payloads in the failure | ||
// message. | ||
func (o *AttrErrorStructure) allPayloadsLen() int { | ||
return o.payloadLen() * o.hopCount | ||
} | ||
|
||
// payloadLen is the size of the per-node payload. It consists of a 1-byte | ||
// payload type followed by the payload data. | ||
func (o *AttrErrorStructure) payloadLen() int { | ||
return 1 + o.fixedPayloadLen | ||
} | ||
|
||
// message returns a slice containing the message in the given failure data | ||
// block. The message is positioned at the beginning of the block. | ||
func (o *AttrErrorStructure) message(data []byte) []byte { | ||
return data[:len(data)-o.hmacsAndPayloadsLen()] | ||
} | ||
|
||
// payloads returns a slice containing all payloads in the given failure | ||
// data block. The payloads follow the message in the block. | ||
func (o *AttrErrorStructure) payloads(data []byte) []byte { | ||
dataLen := len(data) | ||
|
||
return data[dataLen-o.hmacsAndPayloadsLen() : dataLen-o.allHmacsLen()] | ||
} | ||
|
||
// hmacs returns a slice containing all hmacs in the given failure data block. | ||
// The hmacs are positioned at the end of the data block. | ||
func (o *AttrErrorStructure) hmacs(data []byte) []byte { | ||
return data[len(data)-o.allHmacsLen():] | ||
} | ||
|
||
// calculateHmac calculates an hmac given a shared secret and a presumed | ||
// position in the path. Position is expressed as the distance to the error | ||
// source. The error source itself is at position 0. | ||
func (o *AttrErrorStructure) calculateHmac(sharedSecret Hash256, | ||
position int, message, payloads, hmacs []byte) []byte { | ||
|
||
umKey := generateKey("um", &sharedSecret) | ||
hash := hmac.New(sha256.New, umKey[:]) | ||
|
||
// Include message. | ||
_, _ = hash.Write(message) | ||
|
||
// Include payloads including our own. | ||
_, _ = hash.Write(payloads[:(position+1)*o.payloadLen()]) | ||
|
||
// Include downstream hmacs. | ||
writeDownstreamHmacs(position, o.hopCount, hmacs, o.hmacSize, hash) | ||
|
||
hmac := hash.Sum(nil) | ||
|
||
return hmac[:o.hmacSize] | ||
} | ||
|
||
// writeDownstreamHmacs writes the hmacs of downstream nodes that are relevant | ||
// for the given position to a writer instance. Position is expressed as the | ||
// distance to the error source. The error source itself is at position 0. | ||
func writeDownstreamHmacs(position, maxHops int, hmacs []byte, hmacBytes int, | ||
w io.Writer) { | ||
|
||
// Track the index of the next hmac to write in a variable. The first | ||
// maxHops slots are reserved for the hmacs of the current hop and can | ||
// therefore be skipped. The first hmac to write is part of the block of | ||
// hmacs that was written by the first downstream node. Which hmac | ||
// exactly is determined by the assumed position of the current node. | ||
var hmacIdx = maxHops + (maxHops - position - 1) | ||
|
||
// Iterate over all downstream nodes. | ||
for j := 0; j < position; j++ { | ||
_, _ = w.Write( | ||
hmacs[hmacIdx*hmacBytes : (hmacIdx+1)*hmacBytes], | ||
) | ||
|
||
// Calculate the total number of hmacs in the block of the | ||
// current downstream node. | ||
blockSize := maxHops - j - 1 | ||
|
||
// Skip to the next block. The new hmac index will point to the | ||
// hmac that corresponds to the next downstream node which is | ||
// one step closer to the assumed error source. | ||
hmacIdx += blockSize | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually an encrypted message right? So the ciphertext and not the plaintext.
If not, then we need to revise the proposal, as encrypt-then-mac is a must as we want to ensure integrity of the ciphertext. Otherwise the MAC may also give some information about the plaintext itself.
Might just have an out of date mental model tho....decided to take a look at the code before the spec to reaload some context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is hmac-then-encrypt. Legacy failures are also hmac-then-encrypt - at least for the error source because the other nodes only encrypt.
If the complete message including hmacs is encrypted as the final step before passing it back to the upstream node, how can information about the plaintext leak?