]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/mifarecmd.c
Nested loop fix for static nonces
[proxmark3-svn] / armsrc / mifarecmd.c
index bed2f0764c6073050e854083dfb7f2182a8ed959..89afe06a42f8a626df3b564d21e16d13826eb2f1 100644 (file)
@@ -779,6 +779,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
        uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r
        uint8_t par[1];\r
        uint32_t target_nt[2], target_ks[2];\r
+       uint8_t target_nt_duplicate_count = 0;\r
 \r
        uint8_t par_array[4];\r
        uint16_t ncount = 0;\r
@@ -814,12 +815,6 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
 \r
                for (rtr = 0; rtr < 17; rtr++) {\r
 \r
-                       // Test if the action was cancelled\r
-                       if(BUTTON_PRESS()) {\r
-                               isOK = -2;\r
-                               break;\r
-                       }\r
-\r
                        // prepare next select. No need to power down the card.\r
                        if(mifare_classic_halt(pcs, cuid)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Nested: Halt error");\r
@@ -827,6 +822,12 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                continue;\r
                        }\r
 \r
+                       // Test if the action was cancelled\r
+                       if(BUTTON_PRESS()) {\r
+                               isOK = -2;\r
+                               break;\r
+                       }\r
+\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
@@ -893,7 +894,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
        for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
 \r
                target_nt[i] = 0;\r
-               while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
+               while(target_nt[i] == 0 && !isOK) { // continue until we have an unambiguous nonce\r
 \r
                        // prepare next select. No need to power down the card.\r
                        if(mifare_classic_halt(pcs, cuid)) {\r
@@ -901,16 +902,22 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                continue;\r
                        }\r
 \r
+                       // break out of the loop on button press\r
+                       if(BUTTON_PRESS()) {\r
+                               isOK = -2;\r
+                               break;\r
+                       }\r
+\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
+                       }\r
 \r
                        auth1_time = 0;\r
                        if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Nested: Auth1 error");\r
                                continue;\r
-                       };\r
+                       }\r
 \r
                        // nested authentication\r
                        auth2_time = auth1_time + delta_time;\r
@@ -918,7 +925,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                        if (len != 4) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Nested: Auth2 error len=%d", len);\r
                                continue;\r
-                       };\r
+                       }\r
 \r
                        nt2 = bytes_to_num(receivedAnswer, 4);\r
                        if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
@@ -944,7 +951,12 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                        target_ks[i] = ks1;\r
                                        ncount++;\r
                                        if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
-                                               target_nt[i] = 0;\r
+                                               if( ++target_nt_duplicate_count >= NESTED_MAX_TRIES ) { // unable to get a 2nd nonce after NESTED_MAX_TRIES tries, probably a fixed nonce\r
+                                                       if (MF_DBGLEVEL >= 2) Dbprintf("Nonce#2: cannot get nonce that != nonce#1, continuing anyway with single nonce! ntdist=%d", j);\r
+                                                       break;\r
+                                               }\r
+\r
+                                               target_nt[1] = 0;\r
                                                if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
                                                break;\r
                                        }\r
@@ -1013,17 +1025,17 @@ void MifareChkKeys(uint16_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
                int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);\r
 \r
                if (res >= 0) {\r
-                       cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80);\r
+                       cmd_send(CMD_ACK, 1, res, 0, keyIndex, 80);\r
                } else {\r
-                       cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
+                       cmd_send(CMD_ACK, 0, res, 0, NULL, 0);\r
                }\r
        } else {        \r
                int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);\r
                \r
                if (res > 0) {\r
-                       cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);\r
+                       cmd_send(CMD_ACK, 1, res, 0, datain + (res - 1) * 6, 6);\r
                } else {\r
-                       cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
+                       cmd_send(CMD_ACK, 0, res, 0, NULL, 0);\r
                }\r
        }\r
 \r
Impressum, Datenschutz