]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/mifareutil.c
added @pwpiwi 's latest changes to "hf mf hardnested"
[proxmark3-svn] / armsrc / mifareutil.c
index 1b71f05de55cc911c79ab3f1fe52d34a3d5d02de..f155a92d7fc0575237af8f722f2befca97010396 100644 (file)
@@ -18,6 +18,7 @@
 #include "iso14443a.h"\r
 #include "crapto1.h"\r
 #include "mifareutil.h"\r
+#include "parity.h"\r
 #include "des.h"\r
 \r
 int MF_DBGLEVEL = MF_DBG_ALL;\r
@@ -50,7 +51,7 @@ void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, u
                data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r
                if((i&0x0007) == 0) \r
                        par[i>>3] = 0;\r
-               par[i>>3] |= (((filter(pcs->odd) ^ oddparity(bt)) & 0x01)<<(7-(i&0x0007)));\r
+               par[i>>3] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));\r
        }       \r
        return;\r
 }\r
@@ -65,12 +66,6 @@ uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
        return bt;\r
 }\r
 \r
-// send 2 byte commands\r
-int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r
-{\r
-       return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, answer_parity, timing);\r
-}\r
-\r
 // send X byte basic commands\r
 int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r
 {\r
@@ -88,52 +83,8 @@ int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answe
        return len;\r
 }\r
 \r
-/*\r
-int mifare_sendcmd_short_mfucauth(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t *data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)\r
-{\r
-    uint8_t dcmd[19];\r
-       int len; \r
-    dcmd[0] = cmd;\r
-    memcpy(dcmd+1,data,16);\r
-       AppendCrc14443a(dcmd, 17);\r
-       \r
-       ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
-       len = ReaderReceive(answer, answer_parity);\r
-       if(!len) {\r
-        if (MF_DBGLEVEL >= MF_DBG_ERROR)   Dbprintf("Authentication failed. Card timeout.");\r
-        len = ReaderReceive(answer,answer_parity);\r
-    }\r
-    if(len==1) {\r
-               if (MF_DBGLEVEL >= MF_DBG_ERROR)   Dbprintf("NAK - Authentication failed.");\r
-               return 1;\r
-        }\r
-       return len;\r
-}\r
-\r
-int mifare_sendcmd_short_mfuev1auth(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t *data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)\r
-{\r
-    uint8_t dcmd[7];\r
-       int len; \r
-    dcmd[0] = cmd;\r
-    memcpy(dcmd+1,data,4);\r
-       AppendCrc14443a(dcmd, 5);\r
-       \r
-       ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
-       len = ReaderReceive(answer, answer_parity);\r
-       if(!len) {\r
-        if (MF_DBGLEVEL >= MF_DBG_ERROR)   Dbprintf("Authentication failed. Card timeout.");\r
-        len = ReaderReceive(answer,answer_parity);\r
-    }\r
-    if(len==1) {\r
-               if (MF_DBGLEVEL >= MF_DBG_ERROR)   Dbprintf("NAK - Authentication failed.");\r
-               return 1;\r
-        }\r
-       return len;\r
-}\r
-*/\r
-\r
 // send 2 byte commands\r
-int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)\r
+int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)\r
 {\r
        uint8_t dcmd[4], ecmd[4];\r
        uint16_t pos, res;\r
@@ -149,7 +100,7 @@ int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cm
                for (pos = 0; pos < 4; pos++)\r
                {\r
                        ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r
-                       par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));\r
+                       par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));\r
                }       \r
 \r
                ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r
@@ -243,7 +194,7 @@ int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockN
        for (pos = 0; pos < 4; pos++)\r
        {\r
                mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r
-               par[0] |= (((filter(pcs->odd) ^ oddparity(nr[pos])) & 0x01) << (7-pos));\r
+               par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));\r
        }       \r
                \r
        // Skip 32 bits in pseudo random generator\r
@@ -254,7 +205,7 @@ int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockN
        {\r
                nt = prng_successor(nt,8);\r
                mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r
-               par[0] |= (((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) << (7-pos));\r
+               par[0] |= (((filter(pcs->odd) ^ oddparity8(nt & 0xff)) & 0x01) << (7-pos));\r
        }       \r
                \r
        // Transmit reader nonce and reader answer\r
@@ -321,9 +272,8 @@ int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){
 \r
        if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r
                Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);\r
-       \r
        len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);\r
-       //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);\r
+\r
        if (len != 4) {\r
                if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r
                return 0;\r
@@ -478,7 +428,7 @@ int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t bl
        for (pos = 0; pos < 18; pos++)\r
        {\r
                d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
-               par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
+               par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
        }       \r
 \r
        ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
@@ -552,22 +502,21 @@ int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)
     }\r
     return 0;\r
 }\r
-\r
-int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r
-{\r
+int mifare_classic_halt_ex(struct Crypto1State *pcs) {\r
        uint16_t len;   \r
-       uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
-       uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
+       uint8_t receivedAnswer[4];\r
+       uint8_t receivedAnswerPar[4];\r
 \r
        len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
        if (len != 0) {\r
-               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
-                       Dbprintf("halt error. response len: %x", len);  \r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("halt error. response len: %x", len);  \r
                return 1;\r
        }\r
-\r
        return 0;\r
 }\r
+int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) {\r
+       return mifare_classic_halt_ex(pcs);\r
+}\r
 \r
 int mifare_ultra_halt()\r
 {\r
@@ -607,8 +556,12 @@ uint8_t FirstBlockOfSector(uint8_t sectorNo)
 \r
 // work with emulator memory\r
 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
+       emlSetMem_xt(data, blockNum, blocksCount, 16);\r
+}\r
+\r
+void emlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {\r
        uint8_t* emCARD = BigBuf_get_EM_addr();\r
-       memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r
+       memcpy(emCARD + blockNum * blockBtWidth, data, blocksCount * blockBtWidth);\r
 }\r
 \r
 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
Impressum, Datenschutz