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