]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
CHG: Syntax suger
authoriceman1001 <iceman@iuse.se>
Mon, 25 Jan 2016 19:28:34 +0000 (20:28 +0100)
committericeman1001 <iceman@iuse.se>
Mon, 25 Jan 2016 19:28:34 +0000 (20:28 +0100)
Experiment:  unfolded a loop.
Experiment: speeding up "Hf mf nested"
CHG: removed some default keys from "hf mf nested". It wasn't being used anyway.
CHG: changed the way "hf mf nested" reports back keys
CHG: "hf mf nested"  and "hf mf chk"   now prints the key-table.
CHG: changed the output list for "hf mf chk"

client/cmdhfmf.c
client/mifarehost.c

index c5a86b5625f0d63a4c5e1173c7ee82eaf6ee5f54..d354952b10d8a2ab8d227c7f9b4ed92b4bb4dbe9 100644 (file)
@@ -63,7 +63,7 @@ start:
                }\r
                \r
                UsbCommand resp;\r
-               if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r
+               if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
                        isOK  = resp.arg[0];\r
                        uid = (uint32_t)bytes_to_num(resp.d.asBytes +  0, 4);\r
                        nt =  (uint32_t)bytes_to_num(resp.d.asBytes +  4, 4);\r
@@ -96,14 +96,12 @@ start:
                c.arg[0] = false;\r
                goto start;\r
        } else {\r
-               isOK = 0;\r
-               printf("------------------------------------------------------------------\n");\r
                PrintAndLog("Found valid key: %012"llx" \n", r_key);\r
        }\r
+       \r
        t1 = clock() - t1;\r
-       if ( t1 > 0 ){\r
+       if ( t1 > 0 )\r
                PrintAndLog("Time in darkside: %.0f ticks - %4.2f sec\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC);\r
-       }\r
        return 0;\r
 }\r
 \r
@@ -575,7 +573,7 @@ int CmdHF14AMfNested(const char *Cmd)
        uint8_t trgKeyType = 0;\r
        uint8_t SectorsCnt = 0;\r
        uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
-       uint8_t keyBlock[14*6];\r
+       uint8_t keyBlock[6*6];\r
        uint64_t key64 = 0;\r
        bool transferToEml = false;\r
        \r
@@ -649,40 +647,35 @@ int CmdHF14AMfNested(const char *Cmd)
        transferToEml |= (ctmp == 'd' || ctmp == 'D');\r
        \r
        if (cmdp == 'o') {\r
-               PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');\r
                int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);\r
-               if (isOK) {\r
-                       switch (isOK) {\r
-                               case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
-                               case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
-                               case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
-                               default : PrintAndLog("Unknown Error.\n");\r
-                       }\r
-                       return 2;\r
-               }\r
-               key64 = bytes_to_num(keyBlock, 6);\r
-               if (key64) {\r
-                       PrintAndLog("Found valid key:%012"llx, key64);\r
-\r
-                       // transfer key to the emulator\r
-                       if (transferToEml) {\r
-                               uint8_t sectortrailer;\r
-                               if (trgBlockNo < 32*4) {        // 4 block sector\r
-                                       sectortrailer = (trgBlockNo & 0x03) + 3;\r
-                               } else {                                        // 16 block sector\r
-                                       sectortrailer = (trgBlockNo & 0x0f) + 15;\r
+               switch (isOK) {\r
+                       case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
+                       case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
+                       case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
+                       case -4 : PrintAndLog("No valid key found"); break;\r
+                       case -5 : \r
+                               key64 = bytes_to_num(keyBlock, 6);\r
+\r
+                               // transfer key to the emulator\r
+                               if (transferToEml) {\r
+                                       uint8_t sectortrailer;\r
+                                       if (trgBlockNo < 32*4) {        // 4 block sector\r
+                                               sectortrailer = (trgBlockNo & 0x03) + 3;\r
+                                       } else {                                        // 16 block sector\r
+                                               sectortrailer = (trgBlockNo & 0x0f) + 15;\r
+                                       }\r
+                                       mfEmlGetMem(keyBlock, sectortrailer, 1);\r
+                       \r
+                                       if (!trgKeyType)\r
+                                               num_to_bytes(key64, 6, keyBlock);\r
+                                       else\r
+                                               num_to_bytes(key64, 6, &keyBlock[10]);\r
+                                       mfEmlSetMem(keyBlock, sectortrailer, 1);                \r
                                }\r
-                               mfEmlGetMem(keyBlock, sectortrailer, 1);\r
-               \r
-                               if (!trgKeyType)\r
-                                       num_to_bytes(key64, 6, keyBlock);\r
-                               else\r
-                                       num_to_bytes(key64, 6, &keyBlock[10]);\r
-                               mfEmlSetMem(keyBlock, sectortrailer, 1);                \r
-                       }\r
-               } else {\r
-                       PrintAndLog("No valid key found");\r
+                               return 0;\r
+                       default : PrintAndLog("Unknown Error.\n");\r
                }\r
+               return 2;\r
        }\r
        else { // ------------------------------------  multiple sectors working\r
                clock_t t1 = clock();\r
@@ -697,14 +690,6 @@ int CmdHF14AMfNested(const char *Cmd)
                num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
                num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r
                num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r
-               num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));\r
-               num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));\r
-               num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));\r
-               num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));\r
-               num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));\r
-               num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));\r
-               num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));\r
-               num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));\r
 \r
                PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
                for (i = 0; i < SectorsCnt; i++) {\r
@@ -719,44 +704,47 @@ int CmdHF14AMfNested(const char *Cmd)
                                }\r
                        }\r
                }\r
+               clock_t t2 = clock() - t1;\r
+               if ( t2 > 0 )\r
+                       PrintAndLog("Time to check 6 known keys: %.0f ticks %4.2f sec", (float)t2, ((float)t2)/CLOCKS_PER_SEC);\r
+       \r
                \r
                // nested sectors\r
                iterations = 0;\r
-               PrintAndLog("nested...");\r
+               PrintAndLog("enter nested...");\r
                bool calibrate = true;\r
                for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r
-                       for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
-                               for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r
+                       for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; ++sectorNo) {\r
+                               for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) { \r
+\r
                                        if (e_sector[sectorNo].foundKey[trgKeyType]) continue;\r
-                                       PrintAndLog("-----------------------------------------------");\r
-                                       int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);\r
-                                       if(isOK) {\r
-                                               switch (isOK) {\r
-                                                       case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
-                                                       case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
-                                                       case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
-                                                       default : PrintAndLog("Unknown Error.\n");\r
-                                               }\r
-                                               free(e_sector);\r
-                                               return 2;\r
-                                       } else {\r
-                                               calibrate = false;\r
-                                       }\r
                                        \r
-                                       iterations++;\r
-\r
-                                       key64 = bytes_to_num(keyBlock, 6);\r
-                                       if (key64) {\r
-                                               PrintAndLog("Found valid key:%012"llx, key64);\r
-                                               e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
-                                               e_sector[sectorNo].Key[trgKeyType] = key64;\r
+                                       int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);\r
+                                       switch (isOK) {\r
+                                               case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
+                                               case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
+                                               case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
+                                               case -4 : //key not found\r
+                                                       calibrate = false;\r
+                                                       iterations++;\r
+                                                       continue; \r
+                                               case -5 :\r
+                                                       calibrate = false;\r
+                                                       iterations++;\r
+                                                       e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
+                                                       e_sector[sectorNo].Key[trgKeyType] = bytes_to_num(keyBlock, 6);\r
+                                                       continue;\r
+                                                       \r
+                                               default : PrintAndLog("Unknown Error.\n");\r
                                        }\r
+                                       free(e_sector);\r
+                                       return 2;\r
                                }\r
                        }\r
                }\r
 \r
                // 20160116 If Sector A is found, but not Sector B,  try just reading it of the tag?\r
-               PrintAndLog("testing to read B...");\r
+               PrintAndLog("trying to read key B...");\r
                for (i = 0; i < SectorsCnt; i++) {\r
                        // KEY A  but not KEY B\r
                        if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {\r
@@ -993,8 +981,7 @@ int CmdHF14AMfChk(const char *Cmd)
        keyBlock = calloc(stKeyBlock, 6);\r
        if (keyBlock == NULL) return 1;\r
 \r
-       uint64_t defaultKeys[] =\r
-       {\r
+       uint64_t defaultKeys[] = {\r
                0xffffffffffff, // Default key (first key used by program if no user defined key)\r
                0x000000000000, // Blank key\r
                0xa0a1a2a3a4a5, // NFCForum MAD key\r
@@ -1012,9 +999,8 @@ int CmdHF14AMfChk(const char *Cmd)
        int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);\r
 \r
        for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
-       {\r
                num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
-       }\r
+\r
        \r
        if (param_getchar(Cmd, 0)=='*') {\r
                blockNo = 3;\r
@@ -1025,9 +1011,9 @@ int CmdHF14AMfChk(const char *Cmd)
                        case '4': SectorsCnt = 40; break;\r
                        default:  SectorsCnt = 16;\r
                }\r
-       }\r
-       else\r
+       } else {\r
                blockNo = param_get8(Cmd, 0);\r
+       }\r
        \r
        ctmp = param_getchar(Cmd, 1);\r
        switch (ctmp) { \r
@@ -1061,7 +1047,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                }\r
                                keyBlock = p;\r
                        }\r
-                       PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
+                       PrintAndLog("check key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
                        (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
                        (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4],     (keyBlock + 6*keycnt)[5], 6);\r
                        keycnt++;\r
@@ -1101,7 +1087,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                        }\r
                                        memset(keyBlock + 6 * keycnt, 0, 6);\r
                                        num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
-                                       PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
+                                       PrintAndLog("check custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
                                        keycnt++;\r
                                        memset(buf, 0, sizeof(buf));\r
                                }\r
@@ -1118,7 +1104,7 @@ int CmdHF14AMfChk(const char *Cmd)
        if (keycnt == 0) {\r
                PrintAndLog("No key specified, trying default keys");\r
                for (;keycnt < defaultKeysSize; keycnt++)\r
-                       PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
+                       PrintAndLog("check default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
                                (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
                                (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4],     (keyBlock + 6*keycnt)[5], 6);\r
        }\r
@@ -1143,8 +1129,6 @@ int CmdHF14AMfChk(const char *Cmd)
                        // skip already found keys.\r
                        if (e_sector[i].foundKey[trgKeyType]) continue;\r
                        \r
-                       PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, trgKeyType ? 'B':'A', keycnt);\r
-                       \r
                        uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;\r
                        \r
                        for (uint32_t c = 0; c < keycnt; c += max_keys) {\r
@@ -1153,11 +1137,12 @@ int CmdHF14AMfChk(const char *Cmd)
                                \r
                                res = mfCheckKeys(b, trgKeyType, true, size, &keyBlock[6*c], &key64);\r
                                if (!res) {\r
-                                       PrintAndLog("Found valid key:[%012"llx"]",key64);                                       \r
+                                       PrintAndLog("Sector:%3d Block:%3d, key type: %C  -- Found key [%012"llx"]", i, b, trgKeyType ? 'B':'A', key64);\r
+                                                                                \r
                                        e_sector[i].Key[trgKeyType] = key64;\r
                                        e_sector[i].foundKey[trgKeyType] = TRUE;\r
                                        break;\r
-                               } else {\r
+                               } else {                                        \r
                                        e_sector[i].Key[trgKeyType] = 0xffffffffffff;\r
                                        e_sector[i].foundKey[trgKeyType] = FALSE;\r
                                }\r
index 431db9dcd35c44f1a41e5fae84402a55d80be1bd..0987e1f2fa878bad6c41c3bc9f4f83b7ca99a829 100644 (file)
@@ -14,6 +14,7 @@
 #include <pthread.h>\r
 #include "mifarehost.h"\r
 #include "proxmark3.h"\r
+#include "radixsort.h"\r
 \r
 // MIFARE\r
 int compar_int(const void * a, const void * b) {\r
@@ -21,16 +22,31 @@ int compar_int(const void * a, const void * b) {
        //return (*(uint64_t*)b - *(uint64_t*)a);\r
 \r
        // better:\r
-       if (*(uint64_t*)b == *(uint64_t*)a) return 0;\r
-       else if (*(uint64_t*)b > *(uint64_t*)a) return 1;\r
-       else return -1;\r
+       // if (*(uint64_t*)b > *(uint64_t*)a) return 1;\r
+       // if (*(uint64_t*)b < *(uint64_t*)a) return -1;\r
+       // return 0;\r
+\r
+       return (*(uint64_t*)b > *(uint64_t*)a) - (*(uint64_t*)b < *(uint64_t*)a);\r
+       //return (*(int64_t*)b > *(int64_t*)a) - (*(int64_t*)b < *(int64_t*)a);\r
 }\r
 \r
 // Compare 16 Bits out of cryptostate\r
 int Compare16Bits(const void * a, const void * b) {\r
-       if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r
-       else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r
-       else return -1;\r
+\r
+       // if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;   \r
+       // if ((*(uint64_t*)b & 0x00ff000000ff0000) < (*(uint64_t*)a & 0x00ff000000ff0000)) return -1;  \r
+       // return 0;\r
+\r
+       return \r
+               ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000))\r
+               -\r
+               ((*(uint64_t*)b & 0x00ff000000ff0000) < (*(uint64_t*)a & 0x00ff000000ff0000))\r
+               ;\r
+       // return \r
+               // ((*(int64_t*)b & 0x00ff000000ff0000) > (*(int64_t*)a & 0x00ff000000ff0000))\r
+               // -\r
+               // ((*(int64_t*)b & 0x00ff000000ff0000) < (*(int64_t*)a & 0x00ff000000ff0000))\r
+               // ;\r
 }\r
 \r
 typedef \r
@@ -59,7 +75,9 @@ void* nested_worker_thread(void *arg)
        StateList_t *statelist = arg;\r
 \r
        statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r
-       for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r
+       \r
+       for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; ++p1);\r
+       \r
        statelist->len = p1 - statelist->head.slhead;\r
        statelist->tail.sltail = --p1;\r
        qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r
@@ -72,26 +90,21 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
        uint16_t i;\r
        uint32_t uid;\r
        UsbCommand resp;\r
-\r
        StateList_t statelists[2];\r
        struct Crypto1State *p1, *p2, *p3, *p4;\r
        \r
-       // flush queue\r
-       \r
        UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r
        memcpy(c.d.asBytes, key, 6);\r
        clearCommandBuffer();\r
        SendCommand(&c);\r
-\r
        if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;\r
 \r
        // error during nested\r
        if (resp.arg[0]) return resp.arg[0];\r
        \r
        memcpy(&uid, resp.d.asBytes, 4);\r
-       PrintAndLog("UID: %08x Block:%d Key: %c", uid, (uint16_t)resp.arg[2] & 0xff, (resp.arg[2] >> 8) ?'A':'B' );\r
                        \r
-       for (i = 0; i < 2; i++) {\r
+       for (i = 0; i < 2; ++i) {\r
                statelists[i].blockNo = resp.arg[2] & 0xff;\r
                statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r
                statelists[i].uid = uid;\r
@@ -99,19 +112,16 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
                memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r
        }\r
        \r
-       // calc keys\r
-       \r
+       // calc keys    \r
        pthread_t thread_id[2];\r
                \r
        // create and run worker threads\r
-       for (i = 0; i < 2; i++) {\r
+       for (i = 0; i < 2; i++)\r
                pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r
-       }\r
        \r
        // wait for threads to terminate:\r
-       for (i = 0; i < 2; i++) {\r
+       for (i = 0; i < 2; i++)\r
                pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r
-       }\r
 \r
 \r
        // the first 16 Bits of the cryptostate already contain part of our key.\r
@@ -142,6 +152,7 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
                        while (Compare16Bits(p1, p2) == 1) p2++;\r
                }\r
        }\r
+       \r
        p3->even = 0; p3->odd = 0;\r
        p4->even = 0; p4->odd = 0;\r
        statelists[0].len = p3 - statelists[0].head.slhead;\r
@@ -154,6 +165,12 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
        qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compar_int);\r
        qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compar_int);\r
 \r
+       //      clock_t t1 = clock();\r
+       //radixSort(statelists[0].head.keyhead, statelists[0].len);\r
+       //radixSort(statelists[1].head.keyhead, statelists[1].len);\r
+       // t1 = clock() - t1;   \r
+       // PrintAndLog("radixsort, ticks %.0f", (float)t1);\r
+\r
        uint64_t *p5, *p6, *p7;\r
        p5 = p7 = statelists[0].head.keyhead; \r
        p6 = statelists[1].head.keyhead;\r
@@ -168,38 +185,39 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
                }\r
        }\r
        statelists[0].len = p7 - statelists[0].head.keyhead;\r
-       statelists[0].tail.keytail=--p7;\r
+       statelists[0].tail.keytail = --p7;\r
 \r
        memset(resultKey, 0, 6);\r
+       uint64_t key64 = 0;\r
+\r
        // The list may still contain several key candidates. Test each of them with mfCheckKeys\r
        for (i = 0; i < statelists[0].len; i++) {\r
-               uint8_t keyBlock[6];\r
-               uint64_t key64;\r
+\r
                crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r
-               num_to_bytes(key64, 6, keyBlock);\r
-               key64 = 0;\r
-               if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r
-                       num_to_bytes(key64, 6, resultKey);\r
-                       break;\r
+               num_to_bytes(key64, 6, resultKey);\r
+\r
+               if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, resultKey, &key64)) {\r
+                       free(statelists[0].head.slhead);\r
+                       free(statelists[1].head.slhead);\r
+                       PrintAndLog("UID: %08x target block:%3u key type: %c  -- Found key [%012"llx"]", uid, (uint16_t)resp.arg[2] & 0xff, (resp.arg[2] >> 8)?'B':'A', key64);\r
+                       return -5;\r
                }\r
        }\r
-       \r
+       PrintAndLog("UID: %08x target block:%3u key type: %c", uid, (uint16_t)resp.arg[2] & 0xff, (resp.arg[2] >> 8)?'B':'A');  \r
        free(statelists[0].head.slhead);\r
-       free(statelists[1].head.slhead);        \r
-       return 0;\r
+       free(statelists[1].head.slhead);\r
+       return -4;\r
 }\r
 \r
 int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r
 \r
        *key = 0;\r
-\r
-       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r
+       UsbCommand c = {CMD_MIFARE_CHKKEYS, { (blockNo | (keyType<<8)), clear_trace, keycnt}};\r
        memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
-       \r
        clearCommandBuffer();\r
        SendCommand(&c);\r
        UsbCommand resp;\r
-       if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r
+       if (!WaitForResponseTimeout(CMD_ACK,&resp, 3000)) return 1;\r
        if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
        *key = bytes_to_num(resp.d.asBytes, 6);\r
        return 0;\r
@@ -237,14 +255,12 @@ int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, uint8_
        uint8_t params = MAGIC_SINGLE;\r
        uint8_t block0[16];\r
        memset(block0, 0x00, sizeof(block0));\r
-       \r
 \r
        int old = mfCGetBlock(0, block0, params);\r
-       if (old == 0) {\r
+       if (old == 0)\r
                PrintAndLog("old block 0:  %s", sprint_hex(block0, sizeof(block0)));\r
-       } else {\r
-               PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r
-       }\r
+       else \r
+               PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");       \r
 \r
        // fill in the new values\r
        // UID\r
@@ -344,7 +360,7 @@ int isBlockEmpty(int blockN) {
 }\r
 \r
 int isBlockTrailer(int blockN) {\r
- return ((blockN & 0x03) == 0x03);\r
      return ((blockN & 0x03) == 0x03);\r
 }\r
 \r
 int loadTraceCard(uint8_t *tuid) {\r
@@ -439,20 +455,22 @@ void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool i
                for (i = 0; i < len; i++)\r
                        data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r
        } else {\r
-               bt = 0;\r
-               for (i = 0; i < 4; i++)\r
-                       bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r
-                               \r
+               bt = 0;         \r
+               bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 0)) << 0;\r
+               bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 1)) << 1;\r
+               bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 2)) << 2;\r
+               bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 3)) << 3;                        \r
                data[0] = bt;\r
        }\r
        return;\r
 }\r
 \r
-\r
 int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
+       \r
        uint8_t data[64];\r
 \r
        if (traceState == TRACE_ERROR) return 1;\r
+       \r
        if (len > 64) {\r
                traceState = TRACE_ERROR;\r
                return 1;\r
@@ -637,7 +655,6 @@ int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data,
        uint32_t ar_enc;  // encrypted reader response\r
        uint32_t at_enc;  // encrypted tag response\r
        */\r
-\r
        struct Crypto1State *pcs = NULL;\r
        \r
        ks2 = ar_enc ^ prng_successor(nt, 64);\r
Impressum, Datenschutz