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