]>
cvs.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
;
16 static uint8_t sniffUID
[8];
17 static uint8_t sniffATQA
[2];
18 static uint8_t sniffSAK
;
19 static uint8_t sniffBuf
[16];
20 static int timerData
= 0;
23 int MfSniffInit(void){
25 memset(sniffUID
, 0x00, 8);
26 memset(sniffATQA
, 0x00, 2);
28 sniffUIDType
= SNF_UID_4
;
34 // UsbCommand ack = {CMD_ACK, {0, 0, 0}};
37 cmd_send(CMD_ACK
,0,0,0,0,0);
38 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
44 int RAMFUNC
MfSniffLogic(const uint8_t * data
, int len
, uint32_t parity
, int bitCnt
, int reader
) {
46 if ((len
== 1) && (bitCnt
= 9) && (data
[0] > 0x0F)) {
47 sniffState
= SNF_INIT
;
52 if ((reader
) && (len
== 1) && (bitCnt
== 9) && ((data
[0] == 0x26) || (data
[0] == 0x52))) {
53 sniffUIDType
= SNF_UID_4
;
54 memset(sniffUID
, 0x00, 8);
55 memset(sniffATQA
, 0x00, 2);
58 sniffState
= SNF_WUPREQ
;
63 if ((!reader
) && (len
== 2)) {
64 memcpy(sniffATQA
, data
, 2);
66 sniffState
= SNF_ATQA
;
71 if ((reader
) && (len
== 2) && (data
[0] == 0x93) && (data
[1] == 0x20)) {
72 sniffState
= SNF_ANTICOL1
;
77 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) {
78 memcpy(sniffUID
+ 3, data
, 4);
80 sniffState
= SNF_UID1
;
85 if ((reader
) && (len
== 9) && (data
[0] == 0x93) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9))) {
91 if ((!reader
) && (len
== 3) && (CheckCrc14443(CRC_14443_A
, data
, 3))) {
93 if (sniffUID
[3] == 0x88) {
94 sniffState
= SNF_ANTICOL2
;
96 sniffState
= SNF_CARD_IDLE
;
102 if ((!reader
) && (len
== 5) && ((data
[0] ^ data
[1] ^ data
[2] ^ data
[3]) == data
[4])) {
103 memcpy(sniffUID
, data
, 4);
104 sniffUIDType
= SNF_UID_7
;
106 sniffState
= SNF_UID2
;
111 if ((reader
) && (len
== 9) && (data
[0] == 0x95) && (data
[1] == 0x70) && (CheckCrc14443(CRC_14443_A
, data
, 9))) {
112 sniffState
= SNF_SAK
;
120 memcpy(sniffBuf
+ 2, sniffUID
, 7);
121 memcpy(sniffBuf
+ 9, sniffATQA
, 2);
122 sniffBuf
[11] = sniffSAK
;
125 LogTrace(sniffBuf
, 14, 0, parity
, true);
126 timerData
= GetTickCount();
129 LogTrace(data
, len
, 0, parity
, true);
131 sniffState
= SNF_CARD_RESP
;
132 timerData
= GetTickCount();
136 LogTrace(data
, len
, 0, parity
, false);
138 sniffState
= SNF_CARD_CMD
;
139 timerData
= GetTickCount();
144 sniffState
= SNF_INIT
;
151 int RAMFUNC
MfSniffSend(int maxTimeoutMs
) {
152 if (traceLen
&& (timerData
+ maxTimeoutMs
< GetTickCount())) {
153 return intMfSniffSend();
158 // internal seding function. not a RAMFUNC.
159 int intMfSniffSend() {
162 int pckLen
= traceLen
;
165 if (!traceLen
) return 0;
170 pckSize
= MIN(32, pckLen
);
171 // UsbCommand ack = {CMD_ACK, {1, pckSize, pckNum}};
172 // memcpy(ack.d.asBytes, trace + traceLen - pckLen, pckSize);
175 cmd_send(CMD_ACK
,1,pckSize
,pckNum
,trace
+ traceLen
- pckLen
,pckSize
);
176 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
184 // UsbCommand ack = {CMD_ACK, {2, 0, 0}};
187 cmd_send(CMD_ACK
,2,0,0,0,0);
188 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
192 memset(trace
, 0x44, TRACE_SIZE
);