Skip to content

Commit a601ae1

Browse files
committed
crypto+sphinx: add error return value
This is a preparatory commit that adds an error return value to the generateSharedSecret and generateSharedSecrets method. This is needed because the interface we want to abstract the onion key behind has an error return value too.
1 parent d18f9de commit a601ae1

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

crypto.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ func (r *Router) generateSharedSecret(dhKey *btcec.PublicKey) (Hash256, error) {
198198
}
199199

200200
// Compute our shared secret.
201-
sharedSecret = generateSharedSecret(dhKey, r.onionKey)
202-
return sharedSecret, nil
201+
return generateSharedSecret(dhKey, r.onionKey)
203202
}
204203

205204
// generateSharedSecret generates the shared secret for a particular hop. The
@@ -208,11 +207,13 @@ func (r *Router) generateSharedSecret(dhKey *btcec.PublicKey) (Hash256, error) {
208207
// key. We then take the _entire_ point generated by the ECDH operation,
209208
// serialize that using a compressed format, then feed the raw bytes through a
210209
// single SHA256 invocation. The resulting value is the shared secret.
211-
func generateSharedSecret(pub *btcec.PublicKey, priv *btcec.PrivateKey) Hash256 {
210+
func generateSharedSecret(pub *btcec.PublicKey, priv *btcec.PrivateKey) (Hash256,
211+
error) {
212+
212213
s := &btcec.PublicKey{}
213214
s.X, s.Y = btcec.S256().ScalarMult(pub.X, pub.Y, priv.D.Bytes())
214215

215-
return sha256.Sum256(s.SerializeCompressed())
216+
return sha256.Sum256(s.SerializeCompressed()), nil
216217
}
217218

218219
// onionEncrypt obfuscates the data with compliance with BOLT#4. As we use a
@@ -249,10 +250,14 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte) (
249250
len(encryptedData))
250251
}
251252

252-
sharedSecrets := generateSharedSecrets(
253+
sharedSecrets, err := generateSharedSecrets(
253254
o.circuit.PaymentPath,
254255
o.circuit.SessionKey,
255256
)
257+
if err != nil {
258+
return nil, fmt.Errorf("error generating shared secret: %v",
259+
err)
260+
}
256261

257262
var (
258263
sender int

obfuscation_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func TestOnionFailure(t *testing.T) {
3030
errorPath := paymentPath[:len(paymentPath)-1]
3131

3232
failureData := bytes.Repeat([]byte{'A'}, onionErrorLength-sha256.Size)
33-
sharedSecrets := generateSharedSecrets(paymentPath, sessionKey)
33+
sharedSecrets, err := generateSharedSecrets(paymentPath, sessionKey)
34+
if err != nil {
35+
t.Fatalf("Unexpected error while generating secrets: %v", err)
36+
}
3437

3538
// Emulate creation of the obfuscator on node where error have occurred.
3639
obfuscator := &OnionErrorEncrypter{
@@ -194,7 +197,10 @@ func TestOnionFailureSpecVector(t *testing.T) {
194197
}
195198

196199
var obfuscatedData []byte
197-
sharedSecrets := generateSharedSecrets(paymentPath, sessionKey)
200+
sharedSecrets, err := generateSharedSecrets(paymentPath, sessionKey)
201+
if err != nil {
202+
t.Fatalf("Unexpected error while generating secrets: %v", err)
203+
}
198204
for i, test := range onionErrorData {
199205

200206
// Decode the shared secret and check that it matchs with

sphinx.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type OnionPacket struct {
117117
// generateSharedSecrets by the given nodes pubkeys, generates the shared
118118
// secrets.
119119
func generateSharedSecrets(paymentPath []*btcec.PublicKey,
120-
sessionKey *btcec.PrivateKey) []Hash256 {
120+
sessionKey *btcec.PrivateKey) ([]Hash256, error) {
121121

122122
// Each hop performs ECDH with our ephemeral key pair to arrive at a
123123
// shared secret. Additionally, each hop randomizes the group element
@@ -131,8 +131,14 @@ func generateSharedSecrets(paymentPath []*btcec.PublicKey,
131131
// Within the loop each new triplet will be computed recursively based
132132
// off of the blinding factor of the last hop.
133133
lastEphemeralPubKey := sessionKey.PubKey()
134-
hopSharedSecrets[0] = generateSharedSecret(paymentPath[0], sessionKey)
135-
lastBlindingFactor := computeBlindingFactor(lastEphemeralPubKey, hopSharedSecrets[0][:])
134+
sharedSecret, err := generateSharedSecret(paymentPath[0], sessionKey)
135+
if err != nil {
136+
return nil, err
137+
}
138+
hopSharedSecrets[0] = sharedSecret
139+
lastBlindingFactor := computeBlindingFactor(
140+
lastEphemeralPubKey, hopSharedSecrets[0][:],
141+
)
136142

137143
// The cached blinding factor will contain the running product of the
138144
// session private key x and blinding factors b_i, computed as
@@ -184,7 +190,7 @@ func generateSharedSecrets(paymentPath []*btcec.PublicKey,
184190
)
185191
}
186192

187-
return hopSharedSecrets
193+
return hopSharedSecrets, nil
188194
}
189195

190196
// NewOnionPacket creates a new onion packet which is capable of obliviously
@@ -211,9 +217,12 @@ func NewOnionPacket(paymentPath *PaymentPath, sessionKey *btcec.PrivateKey,
211217
return nil, fmt.Errorf("packet filler must be specified")
212218
}
213219

214-
hopSharedSecrets := generateSharedSecrets(
220+
hopSharedSecrets, err := generateSharedSecrets(
215221
paymentPath.NodeKeys(), sessionKey,
216222
)
223+
if err != nil {
224+
return nil, fmt.Errorf("error generating shared secret: %v", err)
225+
}
217226

218227
// Generate the padding, called "filler strings" in the paper.
219228
filler := generateHeaderPadding("rho", paymentPath, hopSharedSecrets)

0 commit comments

Comments
 (0)