#include "cmdparser.h"
#include "cmddata.h" //for g_debugMode, demodbuff cmds
#include "lfdemod.h" // for HIDdemodFSK
-#include "parity.h" // for parity
-#include "util.h" // for param_get8,32
-
-static int CmdHelp(const char *Cmd);
+#include "hidcardformats.h"
+#include "hidcardformatutils.h"
+#include "util.h" // for param_get8,32,64
/**
- * Packs a "short" (<38-bit) HID ID from component parts.
- *
- * This only works with 26, 34, 35 and 37 bit card IDs.
- *
- * NOTE: Parity calculation is only supported on 26-bit tags. Other card lengths
- * may have invalid parity.
- *
- * Returns false on invalid inputs.
- */
-bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const short_hid_info *info) {
- uint32_t high = 0, low = 0;
-
- switch (info->fmtLen) {
- case 26: // HID H10301
- low |= (info->cardnum & 0xffff) << 1;
- low |= (info->fc & 0xff) << 17;
-
- if (info->parityValid) {
- // Calculate parity
- low |= oddparity32((low >> 1) & 0xfff) & 1;
- low |= (evenparity32((low >> 13) & 0xfff) & 1) << 25;
- }
- break;
-
- case 34:
- low |= (info->cardnum & 0xffff) << 1;
- low |= (info->fc & 0x7fff) << 17;
- high |= (info->fc & 0x8000) >> 15;
- // TODO: Calculate parity
- break;
-
- case 35:
- low |= (info->cardnum & 0xfffff) << 1;
- low |= (info->fc & 0x7ff) << 21;
- high |= (info->fc & 0x800) >> 11;
- // TODO: Calculate parity
- break;
-
- case 37:
- low |= (info->cardnum & 0x7ffff) << 1;
- low |= (info->fc & 0xfff) << 20;
- high |= (info->fc & 0xf000) >> 12;
- // TODO: Calculate parity
- break;
-
- default:
- // Invalid / unsupported length
- return false;
- }
-
- // Set the highest bit
- if (info->fmtLen != 37) {
- // Bit 37 is always set
- high |= 0x20;
-
- // Set the bit corresponding to the length.
- if (info->fmtLen < 32) {
- low |= 1 << info->fmtLen;
- } else {
- high |= 1 << (info->fmtLen - 32);
- }
- }
-
- // Return result only if successful.
- *hi = high;
- *lo = low;
- return true;
-}
-
-
-/**
- * Unpacks a "short" (<38-bit) HID ID into its component parts.
- *
- * This only works with 26, 34, 35 and 37 bit card IDs.
- *
- * NOTE: Parity checking is only supported on 26-bit tags.
- *
- * Returns false on invalid inputs.
- */
-bool unpack_short_hid(short_hid_info *out, uint32_t hi, uint32_t lo) {
- memset(out, 0, sizeof(short_hid_info));
-
- if (((hi >> 5) & 1) == 1) {
- // if bit 38 is set then < 37 bit format is used
- uint32_t lo2 = 0;
- // get bits 21-37 to check for format len bit
- lo2 = (((hi & 31) << 12) | (lo >> 20));
- uint8_t idx3 = 1;
- // find last bit set to 1 (format len bit)
- while (lo2 > 1) {
- lo2 = lo2 >> 1;
- idx3++;
- }
-
- out->fmtLen = idx3 + 19;
-
- switch (out->fmtLen) {
- case 26: // HID H10301
- out->cardnum = (lo >> 1) & 0xFFFF;
- out->fc = (lo >> 17) & 0xFF;
-
- if (g_debugMode) {
- PrintAndLog("oddparity : input=%x, calculated=%d, provided=%d",
- (lo >> 1) & 0xFFF, oddparity32((lo >> 1) & 0xFFF), lo & 1);
- PrintAndLog("evenparity: input=%x, calculated=%d, provided=%d",
- (lo >> 13) & 0xFFF, evenparity32((lo >> 13) & 0xFFF) & 1, (lo >> 25) & 1);
- }
-
- out->parityValid =
- (oddparity32((lo >> 1) & 0xFFF) == (lo & 1)) &&
- ((evenparity32((lo >> 13) & 0xFFF) & 1) == ((lo >> 25) & 1));
- break;
-
- case 34:
- out->cardnum = (lo >> 1) & 0xFFFF;
- out->fc = ((hi & 1) << 15) | (lo >> 17);
- // TODO: Calculate parity
- break;
-
- case 35:
- out->cardnum = (lo >> 1) & 0xFFFFF;
- out->fc = ((hi & 1) << 11) | (lo >> 21);
- // TODO: Calculate parity
- break;
-
- default:
- return false;
- }
- } else {
- // If bit 38 is not set, then 37 bit format is used
- out->fmtLen = 37;
- out->cardnum = (lo >> 1) & 0x7FFFF;
- out->fc = ((hi & 0xF) << 12) | (lo >> 20);
- // TODO: Calculate parity
- }
- return true;
-}
-
-
-/**
- * Converts a hex string to component "hi" and "lo" 32-bit integers, one nibble
+ * Converts a hex string to component "hi2", "hi" and "lo" 32-bit integers, one nibble
* at a time.
*
* Returns the number of nibbles (4 bits) entered.
*/
-int hexstring_to_int64(/* out */ uint32_t* hi, /* out */ uint32_t* lo, const char* str) {
+int hexstring_to_int96(/* out */ uint32_t* hi2,/* out */ uint32_t* hi, /* out */ uint32_t* lo, const char* str) {
// TODO: Replace this with param_gethex when it supports arbitrary length
// inputs.
int n = 0, i = 0;
while (sscanf(&str[i++], "%1x", &n ) == 1) {
+ *hi2 = (*hi2 << 4) | (*hi >> 28);
*hi = (*hi << 4) | (*lo >> 28);
*lo = (*lo << 4) | (n & 0xf);
}
if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
return 0;
}
- if (hi2 != 0){ //extra large HID tags
- PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
- (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF);
- }
- else { //standard HID tags <38 bits
- short_hid_info card_info;
- bool ret = unpack_short_hid(&card_info, (uint32_t)hi, (uint32_t)lo);
- PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
- (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
- card_info.fmtLen, card_info.fc, card_info.cardnum);
-
- if (card_info.fmtLen == 26) {
- PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
- }
+
+ if (hi2 != 0)
+ PrintAndLog("HID Prox TAG ID: %x%08x%08x",
+ (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo
+ );
+ else
+ PrintAndLog("HID Prox TAG ID: %x%08x",
+ (unsigned int) hi, (unsigned int) lo
+ );
+
+ hidproxmessage_t packed = initialize_proxmessage_object(hi2, hi, lo);
+ bool ret = HIDTryUnpack(&packed, false);
- if (!ret) {
- PrintAndLog("Invalid or unsupported tag length.");
- }
+ if (!ret) {
+ PrintAndLog("Invalid or unsupported tag length.");
}
setDemodBuf(BitStream,BitLen,idx);
setClockGrid(50, waveIdx + (idx*50));
int CmdHIDSim(const char *Cmd)
{
- uint32_t hi = 0, lo = 0;
- hexstring_to_int64(&hi, &lo, Cmd);
- if (hi >= 0x40) {
+ uint32_t hi2 = 0, hi = 0, lo = 0;
+ hexstring_to_int96(&hi2, &hi, &lo, Cmd);
+ if (hi >= 0x40 || hi2 != 0) {
PrintAndLog("This looks like a long tag ID. Use 'lf simfsk' for long tags. Aborting!");
return 0;
}
{
unsigned int hi2 = 0, hi = 0, lo = 0;
UsbCommand c;
-
- if (strchr(Cmd,'l') != 0) {
- int n = 0, i = 0;
-
- while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
- hi2 = (hi2 << 4) | (hi >> 28);
- hi = (hi << 4) | (lo >> 28);
- lo = (lo << 4) | (n & 0xf);
- }
-
+ hexstring_to_int96(&hi2, &hi, &lo, Cmd);
+
+ if (hi >= 0x40 || hi2 != 0) {
PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
-
c.d.asBytes[0] = 1;
- }
- else {
- hexstring_to_int64(&hi, &lo, Cmd);
- if (hi >= 0x40) {
- PrintAndLog("This looks like a long tag ID. Aborting!");
- return 0;
- }
-
+ } else {
PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
-
- hi2 = 0;
c.d.asBytes[0] = 0;
}
c.cmd = CMD_HID_CLONE_TAG;
- c.arg[0] = hi2;
+ c.arg[0] = (hi2 & 0x000FFFFF);
c.arg[1] = hi;
c.arg[2] = lo;
return 0;
}
-
-int CmdHIDPack(const char *Cmd) {
- uint32_t hi = 0, lo = 0;
- short_hid_info card_info;
-
+int CmdHIDDecode(const char *Cmd){
if (strlen(Cmd)<3) {
- PrintAndLog("Usage: lf hid pack <length> <facility code (decimal)> <card number (decimal)>");
- PrintAndLog(" sample: lf hid pack 26 123 4567");
+ PrintAndLog("Usage: lf hid decode <id> {p}");
+ PrintAndLog(" (optional) p: Ignore invalid parity");
+ PrintAndLog(" sample: lf hid decode 2006f623ae");
return 0;
}
- card_info.fmtLen = param_get8(Cmd, 0);
- card_info.fc = param_get32ex(Cmd, 1, 0, 10);
- card_info.cardnum = param_get32ex(Cmd, 2, 0, 10);
- card_info.parityValid = true;
+ uint32_t top = 0, mid = 0, bot = 0;
+ bool ignoreParity = false;
+ hexstring_to_int96(&top, &mid, &bot, Cmd);
+ hidproxmessage_t packed = initialize_proxmessage_object(top, mid, bot);
+
+ char opt = param_getchar(Cmd, 1);
+ if (opt == 'p') ignoreParity = true;
- // TODO
- if (card_info.fmtLen != 26) {
- PrintAndLog("Warning: Parity bits are only calculated for 26 bit IDs -- this may be invalid!");
+ HIDTryUnpack(&packed, ignoreParity);
+ return 0;
+}
+int CmdHIDEncode(const char *Cmd) {
+ if (strlen(Cmd) == 0) {
+ PrintAndLog("Usage: lf hid encode <format> <facility code (decimal)> <card number (decimal)> [issue level (decimal)]");
+ PrintAndLog(" sample: lf hid encode H10301 123 4567");
+ return 0;
}
- bool ret = pack_short_hid(&hi, &lo, &card_info);
+ int formatIndex = -1;
+ if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
+ HIDListFormats();
+ return 0;
+ } else {
+ char format[16];
+ memset(format, 0, sizeof(format));
+ param_getstr(Cmd, 0, format, sizeof(format));
+ formatIndex = HIDFindCardFormat(format);
+ if (formatIndex == -1) {
+ HIDListFormats();
+ return 0;
+ }
+ }
- if (ret) {
- PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
- (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
- card_info.fmtLen, card_info.fc, card_info.cardnum);
+ hidproxcard_t card;
+ memset(&card, 0, sizeof(hidproxcard_t));
+ card.FacilityCode = param_get32ex(Cmd, 1, 0, 10);
+ card.CardNumber = param_get64ex(Cmd, 2, 0, 10);
+ card.IssueLevel = param_get32ex(Cmd, 3, 0, 10);
+ card.ParitySupported = true; // Try to encode parity if supported.
+
+ hidproxmessage_t packed;
+ memset(&packed, 0, sizeof(hidproxmessage_t));
+ if (HIDPack(formatIndex, &card, &packed)){
+ if (packed.top != 0) {
+ PrintAndLog("HID Prox TAG ID: %x%08x%08x",
+ (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot);
+ } else {
+ PrintAndLog("HID Prox TAG ID: %x%08x",
+ (unsigned int)packed.mid, (unsigned int)packed.bot);
+ }
} else {
- PrintAndLog("Invalid or unsupported tag length.");
+ PrintAndLog("The provided data could not be encoded with the selected format.");
}
return 0;
}
-
-int CmdHIDUnpack(const char *Cmd)
-{
- uint32_t hi = 0, lo = 0;
- if (strlen(Cmd)<1) {
- PrintAndLog("Usage: lf hid unpack <ID>");
- PrintAndLog(" sample: lf hid unpack 2006f623ae");
+int CmdHIDWrite(const char *Cmd) {
+ if (strlen(Cmd) == 0) {
+ PrintAndLog("Usage: lf hid write <format> <facility code (decimal)> <card number (decimal)> [issue level (decimal)]");
+ PrintAndLog(" sample: lf hid write H10301 123 4567");
return 0;
}
- hexstring_to_int64(&hi, &lo, Cmd);
- if (hi >= 0x40) {
- PrintAndLog("This looks like a long tag ID. Aborting!");
+ int formatIndex = -1;
+ if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
+ HIDListFormats();
return 0;
+ } else {
+ char format[16];
+ memset(format, 0, sizeof(format));
+ param_getstr(Cmd, 0, format, sizeof(format));
+ formatIndex = HIDFindCardFormat(format);
+ if (formatIndex == -1) {
+ HIDListFormats();
+ return 0;
+ }
}
- short_hid_info card_info;
- bool ret = unpack_short_hid(&card_info, hi, lo);
-
- PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
- (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
- card_info.fmtLen, card_info.fc, card_info.cardnum);
+ hidproxcard_t card;
+ memset(&card, 0, sizeof(hidproxcard_t));
+ card.FacilityCode = param_get32ex(Cmd, 1, 0, 10);
+ card.CardNumber = param_get64ex(Cmd, 2, 0, 10);
+ card.IssueLevel = param_get32ex(Cmd, 3, 0, 10);
+ card.ParitySupported = true; // Try to encode parity if supported.
+
+ hidproxmessage_t packed;
+ memset(&packed, 0, sizeof(hidproxmessage_t));
+ if (HIDPack(formatIndex, &card, &packed)){
+ UsbCommand c;
+ if (packed.top != 0) {
+ PrintAndLog("HID Prox TAG ID: %x%08x%08x",
+ (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot);
+ c.d.asBytes[0] = 1;
+ } else {
+ PrintAndLog("HID Prox TAG ID: %x%08x",
+ (unsigned int)packed.mid, (unsigned int)packed.bot);
+ c.d.asBytes[0] = 0;
+ }
- if (card_info.fmtLen == 26) {
- PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
- }
+ c.cmd = CMD_HID_CLONE_TAG;
+ c.arg[0] = (packed.top & 0x000FFFFF);
+ c.arg[1] = packed.mid;
+ c.arg[2] = packed.bot;
+ SendCommand(&c);
- if (!ret) {
- PrintAndLog("Invalid or unsupported tag length.");
+ } else {
+ PrintAndLog("The provided data could not be encoded with the selected format.");
}
return 0;
}
-
+static int CmdHelp(const char *Cmd); // define this now so the below won't error out.
static command_t CommandTable[] =
{
{"help", CmdHelp, 1, "This help"},
{"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
{"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
{"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
- {"clone", CmdHIDClone, 0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"},
- {"pack", CmdHIDPack, 1, "<len> <fc> <num> -- packs a <38 bit (short) HID ID from its length, facility code and card number"},
- {"unpack", CmdHIDUnpack, 1, "<ID> -- unpacks a <38 bit (short) HID ID to its length, facility code and card number"},
+ {"clone", CmdHIDClone, 0, "<ID> -- Clone HID to T55x7 (tag must be in antenna)"},
+ {"decode", CmdHIDDecode, 1, "<ID> -- Try to decode an HID tag and show its contents"},
+ {"encode", CmdHIDEncode, 1, "<format> <fc> <num> -- Encode an HID ID with the specified format, facility code and card number"},
+ {"write", CmdHIDWrite, 0, "<format> <fc> <num> -- Encode and write to a T55x7 tag (tag must be in antenna)"},
{NULL, NULL, 0, NULL}
};