-// iso14a_set_tracing(TRUE);\r
-
-}
-
-void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)
-{
- // params
- uint8_t blockNo = arg0;
- byte_t blockdata[16];
-
- memset(blockdata,'\0',16);
- memcpy(blockdata, datain,16);
-
- // variables
- byte_t isOK = 0;
- uint8_t uid[10];
- uint32_t cuid;
-
- // clear trace
- iso14a_clear_trace();
- // iso14a_set_tracing(false);
-
- iso14443a_setup();
-
- LED_A_ON();
- LED_B_OFF();
- LED_C_OFF();
-
- while (true) {
- if(!iso14443a_select_card(uid, NULL, &cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
- break;
- };
-
- if(mifare_ultra_writeblock(cuid, blockNo, blockdata)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
- break;
- };
-
- if(mifare_ultra_halt(cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
- break;
- };
-
- isOK = 1;
- break;
- }
-
- if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
-
- // add trace trailer
- memset(uid, 0x44, 4);
- LogTrace(uid, 4, 0, 0, TRUE);
-
- LED_B_ON();
- cmd_send(CMD_ACK,isOK,0,0,0,0);
-// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
- LED_B_OFF();
-
-
- // Thats it...
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LEDsoff();
-// iso14a_set_tracing(TRUE);
-
-}
-
-void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)
-{
- // params
- uint8_t blockNo = arg0;
- byte_t blockdata[4];
-
- memcpy(blockdata, datain,4);
-
- // variables
- byte_t isOK = 0;
- uint8_t uid[10];
- uint32_t cuid;
-
- // clear trace
- iso14a_clear_trace();
- // iso14a_set_tracing(false);
-
- iso14443a_setup();
-
- LED_A_ON();
- LED_B_OFF();
- LED_C_OFF();
-
- while (true) {
- if(!iso14443a_select_card(uid, NULL, &cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
- break;
- };
-
- if(mifare_ultra_special_writeblock(cuid, blockNo, blockdata)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
- break;
- };
-
- if(mifare_ultra_halt(cuid)) {
- if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
- break;
- };
-
- isOK = 1;
- break;
- }
-
- if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
-
- // add trace trailer
- memset(uid, 0x44, 4);
- LogTrace(uid, 4, 0, 0, TRUE);
-
- LED_B_ON();
- cmd_send(CMD_ACK,isOK,0,0,0,0);
-// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
- LED_B_OFF();
-
-
- // Thats it...
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LEDsoff();
-// iso14a_set_tracing(TRUE);
-
-}
-
-// Return 1 if the nonce is invalid else return 0
-int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, byte_t * parity) {
- return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
+}\r
+\r
+/* // Command not needed but left for future testing \r
+void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r
+{\r
+ uint8_t blockNo = arg0;\r
+ byte_t blockdata[16] = {0x00};\r
+\r
+ memcpy(blockdata, datain, 16);\r
+\r
+ uint8_t uid[10] = {0x00};\r
+\r
+ LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
+\r
+ clear_trace();\r
+ iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
+\r
+ if(!iso14443a_select_card(uid, NULL, NULL)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(0);\r
+ return; };\r
+\r
+ if(mifare_ultra_halt()) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
+\r
+ cmd_send(CMD_ACK,1,0,0,0,0);\r
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+ LEDsoff();\r
+}\r
+*/\r
+\r
+// Arg0 : Block to write to.\r
+// Arg1 : 0 = use no authentication.\r
+// 1 = use 0x1A authentication.\r
+// 2 = use 0x1B authentication.\r
+// datain : 4 first bytes is data to be written.\r
+// : 4/16 next bytes is authentication key.\r
+void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
+{\r
+ uint8_t blockNo = arg0;\r
+ bool useKey = (arg1 == 1); //UL_C\r
+ bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
+ byte_t blockdata[4] = {0x00};\r
+\r
+ memcpy(blockdata, datain,4);\r
+ \r
+ LEDsoff();\r
+ LED_A_ON();\r
+ iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
+\r
+ clear_trace();\r
+\r
+ if(!iso14443a_select_card(NULL, NULL, NULL)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ // UL-C authentication\r
+ if ( useKey ) {\r
+ uint8_t key[16] = {0x00};\r
+ memcpy(key, datain+4, sizeof(key) );\r
+\r
+ if ( !mifare_ultra_auth(key) ) {\r
+ OnError(1);\r
+ return;\r
+ }\r
+ }\r
+ \r
+ // UL-EV1 / NTAG authentication\r
+ if (usePwd) {\r
+ uint8_t pwd[4] = {0x00};\r
+ memcpy(pwd, datain+4, 4);\r
+ uint8_t pack[4] = {0,0,0,0};\r
+ if (!mifare_ul_ev1_auth(pwd, pack)) {\r
+ OnError(1);\r
+ return;\r
+ }\r
+ }\r
+\r
+ if(mifare_ultra_writeblock(blockNo, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ if(mifare_ultra_halt()) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
+\r
+ cmd_send(CMD_ACK,1,0,0,0,0);\r
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+ LEDsoff();\r
+}\r
+\r
+void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r
+ \r
+ uint8_t pwd[16] = {0x00};\r
+ byte_t blockdata[4] = {0x00};\r
+ \r
+ memcpy(pwd, datain, 16);\r
+ \r
+ LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
+ iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
+\r
+ clear_trace();\r
+\r
+ if(!iso14443a_select_card(NULL, NULL, NULL)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[7];\r
+ blockdata[1] = pwd[6];\r
+ blockdata[2] = pwd[5];\r
+ blockdata[3] = pwd[4];\r
+ if(mifare_ultra_writeblock( 44, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(44);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[3];\r
+ blockdata[1] = pwd[2];\r
+ blockdata[2] = pwd[1];\r
+ blockdata[3] = pwd[0];\r
+ if(mifare_ultra_writeblock( 45, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(45);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[15];\r
+ blockdata[1] = pwd[14];\r
+ blockdata[2] = pwd[13];\r
+ blockdata[3] = pwd[12];\r
+ if(mifare_ultra_writeblock( 46, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(46);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[11];\r
+ blockdata[1] = pwd[10];\r
+ blockdata[2] = pwd[9];\r
+ blockdata[3] = pwd[8];\r
+ if(mifare_ultra_writeblock( 47, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(47);\r
+ return;\r
+ }; \r
+\r
+ if(mifare_ultra_halt()) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ cmd_send(CMD_ACK,1,0,0,0,0);\r
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+ LEDsoff();\r
+}\r
+\r
+// Return 1 if the nonce is invalid else return 0\r
+int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
+ return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r