|
@@ -20,6 +20,7 @@ import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
"crypto/aes"
|
|
"crypto/aes"
|
|
|
"crypto/cipher"
|
|
"crypto/cipher"
|
|
|
|
|
+ "crypto/elliptic"
|
|
|
"crypto/rand"
|
|
"crypto/rand"
|
|
|
"crypto/sha256"
|
|
"crypto/sha256"
|
|
|
"crypto/sha512"
|
|
"crypto/sha512"
|
|
@@ -27,7 +28,6 @@ import (
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
pcsc "github.com/gballet/go-libpcsclite"
|
|
pcsc "github.com/gballet/go-libpcsclite"
|
|
|
- "github.com/wsddn/go-ecdh"
|
|
|
|
|
"golang.org/x/crypto/pbkdf2"
|
|
"golang.org/x/crypto/pbkdf2"
|
|
|
"golang.org/x/text/unicode/norm"
|
|
"golang.org/x/text/unicode/norm"
|
|
|
)
|
|
)
|
|
@@ -63,26 +63,19 @@ type SecureChannelSession struct {
|
|
|
// NewSecureChannelSession creates a new secure channel for the given card and public key.
|
|
// NewSecureChannelSession creates a new secure channel for the given card and public key.
|
|
|
func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSession, error) {
|
|
func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSession, error) {
|
|
|
// Generate an ECDSA keypair for ourselves
|
|
// Generate an ECDSA keypair for ourselves
|
|
|
- gen := ecdh.NewEllipticECDH(crypto.S256())
|
|
|
|
|
- private, public, err := gen.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
+ key, err := crypto.GenerateKey()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- cardPublic, ok := gen.Unmarshal(keyData)
|
|
|
|
|
- if !ok {
|
|
|
|
|
- return nil, fmt.Errorf("could not unmarshal public key from card")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- secret, err := gen.GenerateSharedSecret(private, cardPublic)
|
|
|
|
|
|
|
+ cardPublic, err := crypto.UnmarshalPubkey(keyData)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ return nil, fmt.Errorf("could not unmarshal public key from card: %v", err)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ secret, _ := key.Curve.ScalarMult(cardPublic.X, cardPublic.Y, key.D.Bytes())
|
|
|
return &SecureChannelSession{
|
|
return &SecureChannelSession{
|
|
|
card: card,
|
|
card: card,
|
|
|
- secret: secret,
|
|
|
|
|
- publicKey: gen.Marshal(public),
|
|
|
|
|
|
|
+ secret: secret.Bytes(),
|
|
|
|
|
+ publicKey: elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y),
|
|
|
}, nil
|
|
}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|