]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/mifarehost.c
Fix typos
[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#include "mifarehost.h"\r
16#include "proxmark3.h"\r
17\r
18// MIFARE\r
19int 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
30int 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
36typedef \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
56void* 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
70int 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 WaitForResponseTimeout(CMD_ACK, NULL, 100);\r
81 \r
82 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r
83 memcpy(c.d.asBytes, key, 6);\r
84 SendCommand(&c);\r
85\r
86 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
87 return -1;\r
88 }\r
89\r
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
103 }\r
104 \r
105 // calc keys\r
106 \r
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
112 }\r
113 \r
114 // wait for threads to terminate:\r
115 for (i = 0; i < 2; i++) {\r
116 pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r
117 }\r
118\r
119\r
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
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
126 if (Compare16Bits(p1, p2) == 0) {\r
127 struct Crypto1State savestate, *savep = &savestate;\r
128 savestate = *p1;\r
129 while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r
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
136 while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r
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
147 }\r
148 p3->even = 0; p3->odd = 0;\r
149 p4->even = 0; p4->odd = 0;\r
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
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
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
159\r
160 uint64_t *p5, *p6, *p7;\r
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
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
173 statelists[0].len = p7 - statelists[0].head.keyhead;\r
174 statelists[0].tail.keytail=--p7;\r
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
181 crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r
182 num_to_bytes(key64, 6, keyBlock);\r
183 key64 = 0;\r
184 if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r
185 num_to_bytes(key64, 6, resultKey);\r
186 break;\r
187 }\r
188 }\r
189 \r
190 free(statelists[0].head.slhead);\r
191 free(statelists[1].head.slhead);\r
192 \r
193 return 0;\r
194}\r
195\r
196int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r
197\r
198 *key = 0;\r
199\r
200 UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r
201 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
202 SendCommand(&c);\r
203\r
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
208 return 0;\r
209}\r
210\r
211// EMULATOR\r
212\r
213int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
214 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r
215 SendCommand(&c);\r
216\r
217 UsbCommand resp;\r
218 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r
219 memcpy(data, resp.d.asBytes, blocksCount * 16);\r
220 return 0;\r
221}\r
222\r
223int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
224 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};\r
225 memcpy(c.d.asBytes, data, blocksCount * 16); \r
226 SendCommand(&c);\r
227 return 0;\r
228}\r
229\r
230// "MAGIC" CARD\r
231\r
232int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe) {\r
233 uint8_t oldblock0[16] = {0x00};\r
234 uint8_t block0[16] = {0x00};\r
235\r
236 int old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER);\r
237 if (old == 0) {\r
238 memcpy(block0, oldblock0, 16);\r
239 PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r
240 } else {\r
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
255 }\r
256 PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r
257 return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);\r
258}\r
259\r
260int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {\r
261\r
262 uint8_t isOK = 0;\r
263 UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};\r
264 memcpy(c.d.asBytes, data, 16); \r
265 SendCommand(&c);\r
266\r
267 UsbCommand resp;\r
268 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
269 isOK = resp.arg[0] & 0xff;\r
270 if (uid != NULL) \r
271 memcpy(uid, resp.d.asBytes, 4);\r
272 if (!isOK) \r
273 return 2;\r
274 } else {\r
275 PrintAndLog("Command execute timeout");\r
276 return 1;\r
277 }\r
278 return 0;\r
279}\r
280\r
281int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r
282 uint8_t isOK = 0;\r
283\r
284 UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r
285 SendCommand(&c);\r
286\r
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
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
298\r
299// SNIFFER\r
300\r
301// constants\r
302static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r
303\r
304// variables\r
305char logHexFileName[FILE_PATH_SIZE] = {0x00};\r
306static uint8_t traceCard[4096] = {0x00};\r
307static char traceFileName[FILE_PATH_SIZE] = {0x00};\r
308static int traceState = TRACE_IDLE;\r
309static uint8_t traceCurBlock = 0;\r
310static uint8_t traceCurKey = 0;\r
311\r
312struct Crypto1State *traceCrypto1 = NULL;\r
313\r
314struct Crypto1State *revstate;\r
315uint64_t lfsr;\r
316uint32_t ks2;\r
317uint32_t ks3;\r
318\r
319uint32_t uid; // serial number\r
320uint32_t nt; // tag challenge\r
321uint32_t nr_enc; // encrypted reader challenge\r
322uint32_t ar_enc; // encrypted reader response\r
323uint32_t at_enc; // encrypted tag response\r
324\r
325int isTraceCardEmpty(void) {\r
326 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r
327}\r
328\r
329int isBlockEmpty(int blockN) {\r
330 for (int i = 0; i < 16; i++) \r
331 if (traceCard[blockN * 16 + i] != 0) return 0;\r
332\r
333 return 1;\r
334}\r
335\r
336int isBlockTrailer(int blockN) {\r
337 return ((blockN & 0x03) == 0x03);\r
338}\r
339\r
340int loadTraceCard(uint8_t *tuid) {\r
341 FILE * f;\r
342 char buf[64] = {0x00};\r
343 uint8_t buf8[64] = {0x00};\r
344 int i, blockNum;\r
345 \r
346 if (!isTraceCardEmpty()) \r
347 saveTraceCard();\r
348 \r
349 memset(traceCard, 0x00, 4096);\r
350 memcpy(traceCard, tuid + 3, 4);\r
351\r
352 FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r
353\r
354 f = fopen(traceFileName, "r");\r
355 if (!f) return 1;\r
356 \r
357 blockNum = 0;\r
358 \r
359 while(!feof(f)){\r
360 \r
361 memset(buf, 0, sizeof(buf));\r
362 if (fgets(buf, sizeof(buf), f) == NULL) {\r
363 PrintAndLog("File reading error.");\r
364 fclose(f);\r
365 return 2;\r
366 }\r
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
371 fclose(f);\r
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
386int 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
392 if ( !f ) return 1;\r
393 \r
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
400 return 0;\r
401}\r
402\r
403int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r
404\r
405 if (traceCrypto1) \r
406 crypto1_destroy(traceCrypto1);\r
407\r
408 traceCrypto1 = NULL;\r
409\r
410 if (wantSaveToEmlFile) \r
411 loadTraceCard(tuid);\r
412 \r
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
424void 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
442int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
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
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
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
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
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
517 if ((len == 1) && (data[0] == 0x0a)) {\r
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
532 if (wantSaveToEmlFile) saveTraceCard();\r
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
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
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
578\r
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
583 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
584 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
585 \r
586 if (traceCurKey) {\r
587 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r
588 } else {\r
589 num_to_bytes(lfsr, 6, traceCard + blockShift);\r
590 }\r
591 if (wantSaveToEmlFile) saveTraceCard();\r
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
622\r
623int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r
624 /*\r
625 uint32_t nt; // tag challenge\r
626 uint32_t ar_enc; // encrypted reader response\r
627 uint32_t at_enc; // encrypted tag response\r
628 */\r
629 if (traceCrypto1) {\r
630 crypto1_destroy(traceCrypto1);\r
631 }\r
632 ks2 = ar_enc ^ prng_successor(nt, 64);\r
633 ks3 = at_enc ^ prng_successor(nt, 96);\r
634 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
635\r
636 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
637\r
638 PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r
639 crypto1_destroy(traceCrypto1);\r
640 return 0;\r
641}\r
Impressum, Datenschutz