Skip to content

Commit 5b732fe

Browse files
guggeromatheusd
authored andcommitted
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 e168e7b commit 5b732fe

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

crypto.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ func (r *Router) generateSharedSecret(dhKey *secp256k1.PublicKey) (Hash256, erro
228228
}
229229

230230
// Compute our shared secret.
231-
sharedSecret = generateSharedSecret(dhKey, r.onionKey)
232-
return sharedSecret, nil
231+
return generateSharedSecret(dhKey, r.onionKey)
233232
}
234233

235234
// generateSharedSecret generates the shared secret for a particular hop. The
@@ -238,7 +237,8 @@ func (r *Router) generateSharedSecret(dhKey *secp256k1.PublicKey) (Hash256, erro
238237
// key. We then take the _entire_ point generated by the ECDH operation,
239238
// serialize that using a compressed format, then feed the raw bytes through a
240239
// single SHA256 invocation. The resulting value is the shared secret.
241-
func generateSharedSecret(pub *secp256k1.PublicKey, priv *secp256k1.PrivateKey) Hash256 {
240+
func generateSharedSecret(pub *secp256k1.PublicKey, priv *secp256k1.PrivateKey) (Hash256,
241+
error) {
242242
var modNScalar secp256k1.ModNScalar
243243
modNScalar.SetByteSlice(priv.ToECDSA().D.Bytes())
244244

@@ -250,8 +250,7 @@ func generateSharedSecret(pub *secp256k1.PublicKey, priv *secp256k1.PrivateKey)
250250
result.ToAffine()
251251

252252
s := secp256k1.NewPublicKey(&result.X, &result.Y)
253-
254-
return sha256.Sum256(s.SerializeCompressed())
253+
return sha256.Sum256(s.SerializeCompressed()), nil
255254
}
256255

257256
// onionEncrypt obfuscates the data with compliance with BOLT#4. As we use a
@@ -288,10 +287,14 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte) (
288287
len(encryptedData))
289288
}
290289

291-
sharedSecrets := generateSharedSecrets(
290+
sharedSecrets, err := generateSharedSecrets(
292291
o.circuit.PaymentPath,
293292
o.circuit.SessionKey,
294293
)
294+
if err != nil {
295+
return nil, fmt.Errorf("error generating shared secret: %v",
296+
err)
297+
}
295298

296299
var (
297300
sender int

obfuscation_test.go

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

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

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

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

199205
// 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 []*secp256k1.PublicKey,
120-
sessionKey *secp256k1.PrivateKey) []Hash256 {
120+
sessionKey *secp256k1.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 []*secp256k1.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 []*secp256k1.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 *secp256k1.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)