]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/iclass.c
fix 'hf iclass writebl' and 'hf iclass clone' (#896)
[proxmark3-svn] / armsrc / iclass.c
index 9f5bc7ce0c5f9f2fa445789e724565b72d69f2d9..28cfcaa66c407bf5a54d3c47afe817be5ad1cd71 100644 (file)
@@ -646,8 +646,8 @@ static void ReaderTransmitIClass(uint8_t *frame, int len, uint32_t *start_time)
 
 
 static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, size_t max_resp_size,
-                                                                                 uint8_t expected_size, uint8_t retries, uint32_t start_time, uint32_t timeout, uint32_t *eof_time) {
-       while (retries-- > 0) {
+                                                                                 uint8_t expected_size, uint8_t tries, uint32_t start_time, uint32_t timeout, uint32_t *eof_time) {
+       while (tries-- > 0) {
                ReaderTransmitIClass(command, cmdsize, &start_time);
                if (expected_size == GetIso15693AnswerFromTag(resp, max_resp_size, timeout, eof_time)) {
                        return true;
@@ -781,7 +781,7 @@ void iClass_Check(uint8_t *NRMAC) {
        uint8_t resp[4];
        memcpy(check+1, NRMAC, 8);
        uint32_t eof_time;
-       bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
+       bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 3, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
        cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp));
 }
 
@@ -793,7 +793,7 @@ void iClass_Readcheck(uint8_t block, bool use_credit_key) {
        }
        uint8_t resp[8];
        uint32_t eof_time;
-       bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
+       bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 3, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
        cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp));
 }
 
@@ -848,6 +848,9 @@ void iClass_Dump(uint8_t startblock, uint8_t numblks) {
                }
        }
 
+       FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+       LED_D_OFF();
+
        cmd_send(CMD_ACK, isOK, blkCnt, 0, readblockdata, blkCnt*8);
 
        LED_A_OFF();
@@ -863,12 +866,27 @@ static bool iClass_WriteBlock_ext(uint8_t blockNo, uint8_t *data) {
        bool isOK = false;
        uint32_t eof_time = 0;
 
-       isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_UPDATE, &eof_time);
-       if (isOK && blockNo != 3 && blockNo != 4 && memcmp(write+2, resp, 8)) { // check response
-               isOK = false;
+       isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 3, 0, ICLASS_READER_TIMEOUT_UPDATE, &eof_time);
+       if (!isOK) {
+               return false;
+       }
+       
+       uint8_t all_ff[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
+       if (blockNo == 2) {
+               if (memcmp(data+4, resp, 4) || memcmp(data, resp+4, 4)) { // check response. e-purse update swaps first and second half
+                       return false;
+               }
+       } else if (blockNo == 3 || blockNo == 4) {
+               if (memcmp(all_ff, resp, 8)) { // check response. Key updates always return 0xffffffffffffffff
+                       return false;
+               }
+       } else {
+               if (memcmp(data, resp, 8)) { // check response. All other updates return unchanged data
+                       return false;
+               }
        }
 
-       return isOK;
+       return true;
 }
 
 
@@ -892,30 +910,31 @@ void iClass_WriteBlock(uint8_t blockNo, uint8_t *data) {
 
 
 void iClass_Clone(uint8_t startblock, uint8_t endblock, uint8_t *data) {
-       int i;
+
+       LED_A_ON();
+
        int written = 0;
-       int total_block = (endblock - startblock) + 1;
-       for (i = 0; i < total_block; i++) {
+       int total_blocks = (endblock - startblock) + 1;
+
+       for (uint8_t block = startblock; block <= endblock; block++) {
                // block number
-               if (iClass_WriteBlock_ext(i+startblock, data + (i*12))){
-                       Dbprintf("Write block [%02x] successful", i + startblock);
+               if (iClass_WriteBlock_ext(block, data + (block-startblock)*12)) {
+                       Dbprintf("Write block [%02x] successful", block);
                        written++;
                } else {
-                       if (iClass_WriteBlock_ext(i+startblock, data + (i*12))){
-                               Dbprintf("Write block [%02x] successful", i + startblock);
-                               written++;
-                       } else {
-                               Dbprintf("Write block [%02x] failed", i + startblock);
-                       }
+                       Dbprintf("Write block [%02x] failed", block);
                }
        }
-       if (written == total_block)
+
+       if (written == total_blocks)
                Dbprintf("Clone complete");
        else
                Dbprintf("Clone incomplete");
 
-       cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
        LED_D_OFF();
+
+       cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
+
        LED_A_OFF();
 }
Impressum, Datenschutz