]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/mifarecmd.c
add *.eml to .gitignore
[proxmark3-svn] / armsrc / mifarecmd.c
index fa0ff627554f2853fe927e94d6fbabacdaefad39..6a491b532a73739aaf9da9540fe5014fab2bbe91 100644 (file)
@@ -38,7 +38,7 @@ void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        iso14a_clear_trace();\r
 //     iso14a_set_tracing(false);\r
 \r
        iso14a_clear_trace();\r
 //     iso14a_set_tracing(false);\r
 \r
-       iso14443a_setup();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
@@ -94,6 +94,60 @@ void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
 \r
 }\r
 \r
 \r
 }\r
 \r
+void MifareUReadBlock(uint8_t arg0,uint8_t *datain)\r
+{\r
+    // params\r
+       uint8_t blockNo = arg0;\r
+       \r
+       // variables\r
+       byte_t isOK = 0;\r
+       byte_t dataoutbuf[16];\r
+       uint8_t uid[10];\r
+       uint32_t cuid;\r
+    \r
+       // clear trace\r
+       iso14a_clear_trace();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\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)) {\r
+            if (MF_DBGLEVEL >= 1)      Dbprintf("Can't select card");\r
+                       break;\r
+               };\r
+        \r
+               if(mifare_ultra_readblock(cuid, blockNo, dataoutbuf)) {\r
+            if (MF_DBGLEVEL >= 1)      Dbprintf("Read block error");\r
+                       break;\r
+               };\r
+        \r
+               if(mifare_ultra_halt(cuid)) {\r
+            if (MF_DBGLEVEL >= 1)      Dbprintf("Halt error");\r
+                       break;\r
+               };\r
+               \r
+               isOK = 1;\r
+               break;\r
+       }\r
+       \r
+       if (MF_DBGLEVEL >= 2)   DbpString("READ BLOCK FINISHED");\r
+    \r
+       // add trace trailer\r
+       memset(uid, 0x44, 4);\r
+       LogTrace(uid, 4, 0, 0, TRUE);\r
+       LED_B_ON();\r
+        cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
+       LED_B_OFF();\r
+    \r
+    \r
+    // Thats it...\r
+       FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+       LEDsoff();\r
+}\r
+\r
 //-----------------------------------------------------------------------------\r
 // Select, Authenticaate, Read an MIFARE tag. \r
 // read sector (data = 4 x 16 bytes = 64 bytes)\r
 //-----------------------------------------------------------------------------\r
 // Select, Authenticaate, Read an MIFARE tag. \r
 // read sector (data = 4 x 16 bytes = 64 bytes)\r
@@ -119,7 +173,7 @@ void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        iso14a_clear_trace();\r
 //     iso14a_set_tracing(false);\r
 \r
        iso14a_clear_trace();\r
 //     iso14a_set_tracing(false);\r
 \r
-       iso14443a_setup();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
@@ -191,6 +245,66 @@ void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
 \r
 }\r
 \r
 \r
 }\r
 \r
+void MifareUReadCard(uint8_t arg0, uint8_t *datain)\r
+{\r
+  // params\r
+        uint8_t sectorNo = arg0;\r
+        \r
+        // variables\r
+        byte_t isOK = 0;\r
+        byte_t dataoutbuf[16 * 4];\r
+        uint8_t uid[10];\r
+        uint32_t cuid;\r
+\r
+        // clear trace\r
+        iso14a_clear_trace();\r
+//      iso14a_set_tracing(false);\r
+\r
+               iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\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)) {\r
+                if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
+                        break;\r
+                };\r
+               for(int sec=0;sec<16;sec++){\r
+                    if(mifare_ultra_readblock(cuid, sectorNo * 4 + sec, dataoutbuf + 4 * sec)) {\r
+                    if (MF_DBGLEVEL >= 1)   Dbprintf("Read block %d error",sec);\r
+                        break;\r
+                    };\r
+                }\r
+                if(mifare_ultra_halt(cuid)) {\r
+                if (MF_DBGLEVEL >= 1)   Dbprintf("Halt error");\r
+                        break;\r
+                };\r
+\r
+                isOK = 1;\r
+                break;\r
+        }\r
+        \r
+        if (MF_DBGLEVEL >= 2) DbpString("READ CARD FINISHED");\r
+\r
+        // add trace trailer\r
+        memset(uid, 0x44, 4);\r
+        LogTrace(uid, 4, 0, 0, TRUE);\r
+        \r
+        LED_B_ON();\r
+               cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,64);\r
+  //cmd_send(CMD_ACK,isOK,0,0,dataoutbuf+32, 32);\r
+        LED_B_OFF();\r
+\r
+        // Thats it...\r
+        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+        LEDsoff();\r
+//  iso14a_set_tracing(TRUE);\r
+\r
+}\r
+\r
+\r
 //-----------------------------------------------------------------------------\r
 // Select, Authenticaate, Read an MIFARE tag. \r
 // read block\r
 //-----------------------------------------------------------------------------\r
 // Select, Authenticaate, Read an MIFARE tag. \r
 // read block\r
@@ -218,7 +332,7 @@ void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        iso14a_clear_trace();\r
 //  iso14a_set_tracing(false);\r
 \r
        iso14a_clear_trace();\r
 //  iso14a_set_tracing(false);\r
 \r
-       iso14443a_setup();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
@@ -273,6 +387,131 @@ void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
 \r
 }\r
 \r
 \r
 }\r
 \r
+void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)\r
+{\r
+        // params\r
+        uint8_t blockNo = arg0;\r
+        byte_t blockdata[16];\r
+\r
+        memset(blockdata,'\0',16);\r
+        memcpy(blockdata, datain,16);\r
+        \r
+        // variables\r
+        byte_t isOK = 0;\r
+        uint8_t uid[10];\r
+        uint32_t cuid;\r
+\r
+        // clear trace\r
+        iso14a_clear_trace();\r
+       //  iso14a_set_tracing(false);\r
+\r
+               iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\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)) {\r
+                        if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
+                        break;\r
+                };\r
+\r
+                if(mifare_ultra_writeblock(cuid, blockNo, blockdata)) {\r
+                        if (MF_DBGLEVEL >= 1)   Dbprintf("Write block error");\r
+                        break;\r
+                };\r
+\r
+                if(mifare_ultra_halt(cuid)) {\r
+                        if (MF_DBGLEVEL >= 1)   Dbprintf("Halt error");\r
+                        break;\r
+                };\r
+                \r
+                isOK = 1;\r
+                break;\r
+        }\r
+        \r
+        if (MF_DBGLEVEL >= 2)   DbpString("WRITE BLOCK FINISHED");\r
+\r
+        // add trace trailer\r
+        memset(uid, 0x44, 4);\r
+        LogTrace(uid, 4, 0, 0, TRUE);\r
+\r
+        LED_B_ON();\r
+       cmd_send(CMD_ACK,isOK,0,0,0,0);\r
+//      UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
+        LED_B_OFF();\r
+\r
+\r
+        // Thats it...\r
+        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+        LEDsoff();\r
+//  iso14a_set_tracing(TRUE);\r
+\r
+}\r
+\r
+void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)\r
+{\r
+        // params\r
+        uint8_t blockNo = arg0;\r
+        byte_t blockdata[4];\r
+        \r
+       memcpy(blockdata, datain,4);\r
+\r
+        // variables\r
+        byte_t isOK = 0;\r
+        uint8_t uid[10];\r
+        uint32_t cuid;\r
+\r
+        // clear trace\r
+        iso14a_clear_trace();\r
+        //  iso14a_set_tracing(false);\r
+\r
+               iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\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)) {\r
+                        if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
+                        break;\r
+                };\r
+\r
+                if(mifare_ultra_special_writeblock(cuid, blockNo, blockdata)) {\r
+                        if (MF_DBGLEVEL >= 1)   Dbprintf("Write block error");\r
+                        break;\r
+                };\r
+\r
+                if(mifare_ultra_halt(cuid)) {\r
+                        if (MF_DBGLEVEL >= 1)   Dbprintf("Halt error");\r
+                        break;\r
+                };\r
+\r
+                isOK = 1;\r
+                break;\r
+        }\r
+\r
+        if (MF_DBGLEVEL >= 2)   DbpString("WRITE BLOCK FINISHED");\r
+\r
+        // add trace trailer\r
+       memset(uid, 0x44, 4);\r
+        LogTrace(uid, 4, 0, 0, TRUE);\r
+\r
+       LED_B_ON();\r
+        cmd_send(CMD_ACK,isOK,0,0,0,0);\r
+//      UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
+        LED_B_OFF();\r
+\r
+\r
+        // Thats it...\r
+        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+        LEDsoff();\r
+//  iso14a_set_tracing(TRUE);\r
+\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, byte_t * parity) {\r
        return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
 // Return 1 if the nonce is invalid else return 0\r
 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, byte_t * parity) {\r
        return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
@@ -315,20 +554,16 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
        uint32_t auth1_time, auth2_time;\r
        static uint16_t delta_time;\r
 \r
        uint32_t auth1_time, auth2_time;\r
        static uint16_t delta_time;\r
 \r
-       StartCountMifare();\r
-\r
        // clear trace\r
        iso14a_clear_trace();\r
        iso14a_set_tracing(false);\r
        \r
        // clear trace\r
        iso14a_clear_trace();\r
        iso14a_set_tracing(false);\r
        \r
-       iso14443a_setup();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        LED_A_ON();\r
        LED_C_OFF();\r
 \r
 \r
 \r
        LED_A_ON();\r
        LED_C_OFF();\r
 \r
 \r
-       while((GetCountMifare() & 0xffff0000) != 0x00010000);           // wait for counter to reset and "warm up" \r
-\r
        // statistics on nonce distance\r
        if (calibrate) {        // for first call only. Otherwise reuse previous calibration\r
                LED_B_ON();\r
        // statistics on nonce distance\r
        if (calibrate) {        // for first call only. Otherwise reuse previous calibration\r
                LED_B_ON();\r
@@ -370,8 +605,8 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                continue;\r
                        };\r
 \r
                                continue;\r
                        };\r
 \r
-                       nttmp = prng_successor(nt1, 500);\r
-                       for (i = 501; i < 1200; i++) {\r
+                       nttmp = prng_successor(nt1, 100);                               //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
+                       for (i = 101; i < 1200; i++) {\r
                                nttmp = prng_successor(nttmp, 1);\r
                                if (nttmp == nt2) break;\r
                        }\r
                                nttmp = prng_successor(nttmp, 1);\r
                                if (nttmp == nt2) break;\r
                        }\r
@@ -528,7 +763,7 @@ void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        iso14a_clear_trace();\r
        iso14a_set_tracing(TRUE);\r
 \r
        iso14a_clear_trace();\r
        iso14a_set_tracing(TRUE);\r
 \r
-       iso14443a_setup();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
@@ -635,7 +870,7 @@ void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
        iso14a_clear_trace();\r
        iso14a_set_tracing(false);\r
        \r
        iso14a_clear_trace();\r
        iso14a_set_tracing(false);\r
        \r
-       iso14443a_setup();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
 \r
        LED_A_ON();\r
        LED_B_OFF();\r
@@ -753,7 +988,7 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
                iso14a_clear_trace();\r
                iso14a_set_tracing(TRUE);\r
 \r
                iso14a_clear_trace();\r
                iso14a_set_tracing(TRUE);\r
 \r
-               iso14443a_setup();\r
+               iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
                LED_A_ON();\r
                LED_B_OFF();\r
 \r
                LED_A_ON();\r
                LED_B_OFF();\r
@@ -891,7 +1126,7 @@ void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
                iso14a_clear_trace();\r
                iso14a_set_tracing(TRUE);\r
 \r
                iso14a_clear_trace();\r
                iso14a_set_tracing(TRUE);\r
 \r
-               iso14443a_setup();\r
+               iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
                LED_A_ON();\r
                LED_B_OFF();\r
 \r
                LED_A_ON();\r
                LED_B_OFF();\r
@@ -905,7 +1140,7 @@ void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
 \r
        while (true) {\r
                if (workFlags & 0x02) {\r
 \r
        while (true) {\r
                if (workFlags & 0x02) {\r
-      ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
+                       ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
                        if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("wupC1 error");\r
                                break;\r
                        if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("wupC1 error");\r
                                break;\r
Impressum, Datenschutz