2019-08-07

static int LoadKey()
{
int nRet = 0, nIndex = 0;
char sTmk[16] = {0};
char sPinKey[16] = {0};
char sMacKey[16] = {0};
char sDataKey[16] = {0};
char sIPEK[16] = {0xBD,0x35,0x71,0xDB,0x88,0x36,0xCD,0x71,0xD4,0xE4,0x37,0x47,0x35,0x4E,0xC2,0x36};
char sKSN[10] = {0x00,0x01,0x03,0x00,0x64,0x56,0x00,0xE0,0x00,0x00};
char sInput[8] = {0}, sOutput[8] = {0}; // Used to veryfiy DUKPT key. It's like to calculate key check value.

memset(sTmk, 0x00, sizeof(sTmk));
memset(sPinKey, 0x11, 16);  //kcv "\xCA\x25\x1B\x79"
memset(sMacKey, 0x22, 16);  //kcv "\x10\x82\x38\x76"
memset(sDataKey, 0x33, 16); //kcv "\xD9\x7E\xB4\x64"

GetVarMainKeyNo(&nIndex);

if(GetVarKeySystemType() == '0')    // MK/SK
{
    nRet = PubLoadMainKey(nIndex, sTmk, NULL, 16);  // sTmk(main key) should be plain text
    if (nRet != APP_SUCC)
    {
        PubDispErr("LOAD MAINKEY FAIL");
        return APP_FAIL;
    }
    
    PubSetCurrentMainKeyIndex(nIndex);
    
    // sPinKey(work key) should be ciphertext. It you want to inject 32byte 0x11, 
    // You should calculate the ciphertext of 32byte 0x11 (encrypt 32byte 0x11 by sTmk (32 bytes 0x00)), then inject ciphertext to terminal.
    nRet = PubLoadWorkKey(KEY_TYPE_PIN, sPinKey, 16, NULL);
    if (nRet != APP_SUCC)
    {
        PubDispErr(tr("Load PIN Key Fail"));
        return APP_FAIL;
    }
    // sMacKey(work key) should be ciphertext. Same steps as pin key.
    nRet = PubLoadWorkKey(KEY_TYPE_MAC, sMacKey, 16, NULL);
    if (nRet != APP_SUCC)
    {
        PubDispErr(tr("Load MAC Key Fail"));
        return APP_FAIL;
    }
    // sDataKey(work key) should be ciphertext. Same steps as pin key.
    nRet = PubLoadWorkKey(KEY_TYPE_DATA, sDataKey, 16, NULL);
    if (nRet != APP_SUCC)
    {
        PubDispErr(tr("Load Data Key Fail"));
        return APP_FAIL;
    }
}
else    // DUKPT
{
    nRet = PubLoadMainKey(nIndex, sIPEK, sKSN, 16);   // sIPEK should be plain text
    if (nRet != APP_SUCC)
    {
        PubDispErr("LOAD MAINKEY FAIL");
        return APP_FAIL;
    }
    PubGetDukptKSN(sKSN);
    TRACE_HEX((sKSN, 10, "sKSN: "));
    PubSetCurrentMainKeyIndex(nIndex);
    PubDes3(sInput, 8, sOutput);
    TRACE_HEX((sOutput, 8, "sOutput: "));
}
return APP_SUCC;

}

你可能感兴趣的:(2019-08-07)