GlobalPlatform UICC SCP03 AES卡挑战赛

时间:2013-08-13 15:09:55

标签: c# .net encryption aes smartcard

我正在为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);
}

那么,我不做什么,或做错了什么?

2 个答案:

答案 0 :(得分:0)

不确定您是否找到了答案。如果我没有弄错的话,你应该根据NIST 800-38B计算CMAC计算计算卡挑战,而不仅仅是普通的AES加密。

答案 1 :(得分:0)

我可以验证您的卡片质询。请看http://sourceforge.net/p/globalplatform/code/HEAD/tree/trunk/globalplatform/src/crypto.c。有一个名为calculate_card_challenge_SCP03的函数正在使用calculate_CMAC_aes。我是C代码,但您应该能够将其转换为C#代码。我正在使用OpenSSL的加密函数,所以C#应该有类似的东西。

 static void test_card_challenge_SCP03()
 {
    BYTE sequenceCounter[3] = {0x00, 0x00, 0x02};
    BYTE invokingAID[8] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00, 0x00};
    BYTE calculatedCardChallenge[8];
    BYTE cardChallenge[8] = {0x83,0xFA,0x04,0x2C,0x5C,0x10,0xF7,0x78};
    OPGP_ERROR_STATUS status;

    status = calculate_card_challenge_SCP03((PBYTE)OPGP_VISA_DEFAULT_KEY, (PBYTE)sequenceCounter, invokingAID, sizeof(invokingAID), calculatedCardChallenge);
    sput_fail_unless(OPGP_ERROR_CHECK(status) == 0, NULL);
    OPGP_LOG_HEX(_T("Calculated card challenge: "), calculatedCardChallenge, 8);
    OPGP_LOG_HEX(_T("Given card challenge: "), cardChallenge, 8);
    sput_fail_unless(memcmp(calculatedCardChallenge, cardChallenge, 8) == 0, "Card Challenge Comparison");
 }

您使用什么卡进行测试?我正在寻找一个公共可用的测试卡,其中包含SCP03支持以及用于实施SCP03的已知密钥。我正在使用金雅拓IDPrime PIV卡2.0卡从金雅拓商店,但这似乎是使用一些秘密的无证密钥,无证密钥派生或与标准不兼容。