我在 GlobalPlatform UICC (2.2.1) 卡的测试工具中实现 SCP03。该工具的代码是用 C# 编写的,我正在尝试使用 dotNet AES 类。
我遇到的问题是我的代码无法重现卡发送的卡挑战。我不确定这是由于我提供给 AES 加密器的数据,还是我没有正确使用 AES 加密器。
我基于 GP Spec 2.2 Amendment-D 的代码,这有点模糊,至少在生成卡片挑战的过程中并且没有指定初始向量。
我创建的推导数据如下:-
00 00 00 00 00 00 00 00 00 00 00 - 11 bytes all zero
02 - card challenge identifier
00 - separator
00 40 - length
01 - counter
00 00 02 - key sequence counter (received from the card)
A0 00 00 01 51 00 00 00 - appID of the currently selected application (the ISD)
80 00 00 00 00 - padding to 32 bytes.
我已将初始向量 (IV) 设置为 16 个字节的零。
为了创建卡片挑战,我使用了静态密钥 K-ENC,它具有以下值:-
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
卡片返回的卡片挑战(验证为正确)是:-
83 FA 04 2C 5C 10 F7 78
我为重现卡片挑战而编写的代码是:-
public static Byte []
GenerateCardChallengeScp03
( UInt32 seqCounter,
Byte [] selectedAid,
Byte [] baseKeyEnc)
{
Byte [] finalDerivationData = null;
Byte [] sequenceCounter = new Byte [3];
Byte [] derivationData = new Byte [16];
for (int i = 0; i < derivationData.Length; i++)
derivationData [i] = 0x00;
sequenceCounter [0] = (Byte) (seqCounter / 0X10000);
sequenceCounter [1] = (Byte) (seqCounter / 0X100);
sequenceCounter [2] = (Byte) (seqCounter);
derivationData [11] = 0x02; // Card challenge
derivationData [12] = 0x00; // Separator
derivationData [13] = 0x00; // MSB length
derivationData [14] = 0x40; // LSB length
derivationData [15] = 0x01; // Counter
finalDerivationData = GP_Utils.ConcatenateArrays (derivationData, sequenceCounter);
finalDerivationData = GP_Utils.ConcatenateArrays (finalDerivationData, selectedAid);
Byte [] icv = new Byte [] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Byte [] inputData = PadForAesEncryption_0x80 (finalDerivationData);
AesCryptoServiceProvider aes = new AesCryptoServiceProvider ();
aes.BlockSize = 128;
aes.FeedbackSize = 128;
aes.KeySize = baseKeyEnc.Length * 8;
aes.Mode = CipherMode.CBC;
aes.Key = baseKeyEnc;
aes.IV = icv;
aes.Padding = PaddingMode.None;
ICryptoTransform encryptor = aes.CreateEncryptor ();
Byte [] outputData = encryptor.TransformFinalBlock (inputData, 0, inputData.Length);
Byte [] cardChallenge = new Byte [8];
for (int i = 0; i < cardChallenge.Length; i++)
cardChallenge [i] = outputData [i];
return (cardChallenge);
}
那么,我没有做什么,或者做错了什么?