]>
Commit | Line | Data |
---|---|---|
b62a5a84 M |
1 | //-----------------------------------------------------------------------------\r |
2 | // Merlok - 2012\r | |
3 | //\r | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
6 | // the license.\r | |
7 | //-----------------------------------------------------------------------------\r | |
8 | // Routines to support mifare classic sniffer.\r | |
9 | //-----------------------------------------------------------------------------\r | |
10 | \r | |
11 | #include "mifaresniff.h"\r | |
12 | #include "apps.h"\r | |
13 | \r | |
39864b0b | 14 | static int sniffState = SNF_INIT;\r |
13fc2e9c | 15 | static uint8_t sniffUIDType = 0;\r |
16 | static uint8_t sniffUID[10] = {0,0,0,0,0,0,0,0,0,0};\r | |
17 | static uint8_t sniffATQA[2] = {0,0};\r | |
18 | static uint8_t sniffSAK = 0;\r | |
19 | static uint8_t sniffBuf[19];\r | |
20 | static uint32_t timerData = 0;\r | |
21 | \r | |
22 | \r | |
23 | void MfSniffInit(void){\r | |
24 | memset(sniffUID, 0x00, sizeof(sniffUID));\r | |
25 | memset(sniffATQA, 0x00, sizeof(sniffATQA));\r | |
26 | memset(sniffBuf, 0x00, sizeof(sniffBuf));\r | |
39864b0b M |
27 | sniffSAK = 0;\r |
28 | sniffUIDType = SNF_UID_4;\r | |
39864b0b M |
29 | }\r |
30 | \r | |
13fc2e9c | 31 | void MfSniffEnd(void){\r |
39864b0b | 32 | LED_B_ON();\r |
7bc95e2e | 33 | cmd_send(CMD_ACK,0,0,0,0,0);\r |
39864b0b | 34 | LED_B_OFF();\r |
39864b0b M |
35 | }\r |
36 | \r | |
6a1f2d82 | 37 | bool RAMFUNC MfSniffLogic(const uint8_t *data, uint16_t len, uint8_t *parity, uint16_t bitCnt, bool reader) {\r |
39864b0b | 38 | \r |
13fc2e9c | 39 | // reset on 7-Bit commands from reader\r |
40 | if (reader && (len == 1) && (bitCnt == 7)) { \r | |
39864b0b M |
41 | sniffState = SNF_INIT;\r |
42 | }\r | |
43 | \r | |
44 | switch (sniffState) {\r | |
45 | case SNF_INIT:{\r | |
13fc2e9c | 46 | // REQA or WUPA from reader\r |
47 | if ((len == 1) && (reader) && (bitCnt == 7) ) {\r | |
48 | MfSniffInit();\r | |
39864b0b M |
49 | sniffState = SNF_WUPREQ;\r |
50 | }\r | |
51 | break;\r | |
52 | }\r | |
53 | case SNF_WUPREQ:{\r | |
13fc2e9c | 54 | // ATQA from tag\r |
55 | if ((!reader) && (len == 2)) {\r | |
56 | sniffATQA[0] = data[0];\r | |
57 | sniffATQA[1] = data[1];\r | |
39864b0b M |
58 | sniffState = SNF_ATQA;\r |
59 | }\r | |
60 | break;\r | |
61 | }\r | |
62 | case SNF_ATQA:{\r | |
13fc2e9c | 63 | // Select ALL from reader\r |
64 | if ((reader) && (len == 2) && (data[0] == 0x93) && (data[1] == 0x20)) {\r | |
39864b0b M |
65 | sniffState = SNF_ANTICOL1;\r |
66 | }\r | |
67 | break;\r | |
68 | }\r | |
69 | case SNF_ANTICOL1:{\r | |
13fc2e9c | 70 | // UID from tag (CL1) \r |
71 | if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {\r | |
39864b0b | 72 | memcpy(sniffUID + 3, data, 4);\r |
39864b0b M |
73 | sniffState = SNF_UID1;\r |
74 | }\r | |
75 | break;\r | |
76 | }\r | |
77 | case SNF_UID1:{\r | |
13fc2e9c | 78 | // Select 4 Byte UID from reader\r |
79 | if ((reader) && (len == 9) && (data[0] == 0x93) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) {\r | |
39864b0b M |
80 | sniffState = SNF_SAK;\r |
81 | }\r | |
82 | break;\r | |
83 | }\r | |
84 | case SNF_SAK:{\r | |
7bc95e2e | 85 | if ((!reader) && (len == 3) && (CheckCrc14443(CRC_14443_A, data, 3))) { // SAK from card?\r |
39864b0b | 86 | sniffSAK = data[0];\r |
7bc95e2e | 87 | if (sniffUID[3] == 0x88) { // CL2 UID part to be expected\r |
39864b0b | 88 | sniffState = SNF_ANTICOL2;\r |
7bc95e2e | 89 | } else { // select completed\r |
39864b0b M |
90 | sniffState = SNF_CARD_IDLE;\r |
91 | }\r | |
92 | }\r | |
93 | break;\r | |
94 | }\r | |
95 | case SNF_ANTICOL2:{\r | |
13fc2e9c | 96 | // CL2 UID \r |
97 | if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {\r | |
d714d3ef | 98 | memcpy(sniffUID, sniffUID+4, 3);\r |
99 | memcpy(sniffUID+3, data, 4);\r | |
39864b0b | 100 | sniffUIDType = SNF_UID_7;\r |
39864b0b | 101 | sniffState = SNF_UID2;\r |
7bc95e2e | 102 | }\r |
39864b0b M |
103 | break;\r |
104 | }\r | |
105 | case SNF_UID2:{\r | |
13fc2e9c | 106 | // Select 2nd part of 7 Byte UID\r |
107 | if ((reader) && (len == 9) && (data[0] == 0x95) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) {\r | |
108 | sniffState = SNF_SAK;\r | |
109 | }\r | |
110 | break;\r | |
111 | }\r | |
112 | case SNF_ANTICOL3:{\r | |
113 | // CL3 UID \r | |
114 | if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { \r | |
115 | memcpy(sniffUID, sniffUID+4, 3);\r | |
116 | memcpy(sniffUID+3, data, 4);\r | |
117 | sniffUIDType = SNF_UID_10;\r | |
118 | sniffState = SNF_UID3;\r | |
119 | }\r | |
120 | break;\r | |
121 | }\r | |
122 | case SNF_UID3:{\r | |
123 | // Select 3nd part of 10 Byte UID\r | |
124 | if ((reader) && (len == 9) && (data[0] == 0x97) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) {\r | |
39864b0b | 125 | sniffState = SNF_SAK;\r |
39864b0b M |
126 | }\r |
127 | break;\r | |
128 | }\r | |
7bc95e2e | 129 | case SNF_CARD_IDLE:{ // trace the card select sequence\r |
39864b0b M |
130 | sniffBuf[0] = 0xFF;\r |
131 | sniffBuf[1] = 0xFF;\r | |
13fc2e9c | 132 | memcpy(sniffBuf + 2, sniffUID, sizeof(sniffUID));\r |
133 | memcpy(sniffBuf + 9, sniffATQA, sizeof(sniffATQA));\r | |
134 | sniffBuf[14] = sniffSAK;\r | |
135 | sniffBuf[15] = 0xFF;\r | |
136 | sniffBuf[16] = 0xFF;\r | |
137 | LogTrace(sniffBuf, sizeof(sniffBuf), 0, 0, NULL, TRUE);\r | |
7bc95e2e | 138 | } // intentionally no break;\r |
139 | case SNF_CARD_CMD:{ \r | |
6a1f2d82 | 140 | LogTrace(data, len, 0, 0, NULL, TRUE);\r |
39864b0b M |
141 | sniffState = SNF_CARD_RESP;\r |
142 | timerData = GetTickCount();\r | |
143 | break;\r | |
144 | }\r | |
145 | case SNF_CARD_RESP:{\r | |
6a1f2d82 | 146 | LogTrace(data, len, 0, 0, NULL, FALSE);\r |
39864b0b M |
147 | sniffState = SNF_CARD_CMD;\r |
148 | timerData = GetTickCount();\r | |
149 | break;\r | |
150 | }\r | |
39864b0b M |
151 | default:\r |
152 | sniffState = SNF_INIT;\r | |
153 | break;\r | |
154 | }\r | |
7bc95e2e | 155 | return FALSE;\r |
39864b0b M |
156 | }\r |
157 | \r | |
7bc95e2e | 158 | bool RAMFUNC MfSniffSend(uint16_t maxTimeoutMs) {\r |
3000dc4e | 159 | if (BigBuf_get_traceLen() && (GetTickCount() > timerData + maxTimeoutMs)) {\r |
39864b0b M |
160 | return intMfSniffSend();\r |
161 | }\r | |
7bc95e2e | 162 | return FALSE;\r |
39864b0b M |
163 | }\r |
164 | \r | |
7bc95e2e | 165 | // internal sending function. not a RAMFUNC.\r |
166 | bool intMfSniffSend() {\r | |
167 | \r | |
39864b0b | 168 | int pckSize = 0;\r |
3000dc4e | 169 | int pckLen = BigBuf_get_traceLen();\r |
39864b0b | 170 | int pckNum = 0;\r |
117d9ec2 | 171 | uint8_t *trace = BigBuf_get_addr();\r |
172 | \r | |
55acbb2a | 173 | FpgaDisableSscDma();\r |
39864b0b | 174 | while (pckLen > 0) {\r |
7bc95e2e | 175 | pckSize = MIN(USB_CMD_DATA_SIZE, pckLen);\r |
39864b0b | 176 | LED_B_ON();\r |
3000dc4e | 177 | cmd_send(CMD_ACK, 1, BigBuf_get_traceLen(), pckSize, trace + BigBuf_get_traceLen() - pckLen, pckSize);\r |
39864b0b | 178 | LED_B_OFF();\r |
39864b0b M |
179 | pckLen -= pckSize;\r |
180 | pckNum++;\r | |
181 | }\r | |
182 | \r | |
39864b0b | 183 | LED_B_ON();\r |
7bc95e2e | 184 | cmd_send(CMD_ACK,2,0,0,0,0);\r |
39864b0b M |
185 | LED_B_OFF();\r |
186 | \r | |
3000dc4e | 187 | clear_trace();\r |
7bc95e2e | 188 | return TRUE;\r |
39864b0b | 189 | }\r |