1 //-----------------------------------------------------------------------------
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Routines to support mifare classic sniffer.
9 //-----------------------------------------------------------------------------
11 #include "mifaresniff.h"
14 static int sniffState
= SNF_INIT
;
15 static uint8_t sniffUIDType
;
16 static uint8_t sniffUID
[8] = {0x00};
17 static uint8_t sniffATQA
[2] = {0x00};
18 static uint8_t sniffSAK
;
19 static uint8_t sniffBuf
[16] = {0x00};
20 static uint32_t timerData
= 0;
23 bool MfSniffInit(void){
24 memset(sniffUID
, 0x00, 8);
25 memset(sniffATQA
, 0x00, 2);
27 sniffUIDType
= SNF_UID_4
;
32 bool MfSniffEnd(void){
34 cmd_send(CMD_ACK
,0,0,0,0,0);
40 bool RAMFUNC
MfSniffLogic(const uint8_t *data
, uint16_t len
, uint8_t *parity
, uint16_t bitCnt
, bool reader
) {
42 if (reader
&& (len
== 1) && (bitCnt
== 7)) { // reset on 7-Bit commands from reader
43 sniffState
= SNF_INIT
;
48 if ((len
== 1) && (reader
) && (bitCnt
== 7) ) { // REQA or WUPA from reader
49 sniffUIDType
= SNF_UID_4
;
50 memset(sniffUID
, 0x00, 8);
51 memset(sniffATQA
, 0x00, 2);
53 sniffState
= SNF_WUPREQ
;
58 if ((!reader
) && (len
== 2)) { // ATQA from tag
59 memcpy(sniffATQA
, data
, 2);
60 sniffState
= SNF_ATQA
;
65 if ((reader
) && (len
== 2) && (data
[0] == 0x93) && (data
[1] == 0x20)) { // Select ALL from reader
66 sniffState
= SNF_ANTICOL1
;
71 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) { // UID from tag (CL1)
72 memcpy(sniffUID
+ 3, data
, 4);
73 sniffState
= SNF_UID1
;
78 if ((reader
) && (len
== 9) && (data
[0] == 0x93) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9))) { // Select 4 Byte UID from reader
84 if ((!reader
) && (len
== 3) && (CheckCrc14443(CRC_14443_A
, data
, 3))) { // SAK from card?
86 if (sniffUID
[3] == 0x88) { // CL2 UID part to be expected
87 sniffState
= SNF_ANTICOL2
;
88 } else { // select completed
89 sniffState
= SNF_CARD_IDLE
;
95 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) { // CL2 UID
96 memcpy(sniffUID
, sniffUID
+4, 3);
97 memcpy(sniffUID
+3, data
, 4);
98 sniffUIDType
= SNF_UID_7
;
99 sniffState
= SNF_UID2
;
104 if ((reader
) && (len
== 9) && (data
[0] == 0x95) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9))) { // Select 2nd part of 7 Byte UID
105 sniffState
= SNF_SAK
;
109 case SNF_CARD_IDLE
:{ // trace the card select sequence
112 memcpy(sniffBuf
+ 2, sniffUID
, 7);
113 memcpy(sniffBuf
+ 9, sniffATQA
, 2);
114 sniffBuf
[11] = sniffSAK
;
117 LogTrace(sniffBuf
, 14, 0, 0, NULL
, TRUE
);
118 } // intentionally no break;
120 LogTrace(data
, len
, 0, 0, NULL
, TRUE
);
121 sniffState
= SNF_CARD_RESP
;
122 timerData
= GetTickCount();
126 LogTrace(data
, len
, 0, 0, NULL
, FALSE
);
127 sniffState
= SNF_CARD_CMD
;
128 timerData
= GetTickCount();
133 sniffState
= SNF_INIT
;
141 bool RAMFUNC
MfSniffSend(uint16_t maxTimeoutMs
) {
142 if (BigBuf_get_traceLen() && (GetTickCount() > timerData
+ maxTimeoutMs
)) {
143 return intMfSniffSend();
148 // internal sending function. not a RAMFUNC.
149 bool intMfSniffSend() {
152 int pckLen
= BigBuf_get_traceLen();
154 uint8_t *trace
= BigBuf_get_addr();
158 pckSize
= MIN(USB_CMD_DATA_SIZE
, pckLen
);
160 cmd_send(CMD_ACK
, 1, BigBuf_get_traceLen(), pckSize
, trace
+ BigBuf_get_traceLen() - pckLen
, pckSize
);
168 cmd_send(CMD_ACK
,2,0,0,0,0);