44 "encoding/binary"
55 "fmt"
66 "io"
7- "math"
87
98 "github.com/btcsuite/btcd/btcec/v2"
109 sphinx "github.com/lightningnetwork/lightning-onion"
@@ -21,6 +20,12 @@ const (
2120 // proposal](https://github.com/lightning/blips/pull/39) for a detailed
2221 // calculation.
2322 maxNumHopsPerPath = 7
23+
24+ // maxCipherTextLength defines the largest cipher text size allowed.
25+ // This is derived by using the `data_length` upper bound of 639 bytes
26+ // and then assuming the case of a path with only a single hop (meaning
27+ // the cipher text may be as large as possible).
28+ maxCipherTextLength = 535
2429)
2530
2631var (
@@ -215,6 +220,12 @@ func DecodeBlindedHop(r io.Reader) (*sphinx.BlindedHopInfo, error) {
215220 return nil , err
216221 }
217222
223+ if dataLen > maxCipherTextLength {
224+ return nil , fmt .Errorf ("a blinded hop cipher text blob may " +
225+ "not exceed the maximum of %d bytes" ,
226+ maxCipherTextLength )
227+ }
228+
218229 encryptedData := make ([]byte , dataLen )
219230 _ , err = r .Read (encryptedData )
220231 if err != nil {
@@ -238,9 +249,9 @@ func EncodeBlindedHop(w io.Writer, hop *sphinx.BlindedHopInfo) error {
238249 return err
239250 }
240251
241- if len (hop .CipherText ) > math . MaxUint16 {
252+ if len (hop .CipherText ) > maxCipherTextLength {
242253 return fmt .Errorf ("encrypted recipient data can not exceed a " +
243- "length of %d bytes" , math . MaxUint16 )
254+ "length of %d bytes" , maxCipherTextLength )
244255 }
245256
246257 err = tlv .WriteVarInt (w , uint64 (len (hop .CipherText )), & [8 ]byte {})
0 commit comments