From 4003f252819840483ac81756d103fca26001954b Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Tue, 28 Apr 2020 10:06:32 +0200 Subject: [PATCH] signrpc: use ECDH interface for shared key generation --- lnrpc/signrpc/signer_server.go | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/lnrpc/signrpc/signer_server.go b/lnrpc/signrpc/signer_server.go index 127d113e..1d53be2b 100644 --- a/lnrpc/signrpc/signer_server.go +++ b/lnrpc/signrpc/signer_server.go @@ -5,7 +5,6 @@ package signrpc import ( "bytes" "context" - "crypto/sha256" "fmt" "io/ioutil" "os" @@ -515,31 +514,15 @@ func (s *Server) DeriveSharedKey(_ context.Context, in *SharedKeyRequest) ( locator.Index = uint32(in.KeyLoc.KeyIndex) } - // Derive our node's private key from the key ring. - idPrivKey, err := s.cfg.KeyRing.DerivePrivKey(keychain.KeyDescriptor{ - KeyLocator: locator, - }) + // Derive the shared key using ECDH and hashing the serialized + // compressed shared point. + keyDescriptor := keychain.KeyDescriptor{KeyLocator: locator} + sharedKeyHash, err := s.cfg.KeyRing.ECDH(keyDescriptor, ephemeralPubkey) if err != nil { - err := fmt.Errorf("unable to derive node private key: %v", err) + err := fmt.Errorf("unable to derive shared key: %v", err) log.Error(err) return nil, err } - idPrivKey.Curve = btcec.S256() - // Derive the shared key using ECDH and hashing the serialized - // compressed shared point. - sharedKeyHash := ecdh(ephemeralPubkey, idPrivKey) - return &SharedKeyResponse{SharedKey: sharedKeyHash}, nil -} - -// ecdh performs an ECDH operation between pub and priv. The returned value is -// the sha256 of the compressed shared point. -func ecdh(pub *btcec.PublicKey, priv *btcec.PrivateKey) []byte { - s := &btcec.PublicKey{} - x, y := btcec.S256().ScalarMult(pub.X, pub.Y, priv.D.Bytes()) - s.X = x - s.Y = y - - h := sha256.Sum256(s.SerializeCompressed()) - return h[:] + return &SharedKeyResponse{SharedKey: sharedKeyHash[:]}, nil }