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