keycard-go/globalplatform/crypto/crypto.go

131 lines
2.6 KiB
Go
Raw Normal View History

2018-09-27 09:26:21 +00:00
package crypto
import (
2018-09-27 10:08:29 +00:00
"bytes"
2018-09-27 09:26:21 +00:00
"crypto/cipher"
"crypto/des"
)
var (
DerivationPurposeEnc = []byte{0x01, 0x82}
2018-09-27 13:29:07 +00:00
DerivationPurposeMac = []byte{0x01, 0x01}
2018-09-27 16:01:09 +00:00
NullBytes8 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
2018-09-27 09:26:21 +00:00
)
func DeriveKey(cardKey []byte, seq []byte, purpose []byte) ([]byte, error) {
key24 := resizeKey24(cardKey)
derivation := make([]byte, 16)
copy(derivation, purpose[:2])
copy(derivation[2:], seq[:2])
block, err := des.NewTripleDESCipher(key24)
if err != nil {
return nil, err
}
ciphertext := make([]byte, 16)
2018-09-27 16:01:09 +00:00
mode := cipher.NewCBCEncrypter(block, NullBytes8)
2018-09-27 09:26:21 +00:00
mode.CryptBlocks(ciphertext, derivation)
return ciphertext, nil
}
2018-09-27 10:08:29 +00:00
func VerifyCryptogram(encKey, hostChallenge, cardChallenge, cardCryptogram []byte) (bool, error) {
data := make([]byte, 0)
data = append(data, hostChallenge...)
data = append(data, cardChallenge...)
paddedData := appendDESPadding(data)
2018-09-27 16:01:09 +00:00
calculated, err := mac3des(encKey, paddedData, NullBytes8)
2018-09-27 10:08:29 +00:00
if err != nil {
return false, err
}
return bytes.Equal(calculated, cardCryptogram), nil
}
2018-09-27 11:19:19 +00:00
func MacFull3DES(key, data, iv []byte) ([]byte, error) {
data = appendDESPadding(data)
desBlock, err := des.NewCipher(resizeKey8(key))
if err != nil {
return nil, err
}
des3Block, err := des.NewTripleDESCipher(resizeKey24(key))
if err != nil {
return nil, err
}
des3IV := iv
if len(data) > 8 {
length := len(data) - 8
tmp := make([]byte, length)
mode := cipher.NewCBCEncrypter(desBlock, iv)
mode.CryptBlocks(tmp, data[:length])
des3IV = tmp
}
ciphertext := make([]byte, 8)
mode := cipher.NewCBCEncrypter(des3Block, des3IV)
mode.CryptBlocks(ciphertext, data[len(data)-8:])
return ciphertext, nil
}
2018-09-27 16:01:09 +00:00
func EncryptICV(macKey, mac []byte) ([]byte, error) {
block, err := des.NewCipher(resizeKey8(macKey))
if err != nil {
return nil, err
}
ciphertext := make([]byte, 16)
mode := cipher.NewCBCEncrypter(block, NullBytes8)
mode.CryptBlocks(ciphertext, mac)
return ciphertext, nil
}
2018-09-27 10:08:29 +00:00
func mac3des(key, data, iv []byte) ([]byte, error) {
key24 := resizeKey24(key)
block, err := des.NewTripleDESCipher(key24)
if err != nil {
return nil, err
}
ciphertext := make([]byte, 24)
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, data)
return ciphertext[16:], nil
}
2018-09-27 09:26:21 +00:00
func resizeKey24(key []byte) []byte {
data := make([]byte, 24)
copy(data, key[0:16])
copy(data[16:], key[0:8])
return data
}
2018-09-27 09:39:35 +00:00
2018-09-27 11:19:19 +00:00
func resizeKey8(key []byte) []byte {
return key[:8]
}
2018-09-27 09:39:35 +00:00
func appendDESPadding(data []byte) []byte {
length := len(data) + 1
for ; length%8 != 0; length++ {
}
newData := make([]byte, length)
copy(newData, data)
copy(newData[len(data):], []byte{0x80})
return newData
}