diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 8d2fb082b..d95917d3f 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -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) { diff --git a/client/src/wiegand_formatutils.c b/client/src/wiegand_formatutils.c index ed63bc5dc..ea8ee3744 100644 --- a/client/src/wiegand_formatutils.c +++ b/client/src/wiegand_formatutils.c @@ -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) { diff --git a/client/src/wiegand_formatutils.h b/client/src/wiegand_formatutils.h index 046dbdd55..1c2999a92 100644 --- a/client/src/wiegand_formatutils.h +++ b/client/src/wiegand_formatutils.h @@ -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);