]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/mifarehost.c
Merge pull request #600 from marshmellow42/master
[proxmark3-svn] / client / mifarehost.c
index 398e4f15ed1586daeede8b9d18584942cce22bf3..e1ced176580166a7e7015418e8c8463e6fe7c549 100644 (file)
@@ -20,6 +20,7 @@
 #include "usb_cmd.h"\r
 #include "cmdmain.h"\r
 #include "ui.h"\r
+#include "parity.h"\r
 #include "util.h"\r
 #include "iso14443crc.h"\r
 \r
@@ -73,13 +74,12 @@ static uint32_t intersection(uint64_t *list1, uint64_t *list2)
 \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
+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, rr; //nr_diff;\r
+       uint32_t i, pos;\r
        uint8_t bt, ks3x[8], par[8][8];\r
        uint64_t key_recovered;\r
-       static uint64_t *keylist;\r
-       rr = 0;\r
+       uint64_t *keylist;\r
 \r
        // Reset the last three significant bits of the reader nonce\r
        nr &= 0xffffff1f;\r
@@ -92,7 +92,7 @@ static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_i
                }\r
        }\r
 \r
-       states = lfsr_common_prefix(nr, rr, ks3x, par, (par_info == 0));\r
+       states = lfsr_common_prefix(nr, ar, ks3x, par, (par_info == 0));\r
 \r
        if (states == NULL) {\r
                *keys = NULL;\r
@@ -116,7 +116,7 @@ static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_i
 int mfDarkside(uint64_t *key)\r
 {\r
        uint32_t uid = 0;\r
-       uint32_t nt = 0, nr = 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
@@ -159,18 +159,18 @@ int mfDarkside(uint64_t *key)
                                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
+                               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
-                       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
+               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
@@ -178,12 +178,14 @@ int mfDarkside(uint64_t *key)
                        continue;\r
                }\r
 \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
+               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
@@ -198,10 +200,10 @@ int mfDarkside(uint64_t *key)
                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 (last_keylist == NULL) {\r
-                                       num_to_bytes(keylist[i*max_keys + j], 6, keyBlock);\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(last_keylist[i*max_keys + j], 6, keyBlock);\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
@@ -295,7 +297,13 @@ typedef
 \r
 \r
 // wrapper function for multi-threaded lfsr_recovery32\r
-void* nested_worker_thread(void *arg)\r
+void\r
+#ifdef __has_attribute\r
+#if __has_attribute(force_align_arg_pointer)\r
+__attribute__((force_align_arg_pointer)) \r
+#endif\r
+#endif\r
+*nested_worker_thread(void *arg)\r
 {\r
        struct Crypto1State *p1;\r
        StateList_t *statelist = arg;\r
@@ -309,6 +317,7 @@ void* nested_worker_thread(void *arg)
        return statelist->head.slhead;\r
 }\r
 \r
+\r
 int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate)\r
 {\r
        uint16_t i;\r
@@ -574,14 +583,19 @@ struct Crypto1State *traceCrypto1 = NULL;
 \r
 struct Crypto1State *revstate;\r
 uint64_t lfsr;\r
+uint64_t ui64Key;\r
 uint32_t ks2;\r
 uint32_t ks3;\r
 \r
-uint32_t uid;     // serial number\r
-uint32_t nt;      // tag challenge\r
-uint32_t nr_enc;  // encrypted reader challenge\r
-uint32_t ar_enc;  // encrypted reader response\r
-uint32_t at_enc;  // encrypted tag response\r
+uint32_t uid;       // serial number\r
+uint32_t nt;        // tag challenge\r
+uint32_t nt_enc;    // encrypted tag challenge\r
+uint8_t nt_enc_par; // encrypted tag challenge parity\r
+uint32_t nr_enc;    // encrypted reader challenge\r
+uint32_t ar_enc;    // encrypted reader response\r
+uint8_t ar_enc_par; // encrypted reader response parity\r
+uint32_t at_enc;    // encrypted tag response\r
+uint8_t at_enc_par; // encrypted tag response parity\r
 \r
 int isTraceCardEmpty(void) {\r
        return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r
@@ -700,8 +714,36 @@ void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool i
        return;\r
 }\r
 \r
+bool NTParityCheck(uint32_t ntx) {\r
+       if (\r
+               (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((nt_enc_par >> 5) & 0x01) ^ (nt_enc & 0x01)) ||\r
+               (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((nt_enc_par >> 6) & 0x01) ^ (nt_enc >> 8 & 0x01)) ||\r
+               (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((nt_enc_par >> 7) & 0x01) ^ (nt_enc >> 16 & 0x01))\r
+               )\r
+               return false;\r
+       \r
+       uint32_t ar = prng_successor(ntx, 64);\r
+       if (\r
+               (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ar_enc_par >> 5) & 0x01) ^ (ar_enc & 0x01)) ||\r
+               (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ar_enc_par >> 6) & 0x01) ^ (ar_enc >> 8 & 0x01)) ||\r
+               (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ar_enc_par >> 7) & 0x01) ^ (ar_enc >> 16 & 0x01))\r
+               )\r
+               return false;\r
+\r
+       uint32_t at = prng_successor(ntx, 96);\r
+       if (\r
+               (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ar_enc_par >> 4) & 0x01) ^ (at_enc >> 24 & 0x01)) ||\r
+               (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((at_enc_par >> 5) & 0x01) ^ (at_enc & 0x01)) ||\r
+               (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((at_enc_par >> 6) & 0x01) ^ (at_enc >> 8 & 0x01)) ||\r
+               (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((at_enc_par >> 7) & 0x01) ^ (at_enc >> 16 & 0x01))\r
+               )\r
+               return false;\r
+               \r
+       return true;\r
+}\r
 \r
-int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
+\r
+int mfTraceDecode(uint8_t *data_src, int len, uint8_t parity, bool wantSaveToEmlFile) {\r
        uint8_t data[64];\r
 \r
        if (traceState == TRACE_ERROR) return 1;\r
@@ -713,7 +755,9 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
        memcpy(data, data_src, len);\r
        if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r
                mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
-               PrintAndLog("dec> %s", sprint_hex(data, len));\r
+               uint8_t parity[16];\r
+               oddparitybuf(data, len, parity);\r
+               PrintAndLog("dec> %s [%s]", sprint_hex(data, len), printBitsPar(parity, len));\r
                AddLogHex(logHexFileName, "dec> ", data, len);\r
        }\r
 \r
@@ -802,7 +846,12 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
        case TRACE_AUTH1:\r
                if (len == 4) {\r
                        traceState = TRACE_AUTH2;\r
-                       nt = bytes_to_num(data, 4);\r
+                       if (!traceCrypto1) {\r
+                               nt = bytes_to_num(data, 4);\r
+                       } else {\r
+                               nt_enc = bytes_to_num(data, 4);\r
+                               nt_enc_par = parity;\r
+                       }\r
                        return 0;\r
                } else {\r
                        traceState = TRACE_ERROR;\r
@@ -816,6 +865,7 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
 \r
                        nr_enc = bytes_to_num(data, 4);\r
                        ar_enc = bytes_to_num(data + 4, 4);\r
+                       ar_enc_par = parity << 4;\r
                        return 0;\r
                } else {\r
                        traceState = TRACE_ERROR;\r
@@ -828,19 +878,89 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
                        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
-\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
+                       at_enc_par = parity;\r
+                       if (!traceCrypto1) {\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
+                               crypto1_destroy(revstate);\r
+                               ui64Key = lfsr;\r
+                               printf("key> probable key:%x%x Prng:%s ks2:%08x ks3:%08x\n", \r
+                                       (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF), \r
+                                       validate_prng_nonce(nt) ? "WEAK": "HARDEND",\r
+                                       ks2,\r
+                                       ks3);\r
+                               AddLogUint64(logHexFileName, "key> ", lfsr);\r
+                       } else {\r
+                               if (validate_prng_nonce(nt)) {\r
+                                       struct Crypto1State *pcs;\r
+                                       pcs = crypto1_create(ui64Key);\r
+                                       uint32_t nt1 = crypto1_word(pcs, nt_enc ^ uid, 1) ^ nt_enc;\r
+                                       uint32_t ar = prng_successor(nt1, 64);\r
+                                       uint32_t at = prng_successor(nt1, 96);\r
+                                       printf("key> nested auth uid: %08x nt: %08x nt_parity: %s ar: %08x at: %08x\n", uid, nt1, printBitsPar(&nt_enc_par, 4), ar, at);\r
+                                       uint32_t nr1 = crypto1_word(pcs, nr_enc, 1) ^ nr_enc;\r
+                                       uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ar_enc;\r
+                                       uint32_t at1 = crypto1_word(pcs, 0, 0) ^ at_enc;\r
+                                       printf("key> the same key test. nr1: %08x ar1: %08x at1: %08x \n", nr1, ar1, at1);\r
+\r
+                                       if (NTParityCheck(nt1))\r
+                                               printf("key> the same key test OK. key=%x%x\n", (unsigned int)((ui64Key & 0xFFFFFFFF00000000) >> 32), (unsigned int)(ui64Key & 0xFFFFFFFF));\r
+                                       else\r
+                                               printf("key> the same key test. check nt parity error.\n");\r
+                                       \r
+                                       uint32_t ntc = prng_successor(nt, 90);\r
+                                       uint32_t ntx = 0;\r
+                                       int ntcnt = 0;\r
+                                       for (int i = 0; i < 16383; i++) {\r
+                                               ntc = prng_successor(ntc, 1);\r
+                                               if (NTParityCheck(ntc)){\r
+                                                       if (!ntcnt)\r
+                                                               ntx = ntc;\r
+                                                       ntcnt++;\r
+                                               }                                               \r
+                                       }\r
+                                       if (ntcnt)\r
+                                               printf("key> nt candidate=%08x nonce distance=%d candidates count=%d\n", ntx, nonce_distance(nt, ntx), ntcnt);\r
+                                       else\r
+                                               printf("key> don't have any nt candidate( \n");\r
+\r
+                                       nt = ntx;\r
+                                       ks2 = ar_enc ^ prng_successor(ntx, 64);\r
+                                       ks3 = at_enc ^ prng_successor(ntx, 96);\r
+\r
+                                       // decode key\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
+                                       crypto1_destroy(revstate);\r
+                                       ui64Key = lfsr;\r
+                                       printf("key> probable key:%x%x  ks2:%08x ks3:%08x\n", \r
+                                               (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF),\r
+                                               ks2,\r
+                                               ks3);\r
+                                       AddLogUint64(logHexFileName, "key> ", lfsr);\r
+                               } else {                                \r
+                                       printf("key> hardnested not implemented!\n");\r
+                               \r
+                                       crypto1_destroy(traceCrypto1);\r
+\r
+                                       // not implemented\r
+                                       traceState = TRACE_ERROR;\r
+                               }\r
+                       }\r
 \r
                        int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
                        if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
@@ -858,15 +978,6 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
 \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
Impressum, Datenschutz