Merge pull request #3052 from aaronjamt/hex_conv_cleanup

Bytes->Hex conversions cleanup
This commit is contained in:
Iceman
2025-12-20 22:10:45 +01:00
committed by GitHub
4 changed files with 26 additions and 60 deletions
+1 -6
View File
@@ -884,12 +884,7 @@ static int CmdHF14ACUIDs(const char *Cmd) {
if (resp.oldarg[0] == 0) {
PrintAndLogEx(WARNING, "card select failed.");
} else {
char uid_string[20];
for (uint16_t m = 0; m < card->uidlen; m++) {
int offset = 2 * m;
snprintf(uid_string + offset, sizeof(uid_string) - offset, "%02X", card->uid[m]);
}
PrintAndLogEx(SUCCESS, "%s", uid_string);
PrintAndLogEx(SUCCESS, "%s", sprint_hex_inrow(card->uid, card->uidlen));
}
}
PrintAndLogEx(SUCCESS, "end: %" PRIu64 " seconds", (msclock() - t1) / 1000);
+2 -7
View File
@@ -80,20 +80,15 @@ static int CmdHFEPACollectPACENonces(const char *Cmd) {
PrintAndLogEx(FAILED, "Error in step %" PRId64 ", Return code: %" PRId64, resp.oldarg[0], resp.oldarg[1]);
} else {
size_t nonce_length = resp.oldarg[1];
size_t nonce_length_bytes = 2 * nonce_length + 1;
char *nonce = (char *) calloc(2 * nonce_length + 1, sizeof(uint8_t));
if (nonce == NULL) {
PrintAndLogEx(WARNING, "Failed to allocate memory");
return PM3_EMALLOC;
}
for (int j = 0; j < nonce_length; j++) {
int nonce_offset = 2 * j;
snprintf(nonce + nonce_offset, (nonce_length_bytes * sizeof(uint8_t)) - nonce_offset, "%02X", resp.data.asBytes[j]);
}
// print nonce
PrintAndLogEx(SUCCESS, "Length: %zu, Nonce: %s", nonce_length, nonce);
PrintAndLogEx(SUCCESS, "Length: %zu, Nonce: %s", nonce_length, sprint_hex_inrow(resp.data.asBytes, nonce_length));
free(nonce);
}
if (i < n - 1) {
+17 -37
View File
@@ -983,26 +983,6 @@ static bool set_bitfield(saflok_mfc_data_t *data, size_t start_bit, size_t num_b
return true;
}
// Static analysis hint: reads all bytes of `data[len]`
// * NOT THREAD SAFE ... uses a static buffer for result,
// so multiple calls will overwrite previous results.
// This style of design SPAWNS BUGS.
// NOTE: apparently accepted for this legacy codebase.
static char *bytes_to_hex(const uint8_t *data, size_t len) {
static char buf[256]; // WARNING: caller must immediately use or copy the result, and it's still not thread-safe!
static const size_t maximum_data_len = (ARRAYLEN(buf) / 2) - 1; // leave room for null terminator
if (len > maximum_data_len) {
PrintAndLogEx(ERR, "saflok bytes_to_hex: input length %zu exceeds maximum supported %zu, results will be truncated!", len, maximum_data_len);
len = maximum_data_len;
}
memset(buf, 0, sizeof(buf)); // Clear the buffer before use
for (size_t i = 0; i < len; i++) {
sprintf(buf + (i * 2), "%02X", data[i]);
}
return buf;
}
static uint8_t calculated_saflok_checksum(const saflok_mfc_data_t *data) {
_Static_assert(ARRAYLEN(data->raw) == 17, "saflok_mfc_data_t raw size must be 17 bytes");
int sum = 0;
@@ -1196,8 +1176,8 @@ static int CmdHFSaflokRead(const char *Cmd) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "--- " _CYAN_("Card Information"));
PrintAndLogEx(INFO, "Plaintext Data: %s", bytes_to_hex(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(SUCCESS, "Encrypted Data: " _GREEN_("%s"), bytes_to_hex(encrypted->raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Plaintext Data: %s", sprint_hex_inrow(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(SUCCESS, "Encrypted Data: " _GREEN_("%s"), sprint_hex_inrow(encrypted->raw, sizeof(saflok_mfc_data_t)));
saflok_decode(&decrypted);
@@ -1261,8 +1241,8 @@ static int CmdHFSaflokEncode(const char *Cmd) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "--- " _CYAN_("Encoded Card Data"));
PrintAndLogEx(INFO, "Plaintext Data: %s", bytes_to_hex(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(SUCCESS, "Encrypted Data: " _GREEN_("%s"), bytes_to_hex(encrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Plaintext Data: %s", sprint_hex_inrow(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(SUCCESS, "Encrypted Data: " _GREEN_("%s"), sprint_hex_inrow(encrypted.raw, sizeof(saflok_mfc_data_t)));
CLIParserFree(ctx);
@@ -1297,8 +1277,8 @@ static int CmdHFSaflokDecode(const char *Cmd) {
saflok_decrypt(&encrypted, &decrypted);
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "--- " _CYAN_("Decoded Card Data"));
PrintAndLogEx(INFO, "Plaintext Data: %s", bytes_to_hex(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Encrypted Data: %s", bytes_to_hex(encrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Plaintext Data: %s", sprint_hex_inrow(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Encrypted Data: %s", sprint_hex_inrow(encrypted.raw, sizeof(saflok_mfc_data_t)));
saflok_decode(&decrypted);
return PM3_SUCCESS;
@@ -1346,7 +1326,7 @@ static int CmdHFSaflokModify(const char *Cmd) {
}
saflok_decrypt(&encrypted, &decrypted);
PrintAndLogEx(INFO, "Plaintext Data: %s", bytes_to_hex(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Plaintext Data: %s", sprint_hex_inrow(decrypted.raw, sizeof(saflok_mfc_data_t)));
uint32_t card_level = get_saflok_mfc_card_level(&decrypted);
card_level = arg_get_u32_def(ctx, 1, card_level);
@@ -1411,8 +1391,8 @@ static int CmdHFSaflokModify(const char *Cmd) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "--- " _CYAN_("Modified Card Data"));
PrintAndLogEx(INFO, "Plaintext Data: %s", bytes_to_hex(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(SUCCESS, "Encrypted Data: " _GREEN_("%s"), bytes_to_hex(reencrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(INFO, "Plaintext Data: %s", sprint_hex_inrow(decrypted.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(SUCCESS, "Encrypted Data: " _GREEN_("%s"), sprint_hex_inrow(reencrypted.raw, sizeof(saflok_mfc_data_t)));
CLIParserFree(ctx);
@@ -1446,7 +1426,7 @@ static int CmdHFSaflokEncrypt(const char *Cmd) {
}
saflok_encrypt(&raw, &encrypted);
PrintAndLogEx(SUCCESS, "Encrypted: " _GREEN_("%s"), bytes_to_hex(encrypted.raw, 17));
PrintAndLogEx(SUCCESS, "Encrypted: " _GREEN_("%s"), sprint_hex_inrow(encrypted.raw, 17));
CLIParserFree(ctx);
return PM3_SUCCESS;
@@ -1478,7 +1458,7 @@ static int CmdHFSaflokDecrypt(const char *Cmd) {
}
saflok_decrypt(&encrypted, &decrypted);
PrintAndLogEx(SUCCESS, "Decrypted: " _GREEN_("%s"), bytes_to_hex(decrypted.raw, 17));
PrintAndLogEx(SUCCESS, "Decrypted: " _GREEN_("%s"), sprint_hex_inrow(decrypted.raw, 17));
CLIParserFree(ctx);
return PM3_SUCCESS;
@@ -1851,16 +1831,16 @@ static int CmdHFSaflokSelfTest(const char *Cmd) {
saflok_encrypt(&tc->decoded, &tmp);
if (memcmp(tmp.raw, tc->encoded.raw, sizeof(saflok_mfc_data_t)) != 0) {
PrintAndLogEx(FAILED, "Test case %zu: encryption did not match expected data.", i);
PrintAndLogEx(DEBUG, " raw result: %s", bytes_to_hex(tmp.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(DEBUG, " expected result: %s", bytes_to_hex(tc->encoded.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(DEBUG, " raw result: %s", sprint_hex_inrow(tmp.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(DEBUG, " expected result: %s", sprint_hex_inrow(tc->encoded.raw, sizeof(saflok_mfc_data_t)));
result_test_cases = PM3_EFAILED;
continue;
}
saflok_decrypt(&tc->encoded, &tmp);
if (memcmp(tmp.raw, tc->decoded.raw, sizeof(saflok_mfc_data_t)) != 0) {
PrintAndLogEx(FAILED, "Test case %zu: decryption did not match expected data.", i);
PrintAndLogEx(DEBUG, " raw result: %s", bytes_to_hex(tmp.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(DEBUG, " expected result: %s", bytes_to_hex(tc->decoded.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(DEBUG, " raw result: %s", sprint_hex_inrow(tmp.raw, sizeof(saflok_mfc_data_t)));
PrintAndLogEx(DEBUG, " expected result: %s", sprint_hex_inrow(tc->decoded.raw, sizeof(saflok_mfc_data_t)));
result_test_cases = PM3_EFAILED;
continue;
}
@@ -2023,7 +2003,7 @@ static int CmdHFSaflokChecksum(const char *Cmd) {
data.raw[16] = calculated_saflok_checksum(&data);
PrintAndLogEx(SUCCESS, "Block + checksum: " _GREEN_("%s"), bytes_to_hex(data.raw, 17));
PrintAndLogEx(SUCCESS, "Block + checksum: " _GREEN_("%s"), sprint_hex_inrow(data.raw, 17));
PrintAndLogEx(SUCCESS, "Checksum byte: " _GREEN_("0x%02X"), data.raw[16]);
CLIParserFree(ctx);
@@ -2068,7 +2048,7 @@ static int CmdHFSaflokProvision(const char *Cmd) {
saflok_mfc_key_t keyA = {0};
saflok_kdf(saflok_uid, &keyA);
PrintAndLogEx(INFO, "Generated UID-derived key: " _GREEN_("%s"), bytes_to_hex(keyA.key, ARRAYLEN(keyA.key)));
PrintAndLogEx(INFO, "Generated UID-derived key: " _GREEN_("%s"), sprint_hex_inrow(keyA.key, ARRAYLEN(keyA.key)));
uint8_t all_F[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t block1[16];
+6 -10
View File
@@ -1542,12 +1542,10 @@ static int CmdHfSeosPACS(const char *Cmd) {
// convert OID hex to literal string
char oid_buffer[256] = "";
for (int i = 0; i < oid_len; i++) {
sprintf(oid_buffer + (i * 2), "%02X", oid_hex[i]);
}
unsigned char oid_buffer[256] = "";
hex_to_buffer(oid_buffer, oid_hex, oid_len, 256, 0, 0, 1);
const char *oid = oid_buffer;
const unsigned char *oid = oid_buffer;
if (oid_len == 0) {
PrintAndLogEx(ERR, "OID value must be supplied");
@@ -1665,12 +1663,10 @@ static int CmdHfSeosADF(const char *Cmd) {
}
// convert OID hex to literal string
char oid_buffer[256] = "";
for (int i = 0; i < oid_len; i++) {
sprintf(oid_buffer + (i * 2), "%02X", oid_hex[i]);
}
unsigned char oid_buffer[256] = "";
hex_to_buffer(oid_buffer, oid_hex, oid_len, 256, 0, 0, 1);
const char *oid = oid_buffer;
const unsigned char *oid = oid_buffer;
if (oid_len == 0) {
PrintAndLogEx(ERR, "OID value must be supplied");