|
|
@ -24,6 +24,7 @@ import ( |
|
|
|
"crypto/rand" |
|
|
|
"crypto/rand" |
|
|
|
"crypto/sha256" |
|
|
|
"crypto/sha256" |
|
|
|
"crypto/sha512" |
|
|
|
"crypto/sha512" |
|
|
|
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
@ -125,7 +126,7 @@ func (s *SecureChannelSession) Pair(pairingPassword []byte) error { |
|
|
|
// Unpair disestablishes an existing pairing.
|
|
|
|
// Unpair disestablishes an existing pairing.
|
|
|
|
func (s *SecureChannelSession) Unpair() error { |
|
|
|
func (s *SecureChannelSession) Unpair() error { |
|
|
|
if s.PairingKey == nil { |
|
|
|
if s.PairingKey == nil { |
|
|
|
return fmt.Errorf("cannot unpair: not paired") |
|
|
|
return errors.New("cannot unpair: not paired") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err := s.transmitEncrypted(claSCWallet, insUnpair, s.PairingIndex, 0, []byte{}) |
|
|
|
_, err := s.transmitEncrypted(claSCWallet, insUnpair, s.PairingIndex, 0, []byte{}) |
|
|
@ -141,7 +142,7 @@ func (s *SecureChannelSession) Unpair() error { |
|
|
|
// Open initializes the secure channel.
|
|
|
|
// Open initializes the secure channel.
|
|
|
|
func (s *SecureChannelSession) Open() error { |
|
|
|
func (s *SecureChannelSession) Open() error { |
|
|
|
if s.iv != nil { |
|
|
|
if s.iv != nil { |
|
|
|
return fmt.Errorf("session already opened") |
|
|
|
return errors.New("session already opened") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
response, err := s.open() |
|
|
|
response, err := s.open() |
|
|
@ -215,7 +216,7 @@ func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*responseAPDU, error |
|
|
|
// transmitEncrypted sends an encrypted message, and decrypts and returns the response.
|
|
|
|
// transmitEncrypted sends an encrypted message, and decrypts and returns the response.
|
|
|
|
func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) { |
|
|
|
func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) { |
|
|
|
if s.iv == nil { |
|
|
|
if s.iv == nil { |
|
|
|
return nil, fmt.Errorf("channel not open") |
|
|
|
return nil, errors.New("channel not open") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
data, err := s.encryptAPDU(data) |
|
|
|
data, err := s.encryptAPDU(data) |
|
|
@ -254,7 +255,7 @@ func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []b |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
if !bytes.Equal(s.iv, rmac) { |
|
|
|
if !bytes.Equal(s.iv, rmac) { |
|
|
|
return nil, fmt.Errorf("invalid MAC in response") |
|
|
|
return nil, errors.New("invalid MAC in response") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rapdu := &responseAPDU{} |
|
|
|
rapdu := &responseAPDU{} |
|
|
@ -319,7 +320,7 @@ func unpad(data []byte, terminator byte) ([]byte, error) { |
|
|
|
return nil, fmt.Errorf("expected end of padding, got %d", data[len(data)-i]) |
|
|
|
return nil, fmt.Errorf("expected end of padding, got %d", data[len(data)-i]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, fmt.Errorf("expected end of padding, got 0") |
|
|
|
return nil, errors.New("expected end of padding, got 0") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// updateIV is an internal method that updates the initialization vector after
|
|
|
|
// updateIV is an internal method that updates the initialization vector after
|
|
|
|