1 //-----------------------------------------------------------------------------
2 // Copyright (C) Gerhard de Koning Gans - May 2008
3 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
17 // Routines to support mifare classic sniffer.
18 //-----------------------------------------------------------------------------
20 #include "mifaresniff_disabled.h"
23 # define CheckCrc14A(data, len) check_crc(CRC_14443_A, (data), (len))
26 //static int sniffState = SNF_INIT;
27 static uint8_t sniffUIDType
= 0;
28 static uint8_t sniffUID
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
29 static uint8_t sniffATQA
[2] = {0, 0};
30 static uint8_t sniffSAK
= 0;
31 static uint8_t sniffBuf
[17];
32 static uint32_t timerData
= 0;
34 //-----------------------------------------------------------------------------
37 // if no activity for 2sec, it sends the collected data to the client.
38 //-----------------------------------------------------------------------------
40 void RAMFUNC
SniffMifare(uint8_t param
) {
42 // bit 0 - trigger from first card answer
43 // bit 1 - trigger from first reader 7-bit request
45 // C(red) A(yellow) B(green)
47 iso14443a_setup(FPGA_HF_ISO14443A_SNIFFER
);
49 // Allocate memory from BigBuf for some buffers
50 // free all previous allocations first
52 BigBuf_Clear_ext(false);
56 // The command (reader -> tag) that we're receiving.
57 uint8_t receivedCmd
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
58 uint8_t receivedCmdPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
60 // The response (tag -> reader) that we're receiving.
61 uint8_t receivedResp
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
62 uint8_t receivedRespPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
64 // allocate the DMA buffer, used to stream samples from the FPGA
65 uint8_t *dmaBuf
= BigBuf_malloc(DMA_BUFFER_SIZE
);
66 uint8_t *data
= dmaBuf
;
67 uint8_t previous_data
= 0;
68 int dataLen
, maxDataLen
= 0;
69 bool ReaderIsActive
= false;
70 bool TagIsActive
= false;
72 // We won't start recording the frames that we acquire until we trigger;
73 // a good trigger condition to get started is probably when we see a
74 // response from the tag.
75 // triggered == false -- to wait first for card
76 //bool triggered = !(param & 0x03);
79 // Set up the demodulator for tag -> reader responses.
80 Demod14aInit(receivedResp
, receivedRespPar
);
82 // Set up the demodulator for the reader -> tag commands
83 Uart14aInit(receivedCmd
, sizeof(receivedCmd
), receivedCmdPar
);
85 // Setup and start DMA.
86 // set transfer address and number of bytes. Start transfer.
87 if (!FpgaSetupSscDma(dmaBuf
, DMA_BUFFER_SIZE
)) {
88 if (g_dbglevel
> 1) Dbprintf("[!] FpgaSetupSscDma failed. Exiting");
92 tUart14a
*uart
= GetUart14a();
93 tDemod14a
*demod
= GetDemod14a();
97 uint32_t sniffCounter
= 0;
99 while (BUTTON_PRESS() == false) {
103 if ((sniffCounter & 0x0000FFFF) == 0) { // from time to time
104 // check if a transaction is completed (timeout after 2000ms).
105 // if yes, stop the DMA transfer and send what we have so far to the client
106 if (BigBuf_get_traceLen()) {
108 // Reset everything - we missed some sniffed data anyway while the DMA was stopped
110 dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE);
113 ReaderIsActive = false;
115 FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE); // set transfer address and number of bytes. Start transfer.
120 // number of bytes we have processed so far
121 int register readBufDataP
= data
- dmaBuf
;
122 // number of bytes already transferred
123 int register dmaBufDataP
= DMA_BUFFER_SIZE
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
124 if (readBufDataP
<= dmaBufDataP
) // we are processing the same block of data which is currently being transferred
125 dataLen
= dmaBufDataP
- readBufDataP
; // number of bytes still to be processed
127 dataLen
= DMA_BUFFER_SIZE
- readBufDataP
+ dmaBufDataP
; // number of bytes still to be processed
129 // test for length of buffer
130 if (dataLen
> maxDataLen
) { // we are more behind than ever...
131 maxDataLen
= dataLen
;
132 if (dataLen
> (9 * DMA_BUFFER_SIZE
/ 10)) {
133 Dbprintf("[!] blew circular buffer! | datalen %u", dataLen
);
137 if (dataLen
< 1) continue;
139 // primary buffer was stopped ( <-- we lost data!
140 if (!AT91C_BASE_PDC_SSC
->PDC_RCR
) {
141 AT91C_BASE_PDC_SSC
->PDC_RPR
= (uint32_t)dmaBuf
;
142 AT91C_BASE_PDC_SSC
->PDC_RCR
= DMA_BUFFER_SIZE
;
143 Dbprintf("[-] RxEmpty ERROR | data length %d", dataLen
); // temporary
145 // secondary buffer sets as primary, secondary buffer was stopped
146 if (!AT91C_BASE_PDC_SSC
->PDC_RNCR
) {
147 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t)dmaBuf
;
148 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
153 // Need two samples to feed Miller and Manchester-Decoder
154 if (sniffCounter
& 0x01) {
156 // no need to try decoding tag data if the reader is sending
158 uint8_t readerbyte
= (previous_data
& 0xF0) | (*data
>> 4);
159 if (MillerDecoding(readerbyte
, (sniffCounter
- 1) * 4)) {
160 LogTrace(receivedCmd
, uart
->len
, 0, 0, NULL
, true);
164 ReaderIsActive
= (uart
->state
!= STATE_14A_UNSYNCD
);
167 // no need to try decoding tag data if the reader is sending
168 if (!ReaderIsActive
) {
169 uint8_t tagbyte
= (previous_data
<< 4) | (*data
& 0x0F);
170 if (ManchesterDecoding(tagbyte
, 0, (sniffCounter
- 1) * 4)) {
171 LogTrace(receivedResp
, demod
->len
, 0, 0, NULL
, false);
175 TagIsActive
= (demod
->state
!= DEMOD_14A_UNSYNCD
);
178 previous_data
= *data
;
182 if (data
== dmaBuf
+ DMA_BUFFER_SIZE
)
191 void MfSniffInit(void) {
192 memset(sniffUID
, 0x00, sizeof(sniffUID
));
193 memset(sniffATQA
, 0x00, sizeof(sniffATQA
));
194 memset(sniffBuf
, 0x00, sizeof(sniffBuf
));
196 sniffUIDType
= SNF_UID_4
;
200 void MfSniffEnd(void) {
202 reply_old(CMD_ACK
, 0, 0, 0, 0, 0);
207 bool RAMFUNC MfSniffLogic(const uint8_t *data, uint16_t len, uint8_t *parity, uint16_t bitCnt, bool reader) {
209 // reset on 7-Bit commands from reader
210 if (reader && (len == 1) && (bitCnt == 7)) {
211 sniffState = SNF_INIT;
216 switch (sniffState) {
218 // REQA,WUPA or MAGICWUP from reader
219 if ((len == 1) && (reader) && (bitCnt == 7) ) {
221 sniffState = (data[0] == MIFARE_MAGICWUPC1) ? SNF_MAGIC_WUPC2 : SNF_ATQA;
225 case SNF_MAGIC_WUPC2: {
226 if ((len == 1) && (reader) && (data[0] == MIFARE_MAGICWUPC2) ) {
227 sniffState = SNF_CARD_IDLE;
233 if ((!reader) && (len == 2)) {
234 sniffATQA[0] = data[0];
235 sniffATQA[1] = data[1];
236 sniffState = SNF_UID;
242 if ( !reader ) break;
243 if ( len != 9 ) break;
244 if ( !CheckCrc14A(data, 9)) break;
245 if ( data[1] != 0x70 ) break;
247 Dbprintf("[!] UID | %x", data[0]);
249 if ((data[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT)) {
250 // UID_4 - select 4 Byte UID from reader
251 memcpy(sniffUID, data+2, 4);
252 sniffUIDType = SNF_UID_4;
253 sniffState = SNF_SAK;
254 } else if ((data[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2)) {
255 // UID_7 - Select 2nd part of 7 Byte UID
258 sniffUID[0] = sniffUID[1];
259 sniffUID[1] = sniffUID[2];
260 sniffUID[2] = sniffUID[3];
262 memcpy(sniffUID+3, data+2, 4);
263 sniffUIDType = SNF_UID_7;
264 sniffState = SNF_SAK;
265 } else if ((data[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_3)) {
266 // UID_10 - Select 3nd part of 10 Byte UID
268 // get ride of previous 0x88
269 sniffUID[3] = sniffUID[4];
270 sniffUID[4] = sniffUID[5];
271 sniffUID[5] = sniffUID[6];
273 memcpy(sniffUID+6, data+2, 4);
274 sniffUIDType = SNF_UID_10;
275 sniffState = SNF_SAK;
281 if ((!reader) && (len == 3) && (CheckCrc14A(data, 3))) {
283 // CL2 UID part to be expected
284 if (( sniffSAK == 0x04) && (sniffUIDType == SNF_UID_4)) {
285 sniffState = SNF_UID;
286 // CL3 UID part to be expected
287 } else if ((sniffSAK == 0x04) && (sniffUIDType == SNF_UID_7)) {
288 sniffState = SNF_UID;
291 sniffState = SNF_CARD_IDLE;
296 case SNF_CARD_IDLE:{ // trace the card select sequence
299 memcpy(sniffBuf + 2, sniffUID, sizeof(sniffUID));
300 memcpy(sniffBuf + 12, sniffATQA, sizeof(sniffATQA));
301 sniffBuf[14] = sniffSAK;
304 LogTrace(sniffBuf, sizeof(sniffBuf), 0, 0, NULL, true);
305 sniffState = SNF_CARD_CMD;
306 } // intentionally no break;
308 LogTrace(data, len, 0, 0, NULL, reader);
309 timerData = GetTickCount();
313 sniffState = SNF_INIT;
320 void RAMFUNC
MfSniffSend(void) {
321 uint16_t tracelen
= BigBuf_get_traceLen();
322 int packlen
= tracelen
; // total number of bytes to send
323 uint8_t *data
= BigBuf_get_addr();
325 while (packlen
> 0) {
327 uint16_t chunksize
= MIN(PM3_CMD_DATA_SIZE
, packlen
); // chunk size 512
328 reply_old(CMD_ACK
, 1, tracelen
, chunksize
, data
+ tracelen
- packlen
, chunksize
);
329 packlen
-= chunksize
;
334 reply_old(CMD_ACK
, 2, 0, 0, 0, 0); // 2 == data transfer finished.