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