open secure channel

This commit is contained in:
Andrea Franz 2018-10-27 18:52:39 +02:00
parent 435665f5cf
commit 1cdee35414
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
2 changed files with 31 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package actions
import (
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
@ -105,16 +106,29 @@ func Pair(c globalplatform.Channel, pairingPass string, pin string) (*lightwalle
}, nil
}
func OpenSecureChannel(c globalplatform.Channel, appInfo *lightwallet.ApplicationInfo, pairingIndex uint8, pairingKey []byte) error {
func OpenSecureChannel(c globalplatform.Channel, appInfo *lightwallet.ApplicationInfo, pairingIndex uint8, pairingKey []byte) (*lightwallet.SecureChannel, error) {
sc, err := lightwallet.NewSecureChannel(c, appInfo.PublicKey)
cmd := lightwallet.NewCommandOpenSecureChannel(pairingIndex, sc.RawPublicKey())
resp, err := c.Send(cmd)
if err = checkOKResponse(err, resp); err != nil {
return err
return nil, err
}
return nil
salt := resp.Data[:32]
iv := resp.Data[32:]
h := sha512.New()
h.Write(sc.Secret())
h.Write(pairingKey)
h.Write(salt)
data := h.Sum(nil)
encKey := data[:32]
macKey := data[32:]
sc.Init(iv, encKey, macKey)
return sc, nil
}
func parseApplicationInfo(resp *apdu.Response) (*lightwallet.ApplicationInfo, error) {

View File

@ -13,6 +13,9 @@ type SecureChannel struct {
c globalplatform.Channel
secret []byte
publicKey *ecdsa.PublicKey
encKey []byte
macKey []byte
iv []byte
}
func NewSecureChannel(c globalplatform.Channel, cardKeyData []byte) (*SecureChannel, error) {
@ -35,6 +38,16 @@ func NewSecureChannel(c globalplatform.Channel, cardKeyData []byte) (*SecureChan
}, nil
}
func (sc *SecureChannel) Init(iv, encKey, macKey []byte) {
sc.iv = iv
sc.encKey = encKey
sc.macKey = macKey
}
func (sc *SecureChannel) Secret() []byte {
return sc.secret
}
func (sc *SecureChannel) PublicKey() *ecdsa.PublicKey {
return sc.publicKey
}