]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
Fix for USB uart slowness since PR #720 (#787)
[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
18 #include "apps.h"
19 #include "util.h"
20 #include "parity.h"
21 #include "crc.h"
22
23 #define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
24 #define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
25
26 // the block number for the ISO14443-4 PCB
27 static uint8_t pcb_blocknum = 0;
28 // Deselect card by sending a s-block. the crc is precalced for speed
29 static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
30
31 //-----------------------------------------------------------------------------
32 // Select, Authenticate, Read a MIFARE tag.
33 // read block
34 //-----------------------------------------------------------------------------
35 void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
36 {
37 // params
38 uint8_t blockNo = arg0;
39 uint8_t keyType = arg1;
40 uint64_t ui64Key = 0;
41 ui64Key = bytes_to_num(datain, 6);
42
43 // variables
44 byte_t isOK = 0;
45 byte_t dataoutbuf[16];
46 uint8_t uid[10];
47 uint32_t cuid;
48 struct Crypto1State mpcs = {0, 0};
49 struct Crypto1State *pcs;
50 pcs = &mpcs;
51
52 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
53
54 clear_trace();
55
56 LED_A_ON();
57 LED_B_OFF();
58 LED_C_OFF();
59
60 while (true) {
61 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
62 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
63 break;
64 };
65
66 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
67 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
68 break;
69 };
70
71 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {
72 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
73 break;
74 };
75
76 if(mifare_classic_halt(pcs, cuid)) {
77 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
78 break;
79 };
80
81 isOK = 1;
82 break;
83 }
84
85 // ----------------------------- crypto1 destroy
86 crypto1_destroy(pcs);
87
88 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
89
90 LED_B_ON();
91 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);
92 LED_B_OFF();
93
94 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
95 LEDsoff();
96 }
97
98 void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){
99
100 bool turnOffField = (arg0 == 1);
101
102 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
103
104 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
105
106 clear_trace();
107
108 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {
109 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
110 OnError(0);
111 return;
112 };
113
114 if(!mifare_ultra_auth(keybytes)){
115 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");
116 OnError(1);
117 return;
118 }
119
120 if (turnOffField) {
121 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
122 LEDsoff();
123 }
124 cmd_send(CMD_ACK,1,0,0,0,0);
125 }
126
127 // Arg0 = BlockNo,
128 // Arg1 = UsePwd bool
129 // datain = PWD bytes,
130 void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
131 {
132 uint8_t blockNo = arg0;
133 byte_t dataout[16] = {0x00};
134 bool useKey = (arg1 == 1); //UL_C
135 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
136
137 LEDsoff();
138 LED_A_ON();
139 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
140
141 clear_trace();
142
143 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);
144 if(!len) {
145 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);
146 OnError(1);
147 return;
148 }
149
150 // UL-C authentication
151 if ( useKey ) {
152 uint8_t key[16] = {0x00};
153 memcpy(key, datain, sizeof(key) );
154
155 if ( !mifare_ultra_auth(key) ) {
156 OnError(1);
157 return;
158 }
159 }
160
161 // UL-EV1 / NTAG authentication
162 if ( usePwd ) {
163 uint8_t pwd[4] = {0x00};
164 memcpy(pwd, datain, 4);
165 uint8_t pack[4] = {0,0,0,0};
166 if (!mifare_ul_ev1_auth(pwd, pack)) {
167 OnError(1);
168 return;
169 }
170 }
171
172 if( mifare_ultra_readblock(blockNo, dataout) ) {
173 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");
174 OnError(2);
175 return;
176 }
177
178 if( mifare_ultra_halt() ) {
179 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
180 OnError(3);
181 return;
182 }
183
184 cmd_send(CMD_ACK,1,0,0,dataout,16);
185 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
186 LEDsoff();
187 }
188
189 //-----------------------------------------------------------------------------
190 // Select, Authenticate, Read a MIFARE tag.
191 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
192 //-----------------------------------------------------------------------------
193 void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
194 {
195 // params
196 uint8_t sectorNo = arg0;
197 uint8_t keyType = arg1;
198 uint64_t ui64Key = 0;
199 ui64Key = bytes_to_num(datain, 6);
200
201 // variables
202 byte_t isOK = 0;
203 byte_t dataoutbuf[16 * 16];
204 uint8_t uid[10];
205 uint32_t cuid;
206 struct Crypto1State mpcs = {0, 0};
207 struct Crypto1State *pcs;
208 pcs = &mpcs;
209
210 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
211
212 clear_trace();
213
214 LED_A_ON();
215 LED_B_OFF();
216 LED_C_OFF();
217
218 isOK = 1;
219 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
220 isOK = 0;
221 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
222 }
223
224
225 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
226 isOK = 0;
227 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
228 }
229
230 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
231 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {
232 isOK = 0;
233 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);
234 break;
235 }
236 }
237
238 if(mifare_classic_halt(pcs, cuid)) {
239 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
240 }
241
242 // ----------------------------- crypto1 destroy
243 crypto1_destroy(pcs);
244
245 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");
246
247 LED_B_ON();
248 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));
249 LED_B_OFF();
250
251 // Thats it...
252 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
253 LEDsoff();
254 }
255
256 // arg0 = blockNo (start)
257 // arg1 = Pages (number of blocks)
258 // arg2 = useKey
259 // datain = KEY bytes
260 void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
261 {
262 LEDsoff();
263 LED_A_ON();
264 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
265
266 // free eventually allocated BigBuf memory
267 BigBuf_free();
268 clear_trace();
269
270 // params
271 uint8_t blockNo = arg0;
272 uint16_t blocks = arg1;
273 bool useKey = (arg2 == 1); //UL_C
274 bool usePwd = (arg2 == 2); //UL_EV1/NTAG
275 uint32_t countblocks = 0;
276 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);
277 if (dataout == NULL){
278 Dbprintf("out of memory");
279 OnError(1);
280 return;
281 }
282
283 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);
284 if (!len) {
285 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);
286 OnError(1);
287 return;
288 }
289
290 // UL-C authentication
291 if ( useKey ) {
292 uint8_t key[16] = {0x00};
293 memcpy(key, datain, sizeof(key) );
294
295 if ( !mifare_ultra_auth(key) ) {
296 OnError(1);
297 return;
298 }
299 }
300
301 // UL-EV1 / NTAG authentication
302 if (usePwd) {
303 uint8_t pwd[4] = {0x00};
304 memcpy(pwd, datain, sizeof(pwd));
305 uint8_t pack[4] = {0,0,0,0};
306
307 if (!mifare_ul_ev1_auth(pwd, pack)){
308 OnError(1);
309 return;
310 }
311 }
312
313 for (int i = 0; i < blocks; i++){
314 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {
315 Dbprintf("Data exceeds buffer!!");
316 break;
317 }
318
319 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);
320
321 if (len) {
322 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);
323 // if no blocks read - error out
324 if (i==0){
325 OnError(2);
326 return;
327 } else {
328 //stop at last successful read block and return what we got
329 break;
330 }
331 } else {
332 countblocks++;
333 }
334 }
335
336 len = mifare_ultra_halt();
337 if (len) {
338 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
339 OnError(3);
340 return;
341 }
342
343 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);
344
345 countblocks *= 4;
346
347 cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);
348 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
349 LEDsoff();
350 BigBuf_free();
351 }
352
353 //-----------------------------------------------------------------------------
354 // Select, Authenticate, Write a MIFARE tag.
355 // read block
356 //-----------------------------------------------------------------------------
357 void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
358 {
359 // params
360 uint8_t blockNo = arg0;
361 uint8_t keyType = arg1;
362 uint64_t ui64Key = 0;
363 byte_t blockdata[16];
364
365 ui64Key = bytes_to_num(datain, 6);
366 memcpy(blockdata, datain + 10, 16);
367
368 // variables
369 byte_t isOK = 0;
370 uint8_t uid[10];
371 uint32_t cuid;
372 struct Crypto1State mpcs = {0, 0};
373 struct Crypto1State *pcs;
374 pcs = &mpcs;
375
376 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
377
378 clear_trace();
379
380 LED_A_ON();
381 LED_B_OFF();
382 LED_C_OFF();
383
384 while (true) {
385 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
386 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
387 break;
388 };
389
390 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
391 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
392 break;
393 };
394
395 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {
396 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
397 break;
398 };
399
400 if(mifare_classic_halt(pcs, cuid)) {
401 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
402 break;
403 };
404
405 isOK = 1;
406 break;
407 }
408
409 // ----------------------------- crypto1 destroy
410 crypto1_destroy(pcs);
411
412 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
413
414 LED_B_ON();
415 cmd_send(CMD_ACK,isOK,0,0,0,0);
416 LED_B_OFF();
417
418
419 // Thats it...
420 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
421 LEDsoff();
422 }
423
424 /* // Command not needed but left for future testing
425 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
426 {
427 uint8_t blockNo = arg0;
428 byte_t blockdata[16] = {0x00};
429
430 memcpy(blockdata, datain, 16);
431
432 uint8_t uid[10] = {0x00};
433
434 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
435
436 clear_trace();
437 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
438
439 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {
440 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
441 OnError(0);
442 return;
443 };
444
445 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
446 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
447 OnError(0);
448 return; };
449
450 if(mifare_ultra_halt()) {
451 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
452 OnError(0);
453 return;
454 };
455
456 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
457
458 cmd_send(CMD_ACK,1,0,0,0,0);
459 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
460 LEDsoff();
461 }
462 */
463
464 // Arg0 : Block to write to.
465 // Arg1 : 0 = use no authentication.
466 // 1 = use 0x1A authentication.
467 // 2 = use 0x1B authentication.
468 // datain : 4 first bytes is data to be written.
469 // : 4/16 next bytes is authentication key.
470 void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
471 {
472 uint8_t blockNo = arg0;
473 bool useKey = (arg1 == 1); //UL_C
474 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
475 byte_t blockdata[4] = {0x00};
476
477 memcpy(blockdata, datain,4);
478
479 LEDsoff();
480 LED_A_ON();
481 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
482
483 clear_trace();
484
485 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {
486 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
487 OnError(0);
488 return;
489 };
490
491 // UL-C authentication
492 if ( useKey ) {
493 uint8_t key[16] = {0x00};
494 memcpy(key, datain+4, sizeof(key) );
495
496 if ( !mifare_ultra_auth(key) ) {
497 OnError(1);
498 return;
499 }
500 }
501
502 // UL-EV1 / NTAG authentication
503 if (usePwd) {
504 uint8_t pwd[4] = {0x00};
505 memcpy(pwd, datain+4, 4);
506 uint8_t pack[4] = {0,0,0,0};
507 if (!mifare_ul_ev1_auth(pwd, pack)) {
508 OnError(1);
509 return;
510 }
511 }
512
513 if(mifare_ultra_writeblock(blockNo, blockdata)) {
514 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
515 OnError(0);
516 return;
517 };
518
519 if(mifare_ultra_halt()) {
520 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
521 OnError(0);
522 return;
523 };
524
525 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
526
527 cmd_send(CMD_ACK,1,0,0,0,0);
528 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
529 LEDsoff();
530 }
531
532 void MifareUSetPwd(uint8_t arg0, uint8_t *datain){
533
534 uint8_t pwd[16] = {0x00};
535 byte_t blockdata[4] = {0x00};
536
537 memcpy(pwd, datain, 16);
538
539 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
540 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
541
542 clear_trace();
543
544 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {
545 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
546 OnError(0);
547 return;
548 };
549
550 blockdata[0] = pwd[7];
551 blockdata[1] = pwd[6];
552 blockdata[2] = pwd[5];
553 blockdata[3] = pwd[4];
554 if(mifare_ultra_writeblock( 44, blockdata)) {
555 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
556 OnError(44);
557 return;
558 };
559
560 blockdata[0] = pwd[3];
561 blockdata[1] = pwd[2];
562 blockdata[2] = pwd[1];
563 blockdata[3] = pwd[0];
564 if(mifare_ultra_writeblock( 45, blockdata)) {
565 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
566 OnError(45);
567 return;
568 };
569
570 blockdata[0] = pwd[15];
571 blockdata[1] = pwd[14];
572 blockdata[2] = pwd[13];
573 blockdata[3] = pwd[12];
574 if(mifare_ultra_writeblock( 46, blockdata)) {
575 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
576 OnError(46);
577 return;
578 };
579
580 blockdata[0] = pwd[11];
581 blockdata[1] = pwd[10];
582 blockdata[2] = pwd[9];
583 blockdata[3] = pwd[8];
584 if(mifare_ultra_writeblock( 47, blockdata)) {
585 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
586 OnError(47);
587 return;
588 };
589
590 if(mifare_ultra_halt()) {
591 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
592 OnError(0);
593 return;
594 };
595
596 cmd_send(CMD_ACK,1,0,0,0,0);
597 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
598 LEDsoff();
599 }
600
601 // Return 1 if the nonce is invalid else return 0
602 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {
603 return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
604 (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \
605 (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;
606 }
607
608
609 //-----------------------------------------------------------------------------
610 // acquire encrypted nonces in order to perform the attack described in
611 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
612 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
613 // Computer and Communications Security, 2015
614 //-----------------------------------------------------------------------------
615 void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)
616 {
617 uint64_t ui64Key = 0;
618 uint8_t uid[10];
619 uint32_t cuid;
620 uint8_t cascade_levels = 0;
621 struct Crypto1State mpcs = {0, 0};
622 struct Crypto1State *pcs;
623 pcs = &mpcs;
624 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
625 int16_t isOK = 0;
626 uint8_t par_enc[1];
627 uint8_t nt_par_enc = 0;
628 uint8_t buf[USB_CMD_DATA_SIZE];
629 uint32_t timeout;
630
631 uint8_t blockNo = arg0 & 0xff;
632 uint8_t keyType = (arg0 >> 8) & 0xff;
633 uint8_t targetBlockNo = arg1 & 0xff;
634 uint8_t targetKeyType = (arg1 >> 8) & 0xff;
635 ui64Key = bytes_to_num(datain, 6);
636 bool initialize = flags & 0x0001;
637 bool slow = flags & 0x0002;
638 bool field_off = flags & 0x0004;
639
640 LED_A_ON();
641 LED_C_OFF();
642
643 if (initialize) {
644 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
645 clear_trace();
646 set_tracing(true);
647 }
648
649 LED_C_ON();
650
651 uint16_t num_nonces = 0;
652 bool have_uid = false;
653 for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {
654
655 // Test if the action was cancelled
656 if(BUTTON_PRESS()) {
657 isOK = 2;
658 field_off = true;
659 break;
660 }
661
662 if (!have_uid) { // need a full select cycle to get the uid first
663 iso14a_card_select_t card_info;
664 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0, true)) {
665 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");
666 continue;
667 }
668 switch (card_info.uidlen) {
669 case 4 : cascade_levels = 1; break;
670 case 7 : cascade_levels = 2; break;
671 case 10: cascade_levels = 3; break;
672 default: break;
673 }
674 have_uid = true;
675 } else { // no need for anticollision. We can directly select the card
676 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels, true)) {
677 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");
678 continue;
679 }
680 }
681
682 if (slow) {
683 timeout = GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME;
684 while(GetCountSspClk() < timeout);
685 }
686
687 uint32_t nt1;
688 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL)) {
689 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");
690 continue;
691 }
692
693 // nested authentication
694 uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);
695 if (len != 4) {
696 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);
697 continue;
698 }
699
700 // send an incomplete dummy response in order to trigger the card's authentication failure timeout
701 uint8_t dummy_answer[1] = {0};
702 ReaderTransmit(dummy_answer, 1, NULL);
703
704 timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT;
705
706 num_nonces++;
707 if (num_nonces % 2) {
708 memcpy(buf+i, receivedAnswer, 4);
709 nt_par_enc = par_enc[0] & 0xf0;
710 } else {
711 nt_par_enc |= par_enc[0] >> 4;
712 memcpy(buf+i+4, receivedAnswer, 4);
713 memcpy(buf+i+8, &nt_par_enc, 1);
714 i += 9;
715 }
716
717 // wait for the card to become ready again
718 while(GetCountSspClk() < timeout);
719
720 }
721
722 LED_C_OFF();
723
724 crypto1_destroy(pcs);
725
726 LED_B_ON();
727 cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));
728 LED_B_OFF();
729
730 if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");
731
732 if (field_off) {
733 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
734 LEDsoff();
735 }
736 }
737
738
739 //-----------------------------------------------------------------------------
740 // MIFARE nested authentication.
741 //
742 //-----------------------------------------------------------------------------
743 void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)
744 {
745 // params
746 uint8_t blockNo = arg0 & 0xff;
747 uint8_t keyType = (arg0 >> 8) & 0xff;
748 uint8_t targetBlockNo = arg1 & 0xff;
749 uint8_t targetKeyType = (arg1 >> 8) & 0xff;
750 uint64_t ui64Key = 0;
751
752 ui64Key = bytes_to_num(datain, 6);
753
754 // variables
755 uint16_t rtr, i, j, len;
756 uint16_t davg;
757 static uint16_t dmin, dmax;
758 uint8_t uid[10];
759 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;
760 uint8_t par[1];
761 uint32_t target_nt[2], target_ks[2];
762
763 uint8_t par_array[4];
764 uint16_t ncount = 0;
765 struct Crypto1State mpcs = {0, 0};
766 struct Crypto1State *pcs;
767 pcs = &mpcs;
768 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
769
770 uint32_t auth1_time, auth2_time;
771 static uint16_t delta_time;
772
773 LED_A_ON();
774 LED_C_OFF();
775 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
776
777 // free eventually allocated BigBuf memory
778 BigBuf_free();
779
780 if (calibrate) clear_trace();
781 set_tracing(true);
782
783 // statistics on nonce distance
784 int16_t isOK = 0;
785 #define NESTED_MAX_TRIES 12
786 uint16_t unsuccessfull_tries = 0;
787 if (calibrate) { // for first call only. Otherwise reuse previous calibration
788 LED_B_ON();
789 WDT_HIT();
790
791 davg = dmax = 0;
792 dmin = 2000;
793 delta_time = 0;
794
795 for (rtr = 0; rtr < 17; rtr++) {
796
797 // Test if the action was cancelled
798 if(BUTTON_PRESS()) {
799 isOK = -2;
800 break;
801 }
802
803 // prepare next select. No need to power down the card.
804 if(mifare_classic_halt(pcs, cuid)) {
805 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
806 rtr--;
807 continue;
808 }
809
810 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
811 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
812 rtr--;
813 continue;
814 };
815
816 auth1_time = 0;
817 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
818 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
819 rtr--;
820 continue;
821 };
822
823 if (delta_time) {
824 auth2_time = auth1_time + delta_time;
825 } else {
826 auth2_time = 0;
827 }
828 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {
829 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");
830 rtr--;
831 continue;
832 };
833
834 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
835 for (i = 101; i < 1200; i++) {
836 nttmp = prng_successor(nttmp, 1);
837 if (nttmp == nt2) break;
838 }
839
840 if (i != 1200) {
841 if (rtr != 0) {
842 davg += i;
843 dmin = MIN(dmin, i);
844 dmax = MAX(dmax, i);
845 }
846 else {
847 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing
848 }
849 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);
850 } else {
851 unsuccessfull_tries++;
852 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)
853 isOK = -3;
854 }
855 }
856 }
857
858 davg = (davg + (rtr - 1)/2) / (rtr - 1);
859
860 if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);
861
862 dmin = davg - 2;
863 dmax = davg + 2;
864
865 LED_B_OFF();
866
867 }
868 // -------------------------------------------------------------------------------------------------
869
870 LED_C_ON();
871
872 // get crypted nonces for target sector
873 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces
874
875 target_nt[i] = 0;
876 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce
877
878 // prepare next select. No need to power down the card.
879 if(mifare_classic_halt(pcs, cuid)) {
880 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
881 continue;
882 }
883
884 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
885 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
886 continue;
887 };
888
889 auth1_time = 0;
890 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
891 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
892 continue;
893 };
894
895 // nested authentication
896 auth2_time = auth1_time + delta_time;
897 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);
898 if (len != 4) {
899 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);
900 continue;
901 };
902
903 nt2 = bytes_to_num(receivedAnswer, 4);
904 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);
905
906 // Parity validity check
907 for (j = 0; j < 4; j++) {
908 par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));
909 }
910
911 ncount = 0;
912 nttest = prng_successor(nt1, dmin - 1);
913 for (j = dmin; j < dmax + 1; j++) {
914 nttest = prng_successor(nttest, 1);
915 ks1 = nt2 ^ nttest;
916
917 if (valid_nonce(nttest, nt2, ks1, par_array)){
918 if (ncount > 0) { // we are only interested in disambiguous nonces, try again
919 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);
920 target_nt[i] = 0;
921 break;
922 }
923 target_nt[i] = nttest;
924 target_ks[i] = ks1;
925 ncount++;
926 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces
927 target_nt[i] = 0;
928 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);
929 break;
930 }
931 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);
932 }
933 }
934 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);
935 }
936 }
937
938 LED_C_OFF();
939
940 // ----------------------------- crypto1 destroy
941 crypto1_destroy(pcs);
942
943 byte_t buf[4 + 4 * 4];
944 memcpy(buf, &cuid, 4);
945 memcpy(buf+4, &target_nt[0], 4);
946 memcpy(buf+8, &target_ks[0], 4);
947 memcpy(buf+12, &target_nt[1], 4);
948 memcpy(buf+16, &target_ks[1], 4);
949
950 LED_B_ON();
951 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));
952 LED_B_OFF();
953
954 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");
955
956 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
957 LEDsoff();
958 }
959
960 //-----------------------------------------------------------------------------
961 // MIFARE check keys. key count up to 85.
962 //
963 //-----------------------------------------------------------------------------
964 void MifareChkKeys(uint16_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
965 {
966 uint8_t blockNo = arg0 & 0xff;
967 uint8_t keyType = (arg0 >> 8) & 0xff;
968 bool clearTrace = arg1 & 0x01;
969 bool multisectorCheck = arg1 & 0x02;
970 uint8_t set14aTimeout = (arg1 >> 8) & 0xff;
971 uint8_t keyCount = arg2;
972
973 // clear debug level
974 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
975 MF_DBGLEVEL = MF_DBG_NONE;
976
977 LED_A_ON();
978 LED_B_OFF();
979 LED_C_OFF();
980 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
981
982 if (clearTrace) clear_trace();
983 set_tracing(true);
984
985 if (set14aTimeout){
986 iso14a_set_timeout(set14aTimeout * 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
987 }
988
989 if (multisectorCheck) {
990 TKeyIndex keyIndex = {{0}};
991 uint8_t sectorCnt = blockNo;
992 int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);
993
994 LED_B_ON();
995 if (res >= 0) {
996 cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80);
997 } else {
998 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);
999 }
1000 LED_B_OFF();
1001 } else {
1002 int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);
1003
1004 LED_B_ON();
1005 if (res > 0) {
1006 cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);
1007 } else {
1008 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);
1009 }
1010 LED_B_OFF();
1011 }
1012
1013 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1014 LEDsoff();
1015
1016 // restore debug level
1017 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
1018 }
1019
1020 //-----------------------------------------------------------------------------
1021 // MIFARE commands set debug level
1022 //
1023 //-----------------------------------------------------------------------------
1024 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1025 MF_DBGLEVEL = arg0;
1026 Dbprintf("Debug level: %d", MF_DBGLEVEL);
1027 }
1028
1029 //-----------------------------------------------------------------------------
1030 // Work with emulator memory
1031 //
1032 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1033 // involved in dealing with emulator memory. But if it is called later, it might
1034 // destroy the Emulator Memory.
1035 //-----------------------------------------------------------------------------
1036
1037 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1038 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1039 emlClearMem();
1040 }
1041
1042 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1043 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1044 emlSetMem(datain, arg0, arg1); // data, block num, blocks count
1045 }
1046
1047 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1048 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1049 byte_t buf[USB_CMD_DATA_SIZE];
1050 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)
1051
1052 LED_B_ON();
1053 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);
1054 LED_B_OFF();
1055 }
1056
1057 //-----------------------------------------------------------------------------
1058 // Load a card into the emulator memory
1059 //
1060 //-----------------------------------------------------------------------------
1061 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1062 uint8_t numSectors = arg0;
1063 uint8_t keyType = arg1;
1064 uint64_t ui64Key = 0;
1065 uint32_t cuid;
1066 struct Crypto1State mpcs = {0, 0};
1067 struct Crypto1State *pcs;
1068 pcs = &mpcs;
1069
1070 // variables
1071 byte_t dataoutbuf[16];
1072 byte_t dataoutbuf2[16];
1073 uint8_t uid[10];
1074
1075 LED_A_ON();
1076 LED_B_OFF();
1077 LED_C_OFF();
1078 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1079
1080 clear_trace();
1081 set_tracing(false);
1082
1083 bool isOK = true;
1084
1085 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
1086 isOK = false;
1087 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1088 }
1089
1090 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
1091 ui64Key = emlGetKey(sectorNo, keyType);
1092 if (sectorNo == 0){
1093 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
1094 isOK = false;
1095 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);
1096 break;
1097 }
1098 } else {
1099 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {
1100 isOK = false;
1101 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);
1102 break;
1103 }
1104 }
1105
1106 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
1107 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {
1108 isOK = false;
1109 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);
1110 break;
1111 };
1112 if (isOK) {
1113 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {
1114 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);
1115 } else { // sector trailer, keep the keys, set only the AC
1116 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
1117 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
1118 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
1119 }
1120 }
1121 }
1122
1123 }
1124
1125 if(mifare_classic_halt(pcs, cuid)) {
1126 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1127 };
1128
1129 // ----------------------------- crypto1 destroy
1130 crypto1_destroy(pcs);
1131
1132 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1133 LEDsoff();
1134
1135 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
1136
1137 }
1138
1139
1140 //-----------------------------------------------------------------------------
1141 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1142 //
1143 //-----------------------------------------------------------------------------
1144
1145 static bool isBlockTrailer(int blockN) {
1146 if (blockN >= 0 && blockN < 128) {
1147 return ((blockN & 0x03) == 0x03);
1148 }
1149 if (blockN >= 128 && blockN <= 256) {
1150 return ((blockN & 0x0F) == 0x0F);
1151 }
1152 return false;
1153 }
1154
1155 void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1156 // var
1157 byte_t isOK = 0;
1158 uint32_t numBlocks = arg0;
1159 // cmdParams:
1160 // bit 0 - wipe gen1a
1161 // bit 1 - fill card with default data
1162 // bit 2 - gen1a = 0, gen1b = 1
1163 uint8_t cmdParams = arg1;
1164 bool needWipe = cmdParams & 0x01;
1165 bool needFill = cmdParams & 0x02;
1166 bool gen1b = cmdParams & 0x04;
1167
1168 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1169 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1170
1171 uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1172 uint8_t block1[16] = {0x00};
1173 uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1174 uint8_t d_block[18] = {0x00};
1175
1176 // card commands
1177 uint8_t wupC1[] = { 0x40 };
1178 uint8_t wupC2[] = { 0x43 };
1179 uint8_t wipeC[] = { 0x41 };
1180
1181 // iso14443 setup
1182 LED_A_ON();
1183 LED_B_OFF();
1184 LED_C_OFF();
1185 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1186
1187 // tracing
1188 clear_trace();
1189 set_tracing(true);
1190
1191 while (true){
1192 // wipe
1193 if (needWipe){
1194 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1195 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1196 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1197 break;
1198 };
1199
1200 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1201 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1202 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1203 break;
1204 };
1205
1206 if(mifare_classic_halt(NULL, 0)) {
1207 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1208 };
1209 };
1210
1211 // put default data
1212 if (needFill){
1213 // select commands
1214 ReaderTransmitBitsPar(wupC1, 7, 0, NULL);
1215
1216 // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1217 if (!gen1b) {
1218
1219 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1220 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1221 break;
1222 };
1223
1224 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1225 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1226 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1227 break;
1228 };
1229 }
1230
1231 // send blocks command
1232 for (int blockNo = 0; blockNo < numBlocks; blockNo++) {
1233 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1234 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1235 break;
1236 };
1237
1238 // check type of block and add crc
1239 if (!isBlockTrailer(blockNo)){
1240 memcpy(d_block, block1, 16);
1241 } else {
1242 memcpy(d_block, blockK, 16);
1243 }
1244 if (blockNo == 0) {
1245 memcpy(d_block, block0, 16);
1246 }
1247 AppendCrc14443a(d_block, 16);
1248
1249 // send write command
1250 ReaderTransmit(d_block, sizeof(d_block), NULL);
1251 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1252 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1253 break;
1254 };
1255 }
1256
1257 // halt
1258 // do no issue halt command for gen1b
1259 if (!gen1b) {
1260 if (mifare_classic_halt(NULL, 0)) {
1261 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1262 break;
1263 }
1264 }
1265 }
1266 break;
1267 }
1268
1269 // send USB response
1270 LED_B_ON();
1271 cmd_send(CMD_ACK,isOK,0,0,NULL,0);
1272 LED_B_OFF();
1273
1274 // reset fpga
1275 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1276 LEDsoff();
1277
1278 return;
1279 }
1280
1281 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1282
1283 // params
1284 uint8_t needWipe = arg0;
1285 // bit 0 - need get UID
1286 // bit 1 - need wupC
1287 // bit 2 - need HALT after sequence
1288 // bit 3 - need init FPGA and field before sequence
1289 // bit 4 - need reset FPGA and LED
1290 // bit 6 - gen1b backdoor type
1291 uint8_t workFlags = arg1;
1292 uint8_t blockNo = arg2;
1293
1294 // card commands
1295 uint8_t wupC1[] = { 0x40 };
1296 uint8_t wupC2[] = { 0x43 };
1297 uint8_t wipeC[] = { 0x41 };
1298
1299 // variables
1300 byte_t isOK = 0;
1301 uint8_t uid[10] = {0x00};
1302 uint8_t d_block[18] = {0x00};
1303 uint32_t cuid;
1304
1305 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1306 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1307
1308 // reset FPGA and LED
1309 if (workFlags & 0x08) {
1310 LED_A_ON();
1311 LED_B_OFF();
1312 LED_C_OFF();
1313 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1314
1315 clear_trace();
1316 set_tracing(true);
1317 }
1318
1319 while (true) {
1320
1321 // get UID from chip
1322 if (workFlags & 0x01) {
1323 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
1324 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1325 // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.
1326 //break;
1327 };
1328
1329 if(mifare_classic_halt(NULL, cuid)) {
1330 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1331 // Continue, some magic tags misbehavies and send an answer to it.
1332 // break;
1333 };
1334 };
1335
1336 // reset chip
1337 // Wipe command don't work with gen1b
1338 if (needWipe && !(workFlags & 0x40)){
1339 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1340 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1341 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1342 break;
1343 };
1344
1345 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1346 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1347 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1348 break;
1349 };
1350
1351 if(mifare_classic_halt(NULL, 0)) {
1352 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1353 // Continue, some magic tags misbehavies and send an answer to it.
1354 // break;
1355 };
1356 };
1357
1358 // write block
1359 if (workFlags & 0x02) {
1360 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1361
1362 // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)
1363 if (!(workFlags & 0x40)) {
1364
1365 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1366 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1367 break;
1368 };
1369
1370 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1371 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1372 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1373 break;
1374 };
1375 }
1376 }
1377
1378 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1379 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1380 break;
1381 };
1382
1383 memcpy(d_block, datain, 16);
1384 AppendCrc14443a(d_block, 16);
1385
1386 ReaderTransmit(d_block, sizeof(d_block), NULL);
1387 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1388 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1389 break;
1390 };
1391
1392 if (workFlags & 0x04) {
1393 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1394 if (!(workFlags & 0x40)) {
1395 if (mifare_classic_halt(NULL, 0)) {
1396 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1397 // Continue, some magic tags misbehavies and send an answer to it.
1398 // break;
1399 }
1400 }
1401 }
1402
1403 isOK = 1;
1404 break;
1405 }
1406
1407 LED_B_ON();
1408 cmd_send(CMD_ACK,isOK,0,0,uid,4);
1409 LED_B_OFF();
1410
1411 if ((workFlags & 0x10) || (!isOK)) {
1412 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1413 LEDsoff();
1414 }
1415 }
1416
1417
1418 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1419
1420 // params
1421 // bit 1 - need wupC
1422 // bit 2 - need HALT after sequence
1423 // bit 3 - need init FPGA and field before sequence
1424 // bit 4 - need reset FPGA and LED
1425 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1426 // bit 6 - gen1b backdoor type
1427 uint8_t workFlags = arg0;
1428 uint8_t blockNo = arg2;
1429
1430 // card commands
1431 uint8_t wupC1[] = { 0x40 };
1432 uint8_t wupC2[] = { 0x43 };
1433
1434 // variables
1435 byte_t isOK = 0;
1436 uint8_t data[18] = {0x00};
1437 uint32_t cuid = 0;
1438
1439 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1440 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1441
1442 if (workFlags & 0x08) {
1443 LED_A_ON();
1444 LED_B_OFF();
1445 LED_C_OFF();
1446 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1447
1448 clear_trace();
1449 set_tracing(true);
1450 }
1451
1452 while (true) {
1453 if (workFlags & 0x02) {
1454 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1455 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1456 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1457 break;
1458 };
1459 // do no issue for gen1b magic tag
1460 if (!(workFlags & 0x40)) {
1461 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1462 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1463 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1464 break;
1465 };
1466 }
1467 }
1468
1469 // read block
1470 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {
1471 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1472 break;
1473 };
1474 memcpy(data, receivedAnswer, 18);
1475
1476 if (workFlags & 0x04) {
1477 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1478 if (!(workFlags & 0x40)) {
1479 if (mifare_classic_halt(NULL, cuid)) {
1480 if (MF_DBGLEVEL > 1) Dbprintf("Halt error");
1481 // Continue, some magic tags misbehavies and send an answer to it.
1482 // break;
1483 }
1484 }
1485 }
1486
1487 isOK = 1;
1488 break;
1489 }
1490
1491 LED_B_ON();
1492 if (workFlags & 0x20) {
1493 if (isOK)
1494 memcpy(datain, data, 18);
1495 }
1496 else
1497 cmd_send(CMD_ACK,isOK,0,0,data,18);
1498 LED_B_OFF();
1499
1500 if ((workFlags & 0x10) || (!isOK)) {
1501 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1502 LEDsoff();
1503 }
1504 }
1505
1506 void MifareCIdent(){
1507
1508 // card commands
1509 uint8_t wupC1[] = { 0x40 };
1510 uint8_t wupC2[] = { 0x43 };
1511
1512 // variables
1513 byte_t isOK = 0;
1514
1515 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1516 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1517
1518 LED_A_ON();
1519 LED_B_OFF();
1520 LED_C_OFF();
1521 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1522
1523 clear_trace();
1524 set_tracing(true);
1525
1526 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1527 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {
1528 isOK = 2;
1529
1530 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1531 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {
1532 isOK = 1;
1533 };
1534 };
1535
1536 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1537 mifare_classic_halt(NULL, 0);
1538
1539 LED_B_ON();
1540 cmd_send(CMD_ACK,isOK,0,0,0,0);
1541 LED_B_OFF();
1542
1543 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1544 LEDsoff();
1545 }
1546
1547 //
1548 // DESFIRE
1549 //
1550
1551 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
1552
1553 byte_t dataout[11] = {0x00};
1554 uint8_t uid[10] = {0x00};
1555 uint32_t cuid;
1556
1557 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1558 clear_trace();
1559
1560 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);
1561 if(!len) {
1562 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
1563 OnError(1);
1564 return;
1565 };
1566
1567 if(mifare_desfire_des_auth1(cuid, dataout)){
1568 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");
1569 OnError(4);
1570 return;
1571 }
1572
1573 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");
1574 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));
1575 }
1576
1577 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){
1578
1579 uint32_t cuid = arg0;
1580 uint8_t key[16] = {0x00};
1581 byte_t isOK = 0;
1582 byte_t dataout[12] = {0x00};
1583
1584 memcpy(key, datain, 16);
1585
1586 isOK = mifare_desfire_des_auth2(cuid, key, dataout);
1587
1588 if( isOK) {
1589 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");
1590 OnError(4);
1591 return;
1592 }
1593
1594 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");
1595
1596 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));
1597 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1598 LEDsoff();
1599 }
1600
1601 void OnSuccess(){
1602 pcb_blocknum = 0;
1603 ReaderTransmit(deselect_cmd, 3 , NULL);
1604 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1605 LEDsoff();
1606 }
1607
1608 void OnError(uint8_t reason){
1609 pcb_blocknum = 0;
1610 ReaderTransmit(deselect_cmd, 3 , NULL);
1611 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1612 cmd_send(CMD_ACK,0,reason,0,0,0);
1613 LEDsoff();
1614 }
Impressum, Datenschutz