From c279f88f137215dfb23377ee1940c64fbcf0b56f Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Fri, 23 Aug 2024 19:57:28 +0800 Subject: [PATCH] refactor: Move Hitag 2 cmds to protocols.h --- armsrc/hitag2.c | 23 ++++++++++++----------- client/src/cmdlfhitag.c | 25 ++++++++++++++++--------- include/protocols.h | 11 ++++++----- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index 30ba0935a..d782e66d8 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -31,6 +31,7 @@ #include "lfdemod.h" #include "commonutil.h" #include "appmain.h" +#include "protocols.h" #define test_bit(data, i) (*(data + (i/8)) >> (7-(i % 8))) & 1 #define set_bit(data, i) *(data + (i/8)) |= (1 << (7-(i % 8))) @@ -210,9 +211,9 @@ static void hitag2_handle_reader_command(uint8_t *rx, const size_t rxlen, uint8_ // Try to find out which command was send by selecting on length (in bits) switch (rxlen) { // Received 11000 from the reader, request for UID, send UID - case 05: { + case 5: { // Always send over the air in the clear plaintext mode - if (rx_air[0] != 0xC0) { + if (rx_air[0] != HITAG2_START_AUTH) { // Unknown frame ? return; } @@ -234,13 +235,13 @@ static void hitag2_handle_reader_command(uint8_t *rx, const size_t rxlen, uint8_ switch (rx[0] & 0xC6) { // Read command: 11xx x00y - case 0xC0: { + case HITAG2_READ_PAGE: { memcpy(tx, tag.sectors[sector], 4); *txlen = 32; break; } // Inverted Read command: 01xx x10y - case 0x44: { + case HITAG2_READ_PAGE_INVERTED: { for (size_t i = 0; i < 4; i++) { tx[i] = tag.sectors[sector][i] ^ 0xff; } @@ -248,7 +249,7 @@ static void hitag2_handle_reader_command(uint8_t *rx, const size_t rxlen, uint8_ break; } // Write command: 10xx x01y - case 0x82: { + case HITAG2_WRITE_PAGE: { // Prepare write, acknowledge by repeating command memcpy(tx, rx, nbytes(rxlen)); *txlen = rxlen; @@ -648,7 +649,7 @@ static bool hitag2_write_page(uint8_t *rx, const size_t rxlen, uint8_t *tx, size switch (writestate) { case WRITE_STATE_START: { *txlen = 10; - tx[0] = 0x82 | (blocknr << 3) | ((blocknr ^ 7) >> 2); + tx[0] = HITAG2_WRITE_PAGE | (blocknr << 3) | ((blocknr ^ 7) >> 2); tx[1] = ((blocknr ^ 7) << 6); writestate = WRITE_STATE_PAGENUM_WRITTEN; break; @@ -656,7 +657,7 @@ static bool hitag2_write_page(uint8_t *rx, const size_t rxlen, uint8_t *tx, size case WRITE_STATE_PAGENUM_WRITTEN: { // Check if page number was received correctly if ((rxlen == 10) - && (rx[0] == (0x82 | (blocknr << 3) | ((blocknr ^ 7) >> 2))) + && (rx[0] == (HITAG2_WRITE_PAGE | (blocknr << 3) | ((blocknr ^ 7) >> 2))) && (rx[1] == (((blocknr & 0x3) ^ 0x3) << 6))) { *txlen = 32; @@ -748,7 +749,7 @@ static bool hitag2_password(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t } *txlen = 10; - tx[0] = 0xC0 | (blocknr << 3) | ((blocknr ^ 7) >> 2); + tx[0] = HITAG2_READ_PAGE | (blocknr << 3) | ((blocknr ^ 7) >> 2); tx[1] = ((blocknr ^ 7) << 6); } } @@ -871,7 +872,7 @@ static bool hitag2_crypto(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t * return false; } else { *txlen = 10; - tx[0] = 0xc0 | (blocknr << 3) | ((blocknr ^ 7) >> 2); + tx[0] = HITAG2_READ_PAGE | (blocknr << 3) | ((blocknr ^ 7) >> 2); tx[1] = ((blocknr ^ 7) << 6); } } @@ -957,7 +958,7 @@ static bool hitag2_authenticate(uint8_t *rx, const size_t rxlen, uint8_t *tx, si DBG Dbprintf("Sending read block %u", blocknr); *txlen = 10; - tx[0] = 0xc0 | (blocknr << 3) | ((blocknr ^ 7) >> 2); + tx[0] = HITAG2_READ_PAGE | (blocknr << 3) | ((blocknr ^ 7) >> 2); tx[1] = ((blocknr ^ 7) << 6); } } @@ -2628,7 +2629,7 @@ int ht2_read_uid(uint8_t *uid, bool ledcontrol, bool send_answer, bool keep_fiel // start AUTH command size_t txlen = 5; - uint8_t tx[1] = {0xC0}; + uint8_t tx[0] = {HITAG2_START_AUTH}; // Transmit as reader ht2_send(turn_on, &command_start, &command_duration, &response_start, tx, txlen, false); diff --git a/client/src/cmdlfhitag.c b/client/src/cmdlfhitag.c index 0fc537527..eb7ab3eac 100644 --- a/client/src/cmdlfhitag.c +++ b/client/src/cmdlfhitag.c @@ -522,6 +522,13 @@ bool hitag2_get_plain(uint8_t *plain, uint8_t *plen) { return false; } +// HITAG 2 commands +#define HITAG2_BINSTR_START_AUTH "11000" // get UID and/or start the authentication process +#define HITAG2_BINSTR_READ_PAGE "11" // read page after auth +#define HITAG2_BINSTR_READ_PAGE_INVERTED "01" // as read page but all bits inverted +#define HITAG2_BINSTR_WRITE_PAGE "10" // write page after auth +#define HITAG2_BINSTR_HALT "00" // silence currently authenticated tag + static uint8_t hitag2_get_page(const char *bs) { if ((memcmp(bs + 2, "000", 3) == 0) && (memcmp(bs + 2 + 3 + 2, "111", 3) == 0)) { return 0; @@ -578,24 +585,24 @@ void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint8_t c break; } case 10: { - if (memcmp(binstr, HITAG2_HALT, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_HALT, 2) == 0) { snprintf(exp, size, " "); break; } uint8_t page = hitag2_get_page(binstr); - if (memcmp(binstr, HITAG2_READ_PAGE, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_READ_PAGE, 2) == 0) { snprintf(exp, size, "READ PAGE (" _MAGENTA_("%u") ")", page); break; } - if (memcmp(binstr, HITAG2_READ_PAGE_INVERTED, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_READ_PAGE_INVERTED, 2) == 0) { snprintf(exp, size, "READ PAGE INV (" _MAGENTA_("%u") ")", page); break; } - if (memcmp(binstr, HITAG2_WRITE_PAGE, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_WRITE_PAGE, 2) == 0) { snprintf(exp, size, "WRITE PAGE (" _MAGENTA_("%u") ")", page); break; } @@ -654,7 +661,7 @@ void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, case 5: { annotateHitag2_init(); - if (memcmp(binstr, HITAG2_START_AUTH, 5) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_START_AUTH, 5) == 0) { snprintf(exp, size, "START AUTH"); _ht2state.state = STATE_START_AUTH; } else { @@ -669,7 +676,7 @@ void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, break; } - if (memcmp(binstr, HITAG2_HALT, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_HALT, 2) == 0) { snprintf(exp, size, "HALT"); _ht2state.state = STATE_HALT; break; @@ -677,17 +684,17 @@ void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, uint8_t page = hitag2_get_page(binstr); - if (memcmp(binstr, HITAG2_READ_PAGE, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_READ_PAGE, 2) == 0) { snprintf(exp, size, "READ PAGE (" _MAGENTA_("%u") ")", page); break; } - if (memcmp(binstr, HITAG2_READ_PAGE_INVERTED, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_READ_PAGE_INVERTED, 2) == 0) { snprintf(exp, size, "READ PAGE INV (" _MAGENTA_("%u") ")", page); break; } - if (memcmp(binstr, HITAG2_WRITE_PAGE, 2) == 0) { + if (memcmp(binstr, HITAG2_BINSTR_WRITE_PAGE, 2) == 0) { snprintf(exp, size, "WRITE PAGE (" _MAGENTA_("%u") ")", page); break; } diff --git a/include/protocols.h b/include/protocols.h index 2f8b3d098..d44a8151c 100644 --- a/include/protocols.h +++ b/include/protocols.h @@ -911,11 +911,12 @@ ISO 7816-4 Basic interindustry commands. For command APDU's. #define HITAG1_HALT 0x70 // left 4 bits only, followed by 8 bits (dummy) page and 8 bits CRC // HITAG 2 commands -#define HITAG2_START_AUTH "11000" // get UID and/or start the authentication process -#define HITAG2_READ_PAGE "11" // read page after auth -#define HITAG2_READ_PAGE_INVERTED "01" // as read page but all bits inverted -#define HITAG2_WRITE_PAGE "10" // write page after auth -#define HITAG2_HALT "00" // silence currently authenticated tag +#define HITAG2_START_AUTH 0xC0 // left 5 bits only +#define HITAG2_READ_PAGE 0xC0 // page number in bits 5 to 3, page number inverted in bit 0 and following 2 bits +#define HITAG2_READ_PAGE_INVERTED 0x44 // page number in bits 5 to 3, page number inverted in bit 0 and following 2 bits +#define HITAG2_WRITE_PAGE 0x82 // page number in bits 5 to 3, page number inverted in bit 0 and following 2 bits +#define HITAG2_HALT 0x00 // left 5 bits only + // HITAG S commands #define HITAGS_UID_REQ_STD 0x30 // 00110 UID REQUEST Std