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