-//-----------------------------------------------------------------------------
-// Read an ISO 14443a tag. Send out commands and store answers.
-//
-//-----------------------------------------------------------------------------
-void ReaderMifare(uint32_t parameter)
-{
- // Mifare AUTH
- uint8_t mf_auth[] = { 0x60,0x00,0xf5,0x7b };
- uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
-
- uint8_t* receivedAnswer = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
- traceLen = 0;
- tracing = false;
-
- iso14443a_setup();
-
- LED_A_ON();
- LED_B_OFF();
- LED_C_OFF();
-
- byte_t nt_diff = 0;
- LED_A_OFF();
- byte_t par = 0;
- byte_t par_mask = 0xff;
- byte_t par_low = 0;
- int led_on = TRUE;
- uint8_t uid[8];
- uint32_t cuid;
-
- tracing = FALSE;
- byte_t nt[4] = {0,0,0,0};
- byte_t nt_attacked[4], nt_noattack[4];
- byte_t par_list[8] = {0,0,0,0,0,0,0,0};
- byte_t ks_list[8] = {0,0,0,0,0,0,0,0};
- num_to_bytes(parameter, 4, nt_noattack);
- int isOK = 0, isNULL = 0;
-
- while(TRUE)
- {
- LED_C_ON();
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- SpinDelay(200);
- FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
- LED_C_OFF();
-
- // Test if the action was cancelled
- if(BUTTON_PRESS()) {
- break;
- }
-
- if(!iso14443a_select_card(uid, NULL, &cuid)) continue;
-
- // Transmit MIFARE_CLASSIC_AUTH
- ReaderTransmit(mf_auth, sizeof(mf_auth));
-
- // Receive the (16 bit) "random" nonce
- if (!ReaderReceive(receivedAnswer)) continue;
- memcpy(nt, receivedAnswer, 4);
-
- // Transmit reader nonce and reader answer
- ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar),par);
-
- // Receive 4 bit answer
- if (ReaderReceive(receivedAnswer))
- {
- if ( (parameter != 0) && (memcmp(nt, nt_noattack, 4) == 0) ) continue;
-
- isNULL = (nt_attacked[0] = 0) && (nt_attacked[1] = 0) && (nt_attacked[2] = 0) && (nt_attacked[3] = 0);
- if ( (isNULL != 0 ) && (memcmp(nt, nt_attacked, 4) != 0) ) continue;
-
- if (nt_diff == 0)
- {
- LED_A_ON();
- memcpy(nt_attacked, nt, 4);
- par_mask = 0xf8;
- par_low = par & 0x07;
- }
-
- led_on = !led_on;
- if(led_on) LED_B_ON(); else LED_B_OFF();
- par_list[nt_diff] = par;
- ks_list[nt_diff] = receivedAnswer[0] ^ 0x05;
-
- // Test if the information is complete
- if (nt_diff == 0x07) {
- isOK = 1;
- break;
- }
-
- nt_diff = (nt_diff + 1) & 0x07;
- mf_nr_ar[3] = nt_diff << 5;
- par = par_low;
- } else {
- if (nt_diff == 0)
- {
- par++;
- } else {
- par = (((par >> 3) + 1) << 3) | par_low;
- }
- }
- }
-
- LogTrace(nt, 4, 0, GetParity(nt, 4), TRUE);
- LogTrace(par_list, 8, 0, GetParity(par_list, 8), TRUE);
- LogTrace(ks_list, 8, 0, GetParity(ks_list, 8), TRUE);
-
- UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
- memcpy(ack.d.asBytes + 0, uid, 4);
- memcpy(ack.d.asBytes + 4, nt, 4);
- memcpy(ack.d.asBytes + 8, par_list, 8);
- memcpy(ack.d.asBytes + 16, ks_list, 8);
-
- LED_B_ON();
- UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
- LED_B_OFF();
-
- // Thats it...
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LEDsoff();
- tracing = TRUE;
-
- if (MF_DBGLEVEL >= 1) DbpString("COMMAND mifare FINISHED");
-}
-
-//-----------------------------------------------------------------------------
-// Select, Authenticaate, Read an MIFARE tag.
-// read block
-//-----------------------------------------------------------------------------
-void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
-{
- // params
- uint8_t blockNo = arg0;
- uint8_t keyType = arg1;
- uint64_t ui64Key = 0;
- ui64Key = bytes_to_num(datain, 6);
-
- // variables
- byte_t isOK = 0;
- byte_t dataoutbuf[16];
- uint8_t uid[8];
- uint32_t cuid;
- struct Crypto1State mpcs = {0, 0};
- struct Crypto1State *pcs;
- pcs = &mpcs;
-
- // clear trace
- traceLen = 0;
-// tracing = false;
-
- iso14443a_setup();
-
- LED_A_ON();
- LED_B_OFF();
- LED_C_OFF();
-
- while (true) {
- if(!iso14443a_select_card(uid, NULL, &cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
- break;
- };
-
- if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
- break;
- };
-
- if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
- break;
- };
-
- if(mifare_classic_halt(pcs, cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
- break;
- };
-
- isOK = 1;
- break;
- }
-
- // ----------------------------- crypto1 destroy
- crypto1_destroy(pcs);
-
- if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
-
- // add trace trailer
- uid[0] = 0xff;
- uid[1] = 0xff;
- uid[2] = 0xff;
- uid[3] = 0xff;
- LogTrace(uid, 4, 0, 0, TRUE);
-
- UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
- memcpy(ack.d.asBytes, dataoutbuf, 16);
-
- LED_B_ON();
- UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
- LED_B_OFF();
-
-
- // Thats it...
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LEDsoff();
-// tracing = TRUE;
-
-}
-
-//-----------------------------------------------------------------------------
-// Select, Authenticaate, Read an MIFARE tag.
-// read sector (data = 4 x 16 bytes = 64 bytes)
-//-----------------------------------------------------------------------------
-void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
-{
- // params
- uint8_t sectorNo = arg0;
- uint8_t keyType = arg1;
- uint64_t ui64Key = 0;
- ui64Key = bytes_to_num(datain, 6);
-
- // variables
- byte_t isOK = 0;
- byte_t dataoutbuf[16 * 4];
- uint8_t uid[8];
- uint32_t cuid;
- struct Crypto1State mpcs = {0, 0};
- struct Crypto1State *pcs;
- pcs = &mpcs;
-
- // clear trace
- traceLen = 0;
-// tracing = false;
-
- iso14443a_setup();
-
- LED_A_ON();
- LED_B_OFF();
- LED_C_OFF();
-
- while (true) {
- if(!iso14443a_select_card(uid, NULL, &cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
- break;
- };
-
- if(mifare_classic_auth(pcs, cuid, sectorNo * 4, keyType, ui64Key, AUTH_FIRST)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
- break;
- };
-
- if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 0, dataoutbuf + 16 * 0)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Read block 0 error");
- break;
- };
- if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 1, dataoutbuf + 16 * 1)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Read block 1 error");
- break;
- };
- if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 2, dataoutbuf + 16 * 2)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Read block 2 error");
- break;
- };
- if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 3, dataoutbuf + 16 * 3)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Read block 3 error");
- break;
- };
-
- if(mifare_classic_halt(pcs, cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
- break;
- };
-
- isOK = 1;
- break;
- }
-
- // ----------------------------- crypto1 destroy
- crypto1_destroy(pcs);
-
- if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");
-
- // add trace trailer
- uid[0] = 0xff;
- uid[1] = 0xff;
- uid[2] = 0xff;
- uid[3] = 0xff;
- LogTrace(uid, 4, 0, 0, TRUE);
-
- UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
- memcpy(ack.d.asBytes, dataoutbuf, 16 * 2);
-
- LED_B_ON();
- UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
-
- SpinDelay(100);
-
- memcpy(ack.d.asBytes, dataoutbuf + 16 * 2, 16 * 2);
- UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
- LED_B_OFF();
-
- // Thats it...
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LEDsoff();
-// tracing = TRUE;
-
-}