X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/71d90e54cdfbe625c70185b2db53e4d87d68ec14..24c49d36ba3ea8acb9be21bdbb51503969a5a113:/armsrc/mifaresniff.c diff --git a/armsrc/mifaresniff.c b/armsrc/mifaresniff.c index fc5156fd..2dcd6d1a 100644 --- a/armsrc/mifaresniff.c +++ b/armsrc/mifaresniff.c @@ -9,184 +9,179 @@ //----------------------------------------------------------------------------- #include "mifaresniff.h" -#include "apps.h" static int sniffState = SNF_INIT; -static uint8_t sniffUIDType; -static uint8_t sniffUID[8]; -static uint8_t sniffATQA[2]; -static uint8_t sniffSAK; -static uint8_t sniffBuf[16]; -static int timerData = 0; - - -int MfSniffInit(void){ - rsamples = 0; - memset(sniffUID, 0x00, 8); - memset(sniffATQA, 0x00, 2); +static uint8_t sniffUIDType = 0; +static uint8_t sniffUID[10] = {0,0,0,0,0,0,0,0,0,0}; +static uint8_t sniffATQA[2] = {0,0}; +static uint8_t sniffSAK = 0; +static uint8_t sniffBuf[17]; +static uint32_t timerData = 0; + +void MfSniffInit(void){ + memset(sniffUID, 0x00, sizeof(sniffUID)); + memset(sniffATQA, 0x00, sizeof(sniffATQA)); + memset(sniffBuf, 0x00, sizeof(sniffBuf)); sniffSAK = 0; sniffUIDType = SNF_UID_4; - - return 0; } -int MfSniffEnd(void){ - UsbCommand ack = {CMD_ACK, {0, 0, 0}}; - +void MfSniffEnd(void){ LED_B_ON(); - UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand)); + cmd_send(CMD_ACK,0,0,0,0,0); LED_B_OFF(); - - return 0; } -int RAMFUNC MfSniffLogic(const uint8_t * data, int len, uint32_t parity, int bitCnt, int reader) { +bool RAMFUNC MfSniffLogic(const uint8_t *data, uint16_t len, uint8_t *parity, uint16_t bitCnt, bool reader) { - if ((len == 1) && (bitCnt = 9) && (data[0] > 0x0F)) { + // reset on 7-Bit commands from reader + if (reader && (len == 1) && (bitCnt == 7)) { sniffState = SNF_INIT; } switch (sniffState) { case SNF_INIT:{ - if ((reader) && (len == 1) && (bitCnt == 9) && ((data[0] == 0x26) || (data[0] == 0x52))) { - sniffUIDType = SNF_UID_4; - memset(sniffUID, 0x00, 8); - memset(sniffATQA, 0x00, 2); - sniffSAK = 0; - + // REQA or WUPA from reader + if ((len == 1) && (reader) && (bitCnt == 7) ) { + MfSniffInit(); sniffState = SNF_WUPREQ; } break; } case SNF_WUPREQ:{ - if ((!reader) && (len == 2)) { - memcpy(sniffATQA, data, 2); - + // ATQA from tag + if ((!reader) && (len == 2)) { + sniffATQA[0] = data[0]; + sniffATQA[1] = data[1]; sniffState = SNF_ATQA; } break; } case SNF_ATQA:{ - if ((reader) && (len == 2) && (data[0] == 0x93) && (data[1] == 0x20)) { + // Select ALL from reader + if ((reader) && (len == 2) && (data[0] == 0x93) && (data[1] == 0x20)) sniffState = SNF_ANTICOL1; - } break; } case SNF_ANTICOL1:{ - if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { - memcpy(sniffUID + 3, data, 4); - + // UID from tag (CL1) + if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { + memcpy(sniffUID, data, 4); sniffState = SNF_UID1; } break; } case SNF_UID1:{ - if ((reader) && (len == 9) && (data[0] == 0x93) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) { + // Select 4 Byte UID from reader + if ((reader) && (len == 9) && (data[0] == 0x93) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) sniffState = SNF_SAK; - } break; } case SNF_SAK:{ - if ((!reader) && (len == 3) && (CheckCrc14443(CRC_14443_A, data, 3))) { + if ((!reader) && (len == 3) && (CheckCrc14443(CRC_14443_A, data, 3))) { // SAK from card? sniffSAK = data[0]; - if (sniffUID[3] == 0x88) { - sniffState = SNF_ANTICOL2; - } else { + if (sniffUID[0] == 0x88) // CL2/3 UID part to be expected + sniffState = (sniffState == SNF_ANTICOL2 ) ? SNF_ANTICOL3 : SNF_ANTICOL2; + else // select completed sniffState = SNF_CARD_IDLE; - } } break; } case SNF_ANTICOL2:{ - if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { - memcpy(sniffUID, data, 4); + // CL2 UID + if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { + sniffUID[0] = sniffUID[1]; + sniffUID[1] = sniffUID[2]; + sniffUID[2] = sniffUID[3]; + memcpy(sniffUID+3, data, 4); sniffUIDType = SNF_UID_7; - sniffState = SNF_UID2; - } + } break; } case SNF_UID2:{ - if ((reader) && (len == 9) && (data[0] == 0x95) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) { + // Select 2nd part of 7 Byte UID + if ((reader) && (len == 9) && (data[0] == 0x95) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) sniffState = SNF_SAK; - Dbprintf("SNF_SAK"); + break; + } + case SNF_ANTICOL3:{ + // CL3 UID + if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { + // 3+3+4 = 10. + sniffUID[3] = sniffUID[4]; + sniffUID[4] = sniffUID[5]; + sniffUID[5] = sniffUID[6]; + memcpy(sniffUID+6, data, 4); + sniffUIDType = SNF_UID_10; + sniffState = SNF_UID3; } break; } - case SNF_CARD_IDLE:{ + case SNF_UID3:{ + // Select 3nd part of 10 Byte UID + if ((reader) && (len == 9) && (data[0] == 0x97) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) + sniffState = SNF_SAK; + break; + } + case SNF_CARD_IDLE:{ // trace the card select sequence sniffBuf[0] = 0xFF; sniffBuf[1] = 0xFF; - memcpy(sniffBuf + 2, sniffUID, 7); - memcpy(sniffBuf + 9, sniffATQA, 2); - sniffBuf[11] = sniffSAK; - sniffBuf[12] = 0xFF; - sniffBuf[13] = 0xFF; - LogTrace(sniffBuf, 14, 0, parity, true); - timerData = GetTickCount(); - } - case SNF_CARD_CMD:{ - LogTrace(data, len, 0, parity, true); - + memcpy(sniffBuf + 2, sniffUID, sizeof(sniffUID)); + memcpy(sniffBuf + 12, sniffATQA, sizeof(sniffATQA)); + sniffBuf[14] = sniffSAK; + sniffBuf[15] = 0xFF; + sniffBuf[16] = 0xFF; + LogTrace(sniffBuf, sizeof(sniffBuf), 0, 0, NULL, TRUE); + } // intentionally no break; + case SNF_CARD_CMD:{ + LogTrace(data, len, 0, 0, NULL, TRUE); sniffState = SNF_CARD_RESP; timerData = GetTickCount(); break; } case SNF_CARD_RESP:{ - LogTrace(data, len, 0, parity, false); - + LogTrace(data, len, 0, 0, NULL, FALSE); sniffState = SNF_CARD_CMD; timerData = GetTickCount(); break; } - default: sniffState = SNF_INIT; break; } - - return 0; + return FALSE; } -int RAMFUNC MfSniffSend(int maxTimeoutMs) { - if (traceLen && (timerData + maxTimeoutMs < GetTickCount())) { +bool RAMFUNC MfSniffSend(uint16_t maxTimeoutMs) { + if (BigBuf_get_traceLen() && (GetTickCount() > timerData + maxTimeoutMs)) { return intMfSniffSend(); } - return 0; + return FALSE; } -// internal seding function. not a RAMFUNC. -int intMfSniffSend() { - +// internal sending function. not a RAMFUNC. +bool intMfSniffSend() { + int pckSize = 0; - int pckLen = traceLen; + int pckLen = BigBuf_get_traceLen(); int pckNum = 0; + uint8_t *data = BigBuf_get_addr(); - if (!traceLen) return 0; - FpgaDisableSscDma(); - while (pckLen > 0) { - pckSize = min(32, pckLen); - UsbCommand ack = {CMD_ACK, {1, pckSize, pckNum}}; - memcpy(ack.d.asBytes, trace + traceLen - pckLen, pckSize); - + pckSize = MIN(USB_CMD_DATA_SIZE, pckLen); LED_B_ON(); - UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand)); - SpinDelay(20); + cmd_send(CMD_ACK, 1, BigBuf_get_traceLen(), pckSize, data + BigBuf_get_traceLen() - pckLen, pckSize); LED_B_OFF(); - pckLen -= pckSize; pckNum++; } - UsbCommand ack = {CMD_ACK, {2, 0, 0}}; - LED_B_ON(); - UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand)); + cmd_send(CMD_ACK,2,0,0,0,0); // 2 == data transfer is finished. LED_B_OFF(); - traceLen = 0; - memset(trace, 0x44, TRACE_SIZE); - - return 1; + clear_trace(); + return TRUE; }