Skip to content

Commit ad038b6

Browse files
fjlgballet
andauthored
accounts/scwallet: use go-ethereum crypto instead of go-ecdh (#22212)
* accounts/scwallet: use go-ethereum crypto instead of go-ecdh github.com/wsddn/go-ecdh is a wrapper package for ECDH functionality with any elliptic curve. Since 'generic' ECDH is not required in accounts/scwallet (the curve is always secp256k1), we can just use the standard library functionality and our own crypto libraries to perform ECDH and save a dependency. * Update accounts/scwallet/securechannel.go Co-authored-by: Guillaume Ballet <[email protected]> * Use the correct key Co-authored-by: Guillaume Ballet <[email protected]>
1 parent 6816182 commit ad038b6

File tree

3 files changed

+7
-17
lines changed

3 files changed

+7
-17
lines changed

accounts/scwallet/securechannel.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import (
2020
"bytes"
2121
"crypto/aes"
2222
"crypto/cipher"
23+
"crypto/elliptic"
2324
"crypto/rand"
2425
"crypto/sha256"
2526
"crypto/sha512"
2627
"fmt"
2728

2829
"github.com/ethereum/go-ethereum/crypto"
2930
pcsc "github.com/gballet/go-libpcsclite"
30-
"github.com/wsddn/go-ecdh"
3131
"golang.org/x/crypto/pbkdf2"
3232
"golang.org/x/text/unicode/norm"
3333
)
@@ -63,26 +63,19 @@ type SecureChannelSession struct {
6363
// NewSecureChannelSession creates a new secure channel for the given card and public key.
6464
func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSession, error) {
6565
// Generate an ECDSA keypair for ourselves
66-
gen := ecdh.NewEllipticECDH(crypto.S256())
67-
private, public, err := gen.GenerateKey(rand.Reader)
66+
key, err := crypto.GenerateKey()
6867
if err != nil {
6968
return nil, err
7069
}
71-
72-
cardPublic, ok := gen.Unmarshal(keyData)
73-
if !ok {
74-
return nil, fmt.Errorf("could not unmarshal public key from card")
75-
}
76-
77-
secret, err := gen.GenerateSharedSecret(private, cardPublic)
70+
cardPublic, err := crypto.UnmarshalPubkey(keyData)
7871
if err != nil {
79-
return nil, err
72+
return nil, fmt.Errorf("could not unmarshal public key from card: %v", err)
8073
}
81-
74+
secret, _ := key.Curve.ScalarMult(cardPublic.X, cardPublic.Y, key.D.Bytes())
8275
return &SecureChannelSession{
8376
card: card,
84-
secret: secret,
85-
publicKey: gen.Marshal(public),
77+
secret: secret.Bytes(),
78+
publicKey: elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y),
8679
}, nil
8780
}
8881

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ require (
5454
github.com/stretchr/testify v1.7.0
5555
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
5656
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
57-
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208
5857
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
5958
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
6059
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,6 @@ github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZ
365365
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
366366
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
367367
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
368-
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk=
369-
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees=
370368
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
371369
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
372370
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=

0 commit comments

Comments
 (0)