From f7d4f2e9f3a0a069f916b406519104095bf1e9db Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Sun, 20 Oct 2019 18:28:07 +0200 Subject: [PATCH 01/10] Unfinished request service commands. --- armsrc/felica.c | 14 ++-- client/cmdhffelica.c | 149 +++++++++++++++++++++++++++++++++++++++---- client/cmdhffelica.h | 3 +- 3 files changed, 144 insertions(+), 22 deletions(-) diff --git a/armsrc/felica.c b/armsrc/felica.c index ab22bc778..023f4d719 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -14,17 +14,17 @@ // FeliCa timings // minimum time between the start bits of consecutive transfers from reader to tag: 6800 carrier (13.56MHz) cycles #ifndef FELICA_REQUEST_GUARD_TIME -# define FELICA_REQUEST_GUARD_TIME (6800/16 + 1) +# define FELICA_REQUEST_GUARD_TIME (6800/16 + 1) // 426 #endif // FRAME DELAY TIME 2672 carrier cycles #ifndef FELICA_FRAME_DELAY_TIME -# define FELICA_FRAME_DELAY_TIME (2672/16 + 1) +# define FELICA_FRAME_DELAY_TIME (2672/16 + 1) // 168 #endif #ifndef DELAY_AIR2ARM_AS_READER -#define DELAY_AIR2ARM_AS_READER (3 + 16 + 8 + 8*16 + 4*16 - 8*16) +#define DELAY_AIR2ARM_AS_READER (3 + 16 + 8 + 8*16 + 4*16 - 8*16) // 91 #endif #ifndef DELAY_ARM2AIR_AS_READER -#define DELAY_ARM2AIR_AS_READER (4*16 + 8*16 + 8 + 8 + 1) +#define DELAY_ARM2AIR_AS_READER (4*16 + 8*16 + 8 + 8 + 1) // 209 #endif // CRC skips two first sync bits in data buffer @@ -357,7 +357,6 @@ static void TransmitFor18092_AsReader(uint8_t *frame, int len, uint32_t *timing, // sending 0x00 0x00 0x00 0x00 0x00 0x00 uint16_t c = 0; while (c < 6) { - // keep tx buffer in a defined state anyway. if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { AT91C_BASE_SSC->SSC_THR = 0x00; @@ -416,8 +415,6 @@ bool WaitForFelicaReply(uint16_t maxbytes) { uint32_t timeout = iso18092_get_timeout(); if (DBGLEVEL > 3) Dbprintf("timeout set: %i", timeout); - //TODO FIX THIS METHOD - Race Condition or something: TIMING/MEMORY ISSUES - // If you add content here (dbprintf), timing problems appear?! Last Bytes (CRC) of frame will be cutoff. for (;;) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { @@ -527,7 +524,7 @@ void felica_sendraw(PacketCommandNG *c) { arg0 = felica_select_card(&card); reply_mix(CMD_ACK, arg0, sizeof(card.uid), 0, &card, sizeof(felica_card_select_t)); if (arg0 > 0) { - Dbprintf("Error: Failed selecting card! "); + Dbprintf("Error: Failed selecting card!"); felica_reset_frame_mode(); return; } @@ -565,6 +562,7 @@ void felica_sendraw(PacketCommandNG *c) { Dbprintf("Received Frame Code: %d", arg0); Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0); }; + uint32_t result = reply_mix(CMD_ACK, FelicaFrame.len, arg0, 0, FelicaFrame.framebytes, FelicaFrame.len); if (result) { Dbprintf("Reply to Client Error Code: %i", result); diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 22acf6344..288ef409e 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -43,7 +43,7 @@ static int usage_hf_felica_sim(void) { static int usage_hf_felica_sniff(void) { PrintAndLogEx(NORMAL, "It get data from the field and saves it into command buffer."); PrintAndLogEx(NORMAL, "Buffer accessible from command 'hf list felica'"); - PrintAndLogEx(NORMAL, "Usage: hf felica sniff "); + PrintAndLogEx(NORMAL, "Usage: hf felica sniff "); PrintAndLogEx(NORMAL, " s samples to skip (decimal)"); PrintAndLogEx(NORMAL, " t triggers to skip (decimal)"); PrintAndLogEx(NORMAL, "Examples:"); @@ -81,6 +81,17 @@ static int usage_hf_felica_raw(void) { return 0; } +static int usage_hf_felica_request_service(void) { + PrintAndLogEx(NORMAL, "\nInfo: Use this command to verify the existence of Area and Service, and to acquire Key Version:"); + PrintAndLogEx(NORMAL, " - When the specified Area or Service exists, the card returns Key Version."); + PrintAndLogEx(NORMAL, " - When the specified Area or Service does not exist, the card returns FFFFh as Key Version."); + PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] <0A 0B 0C ... IDm hex> <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)> <0A 0B CRC hex>"); + PrintAndLogEx(NORMAL, " -h this help"); + PrintAndLogEx(NORMAL, " -c calculate and append CRC"); + PrintAndLogEx(NORMAL, "Example: rqservice 01100910c11bc407 01 FFFF 2837"); + return 0; +} + static int usage_hf_felica_dump(void) { PrintAndLogEx(NORMAL, "Usage: hf felica dump [-h] "); PrintAndLogEx(NORMAL, " -h this help"); @@ -102,7 +113,95 @@ static int CmdHFFelicaReader(const char *Cmd) { static int CmdHFFelicaDump(const char *Cmd) { if (strlen(Cmd) < 1) return usage_hf_felica_dump(); - dump(*Cmd); + clearCommandBuffer(); + char ctmp = tolower(param_getchar(Cmd, 0)); + if (ctmp == 'h') return usage_hf_felica_dumplite(); + dump(); + return 0; +} + +static int CmdHFFelicaRequestService(const char *Cmd) { + if (strlen(Cmd) < 2) return usage_hf_felica_request_service(); + char buf[5] = ""; + int i = 0; + uint8_t data[PM3_CMD_DATA_SIZE]; + uint16_t datalen = 0; + set_number_of_cmds(); + while (Cmd[i] != '\0') { + PrintAndLogEx(NORMAL, "String %s: ", Cmd[i]); + if (Cmd[i] == '-') { + switch (Cmd[i + 1]) { + case 'H': + case 'h': + return usage_hf_felica_raw(); + case 'c': + crc = true; + break; + default: + return usage_hf_felica_raw(); + } + i += 2; + } + i = i + parse_cmd_parameter(i); + if(is_hex_input()){ + buf[strlen(buf) + 1] = 0; + buf[strlen(buf)] = Cmd[i]; + i++; + i = i + get_cmd_data(i); + }else{ + i++; + } + } + request_service(); + clearCommandBuffer(); + return 0; +} + +/* + * Parses line spacing and tabs. + * Returns 1 if the given char is a space or tab + */ +static int parse_cmd_parameter_separator(const char *Cmd, int i){ + PrintAndLogEx(NORMAL, "parse_cmd_parameter_separator String %s: ", Cmd[i]); + return Cmd[i] == ' ' || Cmd[i] == '\t' ? 1 : 0; +} + +/* + * Counts and sets the number of commands. + */ +static void set_number_of_cmds(const char *Cmd){ + while (*Cmd == ' ' || *Cmd == '\t'){ + Cmd++; + } +} + +/** + * Checks if a char is a hex value. + * @param Cmd + * @return one if it is a valid hex char. Zero if not a valid hex char. + */ +static bool is_hex_input(const char *Cmd, int i){ + PrintAndLogEx(NORMAL, "is_hex_input String %s: ", Cmd[i]); + return (Cmd[i] >= '0' && Cmd[i] <= '9') || (Cmd[i] >= 'a' && Cmd[i] <= 'f') || (Cmd[i] >= 'A' && Cmd[i] <= 'F') ? 1 : 0; +} + +/** + * + * @param Cmd the chars from which the data will be extracted. + * @return a buffer with the data from the command + */ +static int get_cmd_data(const char *Cmd, int i){ + int char_counter = 0; + if (strlen(buf) >= 2) { + sscanf(buf, "%x", &temp); + data[datalen] = (uint8_t)(temp & 0xff); + *buf = 0; + } + return 0; +} + +static int CmdHFFelicaNotImplementedYet(const char *Cmd) { + PrintAndLogEx(NORMAL, "Feature not implemented Yet!"); return 0; } @@ -174,7 +273,6 @@ static int CmdHFFelicaSim(const char *Cmd) { */ static int CmdHFFelicaSniff(const char *Cmd) { - uint8_t cmdp = 0; uint64_t samples2skip = 0; uint64_t triggers2skip = 0; @@ -211,7 +309,6 @@ static int CmdHFFelicaSniff(const char *Cmd) { // uid hex static int CmdHFFelicaSimLite(const char *Cmd) { - uint64_t uid = param_get64ex(Cmd, 0, 0, 16); if (!uid) @@ -446,6 +543,9 @@ static void waitCmdFelica(uint8_t iSelect) { if (!len) return; PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.data.asBytes, len)); + if(!check_crc(CRC_FELICA, resp.data.asBytes + 2, len - 2)){ + PrintAndLogEx(ERR, "Error: CRC of received bytes are incorrect!"); + } } else { PrintAndLogEx(WARNING, "Timeout while waiting for reply."); } @@ -530,7 +630,6 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) { if (crc && datalen > 0 && datalen < sizeof(data) - 2) { uint8_t b1, b2; compute_crc(CRC_FELICA, data, datalen, &b1, &b2); - // TODO FIND OUT IF FeliCa Light has another CRC order - Order changed for FeliCa Standard cards data[datalen++] = b2; data[datalen++] = b1; } @@ -617,27 +716,53 @@ int readFelicaUid(bool verbose) { return status; } +int dump() { -int dump(const char *Cmd) { - clearCommandBuffer(); - char ctmp = tolower(param_getchar(Cmd, 0)); - if (ctmp == 'h') return usage_hf_felica_dumplite(); // TODO FINISH THIS METHOD PrintAndLogEx(SUCCESS, "NOT IMPLEMENTED YET!"); + return 0; +} + +int request_service() { + + + return 0; } static command_t CommandTable[] = { + {"----------- General -----------", CmdHelp, IfPm3Iso14443a, ""}, {"help", CmdHelp, AlwaysAvailable, "This help"}, {"list", CmdHFFelicaList, AlwaysAvailable, "List ISO 18092/FeliCa history"}, {"reader", CmdHFFelicaReader, IfPm3Felica, "Act like an ISO18092/FeliCa reader"}, -// {"sim", CmdHFFelicaSim, IfPm3Felica, " -- Simulate ISO 18092/FeliCa tag"}, - {"sniff", CmdHFFelicaSniff, IfPm3Felica, "sniff ISO 18092/Felica traffic"}, + {"sniff", CmdHFFelicaSniff, IfPm3Felica, "Sniff ISO 18092/FeliCa traffic"}, {"raw", CmdHFFelicaCmdRaw, IfPm3Felica, "Send raw hex data to tag"}, - {"dump", CmdHFFelicaDump, IfPm3Felica, "Wait for and try dumping Felica"}, + {"----------- FeliCa Standard (support in progress) -----------", CmdHelp, IfPm3Iso14443a, ""}, + {"dump", CmdHFFelicaDump, IfPm3Felica, "Wait for and try dumping FeliCa"}, + {"rqservice", CmdHFFelicaRequestService, IfPm3Felica, "verify the existence of Area and Service, and to acquire Key Version."}, + {"rqresponse", CmdHFFelicaNotImplementedYet, IfPm3Felica, "verify the existence of a card and its Mode."}, + //{"rdNoEncryption", CmdHFFelicaNotImplementedYet, IfPm3Felica, "read Block Data from authentication-not-required Service."}, + //{"wrNoEncryption", CmdHFFelicaNotImplementedYet, IfPm3Felica, "write Block Data to an authentication-required Service."}, + //{"searchSvCode", CmdHFFelicaNotImplementedYet, IfPm3Felica, "acquire Area Code and Service Code."}, + //{"rqSysCode", CmdHFFelicaNotImplementedYet, IfPm3Felica, "acquire System Code registered to the card."}, + //{"auth1", CmdHFFelicaNotImplementedYet, IfPm3Felica, "authenticate a card."}, + //{"auth2", CmdHFFelicaNotImplementedYet, IfPm3Felica, "allow a card to authenticate a Reader/Writer."}, + //{"read", CmdHFFelicaNotImplementedYet, IfPm3Felica, "read Block Data from authentication-required Service."}, + //{"write", CmdHFFelicaNotImplementedYet, IfPm3Felica, "write Block Data to an authentication-required Service."}, + //{"searchSvCodeV2", CmdHFFelicaNotImplementedYet, IfPm3Felica, "verify the existence of Area or Service, and to acquire Key Version."}, + //{"getSysStatus", CmdHFFelicaNotImplementedYet, IfPm3Felica, "acquire the setup information in System."}, + //{"rqSpecVer", CmdHFFelicaNotImplementedYet, IfPm3Felica, "acquire the version of card OS."}, + //{"resetMode", CmdHFFelicaNotImplementedYet, IfPm3Felica, "reset Mode to Mode 0."}, + //{"auth1V2", CmdHFFelicaNotImplementedYet, IfPm3Felica, "authenticate a card."}, + //{"auth2V2", CmdHFFelicaNotImplementedYet, IfPm3Felica, "allow a card to authenticate a Reader/Writer."}, + //{"readV2", CmdHFFelicaNotImplementedYet, IfPm3Felica, "read Block Data from authentication-required Service."}, + //{"writeV2", CmdHFFelicaNotImplementedYet, IfPm3Felica, "write Block Data to authentication-required Service."}, + //{"upRandomID", CmdHFFelicaNotImplementedYet, IfPm3Felica, "update Random ID (IDr)."}, + {"----------- FeliCa Light -----------", CmdHelp, IfPm3Iso14443a, ""}, {"litesim", CmdHFFelicaSimLite, IfPm3Felica, " - only reply to poll request"}, {"litedump", CmdHFFelicaDumpLite, IfPm3Felica, "Wait for and try dumping FelicaLite"}, + // {"sim", CmdHFFelicaSim, IfPm3Felica, " -- Simulate ISO 18092/FeliCa tag"} {NULL, NULL, NULL, NULL} }; diff --git a/client/cmdhffelica.h b/client/cmdhffelica.h index abbbd42a4..88a1a0080 100644 --- a/client/cmdhffelica.h +++ b/client/cmdhffelica.h @@ -14,8 +14,7 @@ #include "common.h" int CmdHFFelica(const char *Cmd); - int readFelicaUid(bool verbose); - int dump(); +int request_service(); #endif From a3f8d635ca6f33550fc4cb494b7003b6580c3ac2 Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Sun, 20 Oct 2019 20:26:00 +0200 Subject: [PATCH 02/10] Add new cmd parser functions. --- client/cmdhffelica.c | 134 ++++++++++++++++++++++--------------------- client/cmdhffelica.h | 2 - 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 04580b239..5f963b947 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -88,10 +88,52 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] <0A 0B 0C ... IDm hex> <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)> <0A 0B CRC hex>"); PrintAndLogEx(NORMAL, " -h this help"); PrintAndLogEx(NORMAL, " -c calculate and append CRC"); - PrintAndLogEx(NORMAL, "Example: rqservice 01100910c11bc407 01 FFFF 2837"); + PrintAndLogEx(NORMAL, "\nExample: hf felica rqservice 01100910c11bc407 01 FFFF 2837\n\n"); return PM3_SUCCESS; } +/* + * Parses line spacing and tabs. + * Returns 1 if the given char is a space or tab + */ +static int parse_cmd_parameter_separator(const char *Cmd, int i){ + return Cmd[i] == ' ' || Cmd[i] == '\t' ? 1 : 0; +} + +/* + * Counts and sets the number of commands. + */ +static void strip_cmds(const char *Cmd){ + PrintAndLogEx(NORMAL, "CMD count: %i", strlen(Cmd)); + while (*Cmd == ' ' || *Cmd == '\t'){ + PrintAndLogEx(NORMAL, "CMD: %s", Cmd); + Cmd++; + } + PrintAndLogEx(NORMAL, "CMD string: %s", Cmd); +} + +/** + * Checks if a char is a hex value. + * @param Cmd + * @return one if it is a valid hex char. Zero if not a valid hex char. + */ +static bool is_hex_input(const char *Cmd, int i){ + return (Cmd[i] >= '0' && Cmd[i] <= '9') || (Cmd[i] >= 'a' && Cmd[i] <= 'f') || (Cmd[i] >= 'A' && Cmd[i] <= 'F') ? 1 : 0; +} + +/** + * + * @param Extracts the data from the cmd and puts it into the data array. + */ +static void get_cmd_data(const char *Cmd, int i, uint16_t datalen, uint8_t *data, char buf[]){ + uint32_t temp; + if (strlen(buf) >= 2) { + sscanf(buf, "%x", &temp); + data[datalen] = (uint8_t)(temp & 0xff); + *buf = 0; + } +} + static int usage_hf_felica_dump(void) { PrintAndLogEx(NORMAL, "Usage: hf felica dump [-h] "); PrintAndLogEx(NORMAL, " -h this help"); @@ -120,15 +162,24 @@ static int CmdHFFelicaDump(const char *Cmd) { return 0; } +/** + * Command parser for rqservice. + * @param Cmd input data of the user. + * @return client result code. + */ static int CmdHFFelicaRequestService(const char *Cmd) { if (strlen(Cmd) < 2) return usage_hf_felica_request_service(); - char buf[5] = ""; int i = 0; uint8_t data[PM3_CMD_DATA_SIZE]; + bool crc = false; + bool length = false; uint16_t datalen = 0; - set_number_of_cmds(); + char buf[5] = ""; + + strip_cmds(Cmd); while (Cmd[i] != '\0') { - PrintAndLogEx(NORMAL, "String %s: ", Cmd[i]); + PrintAndLogEx(NORMAL, "Parse String %s: ", Cmd); + PrintAndLogEx(NORMAL, "i = %i: ", i); if (Cmd[i] == '-') { switch (Cmd[i + 1]) { case 'H': @@ -137,72 +188,36 @@ static int CmdHFFelicaRequestService(const char *Cmd) { case 'c': crc = true; break; + case 'l': + length = true; + break; default: return usage_hf_felica_raw(); } i += 2; } - i = i + parse_cmd_parameter(i); - if(is_hex_input()){ + PrintAndLogEx(NORMAL, "i after single params = %i: ", i); + i = i + parse_cmd_parameter_separator(Cmd, i); + PrintAndLogEx(NORMAL, "i after cnd separator: %i", i); + if (is_hex_input(Cmd, i)){ buf[strlen(buf) + 1] = 0; buf[strlen(buf)] = Cmd[i]; i++; - i = i + get_cmd_data(i); - }else{ + PrintAndLogEx(NORMAL, "i after is hex input: %i", i); + get_cmd_data(Cmd, i, datalen, data, buf); + + }else { i++; } } request_service(); clearCommandBuffer(); - return 0; -} - -/* - * Parses line spacing and tabs. - * Returns 1 if the given char is a space or tab - */ -static int parse_cmd_parameter_separator(const char *Cmd, int i){ - PrintAndLogEx(NORMAL, "parse_cmd_parameter_separator String %s: ", Cmd[i]); - return Cmd[i] == ' ' || Cmd[i] == '\t' ? 1 : 0; -} - -/* - * Counts and sets the number of commands. - */ -static void set_number_of_cmds(const char *Cmd){ - while (*Cmd == ' ' || *Cmd == '\t'){ - Cmd++; - } -} - -/** - * Checks if a char is a hex value. - * @param Cmd - * @return one if it is a valid hex char. Zero if not a valid hex char. - */ -static bool is_hex_input(const char *Cmd, int i){ - PrintAndLogEx(NORMAL, "is_hex_input String %s: ", Cmd[i]); - return (Cmd[i] >= '0' && Cmd[i] <= '9') || (Cmd[i] >= 'a' && Cmd[i] <= 'f') || (Cmd[i] >= 'A' && Cmd[i] <= 'F') ? 1 : 0; -} - -/** - * - * @param Cmd the chars from which the data will be extracted. - * @return a buffer with the data from the command - */ -static int get_cmd_data(const char *Cmd, int i){ - int char_counter = 0; - if (strlen(buf) >= 2) { - sscanf(buf, "%x", &temp); - data[datalen] = (uint8_t)(temp & 0xff); - *buf = 0; - } - return 0; + return PM3_SUCCESS; } static int CmdHFFelicaNotImplementedYet(const char *Cmd) { PrintAndLogEx(NORMAL, "Feature not implemented Yet!"); - return 0; + return PM3_SUCCESS; } // simulate iso18092 / FeliCa tag @@ -717,21 +732,10 @@ int readFelicaUid(bool verbose) { return PM3_SUCCESS; } -int dump(const char *Cmd) { - clearCommandBuffer(); - char ctmp = tolower(param_getchar(Cmd, 0)); - if (ctmp == 'h') return usage_hf_felica_dumplite(); - - // TODO FINISH THIS METHOD - PrintAndLogEx(SUCCESS, "NOT IMPLEMENTED YET!"); - return PM3_SUCCESS; -} - int request_service() { return PM3_SUCCESS; } - static command_t CommandTable[] = { {"----------- General -----------", CmdHelp, IfPm3Iso14443a, ""}, {"help", CmdHelp, AlwaysAvailable, "This help"}, @@ -740,7 +744,7 @@ static command_t CommandTable[] = { {"sniff", CmdHFFelicaSniff, IfPm3Felica, "Sniff ISO 18092/FeliCa traffic"}, {"raw", CmdHFFelicaCmdRaw, IfPm3Felica, "Send raw hex data to tag"}, {"----------- FeliCa Standard (support in progress) -----------", CmdHelp, IfPm3Iso14443a, ""}, - {"dump", CmdHFFelicaDump, IfPm3Felica, "Wait for and try dumping FeliCa"}, + //{"dump", CmdHFFelicaDump, IfPm3Felica, "Wait for and try dumping FeliCa"}, {"rqservice", CmdHFFelicaRequestService, IfPm3Felica, "verify the existence of Area and Service, and to acquire Key Version."}, {"rqresponse", CmdHFFelicaNotImplementedYet, IfPm3Felica, "verify the existence of a card and its Mode."}, //{"rdNoEncryption", CmdHFFelicaNotImplementedYet, IfPm3Felica, "read Block Data from authentication-not-required Service."}, diff --git a/client/cmdhffelica.h b/client/cmdhffelica.h index 88a1a0080..59032ee31 100644 --- a/client/cmdhffelica.h +++ b/client/cmdhffelica.h @@ -15,6 +15,4 @@ int CmdHFFelica(const char *Cmd); int readFelicaUid(bool verbose); -int dump(); -int request_service(); #endif From 3a3b66d0798472e82fe22cc667704bb4d7a15c5a Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Sun, 20 Oct 2019 20:28:57 +0200 Subject: [PATCH 03/10] Fix build --- client/cmdhffelica.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 5f963b947..caeb4cb9f 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -134,11 +134,13 @@ static void get_cmd_data(const char *Cmd, int i, uint16_t datalen, uint8_t *data } } +/* static int usage_hf_felica_dump(void) { + // TODO IMPLEMENT PrintAndLogEx(NORMAL, "Usage: hf felica dump [-h] "); PrintAndLogEx(NORMAL, " -h this help"); return PM3_SUCCESS; -} +}*/ static int CmdHFFelicaList(const char *Cmd) { (void)Cmd; // Cmd is not used so far @@ -152,14 +154,19 @@ static int CmdHFFelicaReader(const char *Cmd) { return readFelicaUid(verbose); } +/* static int CmdHFFelicaDump(const char *Cmd) { if (strlen(Cmd) < 1) return usage_hf_felica_dump(); - return dump(*Cmd); - clearCommandBuffer(); - char ctmp = tolower(param_getchar(Cmd, 0)); - if (ctmp == 'h') return usage_hf_felica_dumplite(); - dump(); - return 0; + // TODO IMPLEMENT + return PM3_SUCCESS; +}*/ + +/** + * Sends a request service frame + * @return + */ +static int request_service() { + return PM3_SUCCESS; } /** @@ -732,10 +739,6 @@ int readFelicaUid(bool verbose) { return PM3_SUCCESS; } -int request_service() { - return PM3_SUCCESS; -} - static command_t CommandTable[] = { {"----------- General -----------", CmdHelp, IfPm3Iso14443a, ""}, {"help", CmdHelp, AlwaysAvailable, "This help"}, From e7f7810d2260bd5fa5f27a954af908160400e353 Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Mon, 21 Oct 2019 09:59:15 +0200 Subject: [PATCH 04/10] Make style. Change DBG_DEBUG. Refactor CRC calculation --- armsrc/felica.c | 34 ++++++------ client/cmdhf.c | 2 +- client/cmdhffelica.c | 128 ++++++++++++++++++++++++++----------------- 3 files changed, 96 insertions(+), 68 deletions(-) diff --git a/armsrc/felica.c b/armsrc/felica.c index 6a0e8c190..3b4de9d5f 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -229,21 +229,21 @@ static uint8_t felica_select_card(felica_card_select_t *card) { // timed-out if (len == 0) { - if (DBGLEVEL > 3) + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Error: Time out card selection!"); return 1; } // wrong answer if (FelicaFrame.framebytes[3] != FELICA_POLL_ACK) { - if (DBGLEVEL > 3) + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Error: Wrong answer selecting card!"); return 2; } // VALIDATE CRC residue is 0, hence if crc is a value it failed. if (!check_crc(CRC_FELICA, FelicaFrame.framebytes + 2, FelicaFrame.len - 2)) { - if (DBGLEVEL > 3) { + if (DBGLEVEL >= DBG_DEBUG) { Dbprintf("Error: CRC check failed!"); Dbprintf("CRC check was done on Frame: "); Dbhexdump(FelicaFrame.len - 2, FelicaFrame.framebytes + 2, 0); @@ -251,7 +251,7 @@ static uint8_t felica_select_card(felica_card_select_t *card) { return 3; } - if (DBGLEVEL > 3) + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Card selection successful!"); // copy UID // idm 8 @@ -263,7 +263,7 @@ static uint8_t felica_select_card(felica_card_select_t *card) { memcpy(card->uid, card->IDm + 2, 6); memcpy(card->iccode, card->PMm, 2); memcpy(card->mrt, card->PMm + 2, 6); - if (DBGLEVEL > 3) { + if (DBGLEVEL >= DBG_DEBUG) { Dbprintf("Received Frame: "); Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0); } @@ -365,7 +365,7 @@ static void TransmitFor18092_AsReader(uint8_t *frame, int len, uint32_t *timing, } // sending data with sync bytes c = 0; - if (DBGLEVEL > 3) { + if (DBGLEVEL >= DBG_DEBUG) { Dbprintf("Sending frame:"); Dbhexdump(len, frame, 0); } @@ -403,7 +403,7 @@ static void TransmitFor18092_AsReader(uint8_t *frame, int len, uint32_t *timing, // stop when button is pressed // or return TRUE when command is captured bool WaitForFelicaReply(uint16_t maxbytes) { - if (DBGLEVEL > 3) + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("WaitForFelicaReply Start"); uint32_t c = 0; // power, no modulation @@ -413,7 +413,7 @@ bool WaitForFelicaReply(uint16_t maxbytes) { // clear RXRDY: uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; uint32_t timeout = iso18092_get_timeout(); - if (DBGLEVEL > 3) + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("timeout set: %i", timeout); for (;;) { WDT_HIT(); @@ -435,10 +435,10 @@ bool WaitForFelicaReply(uint16_t maxbytes) { NULL, false ); - if (DBGLEVEL > 3) Dbprintf("All bytes received! STATE_FULL"); + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("All bytes received! STATE_FULL"); return true; } else if (c++ > timeout && FelicaFrame.state == STATE_UNSYNCD) { - if (DBGLEVEL > 3) Dbprintf("Error: Timeout! STATE_UNSYNCD"); + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Error: Timeout! STATE_UNSYNCD"); return false; } } @@ -448,7 +448,7 @@ bool WaitForFelicaReply(uint16_t maxbytes) { // Set up FeliCa communication (similar to iso14443a_setup) // field is setup for "Sending as Reader" static void iso18092_setup(uint8_t fpga_minor_mode) { - if (DBGLEVEL > 3) Dbprintf("Start iso18092_setup"); + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Start iso18092_setup"); LEDsoff(); FpgaDownloadAndGo(FPGA_BITSTREAM_HF); @@ -501,7 +501,7 @@ void felica_reset_frame_mode() { // arg1 len of commandbytes // d.asBytes command bytes to send void felica_sendraw(PacketCommandNG *c) { - if (DBGLEVEL > 3) Dbprintf("FeliCa_sendraw Enter"); + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("FeliCa_sendraw Enter"); felica_command_t param = c->oldarg[0]; size_t len = c->oldarg[1] & 0xffff; @@ -511,7 +511,7 @@ void felica_sendraw(PacketCommandNG *c) { felica_card_select_t card; if ((param & FELICA_CONNECT)) - if (DBGLEVEL > 3) Dbprintf("Clear trace"); + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Clear trace"); clear_trace(); set_tracing(true); @@ -530,7 +530,7 @@ void felica_sendraw(PacketCommandNG *c) { } } } else { - if (DBGLEVEL > 3) Dbprintf("No card selection"); + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("No card selection"); } if ((param & FELICA_RAW)) { @@ -551,14 +551,14 @@ void felica_sendraw(PacketCommandNG *c) { AddCrc(buf, len); } } - if (DBGLEVEL > 3) { + if (DBGLEVEL >= DBG_DEBUG) { Dbprintf("Transmit Frame (no CRC shown):"); Dbhexdump(len, buf, 0); Dbprintf("Buffer Length: %i", buf[2] + 4); }; TransmitFor18092_AsReader(buf, buf[2] + 4, NULL, 1, 0); arg0 = WaitForFelicaReply(1024); - if (DBGLEVEL > 3) { + if (DBGLEVEL >= DBG_DEBUG) { Dbprintf("Received Frame Code: %d", arg0); Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0); }; @@ -571,7 +571,7 @@ void felica_sendraw(PacketCommandNG *c) { if ((param & FELICA_NO_DISCONNECT)) { Dbprintf("Disconnect"); } - if (DBGLEVEL > 3) + if (DBGLEVEL >= DBG_DEBUG) Dbprintf("FeliCa_sendraw Exit"); felica_reset_frame_mode(); return; diff --git a/client/cmdhf.c b/client/cmdhf.c index bf8a9dceb..9166b01ca 100644 --- a/client/cmdhf.c +++ b/client/cmdhf.c @@ -96,7 +96,7 @@ int CmdHFSearch(const char *Cmd) { return PM3_SUCCESS; } } - + PROMPT_CLEARLINE; PrintAndLogEx(INPLACE, "Searching for ISO15693 tag..."); if (IfPm3Iso15693()) { diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index caeb4cb9f..c50382758 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -85,27 +85,47 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, "\nInfo: Use this command to verify the existence of Area and Service, and to acquire Key Version:"); PrintAndLogEx(NORMAL, " - When the specified Area or Service exists, the card returns Key Version."); PrintAndLogEx(NORMAL, " - When the specified Area or Service does not exist, the card returns FFFFh as Key Version."); - PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] <0A 0B 0C ... IDm hex> <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)> <0A 0B CRC hex>"); + PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] [-i] <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)>"); PrintAndLogEx(NORMAL, " -h this help"); - PrintAndLogEx(NORMAL, " -c calculate and append CRC"); + PrintAndLogEx(NORMAL, " -i <0A 0B 0C ... hex> set IDm"); PrintAndLogEx(NORMAL, "\nExample: hf felica rqservice 01100910c11bc407 01 FFFF 2837\n\n"); return PM3_SUCCESS; } +/** + * Wait for response from pm3 or timeout. + * Checks if receveid bytes have a valid CRC. + */ +static void waitCmdFelica(uint8_t iSelect) { + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { + uint16_t len = iSelect ? (resp.oldarg[1] & 0xffff) : (resp.oldarg[0] & 0xffff); + PrintAndLogEx(NORMAL, "Client Received %i octets", len); + if (!len) + return; + PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.data.asBytes, len)); + if (!check_crc(CRC_FELICA, resp.data.asBytes + 2, len - 2)) { + PrintAndLogEx(ERR, "Error: CRC of received bytes are incorrect!"); + } + } else { + PrintAndLogEx(WARNING, "Timeout while waiting for reply."); + } +} + /* * Parses line spacing and tabs. * Returns 1 if the given char is a space or tab */ -static int parse_cmd_parameter_separator(const char *Cmd, int i){ +static int parse_cmd_parameter_separator(const char *Cmd, int i) { return Cmd[i] == ' ' || Cmd[i] == '\t' ? 1 : 0; } /* * Counts and sets the number of commands. */ -static void strip_cmds(const char *Cmd){ +static void strip_cmds(const char *Cmd) { PrintAndLogEx(NORMAL, "CMD count: %i", strlen(Cmd)); - while (*Cmd == ' ' || *Cmd == '\t'){ + while (*Cmd == ' ' || *Cmd == '\t') { PrintAndLogEx(NORMAL, "CMD: %s", Cmd); Cmd++; } @@ -117,15 +137,36 @@ static void strip_cmds(const char *Cmd){ * @param Cmd * @return one if it is a valid hex char. Zero if not a valid hex char. */ -static bool is_hex_input(const char *Cmd, int i){ +static bool is_hex_input(const char *Cmd, int i) { return (Cmd[i] >= '0' && Cmd[i] <= '9') || (Cmd[i] >= 'a' && Cmd[i] <= 'f') || (Cmd[i] >= 'A' && Cmd[i] <= 'F') ? 1 : 0; } /** - * - * @param Extracts the data from the cmd and puts it into the data array. + * Add crc bytes to the end of the given data. + * @param datalen length of the data frame. + * @param data frame on which the crc is calculated. + * @param size of the data. + * @return true if the crc was added. */ -static void get_cmd_data(const char *Cmd, int i, uint16_t datalen, uint8_t *data, char buf[]){ +static bool add_crc_bytes(uint16_t *datalen, uint8_t *data, size_t dataSize) { + if (*datalen > 0 && *datalen < dataSize - 2) { + uint8_t b1, b2; + compute_crc(CRC_FELICA, data, *datalen, &b1, &b2); + data[(*datalen)++] = b2; + data[(*datalen)++] = b1; + return 1; + } + return 0; +} + +/** + * Extracts the data from the cmd and puts it into the data array. + * @param Cmd input string of the user with the data + * @param datalen length of the data frame. + * @param data the array in which the data gets stored. + * @param buf temporary buffer. + */ +static void get_cmd_data(const char *Cmd, uint16_t datalen, uint8_t *data, char buf[]) { uint32_t temp; if (strlen(buf) >= 2) { sscanf(buf, "%x", &temp); @@ -161,14 +202,6 @@ static int CmdHFFelicaDump(const char *Cmd) { return PM3_SUCCESS; }*/ -/** - * Sends a request service frame - * @return - */ -static int request_service() { - return PM3_SUCCESS; -} - /** * Command parser for rqservice. * @param Cmd input data of the user. @@ -178,9 +211,10 @@ static int CmdHFFelicaRequestService(const char *Cmd) { if (strlen(Cmd) < 2) return usage_hf_felica_request_service(); int i = 0; uint8_t data[PM3_CMD_DATA_SIZE]; - bool crc = false; - bool length = false; + bool custom_IDm = false; uint16_t datalen = 0; + uint16_t numbits = 0; + uint8_t flags = 0; char buf[5] = ""; strip_cmds(Cmd); @@ -192,11 +226,8 @@ static int CmdHFFelicaRequestService(const char *Cmd) { case 'H': case 'h': return usage_hf_felica_raw(); - case 'c': - crc = true; - break; - case 'l': - length = true; + case 'i': + custom_IDm = true; break; default: return usage_hf_felica_raw(); @@ -206,19 +237,33 @@ static int CmdHFFelicaRequestService(const char *Cmd) { PrintAndLogEx(NORMAL, "i after single params = %i: ", i); i = i + parse_cmd_parameter_separator(Cmd, i); PrintAndLogEx(NORMAL, "i after cnd separator: %i", i); - if (is_hex_input(Cmd, i)){ + if (is_hex_input(Cmd, i)) { buf[strlen(buf) + 1] = 0; buf[strlen(buf)] = Cmd[i]; i++; PrintAndLogEx(NORMAL, "i after is hex input: %i", i); - get_cmd_data(Cmd, i, datalen, data, buf); - - }else { - i++; + get_cmd_data(Cmd, datalen, data, buf); + } else { + i++; } } - request_service(); + flags |= FELICA_APPEND_CRC; + if (custom_IDm) { + flags |= FELICA_NO_SELECT; + } + if (datalen > 0) { + flags |= FELICA_RAW; + } + datalen = (datalen > PM3_CMD_DATA_SIZE) ? PM3_CMD_DATA_SIZE : datalen; clearCommandBuffer(); + PrintAndLogEx(NORMAL, "Data: %s", data); + SendCommandMIX(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); + if (custom_IDm) { + waitCmdFelica(1); + } + if (datalen > 0) { + waitCmdFelica(0); + } return PM3_SUCCESS; } @@ -557,21 +602,7 @@ static int CmdHFFelicaDumpLite(const char *Cmd) { return PM3_SUCCESS; } -static void waitCmdFelica(uint8_t iSelect) { - PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { - uint16_t len = iSelect ? (resp.oldarg[1] & 0xffff) : (resp.oldarg[0] & 0xffff); - PrintAndLogEx(NORMAL, "Client Received %i octets", len); - if (!len) - return; - PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.data.asBytes, len)); - if(!check_crc(CRC_FELICA, resp.data.asBytes + 2, len - 2)){ - PrintAndLogEx(ERR, "Error: CRC of received bytes are incorrect!"); - } - } else { - PrintAndLogEx(WARNING, "Timeout while waiting for reply."); - } -} + static int CmdHFFelicaCmdRaw(const char *Cmd) { bool reply = 1; @@ -649,11 +680,8 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) { return PM3_EINVARG; } - if (crc && datalen > 0 && datalen < sizeof(data) - 2) { - uint8_t b1, b2; - compute_crc(CRC_FELICA, data, datalen, &b1, &b2); - data[datalen++] = b2; - data[datalen++] = b1; + if (crc) { + add_crc_bytes(&datalen, data, sizeof(data)); } uint8_t flags = 0; From 6144e2d09bab56b29f746db53bac5e82495107af Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Tue, 22 Oct 2019 15:56:05 +0200 Subject: [PATCH 05/10] Add felica request service command. Add cmd parser functions. --- armsrc/felica.c | 84 +-------------------------- client/cmdhffelica.c | 131 +++++++++++++++++++++++++++++++------------ include/mifare.h | 9 +++ 3 files changed, 107 insertions(+), 117 deletions(-) diff --git a/armsrc/felica.c b/armsrc/felica.c index 3b4de9d5f..83ff2bea3 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -337,9 +337,7 @@ static void BuildFliteRdblk(uint8_t *idm, int blocknum, uint16_t *blocks) { } static void TransmitFor18092_AsReader(uint8_t *frame, int len, uint32_t *timing, uint8_t power, uint8_t highspeed) { - uint8_t flags = FPGA_MAJOR_MODE_ISO18092; - if (power) flags |= FPGA_HF_ISO18092_FLAG_READER; if (highspeed) @@ -371,7 +369,6 @@ static void TransmitFor18092_AsReader(uint8_t *frame, int len, uint32_t *timing, } while (c < len) { - // Put byte into tx holding register as soon as it is ready if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { AT91C_BASE_SSC->SSC_THR = frame[c++]; @@ -415,18 +412,16 @@ bool WaitForFelicaReply(uint16_t maxbytes) { uint32_t timeout = iso18092_get_timeout(); if (DBGLEVEL >= DBG_DEBUG) Dbprintf("timeout set: %i", timeout); + for (;;) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { b = (uint8_t)(AT91C_BASE_SSC->SSC_RHR); Process18092Byte(b); if (FelicaFrame.state == STATE_FULL) { - felica_nexttransfertime = - MAX( - felica_nexttransfertime, + felica_nexttransfertime = MAX(felica_nexttransfertime, (GetCountSspClk() & 0xfffffff8) - (DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) / 16 + FELICA_FRAME_DELAY_TIME - ); - + ); LogTrace( FelicaFrame.framebytes, FelicaFrame.len, @@ -774,79 +769,6 @@ void felica_dump() { } } -} - -void felica_send_request_service(uint8_t *request_service) { - Dbprintf("Send Service Request - len: d%", RES_SVC_LEN); - TransmitFor18092_AsReader(request_service, RES_SVC_LEN, NULL, 1, 0); - if (WaitForFelicaReply(512) && FelicaFrame.framebytes[3] == FELICA_REQSRV_ACK) { - Dbprintf("Got Service Response!"); - } -} - -/* Create Request Service Frame -// Use this command to verify the existence of Area and Service, and to acquire Key Version. -// When the specified Area or Service exists, the card returns Key Version. -// When the specified Area or Service does not exist, the card returns FFFFh as Key Version. -*/ -uint8_t *felica_create_request_service_frame(uint8_t nodeNumber, uint8_t *idm) { - if (nodeNumber < 1 && nodeNumber > 32) { - Dbprintf("Node number out of range: 1 <= %d <= 32 - set node number to 1"); - nodeNumber = 1; - } - // Sync 2-Byte, Length 1-Byte, CMD 1-Byte, IDm 8-Byte, nodeNumber 1 <= n <= 32 1-Byte, Node Code List - uint8_t *request_service = BigBuf_malloc(sizeof(uint8_t) * RES_SVC_LEN); - //{ 0xb2, 0x4d, 0x06, FELICA_REQSRV_REQ, 0xff, 0xff, 0x00, 0x00, 0x09, 0x21}; - request_service[0] = 0xb2; //Sync - request_service[1] = 0x4d; //Sync - request_service[2] = 0x0B; // Length - request_service[3] = FELICA_REQSRV_REQ; // CMD - request_service[4] = idm[0]; - request_service[5] = idm[1]; - request_service[6] = idm[2]; - request_service[7] = idm[3]; - request_service[8] = idm[4]; - request_service[9] = idm[5]; - request_service[10] = idm[6]; - request_service[11] = idm[7]; - request_service[12] = nodeNumber; // Node we like to ask for services - request_service[13] = 0x00; // Node Code List // TODO FIND OUT WHAT NEEDS TO BE IN HERE - return request_service; -} - -// Create Frame for authentication1 CMD -void felica_create_authentication1_frame() { - -} - -// Create Frame for authentication2 CMD -void felica_create_authentication2_frame() { - -} - -// Create a Frame for Read without encryption CMD as Payload -void felica_create_read_block_frame(uint16_t blockNr) { - if (blockNr < 1 || blockNr > 567) { - Dbprintf("Block number out of range!"); - return; - } - uint8_t c = 0; - // First Byte of SYNC - frameSpace[c++] = 0xb2; - frameSpace[c++] = 0x4d; - // skip Length of Frame - c++; - // Payload - frameSpace[c++] = FELICA_RDBLK_REQ; //command number - - // Set frame length - - // CRC -} - -void felica_read_block(uint8_t *idm, uint16_t blockNr) { - - } void felica_dump_lite_s() { diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index c50382758..81fb3e67d 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -24,6 +24,11 @@ #include "mifare.h" // felica_card_select_t struct static int CmdHelp(const char *Cmd); +static felica_card_select_t last_known_card; + +static void set_last_known_card(felica_card_select_t card){ + last_known_card = card; +} /* static int usage_hf_felica_sim(void) { @@ -85,10 +90,14 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, "\nInfo: Use this command to verify the existence of Area and Service, and to acquire Key Version:"); PrintAndLogEx(NORMAL, " - When the specified Area or Service exists, the card returns Key Version."); PrintAndLogEx(NORMAL, " - When the specified Area or Service does not exist, the card returns FFFFh as Key Version."); + PrintAndLogEx(NORMAL, "For Node Code List of a command packet, Area Code or Service Code of the target " + "of acquisition of Key Version shall be enumerated in Little Endian format." + "If Key Version of System is the target of acquisition, FFFFh shall be specified " + "in the command packet."); PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] [-i] <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)>"); PrintAndLogEx(NORMAL, " -h this help"); - PrintAndLogEx(NORMAL, " -i <0A 0B 0C ... hex> set IDm"); - PrintAndLogEx(NORMAL, "\nExample: hf felica rqservice 01100910c11bc407 01 FFFF 2837\n\n"); + PrintAndLogEx(NORMAL, " -i <0A 0B 0C ... hex> set custom IDm to use"); + PrintAndLogEx(NORMAL, "\nExample: hf felica rqservice 01 FFFF \n\n"); return PM3_SUCCESS; } @@ -96,20 +105,22 @@ static int usage_hf_felica_request_service(void) { * Wait for response from pm3 or timeout. * Checks if receveid bytes have a valid CRC. */ -static void waitCmdFelica(uint8_t iSelect) { - PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { - uint16_t len = iSelect ? (resp.oldarg[1] & 0xffff) : (resp.oldarg[0] & 0xffff); +static bool waitCmdFelica(uint8_t iSelect, PacketResponseNG *resp) { + if (WaitForResponseTimeout(CMD_ACK, resp, 2000)) { + uint16_t len = iSelect ? (resp->oldarg[1] & 0xffff) : (resp->oldarg[0] & 0xffff); PrintAndLogEx(NORMAL, "Client Received %i octets", len); - if (!len) - return; - PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.data.asBytes, len)); - if (!check_crc(CRC_FELICA, resp.data.asBytes + 2, len - 2)) { - PrintAndLogEx(ERR, "Error: CRC of received bytes are incorrect!"); + if (!len || len < 2){ + PrintAndLogEx(ERR, "Could not receive data correctly!"); } + PrintAndLogEx(NORMAL, "%s", sprint_hex(resp->data.asBytes, len)); + if (!check_crc(CRC_FELICA, resp->data.asBytes + 2, len - 2)) { + PrintAndLogEx(WARNING, "Wrong or no CRC bytes"); + } + return true; } else { PrintAndLogEx(WARNING, "Timeout while waiting for reply."); } + return false; } /* @@ -124,12 +135,9 @@ static int parse_cmd_parameter_separator(const char *Cmd, int i) { * Counts and sets the number of commands. */ static void strip_cmds(const char *Cmd) { - PrintAndLogEx(NORMAL, "CMD count: %i", strlen(Cmd)); while (*Cmd == ' ' || *Cmd == '\t') { - PrintAndLogEx(NORMAL, "CMD: %s", Cmd); Cmd++; } - PrintAndLogEx(NORMAL, "CMD string: %s", Cmd); } /** @@ -160,21 +168,53 @@ static bool add_crc_bytes(uint16_t *datalen, uint8_t *data, size_t dataSize) { } /** - * Extracts the data from the cmd and puts it into the data array. + * Extracts the hex data from the cmd and puts it into the data array. * @param Cmd input string of the user with the data * @param datalen length of the data frame. * @param data the array in which the data gets stored. - * @param buf temporary buffer. + * @param buf buffer with hex data. */ -static void get_cmd_data(const char *Cmd, uint16_t datalen, uint8_t *data, char buf[]) { - uint32_t temp; +static void get_cmd_data(const char *Cmd, uint16_t *datalen, uint8_t *data, char buf[]) { + uint32_t hex; if (strlen(buf) >= 2) { - sscanf(buf, "%x", &temp); - data[datalen] = (uint8_t)(temp & 0xff); + sscanf(buf, "%x", &hex); + data[*datalen] = (uint8_t)(hex & 0xff); + (*datalen)++; *buf = 0; } } +/** + * Converts integer value to equivalent hex value. + * @param number number of hex bytes. + * @return number as hex value. + */ +static uint8_t int_to_hex(uint16_t *number){ + uint32_t hex; + char dataLengthChar[5]; + sprintf(dataLengthChar, "%x", *number); + sscanf(dataLengthChar, "%x", &hex); + return (uint8_t)(hex & 0xff); +} + +/** + * Adds the last known IDm (8-Byte) to the data frame. + * @param position start of where the IDm is added within the frame. + * @param data frame in where the IDM is added. + * @return true if IDm was added; + */ +static bool add_last_IDm(uint8_t position, uint8_t *data){ + if(last_known_card.IDm[0] != 0 && last_known_card.IDm[1] != 0){ + for(int i = 0; i < 8; i++){ + uint16_t number = (uint16_t)last_known_card.IDm[i]; + data[position+i] = int_to_hex(&number); + } + return true; + }else{ + return false; + } +} + /* static int usage_hf_felica_dump(void) { // TODO IMPLEMENT @@ -213,14 +253,12 @@ static int CmdHFFelicaRequestService(const char *Cmd) { uint8_t data[PM3_CMD_DATA_SIZE]; bool custom_IDm = false; uint16_t datalen = 0; - uint16_t numbits = 0; uint8_t flags = 0; + uint16_t numbits = 0; char buf[5] = ""; - + datalen += 10; // length (1) + CMD (1) + IDm (8) strip_cmds(Cmd); while (Cmd[i] != '\0') { - PrintAndLogEx(NORMAL, "Parse String %s: ", Cmd); - PrintAndLogEx(NORMAL, "i = %i: ", i); if (Cmd[i] == '-') { switch (Cmd[i + 1]) { case 'H': @@ -234,15 +272,12 @@ static int CmdHFFelicaRequestService(const char *Cmd) { } i += 2; } - PrintAndLogEx(NORMAL, "i after single params = %i: ", i); i = i + parse_cmd_parameter_separator(Cmd, i); - PrintAndLogEx(NORMAL, "i after cnd separator: %i", i); if (is_hex_input(Cmd, i)) { buf[strlen(buf) + 1] = 0; buf[strlen(buf)] = Cmd[i]; i++; - PrintAndLogEx(NORMAL, "i after is hex input: %i", i); - get_cmd_data(Cmd, datalen, data, buf); + get_cmd_data(Cmd, &datalen, data, buf); } else { i++; } @@ -255,14 +290,37 @@ static int CmdHFFelicaRequestService(const char *Cmd) { flags |= FELICA_RAW; } datalen = (datalen > PM3_CMD_DATA_SIZE) ? PM3_CMD_DATA_SIZE : datalen; + if(!custom_IDm){ + if(!add_last_IDm(2, data)){ + PrintAndLogEx(ERR, "No last known card! Use reader first or set a custom IDm!"); + return PM3_EINVARG; + }else{ + PrintAndLogEx(INFO, "Used last known IDm.", sprint_hex(data, datalen)); + } + } + data[0] = int_to_hex(&datalen); + data[1] = 0x02; // Request Command ID + add_crc_bytes(&datalen, data, sizeof(data)); + PrintAndLogEx(NORMAL, "Send Service Request Frame: %s", sprint_hex(data, datalen)); clearCommandBuffer(); - PrintAndLogEx(NORMAL, "Data: %s", data); SendCommandMIX(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); + PacketResponseNG resp; if (custom_IDm) { - waitCmdFelica(1); + waitCmdFelica(1, &resp); } if (datalen > 0) { - waitCmdFelica(0); + if(!waitCmdFelica(0, &resp)){ + return PM3_ESOFT; + } + felica_request_service_response_t rqs_response; + memcpy(&rqs_response, (felica_request_service_response_t *)resp.data.asBytes, sizeof(felica_request_service_response_t)); + + PrintAndLogEx(SUCCESS, "\nGot Service Response:"); + PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rqs_response.IDm, sizeof(rqs_response.IDm))); + PrintAndLogEx(NORMAL, " -Node Number: %s", sprint_hex(rqs_response.node_number, sizeof(rqs_response.node_number))); + PrintAndLogEx(NORMAL, " -Node Key Version List: %s\n", sprint_hex(rqs_response.node_key_versions, sizeof(rqs_response.node_key_versions))); + }else{ + return PM3_ESOFT; } return PM3_SUCCESS; } @@ -602,8 +660,6 @@ static int CmdHFFelicaDumpLite(const char *Cmd) { return PM3_SUCCESS; } - - static int CmdHFFelicaCmdRaw(const char *Cmd) { bool reply = 1; bool crc = false; @@ -703,15 +759,18 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) { datalen = (datalen > PM3_CMD_DATA_SIZE) ? PM3_CMD_DATA_SIZE : datalen; clearCommandBuffer(); + PrintAndLogEx(NORMAL, "Data: %s", sprint_hex(data, datalen)); SendCommandMIX(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); if (reply) { if (active_select) { PrintAndLogEx(NORMAL, "Active select wait for FeliCa."); - waitCmdFelica(1); + PacketResponseNG resp_IDm; + waitCmdFelica(1, &resp_IDm); } if (datalen > 0) { - waitCmdFelica(0); + PacketResponseNG resp_frame; + waitCmdFelica(0, &resp_frame); } } return PM3_SUCCESS; @@ -724,7 +783,6 @@ int readFelicaUid(bool verbose) { PacketResponseNG resp; if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { if (verbose) PrintAndLogEx(WARNING, "FeliCa card select failed"); - //SendCommandMIX(CMD_HF_FELICA_COMMAND, 0, 0, 0, NULL, 0); return PM3_ESOFT; } @@ -761,6 +819,7 @@ int readFelicaUid(bool verbose) { PrintAndLogEx(NORMAL, " - MRT %s", sprint_hex(card.mrt, sizeof(card.mrt))); PrintAndLogEx(NORMAL, "SERVICE CODE %s", sprint_hex(card.servicecode, sizeof(card.servicecode))); + set_last_known_card(card); break; } } diff --git a/include/mifare.h b/include/mifare.h index 23dbceb68..32427309d 100644 --- a/include/mifare.h +++ b/include/mifare.h @@ -169,6 +169,15 @@ typedef struct { uint8_t servicecode[2]; } PACKED felica_card_select_t; +typedef struct { + uint8_t sync[4]; + uint8_t length[1]; + uint8_t cmd_code[1]; + uint8_t IDm[8]; + uint8_t node_number[1]; + uint8_t node_key_versions[2]; +} PACKED felica_request_service_response_t; + typedef enum FELICA_COMMAND { FELICA_CONNECT = (1 << 0), FELICA_NO_DISCONNECT = (1 << 1), From 3f56116615bf3da583e4ce81dda63ea835e28306 Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Tue, 22 Oct 2019 17:03:54 +0200 Subject: [PATCH 06/10] Add felica request service all nodes command. Make style. --- armsrc/felica.c | 4 +- client/cmdhffelica.c | 107 ++++++++++++++++++++------------ client/cmdhffelica.h | 1 + client/luascripts/init_rdv4.lua | 2 +- include/mifare.h | 2 +- 5 files changed, 71 insertions(+), 45 deletions(-) diff --git a/armsrc/felica.c b/armsrc/felica.c index 83ff2bea3..cc9c2c9ed 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -420,8 +420,8 @@ bool WaitForFelicaReply(uint16_t maxbytes) { Process18092Byte(b); if (FelicaFrame.state == STATE_FULL) { felica_nexttransfertime = MAX(felica_nexttransfertime, - (GetCountSspClk() & 0xfffffff8) - (DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) / 16 + FELICA_FRAME_DELAY_TIME - ); + (GetCountSspClk() & 0xfffffff8) - (DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) / 16 + FELICA_FRAME_DELAY_TIME + ); LogTrace( FelicaFrame.framebytes, FelicaFrame.len, diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 81fb3e67d..bcca6f63a 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -26,7 +26,7 @@ static int CmdHelp(const char *Cmd); static felica_card_select_t last_known_card; -static void set_last_known_card(felica_card_select_t card){ +static void set_last_known_card(felica_card_select_t card) { last_known_card = card; } @@ -91,13 +91,16 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, " - When the specified Area or Service exists, the card returns Key Version."); PrintAndLogEx(NORMAL, " - When the specified Area or Service does not exist, the card returns FFFFh as Key Version."); PrintAndLogEx(NORMAL, "For Node Code List of a command packet, Area Code or Service Code of the target " - "of acquisition of Key Version shall be enumerated in Little Endian format." - "If Key Version of System is the target of acquisition, FFFFh shall be specified " - "in the command packet."); + "of acquisition of Key Version shall be enumerated in Little Endian format." + "If Key Version of System is the target of acquisition, FFFFh shall be specified " + "in the command packet."); PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] [-i] <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)>"); PrintAndLogEx(NORMAL, " -h this help"); PrintAndLogEx(NORMAL, " -i <0A 0B 0C ... hex> set custom IDm to use"); - PrintAndLogEx(NORMAL, "\nExample: hf felica rqservice 01 FFFF \n\n"); + PrintAndLogEx(NORMAL, "\nExamples: hf felica rqservice 01 FF FF"); + PrintAndLogEx(NORMAL, " hf felica rqservice 01 FF FF"); + PrintAndLogEx(NORMAL, " hf felica rqs -a FF FF"); + PrintAndLogEx(NORMAL, " hf felica rqs -i 01 10 09 10 c1 1b c4 07 01 FF FF \n\n"); return PM3_SUCCESS; } @@ -109,7 +112,7 @@ static bool waitCmdFelica(uint8_t iSelect, PacketResponseNG *resp) { if (WaitForResponseTimeout(CMD_ACK, resp, 2000)) { uint16_t len = iSelect ? (resp->oldarg[1] & 0xffff) : (resp->oldarg[0] & 0xffff); PrintAndLogEx(NORMAL, "Client Received %i octets", len); - if (!len || len < 2){ + if (!len || len < 2) { PrintAndLogEx(ERR, "Could not receive data correctly!"); } PrintAndLogEx(NORMAL, "%s", sprint_hex(resp->data.asBytes, len)); @@ -120,7 +123,7 @@ static bool waitCmdFelica(uint8_t iSelect, PacketResponseNG *resp) { } else { PrintAndLogEx(WARNING, "Timeout while waiting for reply."); } - return false; + return false; } /* @@ -150,7 +153,7 @@ static bool is_hex_input(const char *Cmd, int i) { } /** - * Add crc bytes to the end of the given data. + * Add crc bytes to the end of the given data and increments datalen. * @param datalen length of the data frame. * @param data frame on which the crc is calculated. * @param size of the data. @@ -189,7 +192,7 @@ static void get_cmd_data(const char *Cmd, uint16_t *datalen, uint8_t *data, char * @param number number of hex bytes. * @return number as hex value. */ -static uint8_t int_to_hex(uint16_t *number){ +static uint8_t int_to_hex(uint16_t *number) { uint32_t hex; char dataLengthChar[5]; sprintf(dataLengthChar, "%x", *number); @@ -203,14 +206,14 @@ static uint8_t int_to_hex(uint16_t *number){ * @param data frame in where the IDM is added. * @return true if IDm was added; */ -static bool add_last_IDm(uint8_t position, uint8_t *data){ - if(last_known_card.IDm[0] != 0 && last_known_card.IDm[1] != 0){ - for(int i = 0; i < 8; i++){ +static bool add_last_IDm(uint8_t position, uint8_t *data) { + if (last_known_card.IDm[0] != 0 && last_known_card.IDm[1] != 0) { + for (int i = 0; i < 8; i++) { uint16_t number = (uint16_t)last_known_card.IDm[i]; - data[position+i] = int_to_hex(&number); + data[position + i] = int_to_hex(&number); } return true; - }else{ + } else { return false; } } @@ -242,6 +245,32 @@ static int CmdHFFelicaDump(const char *Cmd) { return PM3_SUCCESS; }*/ +/** + * Sends a request service frame to the pm3. + */ +void send_request_service(uint8_t flags, uint16_t datalen, uint8_t *data) { + uint16_t numbits = 0; + clearCommandBuffer(); + PrintAndLogEx(NORMAL, "Send Service Request Frame: %s", sprint_hex(data, datalen)); + SendCommandMIX(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); + PacketResponseNG resp; + if (datalen > 0) { + if (!waitCmdFelica(0, &resp)) { + PrintAndLogEx(ERR, "\nGot no Response from card"); + return; + } + felica_request_service_response_t rqs_response; + memcpy(&rqs_response, (felica_request_service_response_t *)resp.data.asBytes, sizeof(felica_request_service_response_t)); + + if (rqs_response.IDm[0] != 0) { + PrintAndLogEx(SUCCESS, "\nGot Service Response:"); + PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rqs_response.IDm, sizeof(rqs_response.IDm))); + PrintAndLogEx(NORMAL, " -Node Number: %s", sprint_hex(rqs_response.node_number, sizeof(rqs_response.node_number))); + PrintAndLogEx(NORMAL, " -Node Key Version List: %s\n", sprint_hex(rqs_response.node_key_versions, sizeof(rqs_response.node_key_versions))); + } + } +} + /** * Command parser for rqservice. * @param Cmd input data of the user. @@ -252,23 +281,29 @@ static int CmdHFFelicaRequestService(const char *Cmd) { int i = 0; uint8_t data[PM3_CMD_DATA_SIZE]; bool custom_IDm = false; + bool all_nodes = false; uint16_t datalen = 0; uint8_t flags = 0; - uint16_t numbits = 0; + char buf[5] = ""; - datalen += 10; // length (1) + CMD (1) + IDm (8) + datalen += 10; // length (1) + CMD (1) + IDm(8) strip_cmds(Cmd); while (Cmd[i] != '\0') { if (Cmd[i] == '-') { switch (Cmd[i + 1]) { case 'H': case 'h': - return usage_hf_felica_raw(); + return usage_hf_felica_request_service(); case 'i': custom_IDm = true; + datalen -= 8; + break; + case 'a': + all_nodes = true; + datalen += 1; break; default: - return usage_hf_felica_raw(); + return usage_hf_felica_request_service(); } i += 2; } @@ -290,38 +325,28 @@ static int CmdHFFelicaRequestService(const char *Cmd) { flags |= FELICA_RAW; } datalen = (datalen > PM3_CMD_DATA_SIZE) ? PM3_CMD_DATA_SIZE : datalen; - if(!custom_IDm){ - if(!add_last_IDm(2, data)){ + if (!custom_IDm) { + if (!add_last_IDm(2, data)) { PrintAndLogEx(ERR, "No last known card! Use reader first or set a custom IDm!"); return PM3_EINVARG; - }else{ + } else { PrintAndLogEx(INFO, "Used last known IDm.", sprint_hex(data, datalen)); } } data[0] = int_to_hex(&datalen); data[1] = 0x02; // Request Command ID - add_crc_bytes(&datalen, data, sizeof(data)); - PrintAndLogEx(NORMAL, "Send Service Request Frame: %s", sprint_hex(data, datalen)); - clearCommandBuffer(); - SendCommandMIX(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); - PacketResponseNG resp; - if (custom_IDm) { - waitCmdFelica(1, &resp); - } - if (datalen > 0) { - if(!waitCmdFelica(0, &resp)){ - return PM3_ESOFT; + if (all_nodes) { + for (uint16_t y = 1; y < 32; y++) { + data[10] = int_to_hex(&y); + add_crc_bytes(&datalen, data, sizeof(data)); + send_request_service(flags, datalen, data); + datalen -= 2; // Remove CRC bytes before adding new ones } - felica_request_service_response_t rqs_response; - memcpy(&rqs_response, (felica_request_service_response_t *)resp.data.asBytes, sizeof(felica_request_service_response_t)); - - PrintAndLogEx(SUCCESS, "\nGot Service Response:"); - PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rqs_response.IDm, sizeof(rqs_response.IDm))); - PrintAndLogEx(NORMAL, " -Node Number: %s", sprint_hex(rqs_response.node_number, sizeof(rqs_response.node_number))); - PrintAndLogEx(NORMAL, " -Node Key Version List: %s\n", sprint_hex(rqs_response.node_key_versions, sizeof(rqs_response.node_key_versions))); - }else{ - return PM3_ESOFT; + } else { + add_crc_bytes(&datalen, data, sizeof(data)); + send_request_service(flags, datalen, data); } + return PM3_SUCCESS; } diff --git a/client/cmdhffelica.h b/client/cmdhffelica.h index 59032ee31..dd127cfaf 100644 --- a/client/cmdhffelica.h +++ b/client/cmdhffelica.h @@ -15,4 +15,5 @@ int CmdHFFelica(const char *Cmd); int readFelicaUid(bool verbose); +void send_request_service(uint8_t flags, uint16_t datalen, uint8_t *data); #endif diff --git a/client/luascripts/init_rdv4.lua b/client/luascripts/init_rdv4.lua index 4836cda77..d2dd03e24 100644 --- a/client/luascripts/init_rdv4.lua +++ b/client/luascripts/init_rdv4.lua @@ -88,7 +88,7 @@ function main(args) print('') core.console('hw status') print(dash) - + print('all done!') end diff --git a/include/mifare.h b/include/mifare.h index 32427309d..5461bd19f 100644 --- a/include/mifare.h +++ b/include/mifare.h @@ -170,7 +170,7 @@ typedef struct { } PACKED felica_card_select_t; typedef struct { - uint8_t sync[4]; + uint8_t sync[2]; uint8_t length[1]; uint8_t cmd_code[1]; uint8_t IDm[8]; From b6117e499695b9fe05cb1743c904b1184054a37d Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Tue, 22 Oct 2019 17:10:32 +0200 Subject: [PATCH 07/10] Add parameter a to usage text. --- client/cmdhffelica.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index bcca6f63a..1c276c629 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -91,13 +91,14 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, " - When the specified Area or Service exists, the card returns Key Version."); PrintAndLogEx(NORMAL, " - When the specified Area or Service does not exist, the card returns FFFFh as Key Version."); PrintAndLogEx(NORMAL, "For Node Code List of a command packet, Area Code or Service Code of the target " - "of acquisition of Key Version shall be enumerated in Little Endian format." + "of acquisition of Key Version shall be enumerated in Little Endian format. " "If Key Version of System is the target of acquisition, FFFFh shall be specified " "in the command packet."); PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] [-i] <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)>"); PrintAndLogEx(NORMAL, " -h this help"); PrintAndLogEx(NORMAL, " -i <0A 0B 0C ... hex> set custom IDm to use"); - PrintAndLogEx(NORMAL, "\nExamples: hf felica rqservice 01 FF FF"); + PrintAndLogEx(NORMAL, " -a auto node number mode - iterates through all possible node 1 < n < 32"); + PrintAndLogEx(NORMAL, "\nExamples: "); PrintAndLogEx(NORMAL, " hf felica rqservice 01 FF FF"); PrintAndLogEx(NORMAL, " hf felica rqs -a FF FF"); PrintAndLogEx(NORMAL, " hf felica rqs -i 01 10 09 10 c1 1b c4 07 01 FF FF \n\n"); From 22743831297b29eee59916a1ba8949210c33de6b Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Wed, 23 Oct 2019 09:21:43 +0200 Subject: [PATCH 08/10] Remove crc methode. Add CRC macro. Change help text for request service. --- armsrc/felica.c | 2 +- client/cmdhffelica.c | 52 ++++++++++---------------------------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/armsrc/felica.c b/armsrc/felica.c index cc9c2c9ed..09e2b582f 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -3,7 +3,7 @@ #include "BigBuf.h" #include "util.h" #include "protocols.h" -#include "crc16.h" // crc16 ccitt +#include "crc16.h" #include "fpgaloader.h" #include "string.h" #include "commonutil.h" diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 1c276c629..dfdbfe887 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -22,6 +22,7 @@ #include "ui.h" #include "mifare.h" // felica_card_select_t struct +#define AddCrc(data, len) compute_crc(CRC_FELICA, (data), (len), (data)+(len)+1, (data)+(len)) static int CmdHelp(const char *Cmd); static felica_card_select_t last_known_card; @@ -100,8 +101,8 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, " -a auto node number mode - iterates through all possible node 1 < n < 32"); PrintAndLogEx(NORMAL, "\nExamples: "); PrintAndLogEx(NORMAL, " hf felica rqservice 01 FF FF"); - PrintAndLogEx(NORMAL, " hf felica rqs -a FF FF"); - PrintAndLogEx(NORMAL, " hf felica rqs -i 01 10 09 10 c1 1b c4 07 01 FF FF \n\n"); + PrintAndLogEx(NORMAL, " hf felica rqservice -a FF FF"); + PrintAndLogEx(NORMAL, " hf felica rqservice -i 01 10 09 10 c1 1b c4 07 01 FF FF \n\n"); return PM3_SUCCESS; } @@ -153,24 +154,6 @@ static bool is_hex_input(const char *Cmd, int i) { return (Cmd[i] >= '0' && Cmd[i] <= '9') || (Cmd[i] >= 'a' && Cmd[i] <= 'f') || (Cmd[i] >= 'A' && Cmd[i] <= 'F') ? 1 : 0; } -/** - * Add crc bytes to the end of the given data and increments datalen. - * @param datalen length of the data frame. - * @param data frame on which the crc is calculated. - * @param size of the data. - * @return true if the crc was added. - */ -static bool add_crc_bytes(uint16_t *datalen, uint8_t *data, size_t dataSize) { - if (*datalen > 0 && *datalen < dataSize - 2) { - uint8_t b1, b2; - compute_crc(CRC_FELICA, data, *datalen, &b1, &b2); - data[(*datalen)++] = b2; - data[(*datalen)++] = b1; - return 1; - } - return 0; -} - /** * Extracts the hex data from the cmd and puts it into the data array. * @param Cmd input string of the user with the data @@ -219,17 +202,8 @@ static bool add_last_IDm(uint8_t position, uint8_t *data) { } } -/* -static int usage_hf_felica_dump(void) { - // TODO IMPLEMENT - PrintAndLogEx(NORMAL, "Usage: hf felica dump [-h] "); - PrintAndLogEx(NORMAL, " -h this help"); - return PM3_SUCCESS; -}*/ - static int CmdHFFelicaList(const char *Cmd) { - (void)Cmd; // Cmd is not used so far - //PrintAndLogEx(NORMAL, "Deprecated command, use 'hf list felica' instead"); + (void)Cmd; CmdTraceList("felica"); return PM3_SUCCESS; } @@ -239,13 +213,6 @@ static int CmdHFFelicaReader(const char *Cmd) { return readFelicaUid(verbose); } -/* -static int CmdHFFelicaDump(const char *Cmd) { - if (strlen(Cmd) < 1) return usage_hf_felica_dump(); - // TODO IMPLEMENT - return PM3_SUCCESS; -}*/ - /** * Sends a request service frame to the pm3. */ @@ -314,7 +281,7 @@ static int CmdHFFelicaRequestService(const char *Cmd) { buf[strlen(buf)] = Cmd[i]; i++; get_cmd_data(Cmd, &datalen, data, buf); - } else { + } else { i++; } } @@ -339,12 +306,14 @@ static int CmdHFFelicaRequestService(const char *Cmd) { if (all_nodes) { for (uint16_t y = 1; y < 32; y++) { data[10] = int_to_hex(&y); - add_crc_bytes(&datalen, data, sizeof(data)); + AddCrc(data, datalen); send_request_service(flags, datalen, data); datalen -= 2; // Remove CRC bytes before adding new ones } } else { - add_crc_bytes(&datalen, data, sizeof(data)); + PrintAndLogEx(INFO, "Datalen %i", datalen); + AddCrc(data, datalen); + datalen += 2; send_request_service(flags, datalen, data); } @@ -763,7 +732,8 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) { } if (crc) { - add_crc_bytes(&datalen, data, sizeof(data)); + AddCrc(data, datalen); + datalen += 2; } uint8_t flags = 0; From 21f0095029b80e9150cd6d9b6033e66298196075 Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Wed, 23 Oct 2019 09:42:45 +0200 Subject: [PATCH 09/10] Make Style. --- client/cmdhffelica.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index dfdbfe887..e7e410d4b 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -281,7 +281,7 @@ static int CmdHFFelicaRequestService(const char *Cmd) { buf[strlen(buf)] = Cmd[i]; i++; get_cmd_data(Cmd, &datalen, data, buf); - } else { + } else { i++; } } From bdbb4cb5c940fd0bf72775e0a41a03ded56fb32f Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Wed, 23 Oct 2019 13:25:52 +0200 Subject: [PATCH 10/10] Refactor request service parsing. Change Examples. --- client/cmdhffelica.c | 92 ++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index e7e410d4b..bad416212 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -19,7 +19,7 @@ #include "comms.h" #include "cmdtrace.h" #include "crc16.h" - +#include "util.h" #include "ui.h" #include "mifare.h" // felica_card_select_t struct #define AddCrc(data, len) compute_crc(CRC_FELICA, (data), (len), (data)+(len)+1, (data)+(len)) @@ -98,11 +98,11 @@ static int usage_hf_felica_request_service(void) { PrintAndLogEx(NORMAL, "\nUsage: hf felica rqservice [-h] [-i] <01 Number of Node hex> <0A 0B Node Code List hex (Little Endian)>"); PrintAndLogEx(NORMAL, " -h this help"); PrintAndLogEx(NORMAL, " -i <0A 0B 0C ... hex> set custom IDm to use"); - PrintAndLogEx(NORMAL, " -a auto node number mode - iterates through all possible node 1 < n < 32"); + PrintAndLogEx(NORMAL, " -a auto node number mode - iterates through all possible nodes 1 < n < 32"); PrintAndLogEx(NORMAL, "\nExamples: "); - PrintAndLogEx(NORMAL, " hf felica rqservice 01 FF FF"); - PrintAndLogEx(NORMAL, " hf felica rqservice -a FF FF"); - PrintAndLogEx(NORMAL, " hf felica rqservice -i 01 10 09 10 c1 1b c4 07 01 FF FF \n\n"); + PrintAndLogEx(NORMAL, " hf felica rqservice 01 FFFF"); + PrintAndLogEx(NORMAL, " hf felica rqservice -a FFFF"); + PrintAndLogEx(NORMAL, " hf felica rqservice -i 01100910c11bc407 01 FFFF \n\n"); return PM3_SUCCESS; } @@ -129,15 +129,7 @@ static bool waitCmdFelica(uint8_t iSelect, PacketResponseNG *resp) { } /* - * Parses line spacing and tabs. - * Returns 1 if the given char is a space or tab - */ -static int parse_cmd_parameter_separator(const char *Cmd, int i) { - return Cmd[i] == ' ' || Cmd[i] == '\t' ? 1 : 0; -} - -/* - * Counts and sets the number of commands. + * Counts and sets the number of parameters. */ static void strip_cmds(const char *Cmd) { while (*Cmd == ' ' || *Cmd == '\t') { @@ -145,34 +137,9 @@ static void strip_cmds(const char *Cmd) { } } -/** - * Checks if a char is a hex value. - * @param Cmd - * @return one if it is a valid hex char. Zero if not a valid hex char. - */ -static bool is_hex_input(const char *Cmd, int i) { - return (Cmd[i] >= '0' && Cmd[i] <= '9') || (Cmd[i] >= 'a' && Cmd[i] <= 'f') || (Cmd[i] >= 'A' && Cmd[i] <= 'F') ? 1 : 0; -} - -/** - * Extracts the hex data from the cmd and puts it into the data array. - * @param Cmd input string of the user with the data - * @param datalen length of the data frame. - * @param data the array in which the data gets stored. - * @param buf buffer with hex data. - */ -static void get_cmd_data(const char *Cmd, uint16_t *datalen, uint8_t *data, char buf[]) { - uint32_t hex; - if (strlen(buf) >= 2) { - sscanf(buf, "%x", &hex); - data[*datalen] = (uint8_t)(hex & 0xff); - (*datalen)++; - *buf = 0; - } -} - /** * Converts integer value to equivalent hex value. + * Examples: 1 = 1, 11 = B * @param number number of hex bytes. * @return number as hex value. */ @@ -250,11 +217,9 @@ static int CmdHFFelicaRequestService(const char *Cmd) { uint8_t data[PM3_CMD_DATA_SIZE]; bool custom_IDm = false; bool all_nodes = false; - uint16_t datalen = 0; + uint16_t datalen = 13; // length (1) + CMD (1) + IDm(8) + Node Number (1) + Node Code List (2) uint8_t flags = 0; - - char buf[5] = ""; - datalen += 10; // length (1) + CMD (1) + IDm(8) + uint8_t paramCount = 0; strip_cmds(Cmd); while (Cmd[i] != '\0') { if (Cmd[i] == '-') { @@ -263,28 +228,45 @@ static int CmdHFFelicaRequestService(const char *Cmd) { case 'h': return usage_hf_felica_request_service(); case 'i': + paramCount++; custom_IDm = true; - datalen -= 8; + if (param_getlength(Cmd, paramCount) == 16) { + param_gethex(Cmd, paramCount++, data + 2, 16); + } else { + PrintAndLogEx(ERR, "Incorrect IDm length! IDm must be 8-Byte."); + return PM3_EINVARG; + } + i += 8; break; case 'a': + paramCount++; all_nodes = true; - datalen += 1; break; default: return usage_hf_felica_request_service(); } i += 2; } - i = i + parse_cmd_parameter_separator(Cmd, i); - if (is_hex_input(Cmd, i)) { - buf[strlen(buf) + 1] = 0; - buf[strlen(buf)] = Cmd[i]; - i++; - get_cmd_data(Cmd, &datalen, data, buf); + i++; + } + if (!all_nodes) { + // Node Number + if (param_getlength(Cmd, paramCount) == 2) { + param_gethex(Cmd, paramCount++, data + 10, 2); } else { - i++; + PrintAndLogEx(ERR, "Incorrect Node number length!"); + return PM3_EINVARG; } } + + // Node Code List + if (param_getlength(Cmd, paramCount) == 4) { + param_gethex(Cmd, paramCount++, data + 11, 4); + } else { + PrintAndLogEx(ERR, "Incorrect Node Code List length!"); + return PM3_EINVARG; + } + flags |= FELICA_APPEND_CRC; if (custom_IDm) { flags |= FELICA_NO_SELECT; @@ -302,16 +284,16 @@ static int CmdHFFelicaRequestService(const char *Cmd) { } } data[0] = int_to_hex(&datalen); - data[1] = 0x02; // Request Command ID + data[1] = 0x02; // Service Request Command ID if (all_nodes) { for (uint16_t y = 1; y < 32; y++) { data[10] = int_to_hex(&y); AddCrc(data, datalen); + datalen += 2; send_request_service(flags, datalen, data); datalen -= 2; // Remove CRC bytes before adding new ones } } else { - PrintAndLogEx(INFO, "Datalen %i", datalen); AddCrc(data, datalen); datalen += 2; send_request_service(flags, datalen, data);