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