Repository navigation
#4: Add specification onion error obfuscation - #8
Conversation
ad0a979 to
f33493c
Compare
Add BOLT#4 data obfuscation, which will be used inside lnd in order to obfuscate the payment failures.
f33493c to
f6a3f30
Compare
f6a3f30 to
3fe194e
Compare
Roasbeef
left a comment
There was a problem hiding this comment.
Nice! I've been anticpating this PR so we can finally be compliant with the way encrypted errors are defined within the spec.
Most of my comments are minor, the only major one being a bug I observed when handling error replies by the originator of a payment.
Carrying over my comment from the other PR, a sphinx.Circuit object (fields defined in that comment) should be added to the package.
| }, nil | ||
| } | ||
|
|
||
| // Obfuscate is used to make data obfuscation. |
There was a problem hiding this comment.
The paragraph should be properly folded to 80 characters.
| "github.com/roasbeef/btcd/btcec" | ||
| ) | ||
|
|
||
| // onionObfuscation obfuscates the data with compliance with BOLT#4. |
There was a problem hiding this comment.
Either a newline should be inserted, or the line below should be properly wrapped (respecting the 80 character limit) into the one above.
| ErrReplayedPacket = fmt.Errorf("sphinx packet replay attempted") | ||
|
|
||
| // ErrInvalidOnionVersion is returned during decoding of the onion packet, when | ||
| // received packet has unknown version byte. |
There was a problem hiding this comment.
when -> when the
has unknown -> has an unknown
| ErrInvalidOnionVersion = fmt.Errorf("invalid onion packet version") | ||
|
|
||
| // ErrInvalidOnionHMAC is returned during onion parsing process, when received | ||
| // mac do not corresponds to the generated one. |
| ErrInvalidOnionHMAC = fmt.Errorf("invalid mismathed mac") | ||
|
|
||
| // ErrInvalidOnionKey is returned during onion parsing process, when | ||
| // onion key is invalid - not belongs to the secp256k1 curve. |
There was a problem hiding this comment.
Latter portion of the comment should likely be re-written a bit.
| // obfuscation in reverse order. | ||
| func (o *OnionDeobfuscator) Deobfuscate(obfuscatedData []byte) (*btcec.PublicKey, | ||
| []byte, error) { | ||
| for i, sharedSecret := range generateSharedSecrets(o.paymentPath, |
There was a problem hiding this comment.
I may be mis-understanding something, but to me, as currently, written this function is semantically incorrect. Correct me if I'm wrong, but instead we need a double for-loop in order to attempt to decode the packet properly. This is due to the fact that any of the participants on the original route may have originally encrypted the packet. As a result, there are N^2 different possibilities.
Let's take a route of A -> B -> C. Let's say that B was the one who originally created the error. From my reading, this loop would first start with C, apply the stream cipher bytes, then proceed to B finally. This would result in an invalid final packet as C's stream cipher bytes were erroneously applied.
There was a problem hiding this comment.
It seems this function assumes that the error will always be sent by the last participant in a route. This is an incorrect assumption.
| } | ||
|
|
||
| // Decode initializes the deobfuscator from the byte stream. | ||
| func (o *OnionDeobfuscator) Decode(r io.Reader) error { |
There was a problem hiding this comment.
Are these encode and decode methods really necessary? Where are they used?
I guess we may use them within lnd later when we start to persist the raw circuit information as well? In that case they should be removed to the to-be-create sphinx.Circuit struct.
|
|
||
| // Emulate creation of the obfuscator on node where error have occurred. | ||
| obfuscator := &OnionObfuscator{ | ||
| sharedSecret: sharedSecrets[len(errorPath)-1], |
There was a problem hiding this comment.
The test should also test the case of the error originating from a node that isn't the last hop. I think this would've exposed the issue I pointed out above.
There was a problem hiding this comment.
I am already doing this, I reduced the path slightly higher in the diff.
obfuscation_test.go
26: + // Reduce the error path on one node, in order to check that we are
27: + // able to receive the error not only from last hop.
28: + errorPath := paymentPath[:len(paymentPath)-1]
| "9b8e6faa83b81fbfa6133c0af5d6b07c017f7158fa94f0d206ba" + | ||
| "f12dda6b68f785b773b360fd", | ||
| }, | ||
|
|
There was a problem hiding this comment.
Please remove the extra new lines in-between successive elements of the slice literal.
| func NewOnionPacket(paymentPath []*btcec.PublicKey, sessionKey *btcec.PrivateKey, | ||
| hopsData []HopData, assocData []byte) (*OnionPacket, error) { | ||
|
|
||
| func generateSharedSecrets(paymentPath []*btcec.PublicKey, |
In order to properly handle errors during decoding and parsing process the distinct variables of errors have been created.
231f731 to
c3968ec
Compare
Issue: #4