]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
8fe502ca3135c6d184cabcac2677fb5b41202e76
[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 LED_A_ON();
984
985 // clear debug level
986 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
987 MF_DBGLEVEL = MF_DBG_NONE;
988
989 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
990
991 if (clearTrace) {
992 clear_trace();
993 }
994 set_tracing(true);
995
996 if (set14aTimeout){
997 iso14a_set_timeout(set14aTimeout * 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe
998 }
999
1000 if (multisectorCheck) {
1001 TKeyIndex keyIndex = {{0}};
1002 uint8_t sectorCnt = blockNo;
1003 int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);
1004
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 } else {
1011 int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);
1012
1013 if (res > 0) {
1014 cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);
1015 } else {
1016 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);
1017 }
1018 }
1019
1020 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1021 LED_D_OFF();
1022
1023 // restore debug level
1024 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
1025
1026 LED_A_OFF();
1027 }
1028
1029 //-----------------------------------------------------------------------------
1030 // MIFARE commands set debug level
1031 //
1032 //-----------------------------------------------------------------------------
1033 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1034 MF_DBGLEVEL = arg0;
1035 Dbprintf("Debug level: %d", MF_DBGLEVEL);
1036 }
1037
1038 //-----------------------------------------------------------------------------
1039 // Work with emulator memory
1040 //
1041 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1042 // involved in dealing with emulator memory. But if it is called later, it might
1043 // destroy the Emulator Memory.
1044 //-----------------------------------------------------------------------------
1045
1046 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1047 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1048 emlClearMem();
1049 }
1050
1051 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1052 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1053 emlSetMem(datain, arg0, arg1); // data, block num, blocks count
1054 }
1055
1056 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1057 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1058 byte_t buf[USB_CMD_DATA_SIZE];
1059 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)
1060
1061 LED_B_ON();
1062 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);
1063 LED_B_OFF();
1064 }
1065
1066 //-----------------------------------------------------------------------------
1067 // Load a card into the emulator memory
1068 //
1069 //-----------------------------------------------------------------------------
1070 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1071 uint8_t numSectors = arg0;
1072 uint8_t keyType = arg1;
1073 uint64_t ui64Key = 0;
1074 uint32_t cuid;
1075 struct Crypto1State mpcs = {0, 0};
1076 struct Crypto1State *pcs;
1077 pcs = &mpcs;
1078
1079 // variables
1080 byte_t dataoutbuf[16];
1081 byte_t dataoutbuf2[16];
1082 uint8_t uid[10];
1083
1084 LED_A_ON();
1085 LED_B_OFF();
1086 LED_C_OFF();
1087 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1088
1089 clear_trace();
1090 set_tracing(false);
1091
1092 bool isOK = true;
1093
1094 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
1095 isOK = false;
1096 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1097 }
1098
1099 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
1100 ui64Key = emlGetKey(sectorNo, keyType);
1101 if (sectorNo == 0){
1102 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
1103 isOK = false;
1104 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);
1105 break;
1106 }
1107 } else {
1108 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {
1109 isOK = false;
1110 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);
1111 break;
1112 }
1113 }
1114
1115 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
1116 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {
1117 isOK = false;
1118 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);
1119 break;
1120 };
1121 if (isOK) {
1122 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {
1123 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);
1124 } else { // sector trailer, keep the keys, set only the AC
1125 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
1126 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
1127 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
1128 }
1129 }
1130 }
1131
1132 }
1133
1134 if(mifare_classic_halt(pcs, cuid)) {
1135 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1136 };
1137
1138 // ----------------------------- crypto1 destroy
1139 crypto1_destroy(pcs);
1140
1141 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1142 LEDsoff();
1143
1144 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
1145
1146 }
1147
1148
1149 //-----------------------------------------------------------------------------
1150 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1151 //
1152 //-----------------------------------------------------------------------------
1153
1154 static bool isBlockTrailer(int blockN) {
1155 if (blockN >= 0 && blockN < 128) {
1156 return ((blockN & 0x03) == 0x03);
1157 }
1158 if (blockN >= 128 && blockN <= 256) {
1159 return ((blockN & 0x0F) == 0x0F);
1160 }
1161 return false;
1162 }
1163
1164 void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1165 // var
1166 byte_t isOK = 0;
1167 uint32_t numBlocks = arg0;
1168 // cmdParams:
1169 // bit 0 - wipe gen1a
1170 // bit 1 - fill card with default data
1171 // bit 2 - gen1a = 0, gen1b = 1
1172 uint8_t cmdParams = arg1;
1173 bool needWipe = cmdParams & 0x01;
1174 bool needFill = cmdParams & 0x02;
1175 bool gen1b = cmdParams & 0x04;
1176
1177 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1178 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1179
1180 uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};
1181 uint8_t block1[16] = {0x00};
1182 uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1183 uint8_t d_block[18] = {0x00};
1184
1185 // card commands
1186 uint8_t wupC1[] = { 0x40 };
1187 uint8_t wupC2[] = { 0x43 };
1188 uint8_t wipeC[] = { 0x41 };
1189
1190 // iso14443 setup
1191 LED_A_ON();
1192 LED_B_OFF();
1193 LED_C_OFF();
1194 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1195
1196 // tracing
1197 clear_trace();
1198 set_tracing(true);
1199
1200 while (true){
1201 // wipe
1202 if (needWipe){
1203 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1204 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1205 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1206 break;
1207 };
1208
1209 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1210 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1211 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1212 break;
1213 };
1214
1215 if(mifare_classic_halt(NULL, 0)) {
1216 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1217 };
1218 };
1219
1220 // put default data
1221 if (needFill){
1222 // select commands
1223 ReaderTransmitBitsPar(wupC1, 7, 0, NULL);
1224
1225 // 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)
1226 if (!gen1b) {
1227
1228 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1229 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1230 break;
1231 };
1232
1233 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1234 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1235 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1236 break;
1237 };
1238 }
1239
1240 // send blocks command
1241 for (int blockNo = 0; blockNo < numBlocks; blockNo++) {
1242 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1243 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1244 break;
1245 };
1246
1247 // check type of block and add crc
1248 if (!isBlockTrailer(blockNo)){
1249 memcpy(d_block, block1, 16);
1250 } else {
1251 memcpy(d_block, blockK, 16);
1252 }
1253 if (blockNo == 0) {
1254 memcpy(d_block, block0, 16);
1255 }
1256 AppendCrc14443a(d_block, 16);
1257
1258 // send write command
1259 ReaderTransmit(d_block, sizeof(d_block), NULL);
1260 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1261 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1262 break;
1263 };
1264 }
1265
1266 // halt
1267 // do no issue halt command for gen1b
1268 if (!gen1b) {
1269 if (mifare_classic_halt(NULL, 0)) {
1270 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1271 break;
1272 }
1273 }
1274 }
1275 break;
1276 }
1277
1278 // send USB response
1279 LED_B_ON();
1280 cmd_send(CMD_ACK,isOK,0,0,NULL,0);
1281 LED_B_OFF();
1282
1283 // reset fpga
1284 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1285 LEDsoff();
1286
1287 return;
1288 }
1289
1290 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1291
1292 // params
1293 uint8_t needWipe = arg0;
1294 // bit 0 - need get UID
1295 // bit 1 - need wupC
1296 // bit 2 - need HALT after sequence
1297 // bit 3 - need init FPGA and field before sequence
1298 // bit 4 - need reset FPGA and LED
1299 // bit 6 - gen1b backdoor type
1300 uint8_t workFlags = arg1;
1301 uint8_t blockNo = arg2;
1302
1303 // card commands
1304 uint8_t wupC1[] = { 0x40 };
1305 uint8_t wupC2[] = { 0x43 };
1306 uint8_t wipeC[] = { 0x41 };
1307
1308 // variables
1309 byte_t isOK = 0;
1310 uint8_t uid[10] = {0x00};
1311 uint8_t d_block[18] = {0x00};
1312 uint32_t cuid;
1313
1314 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1315 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1316
1317 // reset FPGA and LED
1318 if (workFlags & 0x08) {
1319 LED_A_ON();
1320 LED_B_OFF();
1321 LED_C_OFF();
1322 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1323
1324 clear_trace();
1325 set_tracing(true);
1326 }
1327
1328 while (true) {
1329
1330 // get UID from chip
1331 if (workFlags & 0x01) {
1332 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {
1333 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1334 // 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.
1335 //break;
1336 };
1337
1338 if(mifare_classic_halt(NULL, cuid)) {
1339 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1340 // Continue, some magic tags misbehavies and send an answer to it.
1341 // break;
1342 };
1343 };
1344
1345 // reset chip
1346 // Wipe command don't work with gen1b
1347 if (needWipe && !(workFlags & 0x40)){
1348 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1349 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1350 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1351 break;
1352 };
1353
1354 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1355 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1356 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1357 break;
1358 };
1359
1360 if(mifare_classic_halt(NULL, 0)) {
1361 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1362 // Continue, some magic tags misbehavies and send an answer to it.
1363 // break;
1364 };
1365 };
1366
1367 // write block
1368 if (workFlags & 0x02) {
1369 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1370
1371 // 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)
1372 if (!(workFlags & 0x40)) {
1373
1374 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1375 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1376 break;
1377 };
1378
1379 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1380 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1381 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1382 break;
1383 };
1384 }
1385 }
1386
1387 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1388 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1389 break;
1390 };
1391
1392 memcpy(d_block, datain, 16);
1393 AppendCrc14443a(d_block, 16);
1394
1395 ReaderTransmit(d_block, sizeof(d_block), NULL);
1396 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1397 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1398 break;
1399 };
1400
1401 if (workFlags & 0x04) {
1402 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1403 if (!(workFlags & 0x40)) {
1404 if (mifare_classic_halt(NULL, 0)) {
1405 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");
1406 // Continue, some magic tags misbehavies and send an answer to it.
1407 // break;
1408 }
1409 }
1410 }
1411
1412 isOK = 1;
1413 break;
1414 }
1415
1416 LED_B_ON();
1417 cmd_send(CMD_ACK,isOK,0,0,uid,4);
1418 LED_B_OFF();
1419
1420 if ((workFlags & 0x10) || (!isOK)) {
1421 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1422 LEDsoff();
1423 }
1424 }
1425
1426
1427 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1428
1429 // params
1430 // bit 1 - need wupC
1431 // bit 2 - need HALT after sequence
1432 // bit 3 - need init FPGA and field before sequence
1433 // bit 4 - need reset FPGA and LED
1434 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1435 // bit 6 - gen1b backdoor type
1436 uint8_t workFlags = arg0;
1437 uint8_t blockNo = arg2;
1438
1439 // card commands
1440 uint8_t wupC1[] = { 0x40 };
1441 uint8_t wupC2[] = { 0x43 };
1442
1443 // variables
1444 byte_t isOK = 0;
1445 uint8_t data[18] = {0x00};
1446 uint32_t cuid = 0;
1447
1448 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1449 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1450
1451 if (workFlags & 0x08) {
1452 LED_A_ON();
1453 LED_B_OFF();
1454 LED_C_OFF();
1455 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1456
1457 clear_trace();
1458 set_tracing(true);
1459 }
1460
1461 while (true) {
1462 if (workFlags & 0x02) {
1463 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1464 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1465 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1466 break;
1467 };
1468 // do no issue for gen1b magic tag
1469 if (!(workFlags & 0x40)) {
1470 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1471 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1472 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1473 break;
1474 };
1475 }
1476 }
1477
1478 // read block
1479 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {
1480 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1481 break;
1482 };
1483 memcpy(data, receivedAnswer, 18);
1484
1485 if (workFlags & 0x04) {
1486 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)
1487 if (!(workFlags & 0x40)) {
1488 if (mifare_classic_halt(NULL, cuid)) {
1489 if (MF_DBGLEVEL > 1) Dbprintf("Halt error");
1490 // Continue, some magic tags misbehavies and send an answer to it.
1491 // break;
1492 }
1493 }
1494 }
1495
1496 isOK = 1;
1497 break;
1498 }
1499
1500 LED_B_ON();
1501 if (workFlags & 0x20) {
1502 if (isOK)
1503 memcpy(datain, data, 18);
1504 }
1505 else
1506 cmd_send(CMD_ACK,isOK,0,0,data,18);
1507 LED_B_OFF();
1508
1509 if ((workFlags & 0x10) || (!isOK)) {
1510 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1511 LEDsoff();
1512 }
1513 }
1514
1515 void MifareCIdent(){
1516
1517 // card commands
1518 uint8_t wupC1[] = { 0x40 };
1519 uint8_t wupC2[] = { 0x43 };
1520
1521 // variables
1522 byte_t isOK = 0;
1523
1524 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1525 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1526
1527 LED_A_ON();
1528 LED_B_OFF();
1529 LED_C_OFF();
1530 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1531
1532 clear_trace();
1533 set_tracing(true);
1534
1535 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1536 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {
1537 isOK = 2;
1538
1539 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1540 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {
1541 isOK = 1;
1542 };
1543 };
1544
1545 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.
1546 mifare_classic_halt(NULL, 0);
1547
1548 LED_B_ON();
1549 cmd_send(CMD_ACK,isOK,0,0,0,0);
1550 LED_B_OFF();
1551
1552 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1553 LEDsoff();
1554 }
1555
1556 //
1557 // DESFIRE
1558 //
1559
1560 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
1561
1562 byte_t dataout[11] = {0x00};
1563 uint8_t uid[10] = {0x00};
1564 uint32_t cuid;
1565
1566 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1567 clear_trace();
1568
1569 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);
1570 if(!len) {
1571 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
1572 OnError(1);
1573 return;
1574 };
1575
1576 if(mifare_desfire_des_auth1(cuid, dataout)){
1577 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");
1578 OnError(4);
1579 return;
1580 }
1581
1582 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");
1583 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));
1584 }
1585
1586 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){
1587
1588 uint32_t cuid = arg0;
1589 uint8_t key[16] = {0x00};
1590 byte_t isOK = 0;
1591 byte_t dataout[12] = {0x00};
1592
1593 memcpy(key, datain, 16);
1594
1595 isOK = mifare_desfire_des_auth2(cuid, key, dataout);
1596
1597 if( isOK) {
1598 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");
1599 OnError(4);
1600 return;
1601 }
1602
1603 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");
1604
1605 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));
1606 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1607 LEDsoff();
1608 }
1609
Impressum, Datenschutz