]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
bugfix
[proxmark3-svn] / armsrc / mifarecmd.c
1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
5 // Midnitesnake - Dec 2013
6 // Andy Davies - Apr 2014
7 // Iceman - May 2014
8 //
9 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
10 // at your option, any later version. See the LICENSE.txt file for the text of
11 // the license.
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
15
16 #include "mifarecmd.h"
17 #include "apps.h"
18 #include "util.h"
19
20 //-----------------------------------------------------------------------------
21 // Select, Authenticate, Read a MIFARE tag.
22 // read block
23 //-----------------------------------------------------------------------------
24 void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
25 {
26 // params
27 uint8_t blockNo = arg0;
28 uint8_t keyType = arg1;
29 uint64_t ui64Key = 0;
30 ui64Key = bytes_to_num(datain, 6);
31
32 // variables
33 byte_t isOK = 0;
34 byte_t dataoutbuf[16];
35 uint8_t uid[10];
36 uint32_t cuid;
37 struct Crypto1State mpcs = {0, 0};
38 struct Crypto1State *pcs;
39 pcs = &mpcs;
40
41 // clear trace
42 iso14a_clear_trace();
43 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
44
45 LED_A_ON();
46 LED_B_OFF();
47 LED_C_OFF();
48
49 while (true) {
50 if(!iso14443a_select_card(uid, NULL, &cuid)) {
51 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
52 break;
53 };
54
55 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
56 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
57 break;
58 };
59
60 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {
61 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
62 break;
63 };
64
65 if(mifare_classic_halt(pcs, cuid)) {
66 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
67 break;
68 };
69
70 isOK = 1;
71 break;
72 }
73
74 // ----------------------------- crypto1 destroy
75 crypto1_destroy(pcs);
76
77 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
78
79 LED_B_ON();
80 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);
81 LED_B_OFF();
82
83 // Thats it...
84 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
85 LEDsoff();
86 }
87
88 void MifareUReadBlock(uint8_t arg0,uint8_t *datain)
89 {
90 uint8_t blockNo = arg0;
91 byte_t dataout[16] = {0x00};
92 uint8_t uid[10] = {0x00};
93 uint32_t cuid;
94
95 LED_A_ON();
96 LED_B_OFF();
97 LED_C_OFF();
98
99 iso14a_clear_trace();
100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
101
102 int len = iso14443a_select_card(uid, NULL, &cuid);
103 if(!len) {
104 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
105 //OnError(1);
106 return;
107 };
108
109 len = mifare_ultra_readblock(cuid, blockNo, dataout);
110 if(len) {
111 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");
112 //OnError(2);
113 return;
114 };
115
116 len = mifare_ultra_halt(cuid);
117 if(len) {
118 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
119 //OnError(3);
120 return;
121 };
122
123 cmd_send(CMD_ACK,1,0,0,dataout,16);
124 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
125 LEDsoff();
126 }
127
128 //-----------------------------------------------------------------------------
129 // Select, Authenticate, Read a MIFARE tag.
130 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
131 //-----------------------------------------------------------------------------
132 void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
133 {
134 // params
135 uint8_t sectorNo = arg0;
136 uint8_t keyType = arg1;
137 uint64_t ui64Key = 0;
138 ui64Key = bytes_to_num(datain, 6);
139
140 // variables
141 byte_t isOK = 0;
142 byte_t dataoutbuf[16 * 16];
143 uint8_t uid[10];
144 uint32_t cuid;
145 struct Crypto1State mpcs = {0, 0};
146 struct Crypto1State *pcs;
147 pcs = &mpcs;
148
149 // clear trace
150 iso14a_clear_trace();
151
152 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
153
154 LED_A_ON();
155 LED_B_OFF();
156 LED_C_OFF();
157
158 isOK = 1;
159 if(!iso14443a_select_card(uid, NULL, &cuid)) {
160 isOK = 0;
161 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
162 }
163
164
165 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
166 isOK = 0;
167 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
168 }
169
170 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
171 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {
172 isOK = 0;
173 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);
174 break;
175 }
176 }
177
178 if(mifare_classic_halt(pcs, cuid)) {
179 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
180 }
181
182 // ----------------------------- crypto1 destroy
183 crypto1_destroy(pcs);
184
185 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");
186
187 LED_B_ON();
188 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));
189 LED_B_OFF();
190
191 // Thats it...
192 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
193 LEDsoff();
194 }
195
196 void MifareUReadCard(uint8_t arg0, int arg1, uint8_t *datain)
197 {
198 // params
199 uint8_t sectorNo = arg0;
200 int Pages = arg1;
201 int count_Pages = 0;
202 byte_t dataout[176] = {0x00};;
203 uint8_t uid[10] = {0x00};
204 uint32_t cuid;
205
206 LED_A_ON();
207 LED_B_OFF();
208 LED_C_OFF();
209
210 if (MF_DBGLEVEL >= MF_DBG_ALL)
211 Dbprintf("Pages %d",Pages);
212
213 iso14a_clear_trace();
214 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
215
216 int len = iso14443a_select_card(uid, NULL, &cuid);
217
218 if (!len) {
219 if (MF_DBGLEVEL >= MF_DBG_ERROR)
220 Dbprintf("Can't select card");
221 //OnError(1);
222 return;
223 }
224
225 for (int i = 0; i < Pages; i++){
226
227 len = mifare_ultra_readblock(cuid, sectorNo * 4 + i, dataout + 4 * i);
228
229 if (len) {
230 if (MF_DBGLEVEL >= MF_DBG_ERROR)
231 Dbprintf("Read block %d error",i);
232 //OnError(2);
233 return;
234 } else {
235 count_Pages++;
236 }
237 }
238
239 len = mifare_ultra_halt(cuid);
240 if (len) {
241 if (MF_DBGLEVEL >= MF_DBG_ERROR)
242 Dbprintf("Halt error");
243 //OnError(3);
244 return;
245 }
246
247 if (MF_DBGLEVEL >= MF_DBG_ALL) {
248 Dbprintf("Pages read %d", count_Pages);
249 }
250
251 len = 16*4; //64 bytes
252
253 // Read a UL-C
254 if (Pages == 44 && count_Pages > 16)
255 len = 176;
256
257 cmd_send(CMD_ACK, 1, 0, 0, dataout, len);
258 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
259 LEDsoff();
260 }
261
262
263 //-----------------------------------------------------------------------------
264 // Select, Authenticate, Write a MIFARE tag.
265 // read block
266 //-----------------------------------------------------------------------------
267 void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
268 {
269 // params
270 uint8_t blockNo = arg0;
271 uint8_t keyType = arg1;
272 uint64_t ui64Key = 0;
273 byte_t blockdata[16];
274
275 ui64Key = bytes_to_num(datain, 6);
276 memcpy(blockdata, datain + 10, 16);
277
278 // variables
279 byte_t isOK = 0;
280 uint8_t uid[10];
281 uint32_t cuid;
282 struct Crypto1State mpcs = {0, 0};
283 struct Crypto1State *pcs;
284 pcs = &mpcs;
285
286 // clear trace
287 iso14a_clear_trace();
288
289 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
290
291 LED_A_ON();
292 LED_B_OFF();
293 LED_C_OFF();
294
295 while (true) {
296 if(!iso14443a_select_card(uid, NULL, &cuid)) {
297 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
298 break;
299 };
300
301 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
302 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
303 break;
304 };
305
306 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {
307 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
308 break;
309 };
310
311 if(mifare_classic_halt(pcs, cuid)) {
312 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
313 break;
314 };
315
316 isOK = 1;
317 break;
318 }
319
320 // ----------------------------- crypto1 destroy
321 crypto1_destroy(pcs);
322
323 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
324
325 LED_B_ON();
326 cmd_send(CMD_ACK,isOK,0,0,0,0);
327 LED_B_OFF();
328
329
330 // Thats it...
331 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
332 LEDsoff();
333 }
334
335 void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)
336 {
337 // params
338 uint8_t blockNo = arg0;
339 byte_t blockdata[16] = {0x00};
340
341 memcpy(blockdata, datain,16);
342
343 // variables
344 byte_t isOK = 0;
345 uint8_t uid[10] = {0x00};
346 uint32_t cuid;
347
348 iso14a_clear_trace();
349 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
350
351 LED_A_ON();
352 LED_B_OFF();
353 LED_C_OFF();
354
355 while (true) {
356 if(!iso14443a_select_card(uid, NULL, &cuid)) {
357 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
358 break;
359 };
360
361 if(mifare_ultra_writeblock(cuid, blockNo, blockdata)) {
362 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
363 break;
364 };
365
366 if(mifare_ultra_halt(cuid)) {
367 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
368 break;
369 };
370
371 isOK = 1;
372 break;
373 }
374
375 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
376
377 cmd_send(CMD_ACK,isOK,0,0,0,0);
378 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
379 LEDsoff();
380 }
381
382 void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)
383 {
384 // params
385 uint8_t blockNo = arg0;
386 byte_t blockdata[4] = {0x00};
387
388 memcpy(blockdata, datain,4);
389
390 // variables
391 byte_t isOK = 0;
392 uint8_t uid[10] = {0x00};
393 uint32_t cuid;
394
395 iso14a_clear_trace();
396 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
397
398 LED_A_ON();
399 LED_B_OFF();
400 LED_C_OFF();
401
402 while (true) {
403 if(!iso14443a_select_card(uid, NULL, &cuid)) {
404 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
405 break;
406 };
407
408 if(mifare_ultra_special_writeblock(cuid, blockNo, blockdata)) {
409 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
410 break;
411 };
412
413 if(mifare_ultra_halt(cuid)) {
414 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
415 break;
416 };
417
418 isOK = 1;
419 break;
420 }
421
422 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
423
424 cmd_send(CMD_ACK,isOK,0,0,0,0);
425 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
426 LEDsoff();
427 }
428
429 // Return 1 if the nonce is invalid else return 0
430 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {
431 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
432 (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \
433 (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;
434 }
435
436
437 //-----------------------------------------------------------------------------
438 // MIFARE nested authentication.
439 //
440 //-----------------------------------------------------------------------------
441 void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)
442 {
443 // params
444 uint8_t blockNo = arg0 & 0xff;
445 uint8_t keyType = (arg0 >> 8) & 0xff;
446 uint8_t targetBlockNo = arg1 & 0xff;
447 uint8_t targetKeyType = (arg1 >> 8) & 0xff;
448 uint64_t ui64Key = 0;
449
450 ui64Key = bytes_to_num(datain, 6);
451
452 // variables
453 uint16_t rtr, i, j, len;
454 uint16_t davg;
455 static uint16_t dmin, dmax;
456 uint8_t uid[10];
457 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;
458 uint8_t par[1];
459 uint32_t target_nt[2], target_ks[2];
460
461 uint8_t par_array[4];
462 uint16_t ncount = 0;
463 struct Crypto1State mpcs = {0, 0};
464 struct Crypto1State *pcs;
465 pcs = &mpcs;
466 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
467
468 uint32_t auth1_time, auth2_time;
469 static uint16_t delta_time;
470
471 // clear trace
472 iso14a_clear_trace();
473 iso14a_set_tracing(false);
474
475 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
476
477 LED_A_ON();
478 LED_C_OFF();
479
480
481 // statistics on nonce distance
482 if (calibrate) { // for first call only. Otherwise reuse previous calibration
483 LED_B_ON();
484 WDT_HIT();
485
486 davg = dmax = 0;
487 dmin = 2000;
488 delta_time = 0;
489
490 for (rtr = 0; rtr < 17; rtr++) {
491
492 // prepare next select. No need to power down the card.
493 if(mifare_classic_halt(pcs, cuid)) {
494 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
495 rtr--;
496 continue;
497 }
498
499 if(!iso14443a_select_card(uid, NULL, &cuid)) {
500 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
501 rtr--;
502 continue;
503 };
504
505 auth1_time = 0;
506 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
507 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
508 rtr--;
509 continue;
510 };
511
512 if (delta_time) {
513 auth2_time = auth1_time + delta_time;
514 } else {
515 auth2_time = 0;
516 }
517 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {
518 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");
519 rtr--;
520 continue;
521 };
522
523 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
524 for (i = 101; i < 1200; i++) {
525 nttmp = prng_successor(nttmp, 1);
526 if (nttmp == nt2) break;
527 }
528
529 if (i != 1200) {
530 if (rtr != 0) {
531 davg += i;
532 dmin = MIN(dmin, i);
533 dmax = MAX(dmax, i);
534 }
535 else {
536 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing
537 }
538 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);
539 }
540 }
541
542 if (rtr <= 1) return;
543
544 davg = (davg + (rtr - 1)/2) / (rtr - 1);
545
546 if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);
547
548 dmin = davg - 2;
549 dmax = davg + 2;
550
551 LED_B_OFF();
552
553 }
554 // -------------------------------------------------------------------------------------------------
555
556 LED_C_ON();
557
558 // get crypted nonces for target sector
559 for(i=0; i < 2; i++) { // look for exactly two different nonces
560
561 target_nt[i] = 0;
562 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce
563
564 // prepare next select. No need to power down the card.
565 if(mifare_classic_halt(pcs, cuid)) {
566 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
567 continue;
568 }
569
570 if(!iso14443a_select_card(uid, NULL, &cuid)) {
571 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
572 continue;
573 };
574
575 auth1_time = 0;
576 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
577 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
578 continue;
579 };
580
581 // nested authentication
582 auth2_time = auth1_time + delta_time;
583 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);
584 if (len != 4) {
585 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);
586 continue;
587 };
588
589 nt2 = bytes_to_num(receivedAnswer, 4);
590 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);
591
592 // Parity validity check
593 for (j = 0; j < 4; j++) {
594 par_array[j] = (oddparity(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));
595 }
596
597 ncount = 0;
598 nttest = prng_successor(nt1, dmin - 1);
599 for (j = dmin; j < dmax + 1; j++) {
600 nttest = prng_successor(nttest, 1);
601 ks1 = nt2 ^ nttest;
602
603 if (valid_nonce(nttest, nt2, ks1, par_array)){
604 if (ncount > 0) { // we are only interested in disambiguous nonces, try again
605 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);
606 target_nt[i] = 0;
607 break;
608 }
609 target_nt[i] = nttest;
610 target_ks[i] = ks1;
611 ncount++;
612 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces
613 target_nt[i] = 0;
614 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);
615 break;
616 }
617 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);
618 }
619 }
620 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);
621 }
622 }
623
624 LED_C_OFF();
625
626 // ----------------------------- crypto1 destroy
627 crypto1_destroy(pcs);
628
629 byte_t buf[4 + 4 * 4];
630 memcpy(buf, &cuid, 4);
631 memcpy(buf+4, &target_nt[0], 4);
632 memcpy(buf+8, &target_ks[0], 4);
633 memcpy(buf+12, &target_nt[1], 4);
634 memcpy(buf+16, &target_ks[1], 4);
635
636 LED_B_ON();
637 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));
638 LED_B_OFF();
639
640 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");
641
642 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
643 LEDsoff();
644 iso14a_set_tracing(TRUE);
645 }
646
647 //-----------------------------------------------------------------------------
648 // MIFARE check keys. key count up to 85.
649 //
650 //-----------------------------------------------------------------------------
651 void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
652 {
653 // params
654 uint8_t blockNo = arg0;
655 uint8_t keyType = arg1;
656 uint8_t keyCount = arg2;
657 uint64_t ui64Key = 0;
658
659 // variables
660 int i;
661 byte_t isOK = 0;
662 uint8_t uid[10];
663 uint32_t cuid;
664 struct Crypto1State mpcs = {0, 0};
665 struct Crypto1State *pcs;
666 pcs = &mpcs;
667
668 // clear debug level
669 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
670 MF_DBGLEVEL = MF_DBG_NONE;
671
672 // clear trace
673 iso14a_clear_trace();
674 iso14a_set_tracing(TRUE);
675
676 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
677
678 LED_A_ON();
679 LED_B_OFF();
680 LED_C_OFF();
681
682 for (i = 0; i < keyCount; i++) {
683 if(mifare_classic_halt(pcs, cuid)) {
684 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");
685 }
686
687 if(!iso14443a_select_card(uid, NULL, &cuid)) {
688 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");
689 break;
690 };
691
692 ui64Key = bytes_to_num(datain + i * 6, 6);
693 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
694 continue;
695 };
696
697 isOK = 1;
698 break;
699 }
700
701 // ----------------------------- crypto1 destroy
702 crypto1_destroy(pcs);
703
704 LED_B_ON();
705 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);
706 LED_B_OFF();
707
708 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
709 LEDsoff();
710
711 // restore debug level
712 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
713 }
714
715 //-----------------------------------------------------------------------------
716 // MIFARE commands set debug level
717 //
718 //-----------------------------------------------------------------------------
719 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
720 MF_DBGLEVEL = arg0;
721 Dbprintf("Debug level: %d", MF_DBGLEVEL);
722 }
723
724 //-----------------------------------------------------------------------------
725 // Work with emulator memory
726 //
727 //-----------------------------------------------------------------------------
728 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
729 emlClearMem();
730 }
731
732 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
733 emlSetMem(datain, arg0, arg1); // data, block num, blocks count
734 }
735
736 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
737 byte_t buf[USB_CMD_DATA_SIZE];
738 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)
739
740 LED_B_ON();
741 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);
742 LED_B_OFF();
743 }
744
745 //-----------------------------------------------------------------------------
746 // Load a card into the emulator memory
747 //
748 //-----------------------------------------------------------------------------
749 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
750 uint8_t numSectors = arg0;
751 uint8_t keyType = arg1;
752 uint64_t ui64Key = 0;
753 uint32_t cuid;
754 struct Crypto1State mpcs = {0, 0};
755 struct Crypto1State *pcs;
756 pcs = &mpcs;
757
758 // variables
759 byte_t dataoutbuf[16];
760 byte_t dataoutbuf2[16];
761 uint8_t uid[10];
762
763 // clear trace
764 iso14a_clear_trace();
765 iso14a_set_tracing(false);
766
767 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
768
769 LED_A_ON();
770 LED_B_OFF();
771 LED_C_OFF();
772
773 bool isOK = true;
774
775 if(!iso14443a_select_card(uid, NULL, &cuid)) {
776 isOK = false;
777 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
778 }
779
780 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
781 ui64Key = emlGetKey(sectorNo, keyType);
782 if (sectorNo == 0){
783 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
784 isOK = false;
785 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);
786 break;
787 }
788 } else {
789 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {
790 isOK = false;
791 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);
792 break;
793 }
794 }
795
796 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
797 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {
798 isOK = false;
799 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);
800 break;
801 };
802 if (isOK) {
803 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {
804 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);
805 } else { // sector trailer, keep the keys, set only the AC
806 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
807 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
808 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
809 }
810 }
811 }
812
813 }
814
815 if(mifare_classic_halt(pcs, cuid)) {
816 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
817 };
818
819 // ----------------------------- crypto1 destroy
820 crypto1_destroy(pcs);
821
822 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
823 LEDsoff();
824
825 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
826
827 }
828
829
830 //-----------------------------------------------------------------------------
831 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
832 //
833 //-----------------------------------------------------------------------------
834 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
835
836 // params
837 uint8_t needWipe = arg0;
838 // bit 0 - need get UID
839 // bit 1 - need wupC
840 // bit 2 - need HALT after sequence
841 // bit 3 - need init FPGA and field before sequence
842 // bit 4 - need reset FPGA and LED
843 uint8_t workFlags = arg1;
844 uint8_t blockNo = arg2;
845
846 // card commands
847 uint8_t wupC1[] = { 0x40 };
848 uint8_t wupC2[] = { 0x43 };
849 uint8_t wipeC[] = { 0x41 };
850
851 // variables
852 byte_t isOK = 0;
853 uint8_t uid[10] = {0x00};
854 uint8_t d_block[18] = {0x00};
855 uint32_t cuid;
856
857 uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();
858 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
859
860 // reset FPGA and LED
861 if (workFlags & 0x08) {
862 LED_A_ON();
863 LED_B_OFF();
864 LED_C_OFF();
865
866 iso14a_clear_trace();
867 iso14a_set_tracing(TRUE);
868 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
869 }
870
871 while (true) {
872
873 // get UID from chip
874 if (workFlags & 0x01) {
875 if(!iso14443a_select_card(uid, NULL, &cuid)) {
876 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
877 break;
878 };
879
880 if(mifare_classic_halt(NULL, cuid)) {
881 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
882 break;
883 };
884 };
885
886 // reset chip
887 if (needWipe){
888 ReaderTransmitBitsPar(wupC1,7,0, NULL);
889 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
890 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
891 break;
892 };
893
894 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
895 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
896 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
897 break;
898 };
899
900 if(mifare_classic_halt(NULL, cuid)) {
901 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
902 break;
903 };
904 };
905
906 // write block
907 if (workFlags & 0x02) {
908 ReaderTransmitBitsPar(wupC1,7,0, NULL);
909 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
910 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
911 break;
912 };
913
914 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
915 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
916 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
917 break;
918 };
919 }
920
921 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
922 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
923 break;
924 };
925
926 memcpy(d_block, datain, 16);
927 AppendCrc14443a(d_block, 16);
928
929 ReaderTransmit(d_block, sizeof(d_block), NULL);
930 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
931 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
932 break;
933 };
934
935 if (workFlags & 0x04) {
936 if (mifare_classic_halt(NULL, cuid)) {
937 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
938 break;
939 };
940 }
941
942 isOK = 1;
943 break;
944 }
945
946 LED_B_ON();
947 cmd_send(CMD_ACK,isOK,0,0,uid,4);
948 LED_B_OFF();
949
950 if ((workFlags & 0x10) || (!isOK)) {
951 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
952 LEDsoff();
953 }
954 }
955
956
957 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
958
959 // params
960 // bit 1 - need wupC
961 // bit 2 - need HALT after sequence
962 // bit 3 - need init FPGA and field before sequence
963 // bit 4 - need reset FPGA and LED
964 uint8_t workFlags = arg0;
965 uint8_t blockNo = arg2;
966
967 // card commands
968 uint8_t wupC1[] = { 0x40 };
969 uint8_t wupC2[] = { 0x43 };
970
971 // variables
972 byte_t isOK = 0;
973 uint8_t data[18] = {0x00};
974 uint32_t cuid = 0;
975
976 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
977 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
978
979 if (workFlags & 0x08) {
980 LED_A_ON();
981 LED_B_OFF();
982 LED_C_OFF();
983
984 iso14a_clear_trace();
985 iso14a_set_tracing(TRUE);
986 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
987 }
988
989 while (true) {
990 if (workFlags & 0x02) {
991 ReaderTransmitBitsPar(wupC1,7,0, NULL);
992 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
993 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
994 break;
995 };
996
997 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
998 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
999 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1000 break;
1001 };
1002 }
1003
1004 // read block
1005 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {
1006 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1007 break;
1008 };
1009 memcpy(data, receivedAnswer, 18);
1010
1011 if (workFlags & 0x04) {
1012 if (mifare_classic_halt(NULL, cuid)) {
1013 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1014 break;
1015 };
1016 }
1017
1018 isOK = 1;
1019 break;
1020 }
1021
1022 LED_B_ON();
1023 cmd_send(CMD_ACK,isOK,0,0,data,18);
1024 LED_B_OFF();
1025
1026 if ((workFlags & 0x10) || (!isOK)) {
1027 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1028 LEDsoff();
1029 }
1030 }
1031
1032 void MifareCIdent(){
1033
1034 // card commands
1035 uint8_t wupC1[] = { 0x40 };
1036 uint8_t wupC2[] = { 0x43 };
1037
1038 // variables
1039 byte_t isOK = 1;
1040
1041 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
1042 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
1043
1044 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1045 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1046 isOK = 0;
1047 };
1048
1049 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1050 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1051 isOK = 0;
1052 };
1053
1054 if (mifare_classic_halt(NULL, 0)) {
1055 isOK = 0;
1056 };
1057
1058 cmd_send(CMD_ACK,isOK,0,0,0,0);
1059 }
1060
1061 //
1062 // DESFIRE
1063 //
Impressum, Datenschutz