Merge branch 'RfidResearchGroup:master' into feature/hrt_parser

This commit is contained in:
Sanduuz authored and GitHub committed 2026-06-18 19:06:12 +03:00
commit ba3aebeb2a
3 files changed
+34 -23

No files matched your search

+9 -6
View File
@@ -44,7 +44,7 @@
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_start, int32_t threshold, bool verbose); static int demod_cotag(int32_t *samples, int num_samples, int clock, int clock_start, int32_t threshold, bool verbose);
static int detect_edge(const int32_t *samples, int num_samples, static int detect_edge(const int32_t *samples, int num_samples,
int index_start, int32_t threshold); int index_start, int32_t threshold);
static void find_avg_high_low(const int32_t *samples, int num_samples, static void find_avg_high_low(const int32_t *samples, int num_samples,
@@ -128,11 +128,11 @@ int demodCOTAG(bool verbose) {
* Use -1 for auto clock-start detection. * Use -1 for auto clock-start detection.
* @param threshold Amplitude threshold that defines a "high" sample. Use -1 for auto-threshold detection. * @param threshold Amplitude threshold that defines a "high" sample. Use -1 for auto-threshold detection.
*/ */
static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_start, int32_t threshold, bool verbose) { static int demod_cotag(int32_t *samples, int num_samples, int clock, int clock_start, int32_t threshold, bool verbose) {
int clock_half = clock / 2; int clock_half = clock / 2;
int32_t min, max; int32_t min, max;
double avg; double avg;
bool rv = false; int rv = PM3_EFAILED;
uint8_t *high_low_demod_01 = NULL; uint8_t *high_low_demod_01 = NULL;
uint8_t *manchester_demod = NULL; uint8_t *manchester_demod = NULL;
@@ -188,6 +188,7 @@ static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_
high_low_demod_01 = calloc(high_low_demod_01_len, sizeof(uint8_t)); high_low_demod_01 = calloc(high_low_demod_01_len, sizeof(uint8_t));
if (!high_low_demod_01) { if (!high_low_demod_01) {
PrintAndLogEx(ERR, "Error: out of memory"); PrintAndLogEx(ERR, "Error: out of memory");
rv = PM3_EMALLOC;
goto end; goto end;
} }
int high_low_demod_01_count = 0; int high_low_demod_01_count = 0;
@@ -220,6 +221,7 @@ static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_
manchester_demod = calloc(manchester_demod_len, sizeof(uint8_t)); manchester_demod = calloc(manchester_demod_len, sizeof(uint8_t));
if (!manchester_demod) { if (!manchester_demod) {
PrintAndLogEx(ERR, "Error: out of memory"); PrintAndLogEx(ERR, "Error: out of memory");
rv = PM3_EMALLOC;
goto end; goto end;
} }
@@ -263,6 +265,7 @@ static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_
manchester_demod_reversed = calloc(manchester_demod_len, sizeof(uint8_t)); manchester_demod_reversed = calloc(manchester_demod_len, sizeof(uint8_t));
if (!manchester_demod_reversed) { if (!manchester_demod_reversed) {
PrintAndLogEx(ERR, "Error: out of memory"); PrintAndLogEx(ERR, "Error: out of memory");
rv = PM3_EMALLOC;
goto end; goto end;
} }
for (int i = 0; i < manchester_count; i++) for (int i = 0; i < manchester_count; i++)
@@ -344,6 +347,7 @@ static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_
/* data_bits: 128 bits starting at preamble */ /* data_bits: 128 bits starting at preamble */
if (preamble_index + LF_COTAG_DATA_LEN > manchester_count) { if (preamble_index + LF_COTAG_DATA_LEN > manchester_count) {
PrintAndLogEx(INFO, " Not enough bits after preamble for full 128-bit data block"); PrintAndLogEx(INFO, " Not enough bits after preamble for full 128-bit data block");
rv = PM3_EPARTIAL;
goto end; goto end;
} }
@@ -414,7 +418,7 @@ static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_
PrintAndLogEx(INFO, " Sequence does NOT match at index %d (repeat count = %d)", pos, repeat_count); PrintAndLogEx(INFO, " Sequence does NOT match at index %d (repeat count = %d)", pos, repeat_count);
printf("\n"); printf("\n");
rv = true; rv = PM3_SUCCESS;
end: end:
free(manchester_demod_reversed); free(manchester_demod_reversed);
free(manchester_demod); free(manchester_demod);
@@ -424,8 +428,7 @@ end:
int demodCOTAG(bool verbose, int clock, int threshold) { int demodCOTAG(bool verbose, int clock, int threshold) {
int clk = (clock > 0) ? clock : LF_COTAG_CLOCK; int clk = (clock > 0) ? clock : LF_COTAG_CLOCK;
demod_cotag(g_GraphBuffer, (int)g_GraphTraceLen, clk, -1, threshold, verbose); return demod_cotag(g_GraphBuffer, (int)g_GraphTraceLen, clk, -1, threshold, verbose);
return PM3_SUCCESS;
} }
static int CmdCOTAGDemod(const char *Cmd) { static int CmdCOTAGDemod(const char *Cmd) {
+18 -13
View File
@@ -173,7 +173,8 @@ static void mad_print_aid_verbose(json_t *elm) {
static int mad1CRCCheck(const mad1_t *mad1, bool verbose) { static int mad1CRCCheck(const mad1_t *mad1, bool verbose) {
uint8_t crc = CRC8Mad((uint8_t *)&mad1->info, sizeof(mad1_t) - 1); uint8_t crc = CRC8Mad((uint8_t *)&mad1->info, sizeof(mad1_t) - 1);
if (crc != mad1->crc) { if (crc != mad1->crc) {
PrintAndLogEx(WARNING, _RED_("Wrong MAD 1 CRC") " calculated: 0x%02x != 0x%02x", crc, mad1->crc); if (verbose)
PrintAndLogEx(WARNING, _RED_("Wrong MAD 1 CRC") " calculated: 0x%02x != 0x%02x", crc, mad1->crc);
return PM3_ESOFT; return PM3_ESOFT;
} }
return PM3_SUCCESS; return PM3_SUCCESS;
@@ -182,7 +183,8 @@ static int mad1CRCCheck(const mad1_t *mad1, bool verbose) {
static int mad2CRCCheck(const mad2_t *mad2, bool verbose) { static int mad2CRCCheck(const mad2_t *mad2, bool verbose) {
uint8_t crc = CRC8Mad((uint8_t *)&mad2->info, sizeof(mad2_t) - 1); uint8_t crc = CRC8Mad((uint8_t *)&mad2->info, sizeof(mad2_t) - 1);
if (crc != mad2->crc) { if (crc != mad2->crc) {
PrintAndLogEx(WARNING, _RED_("Wrong MAD 2 CRC") " calculated: 0x%02x != 0x%02x", crc, mad2->crc); if (verbose)
PrintAndLogEx(WARNING, _RED_("Wrong MAD 2 CRC") " calculated: 0x%02x != 0x%02x", crc, mad2->crc);
return PM3_ESOFT; return PM3_ESOFT;
} }
return PM3_SUCCESS; return PM3_SUCCESS;
@@ -231,13 +233,13 @@ int MADCheck(const mad1_sector_t *sector0, const mad2_sector_t *mad2, bool verbo
*haveMAD2 = (mad_ver == 2); *haveMAD2 = (mad_ver == 2);
} }
int res = mad1CRCCheck(&sector0->mad, true); int res = mad1CRCCheck(&sector0->mad, verbose);
if (verbose && res == PM3_SUCCESS) { if (verbose && res == PM3_SUCCESS) {
PrintAndLogEx(SUCCESS, "CRC8...... 0x%02X ( %s )", sector0->mad.crc, _GREEN_("ok")); PrintAndLogEx(SUCCESS, "CRC8...... 0x%02X ( %s )", sector0->mad.crc, _GREEN_("ok"));
} }
if (mad_ver == 2 && mad2) { if (mad_ver == 2 && mad2) {
int res2 = mad2CRCCheck(&mad2->mad, true); int res2 = mad2CRCCheck(&mad2->mad, verbose);
if (res == PM3_SUCCESS) { if (res == PM3_SUCCESS) {
res = res2; res = res2;
} }
@@ -570,7 +572,7 @@ int mad_app_read(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override
PrintAndLogEx(INFO, "read sector %u, %zu data bytes", sno, nbytes); PrintAndLogEx(INFO, "read sector %u, %zu data bytes", sno, nbytes);
} }
if (*out_len == 0) if (*out_len == 0 && ops->verbose)
PrintAndLogEx(WARNING, "no sectors found for AID 0x%04X", aid); PrintAndLogEx(WARNING, "no sectors found for AID 0x%04X", aid);
return PM3_SUCCESS; return PM3_SUCCESS;
@@ -590,8 +592,9 @@ int mad_app_write(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool overrid
} }
if (data_len > capacity) { if (data_len > capacity) {
PrintAndLogEx(ERR, "data (%zu bytes) exceeds capacity (%zu bytes) for AID 0x%04X", if (ops->verbose)
data_len, capacity, aid); PrintAndLogEx(ERR, "data (%zu bytes) exceeds capacity (%zu bytes) for AID 0x%04X",
data_len, capacity, aid);
return PM3_EINVARG; return PM3_EINVARG;
} }
@@ -641,12 +644,14 @@ int mad_app_verify(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool overri
size_t cmp_len = readback_len < expected_len ? readback_len : expected_len; size_t cmp_len = readback_len < expected_len ? readback_len : expected_len;
if (memcmp(expected, readback, cmp_len) != 0) { if (memcmp(expected, readback, cmp_len) != 0) {
PrintAndLogEx(ERR, "Verify " _RED_("FAILED") ": data mismatch"); if (ops->verbose) {
for (size_t j = 0; j < cmp_len; j++) { PrintAndLogEx(ERR, "Verify " _RED_("FAILED") ": data mismatch");
if (expected[j] != readback[j]) { for (size_t j = 0; j < cmp_len; j++) {
PrintAndLogEx(ERR, "first difference at offset %zu: expected %02X, got %02X", if (expected[j] != readback[j]) {
j, expected[j], readback[j]); PrintAndLogEx(ERR, "first difference at offset %zu: expected %02X, got %02X",
break; j, expected[j], readback[j]);
break;
}
} }
} }
return PM3_ESOFT; return PM3_ESOFT;
+7 -4
View File
@@ -211,7 +211,7 @@ static bool test_mad1_crc_corrupt(bool verbose) {
bool haveMAD2 = false; bool haveMAD2 = false;
int res = MADCheck(&s, NULL, false, &haveMAD2); int res = MADCheck(&s, NULL, verbose, &haveMAD2);
ASSERT_TRUE("MADCheck should fail on corrupt CRC", res != PM3_SUCCESS); ASSERT_TRUE("MADCheck should fail on corrupt CRC", res != PM3_SUCCESS);
@@ -493,6 +493,7 @@ static bool test_verify_mismatch(bool verbose) {
PrintAndLogEx(INFO, " verify mismatch..."); PrintAndLogEx(INFO, " verify mismatch...");
mock_card_init(); mock_card_init();
mad_ops_t ops = make_mock_ops(); mad_ops_t ops = make_mock_ops();
ops.verbose = verbose;
// write known data // write known data
uint8_t wdata[240]; uint8_t wdata[240];
@@ -522,6 +523,7 @@ static bool test_write_overflow(bool verbose) {
PrintAndLogEx(INFO, " write overflow..."); PrintAndLogEx(INFO, " write overflow...");
mock_card_init(); mock_card_init();
mad_ops_t ops = make_mock_ops(); mad_ops_t ops = make_mock_ops();
ops.verbose = verbose;
uint8_t wdata[241]; uint8_t wdata[241];
memset(wdata, 0xAA, sizeof(wdata)); memset(wdata, 0xAA, sizeof(wdata));
@@ -538,6 +540,7 @@ static bool test_aid_not_found(bool verbose) {
PrintAndLogEx(INFO, " AID not found..."); PrintAndLogEx(INFO, " AID not found...");
mock_card_init(); mock_card_init();
mad_ops_t ops = make_mock_ops(); mad_ops_t ops = make_mock_ops();
ops.verbose = verbose;
uint8_t data[256] = {0}; uint8_t data[256] = {0};
size_t datalen = 99; size_t datalen = 99;
@@ -741,7 +744,7 @@ static bool test_mad2_crc_independent(bool verbose) {
// valid: both CRCs pass // valid: both CRCs pass
bool haveMAD2 = false; bool haveMAD2 = false;
int res = MADCheck(&s0, &s16, false, &haveMAD2); int res = MADCheck(&s0, &s16, verbose, &haveMAD2);
ASSERT_EQ("both valid", PM3_SUCCESS, res); ASSERT_EQ("both valid", PM3_SUCCESS, res);
ASSERT_EQ("MAD2 present", true, haveMAD2); ASSERT_EQ("MAD2 present", true, haveMAD2);
@@ -750,7 +753,7 @@ static bool test_mad2_crc_independent(bool verbose) {
memcpy(&s16_bad, &s16, sizeof(s16_bad)); memcpy(&s16_bad, &s16, sizeof(s16_bad));
s16_bad.mad.crc ^= 0xFF; s16_bad.mad.crc ^= 0xFF;
res = MADCheck(&s0, &s16_bad, false, &haveMAD2); res = MADCheck(&s0, &s16_bad, verbose, &haveMAD2);
ASSERT_TRUE("should fail with corrupt MAD2 CRC", res != PM3_SUCCESS); ASSERT_TRUE("should fail with corrupt MAD2 CRC", res != PM3_SUCCESS);
// corrupt MAD1 CRC only, MAD2 stays valid // corrupt MAD1 CRC only, MAD2 stays valid
@@ -758,7 +761,7 @@ static bool test_mad2_crc_independent(bool verbose) {
memcpy(&s0_bad, &s0, sizeof(s0_bad)); memcpy(&s0_bad, &s0, sizeof(s0_bad));
s0_bad.mad.crc ^= 0xFF; s0_bad.mad.crc ^= 0xFF;
res = MADCheck(&s0_bad, &s16, false, &haveMAD2); res = MADCheck(&s0_bad, &s16, verbose, &haveMAD2);
ASSERT_TRUE("should fail with corrupt MAD1 CRC", res != PM3_SUCCESS); ASSERT_TRUE("should fail with corrupt MAD1 CRC", res != PM3_SUCCESS);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed")); if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));