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