Make style. Change DBG_DEBUG. Refactor CRC calculation

This commit is contained in:
Thomas Sutter 2019-10-21 09:59:15 +02:00
commit e7f7810d22
3 changed files with 96 additions and 68 deletions

View file

@ -229,21 +229,21 @@ static uint8_t felica_select_card(felica_card_select_t *card) {
// timed-out // timed-out
if (len == 0) { if (len == 0) {
if (DBGLEVEL > 3) if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("Error: Time out card selection!"); Dbprintf("Error: Time out card selection!");
return 1; return 1;
} }
// wrong answer // wrong answer
if (FelicaFrame.framebytes[3] != FELICA_POLL_ACK) { if (FelicaFrame.framebytes[3] != FELICA_POLL_ACK) {
if (DBGLEVEL > 3) if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("Error: Wrong answer selecting card!"); Dbprintf("Error: Wrong answer selecting card!");
return 2; return 2;
} }
// VALIDATE CRC residue is 0, hence if crc is a value it failed. // 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 (!check_crc(CRC_FELICA, FelicaFrame.framebytes + 2, FelicaFrame.len - 2)) {
if (DBGLEVEL > 3) { if (DBGLEVEL >= DBG_DEBUG) {
Dbprintf("Error: CRC check failed!"); Dbprintf("Error: CRC check failed!");
Dbprintf("CRC check was done on Frame: "); Dbprintf("CRC check was done on Frame: ");
Dbhexdump(FelicaFrame.len - 2, FelicaFrame.framebytes + 2, 0); 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; return 3;
} }
if (DBGLEVEL > 3) if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("Card selection successful!"); Dbprintf("Card selection successful!");
// copy UID // copy UID
// idm 8 // 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->uid, card->IDm + 2, 6);
memcpy(card->iccode, card->PMm, 2); memcpy(card->iccode, card->PMm, 2);
memcpy(card->mrt, card->PMm + 2, 6); memcpy(card->mrt, card->PMm + 2, 6);
if (DBGLEVEL > 3) { if (DBGLEVEL >= DBG_DEBUG) {
Dbprintf("Received Frame: "); Dbprintf("Received Frame: ");
Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0); 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 // sending data with sync bytes
c = 0; c = 0;
if (DBGLEVEL > 3) { if (DBGLEVEL >= DBG_DEBUG) {
Dbprintf("Sending frame:"); Dbprintf("Sending frame:");
Dbhexdump(len, frame, 0); 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 // stop when button is pressed
// or return TRUE when command is captured // or return TRUE when command is captured
bool WaitForFelicaReply(uint16_t maxbytes) { bool WaitForFelicaReply(uint16_t maxbytes) {
if (DBGLEVEL > 3) if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("WaitForFelicaReply Start"); Dbprintf("WaitForFelicaReply Start");
uint32_t c = 0; uint32_t c = 0;
// power, no modulation // power, no modulation
@ -413,7 +413,7 @@ bool WaitForFelicaReply(uint16_t maxbytes) {
// clear RXRDY: // clear RXRDY:
uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
uint32_t timeout = iso18092_get_timeout(); uint32_t timeout = iso18092_get_timeout();
if (DBGLEVEL > 3) if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("timeout set: %i", timeout); Dbprintf("timeout set: %i", timeout);
for (;;) { for (;;) {
WDT_HIT(); WDT_HIT();
@ -435,10 +435,10 @@ bool WaitForFelicaReply(uint16_t maxbytes) {
NULL, NULL,
false false
); );
if (DBGLEVEL > 3) Dbprintf("All bytes received! STATE_FULL"); if (DBGLEVEL >= DBG_DEBUG) Dbprintf("All bytes received! STATE_FULL");
return true; return true;
} else if (c++ > timeout && FelicaFrame.state == STATE_UNSYNCD) { } 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; return false;
} }
} }
@ -448,7 +448,7 @@ bool WaitForFelicaReply(uint16_t maxbytes) {
// Set up FeliCa communication (similar to iso14443a_setup) // Set up FeliCa communication (similar to iso14443a_setup)
// field is setup for "Sending as Reader" // field is setup for "Sending as Reader"
static void iso18092_setup(uint8_t fpga_minor_mode) { 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(); LEDsoff();
FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
@ -501,7 +501,7 @@ void felica_reset_frame_mode() {
// arg1 len of commandbytes // arg1 len of commandbytes
// d.asBytes command bytes to send // d.asBytes command bytes to send
void felica_sendraw(PacketCommandNG *c) { 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]; felica_command_t param = c->oldarg[0];
size_t len = c->oldarg[1] & 0xffff; size_t len = c->oldarg[1] & 0xffff;
@ -511,7 +511,7 @@ void felica_sendraw(PacketCommandNG *c) {
felica_card_select_t card; felica_card_select_t card;
if ((param & FELICA_CONNECT)) if ((param & FELICA_CONNECT))
if (DBGLEVEL > 3) Dbprintf("Clear trace"); if (DBGLEVEL >= DBG_DEBUG) Dbprintf("Clear trace");
clear_trace(); clear_trace();
set_tracing(true); set_tracing(true);
@ -530,7 +530,7 @@ void felica_sendraw(PacketCommandNG *c) {
} }
} }
} else { } else {
if (DBGLEVEL > 3) Dbprintf("No card selection"); if (DBGLEVEL >= DBG_DEBUG) Dbprintf("No card selection");
} }
if ((param & FELICA_RAW)) { if ((param & FELICA_RAW)) {
@ -551,14 +551,14 @@ void felica_sendraw(PacketCommandNG *c) {
AddCrc(buf, len); AddCrc(buf, len);
} }
} }
if (DBGLEVEL > 3) { if (DBGLEVEL >= DBG_DEBUG) {
Dbprintf("Transmit Frame (no CRC shown):"); Dbprintf("Transmit Frame (no CRC shown):");
Dbhexdump(len, buf, 0); Dbhexdump(len, buf, 0);
Dbprintf("Buffer Length: %i", buf[2] + 4); Dbprintf("Buffer Length: %i", buf[2] + 4);
}; };
TransmitFor18092_AsReader(buf, buf[2] + 4, NULL, 1, 0); TransmitFor18092_AsReader(buf, buf[2] + 4, NULL, 1, 0);
arg0 = WaitForFelicaReply(1024); arg0 = WaitForFelicaReply(1024);
if (DBGLEVEL > 3) { if (DBGLEVEL >= DBG_DEBUG) {
Dbprintf("Received Frame Code: %d", arg0); Dbprintf("Received Frame Code: %d", arg0);
Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0); Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0);
}; };
@ -571,7 +571,7 @@ void felica_sendraw(PacketCommandNG *c) {
if ((param & FELICA_NO_DISCONNECT)) { if ((param & FELICA_NO_DISCONNECT)) {
Dbprintf("Disconnect"); Dbprintf("Disconnect");
} }
if (DBGLEVEL > 3) if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("FeliCa_sendraw Exit"); Dbprintf("FeliCa_sendraw Exit");
felica_reset_frame_mode(); felica_reset_frame_mode();
return; return;

View file

@ -96,7 +96,7 @@ int CmdHFSearch(const char *Cmd) {
return PM3_SUCCESS; return PM3_SUCCESS;
} }
} }
PROMPT_CLEARLINE; PROMPT_CLEARLINE;
PrintAndLogEx(INPLACE, "Searching for ISO15693 tag..."); PrintAndLogEx(INPLACE, "Searching for ISO15693 tag...");
if (IfPm3Iso15693()) { if (IfPm3Iso15693()) {

View file

@ -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, "\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 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, " - 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, " -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"); PrintAndLogEx(NORMAL, "\nExample: hf felica rqservice 01100910c11bc407 01 FFFF 2837\n\n");
return PM3_SUCCESS; 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. * Parses line spacing and tabs.
* Returns 1 if the given char is a space or tab * 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; return Cmd[i] == ' ' || Cmd[i] == '\t' ? 1 : 0;
} }
/* /*
* Counts and sets the number of commands. * 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)); PrintAndLogEx(NORMAL, "CMD count: %i", strlen(Cmd));
while (*Cmd == ' ' || *Cmd == '\t'){ while (*Cmd == ' ' || *Cmd == '\t') {
PrintAndLogEx(NORMAL, "CMD: %s", Cmd); PrintAndLogEx(NORMAL, "CMD: %s", Cmd);
Cmd++; Cmd++;
} }
@ -117,15 +137,36 @@ static void strip_cmds(const char *Cmd){
* @param Cmd * @param Cmd
* @return one if it is a valid hex char. Zero if not a valid hex char. * @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; 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.
* @param Extracts the data from the cmd and puts it into the data array. * @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; uint32_t temp;
if (strlen(buf) >= 2) { if (strlen(buf) >= 2) {
sscanf(buf, "%x", &temp); sscanf(buf, "%x", &temp);
@ -161,14 +202,6 @@ static int CmdHFFelicaDump(const char *Cmd) {
return PM3_SUCCESS; return PM3_SUCCESS;
}*/ }*/
/**
* Sends a request service frame
* @return
*/
static int request_service() {
return PM3_SUCCESS;
}
/** /**
* Command parser for rqservice. * Command parser for rqservice.
* @param Cmd input data of the user. * @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(); if (strlen(Cmd) < 2) return usage_hf_felica_request_service();
int i = 0; int i = 0;
uint8_t data[PM3_CMD_DATA_SIZE]; uint8_t data[PM3_CMD_DATA_SIZE];
bool crc = false; bool custom_IDm = false;
bool length = false;
uint16_t datalen = 0; uint16_t datalen = 0;
uint16_t numbits = 0;
uint8_t flags = 0;
char buf[5] = ""; char buf[5] = "";
strip_cmds(Cmd); strip_cmds(Cmd);
@ -192,11 +226,8 @@ static int CmdHFFelicaRequestService(const char *Cmd) {
case 'H': case 'H':
case 'h': case 'h':
return usage_hf_felica_raw(); return usage_hf_felica_raw();
case 'c': case 'i':
crc = true; custom_IDm = true;
break;
case 'l':
length = true;
break; break;
default: default:
return usage_hf_felica_raw(); return usage_hf_felica_raw();
@ -206,19 +237,33 @@ static int CmdHFFelicaRequestService(const char *Cmd) {
PrintAndLogEx(NORMAL, "i after single params = %i: ", i); PrintAndLogEx(NORMAL, "i after single params = %i: ", i);
i = i + parse_cmd_parameter_separator(Cmd, i); i = i + parse_cmd_parameter_separator(Cmd, i);
PrintAndLogEx(NORMAL, "i after cnd separator: %i", 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) + 1] = 0;
buf[strlen(buf)] = Cmd[i]; buf[strlen(buf)] = Cmd[i];
i++; i++;
PrintAndLogEx(NORMAL, "i after is hex input: %i", i); PrintAndLogEx(NORMAL, "i after is hex input: %i", i);
get_cmd_data(Cmd, i, datalen, data, buf); get_cmd_data(Cmd, datalen, data, buf);
} else {
}else { i++;
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(); 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; return PM3_SUCCESS;
} }
@ -557,21 +602,7 @@ static int CmdHFFelicaDumpLite(const char *Cmd) {
return PM3_SUCCESS; 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) { static int CmdHFFelicaCmdRaw(const char *Cmd) {
bool reply = 1; bool reply = 1;
@ -649,11 +680,8 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) {
return PM3_EINVARG; return PM3_EINVARG;
} }
if (crc && datalen > 0 && datalen < sizeof(data) - 2) { if (crc) {
uint8_t b1, b2; add_crc_bytes(&datalen, data, sizeof(data));
compute_crc(CRC_FELICA, data, datalen, &b1, &b2);
data[datalen++] = b2;
data[datalen++] = b1;
} }
uint8_t flags = 0; uint8_t flags = 0;