Skip to content

Commit

Permalink
update definition of minOnionErrorLength
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Jan 16, 2023
1 parent ca23184 commit 371dfed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
14 changes: 7 additions & 7 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (p *PrivKeyECDH) PubKey() *btcec.PublicKey {
// k is our private key, and P is the public key, we perform the following
// operation:
//
// sx := k*P
// s := sha256(sx.SerializeCompressed())
// sx := k*P
// s := sha256(sx.SerializeCompressed())
//
// NOTE: This is part of the SingleKeyECDH interface.
func (p *PrivKeyECDH) ECDH(pub *btcec.PublicKey) ([32]byte, error) {
Expand Down Expand Up @@ -237,8 +237,8 @@ func onionEncrypt(sharedSecret *Hash256, data []byte) []byte {

// minOnionErrorLength is the minimally expected length of the onion error
// message. Including padding, all messages on the wire should be at least 256
// bytes. We then add the size of the sha256 HMAC as well.
const minOnionErrorLength = 2 + 2 + 256 + sha256.Size
// bytes.
const minOnionErrorLength = 2 + 2 + 256

// DecryptError attempts to decrypt the passed encrypted error response. The
// onion failure is encrypted in backward manner, starting from the node where
Expand All @@ -249,10 +249,10 @@ const minOnionErrorLength = 2 + 2 + 256 + sha256.Size
func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte) (
*DecryptedError, error) {

// Ensure the error message length is as expected.
if len(encryptedData) < minOnionErrorLength {
// Ensure the error message length including hmac is as expected.
if len(encryptedData) < minOnionErrorLength+sha256.Size {
return nil, fmt.Errorf("invalid error length: "+
"expected at least %v got %v", minOnionErrorLength,
"expected at least %v got %v", minOnionErrorLength+sha256.Size,
len(encryptedData))
}

Expand Down
3 changes: 1 addition & 2 deletions obfuscation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sphinx

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"reflect"
"testing"
Expand All @@ -28,7 +27,7 @@ func TestOnionFailure(t *testing.T) {
// able to receive the error not only from last hop.
errorPath := paymentPath[:len(paymentPath)-1]

failureData := bytes.Repeat([]byte{'A'}, minOnionErrorLength-sha256.Size)
failureData := bytes.Repeat([]byte{'A'}, minOnionErrorLength)
sharedSecrets, err := generateSharedSecrets(paymentPath, sessionKey)
if err != nil {
t.Fatalf("Unexpected error while generating secrets: %v", err)
Expand Down

0 comments on commit 371dfed

Please sign in to comment.