@@ -10,56 +10,54 @@ import (
1010 "github.com/roasbeef/btcd/btcec"
1111)
1212
13- // onionObfuscation obfuscates the data with compliance with BOLT#4.
14- //
15- // In context of Lightning Network this function is used by sender to obfuscate
16- // the onion failure and by receiver to unwrap the failure data.
17- func onionObfuscation (sharedSecret [sha256 .Size ]byte ,
18- data []byte ) []byte {
19- obfuscatedData := make ([]byte , len (data ))
13+ // onionEncrypt obfuscates the data with compliance with BOLT#4. As we use a
14+ // stream cipher, calling onionEncrypt on an already encrypted piece of data
15+ // will decrypt it.
16+ func onionEncrypt (sharedSecret [sha256 .Size ]byte , data []byte ) []byte {
17+
18+ p := make ([]byte , len (data ))
2019
2120 ammagKey := generateKey ("ammag" , sharedSecret )
2221 streamBytes := generateCipherStream (ammagKey , uint (len (data )))
23- xor (obfuscatedData , data , streamBytes )
24- return obfuscatedData
22+ xor (p , data , streamBytes )
23+
24+ return p
2525}
2626
27- // OnionObfuscator represent serializable object which is able to convert the
28- // data to the obfuscated blob, by applying the stream of data generated by
29- // the shared secret.
30- //
31- // In context of Lightning Network the obfuscated data is usually a failure
32- // which will be propagated back to payment sender, and obfuscated by the
33- // forwarding nodes.
34- type OnionObfuscator struct {
27+ // OnionErrorEncrypter is a struct that's used to implement onion error
28+ // encryption as defined within BOLT0004.
29+ type OnionErrorEncrypter struct {
3530 sharedSecret [sha256 .Size ]byte
3631}
3732
38- // NewOnionObfuscator creates new instance of onion obfuscator.
39- func NewOnionObfuscator (router * Router , ephemeralKey * btcec.PublicKey ) (* OnionObfuscator ,
40- error ) {
33+ // NewOnionErrorEncrypter creates new instance of the onion encryper backed by
34+ // the passed router, with encryption to be doing using the passed
35+ // ephemeralKey.
36+ func NewOnionErrorEncrypter (router * Router ,
37+ ephemeralKey * btcec.PublicKey ) (* OnionErrorEncrypter , error ) {
4138
4239 sharedSecret , err := router .generateSharedSecret (ephemeralKey )
4340 if err != nil {
4441 return nil , err
4542 }
4643
47- return & OnionObfuscator {
44+ return & OnionErrorEncrypter {
4845 sharedSecret : sharedSecret ,
4946 }, nil
5047}
5148
52- // Obfuscate is used to make data obfuscation using the generated shared secret.
49+ // EncryptError is used to make data obfuscation using the generated shared
50+ // secret.
5351//
54- // In context of Lightning Network is either used by the nodes in order to
55- // make initial obfuscation with the creation of the hmac or by the forwarding
56- // nodes for backward failure obfuscation of the onion failure blob. By
57- // obfuscating the onion failure on every node in the path we are adding
58- // additional step of the security and barrier for malware nodes to retrieve
59- // valuable information. The reason for using onion obfuscation is to not give
60- // away to the nodes in the payment path the information about the exact failure
61- // and its origin.
62- func (o * OnionObfuscator ) Obfuscate (initial bool , data []byte ) []byte {
52+ // In context of Lightning Network is either used by the nodes in order to make
53+ // initial obfuscation with the creation of the hmac or by the forwarding nodes
54+ // for backward failure obfuscation of the onion failure blob. By obfuscating
55+ // the onion failure on every node in the path we are adding additional step of
56+ // the security and barrier for malware nodes to retrieve valuable information.
57+ // The reason for using onion obfuscation is to not give
58+ // away to the nodes in the payment path the information about the exact
59+ // failure and its origin.
60+ func (o * OnionErrorEncrypter ) EncryptError (initial bool , data []byte ) []byte {
6361 if initial {
6462 umKey := generateKey ("um" , o .sharedSecret )
6563 hash := hmac .New (sha256 .New , umKey [:])
@@ -68,9 +66,7 @@ func (o *OnionObfuscator) Obfuscate(initial bool, data []byte) []byte {
6866 data = append (h , data ... )
6967 }
7068
71- return onionObfuscation (o .sharedSecret , data )
72- }
73-
69+ return onionEncrypt (o .sharedSecret , data )
7470}
7571
7672// Circuit is used encapsulate the data which is needed for data deobfuscation.
@@ -145,26 +141,24 @@ func (c *Circuit) Encode(w io.Writer) error {
145141 return nil
146142}
147143
148- // OnionDeobfuscator represents the serializable object which encapsulate the
149- // all necessary data to properly de-obfuscate previously obfuscated data.
150- // In context of Lightning Network the data which have to be deobfuscated
151- // usually is onion failure.
152- type OnionDeobfuscator struct {
144+ // OnionErrorDecrypter is a struct that's used to decrypt onion errors in
145+ // response to failed HTLC routing attempts according to BOLT#4.
146+ type OnionErrorDecrypter struct {
153147 circuit * Circuit
154148}
155149
156- // NewOnionDeobfuscator creates new instance of onion deobfuscator .
157- func NewOnionDeobfuscator (circuit * Circuit ) * OnionDeobfuscator {
158- return & OnionDeobfuscator {
150+ // NewOnionErrorDecrypter creates new instance of onion decrypter .
151+ func NewOnionErrorDecrypter (circuit * Circuit ) * OnionErrorDecrypter {
152+ return & OnionErrorDecrypter {
159153 circuit : circuit ,
160154 }
161155}
162156
163- // Deobfuscate makes data deobfuscation. The onion failure is obfuscated in
164- // backward manner, starting from the node where error have occurred, so in
165- // order to deobfuscate the error we need get all shared secret and apply
166- // obfuscation in reverse order.
167- func (o * OnionDeobfuscator ) Deobfuscate ( obfuscatedData []byte ) (* btcec.PublicKey , []byte , error ) {
157+ // DecryptError attempts to decrypt the passed encrypted error response. The
158+ // onion failure is encrypted in backward manner, starting from the node where
159+ // error have occurred. As a result, in order to decrypt the error we need get
160+ // all shared secret and apply decryption in the reverse order.
161+ func (o * OnionErrorDecrypter ) DecryptError ( encryptedData []byte ) (* btcec.PublicKey , []byte , error ) {
168162
169163 sharedSecrets := generateSharedSecrets (
170164 o .circuit .PaymentPath ,
@@ -196,12 +190,12 @@ func (o *OnionDeobfuscator) Deobfuscate(obfuscatedData []byte) (*btcec.PublicKey
196190
197191 // With the shared secret, we'll now strip off a layer of
198192 // encryption from the encrypted error payload.
199- obfuscatedData = onionObfuscation (sharedSecret , obfuscatedData )
193+ encryptedData = onionEncrypt (sharedSecret , encryptedData )
200194
201195 // Next, we'll need to separate the data, from the MAC itself
202196 // so we can reconstruct and verify it.
203- expectedMac := obfuscatedData [:sha256 .Size ]
204- data := obfuscatedData [sha256 .Size :]
197+ expectedMac := encryptedData [:sha256 .Size ]
198+ data := encryptedData [sha256 .Size :]
205199
206200 // With the data split, we'll now re-generate the MAC using its
207201 // specified key.
0 commit comments