1 //-----------------------------------------------------------------------------
2 // Copyright (C) Gerhard de Koning Gans - May 2008
3 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
18 #include "mifarecmd.h"
21 #include "proxmark3_arm.h"
23 #include "mifareutil.h"
24 #include "protocols.h"
29 #include "fpgaloader.h"
30 #include "iso14443a.h"
31 #include "mifaredesfire.h"
33 #include "commonutil.h"
37 #include "usb_cdc.h" // usb_poll_validate_length
38 #include "spiffs.h" // spiffs
39 #include "appmain.h" // print_stack_usage
41 #ifndef HARDNESTED_AUTHENTICATION_TIMEOUT
42 # define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
44 #ifndef HARDNESTED_PRE_AUTHENTICATION_LEADTIME
45 # define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
48 // send an incomplete dummy response in order to trigger the card's authentication failure timeout
50 # define CHK_TIMEOUT(void) { \
51 ReaderTransmit(&dummy_answer, 1, NULL); \
52 uint32_t timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT; \
53 while (GetCountSspClk() < timeout) {}; \
57 static uint8_t dummy_answer
= 0;
59 // magic uid card generation 1 commands
60 static uint8_t wupC1
[] = { MIFARE_MAGICWUPC1
};
61 static uint8_t wupC2
[] = { MIFARE_MAGICWUPC2
};
62 static uint8_t wipeC
[] = { MIFARE_MAGICWIPEC
};
63 // GDM alt magic wakeup
64 static uint8_t wupGDM1
[] = { MIFARE_MAGIC_GDM_WUPC1
};
65 static uint8_t wupGDM2
[] = { MIFARE_MAGIC_GDM_WUPC2
};
67 static bool mifare_wakeup_auth(struct Crypto1State
*pcs
, MifareWakeupType wakeup
, uint8_t key_auth_cmd
, uint8_t *key
, uint8_t block_no
) {
69 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
70 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
72 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
85 if (!iso14443a_select_cardEx(NULL
, NULL
, &cuid
, true, 0, true, &WUPA_POLLING_PARAMETERS
)) {
86 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
91 if (!iso14443a_select_cardEx(NULL
, NULL
, &cuid
, true, 0, true, &REQA_POLLING_PARAMETERS
)) {
92 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
97 ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
);
98 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
99 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupC1 error");
103 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
104 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
105 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Assuming Magic Gen 1B tag. [wupC2 failed]");
109 ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
);
110 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
111 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupC1 error");
115 case MF_WAKE_GDM_ALT
:
116 ReaderTransmitBitsPar(wupGDM1
, 7, NULL
, NULL
);
117 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
118 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupGDM1 error");
121 ReaderTransmit(wupGDM2
, sizeof(wupGDM2
), NULL
);
122 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
123 if (g_dbglevel
>= DBG_INFO
) Dbprintf("wupGDM2 error");
124 // maybe this is fine on some tags?
129 if (key_auth_cmd
!= 0) {
130 uint64_t ui64key
= bytes_to_num(key
, 6);
131 if (mifare_classic_authex_cmd(pcs
, cuid
, block_no
, key_auth_cmd
, ui64key
, AUTH_FIRST
, NULL
, NULL
, NULL
, NULL
, false, false)) {
132 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
140 //-----------------------------------------------------------------------------
141 // Select, Authenticate, Read a MIFARE tag.
142 // wakeup determines the type of wakeup
143 // key_auth_cmd is usually one of MIFARE_AUTH_KEYA, MIFARE_AUTH_KEYB, MIFARE_MAGIC_GDM_AUTH_KEY or 0 to disable auth
144 // read_cmd is usually one of ISO14443A_CMD_READBLOCK, MIFARE_MAGIC_GDM_READBLOCK, or MIFARE_MAGIC_GDM_READ_CFG
145 // block_data must be 16*count bytes large
146 // block_no through block_no+count-1 normally needs to be within the same sector
147 //-----------------------------------------------------------------------------
148 int16_t mifare_cmd_readblocks(MifareWakeupType wakeup
, uint8_t key_auth_cmd
, uint8_t *key
, uint8_t read_cmd
, uint8_t block_no
, uint8_t count
, uint8_t *block_data
) {
149 struct Crypto1State mpcs
= {0, 0};
150 struct Crypto1State
*pcs
= (key_auth_cmd
== 0) ? NULL
: &mpcs
;;
152 uint32_t timeout
= iso14a_get_timeout();
154 int retval
= PM3_SUCCESS
;
156 if (mifare_wakeup_auth(pcs
, wakeup
, key_auth_cmd
, key
, block_no
) == false) {
161 // frame waiting time (FWT) in 1/fc
162 uint32_t fwt
= 256 * 16 * (1 << 7);
163 iso14a_set_timeout(fwt
/ (8 * 16));
165 for (uint8_t i
= 0; i
< count
; i
++) {
166 if (mifare_classic_readblock_ex(pcs
, block_no
+ i
, block_data
+ (i
* 16), read_cmd
)) {
167 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Read block error");
173 if (mifare_classic_halt(pcs
)) {
174 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
182 iso14a_set_timeout(timeout
);
183 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
190 //-----------------------------------------------------------------------------
191 // Select, Authenticate, Write a MIFARE tag.
192 // wakeup determines the type of wakeup
193 // key_auth_cmd is usually one of MIFARE_AUTH_KEYA, MIFARE_AUTH_KEYB, MIFARE_MAGIC_GDM_AUTH_KEY or 0 to disable auth
194 // write_cmd is usually one of ISO14443A_CMD_WRITEBLOCK, MIFARE_MAGIC_GDM_WRITEBLOCK, or MIFARE_MAGIC_GDM_WRITE_CFG
195 // block_data must be 16*count bytes large
196 // block_no through block_no+count-1 normally needs to be within the same sector
197 //-----------------------------------------------------------------------------
198 int16_t mifare_cmd_writeblocks(MifareWakeupType wakeup
, uint8_t key_auth_cmd
, uint8_t *key
, uint8_t write_cmd
, uint8_t block_no
, uint8_t count
, uint8_t *block_data
) {
199 struct Crypto1State mpcs
= {0, 0};
200 struct Crypto1State
*pcs
= (key_auth_cmd
== 0) ? NULL
: &mpcs
;;
202 int retval
= PM3_SUCCESS
;
204 if (mifare_wakeup_auth(pcs
, wakeup
, key_auth_cmd
, key
, block_no
) == false) {
209 for (uint8_t i
= 0; i
< count
; i
++) {
210 int res
= mifare_classic_writeblock_ex(pcs
, block_no
+ i
, block_data
+ (i
* 16), write_cmd
);
211 if (res
== PM3_ETEAROFF
) {
212 retval
= PM3_ETEAROFF
;
214 } else if (res
!= PM3_SUCCESS
) {
215 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
221 if (mifare_classic_halt(pcs
)) {
222 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
230 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
238 //-----------------------------------------------------------------------------
239 // Select, Authenticate, Read a MIFARE tag.
240 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
241 //-----------------------------------------------------------------------------
242 void MifareReadSector(uint8_t sector_no
, uint8_t key_type
, uint8_t *key
) {
243 uint8_t block_no
= FirstBlockOfSector(sector_no
);
244 uint8_t num_blocks
= NumBlocksPerSector(sector_no
);
246 uint8_t outbuf
[16 * 16];
247 int16_t retval
= mifare_cmd_readblocks(MF_WAKE_WUPA
, MIFARE_AUTH_KEYA
+ (key_type
& 0xF), key
, ISO14443A_CMD_READBLOCK
, block_no
, num_blocks
, outbuf
);
249 reply_old(CMD_ACK
, retval
== PM3_SUCCESS
, 0, 0, outbuf
, 16 * num_blocks
);
252 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
) {
254 bool turnOffField
= (arg0
== 1);
260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
265 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
266 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
271 if (!mifare_ultra_auth(keybytes
)) {
272 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Authentication failed");
278 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
281 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
284 void MifareUL_AES_Auth(bool turn_off_field
, uint8_t keyno
, uint8_t *keybytes
) {
290 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
295 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
296 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
297 reply_ng(CMD_HF_MIFAREULAES_AUTH
, PM3_ESOFT
, NULL
, 0);
301 if (!mifare_ultra_aes_auth(keyno
, keybytes
)) {
302 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Authentication failed");
303 reply_ng(CMD_HF_MIFAREULAES_AUTH
, PM3_ESOFT
, NULL
, 0);
307 if (turn_off_field
) {
308 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
311 reply_ng(CMD_HF_MIFAREULAES_AUTH
, PM3_SUCCESS
, NULL
, 0);
315 // Arg1 = UsePwd bool
316 // datain = PWD bytes,
317 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) {
318 uint8_t blockNo
= arg0
;
319 uint8_t dataout
[16] = {0x00};
320 bool useKey
= (arg1
== 1); //UL_C
321 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
325 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
330 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
332 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)", len
);
337 // UL-C authentication
339 uint8_t key
[16] = {0x00};
340 memcpy(key
, datain
, sizeof(key
));
342 if (!mifare_ultra_auth(key
)) {
348 // UL-EV1 / NTAG authentication
350 uint8_t pwd
[4] = {0x00};
351 memcpy(pwd
, datain
, 4);
352 uint8_t pack
[4] = {0, 0, 0, 0};
353 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
359 if (mifare_ultra_readblock(blockNo
, dataout
)) {
360 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Read block error");
365 if (mifare_ultra_halt()) {
366 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
371 reply_mix(CMD_ACK
, 1, 0, 0, dataout
, 16);
372 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
376 // arg0 = blockNo (start)
377 // arg1 = Pages (number of blocks)
379 // datain = KEY bytes
380 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
) {
383 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
385 // free eventually allocated BigBuf memory
387 BigBuf_Clear_ext(false);
392 uint8_t blockNo
= arg0
;
393 uint16_t blocks
= arg1
;
394 bool useKey
= (arg2
== 1); // UL_C
395 bool usePwd
= (arg2
== 2); // UL_EV1/NTAG
396 uint32_t countblocks
= 0;
397 uint8_t *dataout
= BigBuf_malloc(CARD_MEMORY_SIZE
);
398 if (dataout
== NULL
) {
399 Dbprintf("out of memory");
404 int len
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true);
406 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card (RC:%d)", len
);
411 // UL-C authentication
413 uint8_t key
[16] = {0x00};
414 memcpy(key
, datain
, sizeof(key
));
416 if (!mifare_ultra_auth(key
)) {
422 // UL-EV1 / NTAG authentication
424 uint8_t pwd
[4] = {0x00};
425 memcpy(pwd
, datain
, sizeof(pwd
));
426 uint8_t pack
[4] = {0, 0, 0, 0};
428 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
434 for (int i
= 0; i
< blocks
; i
++) {
435 if ((i
* 4) + 4 >= CARD_MEMORY_SIZE
) {
436 Dbprintf("Data exceeds buffer!!");
440 len
= mifare_ultra_readblock(blockNo
+ i
, dataout
+ (4 * i
));
443 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Read block %d error", i
);
444 // if no blocks read - error out
449 //stop at last successful read block and return what we got
457 len
= mifare_ultra_halt();
459 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
464 if (g_dbglevel
>= DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
);
468 reply_mix(CMD_ACK
, 1, countblocks
, dataout
- BigBuf_get_addr(), 0, 0);
469 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
475 void MifareValue(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) {
477 uint8_t blockNo
= arg0
;
478 uint8_t keyType
= arg1
;
479 uint8_t transferKeyType
= arg2
;
480 uint64_t ui64Key
= 0;
481 uint64_t transferUi64Key
= 0;
482 uint8_t blockdata
[16] = {0x00};
484 ui64Key
= bytes_to_num(datain
, 6);
485 memcpy(blockdata
, datain
+ 11, 16);
486 transferUi64Key
= bytes_to_num(datain
+ 27, 6);
489 uint8_t action
= datain
[9];
490 uint8_t transferBlk
= datain
[10];
491 bool needAuth
= datain
[33];
493 uint8_t uid
[10] = {0x00};
495 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
497 struct Crypto1State mpcs
= {0, 0};
498 struct Crypto1State
*pcs
;
501 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
511 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
512 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
516 if (mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
517 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
521 if (mifare_classic_value(pcs
, blockNo
, blockdata
, action
) != PM3_SUCCESS
) {
522 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
527 // transfer to other sector
528 if (mifare_classic_auth(pcs
, cuid
, transferBlk
, transferKeyType
, transferUi64Key
, AUTH_NESTED
)) {
529 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Nested auth error");
534 // send transfer (commit the change)
535 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_TRANSFER
, (transferBlk
!= 0) ? transferBlk
: blockNo
, receivedAnswer
, sizeof(receivedAnswer
), NULL
, NULL
);
536 if (len
!= 1 && receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
537 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Cmd Error in transfer: %02x", receivedAnswer
[0]);
541 if (mifare_classic_halt(pcs
)) {
542 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
552 if (g_dbglevel
>= 2) DbpString("WRITE BLOCK FINISHED");
554 reply_mix(CMD_ACK
, isOK
, 0, 0, 0, 0);
556 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
561 // Arg0 : Block to write to.
562 // Arg1 : 0 = use no authentication.
563 // 1 = use 0x1A authentication.
564 // 2 = use 0x1B authentication.
565 // datain : 4 first bytes is data to be written.
566 // : 4/16 next bytes is authentication key.
567 static void MifareUWriteBlockEx(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
, bool reply
) {
568 uint8_t blockNo
= arg0
;
569 bool useKey
= (arg1
== 1); //UL_C
570 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
571 uint8_t blockdata
[4] = {0x00};
573 memcpy(blockdata
, datain
, 4);
577 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
582 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
583 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
588 // UL-C authentication
590 uint8_t key
[16] = {0x00};
591 memcpy(key
, datain
+ 4, sizeof(key
));
593 if (!mifare_ultra_auth(key
)) {
599 // UL-EV1 / NTAG authentication
601 uint8_t pwd
[4] = {0x00};
602 memcpy(pwd
, datain
+ 4, 4);
603 uint8_t pack
[4] = {0, 0, 0, 0};
604 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
610 if (mifare_ultra_writeblock(blockNo
, blockdata
) != PM3_SUCCESS
) {
611 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
616 if (mifare_ultra_halt()) {
617 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
622 if (g_dbglevel
>= 2) DbpString("WRITE BLOCK FINISHED");
625 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
626 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
631 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) {
632 MifareUWriteBlockEx(arg0
, arg1
, datain
, true);
635 // Arg0 : Block to write to.
636 // Arg1 : 0 = use no authentication.
637 // 1 = use 0x1A authentication.
638 // 2 = use 0x1B authentication.
639 // datain : 16 first bytes is data to be written.
640 // : 4/16 next bytes is authentication key.
641 void MifareUWriteBlockCompat(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) {
642 uint8_t blockNo
= arg0
;
643 bool useKey
= (arg1
== 1); //UL_C
644 bool usePwd
= (arg1
== 2); //UL_EV1/NTAG
645 uint8_t blockdata
[16] = {0x00};
647 memcpy(blockdata
, datain
, 16);
651 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
656 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
657 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
662 // UL-C authentication
664 uint8_t key
[16] = {0x00};
665 memcpy(key
, datain
+ 16, sizeof(key
));
667 if (!mifare_ultra_auth(key
)) {
673 // UL-EV1 / NTAG authentication
675 uint8_t pwd
[4] = {0x00};
676 memcpy(pwd
, datain
+ 16, 4);
677 uint8_t pack
[4] = {0, 0, 0, 0};
678 if (!mifare_ul_ev1_auth(pwd
, pack
)) {
684 if (mifare_ultra_writeblock_compat(blockNo
, blockdata
) != PM3_SUCCESS
) {
685 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
690 if (mifare_ultra_halt()) {
691 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
696 if (g_dbglevel
>= 2) DbpString("WRITE BLOCK FINISHED");
698 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
699 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
704 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
) {
706 uint8_t pwd
[16] = {0x00};
707 uint8_t blockdata
[4] = {0x00};
709 memcpy(pwd
, datain
, 16);
714 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
719 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
720 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
725 blockdata
[0] = pwd
[7];
726 blockdata
[1] = pwd
[6];
727 blockdata
[2] = pwd
[5];
728 blockdata
[3] = pwd
[4];
729 if (mifare_ultra_writeblock(44, blockdata
) != PM3_SUCCESS
) {
730 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
735 blockdata
[0] = pwd
[3];
736 blockdata
[1] = pwd
[2];
737 blockdata
[2] = pwd
[1];
738 blockdata
[3] = pwd
[0];
739 if (mifare_ultra_writeblock(45, blockdata
) != PM3_SUCCESS
) {
740 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
745 blockdata
[0] = pwd
[15];
746 blockdata
[1] = pwd
[14];
747 blockdata
[2] = pwd
[13];
748 blockdata
[3] = pwd
[12];
749 if (mifare_ultra_writeblock(46, blockdata
) != PM3_SUCCESS
) {
750 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
755 blockdata
[0] = pwd
[11];
756 blockdata
[1] = pwd
[10];
757 blockdata
[2] = pwd
[9];
758 blockdata
[3] = pwd
[8];
759 if (mifare_ultra_writeblock(47, blockdata
) != PM3_SUCCESS
) {
760 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Write block error");
765 if (mifare_ultra_halt()) {
766 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
771 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
772 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
777 // Return 1 if the nonce is invalid else return 0
778 static int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, const uint8_t *parity
) {
780 (oddparity8((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
, 16))) && \
781 (oddparity8((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
, 8))) && \
782 (oddparity8((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
, 0)))
786 void MifareAcquireNonces(uint32_t arg0
, uint32_t flags
) {
788 uint8_t uid
[10] = {0x00};
789 uint8_t answer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
790 uint8_t par
[1] = {0x00};
791 uint8_t buf
[PM3_CMD_DATA_SIZE
] = {0x00};
794 uint16_t num_nonces
= 0;
795 uint8_t cascade_levels
= 0;
796 uint8_t blockNo
= arg0
& 0xff;
797 uint8_t keyType
= (arg0
>> 8) & 0xff;
798 bool initialize
= flags
& 0x0001;
799 bool field_off
= flags
& 0x0004;
800 bool have_uid
= false;
806 BigBuf_Clear_ext(false);
811 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
815 while (num_nonces
< PM3_CMD_DATA_SIZE
/ 4) {
817 // Test if the action was cancelled
818 if (BUTTON_PRESS()) {
824 if (!have_uid
) { // need a full select cycle to get the uid first
825 iso14a_card_select_t card_info
;
826 if (iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true) == 0) {
827 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireNonces: Can't select card (ALL)");
830 switch (card_info
.uidlen
) {
844 } else { // no need for anticollision. We can directly select the card
845 if (iso14443a_fast_select_card(uid
, cascade_levels
) == 0) {
846 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireNonces: Can't select card (UID)");
851 // Transmit MIFARE_CLASSIC_AUTH
852 uint8_t dcmd
[4] = {0x60 + (keyType
& 0x01), blockNo
, 0x00, 0x00};
854 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
855 int len
= ReaderReceive(answer
, sizeof(answer
), par
);
857 // wait for the card to become ready again
861 if (g_dbglevel
>= 2) Dbprintf("AcquireNonces: Auth1 error");
865 // Save the tag nonce (nt)
866 memcpy(buf
+ num_nonces
* 4, answer
, 4);
872 reply_old(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
875 if (g_dbglevel
>= 3) DbpString("AcquireNonces finished");
878 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
884 //-----------------------------------------------------------------------------
885 // acquire encrypted nonces in order to perform the attack described in
886 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
887 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
888 // Computer and Communications Security, 2015
889 //-----------------------------------------------------------------------------
890 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
) {
892 struct Crypto1State mpcs
= {0, 0};
893 struct Crypto1State
*pcs
;
896 uint8_t uid
[10] = {0x00};
897 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
898 uint8_t par_enc
[1] = {0x00};
899 uint8_t buf
[PM3_CMD_DATA_SIZE
] = {0x00};
901 uint64_t ui64Key
= bytes_to_num(datain
, 6);
903 int16_t isOK
= PM3_SUCCESS
;
904 uint16_t num_nonces
= 0;
905 uint8_t nt_par_enc
= 0;
906 uint8_t cascade_levels
= 0;
907 uint8_t blockNo
= arg0
& 0xff;
908 uint8_t keyType
= (arg0
>> 8) & 0xff;
909 uint8_t targetBlockNo
= arg1
& 0xff;
910 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
911 bool initialize
= flags
& 0x0001;
912 bool slow
= flags
& 0x0002;
913 bool field_off
= flags
& 0x0004;
914 bool have_uid
= false;
920 BigBuf_Clear_ext(false);
925 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
929 uint8_t prev_enc_nt
[] = {0, 0, 0, 0};
930 uint8_t prev_counter
= 0;
932 for (uint16_t i
= 0; i
<= PM3_CMD_DATA_SIZE
- 9;) {
934 // Test if the action was cancelled
935 if (BUTTON_PRESS()) {
936 isOK
= PM3_EOPABORTED
;
941 if (have_uid
== false) { // need a full select cycle to get the uid first
942 iso14a_card_select_t card_info
;
943 if (iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true) == 0) {
944 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireEncryptedNonces: Can't select card (ALL)");
947 switch (card_info
.uidlen
) {
961 } else { // no need for anticollision. We can directly select the card
962 if (iso14443a_fast_select_card(uid
, cascade_levels
) == 0) {
963 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireEncryptedNonces: Can't select card (UID)");
969 SpinDelayUs(HARDNESTED_PRE_AUTHENTICATION_LEADTIME
);
972 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
973 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireEncryptedNonces: Auth1 error");
977 // nested authentication
978 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, MIFARE_AUTH_KEYA
+ (targetKeyType
& 0xF), targetBlockNo
, receivedAnswer
, sizeof(receivedAnswer
), par_enc
, NULL
);
980 // wait for the card to become ready again
984 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireEncryptedNonces: Auth2 error len=%d", len
);
989 if (num_nonces
% 2) {
990 memcpy(buf
+ i
, receivedAnswer
, 4);
991 nt_par_enc
= par_enc
[0] & 0xf0;
993 nt_par_enc
|= par_enc
[0] >> 4;
994 memcpy(buf
+ i
+ 4, receivedAnswer
, 4);
995 memcpy(buf
+ i
+ 8, &nt_par_enc
, 1);
1000 if (prev_enc_nt
[0] == receivedAnswer
[0] &&
1001 prev_enc_nt
[1] == receivedAnswer
[1] &&
1002 prev_enc_nt
[2] == receivedAnswer
[2] &&
1003 prev_enc_nt
[3] == receivedAnswer
[3]
1007 memcpy(prev_enc_nt
, receivedAnswer
, 4);
1008 if (prev_counter
== 5) {
1009 if (g_dbglevel
>= DBG_EXTENDED
) {
1010 DbpString("Static encrypted nonce detected, exiting...");
1011 uint32_t a
= bytes_to_num(prev_enc_nt
, 4);
1012 uint32_t b
= bytes_to_num(receivedAnswer
, 4);
1013 Dbprintf("( %08x vs %08x )", a
, b
);
1015 isOK
= PM3_ESTATIC_NONCE
;
1022 crypto1_deinit(pcs
);
1024 reply_old(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
));
1028 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1035 //-----------------------------------------------------------------------------
1036 // acquire static encrypted nonces in order to perform the attack described in
1037 // Philippe Teuwen, "MIFARE Classic: exposing the static encrypted nonce variant"
1038 //-----------------------------------------------------------------------------
1039 int MifareAcquireStaticEncryptedNonces(uint32_t flags
, const uint8_t *key
, bool reply
) {
1040 struct Crypto1State mpcs
= {0, 0};
1041 struct Crypto1State
*pcs
;
1044 uint8_t uid
[10] = {0x00};
1045 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
1046 uint8_t par_enc
[1] = {0x00};
1047 // ((MIFARE_1K_MAXSECTOR + 1) * 2) * 8 < PM3_CMD_DATA_SIZE
1048 // we're storing nonces in emulator memory at CARD_MEMORY_RF08S_OFFSET
1049 // one sector data in one 16-byte block with for each keytype:
1050 // uint16_t nt_first_half (as we can reconstruct the other half)
1051 // uint8_t nt_par_err
1052 // uint8_t flag: if 0xAA and key=000000000000 it means we don't know the key yet
1054 // buf: working buffer to prepare those "blocks"
1055 uint8_t buf
[MIFARE_BLOCK_SIZE
] = {0x00};
1056 uint64_t ui64Key
= bytes_to_num(key
, 6);
1057 bool with_data
= flags
& 1;
1059 int16_t isOK
= PM3_SUCCESS
;
1060 uint8_t cascade_levels
= 0;
1061 bool have_uid
= false;
1067 BigBuf_Clear_ext(false);
1071 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1075 for (uint16_t sec
= 0; sec
< MIFARE_1K_MAXSECTOR
+ 1; sec
++) {
1076 uint16_t sec_gap
= sec
;
1077 if (sec
>= MIFARE_1K_MAXSECTOR
) {
1078 // gap between user blocks and advanced verification method blocks
1081 uint16_t blockNo
= sec_gap
* 4;
1082 for (uint8_t keyType
= 0; keyType
< 2; keyType
++) {
1083 // Test if the action was cancelled
1084 if (BUTTON_PRESS()) {
1085 isOK
= PM3_EOPABORTED
;
1088 if (have_uid
== false) { // need a full select cycle to get the uid first
1089 iso14a_card_select_t card_info
;
1090 if (iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true) == 0) {
1091 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Can't select card (ALL)");
1092 isOK
= PM3_ERFTRANS
;
1095 switch (card_info
.uidlen
) {
1109 } else { // no need for anticollision. We can directly select the card
1110 if (iso14443a_fast_select_card(uid
, cascade_levels
) == 0) {
1111 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Can't select card (UID)");
1112 isOK
= PM3_ERFTRANS
;
1118 if (mifare_classic_authex_cmd(pcs
, cuid
, blockNo
, MIFARE_AUTH_KEYA
+ keyType
+ 4, ui64Key
, AUTH_FIRST
, &nt1
, NULL
, NULL
, NULL
, false, false)) {
1119 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Auth1 error");
1123 if ((with_data
) && (keyType
== 0)) {
1126 if (blockNo
>= MIFARE_1K_MAXSECTOR
* 4) {
1127 // special RF08S advanced authentication blocks, let's dump in emulator just in case
1130 for (uint16_t tb
= blockNo
; tb
< blockNo
+ blocks
; tb
++) {
1131 memset(data
, 0x00, sizeof(data
));
1132 int res
= mifare_classic_readblock(pcs
, tb
, data
);
1134 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Read error");
1138 emlSetMem_xt(data
, tb
, 1, 16);
1141 // nested authentication
1142 uint16_t len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, MIFARE_AUTH_KEYA
+ keyType
+ 4, blockNo
, receivedAnswer
, sizeof(receivedAnswer
), par_enc
, NULL
);
1144 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Auth2 error len=%d", len
);
1148 uint32_t nt_enc
= bytes_to_num(receivedAnswer
, 4);
1149 crypto1_init(pcs
, ui64Key
);
1150 uint32_t nt
= crypto1_word(pcs
, nt_enc
^ cuid
, 1) ^ nt_enc
;
1151 // Dbprintf("Sec %2i key %i nT=%08x", sec, keyType + 4, nt);
1152 // store nt (first half)
1153 num_to_bytes(nt
>> 16, 2, buf
+ (keyType
* 8));
1154 // send some crap to fail auth
1155 uint8_t nack
[] = {0x04};
1156 ReaderTransmit(nack
, sizeof(nack
), NULL
);
1158 if (iso14443a_fast_select_card(uid
, cascade_levels
) == 0) {
1159 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Can't select card (UID)");
1160 isOK
= PM3_ERFTRANS
;
1163 if (mifare_classic_authex_cmd(pcs
, cuid
, blockNo
, MIFARE_AUTH_KEYA
+ keyType
+ 4, ui64Key
, AUTH_FIRST
, &nt1
, NULL
, NULL
, NULL
, false, false)) {
1164 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Auth1 error");
1169 // nested authentication on regular keytype
1170 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, MIFARE_AUTH_KEYA
+ keyType
, blockNo
, receivedAnswer
, sizeof(receivedAnswer
), par_enc
, NULL
);
1172 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("AcquireStaticEncryptedNonces: Auth2 error len=%d", len
);
1177 memcpy(buf
+ (keyType
* 8) + 4, receivedAnswer
, 4);
1178 nt_enc
= bytes_to_num(receivedAnswer
, 4);
1179 uint8_t nt_par_err
= ((((par_enc
[0] >> 7) & 1) ^ oddparity8((nt_enc
>> 24) & 0xFF)) << 3 |
1180 (((par_enc
[0] >> 6) & 1) ^ oddparity8((nt_enc
>> 16) & 0xFF)) << 2 |
1181 (((par_enc
[0] >> 5) & 1) ^ oddparity8((nt_enc
>> 8) & 0xFF)) << 1 |
1182 (((par_enc
[0] >> 4) & 1) ^ oddparity8((nt_enc
>> 0) & 0xFF)));
1183 // Dbprintf("Sec %2i key %i {nT}=%02x%02x%02x%02x perr=%x", sec, keyType, receivedAnswer[0], receivedAnswer[1], receivedAnswer[2], receivedAnswer[3], nt_par_err);
1185 buf
[(keyType
* 8) + 2] = nt_par_err
;
1186 buf
[(keyType
* 8) + 3] = 0xAA; // extra check to tell we have nt/nt_enc/par_err
1187 emlSetMem_xt(buf
, (CARD_MEMORY_RF08S_OFFSET
/ MIFARE_BLOCK_SIZE
) + sec
, 1, MIFARE_BLOCK_SIZE
);
1188 // send some crap to fail auth
1189 ReaderTransmit(nack
, sizeof(nack
), NULL
);
1194 crypto1_deinit(pcs
);
1197 reply_old(CMD_ACK
, isOK
, cuid
, 0, BigBuf_get_EM_addr() + CARD_MEMORY_RF08S_OFFSET
, MIFARE_BLOCK_SIZE
* (MIFARE_1K_MAXSECTOR
+ 1));
1201 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1208 //-----------------------------------------------------------------------------
1209 // MIFARE nested authentication.
1211 //-----------------------------------------------------------------------------
1212 void MifareNested(uint8_t blockNo
, uint8_t keyType
, uint8_t targetBlockNo
, uint8_t targetKeyType
, bool calibrate
, uint8_t *key
) {
1213 uint64_t ui64Key
= 0;
1214 ui64Key
= bytes_to_num(key
, 6);
1218 static uint16_t dmin
, dmax
;
1220 uint8_t par
[1] = {0x00};
1221 uint8_t par_array
[4] = {0x00};
1222 uint8_t uid
[10] = {0x00};
1223 uint32_t cuid
= 0, nt1
, nt2
, nttest
, ks1
;
1224 uint32_t target_nt
[2] = {0x00}, target_ks
[2] = {0x00};
1226 uint16_t ncount
= 0;
1227 struct Crypto1State mpcs
= {0, 0};
1228 struct Crypto1State
*pcs
;
1230 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
1232 uint32_t auth1_time
, auth2_time
;
1233 static uint16_t delta_time
= 0;
1237 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1239 // free eventually allocated BigBuf memory
1241 BigBuf_Clear_ext(false);
1248 // statistics on nonce distance
1249 int16_t isOK
= PM3_SUCCESS
;
1250 #define NESTED_MAX_TRIES 12
1251 if (calibrate
) { // calibrate: for first call only. Otherwise reuse previous calibration
1255 uint32_t prev_enc_nt
= 0;
1256 uint8_t prev_counter
= 0;
1258 uint16_t unsuccessful_tries
= 0;
1264 for (rtr
= 0; rtr
< 17; rtr
++) {
1266 // Test if the action was cancelled
1267 if (BUTTON_PRESS() || data_available()) {
1268 isOK
= PM3_EOPABORTED
;
1272 // prepare next select. No need to power down the card.
1273 if (mifare_classic_halt(pcs
)) {
1274 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Halt error");
1279 if (iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true) == 0) {
1280 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Can't select card");
1286 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
1287 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Auth1 error");
1291 auth2_time
= (delta_time
) ? auth1_time
+ delta_time
: 0;
1293 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
1294 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Auth2 error");
1299 // cards with fixed nonce
1300 // NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
1302 uint32_t nttmp
= prng_successor(nt1
, 100);
1303 for (i
= 101; i
< 1200; i
++) {
1304 nttmp
= prng_successor(nttmp
, 1);
1305 if (nttmp
== nt2
) break;
1311 dmin
= MIN(dmin
, i
);
1312 dmax
= MAX(dmax
, i
);
1314 // allow some slack for proper timing
1315 delta_time
= auth2_time
- auth1_time
+ 32;
1317 if (g_dbglevel
>= DBG_DEBUG
) Dbprintf("Nested: calibrating... ntdist=%d", i
);
1319 unsuccessful_tries
++;
1320 // card isn't vulnerable to nested attack (random numbers are not predictable)
1321 if (unsuccessful_tries
> NESTED_MAX_TRIES
) {
1332 if (prev_counter
== 5) {
1333 if (g_dbglevel
>= DBG_EXTENDED
) {
1334 DbpString("Static encrypted nonce detected, exiting...");
1335 Dbprintf("( %08x vs %08x )", prev_enc_nt
, nt2
);
1337 isOK
= PM3_ESTATIC_NONCE
;
1343 davg
= (davg
+ (rtr
- 1) / 2) / (rtr
- 1);
1345 if (g_dbglevel
>= DBG_DEBUG
) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr
, isOK
, dmin
, dmax
, davg
, delta_time
);
1352 // -------------------------------------------------------------------------------------------------
1356 // get crypted nonces for target sector
1357 for (i
= 0; ((i
< 2) && (isOK
== PM3_SUCCESS
)); i
++) {
1359 // look for exactly two different nonces
1362 // continue until we have an unambiguous nonce
1363 while (target_nt
[i
] == 0) {
1365 // Test if the action was cancelled
1366 if (BUTTON_PRESS() || data_available()) {
1367 isOK
= PM3_EOPABORTED
;
1371 // prepare next select. No need to power down the card.
1372 if (mifare_classic_halt(pcs
)) {
1373 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Halt error");
1377 if (iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true) == false) {
1378 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Can't select card");
1383 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
1384 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Auth1 error");
1388 // nested authentication
1389 auth2_time
= auth1_time
+ delta_time
;
1391 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, MIFARE_AUTH_KEYA
+ (targetKeyType
& 0xF), targetBlockNo
, receivedAnswer
, sizeof(receivedAnswer
), par
, &auth2_time
);
1393 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Nested: Auth2 error len=%d", len
);
1397 nt2
= bytes_to_num(receivedAnswer
, 4);
1398 if (g_dbglevel
>= DBG_DEBUG
) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+ 1, nt1
, nt2
, par
[0]);
1400 // Parity validity check
1401 for (j
= 0; j
< 4; j
++) {
1402 par_array
[j
] = (oddparity8(receivedAnswer
[j
]) != ((par
[0] >> (7 - j
)) & 0x01));
1406 nttest
= prng_successor(nt1
, dmin
- 1);
1407 for (j
= dmin
; j
< dmax
+ 1; j
++) {
1408 nttest
= prng_successor(nttest
, 1);
1411 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)) {
1412 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
1413 if (g_dbglevel
>= DBG_DEBUG
) Dbprintf("Nonce#%d: dismissed (ambiguous), ntdist=%d", i
+ 1, j
);
1417 target_nt
[i
] = nttest
;
1420 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
1422 if (g_dbglevel
>= DBG_DEBUG
) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
1425 if (g_dbglevel
>= DBG_DEBUG
) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+ 1, j
);
1428 if (target_nt
[i
] == 0 && j
== dmax
+ 1 && g_dbglevel
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+ 1);
1434 crypto1_deinit(pcs
);
1446 payload
.isOK
= isOK
;
1447 payload
.block
= targetBlockNo
;
1448 payload
.keytype
= targetKeyType
;
1450 memcpy(payload
.cuid
, &cuid
, 4);
1451 memcpy(payload
.nt_a
, &target_nt
[0], 4);
1452 memcpy(payload
.ks_a
, &target_ks
[0], 4);
1453 memcpy(payload
.nt_b
, &target_nt
[1], 4);
1454 memcpy(payload
.ks_b
, &target_ks
[1], 4);
1457 reply_ng(CMD_HF_MIFARE_NESTED
, PM3_SUCCESS
, (uint8_t *)&payload
, sizeof(payload
));
1458 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1463 void MifareStaticNested(uint8_t blockNo
, uint8_t keyType
, uint8_t targetBlockNo
, uint8_t targetKeyType
, uint8_t *key
) {
1467 uint64_t ui64Key
= bytes_to_num(key
, 6);
1468 uint16_t len
, dist1
= 160, dist2
= 320;
1469 uint8_t uid
[10] = { 0x00 };
1470 uint32_t cuid
= 0, nt1
= 0, nt2
= 0, nt3
= 0;
1471 uint32_t target_nt
[2] = {0x00}, target_ks
[2] = {0x00};
1472 uint8_t par
[1] = { 0x00 };
1473 uint8_t receivedAnswer
[10] = { 0x00 };
1475 struct Crypto1State mpcs
= { 0, 0 };
1476 struct Crypto1State
*pcs
;
1480 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1482 // free eventually allocated BigBuf memory
1484 BigBuf_Clear_ext(false);
1488 int16_t isOK
= PM3_ESOFT
;
1491 // Main loop - get crypted nonces for target sector
1492 for (uint8_t rtr
= 0; rtr
< 2; rtr
++) {
1494 // distance measurement
1495 if (mifare_classic_halt(pcs
)) {
1499 if (iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true) == false) {
1503 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
1507 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, NULL
)) {
1511 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt3
, NULL
)) {
1515 dist1
= nonce_distance(nt1
, nt2
);
1516 dist2
= nonce_distance(nt1
, nt3
);
1518 if (mifare_classic_halt(pcs
)) {
1522 if (iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true) == false) {
1527 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
1531 // pre-generate nonces
1532 if (targetKeyType
== 1 && nt1
== 0x009080A2) {
1533 target_nt
[0] = prng_successor(nt1
, 161);
1534 target_nt
[1] = prng_successor(nt1
, 321);
1536 target_nt
[0] = prng_successor(nt1
, dist1
);
1537 target_nt
[1] = prng_successor(nt1
, dist2
);
1540 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, MIFARE_AUTH_KEYA
+ (targetKeyType
& 0xF), targetBlockNo
, receivedAnswer
, sizeof(receivedAnswer
), par
, NULL
);
1545 nt2
= bytes_to_num(receivedAnswer
, 4);
1546 target_ks
[0] = nt2
^ target_nt
[0];
1548 // second collection
1549 if (mifare_classic_halt(pcs
)) {
1553 if (iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true) == false) {
1557 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) {
1561 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, NULL
, NULL
)) {
1565 len
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, MIFARE_AUTH_KEYA
+ (targetKeyType
& 0xF), targetBlockNo
, receivedAnswer
, sizeof(receivedAnswer
), par
, NULL
);
1570 nt3
= bytes_to_num(receivedAnswer
, 4);
1571 target_ks
[1] = nt3
^ target_nt
[1];
1578 crypto1_deinit(pcs
);
1589 payload
.block
= targetBlockNo
;
1590 payload
.keytype
= targetKeyType
;
1592 memcpy(payload
.cuid
, &cuid
, 4);
1593 memcpy(payload
.nt_a
, &target_nt
[0], 4);
1594 memcpy(payload
.ks_a
, &target_ks
[0], 4);
1595 memcpy(payload
.nt_b
, &target_nt
[1], 4);
1596 memcpy(payload
.ks_b
, &target_ks
[1], 4);
1599 reply_ng(CMD_HF_MIFARE_STATIC_NESTED
, isOK
, (uint8_t *)&payload
, sizeof(payload
));
1600 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1605 //-----------------------------------------------------------------------------
1606 // MIFARE check keys. key count up to 85.
1608 //-----------------------------------------------------------------------------
1609 typedef struct sector_t
{
1614 typedef struct chk_t
{
1621 struct Crypto1State
*pcs
;
1625 // fast select, tries 5 times to select
1628 // 2 = failed to select.
1631 static uint8_t chkKey(struct chk_t
*c
) {
1632 uint8_t i
= 0, res
= 2;
1633 bool selected
= false;
1635 // this part is from Piwi's faster nonce collecting part in Hardnested.
1636 // assume: fast select
1637 if (!iso14443a_fast_select_card(c
->uid
, c
->cl
)) {
1642 res
= mifare_classic_authex(c
->pcs
, c
->cuid
, c
->block
, c
->keyType
, c
->key
, AUTH_FIRST
, NULL
, NULL
);
1646 // if successful auth, send HALT
1648 // mifare_classic_halt(c->pcs);
1651 if (selected
== false) {
1652 Dbprintf("chkKey: Failed at fast selecting the card!");
1657 static uint8_t chkKey_readb(struct chk_t
*c
, uint8_t *keyb
) {
1659 if (!iso14443a_fast_select_card(c
->uid
, c
->cl
))
1662 if (mifare_classic_authex(c
->pcs
, c
->cuid
, c
->block
, 0, c
->key
, AUTH_FIRST
, NULL
, NULL
))
1665 uint8_t data
[16] = {0x00};
1666 uint8_t res
= mifare_classic_readblock(c
->pcs
, c
->block
, data
);
1670 // data was something else than zeros.
1671 if (memcmp(data
+ 10, "\x00\x00\x00\x00\x00\x00", 6) != 0) {
1672 memcpy(keyb
, data
+ 10, 6);
1677 mifare_classic_halt(c
->pcs
);
1682 static void chkKey_scanA(struct chk_t
*c
, struct sector_t
*k_sector
, uint8_t *found
, const uint8_t *sectorcnt
, uint8_t *foundkeys
) {
1683 for (uint8_t s
= 0; s
< *sectorcnt
; s
++) {
1685 // skip already found A keys
1689 c
->block
= FirstBlockOfSector(s
);
1690 if (chkKey(c
) == 0) {
1691 num_to_bytes(c
->key
, 6, k_sector
[s
].keyA
);
1695 if (g_dbglevel
>= 3) Dbprintf("ChkKeys_fast: Scan A found (%d)", c
->block
);
1700 static void chkKey_scanB(struct chk_t
*c
, struct sector_t
*k_sector
, uint8_t *found
, const uint8_t *sectorcnt
, uint8_t *foundkeys
) {
1701 for (uint8_t s
= 0; s
< *sectorcnt
; s
++) {
1703 // skip already found B keys
1704 if (found
[(s
* 2) + 1])
1707 c
->block
= FirstBlockOfSector(s
);
1708 if (chkKey(c
) == 0) {
1709 num_to_bytes(c
->key
, 6, k_sector
[s
].keyB
);
1710 found
[(s
* 2) + 1] = 1;
1713 if (g_dbglevel
>= 3) Dbprintf("ChkKeys_fast: Scan B found (%d)", c
->block
);
1719 // when A is found but not B, try to read B.
1720 static void chkKey_loopBonly(struct chk_t
*c
, struct sector_t
*k_sector
, uint8_t *found
, uint8_t *sectorcnt
, uint8_t *foundkeys
) {
1722 // read Block B, if A is found.
1723 for (uint8_t s
= 0; s
< *sectorcnt
; ++s
) {
1725 if (found
[(s
* 2)] && found
[(s
* 2) + 1])
1728 c
->block
= (FirstBlockOfSector(s
) + NumBlocksPerSector(s
) - 1);
1731 if (found
[(s
* 2)] && !found
[(s
* 2) + 1]) {
1732 c
->key
= bytes_to_num(k_sector
[s
].keyA
, 6);
1733 uint8_t status
= chkKey_readb(c
, k_sector
[s
].keyB
);
1735 found
[(s
* 2) + 1] = 1;
1738 if (g_dbglevel
>= 3) Dbprintf("ChkKeys_fast: Reading B found (%d)", c
->block
);
1740 // try quick find all B?
1741 // assume: keys comes in groups. Find one B, test against all B.
1742 c
->key
= bytes_to_num(k_sector
[s
].keyB
, 6);
1744 chkKey_scanB(c
, k_sector
, found
, sectorcnt
, foundkeys
);
1750 // get Chunks of keys, to test authentication against card.
1751 // arg0 = antal sectorer
1752 // arg0 = first time
1753 // arg1 = clear trace
1754 // arg2 = antal nycklar i keychunk
1755 // datain = keys as array
1756 void MifareChkKeys_fast(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
) {
1759 uint8_t sectorcnt
= arg0
& 0xFF; // 16;
1760 uint8_t firstchunk
= (arg0
>> 8) & 0xF;
1761 uint8_t lastchunk
= (arg0
>> 12) & 0xF;
1762 uint16_t singleSectorParams
= (arg0
>> 16) & 0xFFFF;
1763 uint8_t strategy
= arg1
& 0xFF;
1764 uint8_t use_flashmem
= (arg1
>> 8) & 0xFF;
1765 uint16_t keyCount
= arg2
& 0xFF;
1767 bool singleSectorMode
= (singleSectorParams
>> 15) & 1;
1768 uint8_t keytype
= (singleSectorParams
>> 8) & 1;
1769 uint8_t blockn
= singleSectorParams
& 0xFF;
1771 struct Crypto1State mpcs
= {0, 0};
1772 struct Crypto1State
*pcs
;
1774 struct chk_t chk_data
;
1776 uint8_t allkeys
= sectorcnt
<< 1;
1778 static uint32_t cuid
= 0;
1779 static uint8_t cascade_levels
= 0;
1780 static uint8_t foundkeys
= 0;
1781 static sector_t k_sector
[80];
1782 static uint8_t found
[80];
1783 static uint8_t *uid
;
1785 int oldbg
= g_dbglevel
;
1791 uint8_t size
[2] = {0x00, 0x00};
1792 isok
= Flash_ReadData(DEFAULT_MF_KEYS_OFFSET_P(spi_flash_pages64k
), size
, 2);
1796 keyCount
= size
[1] << 8 | size
[0];
1801 // limit size of available for keys in bigbuff
1803 uint16_t key_mem_available
= MIN(BigBuf_get_size(), keyCount
* 6);
1805 keyCount
= key_mem_available
/ 6;
1807 datain
= BigBuf_malloc(key_mem_available
);
1811 isok
= Flash_ReadData(DEFAULT_MF_KEYS_OFFSET_P(spi_flash_pages64k
) + 2, datain
, key_mem_available
);
1812 if (isok
!= key_mem_available
)
1818 if (uid
== NULL
|| firstchunk
) {
1819 uid
= BigBuf_malloc(10);
1824 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1833 memset(k_sector
, 0x00, 480 + 10);
1834 memset(found
, 0x00, sizeof(found
));
1837 iso14a_card_select_t card_info
;
1838 if (!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true)) {
1839 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("ChkKeys_fast: Can't select card (ALL)");
1843 switch (card_info
.uidlen
) {
1860 // clear debug level. We are expecting lots of authentication failures...
1861 g_dbglevel
= DBG_NONE
;
1863 // set check struct.
1865 chk_data
.cuid
= cuid
;
1866 chk_data
.cl
= cascade_levels
;
1870 if (singleSectorMode
) {
1872 chk_data
.block
= blockn
;
1873 chk_data
.keyType
= keytype
;
1874 for (uint16_t i
= 0; i
< keyCount
; ++i
) {
1876 // Allow button press / usb cmd to interrupt device
1877 if (BUTTON_PRESS() || data_available()) {
1883 chk_data
.key
= bytes_to_num(datain
+ i
* 6, 6);
1884 if (chkKey(&chk_data
) == 0) {
1886 reply_old(CMD_ACK
, 1, 0, 0, datain
+ i
* 6, 6);
1890 reply_mix(CMD_ACK
, 0, 0, 0, 0, 0);
1893 crypto1_deinit(pcs
);
1894 if (foundkeys
== allkeys
|| lastchunk
) {
1896 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1898 BigBuf_Clear_ext(false);
1905 // keychunk loop - depth first one sector.
1906 if (strategy
== 1 || use_flashmem
) {
1908 uint8_t newfound
= foundkeys
;
1910 uint16_t lastpos
= 0;
1911 uint16_t s_point
= 0;
1913 // keep track of how many sectors on card.
1914 for (uint8_t s
= 0; s
< sectorcnt
; ++s
) {
1916 if (found
[(s
* 2)] && found
[(s
* 2) + 1])
1919 for (uint16_t i
= s_point
; i
< keyCount
; ++i
) {
1921 // Allow button press / usb cmd to interrupt device
1922 if (BUTTON_PRESS() || data_available()) {
1927 if (foundkeys
== allkeys
)
1932 // assume: block0,1,2 has more read rights in accessbits than the sectortrailer. authenticating against block0 in each sector
1933 chk_data
.block
= FirstBlockOfSector(s
);
1936 chk_data
.key
= bytes_to_num(datain
+ i
* 6, 6);
1938 // skip already found A keys
1939 if (!found
[(s
* 2)]) {
1940 chk_data
.keyType
= 0;
1941 status
= chkKey(&chk_data
);
1943 memcpy(k_sector
[s
].keyA
, datain
+ i
* 6, 6);
1947 chkKey_scanA(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
1949 // read Block B, if A is found.
1950 chkKey_loopBonly(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
1952 chk_data
.keyType
= 1;
1953 chkKey_scanB(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
1955 chk_data
.keyType
= 0;
1956 chk_data
.block
= FirstBlockOfSector(s
);
1959 if (lastpos
!= i
&& lastpos
!= 0) {
1960 if (i
- lastpos
< 0xF) {
1961 s_point
= i
& 0xFFF0;
1970 // skip already found B keys
1971 if (!found
[(s
* 2) + 1]) {
1972 chk_data
.keyType
= 1;
1973 status
= chkKey(&chk_data
);
1975 memcpy(k_sector
[s
].keyB
, datain
+ i
* 6, 6);
1976 found
[(s
* 2) + 1] = 1;
1979 chkKey_scanB(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
1982 if (lastpos
!= i
&& lastpos
!= 0) {
1984 if (i
- lastpos
< 0xF)
1985 s_point
= i
& 0xFFF0;
1993 if (found
[(s
* 2)] && found
[(s
* 2) + 1])
1996 } // end keys test loop - depth first
1998 // assume1. if no keys found in first sector, get next keychunk from client
1999 if (!use_flashmem
&& (newfound
- foundkeys
== 0))
2002 } // end loop - sector
2005 if (foundkeys
== allkeys
)
2008 if (strategy
== 2 || use_flashmem
) {
2011 for (uint16_t i
= 0; i
< keyCount
; i
++) {
2013 // Allow button press / usb cmd to interrupt device
2014 if (BUTTON_PRESS() || data_available()) break;
2017 if (foundkeys
== allkeys
)
2023 chk_data
.key
= bytes_to_num(datain
+ i
* 6, 6);
2026 // keep track of how many sectors on card.
2027 for (uint8_t s
= 0; s
< sectorcnt
; ++s
) {
2029 if (found
[(s
* 2)] && found
[(s
* 2) + 1]) continue;
2032 if (foundkeys
== allkeys
)
2035 // assume: block0,1,2 has more read rights in accessbits than the sectortrailer. authenticating against block0 in each sector
2036 chk_data
.block
= FirstBlockOfSector(s
);
2038 // skip already found A keys
2039 if (!found
[(s
* 2)]) {
2040 chk_data
.keyType
= 0;
2041 status
= chkKey(&chk_data
);
2043 memcpy(k_sector
[s
].keyA
, datain
+ i
* 6, 6);
2047 chkKey_scanA(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
2049 // read Block B, if A is found.
2050 chkKey_loopBonly(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
2052 chk_data
.block
= FirstBlockOfSector(s
);
2056 // skip already found B keys
2057 if (!found
[(s
* 2) + 1]) {
2058 chk_data
.keyType
= 1;
2059 status
= chkKey(&chk_data
);
2061 memcpy(k_sector
[s
].keyB
, datain
+ i
* 6, 6);
2062 found
[(s
* 2) + 1] = 1;
2065 chkKey_scanB(&chk_data
, k_sector
, found
, §orcnt
, &foundkeys
);
2068 } // end loop sectors
2070 } // end loop strategy 2
2074 crypto1_deinit(pcs
);
2076 // All keys found, send to client, or last keychunk from client
2077 if (foundkeys
== allkeys
|| lastchunk
) {
2080 for (uint8_t m
= 0; m
< 64; m
++) {
2081 foo
|= ((uint64_t)(found
[m
] & 1) << m
);
2086 for (uint8_t m
= 64; m
< ARRAYLEN(found
); m
++) {
2087 bar
|= ((uint16_t)(found
[m
] & 1) << j
++);
2090 uint8_t *tmp
= BigBuf_malloc(480 + 10);
2091 memcpy(tmp
, k_sector
, sectorcnt
* sizeof(sector_t
));
2092 num_to_bytes(foo
, 8, tmp
+ 480);
2093 tmp
[488] = bar
& 0xFF;
2094 tmp
[489] = bar
>> 8 & 0xFF;
2096 reply_old(CMD_ACK
, foundkeys
, 0, 0, tmp
, 480 + 10);
2099 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2101 BigBuf_Clear_ext(false);
2103 // special trick ecfill
2104 if (use_flashmem
&& foundkeys
== allkeys
) {
2106 uint8_t block
[16] = {0};
2107 for (int i
= 0; i
< sectorcnt
; i
++) {
2111 blockno
= (i
* 4) ^ 0x3;
2113 blockno
= (32 * 4 + (i
- 32) * 16) ^ 0xF;
2116 emlGetMem(block
, blockno
, 1);
2118 memcpy(block
, k_sector
[i
].keyA
, 6);
2119 memcpy(block
+ 10, k_sector
[i
].keyB
, 6);
2121 emlSetMem_xt(block
, blockno
, 1, sizeof(block
));
2124 MifareECardLoad(sectorcnt
, MF_KEY_A
, NULL
);
2125 MifareECardLoad(sectorcnt
, MF_KEY_B
, NULL
);
2128 // partial/none keys found
2129 reply_mix(CMD_ACK
, foundkeys
, 0, 0, 0, 0);
2135 void MifareChkKeys(uint8_t *datain
, uint8_t reserved_mem
) {
2137 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2139 struct Crypto1State mpcs
= {0, 0};
2140 struct Crypto1State
*pcs
;
2143 uint8_t uid
[10] = {0x00};
2147 uint8_t cascade_levels
= 0;
2152 keyresult
.found
= false;
2153 memset(keyresult
.key
, 0x00, sizeof(keyresult
.key
));
2155 bool have_uid
= false;
2157 uint8_t keyType
= datain
[0];
2158 uint8_t blockNo
= datain
[1];
2159 bool clearTrace
= datain
[2];
2160 uint16_t key_count
= (datain
[3] << 8) | datain
[4];
2162 uint16_t key_mem_available
;
2164 key_mem_available
= key_count
* 6;
2166 key_mem_available
= MIN((PM3_CMD_DATA_SIZE
- 5), key_count
* 6);
2168 key_count
= key_mem_available
/ 6;
2175 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2180 int oldbg
= g_dbglevel
;
2181 g_dbglevel
= DBG_NONE
;
2185 for (uint16_t i
= 0; i
< key_count
; i
++) {
2187 // Iceman: use piwi's faster nonce collecting part in hardnested.
2188 if (have_uid
== false) { // need a full select cycle to get the uid first
2189 iso14a_card_select_t card_info
;
2190 if (iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true) == false) {
2191 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("ChkKeys: Can't select card (ALL)");
2192 --i
; // try same key once again
2195 switch (card_info
.uidlen
) {
2209 } else { // no need for anticollision. We can directly select the card
2210 if (iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
, true) == false) {
2211 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("ChkKeys: Can't select card (UID)");
2212 --i
; // try same key once again
2217 key
= bytes_to_num(datain
+ i
* 6, 6);
2218 if (mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, key
, AUTH_FIRST
)) {
2223 memcpy(keyresult
.key
, datain
+ i
* 6, 6);
2224 keyresult
.found
= true;
2229 crypto1_deinit(pcs
);
2231 reply_ng(CMD_HF_MIFARE_CHKKEYS
, PM3_SUCCESS
, (uint8_t *)&keyresult
, sizeof(keyresult
));
2232 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2238 void MifareChkKeys_file(uint8_t *fn
) {
2245 int changed
= rdv40_spiffs_lazy_mount();
2246 uint32_t size
= size_in_spiffs((char *)fn
);
2247 uint8_t *mem
= BigBuf_malloc(size
);
2249 rdv40_spiffs_read_as_filetype((char *)fn
, mem
, size
, RDV40_SPIFFS_SAFETY_SAFE
);
2252 rdv40_spiffs_lazy_unmount();
2257 MifareChkKeys(mem
, true);
2263 //-----------------------------------------------------------------------------
2264 // MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID
2265 //-----------------------------------------------------------------------------
2266 void MifarePersonalizeUID(uint8_t keyType
, uint8_t perso_option
, uint64_t key
) {
2268 uint16_t isOK
= PM3_EUNDEF
;
2271 struct Crypto1State mpcs
= {0, 0};
2272 struct Crypto1State
*pcs
;
2275 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2282 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
2283 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
2287 uint8_t block_number
= 0;
2288 if (mifare_classic_auth(pcs
, cuid
, block_number
, keyType
, key
, AUTH_FIRST
)) {
2289 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
2293 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
2294 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
2295 int len
= mifare_sendcmd_short(pcs
, true, MIFARE_EV1_PERSONAL_UID
, perso_option
, receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
, NULL
);
2296 if (len
!= 1 || receivedAnswer
[0] != CARD_ACK
) {
2297 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
2301 if (mifare_classic_halt(pcs
)) {
2302 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
2309 crypto1_deinit(pcs
);
2312 reply_ng(CMD_HF_MIFARE_PERSONALIZE_UID
, isOK
, NULL
, 0);
2315 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2320 //-----------------------------------------------------------------------------
2321 // Work with emulator memory
2323 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
2324 // involved in dealing with emulator memory. But if it is called later, it might
2325 // destroy the Emulator Memory.
2326 //-----------------------------------------------------------------------------
2328 void MifareEMemClr(void) {
2329 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
2333 void MifareEMemGet(uint8_t blockno
, uint8_t blockcnt
) {
2334 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
2337 size_t size
= blockcnt
* 16;
2338 if (size
> PM3_CMD_DATA_SIZE
) {
2339 reply_ng(CMD_HF_MIFARE_EML_MEMGET
, PM3_EMALLOC
, NULL
, 0);
2343 uint8_t *buf
= BigBuf_malloc(size
);
2345 emlGetMem(buf
, blockno
, blockcnt
); // data, block num, blocks count (max 4)
2348 reply_ng(CMD_HF_MIFARE_EML_MEMGET
, PM3_SUCCESS
, buf
, size
);
2350 BigBuf_free_keep_EM();
2353 //-----------------------------------------------------------------------------
2354 // Load a card into the emulator memory
2356 //-----------------------------------------------------------------------------
2357 int MifareECardLoadExt(uint8_t sectorcnt
, uint8_t keytype
, uint8_t *key
) {
2358 int retval
= MifareECardLoad(sectorcnt
, keytype
, key
);
2359 reply_ng(CMD_HF_MIFARE_EML_LOAD
, retval
, NULL
, 0);
2363 int MifareECardLoad(uint8_t sectorcnt
, uint8_t keytype
, uint8_t *key
) {
2364 if ((keytype
> MF_KEY_B
) && (key
== NULL
)) {
2365 if (g_dbglevel
>= DBG_ERROR
) {
2366 Dbprintf("Error, missing key");
2371 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2377 bool have_uid
= false;
2378 bool bd_authenticated
= false;
2379 uint8_t cascade_levels
= 0;
2381 uint8_t uid
[10] = {0x00};
2382 struct Crypto1State mpcs
= {0, 0};
2383 struct Crypto1State
*pcs
;
2386 int retval
= PM3_SUCCESS
;
2388 // increase time-out. Magic card etc are slow
2389 uint32_t timeout
= iso14a_get_timeout();
2390 // frame waiting time (FWT) in 1/fc
2391 uint32_t fwt
= 256 * 16 * (1 << 6);
2392 iso14a_set_timeout(fwt
/ (8 * 16));
2394 for (uint8_t s
= 0; s
< sectorcnt
; s
++) {
2396 uint64_t ui64Key
= emlGetKey(s
, keytype
);
2398 // MIFARE Classic 1K Ev1 , MIFARE Classic MINI Ev1
2399 if (sectorcnt
== 18) {
2400 // MFC 1K EV1, skip sector 16 since its lockdown
2402 // unknown sector trailer, keep the keys, set only the AC
2403 uint8_t st
[16] = {0x00};
2404 emlGetMem(st
, FirstBlockOfSector(s
) + 3, 1);
2405 memcpy(st
+ 6, "\x70\xF0\xF8\x69", 4);
2406 emlSetMem_xt(st
, FirstBlockOfSector(s
) + 3, 1, 16);
2410 // ICEMAN: ugly hack, we don't want to trigger the partial load message
2411 // MFC 1K EV1 sector 17 don't use key A.
2412 // not mention we don't save signatures in our MFC dump files.
2413 if (s
== 17 && keytype
== MF_KEY_A
) {
2414 ui64Key
= 0x4B791BEA7BCC;
2420 if (have_uid
== false) { // need a full select cycle to get the uid first
2421 iso14a_card_select_t card_info
;
2422 if (iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0, true) == 0) {
2424 // first attempt, if no card let's stop directly
2425 retval
= PM3_EFAILED
;
2426 if (g_dbglevel
>= DBG_ERROR
) {
2427 Dbprintf("Card not found");
2434 switch (card_info
.uidlen
) {
2448 } else { // no need for anticollision. We can directly select the card
2449 if (!bd_authenticated
) { // no need to select if bd_authenticated with backdoor
2450 if (iso14443a_fast_select_card(uid
, cascade_levels
) == 0) {
2457 if (keytype
> MF_KEY_B
) {
2458 if (! bd_authenticated
) {
2459 ui64Key
= bytes_to_num(key
, 6);
2460 if (mifare_classic_auth(pcs
, cuid
, 0, keytype
, ui64Key
, AUTH_FIRST
)) {
2461 retval
= PM3_EFAILED
;
2462 if (g_dbglevel
>= DBG_ERROR
) {
2463 Dbprintf("Auth error");
2467 bd_authenticated
= true;
2469 } else if (mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(s
), keytype
, ui64Key
, AUTH_FIRST
)) {
2471 ui64Key
= emlGetKey(s
, MF_KEY_B
);
2473 if (mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(s
), MF_KEY_B
, ui64Key
, AUTH_FIRST
)) {
2474 retval
= PM3_EPARTIAL
;
2475 if (g_dbglevel
>= DBG_ERROR
) {
2476 Dbprintf("Sector %2d - Auth error", s
);
2483 #define MAX_RETRIES 2
2485 uint8_t data
[16] = {0x00};
2486 for (uint8_t b
= 0; b
< NumBlocksPerSector(s
); b
++) {
2488 memset(data
, 0x00, sizeof(data
));
2489 uint8_t tb
= FirstBlockOfSector(s
) + b
;
2491 for (; r
< MAX_RETRIES
; r
++) {
2493 int res
= mifare_classic_readblock(pcs
, tb
, data
);
2495 retval
|= PM3_EPARTIAL
;
2496 if (g_dbglevel
>= DBG_ERROR
) {
2497 Dbprintf("Error No rights reading sector %2d block %2d", s
, b
);
2501 // retry if wrong len.
2506 // No need to copy empty
2507 if (memcmp(data
, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16) == 0) {
2511 if (IsSectorTrailer(b
)) {
2512 // sector trailer, keep the keys, set only the AC
2513 uint8_t st
[16] = {0x00};
2514 emlGetMem(st
, tb
, 1);
2515 memcpy(st
+ 6, data
+ 6, 4);
2516 emlSetMem_xt(st
, tb
, 1, 16);
2518 emlSetMem_xt(data
, tb
, 1, 16);
2523 // if we failed all retries, notify client
2524 if (r
== MAX_RETRIES
) {
2525 retval
|= PM3_EPARTIAL
;
2531 res
= mifare_classic_halt(pcs
);
2534 iso14a_set_timeout(timeout
);
2535 crypto1_deinit(pcs
);
2536 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2544 //-----------------------------------------------------------------------------
2545 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
2547 // PARAMS - workFlags
2548 // bit 0 - need get UID
2549 // bit 1 - need wupC
2550 // bit 2 - need HALT after sequence
2551 // bit 3 - need turn on FPGA before sequence
2552 // bit 4 - need turn off FPGA
2553 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
2554 // bit 6 - wipe tag.
2555 // bit 7 - use USCUID/GDM (20/23) magic wakeup
2556 //-----------------------------------------------------------------------------
2558 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
) {
2561 uint8_t workFlags
= arg0
;
2562 uint8_t blockNo
= arg1
;
2568 bool isOK
= false; //assume we will get an error
2569 uint8_t errormsg
= 0x00;
2570 uint8_t uid
[10] = {0x00};
2571 uint8_t data
[18] = {0x00};
2574 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
2575 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
2577 if (workFlags
& MAGIC_INIT
) {
2580 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2585 //loop doesn't loop just breaks out if error
2587 // read UID and return to client with write
2588 if (workFlags
& MAGIC_UID
) {
2589 if (!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true)) {
2590 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
2591 errormsg
= MAGIC_UID
;
2592 mifare_classic_halt(NULL
);
2595 mifare_classic_halt(NULL
);
2598 // wipe tag, fill it with zeros
2599 if (workFlags
& MAGIC_WIPE
) {
2600 ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
);
2601 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2602 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupC1 error");
2603 errormsg
= MAGIC_WIPE
;
2607 uint32_t old_timeout
= iso14a_get_timeout();
2610 // 13560000 / 1000 / (8 * 16) * timeout
2611 iso14a_set_timeout(21190);
2613 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
2614 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2615 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wipeC error");
2616 errormsg
= MAGIC_WIPE
;
2619 iso14a_set_timeout(old_timeout
);
2621 mifare_classic_halt(NULL
);
2624 if (workFlags
& MAGIC_GDM_ALT_WUPC
) {
2625 ReaderTransmitBitsPar(wupGDM1
, 7, NULL
, NULL
);
2626 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2627 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupGDM1 error");
2628 errormsg
= MAGIC_WUPC
;
2632 ReaderTransmit(wupGDM2
, sizeof(wupC2
), NULL
);
2633 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2634 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupGDM2 error");
2635 errormsg
= MAGIC_WUPC
;
2641 if (workFlags
& MAGIC_WUPC
) {
2642 ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
);
2643 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2644 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupC1 error");
2645 errormsg
= MAGIC_WUPC
;
2650 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
2651 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2652 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Assuming Magic Gen 1B tag. [wupC2 failed]");
2659 if ((mifare_sendcmd_short(NULL
, CRYPT_NONE
, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
2660 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("write block send command error");
2665 memcpy(data
, datain
, 16);
2666 AddCrc14A(data
, 16);
2668 ReaderTransmit(data
, sizeof(data
), NULL
);
2669 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) {
2670 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("write block send data error");
2675 if (workFlags
& MAGIC_HALT
) {
2676 mifare_classic_halt(NULL
);
2685 reply_mix(CMD_ACK
, 1, 0, 0, uid
, sizeof(uid
));
2687 OnErrorMagic(errormsg
);
2689 if (workFlags
& MAGIC_OFF
)
2693 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
) {
2695 uint8_t workFlags
= arg0
;
2696 uint8_t blockNo
= arg1
;
2697 uint8_t errormsg
= 0x00;
2698 bool isOK
= false; //assume we will get an error
2704 uint8_t data
[MAX_MIFARE_FRAME_SIZE
];
2705 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
2706 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
2708 memset(data
, 0x00, sizeof(data
));
2710 if (workFlags
& MAGIC_INIT
) {
2713 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2718 // increase time-out. Magic card etc are slow
2719 uint32_t timeout
= iso14a_get_timeout();
2720 // frame waiting time (FWT) in 1/fc
2721 uint32_t fwt
= 256 * 16 * (1 << 7);
2722 iso14a_set_timeout(fwt
/ (8 * 16));
2724 //loop doesn't loop just breaks out if error or done
2726 if (workFlags
& MAGIC_GDM_ALT_WUPC
) {
2727 ReaderTransmitBitsPar(wupGDM1
, 7, NULL
, NULL
);
2728 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2729 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupGDM1 error");
2730 errormsg
= MAGIC_WUPC
;
2734 ReaderTransmit(wupGDM2
, sizeof(wupC2
), NULL
);
2735 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2736 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupGDM2 error");
2737 errormsg
= MAGIC_WUPC
;
2742 if (workFlags
& MAGIC_WUPC
) {
2743 ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
);
2744 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2745 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("wupC1 error");
2746 errormsg
= MAGIC_WUPC
;
2750 if (is1b
== false) {
2751 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
2752 if ((ReaderReceive(receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
) == 0) || (receivedAnswer
[0] != 0x0a)) {
2753 if (g_dbglevel
>= DBG_INFO
) Dbprintf("Assuming Magic Gen 1B tag. [wupC2 failed]");
2761 if ((mifare_sendcmd_short(NULL
, CRYPT_NONE
, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, sizeof(receivedAnswer
), receivedAnswerPar
, NULL
) != MAX_MIFARE_FRAME_SIZE
)) {
2762 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("read block send command error");
2767 memcpy(data
, receivedAnswer
, sizeof(data
));
2770 if (workFlags
& MAGIC_HALT
) {
2771 mifare_classic_halt(NULL
);
2777 // if MAGIC_DATAIN, the data stays on device side.
2778 if (workFlags
& MAGIC_DATAIN
) {
2780 memcpy(datain
, data
, sizeof(data
));
2784 reply_old(CMD_ACK
, 1, 0, 0, data
, sizeof(data
));
2786 OnErrorMagic(errormsg
);
2790 if (workFlags
& MAGIC_OFF
) {
2794 iso14a_set_timeout(timeout
);
2797 static void mf_reset_card(void) {
2798 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2800 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2803 void MifareCIdent(bool is_mfc
, uint8_t keytype
, uint8_t *key
) {
2805 uint8_t rec
[1] = {0x00};
2806 uint8_t recpar
[1] = {0x00};
2807 uint8_t rats
[4] = {ISO14443A_CMD_RATS
, 0x80, 0x31, 0x73};
2808 uint8_t rdblf0
[4] = {ISO14443A_CMD_READBLOCK
, 0xF0, 0x8D, 0x5f};
2809 uint8_t rdbl00
[4] = {ISO14443A_CMD_READBLOCK
, 0x00, 0x02, 0xa8};
2810 uint8_t gen4gdmAuth
[4] = {MIFARE_MAGIC_GDM_AUTH_KEY
, 0x00, 0x6C, 0x92};
2811 uint8_t gen4gdmGetConf
[4] = {MIFARE_MAGIC_GDM_READ_CFG
, 0x00, 0x39, 0xF7};
2812 uint8_t gen4GetConf
[8] = {GEN_4GTU_CMD
, 0x00, 0x00, 0x00, 0x00, GEN_4GTU_GETCNF
, 0, 0};
2813 uint8_t superGen1
[9] = {0x0A, 0x00, 0x00, 0xA6, 0xB0, 0x00, 0x10, 0x14, 0x1D};
2814 bool isGen2
= false;
2816 uint8_t *par
= BigBuf_calloc(MAX_PARITY_SIZE
);
2817 uint8_t *buf
= BigBuf_calloc(PM3_CMD_DATA_SIZE
);
2818 uint8_t *uid
= BigBuf_calloc(10);
2819 uint16_t flag
= MAGIC_FLAG_NONE
;
2823 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
2825 // Generation 1 test
2826 ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
);
2827 if (ReaderReceive(rec
, 1, recpar
) && (rec
[0] == 0x0A)) {
2829 flag
= MAGIC_FLAG_GEN_1A
;
2830 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
2831 uint16_t tmp
= ReaderReceive(rec
, 1, recpar
);
2832 if ((tmp
&& (rec
[0] != 0x0A)) || (tmp
== 0)) {
2833 flag
= MAGIC_FLAG_GEN_1B
;
2836 // check for GDM config
2837 ReaderTransmit(gen4gdmGetConf
, sizeof(gen4gdmGetConf
), NULL
);
2838 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2840 flag
|= MAGIC_FLAG_GDM_WUP_40
;
2846 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2848 // Check for Magic Gen4 GTU with default password:
2849 // Get config should return 30 or 32 bytes
2850 AddCrc14A(gen4GetConf
, sizeof(gen4GetConf
) - 2);
2851 ReaderTransmit(gen4GetConf
, sizeof(gen4GetConf
), NULL
);
2852 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2853 if (res
== 32 || res
== 34) {
2854 flag
|= MAGIC_FLAG_GEN_4GTU
;
2861 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, false);
2863 if (cuid
== 0xAA55C396) {
2864 flag
|= MAGIC_FLAG_GEN_UNFUSED
;
2867 ReaderTransmit(rats
, sizeof(rats
), NULL
);
2868 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2871 if (memcmp(buf
, "\x09\x78\x00\x91\x02\xDA\xBC\x19\x10", 9) == 0) {
2872 // test for some MFC gen2
2874 flag
|= MAGIC_FLAG_GEN_2
;
2875 } else if (memcmp(buf
, "\x0D\x78\x00\x71\x02\x88\x49\xA1\x30\x20\x15\x06\x08\x56\x3D", 15) == 0) {
2876 // test for some MFC 7b gen2
2878 flag
|= MAGIC_FLAG_GEN_2
;
2879 } else if (memcmp(buf
, "\x0A\x78\x00\x81\x02\xDB\xA0\xC1\x19\x40\x2A\xB5", 12) == 0) {
2880 // test for Ultralight magic gen2
2882 flag
|= MAGIC_FLAG_GEN_2
;
2883 } else if (memcmp(buf
, "\x85\x00\x00\xA0\x00\x00\x0A\xC3\x00\x04\x03\x01\x01\x00\x0B\x03\x41\xDF", 18) == 0) {
2884 // test for Ultralight EV1 magic gen2
2886 flag
|= MAGIC_FLAG_GEN_2
;
2887 } else if (memcmp(buf
, "\x85\x00\x00\xA0\x0A\x00\x0A\xC3\x00\x04\x03\x01\x01\x00\x0B\x03\x16\xD7", 18) == 0) {
2888 // test for some other Ultralight EV1 magic gen2
2890 flag
|= MAGIC_FLAG_GEN_2
;
2891 } else if (memcmp(buf
, "\x85\x00\x00\xA0\x0A\x00\x0A\xB0\x00\x00\x00\x00\x00\x00\x00\x00\x18\x4D", 18) == 0) {
2892 // test for some other Ultralight magic gen2
2894 flag
|= MAGIC_FLAG_GEN_2
;
2895 } else if (memcmp(buf
, "\x85\x00\x00\xA0\x00\x00\x0A\xA5\x00\x04\x04\x02\x01\x00\x0F\x03\x79\x0C", 18) == 0) {
2896 // test for NTAG213 magic gen2
2898 flag
|= MAGIC_FLAG_GEN_2
;
2901 // test for super card
2902 ReaderTransmit(superGen1
, sizeof(superGen1
), NULL
);
2903 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2905 uint8_t isGen
= MAGIC_FLAG_SUPER_GEN1
;
2907 // check for super card gen2
2908 // not available after RATS, reset card before executing
2911 iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2912 ReaderTransmit(rdbl00
, sizeof(rdbl00
), NULL
);
2913 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2915 isGen
= MAGIC_FLAG_SUPER_GEN2
;
2922 if (is_mfc
== false) {
2926 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2928 ReaderTransmit(rdblf0
, sizeof(rdblf0
), NULL
);
2929 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2931 flag
|= MAGIC_FLAG_NTAG21X
;
2936 struct Crypto1State mpcs
= {0, 0};
2937 struct Crypto1State
*pcs
;
2940 // CUID (with default sector 0 B key) test
2941 // regular cards will NAK the WRITEBLOCK(0) command, while DirectWrite will ACK it
2942 // if we do get an ACK, we immediately abort to ensure nothing is ever actually written
2943 // only perform test if we haven't already identified Gen2. No need test if we have a positive identification already
2944 if (isGen2
== false) {
2947 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2950 uint64_t tmpkey
= bytes_to_num(key
, 6);
2951 if (mifare_classic_authex(pcs
, cuid
, 0, keytype
, tmpkey
, AUTH_FIRST
, NULL
, NULL
) == 0) {
2953 if ((mifare_sendcmd_short(pcs
, 1, ISO14443A_CMD_WRITEBLOCK
, 0, buf
, PM3_CMD_DATA_SIZE
, par
, NULL
) == 1) && (buf
[0] == 0x0A)) {
2954 flag
|= MAGIC_FLAG_GEN_2
;
2955 // turn off immediately to ensure nothing ever accidentally writes to the block
2956 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
2959 crypto1_deinit(pcs
);
2963 // magic MFC Gen3 test 1
2966 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2968 ReaderTransmit(rdbl00
, sizeof(rdbl00
), NULL
);
2969 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2971 flag
|= MAGIC_FLAG_GEN_3
;
2975 // magic MFC Gen4 GDM magic auth test
2978 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2980 ReaderTransmit(gen4gdmAuth
, sizeof(gen4gdmAuth
), NULL
);
2981 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
2983 flag
|= MAGIC_FLAG_GDM_AUTH
;
2990 res
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true);
2992 if (mifare_classic_authex(pcs
, cuid
, 68, MF_KEY_B
, 0x707B11FC1481, AUTH_FIRST
, NULL
, NULL
) == 0) {
2993 flag
|= MAGIC_FLAG_QL88
;
2995 crypto1_deinit(pcs
);
3000 // GDM alt magic wakeup (20)
3001 ReaderTransmitBitsPar(wupGDM1
, 7, NULL
, NULL
);
3002 if (ReaderReceive(rec
, 1, recpar
) && (rec
[0] == 0x0a)) {
3003 flag
|= MAGIC_FLAG_GDM_WUP_20
;
3006 reply_ng(CMD_HF_MIFARE_CIDENT
, PM3_SUCCESS
, (uint8_t *)&flag
, sizeof(uint16_t));
3012 void MifareHasStaticNonce(void) {
3014 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3017 int retval
= PM3_SUCCESS
;
3019 uint8_t *uid
= BigBuf_malloc(10);
3021 memset(uid
, 0x00, 10);
3023 uint8_t data
[1] = { NONCE_FAIL
};
3024 struct Crypto1State mpcs
= {0, 0};
3025 struct Crypto1State
*pcs
;
3028 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3030 uint8_t counter
= 0;
3031 for (uint8_t i
= 0; i
< 3; i
++) {
3033 if (!iso14443a_select_card(uid
, NULL
, NULL
, true, 0, true)) {
3038 uint8_t rec
[4] = {0x00};
3039 uint8_t recpar
[1] = {0x00};
3040 // Transmit MIFARE_CLASSIC_AUTH 0x60, block 0
3041 int len
= mifare_sendcmd_short(pcs
, false, MIFARE_AUTH_KEYA
, 0, rec
, sizeof(rec
), recpar
, NULL
);
3047 // Save the tag nonce (nt)
3048 if (nt
== bytes_to_num(rec
, 4)) {
3052 nt
= bytes_to_num(rec
, 4);
3054 // some cards with static nonce need to be reset before next query
3055 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3059 memset(rec
, 0x00, sizeof(rec
));
3063 Dbprintf("Static nonce......... " _YELLOW_("%08x"), nt
);
3064 data
[0] = NONCE_STATIC
;
3066 data
[0] = NONCE_NORMAL
;
3070 reply_ng(CMD_HF_MIFARE_STATIC_NONCE
, retval
, data
, sizeof(data
));
3074 crypto1_deinit(pcs
);
3077 // FUDAN card w static encrypted nonces
3078 // 2B F9 1C 1B D5 08 48 48 03 A4 B1 B1 75 FF 2D 90
3081 void MifareHasStaticEncryptedNonce(uint8_t block_no
, uint8_t key_type
, uint8_t *key
, uint8_t block_no_nested
, uint8_t key_type_nested
, uint8_t *key_nested
, uint8_t nr_nested
, bool reset
, bool hardreset
, bool addread
, bool addauth
, bool incblk2
, bool corruptnrar
, bool corruptnrarparity
) {
3082 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3088 int retval
= PM3_SUCCESS
;
3090 uint8_t data
[14] = { NONCE_FAIL
};
3092 struct Crypto1State mpcs
= {0, 0};
3093 struct Crypto1State
*pcs
;
3096 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3098 uint8_t first_nt_counter
= 0;
3099 uint8_t first_nt_repetition_counter
= 0;
3100 uint8_t nested_nt_session_counter
= 0;
3101 uint8_t nested_nt_repetition_counter
= 0;
3102 uint8_t first_and_nested_nt_repetition_counter
= 0;
3103 uint8_t key_auth_cmd
= MIFARE_AUTH_KEYA
+ key_type
;
3104 uint8_t key_auth_cmd_nested
= MIFARE_AUTH_KEYA
+ key_type_nested
;
3105 uint64_t ui64key
= bytes_to_num(key
, 6);
3106 uint64_t ui64key_nested
= bytes_to_num(key_nested
, 6);
3107 uint32_t oldntenc
= 0;
3108 bool need_first_auth
= true;
3111 uint32_t old_nt
= 0;
3112 uint32_t nt_first
= 0;
3113 uint32_t old_nt_first
= 0;
3115 uint8_t ntencpar
= 0;
3116 bool is_last_auth_first_auth
= true;
3117 if (nr_nested
== 0) {
3119 if (iso14443a_select_card(NULL
, NULL
, &cuid
, true, 0, true) == false) {
3120 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Select error");
3124 if (mifare_classic_authex_cmd(pcs
, cuid
, block_no
, key_auth_cmd
, ui64key
, AUTH_FIRST
, &nt
, NULL
, NULL
, NULL
, corruptnrar
, corruptnrarparity
)) {
3125 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
3130 } else for (uint8_t i
= 0; i
< nr_nested
; i
++) {
3131 if (need_first_auth
) {
3135 if (g_dbglevel
>= DBG_EXTENDED
) {
3136 Dbprintf("RF reset");
3138 // some cards need longer than mf_reset_card() to see effect on nT
3139 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3141 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3143 if (g_dbglevel
>= DBG_EXTENDED
) {
3146 if (iso14443a_select_card(NULL
, NULL
, &cuid
, true, 0, true) == false) {
3150 if (mifare_classic_authex_cmd(pcs
, cuid
, block_no
, key_auth_cmd
, ui64key
, AUTH_FIRST
, &nt_first
, NULL
, NULL
, NULL
, corruptnrar
, corruptnrarparity
)) {
3151 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
3155 is_last_auth_first_auth
= true;
3157 if ((first_nt_counter
> 1) && (old_nt_first
== nt_first
)) {
3158 first_nt_repetition_counter
++;
3160 old_nt_first
= nt_first
;
3161 if (!reset
&& !hardreset
) {
3162 need_first_auth
= false;
3165 uint8_t dataread
[16] = {0x00};
3166 mifare_classic_readblock(pcs
, block_no
, dataread
);
3169 if (mifare_classic_authex_cmd(pcs
, cuid
, block_no
, key_auth_cmd
, ui64key
, AUTH_NESTED
, &nt
, NULL
, NULL
, NULL
, false, false)) {
3170 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
3173 } else if (g_dbglevel
>= DBG_EXTENDED
) {
3174 Dbprintf("Nonce distance: %5i (first nonce <> nested nonce)", nonce_distance(nt_first
, nt
));
3176 is_last_auth_first_auth
= false;
3177 if (nt
== nt_first
) {
3178 first_and_nested_nt_repetition_counter
++;
3186 if (mifare_classic_authex_cmd(pcs
, cuid
, incblk2
? block_no_nested
+ (i
* 4) : block_no_nested
, key_auth_cmd_nested
, ui64key_nested
, AUTH_NESTED
, &nt
, &ntenc
, &ntencpar
, NULL
, false, false)) {
3187 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Nested auth error");
3188 need_first_auth
= true;
3189 } else if (g_dbglevel
>= DBG_EXTENDED
) {
3190 if (is_last_auth_first_auth
) {
3191 Dbprintf("Nonce distance: %5i (first nonce <> nested nonce)", nonce_distance(nt_first
, nt
));
3193 Dbprintf("Nonce distance: %5i", nonce_distance(old_nt
, nt
));
3196 nested_nt_session_counter
++;
3197 is_last_auth_first_auth
= false;
3199 if (nt
== nt_first
) {
3200 first_and_nested_nt_repetition_counter
++;
3202 if ((nested_nt_session_counter
> 1) && (oldntenc
== ntenc
)) {
3203 nested_nt_repetition_counter
++;
3208 data
[1] = (cuid
>> 24) & 0xFF;
3209 data
[2] = (cuid
>> 16) & 0xFF;
3210 data
[3] = (cuid
>> 8) & 0xFF;
3211 data
[4] = (cuid
>> 0) & 0xFF;
3212 if (first_and_nested_nt_repetition_counter
) {
3213 data
[0] = NONCE_SUPERSTATIC
;
3214 data
[5] = (nt
>> 24) & 0xFF;
3215 data
[6] = (nt
>> 16) & 0xFF;
3216 data
[7] = (nt
>> 8) & 0xFF;
3217 data
[8] = (nt
>> 0) & 0xFF;
3218 } else if (first_nt_repetition_counter
) {
3219 data
[0] = NONCE_STATIC
;
3220 data
[5] = (nt_first
>> 24) & 0xFF;
3221 data
[6] = (nt_first
>> 16) & 0xFF;
3222 data
[7] = (nt_first
>> 8) & 0xFF;
3223 data
[8] = (nt_first
>> 0) & 0xFF;
3224 } else if (nested_nt_repetition_counter
) {
3225 data
[0] = NONCE_STATIC_ENC
;
3226 data
[5] = (nt
>> 24) & 0xFF;
3227 data
[6] = (nt
>> 16) & 0xFF;
3228 data
[7] = (nt
>> 8) & 0xFF;
3229 data
[8] = (nt
>> 0) & 0xFF;
3230 data
[9] = (ntenc
>> 24) & 0xFF;
3231 data
[10] = (ntenc
>> 16) & 0xFF;
3232 data
[11] = (ntenc
>> 8) & 0xFF;
3233 data
[12] = (ntenc
>> 0) & 0xFF;
3234 data
[13] = ntencpar
;
3236 data
[0] = NONCE_NORMAL
;
3237 data
[5] = (nt
>> 24) & 0xFF;
3238 data
[6] = (nt
>> 16) & 0xFF;
3239 data
[7] = (nt
>> 8) & 0xFF;
3240 data
[8] = (nt
>> 0) & 0xFF;
3241 data
[9] = (ntenc
>> 24) & 0xFF;
3242 data
[10] = (ntenc
>> 16) & 0xFF;
3243 data
[11] = (ntenc
>> 8) & 0xFF;
3244 data
[12] = (ntenc
>> 0) & 0xFF;
3245 data
[13] = ntencpar
;
3249 crypto1_deinit(pcs
);
3251 reply_ng(CMD_HF_MIFARE_STATIC_ENCRYPTED_NONCE
, retval
, data
, sizeof(data
));
3257 void OnSuccessMagic(void) {
3258 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3263 void OnErrorMagic(uint8_t reason
) {
3264 // ACK, ISOK, reason,0,0,0
3265 reply_mix(CMD_ACK
, 0, reason
, 0, 0, 0);
3269 int DoGen3Cmd(uint8_t *cmd
, uint8_t cmd_len
) {
3270 int retval
= PM3_SUCCESS
;
3271 uint8_t *par
= BigBuf_calloc(MAX_PARITY_SIZE
);
3272 uint8_t *buf
= BigBuf_calloc(PM3_CMD_DATA_SIZE
);
3275 uint32_t save_iso14a_timeout
= iso14a_get_timeout();
3276 iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 2000); // 2 seconds timeout
3278 ReaderTransmit(cmd
, cmd_len
, NULL
);
3279 int res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
3280 if (res
== 4 && memcmp(buf
, "\x90\x00\xfd\x07", 4) == 0) {
3281 // timeout for card memory reset
3284 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Card operation not completed");
3287 iso14a_set_timeout(save_iso14a_timeout
);
3292 void MifareGen3UID(uint8_t uidlen
, uint8_t *uid
) {
3293 int retval
= PM3_SUCCESS
;
3294 uint8_t uid_cmd
[5] = { 0x90, 0xfb, 0xcc, 0xcc, 0x07 };
3295 uint8_t *old_uid
= BigBuf_calloc(10);
3296 uint8_t *cmd
= BigBuf_calloc(sizeof(uid_cmd
) + uidlen
+ 2);
3297 iso14a_card_select_t
*card_info
= (iso14a_card_select_t
*) BigBuf_calloc(sizeof(iso14a_card_select_t
));
3300 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3304 if (iso14443a_select_card(old_uid
, card_info
, NULL
, true, 0, true) == false) {
3308 if (card_info
->uidlen
!= uidlen
) {
3309 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Wrong UID length");
3314 memcpy(cmd
, uid_cmd
, sizeof(uid_cmd
));
3315 memcpy(&cmd
[sizeof(uid_cmd
)], uid
, uidlen
);
3316 AddCrc14A(cmd
, sizeof(uid_cmd
) + uidlen
);
3318 retval
= DoGen3Cmd(cmd
, sizeof(uid_cmd
) + uidlen
+ 2);
3321 reply_ng(CMD_HF_MIFARE_GEN3UID
, retval
, old_uid
, uidlen
);
3327 void MifareGen3Blk(uint8_t block_len
, uint8_t *block
) {
3329 int retval
= PM3_SUCCESS
;
3330 uint8_t block_cmd
[5] = { 0x90, 0xf0, 0xcc, 0xcc, 0x10 };
3331 uint8_t cmdlen
= sizeof(block_cmd
) + MAX_MIFARE_FRAME_SIZE
;
3332 uint8_t *cmd
= BigBuf_calloc(cmdlen
);
3334 iso14a_card_select_t
*card_info
= (iso14a_card_select_t
*) BigBuf_calloc(sizeof(iso14a_card_select_t
));
3337 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3341 if (iso14443a_select_card(NULL
, card_info
, NULL
, true, 0, true) == false) {
3346 bool doReselect
= false;
3347 if (block_len
< MIFARE_BLOCK_SIZE
) {
3348 if ((mifare_sendcmd_short(NULL
, CRYPT_NONE
, ISO14443A_CMD_READBLOCK
, 0, &cmd
[sizeof(block_cmd
)], MAX_MIFARE_FRAME_SIZE
, NULL
, NULL
) != MAX_MIFARE_FRAME_SIZE
)) {
3349 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Read manufacturer block failed");
3356 if (block_len
> 0) {
3357 memcpy(cmd
, block_cmd
, sizeof(block_cmd
));
3358 memcpy(&cmd
[sizeof(block_cmd
)], block
, block_len
);
3359 int ofs
= sizeof(block_cmd
);
3360 if (card_info
->uidlen
== 4) {
3361 cmd
[ofs
+ 4] = cmd
[ofs
+ 0] ^ cmd
[ofs
+ 1] ^ cmd
[ofs
+ 2] ^ cmd
[ofs
+ 3];
3363 } else if (card_info
->uidlen
== 7) {
3366 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Wrong Card UID length");
3370 cmd
[ofs
++] = card_info
->sak
;
3371 cmd
[ofs
++] = card_info
->atqa
[0];
3372 cmd
[ofs
++] = card_info
->atqa
[1];
3373 AddCrc14A(cmd
, sizeof(block_cmd
) + MIFARE_BLOCK_SIZE
);
3376 if (!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true)) {
3382 retval
= DoGen3Cmd(cmd
, sizeof(block_cmd
) + MAX_MIFARE_FRAME_SIZE
);
3386 reply_ng(CMD_HF_MIFARE_GEN3BLK
, retval
, &cmd
[sizeof(block_cmd
)], MIFARE_BLOCK_SIZE
);
3392 void MifareGen3Freez(void) {
3395 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3399 int retval
= PM3_SUCCESS
;
3400 uint8_t freeze_cmd
[7] = { 0x90, 0xfd, 0x11, 0x11, 0x00, 0xe7, 0x91 };
3402 if (iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true) == false) {
3407 retval
= DoGen3Cmd(freeze_cmd
, sizeof(freeze_cmd
));
3410 reply_ng(CMD_HF_MIFARE_GEN3FREEZ
, retval
, NULL
, 0);
3416 void MifareG4ReadBlk(uint8_t blockno
, uint8_t *pwd
, uint8_t workFlags
) {
3417 bool setup
= ((workFlags
& MAGIC_INIT
) == MAGIC_INIT
) ;
3418 bool done
= ((workFlags
& MAGIC_OFF
) == MAGIC_OFF
) ;
3421 int retval
= PM3_SUCCESS
;
3423 uint8_t *buf
= BigBuf_malloc(PM3_CMD_DATA_SIZE
);
3425 retval
= PM3_EMALLOC
;
3429 uint8_t *par
= BigBuf_malloc(MAX_PARITY_SIZE
);
3431 retval
= PM3_EMALLOC
;
3437 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3441 if (iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true) == false) {
3449 static uint32_t save_iso14a_timeout
;
3451 save_iso14a_timeout
= iso14a_get_timeout();
3452 iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout
3455 uint8_t cmd
[] = { GEN_4GTU_CMD
, 0x00, 0x00, 0x00, 0x00, GEN_4GTU_READ
, blockno
,
3459 memcpy(cmd
+ 1, pwd
, 4);
3461 AddCrc14A(cmd
, sizeof(cmd
) - 2);
3463 ReaderTransmit(cmd
, sizeof(cmd
), NULL
);
3464 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
3470 if (done
|| retval
!= 0) {
3471 iso14a_set_timeout(save_iso14a_timeout
);
3477 reply_ng(CMD_HF_MIFARE_G4_RDBL
, retval
, buf
, res
);
3480 if (done
|| retval
!= 0) {
3481 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3485 if (done
|| retval
!= 0) {
3492 void MifareG4WriteBlk(uint8_t blockno
, uint8_t *pwd
, uint8_t *data
, uint8_t workFlags
) {
3493 bool setup
= ((workFlags
& MAGIC_INIT
) == MAGIC_INIT
) ;
3494 bool done
= ((workFlags
& MAGIC_OFF
) == MAGIC_OFF
) ;
3497 int retval
= PM3_SUCCESS
;
3499 uint8_t *buf
= BigBuf_malloc(PM3_CMD_DATA_SIZE
);
3501 retval
= PM3_EMALLOC
;
3507 retval
= PM3_EINVARG
;
3511 uint8_t *par
= BigBuf_malloc(MAX_PARITY_SIZE
);
3513 retval
= PM3_EMALLOC
;
3519 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3523 if (iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true) == false) {
3531 static uint32_t save_iso14a_timeout
;
3533 save_iso14a_timeout
= iso14a_get_timeout();
3534 iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout
3537 uint8_t cmd
[] = { GEN_4GTU_CMD
, 0x00, 0x00, 0x00, 0x00, GEN_4GTU_WRITE
, blockno
,
3538 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3539 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3543 memcpy(cmd
+ 1, pwd
, 4);
3544 memcpy(cmd
+ 7, data
, 16);
3546 AddCrc14A(cmd
, sizeof(cmd
) - 2);
3548 ReaderTransmit(cmd
, sizeof(cmd
), NULL
);
3549 res
= ReaderReceive(buf
, PM3_CMD_DATA_SIZE
, par
);
3551 if ((res
!= 4) || (memcmp(buf
, "\x90\x00\xfd\x07", 4) != 0)) {
3555 if (done
|| retval
!= 0) {
3556 iso14a_set_timeout(save_iso14a_timeout
);
3561 reply_ng(CMD_HF_MIFARE_G4_WRBL
, retval
, buf
, res
);
3563 if (done
|| retval
!= 0) {
3564 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3568 if (done
|| retval
!= 0) {
3575 void MifareSetMod(uint8_t *datain
) {
3577 uint8_t mod
= datain
[0];
3578 uint64_t ui64Key
= bytes_to_num(datain
+ 1, 6);
3581 uint16_t isOK
= PM3_EUNDEF
;
3582 uint8_t *uid
= BigBuf_calloc(10);
3585 struct Crypto1State mpcs
= {0, 0};
3586 struct Crypto1State
*pcs
= &mpcs
;
3588 uint8_t *buf
= BigBuf_calloc(MAX_MIFARE_FRAME_SIZE
);
3589 uint8_t *par
= BigBuf_calloc(MAX_MIFARE_PARITY_SIZE
);
3592 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3600 if (iso14443a_select_card(uid
, NULL
, &cuid
, true, 0, true) == false) {
3601 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
3605 if (mifare_classic_auth(pcs
, cuid
, 0, 0, ui64Key
, AUTH_FIRST
)) {
3606 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Auth error");
3611 if (((respLen
= mifare_sendcmd_short(pcs
, CRYPT_ALL
, MIFARE_EV1_SETMOD
, mod
, buf
, MAX_MIFARE_FRAME_SIZE
, par
, NULL
)) != 1) || (buf
[0] != 0x0a)) {
3612 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("SetMod error; response[0]: %hhX, len: %d", buf
[0], respLen
);
3616 if (mifare_classic_halt(pcs
)) {
3617 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Halt error");
3625 crypto1_deinit(pcs
);
3628 reply_ng(CMD_HF_MIFARE_SETMOD
, isOK
, NULL
, 0);
3631 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3639 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
) {
3640 uint8_t dataout
[12] = {0x00};
3643 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3647 int len
= iso14443a_select_card(NULL
, NULL
, &cuid
, true, 0, false);
3649 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
3654 if (mifare_desfire_des_auth1(cuid
, dataout
) != PM3_SUCCESS
) {
3655 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Authentication part1: Fail.");
3660 if (g_dbglevel
>= DBG_EXTENDED
) DbpString("AUTH 1 FINISHED");
3661 reply_mix(CMD_ACK
, 1, cuid
, 0, dataout
, sizeof(dataout
));
3664 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
) {
3665 uint32_t cuid
= arg0
;
3666 uint8_t key
[16] = {0x00};
3667 uint8_t dataout
[12] = {0x00};
3670 memcpy(key
, datain
, 16);
3672 isOK
= mifare_desfire_des_auth2(cuid
, key
, dataout
);
3674 if (isOK
!= PM3_SUCCESS
) {
3675 if (g_dbglevel
>= DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");
3680 if (g_dbglevel
>= DBG_EXTENDED
) DbpString("AUTH 2 FINISHED");
3682 reply_old(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
));
3683 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
3689 // Tear-off attack against MFU.
3691 void MifareU_Otp_Tearoff(uint8_t blno
, uint32_t tearoff_time
, uint8_t *data_testwrite
) {
3692 uint8_t blockNo
= blno
;
3694 if (g_dbglevel
>= DBG_DEBUG
) DbpString("Preparing OTP tear-off");
3696 if (tearoff_time
> 43000) {
3697 tearoff_time
= 43000;
3700 g_tearoff_delay_us
= tearoff_time
;
3701 g_tearoff_enabled
= true;
3704 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3708 // write cmd to send, include CRC
3709 // 1b write, 1b block, 4b data, 2 crc
3711 MIFARE_ULC_WRITE
, blockNo
,
3712 data_testwrite
[0], data_testwrite
[1], data_testwrite
[2], data_testwrite
[3],
3715 AddCrc14A(cmd
, sizeof(cmd
) - 2);
3717 // anticollision / select card
3718 if (iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true) == false) {
3719 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
3721 reply_ng(CMD_HF_MFU_OTP_TEAROFF
, PM3_EFAILED
, NULL
, 0);
3726 ReaderTransmit(cmd
, sizeof(cmd
), NULL
);
3728 reply_ng(CMD_HF_MFU_OTP_TEAROFF
, PM3_SUCCESS
, NULL
, 0);
3732 // Tear-off attack against MFU counter
3733 void MifareU_Counter_Tearoff(uint8_t counter
, uint32_t tearoff_time
, uint8_t *datain
) {
3735 if (tearoff_time
> 43000) {
3736 tearoff_time
= 43000;
3740 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
3744 // Send MFU counter increase cmd
3746 MIFARE_ULEV1_INCR_CNT
,
3755 AddCrc14A(cmd
, sizeof(cmd
) - 2);
3757 // anticollision / select card
3758 if (iso14443a_select_card(NULL
, NULL
, NULL
, true, 0, true) == false) {
3759 if (g_dbglevel
>= DBG_ERROR
) Dbprintf("Can't select card");
3767 ReaderTransmit(cmd
, sizeof(cmd
), NULL
);
3769 SpinDelayUsPrecision(tearoff_time
);
3772 reply_ng(CMD_HF_MFU_COUNTER_TEAROFF
, PM3_SUCCESS
, NULL
, 0);