Skip to content

Commit afbad42

Browse files
gzliudangballet
andcommitted
accounts/scwallet: use go-ethereum crypto instead of go-ecdh (ethereum#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 c35905b commit afbad42

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/XinFinOrg/XDPoSChain/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
@@ -62,7 +62,6 @@ require (
6262
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
6363
github.com/status-im/keycard-go v0.3.3
6464
github.com/urfave/cli/v2 v2.27.5
65-
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208
6665
golang.org/x/text v0.20.0
6766
google.golang.org/protobuf v1.31.0
6867
gopkg.in/natefinch/lumberjack.v2 v2.2.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5
222222
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
223223
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
224224
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
225-
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk=
226-
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees=
227225
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
228226
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
229227
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)