return 0;\r
}\r
\r
-int mfCIdentify()\r
-{\r
- UsbCommand c;\r
-// UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};\r
-// SendCommand(&c);\r
-\r
- UsbCommand resp;\r
-// WaitForResponse(CMD_ACK,&resp);\r
-\r
- // iso14a_card_select_t card;\r
- // memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));\r
-\r
- // uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision\r
-\r
- // if(select_status != 0) {\r
- // uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0\r
- // c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;\r
- // c.arg[1] = 2;\r
- // c.arg[2] = 0;\r
- // memcpy(c.d.asBytes, rats, 2);\r
- // SendCommand(&c);\r
- // WaitForResponse(CMD_ACK,&resp);\r
- // }\r
-\r
- c.cmd = CMD_MIFARE_CIDENT;\r
- c.arg[0] = 0;\r
- c.arg[1] = 0;\r
- c.arg[2] = 0;\r
+int mfCIdentify() {\r
+ UsbCommand c = {CMD_MIFARE_CIDENT, {0, 0, 0}};\r
SendCommand(&c);\r
+ UsbCommand resp;\r
WaitForResponse(CMD_ACK,&resp);\r
\r
uint8_t isGeneration = resp.arg[0] & 0xff;\r
default: PrintAndLog("No chinese magic backdoor command detected"); break;\r
}\r
\r
- // disconnect\r
-// c.cmd = CMD_READER_ISO_14443a;\r
-// c.arg[0] = 0;\r
-// c.arg[1] = 0;\r
-// c.arg[2] = 0;\r
-// SendCommand(&c);\r
-\r
return (int) isGeneration;\r
}\r
\r
for (int i = 0; i < 64; i++) { // blocks\r
for (int j = 0; j < 16; j++) // bytes\r
fprintf(f, "%02x", *(traceCard + i * 16 + j));\r
- fprintf(f,"\n");\r
+ if (i < 63)\r
+ fprintf(f,"\n");\r
}\r
fclose(f);\r
return 0;\r
if (len ==4) {\r
traceState = TRACE_IDLE;\r
\r
- at_enc = bytes_to_num(data, 4);\r
-\r
- // decode key here)\r
- ks2 = ar_enc ^ prng_successor(nt, 64);\r
- ks3 = at_enc ^ prng_successor(nt, 96);\r
- revstate = lfsr_recovery64(ks2, ks3);\r
- lfsr_rollback_word(revstate, 0, 0);\r
- lfsr_rollback_word(revstate, 0, 0);\r
- lfsr_rollback_word(revstate, nr_enc, 1);\r
- lfsr_rollback_word(revstate, uid ^ nt, 0);\r
+ if (!traceCrypto1) {\r
+ at_enc = bytes_to_num(data, 4);\r
+\r
+ // decode key here)\r
+ ks2 = ar_enc ^ prng_successor(nt, 64);\r
+ ks3 = at_enc ^ prng_successor(nt, 96);\r
+ revstate = lfsr_recovery64(ks2, ks3);\r
+ lfsr_rollback_word(revstate, 0, 0);\r
+ lfsr_rollback_word(revstate, 0, 0);\r
+ lfsr_rollback_word(revstate, nr_enc, 1);\r
+ lfsr_rollback_word(revstate, uid ^ nt, 0);\r
+\r
+ crypto1_get_lfsr(revstate, &lfsr);\r
+ printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
+ AddLogUint64(logHexFileName, "key> ", lfsr);\r
+ } else {\r
+ printf("key> nested not implemented!\n");\r
+ at_enc = bytes_to_num(data, 4);\r
+ \r
+ crypto1_destroy(traceCrypto1);\r
\r
- crypto1_get_lfsr(revstate, &lfsr);\r
- printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
- AddLogUint64(logHexFileName, "key> ", lfsr);\r
+ // not implemented\r
+ traceState = TRACE_ERROR;\r
+ }\r
\r
int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
\r
// set cryptosystem state\r
traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
-\r
-// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
-\r
- /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r
- crypto1_word(traceCrypto1, nt ^ uid, 0);\r
- crypto1_word(traceCrypto1, ar, 1);\r
- crypto1_word(traceCrypto1, 0, 0);\r
- crypto1_word(traceCrypto1, 0, 0);*/\r
-\r
return 0;\r
} else {\r
traceState = TRACE_ERROR;\r
return 0;\r
}\r
\r
+/** validate_prng_nonce\r
+ * Determine if nonce is deterministic. ie: Suspectable to Darkside attack.\r
+ * returns\r
+ * true = weak prng\r
+ * false = hardend prng\r
+ */\r
+bool validate_prng_nonce(uint32_t nonce) {\r
+ uint16_t *dist = 0;\r
+ uint16_t x, i;\r
+\r
+ dist = malloc(2 << 16);\r
+ if(!dist)\r
+ return -1;\r
+\r
+ // init prng table:\r
+ for (x = i = 1; i; ++i) {\r
+ dist[(x & 0xff) << 8 | x >> 8] = i;\r
+ x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15;\r
+ }\r
+ \r
+ uint32_t res = (65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535;\r
+ \r
+ free(dist); \r
+ return (res == 16);\r
+}\r
+\r
+/* Detect Tag Prng, \r
+* function performs a partial AUTH, where it tries to authenticate against block0, key A, but only collects tag nonce.\r
+* the tag nonce is check to see if it has a predictable PRNG.\r
+* @returns \r
+* TRUE if tag uses WEAK prng (ie Now the NACK bug also needs to be present for Darkside attack)\r
+* FALSE is tag uses HARDEND prng (ie hardnested attack possible, with known key)\r
+*/\r
+int DetectClassicPrng(void){\r
+\r
+ UsbCommand resp, respA; \r
+ uint8_t cmd[] = {0x60, 0x00}; // MIFARE_AUTH_KEYA\r
+ uint32_t flags = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_RATS;\r
+ \r
+ UsbCommand c = {CMD_READER_ISO_14443a, {flags, sizeof(cmd), 0}};\r
+ memcpy(c.d.asBytes, cmd, sizeof(cmd));\r
+\r
+ clearCommandBuffer();\r
+ SendCommand(&c);\r
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {\r
+ PrintAndLog("PRNG UID: Reply timeout.");\r
+ return -1;\r
+ }\r
+ \r
+ // if select tag failed.\r
+ if (resp.arg[0] == 0) {\r
+ PrintAndLog("PRNG error: selecting tag failed, can't detect prng.");\r
+ return -1;\r
+ }\r
+ \r
+ if (!WaitForResponseTimeout(CMD_ACK, &respA, 5000)) {\r
+ PrintAndLog("PRNG data: Reply timeout.");\r
+ return -1;\r
+ }\r
+\r
+ // check respA\r
+ if (respA.arg[0] != 4) {\r
+ PrintAndLog("PRNG data error: Wrong length: %d", respA.arg[0]);\r
+ return -1;\r
+ }\r
+\r
+ uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]);\r
+ return validate_prng_nonce(nonce);\r
+}\r