]>
Commit | Line | Data |
---|---|---|
b62a5a84 | 1 | // Merlok, 2011, 2012\r |
f397b5cc M |
2 | // people from mifare@nethemba.com, 2010\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 | |
55acbb2a | 8 | // mifare commands\r |
f397b5cc M |
9 | //-----------------------------------------------------------------------------\r |
10 | \r | |
11 | #include <stdio.h>\r | |
873014de | 12 | #include <stdlib.h> \r |
31b6e9af | 13 | #include <string.h>\r |
9492e0b0 | 14 | #include <pthread.h>\r |
f397b5cc | 15 | #include "mifarehost.h"\r |
902cb3c0 | 16 | #include "proxmark3.h"\r |
f397b5cc | 17 | \r |
55acbb2a | 18 | // MIFARE\r |
f397b5cc | 19 | int compar_int(const void * a, const void * b) {\r |
9492e0b0 | 20 | // didn't work: (the result is truncated to 32 bits)\r |
21 | //return (*(uint64_t*)b - *(uint64_t*)a);\r | |
22 | \r | |
23 | // better:\r | |
24 | if (*(uint64_t*)b == *(uint64_t*)a) return 0;\r | |
25 | else if (*(uint64_t*)b > *(uint64_t*)a) return 1;\r | |
26 | else return -1;\r | |
f397b5cc M |
27 | }\r |
28 | \r | |
9492e0b0 | 29 | // Compare 16 Bits out of cryptostate\r |
30 | int Compare16Bits(const void * a, const void * b) {\r | |
31 | if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r | |
32 | else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r | |
33 | else return -1;\r | |
f397b5cc M |
34 | }\r |
35 | \r | |
9492e0b0 | 36 | typedef \r |
37 | struct {\r | |
38 | union {\r | |
39 | struct Crypto1State *slhead;\r | |
40 | uint64_t *keyhead;\r | |
3f7c993d | 41 | } head;\r |
9492e0b0 | 42 | union {\r |
43 | struct Crypto1State *sltail;\r | |
44 | uint64_t *keytail;\r | |
3f7c993d | 45 | } tail;\r |
9492e0b0 | 46 | uint32_t len;\r |
47 | uint32_t uid;\r | |
48 | uint32_t blockNo;\r | |
49 | uint32_t keyType;\r | |
50 | uint32_t nt;\r | |
51 | uint32_t ks1;\r | |
52 | } StateList_t;\r | |
53 | \r | |
54 | \r | |
55 | // wrapper function for multi-threaded lfsr_recovery32\r | |
56 | void* nested_worker_thread(void *arg)\r | |
57 | {\r | |
58 | struct Crypto1State *p1;\r | |
59 | StateList_t *statelist = arg;\r | |
60 | \r | |
3f7c993d | 61 | statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r |
62 | for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r | |
63 | statelist->len = p1 - statelist->head.slhead;\r | |
64 | statelist->tail.sltail = --p1;\r | |
65 | qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r | |
f397b5cc | 66 | \r |
3f7c993d | 67 | return statelist->head.slhead;\r |
f397b5cc M |
68 | }\r |
69 | \r | |
9492e0b0 | 70 | int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t * resultKey, bool calibrate) \r |
f397b5cc | 71 | {\r |
dc8ba239 | 72 | uint16_t i;\r |
f397b5cc | 73 | uint32_t uid;\r |
902cb3c0 | 74 | UsbCommand resp;\r |
f397b5cc | 75 | \r |
9492e0b0 | 76 | StateList_t statelists[2];\r |
77 | struct Crypto1State *p1, *p2, *p3, *p4;\r | |
78 | \r | |
f397b5cc | 79 | // flush queue\r |
dc8ba239 | 80 | WaitForResponseTimeout(CMD_ACK, NULL, 100);\r |
f397b5cc | 81 | \r |
9492e0b0 | 82 | UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r |
f397b5cc | 83 | memcpy(c.d.asBytes, key, 6);\r |
9492e0b0 | 84 | SendCommand(&c);\r |
f397b5cc | 85 | \r |
dc8ba239 | 86 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r |
87 | return -1;\r | |
88 | }\r | |
f397b5cc | 89 | \r |
dc8ba239 | 90 | if (resp.arg[0]) {\r |
91 | return resp.arg[0]; // error during nested\r | |
92 | }\r | |
93 | \r | |
94 | memcpy(&uid, resp.d.asBytes, 4);\r | |
95 | PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);\r | |
96 | \r | |
97 | for (i = 0; i < 2; i++) {\r | |
98 | statelists[i].blockNo = resp.arg[2] & 0xff;\r | |
99 | statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r | |
100 | statelists[i].uid = uid;\r | |
101 | memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r | |
102 | memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r | |
f397b5cc M |
103 | }\r |
104 | \r | |
f397b5cc | 105 | // calc keys\r |
f397b5cc | 106 | \r |
9492e0b0 | 107 | pthread_t thread_id[2];\r |
108 | \r | |
109 | // create and run worker threads\r | |
110 | for (i = 0; i < 2; i++) {\r | |
111 | pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r | |
f397b5cc M |
112 | }\r |
113 | \r | |
9492e0b0 | 114 | // wait for threads to terminate:\r |
115 | for (i = 0; i < 2; i++) {\r | |
3f7c993d | 116 | pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r |
f397b5cc M |
117 | }\r |
118 | \r | |
f397b5cc | 119 | \r |
9492e0b0 | 120 | // the first 16 Bits of the cryptostate already contain part of our key.\r |
121 | // Create the intersection of the two lists based on these 16 Bits and\r | |
122 | // roll back the cryptostate\r | |
3f7c993d | 123 | p1 = p3 = statelists[0].head.slhead; \r |
124 | p2 = p4 = statelists[1].head.slhead;\r | |
125 | while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r | |
9492e0b0 | 126 | if (Compare16Bits(p1, p2) == 0) {\r |
127 | struct Crypto1State savestate, *savep = &savestate;\r | |
128 | savestate = *p1;\r | |
3f7c993d | 129 | while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r |
9492e0b0 | 130 | *p3 = *p1;\r |
131 | lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r | |
132 | p3++;\r | |
133 | p1++;\r | |
134 | }\r | |
135 | savestate = *p2;\r | |
3f7c993d | 136 | while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r |
9492e0b0 | 137 | *p4 = *p2;\r |
138 | lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r | |
139 | p4++;\r | |
140 | p2++;\r | |
141 | }\r | |
142 | }\r | |
143 | else {\r | |
144 | while (Compare16Bits(p1, p2) == -1) p1++;\r | |
145 | while (Compare16Bits(p1, p2) == 1) p2++;\r | |
146 | }\r | |
f397b5cc | 147 | }\r |
9492e0b0 | 148 | p3->even = 0; p3->odd = 0;\r |
149 | p4->even = 0; p4->odd = 0;\r | |
3f7c993d | 150 | statelists[0].len = p3 - statelists[0].head.slhead;\r |
151 | statelists[1].len = p4 - statelists[1].head.slhead;\r | |
152 | statelists[0].tail.sltail=--p3;\r | |
153 | statelists[1].tail.sltail=--p4;\r | |
9492e0b0 | 154 | \r |
155 | // the statelists now contain possible keys. The key we are searching for must be in the\r | |
156 | // intersection of both lists. Create the intersection:\r | |
3f7c993d | 157 | qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compar_int);\r |
158 | qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compar_int);\r | |
9492e0b0 | 159 | \r |
160 | uint64_t *p5, *p6, *p7;\r | |
3f7c993d | 161 | p5 = p7 = statelists[0].head.keyhead; \r |
162 | p6 = statelists[1].head.keyhead;\r | |
163 | while (p5 <= statelists[0].tail.keytail && p6 <= statelists[1].tail.keytail) {\r | |
9492e0b0 | 164 | if (compar_int(p5, p6) == 0) {\r |
165 | *p7++ = *p5++;\r | |
166 | p6++;\r | |
167 | }\r | |
168 | else {\r | |
169 | while (compar_int(p5, p6) == -1) p5++;\r | |
170 | while (compar_int(p5, p6) == 1) p6++;\r | |
171 | }\r | |
172 | }\r | |
3f7c993d | 173 | statelists[0].len = p7 - statelists[0].head.keyhead;\r |
174 | statelists[0].tail.keytail=--p7;\r | |
9492e0b0 | 175 | \r |
176 | memset(resultKey, 0, 6);\r | |
177 | // The list may still contain several key candidates. Test each of them with mfCheckKeys\r | |
178 | for (i = 0; i < statelists[0].len; i++) {\r | |
179 | uint8_t keyBlock[6];\r | |
180 | uint64_t key64;\r | |
3f7c993d | 181 | crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r |
9492e0b0 | 182 | num_to_bytes(key64, 6, keyBlock);\r |
183 | key64 = 0;\r | |
5330f532 | 184 | if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r |
9492e0b0 | 185 | num_to_bytes(key64, 6, resultKey);\r |
186 | break;\r | |
187 | }\r | |
188 | }\r | |
189 | \r | |
3f7c993d | 190 | free(statelists[0].head.slhead);\r |
191 | free(statelists[1].head.slhead);\r | |
9492e0b0 | 192 | \r |
f397b5cc M |
193 | return 0;\r |
194 | }\r | |
195 | \r | |
5330f532 | 196 | int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r |
9492e0b0 | 197 | \r |
f397b5cc M |
198 | *key = 0;\r |
199 | \r | |
5330f532 | 200 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r |
f397b5cc | 201 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r |
9492e0b0 | 202 | SendCommand(&c);\r |
f397b5cc | 203 | \r |
902cb3c0 | 204 | UsbCommand resp;\r |
205 | if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r | |
206 | if ((resp.arg[0] & 0xff) != 0x01) return 2;\r | |
207 | *key = bytes_to_num(resp.d.asBytes, 6);\r | |
f397b5cc M |
208 | return 0;\r |
209 | }\r | |
8556b852 | 210 | \r |
55acbb2a M |
211 | // EMULATOR\r |
212 | \r | |
8556b852 | 213 | int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r |
545a1f38 | 214 | UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r |
902cb3c0 | 215 | SendCommand(&c);\r |
8556b852 | 216 | \r |
902cb3c0 | 217 | UsbCommand resp;\r |
218 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r | |
219 | memcpy(data, resp.d.asBytes, blocksCount * 16);\r | |
8556b852 M |
220 | return 0;\r |
221 | }\r | |
222 | \r | |
223 | int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
545a1f38 | 224 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};\r |
8556b852 | 225 | memcpy(c.d.asBytes, data, blocksCount * 16); \r |
545a1f38 | 226 | SendCommand(&c);\r |
8556b852 M |
227 | return 0;\r |
228 | }\r | |
229 | \r | |
55acbb2a M |
230 | // "MAGIC" CARD\r |
231 | \r | |
3bba7dea | 232 | int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe) {\r |
80b1b53f | 233 | uint8_t oldblock0[16] = {0x00};\r |
3fe4ff4f | 234 | uint8_t block0[16] = {0x00};\r |
3bba7dea | 235 | \r |
80b1b53f | 236 | int old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER);\r |
3bba7dea JH |
237 | if (old == 0) {\r |
238 | memcpy(block0, oldblock0, 16);\r | |
239 | PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r | |
80b1b53f | 240 | } else {\r |
3bba7dea JH |
241 | PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r |
242 | }\r | |
243 | \r | |
244 | // fill in the new values\r | |
245 | // UID\r | |
246 | memcpy(block0, uid, 4); \r | |
247 | // Mifare UID BCC\r | |
248 | block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\r | |
249 | // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r | |
250 | if (sak!=NULL)\r | |
251 | block0[5]=sak[0];\r | |
252 | if (atqa!=NULL) {\r | |
253 | block0[6]=atqa[1];\r | |
254 | block0[7]=atqa[0];\r | |
80b1b53f | 255 | }\r |
3bba7dea | 256 | PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r |
208a0166 | 257 | return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);\r |
f774db95 M |
258 | }\r |
259 | \r | |
6a1f2d82 | 260 | int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {\r |
0675f200 | 261 | \r |
3fe4ff4f | 262 | uint8_t isOK = 0;\r |
263 | UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};\r | |
f774db95 | 264 | memcpy(c.d.asBytes, data, 16); \r |
545a1f38 | 265 | SendCommand(&c);\r |
0675f200 | 266 | \r |
902cb3c0 | 267 | UsbCommand resp;\r |
268 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
269 | isOK = resp.arg[0] & 0xff;\r | |
80b1b53f | 270 | if (uid != NULL) \r |
271 | memcpy(uid, resp.d.asBytes, 4);\r | |
272 | if (!isOK) \r | |
273 | return 2;\r | |
0675f200 M |
274 | } else {\r |
275 | PrintAndLog("Command execute timeout");\r | |
276 | return 1;\r | |
277 | }\r | |
278 | return 0;\r | |
279 | }\r | |
545a1f38 M |
280 | \r |
281 | int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r | |
282 | uint8_t isOK = 0;\r | |
283 | \r | |
3fe4ff4f | 284 | UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r |
545a1f38 M |
285 | SendCommand(&c);\r |
286 | \r | |
902cb3c0 | 287 | UsbCommand resp;\r |
288 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
289 | isOK = resp.arg[0] & 0xff;\r | |
290 | memcpy(data, resp.d.asBytes, 16);\r | |
545a1f38 M |
291 | if (!isOK) return 2;\r |
292 | } else {\r | |
293 | PrintAndLog("Command execute timeout");\r | |
294 | return 1;\r | |
295 | }\r | |
296 | return 0;\r | |
297 | }\r | |
55acbb2a M |
298 | \r |
299 | // SNIFFER\r | |
300 | \r | |
e0c635d1 M |
301 | // constants\r |
302 | static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r | |
303 | \r | |
55acbb2a | 304 | // variables\r |
e4691591 | 305 | char logHexFileName[FILE_PATH_SIZE] = {0x00};\r |
e0c635d1 | 306 | static uint8_t traceCard[4096] = {0x00};\r |
e4691591 | 307 | static char traceFileName[FILE_PATH_SIZE] = {0x00};\r |
55acbb2a M |
308 | static int traceState = TRACE_IDLE;\r |
309 | static uint8_t traceCurBlock = 0;\r | |
310 | static uint8_t traceCurKey = 0;\r | |
311 | \r | |
312 | struct Crypto1State *traceCrypto1 = NULL;\r | |
313 | \r | |
314 | struct Crypto1State *revstate;\r | |
315 | uint64_t lfsr;\r | |
316 | uint32_t ks2;\r | |
317 | uint32_t ks3;\r | |
318 | \r | |
319 | uint32_t uid; // serial number\r | |
320 | uint32_t nt; // tag challenge\r | |
321 | uint32_t nr_enc; // encrypted reader challenge\r | |
322 | uint32_t ar_enc; // encrypted reader response\r | |
323 | uint32_t at_enc; // encrypted tag response\r | |
324 | \r | |
e0c635d1 M |
325 | int isTraceCardEmpty(void) {\r |
326 | return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r | |
327 | }\r | |
55acbb2a | 328 | \r |
e0c635d1 M |
329 | int isBlockEmpty(int blockN) {\r |
330 | for (int i = 0; i < 16; i++) \r | |
331 | if (traceCard[blockN * 16 + i] != 0) return 0;\r | |
55acbb2a | 332 | \r |
e0c635d1 M |
333 | return 1;\r |
334 | }\r | |
335 | \r | |
336 | int isBlockTrailer(int blockN) {\r | |
337 | return ((blockN & 0x03) == 0x03);\r | |
338 | }\r | |
339 | \r | |
340 | int loadTraceCard(uint8_t *tuid) {\r | |
341 | FILE * f;\r | |
80b1b53f | 342 | char buf[64] = {0x00};\r |
343 | uint8_t buf8[64] = {0x00};\r | |
e0c635d1 M |
344 | int i, blockNum;\r |
345 | \r | |
80b1b53f | 346 | if (!isTraceCardEmpty()) \r |
347 | saveTraceCard();\r | |
348 | \r | |
55acbb2a M |
349 | memset(traceCard, 0x00, 4096);\r |
350 | memcpy(traceCard, tuid + 3, 4);\r | |
80b1b53f | 351 | \r |
e0c635d1 M |
352 | FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r |
353 | \r | |
354 | f = fopen(traceFileName, "r");\r | |
e6432f05 | 355 | if (!f) return 1;\r |
e0c635d1 M |
356 | \r |
357 | blockNum = 0;\r | |
e4691591 | 358 | \r |
e0c635d1 | 359 | while(!feof(f)){\r |
e4691591 | 360 | \r |
e0c635d1 | 361 | memset(buf, 0, sizeof(buf));\r |
759c16b3 | 362 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
ca4714cd MHS |
363 | PrintAndLog("File reading error.");\r |
364 | fclose(f);\r | |
759c16b3 | 365 | return 2;\r |
ca4714cd | 366 | }\r |
e0c635d1 M |
367 | \r |
368 | if (strlen(buf) < 32){\r | |
369 | if (feof(f)) break;\r | |
370 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
ca4714cd | 371 | fclose(f);\r |
e0c635d1 M |
372 | return 2;\r |
373 | }\r | |
374 | for (i = 0; i < 32; i += 2)\r | |
375 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
376 | \r | |
377 | memcpy(traceCard + blockNum * 16, buf8, 16);\r | |
378 | \r | |
379 | blockNum++;\r | |
380 | }\r | |
381 | fclose(f);\r | |
382 | \r | |
383 | return 0;\r | |
384 | }\r | |
385 | \r | |
386 | int saveTraceCard(void) {\r | |
387 | FILE * f;\r | |
388 | \r | |
389 | if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r | |
390 | \r | |
391 | f = fopen(traceFileName, "w+");\r | |
e6432f05 | 392 | if ( !f ) return 1;\r |
e4691591 | 393 | \r |
e0c635d1 M |
394 | for (int i = 0; i < 64; i++) { // blocks\r |
395 | for (int j = 0; j < 16; j++) // bytes\r | |
396 | fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r | |
397 | fprintf(f,"\n");\r | |
398 | }\r | |
399 | fclose(f);\r | |
e0c635d1 M |
400 | return 0;\r |
401 | }\r | |
402 | \r | |
403 | int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r | |
404 | \r | |
80b1b53f | 405 | if (traceCrypto1) \r |
406 | crypto1_destroy(traceCrypto1);\r | |
407 | \r | |
e0c635d1 M |
408 | traceCrypto1 = NULL;\r |
409 | \r | |
80b1b53f | 410 | if (wantSaveToEmlFile) \r |
411 | loadTraceCard(tuid);\r | |
412 | \r | |
55acbb2a M |
413 | traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r |
414 | traceCard[5] = sak;\r | |
415 | memcpy(&traceCard[6], atqa, 2);\r | |
416 | traceCurBlock = 0;\r | |
417 | uid = bytes_to_num(tuid + 3, 4);\r | |
418 | \r | |
419 | traceState = TRACE_IDLE;\r | |
420 | \r | |
421 | return 0;\r | |
422 | }\r | |
423 | \r | |
424 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r | |
425 | uint8_t bt = 0;\r | |
426 | int i;\r | |
427 | \r | |
428 | if (len != 1) {\r | |
429 | for (i = 0; i < len; i++)\r | |
430 | data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r | |
431 | } else {\r | |
432 | bt = 0;\r | |
433 | for (i = 0; i < 4; i++)\r | |
434 | bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r | |
435 | \r | |
436 | data[0] = bt;\r | |
437 | }\r | |
438 | return;\r | |
439 | }\r | |
440 | \r | |
441 | \r | |
6a1f2d82 | 442 | int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r |
55acbb2a M |
443 | uint8_t data[64];\r |
444 | \r | |
445 | if (traceState == TRACE_ERROR) return 1;\r | |
446 | if (len > 64) {\r | |
447 | traceState = TRACE_ERROR;\r | |
448 | return 1;\r | |
449 | }\r | |
450 | \r | |
451 | memcpy(data, data_src, len);\r | |
452 | if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r | |
453 | mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r | |
454 | PrintAndLog("dec> %s", sprint_hex(data, len));\r | |
455 | AddLogHex(logHexFileName, "dec> ", data, len); \r | |
456 | }\r | |
457 | \r | |
458 | switch (traceState) {\r | |
459 | case TRACE_IDLE: \r | |
8b6a93df M |
460 | // check packet crc16!\r |
461 | if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r | |
462 | PrintAndLog("dec> CRC ERROR!!!");\r | |
463 | AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r | |
464 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
465 | return 1;\r | |
466 | }\r | |
55acbb2a M |
467 | \r |
468 | // AUTHENTICATION\r | |
469 | if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r | |
470 | traceState = TRACE_AUTH1;\r | |
471 | traceCurBlock = data[1];\r | |
472 | traceCurKey = data[0] == 60 ? 1:0;\r | |
473 | return 0;\r | |
474 | }\r | |
475 | \r | |
476 | // READ\r | |
477 | if ((len ==4) && ((data[0] == 0x30))) {\r | |
478 | traceState = TRACE_READ_DATA;\r | |
479 | traceCurBlock = data[1];\r | |
480 | return 0;\r | |
481 | }\r | |
482 | \r | |
483 | // WRITE\r | |
484 | if ((len ==4) && ((data[0] == 0xA0))) {\r | |
485 | traceState = TRACE_WRITE_OK;\r | |
486 | traceCurBlock = data[1];\r | |
487 | return 0;\r | |
488 | }\r | |
489 | \r | |
490 | // HALT\r | |
491 | if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r | |
492 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
493 | return 0;\r | |
494 | }\r | |
495 | \r | |
496 | return 0;\r | |
497 | break;\r | |
498 | \r | |
499 | case TRACE_READ_DATA: \r | |
500 | if (len == 18) {\r | |
501 | traceState = TRACE_IDLE;\r | |
502 | \r | |
e0c635d1 M |
503 | if (isBlockTrailer(traceCurBlock)) {\r |
504 | memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r | |
505 | } else {\r | |
506 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
507 | }\r | |
508 | if (wantSaveToEmlFile) saveTraceCard();\r | |
55acbb2a M |
509 | return 0;\r |
510 | } else {\r | |
511 | traceState = TRACE_ERROR;\r | |
512 | return 1;\r | |
513 | }\r | |
514 | break;\r | |
515 | \r | |
516 | case TRACE_WRITE_OK: \r | |
97d582a6 | 517 | if ((len == 1) && (data[0] == 0x0a)) {\r |
55acbb2a M |
518 | traceState = TRACE_WRITE_DATA;\r |
519 | \r | |
520 | return 0;\r | |
521 | } else {\r | |
522 | traceState = TRACE_ERROR;\r | |
523 | return 1;\r | |
524 | }\r | |
525 | break;\r | |
526 | \r | |
527 | case TRACE_WRITE_DATA: \r | |
528 | if (len == 18) {\r | |
529 | traceState = TRACE_IDLE;\r | |
530 | \r | |
531 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
e0c635d1 | 532 | if (wantSaveToEmlFile) saveTraceCard();\r |
55acbb2a M |
533 | return 0;\r |
534 | } else {\r | |
535 | traceState = TRACE_ERROR;\r | |
536 | return 1;\r | |
537 | }\r | |
538 | break;\r | |
539 | \r | |
540 | case TRACE_AUTH1: \r | |
541 | if (len == 4) {\r | |
542 | traceState = TRACE_AUTH2;\r | |
55acbb2a M |
543 | nt = bytes_to_num(data, 4);\r |
544 | return 0;\r | |
545 | } else {\r | |
546 | traceState = TRACE_ERROR;\r | |
547 | return 1;\r | |
548 | }\r | |
549 | break;\r | |
550 | \r | |
551 | case TRACE_AUTH2: \r | |
552 | if (len == 8) {\r | |
553 | traceState = TRACE_AUTH_OK;\r | |
554 | \r | |
555 | nr_enc = bytes_to_num(data, 4);\r | |
556 | ar_enc = bytes_to_num(data + 4, 4);\r | |
557 | return 0;\r | |
558 | } else {\r | |
559 | traceState = TRACE_ERROR;\r | |
560 | return 1;\r | |
561 | }\r | |
562 | break;\r | |
563 | \r | |
564 | case TRACE_AUTH_OK: \r | |
565 | if (len ==4) {\r | |
566 | traceState = TRACE_IDLE;\r | |
567 | \r | |
568 | at_enc = bytes_to_num(data, 4);\r | |
569 | \r | |
570 | // decode key here)\r | |
97d582a6 MHS |
571 | ks2 = ar_enc ^ prng_successor(nt, 64);\r |
572 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
573 | revstate = lfsr_recovery64(ks2, ks3);\r | |
574 | lfsr_rollback_word(revstate, 0, 0);\r | |
575 | lfsr_rollback_word(revstate, 0, 0);\r | |
576 | lfsr_rollback_word(revstate, nr_enc, 1);\r | |
577 | lfsr_rollback_word(revstate, uid ^ nt, 0);\r | |
3fe4ff4f | 578 | \r |
55acbb2a M |
579 | crypto1_get_lfsr(revstate, &lfsr);\r |
580 | printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r | |
581 | AddLogUint64(logHexFileName, "key> ", lfsr); \r | |
582 | \r | |
e0c635d1 M |
583 | int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r |
584 | if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r | |
585 | \r | |
55acbb2a | 586 | if (traceCurKey) {\r |
e0c635d1 | 587 | num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r |
55acbb2a | 588 | } else {\r |
e0c635d1 | 589 | num_to_bytes(lfsr, 6, traceCard + blockShift);\r |
55acbb2a | 590 | }\r |
e0c635d1 | 591 | if (wantSaveToEmlFile) saveTraceCard();\r |
55acbb2a M |
592 | \r |
593 | if (traceCrypto1) {\r | |
594 | crypto1_destroy(traceCrypto1);\r | |
595 | }\r | |
596 | \r | |
597 | // set cryptosystem state\r | |
598 | traceCrypto1 = lfsr_recovery64(ks2, ks3);\r | |
599 | \r | |
600 | // nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r | |
601 | \r | |
602 | /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r | |
603 | crypto1_word(traceCrypto1, nt ^ uid, 0);\r | |
604 | crypto1_word(traceCrypto1, ar, 1);\r | |
605 | crypto1_word(traceCrypto1, 0, 0);\r | |
606 | crypto1_word(traceCrypto1, 0, 0);*/\r | |
607 | \r | |
608 | return 0;\r | |
609 | } else {\r | |
610 | traceState = TRACE_ERROR;\r | |
611 | return 1;\r | |
612 | }\r | |
613 | break;\r | |
614 | \r | |
615 | default: \r | |
616 | traceState = TRACE_ERROR;\r | |
617 | return 1;\r | |
618 | }\r | |
619 | \r | |
620 | return 0;\r | |
621 | }\r |