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