+ 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
+ // uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;\r
+ uint8_t keyBlock[USB_CMD_DATA_SIZE] = {0x00};\r
+\r
+ for (i = 0; i < numOfCandidates; ++i){\r
+ crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r
+ num_to_bytes(key64, 6, keyBlock + i * 6);\r
+ }\r
+\r
+ if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, numOfCandidates, keyBlock, &key64)) { \r
+ free(statelists[0].head.slhead);\r
+ free(statelists[1].head.slhead);\r
+ num_to_bytes(key64, 6, resultKey);\r
+\r
+ PrintAndLog("UID: %08x target block:%3u key type: %c -- Found key [%012"llx"]",\r
+ uid,\r
+ (uint16_t)resp.arg[2] & 0xff,\r
+ (resp.arg[2] >> 8) ? 'B' : 'A',\r
+ key64\r
+ );\r
+ return -5;\r
+ }\r
+ \r
+out:\r
+ PrintAndLog("UID: %08x target block:%3u key type: %c",\r
+ uid,\r
+ (uint16_t)resp.arg[2] & 0xff,\r
+ (resp.arg[2] >> 8) ? 'B' : 'A'\r
+ ); \r
+\r
+ free(statelists[0].head.slhead);\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
+ UsbCommand c = {CMD_MIFARE_CHKKEYS, { (blockNo | (keyType<<8)), clear_trace, keycnt}};\r
+ memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
+ clearCommandBuffer();\r
+ SendCommand(&c);\r
+ UsbCommand resp;\r
+ if (!WaitForResponseTimeout(CMD_ACK,&resp, 2500)) return 1;\r
+ if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
+ *key = bytes_to_num(resp.d.asBytes, 6);\r
+ return 0;\r
+}\r
+\r
+// EMULATOR\r
+\r
+int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
+ UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r
+ clearCommandBuffer();\r
+ SendCommand(&c);\r
+ UsbCommand resp;\r
+ if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r
+ memcpy(data, resp.d.asBytes, blocksCount * 16);\r
+ return 0;\r
+}\r
+\r
+int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
+ return mfEmlSetMem_xt(data, blockNum, blocksCount, 16);\r
+}\r
+\r
+int mfEmlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {\r
+ UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, blockBtWidth}};\r
+ memcpy(c.d.asBytes, data, blocksCount * blockBtWidth); \r
+\r
+ clearCommandBuffer();\r
+ SendCommand(&c);\r
+ return 0;\r
+}\r
+\r
+// "MAGIC" CARD\r
+\r
+int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, uint8_t wipecard) {\r
+\r
+ uint8_t params = MAGIC_SINGLE;\r
+ uint8_t block0[16];\r
+ memset(block0, 0x00, sizeof(block0));\r
+\r
+ int old = mfCGetBlock(0, block0, params);\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
+ // fill in the new values\r
+ // UID\r
+ memcpy(block0, uid, 4); \r
+ // Mifare UID BCC\r
+ block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\r
+ // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r
+ if ( sak != NULL )\r
+ block0[5]=sak[0];\r
+ \r
+ if ( atqa != NULL ) {\r
+ block0[6]=atqa[1];\r
+ block0[7]=atqa[0];\r
+ }\r
+ PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r
+ \r
+ if ( wipecard ) params |= MAGIC_WIPE; \r
+ if ( oldUID == NULL) params |= MAGIC_UID;\r
+ \r
+ return mfCSetBlock(0, block0, oldUID, params);\r
+}\r
+\r
+int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, uint8_t params) {\r
+\r
+ uint8_t isOK = 0;\r
+ UsbCommand c = {CMD_MIFARE_CSETBLOCK, {params, blockNo, 0}};\r
+ memcpy(c.d.asBytes, data, 16); \r
+ clearCommandBuffer();\r
+ SendCommand(&c);\r
+ UsbCommand resp;\r
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
+ isOK = resp.arg[0] & 0xff;\r
+ if (uid != NULL) \r
+ memcpy(uid, resp.d.asBytes, 4);\r
+ if (!isOK) \r
+ return 2;\r
+ } else {\r
+ PrintAndLog("Command execute timeout");\r