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