]>
git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifaresniff.c
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
= 0;
16 static uint8_t sniffUID
[10] = {0,0,0,0,0,0,0,0,0,0};
17 static uint8_t sniffATQA
[2] = {0,0};
18 static uint8_t sniffSAK
= 0;
19 static uint8_t sniffBuf
[17];
20 static uint32_t timerData
= 0;
22 void MfSniffInit(void){
23 memset(sniffUID
, 0x00, sizeof(sniffUID
));
24 memset(sniffATQA
, 0x00, sizeof(sniffATQA
));
25 memset(sniffBuf
, 0x00, sizeof(sniffBuf
));
27 sniffUIDType
= SNF_UID_4
;
30 void MfSniffEnd(void){
32 cmd_send(CMD_ACK
,0,0,0,0,0);
36 bool RAMFUNC
MfSniffLogic(const uint8_t *data
, uint16_t len
, uint8_t *parity
, uint16_t bitCnt
, bool reader
) {
38 // reset on 7-Bit commands from reader
39 if (reader
&& (len
== 1) && (bitCnt
== 7)) {
40 sniffState
= SNF_INIT
;
45 // REQA or WUPA from reader
46 if ((len
== 1) && (reader
) && (bitCnt
== 7) ) {
48 sniffState
= SNF_WUPREQ
;
54 if ((!reader
) && (len
== 2)) {
55 sniffATQA
[0] = data
[0];
56 sniffATQA
[1] = data
[1];
57 sniffState
= SNF_ATQA
;
62 // Select ALL from reader
63 if ((reader
) && (len
== 2) && (data
[0] == 0x93) && (data
[1] == 0x20))
64 sniffState
= SNF_ANTICOL1
;
69 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) {
70 memcpy(sniffUID
, data
, 4);
71 sniffState
= SNF_UID1
;
76 // Select 4 Byte UID from reader
77 if ((reader
) && (len
== 9) && (data
[0] == 0x93) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9)))
82 if ((!reader
) && (len
== 3) && (CheckCrc14443(CRC_14443_A
, data
, 3))) { // SAK from card?
84 if (sniffUID
[0] == 0x88) // CL2/3 UID part to be expected
85 sniffState
= (sniffState
== SNF_ANTICOL2
) ? SNF_ANTICOL3
: SNF_ANTICOL2
;
86 else // select completed
87 sniffState
= SNF_CARD_IDLE
;
93 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) {
94 sniffUID
[0] = sniffUID
[1];
95 sniffUID
[1] = sniffUID
[2];
96 sniffUID
[2] = sniffUID
[3];
97 memcpy(sniffUID
+3, data
, 4);
98 sniffUIDType
= SNF_UID_7
;
99 sniffState
= SNF_UID2
;
104 // Select 2nd part of 7 Byte UID
105 if ((reader
) && (len
== 9) && (data
[0] == 0x95) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9)))
106 sniffState
= SNF_SAK
;
111 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) {
113 sniffUID
[3] = sniffUID
[4];
114 sniffUID
[4] = sniffUID
[5];
115 sniffUID
[5] = sniffUID
[6];
116 memcpy(sniffUID
+6, data
, 4);
117 sniffUIDType
= SNF_UID_10
;
118 sniffState
= SNF_UID3
;
123 // Select 3nd part of 10 Byte UID
124 if ((reader
) && (len
== 9) && (data
[0] == 0x97) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9)))
125 sniffState
= SNF_SAK
;
128 case SNF_CARD_IDLE
:{ // trace the card select sequence
131 memcpy(sniffBuf
+ 2, sniffUID
, sizeof(sniffUID
));
132 memcpy(sniffBuf
+ 12, sniffATQA
, sizeof(sniffATQA
));
133 sniffBuf
[14] = sniffSAK
;
136 LogTrace(sniffBuf
, sizeof(sniffBuf
), 0, 0, NULL
, TRUE
);
137 } // intentionally no break;
139 LogTrace(data
, len
, 0, 0, NULL
, TRUE
);
140 sniffState
= SNF_CARD_RESP
;
141 timerData
= GetTickCount();
145 LogTrace(data
, len
, 0, 0, NULL
, FALSE
);
146 sniffState
= SNF_CARD_CMD
;
147 timerData
= GetTickCount();
151 sniffState
= SNF_INIT
;
157 bool RAMFUNC
MfSniffSend(uint16_t maxTimeoutMs
) {
158 if (BigBuf_get_traceLen() && (GetTickCount() > timerData
+ maxTimeoutMs
)) {
159 return intMfSniffSend();
164 // internal sending function. not a RAMFUNC.
165 bool intMfSniffSend() {
168 int pckLen
= BigBuf_get_traceLen();
170 uint8_t *data
= BigBuf_get_addr();
174 pckSize
= MIN(USB_CMD_DATA_SIZE
, pckLen
);
176 cmd_send(CMD_ACK
, 1, BigBuf_get_traceLen(), pckSize
, data
+ BigBuf_get_traceLen() - pckLen
, pckSize
);
183 cmd_send(CMD_ACK
,2,0,0,0,0); // 2 == data transfer is finished.