wiegand: share the sentinel right-aligned PACS packing

HID stores PACS bits behind a sentinel bit, right aligned, both in iCLASS
block 7 and in MIFARE Classic block 5. Move hf mf encodehid's packing
into wiegand_pack_sentinel_bytes() so the SAM downgrades reuse it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
iceman1001andClaude Opus 5.5 committed 2026-11-10 10:23:39 +07:00
1 parent 0fdbdbdadd
commit fb193860e8
3 files changed
+32 -21

No files matched your search

+2 -21
View File
@@ -11510,32 +11510,13 @@ static int CmdHF14AMfValue(const char *Cmd) {
// Encode the normalized Wiegand payload into the sentinel-prefixed byte layout stored in
// the HID-specific MIFARE payload block.
static int hfmf_encodehid_pack_block5(const char *binstr, uint8_t *block) {
if (binstr == NULL || block == NULL) {
return PM3_EINVARG;
}
char bits_with_sentinel[121] = {0};
size_t binlen = strlen(binstr);
if (binlen == 0 || (binlen + 1) > 120) {
if (block == NULL) {
return PM3_EINVARG;
}
// MIFARE block 5 stores the Wiegand payload with the same sentinel-prefixed layout
// expected by existing HID cards: one leading 1 followed by the logical payload bits.
bits_with_sentinel[0] = '1';
memcpy(bits_with_sentinel + 1, binstr, binlen + 1);
size_t hexlen = 0;
uint8_t hex[15] = {0};
binstr_2_bytes(hex, &hexlen, bits_with_sentinel);
if (hexlen == 0 || hexlen > (MFBLOCK_SIZE - 1)) {
return PM3_EINVARG;
}
memset(block + 1, 0x00, MFBLOCK_SIZE - 1);
memcpy(block + 1 + ((MFBLOCK_SIZE - 1) - hexlen), hex, hexlen);
return PM3_SUCCESS;
return wiegand_pack_sentinel_bytes(binstr, block + 1, MFBLOCK_SIZE - 1);
}
static int CmdHFMFHidEncode(const char *Cmd) {
+27
View File
@@ -283,6 +283,33 @@ bool wiegand_new_pacs_to_binstr(const uint8_t *pacs, size_t pacs_len, char *bins
return true;
}
int wiegand_pack_sentinel_bytes(const char *binstr, uint8_t *out, size_t out_len) {
if (binstr == NULL || out == NULL) {
return PM3_EINVARG;
}
size_t bin_len = strlen(binstr);
if (bin_len == 0 || (bin_len + 1) > (out_len * 8)) {
return PM3_EINVARG;
}
memset(out, 0, out_len);
// MSB first bit index of the sentinel
size_t pos = (out_len * 8) - bin_len - 1;
out[pos / 8] |= (uint8_t)(0x80 >> (pos % 8));
for (size_t i = 0; i < bin_len; i++) {
pos++;
if (binstr[i] == '1') {
out[pos / 8] |= (uint8_t)(0x80 >> (pos % 8));
} else if (binstr[i] != '0') {
return PM3_EINVARG;
}
}
return PM3_SUCCESS;
}
int wiegand_pack_bin_with_hid_header(const char *binstr, wiegand_message_t *packed) {
size_t bin_len = strlen(binstr);
if (packed == NULL || bin_len == 0 || bin_len > 84) {
+3
View File
@@ -78,6 +78,9 @@ int wiegand_set_plain_binstr(const char *binstr, wiegand_input_t *input);
// Decode ASN.1 PACS input into the normalized binary representation used by downstream callers.
int wiegand_set_new_pacs_binstr(const uint8_t *pacs, size_t pacs_len, wiegand_input_t *input);
// Right-align a sentinel bit plus the payload bits in out_len bytes, as HID stores PACS on iCLASS and MIFARE.
int wiegand_pack_sentinel_bytes(const char *binstr, uint8_t *out, size_t out_len);
// Build HID transport words from a plain Wiegand payload, including sentinel placement and headers.
int wiegand_pack_bin_with_hid_header(const char *binstr, wiegand_message_t *packed);