hf topaz reader implementation:
authorpwpiwi <pwpiwi@users.noreply.github.com>
Mon, 28 Sep 2015 06:26:00 +0000 (08:26 +0200)
committerpwpiwi <pwpiwi@users.noreply.github.com>
Mon, 28 Sep 2015 06:26:00 +0000 (08:26 +0200)
- read and display dynamic memory as well
- display "locked" status per byte instead per block

client/cmdhf.c
client/cmdhftopaz.c

index ee0b4fd8c1855f83da631df3476ad46b6de83bbd..caae08e5dba137c1c96529e3a0fbd6e86fa60b19 100644 (file)
@@ -690,7 +690,6 @@ int CmdHFSearch(const char *Cmd){
 
 static command_t CommandTable[] = 
 {
-<<<<<<< HEAD
        {"help",        CmdHelp,                1, "This help"},
        {"14a",         CmdHF14A,               1, "{ ISO14443A RFIDs... }"},
        {"14b",         CmdHF14B,               1, "{ ISO14443B RFIDs... }"},
@@ -703,22 +702,8 @@ static command_t CommandTable[] =
        {"topaz",       CmdHFTopaz,             1, "{ TOPAZ (NFC Type 1) RFIDs... }"},
        {"tune",        CmdHFTune,              0, "Continuously measure HF antenna tuning"},
        {"list",        CmdHFList,              1, "List protocol data in trace buffer"},
+       {"search",      CmdHFSearch,    1, "Search for known HF tags [preliminary]"},
        {NULL,          NULL,                   0, NULL}
-=======
-  {"help",        CmdHelp,          1, "This help"},
-  {"14a",         CmdHF14A,         1, "{ ISO14443A RFIDs... }"},
-  {"14b",         CmdHF14B,         1, "{ ISO14443B RFIDs... }"},
-  {"15",          CmdHF15,          1, "{ ISO15693 RFIDs... }"},
-  {"epa",         CmdHFEPA,         1, "{ German Identification Card... }"},
-  {"legic",       CmdHFLegic,       0, "{ LEGIC RFIDs... }"},
-  {"iclass",      CmdHFiClass,      1, "{ ICLASS RFIDs... }"},
-  {"mf",          CmdHFMF,          1, "{ MIFARE RFIDs... }"},
-  {"mfu",         CmdHFMFUltra,     1, "{ MIFARE Ultralight RFIDs... }"},
-  {"tune",        CmdHFTune,        0, "Continuously measure HF antenna tuning"},
-  {"list",        CmdHFList,        1, "List protocol data in trace buffer"},
-  {"search",      CmdHFSearch,      1, "Search for known HF tags [preliminary]"},
-       {NULL, NULL, 0, NULL}
->>>>>>> master
 };
 
 int CmdHF(const char *Cmd)
index ebe3a5717435aa177a657dfe30996fa129c6d14a..bf0f5dcfed6ba762b93659294655eeed7967d303 100644 (file)
 #include "iso14443crc.h"
 #include "protocols.h"
 
-#define TOPAZ_STATIC_MEMORY    (0x0f * 8)
+#define TOPAZ_STATIC_MEMORY    (0x0f * 8)              // 15 blocks with 8 Bytes each
 
+// a struct to describe a memory area which contains lock bits and the corresponding lockable memory area
 typedef struct dynamic_lock_area {
        struct dynamic_lock_area *next;
-       uint16_t        byte_offset;
+       uint16_t        byte_offset;                    // the address of the lock bits
        uint16_t        size_in_bits;
-       uint16_t        first_locked_byte;
+       uint16_t        first_locked_byte;              // the address of the lockable area
        uint16_t        bytes_locked_per_bit;
 } dynamic_lock_area_t;
 
@@ -36,11 +37,10 @@ typedef struct dynamic_lock_area {
 static struct {
        uint8_t HR01[2];
        uint8_t uid[7];
-       uint8_t size;
-       uint8_t data_blocks[TOPAZ_STATIC_MEMORY/8][8];
-       dynamic_lock_area_t *dynamic_lock_areas;
-       uint8_t *dynamic_reserved_areas;
-       uint8_t *dynamic_memory;
+       uint16_t size;
+       uint8_t data_blocks[TOPAZ_STATIC_MEMORY/8][8];  // this memory is always there
+       uint8_t *dynamic_memory;                                                // this memory can be there
+       dynamic_lock_area_t *dynamic_lock_areas;                // lock area descriptors
 } topaz_tag;
 
 
@@ -58,6 +58,7 @@ static void topaz_switch_off_field(void)
 }
 
 
+// send a raw topaz command, returns the length of the response (0 in case of error)
 static int topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response)
 {
        UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE, len, 0}};
@@ -75,6 +76,7 @@ static int topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response)
 }
 
 
+// calculate CRC bytes and send topaz command, returns the length of the response (0 in case of error) 
 static int topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response)
 {
        if (len > 1) {
@@ -88,6 +90,7 @@ static int topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response)
 }
 
 
+// select a topaz tag. Send WUPA and RID.
 static int topaz_select(uint8_t *atqa, uint8_t *rid_response)
 {
        // ToDo: implement anticollision
@@ -111,6 +114,7 @@ static int topaz_select(uint8_t *atqa, uint8_t *rid_response)
 }
 
 
+// read all of the static memory of a selected Topaz tag.
 static int topaz_rall(uint8_t *uid, uint8_t *response)
 {
        uint8_t rall_cmd[] = {TOPAZ_RALL, 0, 0, 0, 0, 0, 0, 0, 0};
@@ -125,6 +129,45 @@ static int topaz_rall(uint8_t *uid, uint8_t *response)
 }
 
 
+// read a block (8 Bytes) of a selected Topaz tag.
+static int topaz_read_block(uint8_t *uid, uint8_t blockno, uint8_t *block_data)
+{
+       uint8_t read8_cmd[] = {TOPAZ_READ8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+       uint8_t read8_response[11];
+       
+       read8_cmd[1] = blockno;
+       memcpy(&read8_cmd[10], uid, 4);
+       if (!topaz_send_cmd(read8_cmd, sizeof(read8_cmd), read8_response)) {
+               topaz_switch_off_field();
+               return -1;              // READ8 failed
+       }
+       
+       memcpy(block_data, &read8_response[1], 8);
+       
+       return 0;
+}
+
+
+// read a segment (16 blocks = 128 Bytes) of a selected Topaz tag. Works only for tags with dynamic memory.
+static int topaz_read_segment(uint8_t *uid, uint8_t segno, uint8_t *segment_data)
+{
+       uint8_t rseg_cmd[] = {TOPAZ_RSEG, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+       uint8_t rseg_response[131];
+       
+       rseg_cmd[1] = segno << 4;
+       memcpy(&rseg_cmd[10], uid, 4);
+       if (!topaz_send_cmd(rseg_cmd, sizeof(rseg_cmd), rseg_response)) {
+               topaz_switch_off_field();
+               return -1;              // RSEG failed
+       }
+       
+       memcpy(segment_data, &rseg_response[1], 128);
+       
+       return 0;
+}
+
+
+// search for the lock area descriptor for the lockable area including byteno 
 static dynamic_lock_area_t *get_dynamic_lock_area(uint16_t byteno)
 {
        dynamic_lock_area_t *lock_area;
@@ -143,8 +186,7 @@ static dynamic_lock_area_t *get_dynamic_lock_area(uint16_t byteno)
 }
 
 
-// check if a memory block (8 Bytes) is locked.
-// TODO: support other sizes of locked_bytes_per_bit (current assumption: each lock bit locks 8 Bytes) 
+// check if a memory byte is locked.
 static bool topaz_byte_is_locked(uint16_t byteno)
 {
        uint8_t *lockbits;
@@ -158,14 +200,15 @@ static bool topaz_byte_is_locked(uint16_t byteno)
                lock_area = get_dynamic_lock_area(byteno);
                if (lock_area == NULL) {
                        return false;
+               } else {
+                       lockbits = &topaz_tag.dynamic_memory[lock_area->byte_offset - TOPAZ_STATIC_MEMORY];
+                       locked_bytes_per_bit = lock_area->bytes_locked_per_bit;
+                       byteno = byteno - lock_area->first_locked_byte;
                }
-               locked_bytes_per_bit = lock_area->bytes_locked_per_bit;
-               byteno = byteno - lock_area->first_locked_byte;
-               lockbits = &topaz_tag.dynamic_memory[lock_area->byte_offset - TOPAZ_STATIC_MEMORY];
        }
 
        uint16_t blockno = byteno / locked_bytes_per_bit;
-       if(lockbits[blockno/8] >> (blockno % 8) & 0x01) {
+       if(lockbits[blockno/8] & (0x01 << (blockno % 8))) {
                return true;
        } else {
                return false;
@@ -173,9 +216,11 @@ static bool topaz_byte_is_locked(uint16_t byteno)
 }
 
 
+// read and print the Capability Container
 static int topaz_print_CC(uint8_t *data)
 {
        if(data[0] != 0xe1) {
+               topaz_tag.size = TOPAZ_STATIC_MEMORY;
                return -1;              // no NDEF message
        }
 
@@ -183,6 +228,7 @@ static int topaz_print_CC(uint8_t *data)
        PrintAndLog("  %02x: NDEF Magic Number", data[0]); 
        PrintAndLog("  %02x: version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
        uint16_t memsize = (data[2] + 1) * 8;
+       topaz_tag.size = memsize;
        topaz_tag.dynamic_memory = malloc(memsize - TOPAZ_STATIC_MEMORY);
        PrintAndLog("  %02x: Physical Memory Size of this tag: %d bytes", data[2], memsize);
        PrintAndLog("  %02x: %s / %s", data[3], 
@@ -192,14 +238,15 @@ static int topaz_print_CC(uint8_t *data)
 }
 
 
-static void get_TLV(uint8_t **TLV_ptr, uint8_t *tag, uint16_t *length, uint8_t **value)
+// return type, length and value of a TLV, starting at memory position *TLV_ptr 
+static void get_TLV(uint8_t **TLV_ptr, uint8_t *TLV_type, uint16_t *TLV_length, uint8_t **TLV_value)
 {
-       *length = 0;
-       *value = NULL;
+       *TLV_length = 0;
+       *TLV_value = NULL;
 
-       *tag = **TLV_ptr;
+       *TLV_type = **TLV_ptr;
        *TLV_ptr += 1;
-       switch (*tag) {
+       switch (*TLV_type) {
                case 0x00:                      // NULL TLV.
                case 0xFE:                      // Terminator TLV.
                        break;
@@ -207,16 +254,16 @@ static void get_TLV(uint8_t **TLV_ptr, uint8_t *tag, uint16_t *length, uint8_t *
                case 0x02:                      // Reserved Memory TLV
                case 0x03:                      // NDEF message TLV
                case 0xFD:                      // proprietary TLV
-                       *length = **TLV_ptr;
+                       *TLV_length = **TLV_ptr;
                        *TLV_ptr += 1;
-                       if (*length == 0xff) {
-                               *length = **TLV_ptr << 8;
+                       if (*TLV_length == 0xff) {
+                               *TLV_length = **TLV_ptr << 8;
                                *TLV_ptr += 1;
-                               *length |= **TLV_ptr;
+                               *TLV_length |= **TLV_ptr;
                                *TLV_ptr += 1;
                        }
-                       *value = *TLV_ptr;
-                       *TLV_ptr += *length;
+                       *TLV_value = *TLV_ptr;
+                       *TLV_ptr += *TLV_length;
                        break;
                default:                        // RFU
                        break;
@@ -224,27 +271,46 @@ static void get_TLV(uint8_t **TLV_ptr, uint8_t *tag, uint16_t *length, uint8_t *
 }
 
 
-static bool topaz_print_lock_control_TLVs(uint8_t *memory)
+// lock area TLVs contain no information on the start of the respective lockable area. Lockable areas
+// do not include the lock bits and reserved memory. We therefore need to adjust the start of the 
+// respective lockable areas accordingly
+static void adjust_lock_areas(uint16_t block_start, uint16_t block_size)
+{
+       dynamic_lock_area_t *lock_area = topaz_tag.dynamic_lock_areas;
+       while (lock_area != NULL) {
+               if (lock_area->first_locked_byte <= block_start) {
+                       lock_area->first_locked_byte += block_size;
+               }
+               lock_area = lock_area->next;
+       }
+}
+
+
+// read and print the lock area and reserved memory TLVs
+static void topaz_print_control_TLVs(uint8_t *memory)
 {
        uint8_t *TLV_ptr = memory;
-       uint8_t tag = 0;
-       uint16_t length;
-       uint8_t *value;
+       uint8_t TLV_type = 0;
+       uint16_t TLV_length;
+       uint8_t *TLV_value;
        bool lock_TLV_present = false;
-       uint16_t first_locked_byte = 0x0f * 8;
+       bool reserved_memory_control_TLV_present = false;
+       uint16_t next_lockable_byte = 0x0f * 8;                         // first byte after static memory area
        
        while(*TLV_ptr != 0x03 && *TLV_ptr != 0xFD && *TLV_ptr != 0xFE) {       
                // all Lock Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
-               get_TLV(&TLV_ptr, &tag, &length, &value);
-               if (tag == 0x01) {                      // the Lock Control TLV
-                       uint8_t pages_addr = value[0] >> 4;
-                       uint8_t byte_offset = value[0] & 0x0f;
-                       uint16_t size_in_bits = value[1] ? value[1] : 256;
-                       uint16_t bytes_per_page = 1 << (value[2] & 0x0f);
-                       uint16_t bytes_locked_per_bit = 1 << (value[2] >> 4);
+               get_TLV(&TLV_ptr, &TLV_type, &TLV_length, &TLV_value);
+               if (TLV_type == 0x01) {                 // a Lock Control TLV
+                       uint8_t pages_addr = TLV_value[0] >> 4;
+                       uint8_t byte_offset = TLV_value[0] & 0x0f;
+                       uint16_t size_in_bits = TLV_value[1] ? TLV_value[1] : 256;
+                       uint16_t size_in_bytes = (size_in_bits + 7)/8;
+                       uint16_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
+                       uint16_t bytes_locked_per_bit = 1 << (TLV_value[2] >> 4);
+                       uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
                        PrintAndLog("Lock Area of %d bits at byte offset 0x%04x. Each Lock Bit locks %d bytes.", 
                                                size_in_bits,
-                                               pages_addr * bytes_per_page + byte_offset,
+                                               area_start,
                                                bytes_locked_per_bit);
                        lock_TLV_present = true;
                        dynamic_lock_area_t *old = topaz_tag.dynamic_lock_areas;
@@ -258,67 +324,101 @@ static bool topaz_print_lock_control_TLVs(uint8_t *memory)
                                new = old->next = (dynamic_lock_area_t *)malloc(sizeof(dynamic_lock_area_t));
                        }
                        new->next = NULL;
-                       new->first_locked_byte = first_locked_byte;
-                       new->byte_offset = pages_addr * bytes_per_page + byte_offset;
+                       if (area_start <= next_lockable_byte) {
+                               // lock areas are not lockable
+                               next_lockable_byte += size_in_bytes;
+                       }
+                       new->first_locked_byte = next_lockable_byte;
+                       new->byte_offset = area_start;
                        new->size_in_bits = size_in_bits;
                        new->bytes_locked_per_bit = bytes_locked_per_bit;
-                       first_locked_byte = first_locked_byte + size_in_bits*bytes_locked_per_bit;
+                       next_lockable_byte += size_in_bits * bytes_locked_per_bit;              
+               }
+               if (TLV_type == 0x02) {                 // a Reserved Memory Control TLV
+                       uint8_t pages_addr = TLV_value[0] >> 4;
+                       uint8_t byte_offset = TLV_value[0] & 0x0f;
+                       uint8_t size_in_bytes = TLV_value[1] ? TLV_value[1] : 256;
+                       uint8_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
+                       uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
+                       PrintAndLog("Reserved Memory of %d bytes at byte offset 0x%02x.", 
+                                               size_in_bytes,
+                                               area_start);
+                       reserved_memory_control_TLV_present = true;
+                       adjust_lock_areas(area_start, size_in_bytes);  // reserved memory areas are not lockable
+                       if (area_start <= next_lockable_byte) {
+                               next_lockable_byte += size_in_bytes;
+                       }
                }
        }
        
        if (!lock_TLV_present) {
                PrintAndLog("(No Lock Control TLV present)");
-               return -1;
-       } else {
-               return 0;
        }
+       
+       if (!reserved_memory_control_TLV_present) {
+               PrintAndLog("(No Reserved Memory Control TLV present)");
+       }       
 }
 
 
-static int topaz_print_reserved_memory_control_TLVs(uint8_t *memory)
+// read all of the dynamic memory
+static int topaz_read_dynamic_data(void)
 {
-       uint8_t *TLV_ptr = memory;
-       uint8_t tag = 0;
-       uint16_t length;
-       uint8_t *value;
-       bool reserved_memory_control_TLV_present = false;
+       // first read the remaining block of segment 0
+       if(topaz_read_block(topaz_tag.uid, 0x0f, &topaz_tag.dynamic_memory[0]) == -1) {
+               PrintAndLog("Error while reading dynamic memory block %02x. Aborting...", 0x0f);
+               return -1;
+       }
        
-       while(*TLV_ptr != 0x03 && *TLV_ptr != 0xFD && *TLV_ptr != 0xFE) {       
-               // all Reserved Memory Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
-               get_TLV(&TLV_ptr, &tag, &length, &value);
-               if (tag == 0x02) {                      // the Reserved Memory Control TLV
-                       uint8_t pages_addr = value[0] >> 4;
-                       uint8_t byte_offset = value[0] & 0x0f;
-                       uint8_t size_in_bytes = value[1] ? value[1] : 256;
-                       uint8_t bytes_per_page = 1 << (value[2] & 0x0f);
-                       PrintAndLog("Reserved Memory of %d bytes at byte offset 0x%02x.", 
-                                               size_in_bytes,
-                                               pages_addr * bytes_per_page + byte_offset);
-                       reserved_memory_control_TLV_present = true;
+       // read the remaining segments
+       uint8_t max_segment = topaz_tag.size / 128 - 1;
+       for(uint8_t segment = 1; segment <= max_segment; segment++) {
+               if(topaz_read_segment(topaz_tag.uid, segment, &topaz_tag.dynamic_memory[(segment-1)*128+8]) == -1) {
+                       PrintAndLog("Error while reading dynamic memory block %02x. Aborting...", 0x0f);
+                       return -1;
                }
        }
        
-       if (!reserved_memory_control_TLV_present) {
-               PrintAndLog("(No Reserved Memory Control TLV present)");
-               return -1;
-       } else {
-               return 0;
+       return 0;
+}
+       
+
+// read and print the dynamic memory
+static void topaz_print_dynamic_data(void)
+{
+       if (topaz_tag.size > TOPAZ_STATIC_MEMORY) {
+               PrintAndLog("Dynamic Data blocks:");
+               if (topaz_read_dynamic_data() == 0) {
+                       PrintAndLog("block# | offset | Data                    | Locked(y/n)");
+                       char line[80];
+                       for (uint16_t blockno = 0x0f; blockno < topaz_tag.size/8; blockno++) {
+                               uint8_t *block_data = &topaz_tag.dynamic_memory[(blockno-0x0f)*8];
+                               char lockbits[9];
+                               for (uint16_t j = 0; j < 8; j++) {
+                                       sprintf(&line[3*j], "%02x ", block_data[j]);
+                                       lockbits[j] = topaz_byte_is_locked(blockno*8+j) ? 'y' : 'n';
+                               }
+                               lockbits[8] = '\0';
+                               PrintAndLog("  0x%02x | 0x%04x | %s|   %-3s", blockno, blockno*8, line, lockbits);
+                       }
+               }
        }
 }
 
 
 static void topaz_print_lifecycle_state(uint8_t *data)
 {
-
+       // to be done
 }
 
 
 static void topaz_print_NDEF(uint8_t *data)
 {
-
+       // to be done. 
 }
 
-       
+
+// read a Topaz tag and print some usefull information 
 int CmdHFTopazReader(const char *Cmd)
 {
        int status;
@@ -378,16 +478,19 @@ int CmdHFTopazReader(const char *Cmd)
                        topaz_tag.uid[6], 
                        getTagInfo(topaz_tag.uid[6]));
 
-       memcpy(topaz_tag.data_blocks, rall_response+2, 0x10*8);
+       memcpy(topaz_tag.data_blocks, rall_response+2, 0x0f*8);
        PrintAndLog("");
        PrintAndLog("Static Data blocks 00 to 0c:");
        PrintAndLog("block# | offset | Data                    | Locked(y/n)");
        char line[80];
        for (uint16_t i = 0; i <= 0x0c; i++) {
+               char lockbits[9];
                for (uint16_t j = 0; j < 8; j++) {
                        sprintf(&line[3*j], "%02x ", topaz_tag.data_blocks[i][j] /*rall_response[2 + 8*i + j]*/);
+                       lockbits[j] = topaz_byte_is_locked(i*8+j) ? 'y' : 'n';
                }
-               PrintAndLog("  0x%02x | 0x%04x | %s|   %-3s", i, i*8, line, topaz_byte_is_locked(i*8) ? "yyyyyyyy" : "nnnnnnnn");
+               lockbits[8] = '\0';
+               PrintAndLog("  0x%02x | 0x%04x | %s|   %-3s", i, i*8, line, lockbits);
        }
        
        PrintAndLog("");
@@ -409,17 +512,17 @@ int CmdHFTopazReader(const char *Cmd)
        status = topaz_print_CC(&topaz_tag.data_blocks[1][0]);
        
        if (status == -1) {
-               PrintAndLog("No NDEF message present");
+               PrintAndLog("No NDEF message data present");
                topaz_switch_off_field();
                return 0;
        }
 
        PrintAndLog("");
-       bool lock_TLV_present = topaz_print_lock_control_TLVs(&topaz_tag.data_blocks[1][4]);
+       topaz_print_control_TLVs(&topaz_tag.data_blocks[1][4]);
 
        PrintAndLog("");
-       bool reserved_mem_present = topaz_print_reserved_memory_control_TLVs(&topaz_tag.data_blocks[1][4]);
-
+       topaz_print_dynamic_data();
+       
        topaz_print_lifecycle_state(&topaz_tag.data_blocks[1][0]);
 
        topaz_print_NDEF(&topaz_tag.data_blocks[1][0]);
@@ -429,16 +532,9 @@ int CmdHFTopazReader(const char *Cmd)
 }
 
 
-int CmdHFTopazSim(const char *Cmd)
-{
-       PrintAndLog("not yet implemented");
-       return 0;
-}
-
-
 int CmdHFTopazCmdRaw(const char *Cmd)
 {
-       PrintAndLog("not yet implemented");
+       PrintAndLog("not yet implemented. Use hf 14 raw with option -T.");
        return 0;
 }
 
@@ -450,7 +546,6 @@ static command_t CommandTable[] =
 {
        {"help",        CmdHelp,                        1, "This help"},
        {"reader",      CmdHFTopazReader,       0, "Act like a Topaz reader"},
-       {"sim",         CmdHFTopazSim,          0, "<UID> -- Simulate Topaz tag"},
        {"snoop",       CmdHF14ASnoop,          0, "Eavesdrop a Topaz reader-tag communication"},
        {"raw",         CmdHFTopazCmdRaw,       0, "Send raw hex data to tag"},
        {NULL,          NULL,                           0, NULL}
@@ -466,6 +561,7 @@ int CmdHFTopaz(const char *Cmd) {
        return 0;
 }
 
+
 static int CmdHelp(const char *Cmd)
 {
        CmdsHelp(CommandTable);
Impressum, Datenschutz