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