]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/mifarehost.c
split lf indala[cmd] to lf indala [cmd]
[proxmark3-svn] / client / mifarehost.c
... / ...
CommitLineData
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\r
16#include "crapto1/crapto1.h"\r
17#include "proxmark3.h"\r
18#include "usb_cmd.h"\r
19#include "cmdmain.h"\r
20#include "ui.h"\r
21#include "util.h"\r
22#include "iso14443crc.h"\r
23#include "mifarehost.h"\r
24\r
25// mifare tracer flags used in mfTraceDecode()\r
26#define TRACE_IDLE 0x00\r
27#define TRACE_AUTH1 0x01\r
28#define TRACE_AUTH2 0x02\r
29#define TRACE_AUTH_OK 0x03\r
30#define TRACE_READ_DATA 0x04\r
31#define TRACE_WRITE_OK 0x05\r
32#define TRACE_WRITE_DATA 0x06\r
33#define TRACE_ERROR 0xFF\r
34\r
35\r
36static int compare_uint64(const void *a, const void *b) {\r
37 // didn't work: (the result is truncated to 32 bits)\r
38 //return (*(int64_t*)b - *(int64_t*)a);\r
39\r
40 // better:\r
41 if (*(uint64_t*)b == *(uint64_t*)a) return 0;\r
42 else if (*(uint64_t*)b < *(uint64_t*)a) return 1;\r
43 else return -1;\r
44}\r
45\r
46\r
47// create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned.\r
48static uint32_t intersection(uint64_t *list1, uint64_t *list2)\r
49{\r
50 if (list1 == NULL || list2 == NULL) {\r
51 return 0;\r
52 }\r
53 uint64_t *p1, *p2, *p3;\r
54 p1 = p3 = list1; \r
55 p2 = list2;\r
56\r
57 while ( *p1 != -1 && *p2 != -1 ) {\r
58 if (compare_uint64(p1, p2) == 0) {\r
59 *p3++ = *p1++;\r
60 p2++;\r
61 }\r
62 else {\r
63 while (compare_uint64(p1, p2) < 0) ++p1;\r
64 while (compare_uint64(p1, p2) > 0) ++p2;\r
65 }\r
66 }\r
67 *p3 = -1;\r
68 return p3 - list1;\r
69}\r
70\r
71\r
72// Darkside attack (hf mf mifare)\r
73static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r
74 struct Crypto1State *states;\r
75 uint32_t i, pos, rr; //nr_diff;\r
76 uint8_t bt, ks3x[8], par[8][8];\r
77 uint64_t key_recovered;\r
78 static uint64_t *keylist;\r
79 rr = 0;\r
80\r
81 // Reset the last three significant bits of the reader nonce\r
82 nr &= 0xffffff1f;\r
83\r
84 for (pos=0; pos<8; pos++) {\r
85 ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;\r
86 bt = (par_info >> (pos*8)) & 0xff;\r
87 for (i=0; i<8; i++) {\r
88 par[7-pos][i] = (bt >> i) & 0x01;\r
89 }\r
90 }\r
91\r
92 states = lfsr_common_prefix(nr, rr, ks3x, par, (par_info == 0));\r
93\r
94 if (states == NULL) {\r
95 *keys = NULL;\r
96 return 0;\r
97 }\r
98\r
99 keylist = (uint64_t*)states;\r
100 \r
101 for (i = 0; keylist[i]; i++) {\r
102 lfsr_rollback_word(states+i, uid^nt, 0);\r
103 crypto1_get_lfsr(states+i, &key_recovered);\r
104 keylist[i] = key_recovered;\r
105 }\r
106 keylist[i] = -1;\r
107\r
108 *keys = keylist;\r
109 return i;\r
110}\r
111\r
112\r
113int mfDarkside(uint64_t *key)\r
114{\r
115 uint32_t uid = 0;\r
116 uint32_t nt = 0, nr = 0;\r
117 uint64_t par_list = 0, ks_list = 0;\r
118 uint64_t *keylist = NULL, *last_keylist = NULL;\r
119 uint32_t keycount = 0;\r
120 int16_t isOK = 0;\r
121\r
122 UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r
123\r
124 // message\r
125 printf("-------------------------------------------------------------------------\n");\r
126 printf("Executing command. Expected execution time: 25sec on average\n");\r
127 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");\r
128 printf("-------------------------------------------------------------------------\n");\r
129\r
130 \r
131 while (true) {\r
132 clearCommandBuffer();\r
133 SendCommand(&c);\r
134 \r
135 //flush queue\r
136 while (ukbhit()) {\r
137 int c = getchar(); (void) c;\r
138 }\r
139 \r
140 // wait cycle\r
141 while (true) {\r
142 printf(".");\r
143 fflush(stdout);\r
144 if (ukbhit()) {\r
145 return -5;\r
146 break;\r
147 }\r
148 \r
149 UsbCommand resp;\r
150 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r
151 isOK = resp.arg[0];\r
152 if (isOK < 0) {\r
153 return isOK;\r
154 }\r
155 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r
156 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r
157 par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r
158 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r
159 nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
160 break;\r
161 }\r
162 } \r
163\r
164 if (par_list == 0 && c.arg[0] == true) {\r
165 PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");\r
166 PrintAndLog("Attack will take a few seconds longer because we need two consecutive successful runs.");\r
167 }\r
168 c.arg[0] = false;\r
169\r
170 keycount = nonce2key(uid, nt, nr, par_list, ks_list, &keylist);\r
171\r
172 if (keycount == 0) {\r
173 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r
174 PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r
175 continue;\r
176 }\r
177\r
178 qsort(keylist, keycount, sizeof(*keylist), compare_uint64);\r
179 keycount = intersection(last_keylist, keylist);\r
180 if (keycount == 0) {\r
181 free(last_keylist);\r
182 last_keylist = keylist;\r
183 continue;\r
184 }\r
185\r
186 if (keycount > 1) {\r
187 PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount);\r
188 } else {\r
189 PrintAndLog("Found a possible key. Trying to authenticate...\n");\r
190 } \r
191\r
192 *key = -1;\r
193 uint8_t keyBlock[USB_CMD_DATA_SIZE];\r
194 int max_keys = USB_CMD_DATA_SIZE/6;\r
195 for (int i = 0; i < keycount; i += max_keys) {\r
196 int size = keycount - i > max_keys ? max_keys : keycount - i;\r
197 for (int j = 0; j < size; j++) {\r
198 if (last_keylist == NULL) {\r
199 num_to_bytes(keylist[i*max_keys + j], 6, keyBlock);\r
200 } else {\r
201 num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock);\r
202 }\r
203 }\r
204 if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) {\r
205 break;\r
206 }\r
207 } \r
208 \r
209 if (*key != -1) {\r
210 free(last_keylist);\r
211 free(keylist);\r
212 break;\r
213 } else {\r
214 PrintAndLog("Authentication failed. Trying again...");\r
215 free(last_keylist);\r
216 last_keylist = keylist;\r
217 }\r
218 }\r
219 \r
220 return 0;\r
221}\r
222\r
223\r
224int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r
225\r
226 *key = 0;\r
227\r
228 UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r
229 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
230 SendCommand(&c);\r
231\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// Compare 16 Bits out of cryptostate\r
240int Compare16Bits(const void * a, const void * b) {\r
241 if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r
242 else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r
243 else return -1;\r
244}\r
245\r
246typedef \r
247 struct {\r
248 union {\r
249 struct Crypto1State *slhead;\r
250 uint64_t *keyhead;\r
251 } head;\r
252 union {\r
253 struct Crypto1State *sltail;\r
254 uint64_t *keytail;\r
255 } tail;\r
256 uint32_t len;\r
257 uint32_t uid;\r
258 uint32_t blockNo;\r
259 uint32_t keyType;\r
260 uint32_t nt;\r
261 uint32_t ks1;\r
262 } StateList_t;\r
263\r
264\r
265// wrapper function for multi-threaded lfsr_recovery32\r
266void* nested_worker_thread(void *arg)\r
267{\r
268 struct Crypto1State *p1;\r
269 StateList_t *statelist = arg;\r
270\r
271 statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r
272 for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r
273 statelist->len = p1 - statelist->head.slhead;\r
274 statelist->tail.sltail = --p1;\r
275 qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r
276 \r
277 return statelist->head.slhead;\r
278}\r
279\r
280int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate) \r
281{\r
282 uint16_t i;\r
283 uint32_t uid;\r
284 UsbCommand resp;\r
285\r
286 StateList_t statelists[2];\r
287 struct Crypto1State *p1, *p2, *p3, *p4;\r
288 \r
289 // flush queue\r
290 WaitForResponseTimeout(CMD_ACK, NULL, 100);\r
291 \r
292 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r
293 memcpy(c.d.asBytes, key, 6);\r
294 SendCommand(&c);\r
295\r
296 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
297 return -1;\r
298 }\r
299\r
300 if (resp.arg[0]) {\r
301 return resp.arg[0]; // error during nested\r
302 }\r
303 \r
304 memcpy(&uid, resp.d.asBytes, 4);\r
305 PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);\r
306 \r
307 for (i = 0; i < 2; i++) {\r
308 statelists[i].blockNo = resp.arg[2] & 0xff;\r
309 statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r
310 statelists[i].uid = uid;\r
311 memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r
312 memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r
313 }\r
314 \r
315 // calc keys\r
316 \r
317 pthread_t thread_id[2];\r
318 \r
319 // create and run worker threads\r
320 for (i = 0; i < 2; i++) {\r
321 pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r
322 }\r
323 \r
324 // wait for threads to terminate:\r
325 for (i = 0; i < 2; i++) {\r
326 pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r
327 }\r
328\r
329\r
330 // the first 16 Bits of the cryptostate already contain part of our key.\r
331 // Create the intersection of the two lists based on these 16 Bits and\r
332 // roll back the cryptostate\r
333 p1 = p3 = statelists[0].head.slhead; \r
334 p2 = p4 = statelists[1].head.slhead;\r
335 while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r
336 if (Compare16Bits(p1, p2) == 0) {\r
337 struct Crypto1State savestate, *savep = &savestate;\r
338 savestate = *p1;\r
339 while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r
340 *p3 = *p1;\r
341 lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r
342 p3++;\r
343 p1++;\r
344 }\r
345 savestate = *p2;\r
346 while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r
347 *p4 = *p2;\r
348 lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r
349 p4++;\r
350 p2++;\r
351 }\r
352 }\r
353 else {\r
354 while (Compare16Bits(p1, p2) == -1) p1++;\r
355 while (Compare16Bits(p1, p2) == 1) p2++;\r
356 }\r
357 }\r
358 *(uint64_t*)p3 = -1;\r
359 *(uint64_t*)p4 = -1;\r
360 statelists[0].len = p3 - statelists[0].head.slhead;\r
361 statelists[1].len = p4 - statelists[1].head.slhead;\r
362 statelists[0].tail.sltail=--p3;\r
363 statelists[1].tail.sltail=--p4;\r
364\r
365 // the statelists now contain possible keys. The key we are searching for must be in the\r
366 // intersection of both lists. Create the intersection:\r
367 qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64);\r
368 qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64);\r
369 statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead);\r
370\r
371 memset(resultKey, 0, 6);\r
372 // The list may still contain several key candidates. Test each of them with mfCheckKeys\r
373 for (i = 0; i < statelists[0].len; i++) {\r
374 uint8_t keyBlock[6];\r
375 uint64_t key64;\r
376 crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r
377 num_to_bytes(key64, 6, keyBlock);\r
378 key64 = 0;\r
379 if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r
380 num_to_bytes(key64, 6, resultKey);\r
381 break;\r
382 }\r
383 }\r
384 \r
385 free(statelists[0].head.slhead);\r
386 free(statelists[1].head.slhead);\r
387 \r
388 return 0;\r
389}\r
390\r
391// EMULATOR\r
392\r
393int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
394 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r
395 SendCommand(&c);\r
396\r
397 UsbCommand resp;\r
398 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r
399 memcpy(data, resp.d.asBytes, blocksCount * 16);\r
400 return 0;\r
401}\r
402\r
403int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
404 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};\r
405 memcpy(c.d.asBytes, data, blocksCount * 16); \r
406 SendCommand(&c);\r
407 return 0;\r
408}\r
409\r
410// "MAGIC" CARD\r
411\r
412int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r
413 uint8_t isOK = 0;\r
414\r
415 UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r
416 SendCommand(&c);\r
417\r
418 UsbCommand resp;\r
419 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
420 isOK = resp.arg[0] & 0xff;\r
421 memcpy(data, resp.d.asBytes, 16);\r
422 if (!isOK) return 2;\r
423 } else {\r
424 PrintAndLog("Command execute timeout");\r
425 return 1;\r
426 }\r
427 return 0;\r
428}\r
429\r
430int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {\r
431\r
432 uint8_t isOK = 0;\r
433 UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};\r
434 memcpy(c.d.asBytes, data, 16); \r
435 SendCommand(&c);\r
436\r
437 UsbCommand resp;\r
438 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
439 isOK = resp.arg[0] & 0xff;\r
440 if (uid != NULL) \r
441 memcpy(uid, resp.d.asBytes, 4);\r
442 if (!isOK) \r
443 return 2;\r
444 } else {\r
445 PrintAndLog("Command execute timeout");\r
446 return 1;\r
447 }\r
448 return 0;\r
449}\r
450\r
451int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe) {\r
452 uint8_t oldblock0[16] = {0x00};\r
453 uint8_t block0[16] = {0x00};\r
454\r
455 int old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER);\r
456 if (old == 0) {\r
457 memcpy(block0, oldblock0, 16);\r
458 PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r
459 } else {\r
460 PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r
461 }\r
462\r
463 // fill in the new values\r
464 // UID\r
465 memcpy(block0, uid, 4); \r
466 // Mifare UID BCC\r
467 block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\r
468 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r
469 if (sak!=NULL)\r
470 block0[5]=sak[0];\r
471 if (atqa!=NULL) {\r
472 block0[6]=atqa[1];\r
473 block0[7]=atqa[0];\r
474 }\r
475 PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r
476 return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);\r
477}\r
478\r
479// SNIFFER\r
480\r
481// constants\r
482static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r
483\r
484// variables\r
485char logHexFileName[FILE_PATH_SIZE] = {0x00};\r
486static uint8_t traceCard[4096] = {0x00};\r
487static char traceFileName[FILE_PATH_SIZE] = {0x00};\r
488static int traceState = TRACE_IDLE;\r
489static uint8_t traceCurBlock = 0;\r
490static uint8_t traceCurKey = 0;\r
491\r
492struct Crypto1State *traceCrypto1 = NULL;\r
493\r
494struct Crypto1State *revstate;\r
495uint64_t lfsr;\r
496uint32_t ks2;\r
497uint32_t ks3;\r
498\r
499uint32_t uid; // serial number\r
500uint32_t nt; // tag challenge\r
501uint32_t nr_enc; // encrypted reader challenge\r
502uint32_t ar_enc; // encrypted reader response\r
503uint32_t at_enc; // encrypted tag response\r
504\r
505int isTraceCardEmpty(void) {\r
506 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r
507}\r
508\r
509int isBlockEmpty(int blockN) {\r
510 for (int i = 0; i < 16; i++) \r
511 if (traceCard[blockN * 16 + i] != 0) return 0;\r
512\r
513 return 1;\r
514}\r
515\r
516int isBlockTrailer(int blockN) {\r
517 return ((blockN & 0x03) == 0x03);\r
518}\r
519\r
520int saveTraceCard(void) {\r
521 FILE * f;\r
522 \r
523 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r
524 \r
525 f = fopen(traceFileName, "w+");\r
526 if ( !f ) return 1;\r
527 \r
528 for (int i = 0; i < 64; i++) { // blocks\r
529 for (int j = 0; j < 16; j++) // bytes\r
530 fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r
531 fprintf(f,"\n");\r
532 }\r
533 fclose(f);\r
534 return 0;\r
535}\r
536\r
537int loadTraceCard(uint8_t *tuid) {\r
538 FILE * f;\r
539 char buf[64] = {0x00};\r
540 uint8_t buf8[64] = {0x00};\r
541 int i, blockNum;\r
542 \r
543 if (!isTraceCardEmpty()) \r
544 saveTraceCard();\r
545 \r
546 memset(traceCard, 0x00, 4096);\r
547 memcpy(traceCard, tuid + 3, 4);\r
548\r
549 FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r
550\r
551 f = fopen(traceFileName, "r");\r
552 if (!f) return 1;\r
553 \r
554 blockNum = 0;\r
555 \r
556 while(!feof(f)){\r
557 \r
558 memset(buf, 0, sizeof(buf));\r
559 if (fgets(buf, sizeof(buf), f) == NULL) {\r
560 PrintAndLog("File reading error.");\r
561 fclose(f);\r
562 return 2;\r
563 }\r
564\r
565 if (strlen(buf) < 32){\r
566 if (feof(f)) break;\r
567 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
568 fclose(f);\r
569 return 2;\r
570 }\r
571 for (i = 0; i < 32; i += 2)\r
572 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
573\r
574 memcpy(traceCard + blockNum * 16, buf8, 16);\r
575\r
576 blockNum++;\r
577 }\r
578 fclose(f);\r
579\r
580 return 0;\r
581}\r
582\r
583int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r
584\r
585 if (traceCrypto1) \r
586 crypto1_destroy(traceCrypto1);\r
587\r
588 traceCrypto1 = NULL;\r
589\r
590 if (wantSaveToEmlFile) \r
591 loadTraceCard(tuid);\r
592 \r
593 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r
594 traceCard[5] = sak;\r
595 memcpy(&traceCard[6], atqa, 2);\r
596 traceCurBlock = 0;\r
597 uid = bytes_to_num(tuid + 3, 4);\r
598 \r
599 traceState = TRACE_IDLE;\r
600\r
601 return 0;\r
602}\r
603\r
604void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r
605 uint8_t bt = 0;\r
606 int i;\r
607 \r
608 if (len != 1) {\r
609 for (i = 0; i < len; i++)\r
610 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r
611 } else {\r
612 bt = 0;\r
613 for (i = 0; i < 4; i++)\r
614 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r
615 \r
616 data[0] = bt;\r
617 }\r
618 return;\r
619}\r
620\r
621\r
622int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
623 uint8_t data[64];\r
624\r
625 if (traceState == TRACE_ERROR) return 1;\r
626 if (len > 64) {\r
627 traceState = TRACE_ERROR;\r
628 return 1;\r
629 }\r
630 \r
631 memcpy(data, data_src, len);\r
632 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r
633 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
634 PrintAndLog("dec> %s", sprint_hex(data, len));\r
635 AddLogHex(logHexFileName, "dec> ", data, len); \r
636 }\r
637 \r
638 switch (traceState) {\r
639 case TRACE_IDLE: \r
640 // check packet crc16!\r
641 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r
642 PrintAndLog("dec> CRC ERROR!!!");\r
643 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r
644 traceState = TRACE_ERROR; // do not decrypt the next commands\r
645 return 1;\r
646 }\r
647 \r
648 // AUTHENTICATION\r
649 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r
650 traceState = TRACE_AUTH1;\r
651 traceCurBlock = data[1];\r
652 traceCurKey = data[0] == 60 ? 1:0;\r
653 return 0;\r
654 }\r
655\r
656 // READ\r
657 if ((len ==4) && ((data[0] == 0x30))) {\r
658 traceState = TRACE_READ_DATA;\r
659 traceCurBlock = data[1];\r
660 return 0;\r
661 }\r
662\r
663 // WRITE\r
664 if ((len ==4) && ((data[0] == 0xA0))) {\r
665 traceState = TRACE_WRITE_OK;\r
666 traceCurBlock = data[1];\r
667 return 0;\r
668 }\r
669\r
670 // HALT\r
671 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r
672 traceState = TRACE_ERROR; // do not decrypt the next commands\r
673 return 0;\r
674 }\r
675 \r
676 return 0;\r
677 break;\r
678 \r
679 case TRACE_READ_DATA: \r
680 if (len == 18) {\r
681 traceState = TRACE_IDLE;\r
682\r
683 if (isBlockTrailer(traceCurBlock)) {\r
684 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r
685 } else {\r
686 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
687 }\r
688 if (wantSaveToEmlFile) saveTraceCard();\r
689 return 0;\r
690 } else {\r
691 traceState = TRACE_ERROR;\r
692 return 1;\r
693 }\r
694 break;\r
695\r
696 case TRACE_WRITE_OK: \r
697 if ((len == 1) && (data[0] == 0x0a)) {\r
698 traceState = TRACE_WRITE_DATA;\r
699\r
700 return 0;\r
701 } else {\r
702 traceState = TRACE_ERROR;\r
703 return 1;\r
704 }\r
705 break;\r
706\r
707 case TRACE_WRITE_DATA: \r
708 if (len == 18) {\r
709 traceState = TRACE_IDLE;\r
710\r
711 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
712 if (wantSaveToEmlFile) saveTraceCard();\r
713 return 0;\r
714 } else {\r
715 traceState = TRACE_ERROR;\r
716 return 1;\r
717 }\r
718 break;\r
719\r
720 case TRACE_AUTH1: \r
721 if (len == 4) {\r
722 traceState = TRACE_AUTH2;\r
723 nt = bytes_to_num(data, 4);\r
724 return 0;\r
725 } else {\r
726 traceState = TRACE_ERROR;\r
727 return 1;\r
728 }\r
729 break;\r
730\r
731 case TRACE_AUTH2: \r
732 if (len == 8) {\r
733 traceState = TRACE_AUTH_OK;\r
734\r
735 nr_enc = bytes_to_num(data, 4);\r
736 ar_enc = bytes_to_num(data + 4, 4);\r
737 return 0;\r
738 } else {\r
739 traceState = TRACE_ERROR;\r
740 return 1;\r
741 }\r
742 break;\r
743\r
744 case TRACE_AUTH_OK: \r
745 if (len ==4) {\r
746 traceState = TRACE_IDLE;\r
747\r
748 at_enc = bytes_to_num(data, 4);\r
749 \r
750 // decode key here)\r
751 ks2 = ar_enc ^ prng_successor(nt, 64);\r
752 ks3 = at_enc ^ prng_successor(nt, 96);\r
753 revstate = lfsr_recovery64(ks2, ks3);\r
754 lfsr_rollback_word(revstate, 0, 0);\r
755 lfsr_rollback_word(revstate, 0, 0);\r
756 lfsr_rollback_word(revstate, nr_enc, 1);\r
757 lfsr_rollback_word(revstate, uid ^ nt, 0);\r
758\r
759 crypto1_get_lfsr(revstate, &lfsr);\r
760 printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
761 AddLogUint64(logHexFileName, "key> ", lfsr); \r
762 \r
763 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
764 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
765 \r
766 if (traceCurKey) {\r
767 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r
768 } else {\r
769 num_to_bytes(lfsr, 6, traceCard + blockShift);\r
770 }\r
771 if (wantSaveToEmlFile) saveTraceCard();\r
772\r
773 if (traceCrypto1) {\r
774 crypto1_destroy(traceCrypto1);\r
775 }\r
776 \r
777 // set cryptosystem state\r
778 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
779 \r
780// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
781\r
782 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r
783 crypto1_word(traceCrypto1, nt ^ uid, 0);\r
784 crypto1_word(traceCrypto1, ar, 1);\r
785 crypto1_word(traceCrypto1, 0, 0);\r
786 crypto1_word(traceCrypto1, 0, 0);*/\r
787 \r
788 return 0;\r
789 } else {\r
790 traceState = TRACE_ERROR;\r
791 return 1;\r
792 }\r
793 break;\r
794\r
795 default: \r
796 traceState = TRACE_ERROR;\r
797 return 1;\r
798 }\r
799\r
800 return 0;\r
801}\r
802\r
803int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r
804 /*\r
805 uint32_t nt; // tag challenge\r
806 uint32_t ar_enc; // encrypted reader response\r
807 uint32_t at_enc; // encrypted tag response\r
808 */\r
809 if (traceCrypto1) {\r
810 crypto1_destroy(traceCrypto1);\r
811 }\r
812 ks2 = ar_enc ^ prng_successor(nt, 64);\r
813 ks3 = at_enc ^ prng_successor(nt, 96);\r
814 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
815\r
816 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
817\r
818 PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r
819 crypto1_destroy(traceCrypto1);\r
820 return 0;\r
821}\r
Impressum, Datenschutz