一、概述
在智能卡中,卡可卡外实体都可以作为安全消息的发送实体和接收实体。SCP02是卡和卡外实体之间,用于保证实体认证、数据完整性和保密性的安全通道协议。
二、apdu命令
INITIALIZE UPDATE 命令
Code | Value | Meaning |
---|---|---|
CLA | '80' | |
INS | '50' | INITIALIZE UPDATE |
P1 | 'xx' | Key version |
P2 | 'xx' | Key Identifier |
Lc | '08' | Length of host challenge |
Data | 'xx xx...' | Host challenge |
Le | '00' |
EXTERNAL AUTHENTICATE 命令
Code | Value | Meaning |
---|---|---|
CLA | '84' | |
INS | '82' | EXTERNAL AUTHENTICATE |
P1 | 'xx' | Security level |
P2 | '00' | Reference control parameter P2 |
Lc | '10' | Length of host cryptogram and MAC |
Data | 'xx xx...' | Host cryptogram and MAC |
Le |
三、apdu命令实例
命令1:选择应用
命令2:INITIALIZE UPDATE
命令3:EXTERNAL AUTHENTICATE
命令4:Storedata info1
命令5:Storedata info2
//----------------------------------------------------------------------------------------------------
cm> /send 00a404000701020304040500
=> 00 A4 04 00 07 01 02 03 04 04 05 00
<= 90 00
cm> /send 80500000080102030405060708
=> 80 50 00 00 08 01 02 03 04 05 06 07 08
<= 00 00 F9 02 71 E3 BB AD BD CD FF 02 00 01 71 CF A1 BC 96 CF 29 D3 3E 95 48 F8 1B FE 90 00
cm> /send 84820100104CBB168327EB4571AB5AF6CED2CB807F
=> 84 82 01 00 10 4C BB 16 83 27 EB 45 71 AB 5A F6 CE D2 CB 80 7F
<= 90 00
cm> /send 84E200001B020210010101010101010101010101010101016F8FDC36D3538D7D
=> 84 E2 00 00 1B 02 02 10 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 6F 8F DC 36 D3 53 8D 7D
<= 90 00
cm> /send 84E200001B030310020202020202020202020202020202028FC5AF3B817DEA31
=> 84 E2 00 00 1B 03 03 10 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 8F C5 AF 3B 81 7D EA 31
<= 90 00
//----------------------------------------------------------------------------------------------------
四、命令计算过程分解
前提条件:
STATIC ENC KEY: 404142434445464748494A4B4C4D4E4F
STATIC MAC KEY: 404142434445464748494A4B4C4D4E4F
STATIC DEK KEY: 404142434445464748494A4B4C4D4E4F
解析INITIALIZE UPDATE命令返回数据:
Key diversification data | Key information | Sequence Counter | Card challenge | Card cryptogram |
---|---|---|---|---|
0000F90271E3BBADBDCD | FF02 | 0001 | 71CFA1BC96CF | 29D33E9548F81BFE |
1、第一步由SCP02 static keys计算SESSION KEY:
(1)ENC SESSION KEY计算方法:
使用算法:3DES - CBC模式加密(输入数据不再补位,icv为0)
使用key:static ENC key (404142434445464748494A4B4C4D4E4F)
输入数据:2字节S-ENC常量 + 2字节Sequence Counter + 12字节‘00’填充 (01820001000000000000000000000000)
ENC SESSION KEY: 25C9794A1205FF244F5FA0378D2F8D59
(2)C-MAC SESSION KEY计算方法:
使用算法:3DES - CBC模式加密(输入数据不再补位,icv为0)
使用key:static ENC key (404142434445464748494A4B4C4D4E4F)
输入数据:2字节C-MAC常量 + 2字节Sequence Counter + 12字节‘00’填充 (01010001000000000000000000000000)
ENC SESSION KEY: 9BED98891580C3B245FE9EC58BFA8D2A
2、Host Authentication Cryptogram计算方法:
使用算法:3DES CBC运算,icv为0
使用key: ENC SESSION KEY(25C9794A1205FF244F5FA0378D2F8D59)
输入数据:2字节Sequence Counter + 6字节Card Challenge + 8字节host challenge,数据需要‘80’方式填充(0001 71CFA1BC96CF 0102030405060708 8000000000000000)
结果:DBE5F78F49944BA28198A656419307304CBB168327EB4571
Host Authentication Cryptogram为取上述结果的后8字节:4CBB168327EB4571
3、8482 C-MAC 计算
使用算法:RetailMAC3 ,前面都为des加密,最后一组为3des加密, icv为0
使用key: C-MAC SESSION KEY ( 9BED98891580C3B245FE9EC58BFA8D2A )
输入数据: “8482” + LEVEL(“01”)+ “0010” + HostCrypt(第四步的值) + “800000”
ToMac数据: 8482010010 4CBB168327EB4571 800000
结果: AB5AF6CED2CB807F
8482命令发送数据: 84820100104CBB168327EB4571AB5AF6CED2CB807F (MAC数据+MAC)
注意1:EXTERNAL AUTHENTICATE命令的P1表示安全级别,LEVEL=01表示需要CMAC,那么随后所有经过安全通道的命令都要验证CMAC。如果LEVEL=00,那么加下来的命令不需要MAC验证。
4、storedata命令 mac计算
注意2:EXTERNAL AUTHENTICATE之后的命令中,计算CMAC的icv都是用上条apdu命令的CMAC值。先将上次命令的Mac值,用singledes加密,密钥为session mac的前8个字节,icv为0。
(1)第一条storedata
AB5AF6CED2CB807F ===加密完===> 56556286C0325DA9
icv: 56556286C0325DA9
使用key: session mac key(9BED98891580C3B245FE9EC58BFA8D2A)
输入数据:84E200001B02021001010101010101010101010101010101
结果:6F8FDC36D3538D7D
最后发送的命令:84E200001B020210010101010101010101010101010101016F8FDC36D3538D7D
(2)第二条storedata
6F8FDC36D3538D7D ===加密完===> 0B0B61C8761F80B0
icv: 0B0B61C8761F80B0
使用key: session mac key(9BED98891580C3B245FE9EC58BFA8D2A)
输入数据:84E200001B03031002020202020202020202020202020202
结果:8FC5AF3B817DEA31
最后发送的命令:84E200001B030310020202020202020202020202020202028FC5AF3B817DEA31