Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stddef.h>
- #include <stdlib.h>
- #include "Global.h"
- #include "Emulator.h"
- #include "Crypto_emu.h"
- #define reply SetTrailer
- extern CryptoState_t State;
- typedef struct {
- u8 len;
- u8 pt1;
- u8 pt2;
- u8 dBuff[256];
- } b1_t;
- static b1_t Input;
- static b1_t Output;
- static u8 CkSum;
- static u8 Flg;
- static u8 CardInfo[8][16];
- static Tier_t WkKey[32];
- static u8 BroadCastID;
- static void chksum1(b1_t *pBuff)
- {
- u8 i, tmp = 0;
- for (i = 0 ; i <= pBuff->len ; i++) {
- tmp ^= pBuff->dBuff[i];
- }
- CkSum = tmp ^ pBuff->len ^ pBuff->pt1 ^ pBuff->pt2;
- }
- #if !defined(FAKE_WINSCARD)
- static void testptrn1(void)
- {
- const u8 dt1[] = { 0x90, 0x30, 0x00, 0x00, 0x00 }; // INT
- const u8 dt2[] = { 0x90, 0x32, 0x00, 0x00, 0x00 }; // IDI
- /* const u8 dt3[] = { 0x90, 0x34, 0x00, 0x00, 0x26, 0x44, 0x1E, 0x02,
- 0x54, 0x68, 0x69, 0x73, 0x20, 0x6?, 0x69, 0x6?, // Odd Key
- 0x6?, 0x20, 0x70, 0x6?, 0x73, 0x74, 0x31, 0x39, // Even Key
- 0x??, 0x3?, 0x?2, 0x??, 0x54, 0x21, 0x95,
- 0x6?, 0x25, 0x2?, 0x33, 0x00, 0x?6, 0x?9,
- 0x1?, 0x42, 0x40, 0x8?, 0x00, 0x00 }; // ECM
- */
- struct {
- const u8 *ptr;
- u8 size;
- } testapdu[] = {
- { dt1, sizeof(dt1) }, // INT
- { dt2, sizeof(dt2) }, // IDI
- /* { dt3, sizeof(dt3) }, // ECM
- */ { NULL, 0 },
- };
- static u8 index = 0;
- if (testapdu[index].ptr != NULL) {
- memcpy(Input.dBuff, testapdu[index].ptr, testapdu[index].size);
- Input.len = testapdu[index].size;
- index++;
- } else {
- exit(0); // test complete
- }
- Input.pt1 = 0x00;
- Input.pt2 = Flg;
- Flg ^= 0x40;
- Input.dBuff[Input.len] = 0x00; // zero clear
- chksum1(&Input); // calculate sum
- Input.dBuff[Input.len] = CkSum; // store sum
- #if !defined(FAKE_WINSCARD)
- printf(" :");
- for (u8 i = 0 ; i < Input.len ; i++)
- printf(" %02x", Input.dBuff[i]);
- printf(" %02x",Input.dBuff[Input.len]); // store sum
- printf("\n");
- #endif
- }
- #endif
- static void reply1(void)
- {
- Output.pt1 = Input.pt1;
- Output.pt2 = Input.pt2;
- Output.dBuff[Output.len] = 0x00;
- chksum1(&Output);
- Output.dBuff[Output.len] = CkSum;
- memset(&Input, 0, sizeof(Input));
- #if !defined(FAKE_WINSCARD)
- printf(" :");
- for (u8 i = 0 ; i < Output.len ; i++)
- printf(" %02x", Output.dBuff[i]);
- printf("\n");
- #endif
- }
- static u8 CheckProtocol(u8 Protocol)
- {
- if (Protocol == 0x00 || Protocol == 0x04 || Protocol == 0x40 || Protocol == 0x44)
- return 1;
- return 0;
- }
- static void SetReturnCode(u16 data) // set return code.
- {
- Output.dBuff[OUTPUT_OFFSET_RCODE + 0] = (data >> 8) & 0xff;
- Output.dBuff[OUTPUT_OFFSET_RCODE + 1] = (data >> 0) & 0xff;
- }
- static void SetOutputLen(u8 size) // set length
- {
- Output.len = size + 2;
- }
- static void SetTrailer(u16 data)
- {
- Output.dBuff[Output.len - 2] = (data >> 8) & 0xff;
- Output.dBuff[Output.len - 1] = (data >> 0) & 0xff;
- }
- static void Save_Card(void);
- static void Load_Card(void)
- {
- FILE *fp;
- fp = fopen("b_cas.card1", "rb");
- if (fp != NULL) {
- fread(&State, sizeof(State), 1, fp);
- fread(CardInfo, sizeof(CardInfo), 1, fp);
- fread(WkKey, sizeof(WkKey), 1, fp);
- fclose(fp);
- } else {
- Save_Card(); // Create file when file not found.
- }
- }
- static void Save_Card(void)
- {
- FILE *fp;
- fp = fopen("b_cas.card1", "wb");
- if (fp != NULL) {
- fwrite(&State, sizeof(State), 1, fp);
- fwrite(CardInfo, sizeof(CardInfo), 1, fp);
- fwrite(WkKey, sizeof(WkKey), 1, fp);
- fclose(fp);
- }
- }
- static void WriteOffsetData(u16 Offset, u16 data)
- {
- FILE *fp;
- fp = fopen("b_cas.card1", "w+b");
- if (fp != NULL) {
- fseek(fp, Offset, SEEK_SET);
- fwrite((const u8 *)&State + Offset, data, 1, fp);
- fclose(fp);
- }
- }
- static void WriteWkKeyData(void)
- {
- FILE *fp;
- fp = fopen("b_cas.card1", "w+b");
- if (fp != NULL) {
- fseek(fp, sizeof(State) + sizeof(CardInfo), SEEK_SET);
- fwrite(WkKey, sizeof(WkKey), 1, fp);
- fclose(fp);
- }
- }
- static void WriteCardInfoData(void)
- {
- FILE *fp;
- fp = fopen("b_cas.card1", "w+b");
- if (fp != NULL) {
- fseek(fp, sizeof(State), SEEK_SET);
- fwrite(CardInfo, sizeof(CardInfo), 1, fp);
- fclose(fp);
- }
- }
- static void ECM_variable_dt(u8 size)
- {
- u8 hensu = INPUT_OFFSET_DATA + sizeof(BCAS_ECM_Request_t);
- u8 *pECM = &Input.dBuff[hensu];
- u8 siz, cmd, i;
- while (size >= 2) {
- size -= 2;
- siz = *pECM++;
- cmd = *pECM++;
- if (siz > size)
- siz = size;
- switch (cmd) {
- case 0x52:
- for ( i= 0 ; i < siz && i < 32; i++) {
- if (WkKey[BroadCastID].Bouquet[i] & pECM[i])
- break;
- }
- if (i == siz || i == 32)
- SetReturnCode(0x8901);
- break;
- }
- pECM += siz;
- size -= siz;
- }
- }
- static void EMM_variable_dt(u8 size)
- {
- u8 block = INPUT_OFFSET_DATA + sizeof(BCAS_EMM_Request_t);
- u8 *pEMM = &Input.dBuff[block];
- u8 siz, cmd, i;
- while (size >= 2) {
- size -= 2;
- siz = *pEMM++;
- cmd = *pEMM++;
- if (siz > size)
- siz = size;
- switch (cmd) {
- case 0x10:
- if (siz == 9) {
- WkKey[BroadCastID].ActivationState = 0x01;
- WkKey[BroadCastID].Key[pEMM[0] & 1].WorkKeyID = pEMM[0];
- memcpy(WkKey[BroadCastID].Key[pEMM[0] & 1].Key, pEMM + 1, 8);
- }
- break;
- case 0x11:
- for (i = 0 ; i < siz && i < 32; i++)
- WkKey[BroadCastID].Bouquet[i] = pEMM[i];
- break;
- case 0x12:
- printf(" 139:");
- for (i = 0 ; i < siz && i < 8; i++)
- printf(" %02x", pEMM[i]);
- printf("\n");
- break;
- case 0x13: // CardInfo update
- if (siz == 14) {
- block = pEMM[0] >> 5;
- if (block) {
- memcpy(CardInfo[block] + 0, pEMM, 6);
- CardInfo[block][6] = CardInfo[block][0] ^ CardInfo[block][2] ^ CardInfo[block][4];
- CardInfo[block][7] = CardInfo[block][1] ^ CardInfo[block][3] ^ CardInfo[block][5];
- memcpy(CardInfo[block] + 8, pEMM + 6, 8);
- WriteCardInfoData();
- }
- }
- break;
- }
- pEMM += siz;
- size -= siz;
- }
- }
- // INT
- static void bcas_int(void)
- {
- BCAS_INT_Response_t *Response;
- u8 i;
- if (Input.dBuff[INPUT_OFFSET_LC]) {
- reply(0x6700); // length error
- return;
- }
- Response = (BCAS_INT_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x39;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- Response->ReturnCode[0] = 0x21;
- Response->ReturnCode[1] = 0x00;
- Response->CASystemID[0] = 0x00;
- Response->CASystemID[1] = 0x05;
- memcpy(Response->CardID, CardInfo[0], 6);
- Response->CardType = 0x01;
- Response->MessagePartitionLength = 0x50;
- memcpy(Response->SystemKey, State.SystemKey, 32);
- memcpy(Response->IV, State.SystemIV, 8);
- Response->SystemManagementIDCount = 0x01;
- Response->SystemManagementID[0] = 0x02;
- Response->SystemManagementID[1] = 0x01; // [1] header bug?
- for (i = 1 ; i < 8 ; i++) {
- if (CardInfo[i][0]) {
- Response->ICCardInstruction[0] |= 0x04;
- break;
- }
- }
- // fit size '12-06-23
- SetOutputLen(sizeof(BCAS_INT_Response_t) + Response->SystemManagementIDCount * sizeof(Response->SystemManagementID[1]));
- reply(0x9000);
- }
- // IDI
- static void bcas_idi(void)
- {
- BCAS_IDI_Response_t *Response;
- u8 i;
- Response = (BCAS_IDI_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x05; // 04 -> 05 '12-06-23
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- Response->ReturnCode[0] = 0x21;
- Response->ReturnCode[1] = 0x00;
- Response->Count = 0x00;
- for (i = 0 ; i < 8 ; i++) {
- u8 Count;
- if (i && CardInfo[i][0] == 0)
- continue;
- Count = Response->Count++;
- Response->Info[Count].Manufacturer = 'T'; //T002 CARD
- Response->Info[Count].Version = 0x02;
- memcpy(Response->Info[Count].ID, CardInfo[i], 8);
- Response->UnitLength += 10;
- }
- SetOutputLen(sizeof(BCAS_IDI_Response_t) + Response->Count * sizeof(Response->Info[0]));
- reply(0x9000);
- }
- // ECM
- static void bcas_ecm(void)
- {
- BCAS_ECM_Request_t *Request;
- BCAS_ECM_Response_t *Response;
- u8 MAC[4], size;
- Key_t *Key;
- Tier_t *Tier;
- u16 MJDxxx7, MJDxxxx8;
- size = Input.dBuff[INPUT_OFFSET_LC];
- if (size < 30) {
- reply(0x6700); // length error
- return;
- }
- SetOutputLen(sizeof(BCAS_ECM_Response_t));
- reply(0x9000);
- Request = (BCAS_ECM_Request_t *)&Input.dBuff[INPUT_OFFSET_DATA];
- Response = (BCAS_ECM_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x15;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- if (CheckProtocol(Request->ProtocolNumber) == 0) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x02;
- goto L419;
- }
- BroadCastID = Request->BroadcasterGroupID;
- if (BroadCastID >= 32) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x03;
- goto L419;
- }
- Tier = &WkKey[BroadCastID];
- if (Tier->ActivationState == 0) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x03;
- goto L419;
- }
- Key = &Tier->Key[Request->WorkKeyID & 1];
- if (Key->WorkKeyID != Request->WorkKeyID) {
- u8 i;
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x03;
- goto L419;
- }
- // LogWrite("EMUDecrIN : ", Request->OddKey, size - 3);
- // LogWrite("Key : ", Key->Key, 8);
- Decrypt(Request->ProtocolNumber, Key->Key, Request->OddKey, size - 3);
- // LogWrite("EMUDecrOUT : ", Request->OddKey, size -3);
- // LogWrite("MAC_IN : ", ((u8 *)Request + size - 4), 4 );
- GenerateMAC(Request->ProtocolNumber, Key->Key, (const u8 *)Request, size - 4, MAC);
- // LogWrite("MAC_OUT : ", MAC, 4 );
- if (memcmp(((u8 *)Request) + size - 4, MAC, 4)) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x06;
- goto L419;
- }
- MJDxxx7 = (Request->Date[0] << 8) | Request->Date[1];
- MJDxxxx8 = (Tier->ExpiryDate[0] << 8) | Tier->ExpiryDate[1];
- if (MJDxxx7 > MJDxxxx8) {
- Response->ReturnCode[0] = 0x89;
- Response->ReturnCode[1] = 0x02;
- goto L419;
- }
- Response->ReturnCode[0] = 0x08; // 0x21 -> 0x08
- Response->ReturnCode[1] = 0x00;
- ECM_variable_dt(size - 30);
- memcpy(Response->OddKey, Request->OddKey, 8);
- memcpy(Response->EvenKey, Request->EvenKey, 8);
- Response->RecordingControl = 0x01;
- return;
- L419:
- memset(Response->OddKey, 0, 8);
- memset(Response->EvenKey, 0, 8);
- Response->RecordingControl = 0x00;
- }
- // EMM
- static void bcas_emm(void)
- {
- BCAS_EMM_Request_t *Request;
- BCAS_EMM_Response_t *Response;
- u8 MAC[4], size, i, *key;
- u16 MJDlocal9, MJDlocal10;
- Request = (BCAS_EMM_Request_t *)&Input.dBuff[INPUT_OFFSET_DATA];
- Response = (BCAS_EMM_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- SetOutputLen(sizeof(BCAS_EMM_Response_t));
- reply(0x9000);
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x04;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- if (CheckProtocol(Request->ProtocolNumber) == 0) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x02;
- return;
- }
- for (i = 0 ; i < 8 ; i++) {
- if (memcmp(Request->ID, CardInfo[i], 6) == 0)
- break;
- }
- if (i == 8) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0xFE;
- return;
- }
- key = CardInfo[i] + 8;
- size = Input.dBuff[INPUT_OFFSET_LC];
- Decrypt(Request->ProtocolNumber, key, &Request->BroadcasterGroupID, Request->Length - 1);
- GenerateMAC(Request->ProtocolNumber, key, (const u8 *)Request, Request->Length + 3, MAC);
- if (memcmp(((u8 *)Request) + size - 4, MAC, 4)) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x07;
- return;
- }
- BroadCastID = Request->BroadcasterGroupID;
- Response->ReturnCode[0] = 0x21;
- Response->ReturnCode[1] = 0x00;
- MJDlocal9 = (Request->ExpiryDate[0] << 8) | Request->ExpiryDate[1];
- MJDlocal10 = (WkKey[BroadCastID].ExpiryDate[0] << 8) | WkKey[BroadCastID].ExpiryDate[1];
- if (MJDlocal9 <= MJDlocal10)
- return;
- WkKey[BroadCastID].ExpiryDate[0] = Request->ExpiryDate[0];
- WkKey[BroadCastID].ExpiryDate[1] = Request->ExpiryDate[1];
- EMM_variable_dt(size - 17);
- WriteWkKeyData();
- }
- // EMG
- static void bcas_emg(void)
- {
- BCAS_EMG_Response_t *Response;
- Response = (BCAS_EMG_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x04;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- Response->ReturnCode[0] = 0x21;
- Response->ReturnCode[1] = 0x00;
- SetOutputLen(sizeof(BCAS_EMG_Response_t));
- reply(0x9000);
- }
- // EMD
- static void bcas_emd(void)
- {
- BCAS_EMD_Response_t *Response;
- Response = (BCAS_EMD_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x0B; // 0x0?
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- Response->ReturnCode[0] = 0xA1; // 0x?1 -> 0xA1
- Response->ReturnCode[1] = 0x01;
- Response->ExpiryDate[0] = 0x00;
- Response->ExpiryDate[1] = 0x00;
- Response->PresetText[0] = 0x00;
- Response->PresetText[1] = 0x00;
- Response->Number = 0x00;
- Response->Length[0] = 0x00;
- Response->Length[1] = 0x00;
- SetOutputLen(sizeof(BCAS_EMD_Response_t));
- reply(0x9000);
- }
- // CHK
- static void bcas_chk(void)
- {
- BCAS_CHK_Request_t *Request;
- BCAS_CHK_Response_t *Response;
- u8 size;
- Key_t *Key;
- Tier_t *Tier;
- u16 MJD1111, MJD22222;
- size = Input.dBuff[INPUT_OFFSET_LC];
- if (size < 2) {
- reply(0x6700); // length error
- return;
- }
- SetOutputLen(sizeof(BCAS_CHK_Response_t));
- reply(0x9000);
- Request = (BCAS_CHK_Request_t *)&Input.dBuff[INPUT_OFFSET_DATA];
- Response = (BCAS_CHK_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = sizeof(BCAS_CHK_Response_t) - 2;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- BroadCastID = Request->BroadcasterGroupID;
- if (BroadCastID >= 32) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x03;
- goto L598;
- }
- Tier = &WkKey[BroadCastID];
- if (Tier->ActivationState == 0) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x03;
- goto L598;
- }
- Key = &Tier->Key[Request->WorkKeyID & 1];
- if (Key->WorkKeyID != Request->WorkKeyID) {
- Response->ReturnCode[0] = 0xA1;
- Response->ReturnCode[1] = 0x03;
- goto L598;
- }
- Decrypt(Request->ProtocolNumber, Key->Key, (u8 *)(Request + 1), size - 5);
- MJD1111 = (Request->Date[0] << 8) | Request->Date[1];
- MJD22222 = (Tier->ExpiryDate[0] << 8) | Tier->ExpiryDate[1];
- if (MJD1111 > MJD22222) {
- Response->ReturnCode[0] = 0x89;
- Response->ReturnCode[1] = 0x02;
- goto L598;
- }
- Response->ReturnCode[0] = 0x08;
- Response->ReturnCode[1] = 0x00;
- Response->BroadcasterGroupID = Request->BroadcasterGroupID;
- Response->RecordingControl = 0x01;
- return;
- L598:
- Response->RecordingControl = 0x00;
- }
- // CRQ
- static void bcas_crq(void)
- {
- BCAS_CRQ_Response_t *Response;
- if (Input.dBuff[INPUT_OFFSET_LC] != 0x05) {
- reply(0x6700); // length error
- return;
- }
- Response = (BCAS_CRQ_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x04;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- Response->ReturnCode[0] = 0x21;
- Response->ReturnCode[1] = 0x00;
- SetOutputLen(sizeof(BCAS_CRQ_Response_t));
- reply(0x9000);
- }
- // WUI
- static void bcas_wui(void)
- {
- BCAS_WUI_Response_t *Response;
- if (Input.dBuff[INPUT_OFFSET_LC] != 0x01) {
- reply(0x6700);
- return;
- }
- SetOutputLen(sizeof(BCAS_WUI_Response_t));
- Response = (BCAS_WUI_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- memset(Response, 0, sizeof(BCAS_WUI_Response_t));
- Response->UnitLength = 0x04;
- Response->ReturnCode[0] = 0xA1; // 0x?1 -> 0xA1
- Response->ReturnCode[1] = 0x01;
- reply(0x9000);
- }
- // update_Request
- static void bcas_upd(void)
- {
- BCAS_UPD_Request_t *Request;
- BCAS_UPD_Response_t *Response;
- if (Input.dBuff[INPUT_OFFSET_LC] < 33 || Input.dBuff[INPUT_OFFSET_LC] > sizeof(BCAS_UPD_Request_t)) {
- reply(0x6700); // length error
- return;
- }
- Request = (BCAS_UPD_Request_t *)&Input.dBuff[INPUT_OFFSET_DATA];
- Response = (BCAS_UPD_Response_t *)&Output.dBuff[OUTPUT_OFFSET_DATA];
- //
- //
- //
- Response->ProtocolUnitNumber = 0x00;
- Response->UnitLength = 0x04;
- Response->ICCardInstruction[0] = 0x00;
- Response->ICCardInstruction[1] = 0x00;
- Response->ReturnCode[0] = 0x21;
- Response->ReturnCode[1] = 0x00;
- SetOutputLen(sizeof(BCAS_UPD_Response_t));
- SetReturnCode(0x9000);
- switch (Request->Index) {
- case BCAS_UPD_INDEX_SystemKey:
- memcpy(State.SystemKey, Request->Update.SystemKey, sizeof(State.SystemKey));
- WriteOffsetData(offsetof(CryptoState_t, SystemKey), sizeof(State.SystemKey));
- break;
- case BCAS_UPD_INDEX_SystemIV:
- memcpy(State.SystemIV, Request->Update.SystemIV, sizeof(State.SystemIV));
- WriteOffsetData(offsetof(CryptoState_t, SystemIV), sizeof(State.SystemIV));
- break;
- case BCAS_UPD_INDEX_Sbox:
- memcpy(State.Sbox, Request->Update.Sbox, sizeof(State.Sbox));
- WriteOffsetData(offsetof(CryptoState_t, Sbox), sizeof(State.Sbox));
- break;
- case BCAS_UPD_INDEX_LUT0:
- memcpy(State.LookupTable + 0, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 0, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT1:
- memcpy(State.LookupTable + 32, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 32, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT2:
- memcpy(State.LookupTable + 64, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 64, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT3:
- memcpy(State.LookupTable + 96, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 96, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT4:
- memcpy(State.LookupTable + 128, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 128, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT5:
- memcpy(State.LookupTable + 160, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 160, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT6:
- memcpy(State.LookupTable + 192, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 192, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_LUT7:
- memcpy(State.LookupTable + 224, Request->Update.LookupTable, sizeof(Request->Update.LookupTable));
- WriteOffsetData(offsetof(CryptoState_t, LookupTable) + 224, sizeof(Request->Update.LookupTable));
- break;
- case BCAS_UPD_INDEX_IV1:
- memcpy(State.InitialisationVector1, Request->Update.IV, sizeof(State.InitialisationVector1));
- WriteOffsetData(offsetof(CryptoState_t, InitialisationVector1), sizeof(State.InitialisationVector1));
- break;
- case BCAS_UPD_INDEX_IV2:
- memcpy(State.InitialisationVector2, Request->Update.IV, sizeof(State.InitialisationVector2));
- WriteOffsetData(offsetof(CryptoState_t, InitialisationVector2), sizeof(State.InitialisationVector2));
- break;
- case BCAS_UPD_INDEX_ScheduleIV:
- memcpy(State.ScheduleIV, Request->Update.ScheduleIV, sizeof(State.ScheduleIV));
- WriteOffsetData(offsetof(CryptoState_t, ScheduleIV), sizeof(State.ScheduleIV));
- break;
- case BCAS_UPD_INDEX_CardInfo0:
- memcpy(CardInfo[0], Request->Update.CardInfo, sizeof(CardInfo[0]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo1:
- memcpy(CardInfo[1], Request->Update.CardInfo, sizeof(CardInfo[1]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo2:
- memcpy(CardInfo[2], Request->Update.CardInfo, sizeof(CardInfo[2]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo3:
- memcpy(CardInfo[3], Request->Update.CardInfo, sizeof(CardInfo[3]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo4:
- memcpy(CardInfo[4], Request->Update.CardInfo, sizeof(CardInfo[4]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo5:
- memcpy(CardInfo[5], Request->Update.CardInfo, sizeof(CardInfo[5]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo6:
- memcpy(CardInfo[6], Request->Update.CardInfo, sizeof(CardInfo[6]));
- WriteCardInfoData();
- break;
- case BCAS_UPD_INDEX_CardInfo7:
- memcpy(CardInfo[7], Request->Update.CardInfo, sizeof(CardInfo[7]));
- WriteCardInfoData();
- break;
- default:
- SetReturnCode(0x6900);
- break;
- }
- }
- // Input_perser
- static void cardrun_1(void)
- {
- memset(&Output, 0, sizeof(Output));
- chksum1(&Input);
- SetOutputLen(0);
- if (CkSum) {
- //reply(0x6 );
- goto L831;
- }
- if (Input.len != (5 + Input.dBuff[INPUT_OFFSET_LC] + 1)) {
- if (Input.dBuff[INPUT_OFFSET_P1] || Input.len != (5 + Input.dBuff[INPUT_OFFSET_LC])) {
- reply(0x6F00);
- goto L831;
- }
- }
- if (Input.dBuff[INPUT_OFFSET_CLA] == 0x00) { // Classs 0 ?
- //
- } else if (Input.dBuff[INPUT_OFFSET_CLA] == 0x90) {
- if (Input.dBuff[INPUT_OFFSET_P1] | Input.dBuff[INPUT_OFFSET_P2]) {
- reply(0x6A86); // P1,P2 is not correct.
- goto L831;
- }
- switch (Input.dBuff[INPUT_OFFSET_INS]) {
- case BCAS_INS_INT: // 0x30
- bcas_int();
- break;
- case BCAS_INS_IDI: // 0x32
- bcas_idi();
- break;
- case BCAS_INS_ECM: // 0x34
- bcas_ecm();
- break;
- case BCAS_INS_EMM: // 0x36
- bcas_emm();
- break;
- case BCAS_INS_EMG: // 0x38
- bcas_emg();
- break;
- case BCAS_INS_EMD: // 0x3A
- bcas_emd();
- break;
- case BCAS_INS_CHK: // 0x3C
- bcas_chk();
- break;
- case BCAS_INS_CRQ: // 0x50
- bcas_crq();
- break;
- case BCAS_INS_WUI: // 0x80
- bcas_wui();
- break;
- case BCAS_INS_UPD: //
- bcas_upd();
- break;
- default:
- reply(0x6D00); // Unknown INS
- break;
- }
- } else {
- if (Input.dBuff[INPUT_OFFSET_CLA] & 0 )
- reply(0x6800); // lower nibble != 0
- else //if (Input.dBuff[INPUT_OFFSET_CLA & 0xf0)!= 0x90 )
- reply(0x6E00); // upper nibble != 9
- }
- L831:
- reply1();
- }
- void DefaultData(void);
- #if !defined(FAKE_WINSCARD) // Driver mode
- static void mainloop(void)
- {
- while (1) {
- testptrn1();
- cardrun_1();
- }
- }
- #define entry main
- int entry(int argc, char *argv[])
- {
- Load_Card();
- DefaultData();
- mainloop();
- return 0;
- }
- #else // Fake WINSCARD
- void CardInit(void)
- {
- Load_Card();
- DefaultData();
- }
- void CardRun(void)
- {
- cardrun_1();
- }
- void CardIN(const u8 *In, u8 Length)
- {
- Input.pt1 = 0x00;
- Input.pt2 = Flg;
- Input.len = Length;
- memcpy(Input.dBuff, In, Length);
- Input.dBuff[Input.len] = 0x00; // zero clear
- chksum1(&Input); // calculate sum
- Input.dBuff[Input.len] = CkSum; // store sum
- Flg ^= 0x40;
- }
- u8 CardOUT(u8 *Out)
- {
- memcpy(Out, Output.dBuff, Output.len);
- return Output.len;
- }
- #endif
- #include "Init_data.cpp"
Advertisement
Add Comment
Please, Sign In to add comment