Implements parity checks/calculation for 26-bit H10301 cards.

Adds links to useful resources for HID Prox cards.

Adds an explicit warning about the lack of parities on non-26-bit cards.

Changes all the examples to use 26-bit IDs that have a parity bit set.
This commit is contained in:
Michael Farrell 2018-07-07 09:46:22 +10:00
commit 561cec798e
2 changed files with 79 additions and 14 deletions

View file

@ -6,6 +6,16 @@
// the license. // the license.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Low frequency HID commands (known) // Low frequency HID commands (known)
//
// Useful resources:
// RF interface, programming a T55x7 clone, 26-bit HID H10301 encoding:
// http://www.proxmark.org/files/Documents/125%20kHz%20-%20HID/HID_format_example.pdf
//
// "Understanding Card Data Formats"
// https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
//
// "What Format Do You Need?"
// https://www.hidglobal.com/sites/default/files/resource_files/hid-prox-br-en.pdf
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include "cmdlfhid.h" #include "cmdlfhid.h"
@ -18,6 +28,7 @@
#include "cmdparser.h" #include "cmdparser.h"
#include "cmddata.h" //for g_debugMode, demodbuff cmds #include "cmddata.h" //for g_debugMode, demodbuff cmds
#include "lfdemod.h" // for HIDdemodFSK #include "lfdemod.h" // for HIDdemodFSK
#include "parity.h" // for parity
#include "util.h" // for param_get8,32 #include "util.h" // for param_get8,32
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
@ -28,33 +39,45 @@ static int CmdHelp(const char *Cmd);
* *
* This only works with 26, 34, 35 and 37 bit card IDs. * 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. * Returns false on invalid inputs.
*/ */
bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const short_hid_info *info) { bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const short_hid_info *info) {
uint32_t high = 0, low = 0; uint32_t high = 0, low = 0;
switch (info->fmtLen) { switch (info->fmtLen) {
case 26: case 26: // HID H10301
low |= (info->cardnum & 0xffff) << 1; low |= (info->cardnum & 0xffff) << 1;
low |= (info->fc & 0xff) << 17; low |= (info->fc & 0xff) << 17;
if (info->parityValid) {
// Calculate parity
low |= oddparity32((low >> 1) & 0xfff) & 1;
low |= (evenparity32((low >> 13) & 0xfff) & 1) << 25;
}
break; break;
case 34: case 34:
low |= (info->cardnum & 0xffff) << 1; low |= (info->cardnum & 0xffff) << 1;
low |= (info->fc & 0x7fff) << 17; low |= (info->fc & 0x7fff) << 17;
high |= (info->fc & 0x8000) >> 15; high |= (info->fc & 0x8000) >> 15;
// TODO: Calculate parity
break; break;
case 35: case 35:
low |= (info->cardnum & 0xfffff) << 1; low |= (info->cardnum & 0xfffff) << 1;
low |= (info->fc & 0x7ff) << 21; low |= (info->fc & 0x7ff) << 21;
high |= (info->fc & 0x800) >> 11; high |= (info->fc & 0x800) >> 11;
// TODO: Calculate parity
break; break;
case 37: case 37:
low |= (info->cardnum & 0x7ffff) << 1; low |= (info->cardnum & 0x7ffff) << 1;
low |= (info->fc & 0xfff) << 20; low |= (info->fc & 0xfff) << 20;
high |= (info->fc & 0xf000) >> 12; high |= (info->fc & 0xf000) >> 12;
// TODO: Calculate parity
break; break;
default: default:
@ -87,6 +110,8 @@ bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ con
* *
* This only works with 26, 34, 35 and 37 bit card IDs. * 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. * Returns false on invalid inputs.
*/ */
bool unpack_short_hid(short_hid_info *out, uint32_t hi, uint32_t lo) { bool unpack_short_hid(short_hid_info *out, uint32_t hi, uint32_t lo) {
@ -107,29 +132,43 @@ bool unpack_short_hid(short_hid_info *out, uint32_t hi, uint32_t lo) {
out->fmtLen = idx3 + 19; out->fmtLen = idx3 + 19;
switch (out->fmtLen) { switch (out->fmtLen) {
case 26: case 26: // HID H10301
out->cardnum = (lo >> 1) & 0xFFFF; out->cardnum = (lo >> 1) & 0xFFFF;
out->fc = (lo >> 17) & 0xFF; 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; break;
case 34: case 34:
out->cardnum = (lo >> 1) & 0xFFFF; out->cardnum = (lo >> 1) & 0xFFFF;
out->fc = ((hi & 1) << 15) | (lo >> 17); out->fc = ((hi & 1) << 15) | (lo >> 17);
// TODO: Calculate parity
break; break;
case 35: case 35:
out->cardnum = (lo >> 1) & 0xFFFFF; out->cardnum = (lo >> 1) & 0xFFFFF;
out->fc = ((hi & 1) << 11) | (lo >> 21); out->fc = ((hi & 1) << 11) | (lo >> 21);
// TODO: Calculate parity
break; break;
default: default:
return false; return false;
} }
} else { } else {
// if bit 38 is not set then 37 bit format is used // If bit 38 is not set, then 37 bit format is used
out->fmtLen = 37; out->fmtLen = 37;
out->cardnum = (lo >> 1) & 0x7FFFF; out->cardnum = (lo >> 1) & 0x7FFFF;
out->fc = ((hi & 0xF) << 12) | (lo >> 20); out->fc = ((hi & 0xF) << 12) | (lo >> 20);
// TODO: Calculate parity
} }
return true; return true;
} }
@ -198,6 +237,11 @@ int CmdFSKdemodHID(const char *Cmd)
PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u", 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, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
card_info.fmtLen, card_info.fc, card_info.cardnum); card_info.fmtLen, card_info.fc, card_info.cardnum);
if (card_info.fmtLen == 26) {
PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
}
if (!ret) { if (!ret) {
PrintAndLog("Invalid or unsupported tag length."); PrintAndLog("Invalid or unsupported tag length.");
} }
@ -285,13 +329,19 @@ int CmdHIDPack(const char *Cmd) {
if (strlen(Cmd)<3) { if (strlen(Cmd)<3) {
PrintAndLog("Usage: lf hid pack <length> <facility code (decimal)> <card number (decimal)>"); PrintAndLog("Usage: lf hid pack <length> <facility code (decimal)> <card number (decimal)>");
PrintAndLog(" sample: lf hid pack 34 111 2345"); PrintAndLog(" sample: lf hid pack 26 123 4567");
return 0; return 0;
} }
card_info.fmtLen = param_get8(Cmd, 0); card_info.fmtLen = param_get8(Cmd, 0);
card_info.fc = param_get32ex(Cmd, 1, 0, 10); card_info.fc = param_get32ex(Cmd, 1, 0, 10);
card_info.cardnum = param_get32ex(Cmd, 2, 0, 10); card_info.cardnum = param_get32ex(Cmd, 2, 0, 10);
card_info.parityValid = true;
// TODO
if (card_info.fmtLen != 26) {
PrintAndLog("Warning: Parity bits are only calculated for 26 bit IDs -- this may be invalid!");
}
bool ret = pack_short_hid(&hi, &lo, &card_info); bool ret = pack_short_hid(&hi, &lo, &card_info);
@ -311,7 +361,7 @@ int CmdHIDUnpack(const char *Cmd)
uint32_t hi = 0, lo = 0; uint32_t hi = 0, lo = 0;
if (strlen(Cmd)<1) { if (strlen(Cmd)<1) {
PrintAndLog("Usage: lf hid unpack <ID>"); PrintAndLog("Usage: lf hid unpack <ID>");
PrintAndLog(" sample: lf hid unpack 2400de1252"); PrintAndLog(" sample: lf hid unpack 2006f623ae");
return 0; return 0;
} }
@ -328,6 +378,10 @@ int CmdHIDUnpack(const char *Cmd)
(unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
card_info.fmtLen, card_info.fc, card_info.cardnum); card_info.fmtLen, card_info.fc, card_info.cardnum);
if (card_info.fmtLen == 26) {
PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
}
if (!ret) { if (!ret) {
PrintAndLog("Invalid or unsupported tag length."); PrintAndLog("Invalid or unsupported tag length.");
} }

View file

@ -18,10 +18,21 @@
typedef struct { typedef struct {
// Format length, in bits. // Format length, in bits.
uint8_t fmtLen; uint8_t fmtLen;
// Facility code. // Facility code.
uint32_t fc; uint32_t fc;
// Card number. // Card number.
uint32_t cardnum; uint32_t cardnum;
// Parity validity.
//
// When used with pack_short_hid, this determines if we should calculate
// parity values for the ID.
//
// When used with unpack_short_hid, this indicates if we got valid parity
// values for the ID.
bool parityValid;
} short_hid_info; } short_hid_info;
bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const short_hid_info *info); bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const short_hid_info *info);