]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/mifarecmd.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / armsrc / mifarecmd.c
index 60a85c80117c571f14132084f915b9a745317e78..ca513cec953c897b00833595ce07781a402085e0 100644 (file)
@@ -20,9 +20,8 @@
 #include "parity.h"\r
 #include "crc.h"\r
 \r
-#define AUTHENTICATION_TIMEOUT 848                     // card times out 1ms after wrong authentication (according to NXP documentation)\r
-#define PRE_AUTHENTICATION_LEADTIME 400                // some (non standard) cards need a pause after select before they are ready for first authentication\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
 // the block number for the ISO14443-4 PCB\r
 static uint8_t pcb_blocknum = 0;\r
@@ -59,7 +58,7 @@ void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        LED_C_OFF();\r
 \r
        while (true) {\r
-               if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+               if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                        if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
                        break;\r
                };\r
@@ -106,7 +105,7 @@ void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){
 \r
        clear_trace();\r
 \r
-       if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\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
@@ -141,7 +140,7 @@ void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
 \r
        clear_trace();\r
 \r
-       int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\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
                OnError(1);\r
@@ -217,7 +216,7 @@ void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        LED_C_OFF();\r
 \r
        isOK = 1;\r
-       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                isOK = 0;\r
                if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
        }\r
@@ -281,7 +280,7 @@ void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
                return;\r
        }\r
 \r
-       int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\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:%d)",len);\r
                OnError(1);\r
@@ -383,7 +382,7 @@ void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        LED_C_OFF();\r
 \r
        while (true) {\r
-                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                        if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
                        break;\r
                };\r
@@ -483,7 +482,7 @@ void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
 \r
        clear_trace();\r
 \r
-       if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
+       if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
                if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
                OnError(0);\r
                return;\r
@@ -542,7 +541,7 @@ void MifareUSetPwd(uint8_t arg0, uint8_t *datain){
 \r
        clear_trace();\r
 \r
-       if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
+       if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
                if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
                OnError(0);\r
                return;\r
@@ -662,7 +661,7 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags,
 \r
                if (!have_uid) { // need a full select cycle to get the uid first\r
                        iso14a_card_select_t card_info;\r
-                       if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
+                       if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0, true)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("AcquireNonces: Can't select card (ALL)");\r
                                continue;\r
                        }\r
@@ -674,14 +673,14 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags,
                        }\r
                        have_uid = true;\r
                } else { // no need for anticollision. We can directly select the card\r
-                       if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
+                       if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels, true)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("AcquireNonces: Can't select card (UID)");\r
                                continue;\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
@@ -698,11 +697,12 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags,
                        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
@@ -807,7 +807,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                continue;\r
                        }\r
 \r
-                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Nested: Can't select card");\r
                                rtr--;\r
                                continue;\r
@@ -881,7 +881,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                continue;\r
                        }\r
 \r
-                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Nested: Can't select card");\r
                                continue;\r
                        };\r
@@ -961,24 +961,14 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
 // MIFARE check keys. key count up to 85.\r
 //\r
 //-----------------------------------------------------------------------------\r
-void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
+void MifareChkKeys(uint16_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
 {\r
        uint8_t blockNo = arg0 & 0xff;\r
        uint8_t keyType = (arg0 >> 8) & 0xff;\r
-       bool clearTrace = arg1;\r
+       bool clearTrace = arg1 & 0x01;\r
+       bool multisectorCheck = arg1 & 0x02;\r
+       uint8_t set14aTimeout = (arg1 >> 8) & 0xff;\r
        uint8_t keyCount = arg2;\r
-       uint64_t ui64Key = 0;\r
-\r
-       bool have_uid = false;\r
-       uint8_t cascade_levels = 0;\r
-       uint32_t timeout = 0;\r
-       int i;\r
-       byte_t isOK = 0;\r
-       uint8_t uid[10];\r
-       uint32_t cuid;\r
-       struct Crypto1State mpcs = {0, 0};\r
-       struct Crypto1State *pcs;\r
-       pcs = &mpcs;\r
 \r
        // clear debug level\r
        int OLD_MF_DBGLEVEL = MF_DBGLEVEL;\r
@@ -992,53 +982,34 @@ void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
        if (clearTrace) clear_trace();\r
        set_tracing(true);\r
 \r
-       for (i = 0; i < keyCount; i++) {\r
-//             if(mifare_classic_halt(pcs, cuid)) {\r
-//                     if (MF_DBGLEVEL >= 1)   Dbprintf("ChkKeys: Halt error");\r
-//             }\r
+       if (set14aTimeout){\r
+               iso14a_set_timeout(set14aTimeout * 10); // timeout: ms = x/106  35-minimum, 50-OK 106-recommended 500-safe\r
+       }\r
+       \r
+       if (multisectorCheck) {\r
+               TKeyIndex keyIndex = {{0}};\r
+               uint8_t sectorCnt = blockNo;\r
+               int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);\r
 \r
-               // Iceman: use piwi's faster nonce collecting part in hardnested.\r
-               if (!have_uid) { // need a full select cycle to get the uid first\r
-                       iso14a_card_select_t card_info;\r
-                       if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
-                               if (OLD_MF_DBGLEVEL >= 1)       Dbprintf("ChkKeys: Can't select card");\r
-                               --i; // try same key once again\r
-                               continue;\r
-                       }\r
-                       switch (card_info.uidlen) {\r
-                               case 4 : cascade_levels = 1; break;\r
-                               case 7 : cascade_levels = 2; break;\r
-                               case 10: cascade_levels = 3; break;\r
-                               default: break;\r
-                       }\r
-                       have_uid = true;\r
-               } else { // no need for anticollision. We can directly select the card\r
-                       if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
-                               if (OLD_MF_DBGLEVEL >= 1)       Dbprintf("ChkKeys: Can't select card (UID)");\r
-                               --i; // try same key once again\r
-                               continue;\r
-                       }\r
+               LED_B_ON();\r
+               if (res >= 0) {\r
+                       cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80);\r
+               } else {\r
+                       cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
                }\r
-\r
-               ui64Key = bytes_to_num(datain + i * 6, 6);\r
-               if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
-                       uint8_t dummy_answer = 0;\r
-                       ReaderTransmit(&dummy_answer, 1, NULL);\r
-                       timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
-\r
-                       // wait for the card to become ready again\r
-                       while(GetCountSspClk() < timeout);\r
-                       continue;\r
+               LED_B_OFF();\r
+       } else {        \r
+               int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);\r
+               \r
+               LED_B_ON();\r
+               if (res > 0) {\r
+                       cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);\r
+               } else {\r
+                       cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
                }\r
-\r
-               isOK = 1;\r
-               break;\r
+               LED_B_OFF();\r
        }\r
 \r
-       LED_B_ON();\r
-    cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);\r
-       LED_B_OFF();\r
-\r
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 \r
@@ -1111,7 +1082,7 @@ void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
 \r
        bool isOK = true;\r
 \r
-       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                isOK = false;\r
                if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
        }\r
@@ -1178,7 +1149,7 @@ static bool isBlockTrailer(int blockN) {
        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
@@ -1349,7 +1320,7 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
 \r
                // get UID from chip\r
                if (workFlags & 0x01) {\r
-                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
+                       if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card");\r
                                // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.\r
                                //break;\r
@@ -1543,6 +1514,14 @@ void MifareCIdent(){
 \r
        uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
        uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
+       \r
+       LED_A_ON();\r
+       LED_B_OFF();\r
+       LED_C_OFF();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
+\r
+       clear_trace();\r
+       set_tracing(true);      \r
 \r
        ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
        if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {\r
@@ -1556,8 +1535,13 @@ void MifareCIdent(){
 \r
        // From iceman1001: removed the if,  since some magic tags misbehavies and send an answer to it.\r
        mifare_classic_halt(NULL, 0);\r
-\r
+       \r
+       LED_B_ON();\r
        cmd_send(CMD_ACK,isOK,0,0,0,0);\r
+       LED_B_OFF();\r
+\r
+       FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+       LEDsoff();      \r
 }\r
 \r
 //\r
@@ -1573,7 +1557,7 @@ void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
        iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
        clear_trace();\r
 \r
-       int len = iso14443a_select_card(uid, NULL, &cuid, true, 0);\r
+       int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);\r
        if(!len) {\r
                if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
                OnError(1);\r
Impressum, Datenschutz