keycard-go/globalplatform/commands.go

61 lines
1.3 KiB
Go
Raw Normal View History

2018-09-19 13:31:06 +00:00
package globalplatform
2018-10-01 11:25:24 +00:00
import (
"github.com/status-im/status-go/smartcard/apdu"
"github.com/status-im/status-go/smartcard/globalplatform/crypto"
)
2018-09-19 13:31:06 +00:00
2018-09-25 15:43:26 +00:00
const (
2018-10-01 11:25:24 +00:00
Cla = uint8(0x00)
ClaGp = uint8(0x80)
ClaMac = uint8(0x84)
2018-09-19 13:31:06 +00:00
2018-10-01 11:25:24 +00:00
InsSelect = uint8(0xA4)
InsInitializeUpdate = uint8(0x50)
InsExternalAuthenticate = uint8(0x82)
2018-09-25 15:43:26 +00:00
)
2018-09-19 13:31:06 +00:00
func NewCommandSelect(aid []byte) *apdu.Command {
return apdu.NewCommand(
Cla,
InsSelect,
uint8(0x04),
uint8(0x00),
aid,
)
}
2018-09-25 15:43:26 +00:00
func NewCommandInitializeUpdate(challenge []byte) *apdu.Command {
return apdu.NewCommand(
ClaGp,
InsInitializeUpdate,
uint8(0x00),
uint8(0x00),
challenge,
)
}
2018-10-01 11:25:24 +00:00
func NewCommandExternalAuthenticate(encKey, cardChallenge, hostChallenge []byte) (*apdu.Command, error) {
hostCryptogram, err := calculateHostCryptogram(encKey, cardChallenge, hostChallenge)
if err != nil {
return nil, err
}
return apdu.NewCommand(
ClaMac,
InsExternalAuthenticate,
uint8(0x01), // C-MAC
uint8(0x00),
hostCryptogram,
), nil
}
func calculateHostCryptogram(encKey, cardChallenge, hostChallenge []byte) ([]byte, error) {
var data []byte
data = append(data, cardChallenge...)
data = append(data, hostChallenge...)
data = crypto.AppendDESPadding(data)
return crypto.Mac3DES(encKey, data, crypto.NullBytes8)
}