Skip to content

Commit e769c54

Browse files
committed
multi: hide long-term private key behind interface
To be able to eventually extract the private keys out of the node itself into a hardware wallet or HSM, we need to abstract the ECDH operation against the long-term key behind an interface.
1 parent 9358fde commit e769c54

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

cmd/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,11 @@ func main() {
133133
}
134134

135135
privkey, _ := btcec.PrivKeyFromBytes(btcec.S256(), binKey)
136+
privKeyECDH := &sphinx.PrivKeyECDH{PrivKey: privkey}
136137
replayLog := sphinx.NewMemoryReplayLog()
137-
s := sphinx.NewRouter(privkey, &chaincfg.TestNet3Params, replayLog)
138+
s := sphinx.NewRouter(
139+
privKeyECDH, &chaincfg.TestNet3Params, replayLog,
140+
)
138141

139142
replayLog.Start()
140143
defer replayLog.Stop()

crypto.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,10 @@ func (r *Router) generateSharedSecret(dhKey *btcec.PublicKey) (Hash256, error) {
204204
// key. We then take the _entire_ point generated by the ECDH operation,
205205
// serialize that using a compressed format, then feed the raw bytes through a
206206
// single SHA256 invocation. The resulting value is the shared secret.
207-
func generateSharedSecret(pub *btcec.PublicKey, priv *btcec.PrivateKey) (Hash256,
207+
func generateSharedSecret(pub *btcec.PublicKey, priv SingleKeyECDH) (Hash256,
208208
error) {
209209

210-
s := &btcec.PublicKey{}
211-
s.X, s.Y = btcec.S256().ScalarMult(pub.X, pub.Y, priv.D.Bytes())
212-
213-
return sha256.Sum256(s.SerializeCompressed()), nil
210+
return priv.ECDH(pub)
214211
}
215212

216213
// onionEncrypt obfuscates the data with compliance with BOLT#4. As we use a

sphinx.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package sphinx
22

33
import (
44
"bytes"
5-
"crypto/ecdsa"
65
"crypto/hmac"
76
"crypto/sha256"
87
"fmt"
@@ -134,7 +133,7 @@ func generateSharedSecrets(paymentPath []*btcec.PublicKey,
134133
// off of the blinding factor of the last hop.
135134
lastEphemeralPubKey := sessionKey.PubKey()
136135
hopSharedSecrets[0], err = generateSharedSecret(
137-
paymentPath[0], sessionKey,
136+
paymentPath[0], &PrivKeyECDH{PrivKey: sessionKey},
138137
)
139138
if err != nil {
140139
return nil, err
@@ -489,14 +488,14 @@ type Router struct {
489488
nodeID [AddressSize]byte
490489
nodeAddr *btcutil.AddressPubKeyHash
491490

492-
onionKey *btcec.PrivateKey
491+
onionKey SingleKeyECDH
493492

494493
log ReplayLog
495494
}
496495

497496
// NewRouter creates a new instance of a Sphinx onion Router given the node's
498497
// currently advertised onion private key, and the target Bitcoin network.
499-
func NewRouter(nodeKey *btcec.PrivateKey, net *chaincfg.Params, log ReplayLog) *Router {
498+
func NewRouter(nodeKey SingleKeyECDH, net *chaincfg.Params, log ReplayLog) *Router {
500499
var nodeID [AddressSize]byte
501500
copy(nodeID[:], btcutil.Hash160(nodeKey.PubKey().SerializeCompressed()))
502501

@@ -506,15 +505,8 @@ func NewRouter(nodeKey *btcec.PrivateKey, net *chaincfg.Params, log ReplayLog) *
506505
return &Router{
507506
nodeID: nodeID,
508507
nodeAddr: nodeAddr,
509-
onionKey: &btcec.PrivateKey{
510-
PublicKey: ecdsa.PublicKey{
511-
Curve: btcec.S256(),
512-
X: nodeKey.X,
513-
Y: nodeKey.Y,
514-
},
515-
D: nodeKey.D,
516-
},
517-
log: log,
508+
onionKey: nodeKey,
509+
log: log,
518510
}
519511
}
520512

sphinx_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func newTestRoute(numHops int) ([]*Router, *PaymentPath, *[]HopData, *OnionPacke
102102
}
103103

104104
nodes[i] = NewRouter(
105-
privKey, &chaincfg.MainNetParams, NewMemoryReplayLog(),
105+
&PrivKeyECDH{PrivKey: privKey}, &chaincfg.MainNetParams,
106+
NewMemoryReplayLog(),
106107
)
107108
}
108109

@@ -543,7 +544,8 @@ func newEOBRoute(numHops uint32,
543544
}
544545

545546
nodes[i] = NewRouter(
546-
privKey, &chaincfg.MainNetParams, NewMemoryReplayLog(),
547+
&PrivKeyECDH{PrivKey: privKey}, &chaincfg.MainNetParams,
548+
NewMemoryReplayLog(),
547549
)
548550
}
549551

0 commit comments

Comments
 (0)