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