\r
#include "mifarecmd.h"\r
\r
-#include "apps.h"\r
#include "util.h"\r
#include "parity.h"\r
#include "crc.h"\r
+#include "fpgaloader.h"\r
\r
+#define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)\r
+#define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication \r
+\r
+/*\r
// the block number for the ISO14443-4 PCB\r
static uint8_t pcb_blocknum = 0;\r
// Deselect card by sending a s-block. the crc is precalced for speed\r
static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};\r
\r
+static void OnSuccess(){\r
+ pcb_blocknum = 0;\r
+ ReaderTransmit(deselect_cmd, 3 , NULL);\r
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+ LEDsoff();\r
+}\r
+*/\r
+\r
+static void OnError(uint8_t reason){\r
+ // pcb_blocknum = 0;\r
+ // ReaderTransmit(deselect_cmd, 3 , NULL);\r
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+ LED_D_OFF();\r
+ cmd_send(CMD_ACK,0,reason,0,0,0);\r
+ LED_A_OFF();\r
+}\r
+\r
//-----------------------------------------------------------------------------\r
// Select, Authenticate, Read a MIFARE tag.\r
// read block\r
//-----------------------------------------------------------------------------\r
void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
{\r
- // params\r
+ LED_A_ON();\r
+\r
uint8_t blockNo = arg0;\r
uint8_t keyType = arg1;\r
uint64_t ui64Key = 0;\r
ui64Key = bytes_to_num(datain, 6);\r
\r
- // variables\r
byte_t isOK = 0;\r
byte_t dataoutbuf[16];\r
uint8_t uid[10];\r
\r
clear_trace();\r
\r
- LED_A_ON();\r
- LED_B_OFF();\r
- LED_C_OFF();\r
-\r
while (true) {\r
if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
\r
void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r
\r
+ LED_A_ON();\r
bool turnOffField = (arg0 == 1);\r
\r
- LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
-\r
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
\r
- clear_trace();\r
-\r
- if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
+ if (!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
OnError(0);\r
return;\r
};\r
\r
- if(!mifare_ultra_auth(keybytes)){\r
+ if (!mifare_ultra_auth(keybytes)){\r
if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r
OnError(1);\r
return;\r
\r
if (turnOffField) {\r
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
- LEDsoff();\r
+ LED_D_OFF();\r
}\r
+\r
cmd_send(CMD_ACK,1,0,0,0,0);\r
+ LED_A_OFF();\r
}\r
\r
// Arg0 = BlockNo,\r
// datain = PWD bytes,\r
void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
{\r
+ LED_A_ON();\r
+\r
uint8_t blockNo = arg0;\r
byte_t dataout[16] = {0x00};\r
bool useKey = (arg1 == 1); //UL_C\r
bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
\r
- LEDsoff();\r
- LED_A_ON();\r
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
\r
- clear_trace();\r
-\r
int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r
if(!len) {\r
if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
}\r
\r
// UL-C authentication\r
- if ( useKey ) {\r
+ if (useKey) {\r
uint8_t key[16] = {0x00};\r
memcpy(key, datain, sizeof(key) );\r
\r
}\r
\r
// UL-EV1 / NTAG authentication\r
- if ( usePwd ) {\r
+ if (usePwd) {\r
uint8_t pwd[4] = {0x00};\r
memcpy(pwd, datain, 4);\r
uint8_t pack[4] = {0,0,0,0};\r
}\r
}\r
\r
- if( mifare_ultra_readblock(blockNo, dataout) ) {\r
+ if (mifare_ultra_readblock(blockNo, dataout)) {\r
if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
OnError(2);\r
return;\r
}\r
\r
- if( mifare_ultra_halt() ) {\r
+ if (mifare_ultra_halt()) {\r
if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
OnError(3);\r
return;\r
\r
cmd_send(CMD_ACK,1,0,0,dataout,16);\r
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
- LEDsoff();\r
+ LED_D_OFF();\r
+ LED_A_OFF();\r
}\r
\r
//-----------------------------------------------------------------------------\r
// datain = KEY bytes\r
void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
{\r
- LEDsoff();\r
LED_A_ON();\r
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
\r
// free eventually allocated BigBuf memory\r
BigBuf_free();\r
- clear_trace();\r
\r
// params\r
uint8_t blockNo = arg0;\r
}\r
\r
// UL-C authentication\r
- if ( useKey ) {\r
+ if (useKey) {\r
uint8_t key[16] = {0x00};\r
memcpy(key, datain, sizeof(key) );\r
\r
return;\r
}\r
\r
- if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);\r
+ if (MF_DBGLEVEL >= MF_DBG_DEBUG) Dbprintf("Blocks read %d", countblocks);\r
\r
- countblocks *= 4;\r
+ cmd_send(CMD_ACK, 1, countblocks*4, BigBuf_max_traceLen(), 0, 0);\r
\r
- cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);\r
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
- LEDsoff();\r
+ LED_D_OFF();\r
BigBuf_free();\r
+ LED_A_OFF();\r
}\r
\r
//-----------------------------------------------------------------------------\r
}\r
\r
if (slow) {\r
- timeout = GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME;\r
+ timeout = GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME;\r
while(GetCountSspClk() < timeout);\r
}\r
\r
continue;\r
}\r
\r
- // send a dummy byte as reader response in order to trigger the cards authentication timeout\r
- uint8_t dummy_answer = 0;\r
- ReaderTransmit(&dummy_answer, 1, NULL);\r
- timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
+ // send an incomplete dummy response in order to trigger the card's authentication failure timeout\r
+ uint8_t dummy_answer[1] = {0};\r
+ ReaderTransmit(dummy_answer, 1, NULL);\r
\r
+ timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT;\r
+ \r
num_nonces++;\r
if (num_nonces % 2) {\r
memcpy(buf+i, receivedAnswer, 4);\r
if (blockN >= 128 && blockN <= 256) {\r
return ((blockN & 0x0F) == 0x0F);\r
}\r
- return FALSE;\r
+ return false;\r
}\r
\r
void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
LED_A_ON();\r
LED_B_OFF();\r
LED_C_OFF();\r
-// FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
-// SpinDelay(100);\r
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
\r
clear_trace();\r
LEDsoff();\r
}\r
\r
-void OnSuccess(){\r
- pcb_blocknum = 0;\r
- ReaderTransmit(deselect_cmd, 3 , NULL);\r
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
- LEDsoff();\r
-}\r
-\r
-void OnError(uint8_t reason){\r
- pcb_blocknum = 0;\r
- ReaderTransmit(deselect_cmd, 3 , NULL);\r
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
- cmd_send(CMD_ACK,0,reason,0,0,0);\r
- LEDsoff();\r
-}\r