From b82f6036222b8e5c1b43ebebd64bbd21f7769be7 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:11:42 +0200 Subject: [PATCH 1/8] urmet parsing --- CHANGELOG.md | 1 + client/CMakeLists.txt | 1 + client/Makefile | 1 + client/experimental_lib/CMakeLists.txt | 1 + client/src/cmdhfmf.c | 5 + client/src/parsers/parseproac.c | 159 +++++++++++++++++++++++++ client/src/parsers/parseproac.h | 33 +++++ client/src/parsers/parsevigik.c | 117 +++++++++++++++--- 8 files changed, 299 insertions(+), 19 deletions(-) create mode 100644 client/src/parsers/parseproac.c create mode 100644 client/src/parsers/parseproac.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 26416eb99..9b7ea9403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `hf mf view` - Urmet Captiv cards now decode their number field and data regions (@iceman1001) - Changed `hf mf view --selftest` - now also checks the Hexact payload decoder, both ways (@iceman1001) - Changed `hf mf view` - Hexact sector 9,11 payload is now decoded and cross checked against sector 0,15 and UID (@iceman1001) - Added `hf mfdes dump` - now walks the whole PICC and saves a `hf-mfdes--dump.json` card image, picking up `hf-mfdes--keys.json` on its own (@iceman1001) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 466d22ab2..19579482e 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -510,6 +510,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehid.c ${PM3_ROOT}/client/src/parsers/parsevigik.c ${PM3_ROOT}/client/src/parsers/parsehexact.c + ${PM3_ROOT}/client/src/parsers/parseproac.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/Makefile b/client/Makefile index 7e2248b33..7d38fb544 100644 --- a/client/Makefile +++ b/client/Makefile @@ -908,6 +908,7 @@ SRCS = mifare/aiddesfire.c \ parsers/parsehid.c \ parsers/parsevigik.c \ parsers/parsehexact.c \ + parsers/parseproac.c \ parsers/hrtparser/hrtparser.c \ pm3.c \ pm3_binlib.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 7dcc6d6fc..78a76f9c7 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -437,6 +437,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehid.c ${PM3_ROOT}/client/src/parsers/parsevigik.c ${PM3_ROOT}/client/src/parsers/parsehexact.c + ${PM3_ROOT}/client/src/parsers/parseproac.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 70e7401c7..b37506de7 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -46,6 +46,7 @@ #include "parsers/parsehid.h" #include "parsers/parsevigik.h" #include "parsers/parsehexact.h" +#include "parsers/parseproac.h" #include "generator.h" // keygens. #include "fpga.h" #include "mifare/mifarehost.h" @@ -890,6 +891,10 @@ static int mf_view_dump(uint8_t *dump, size_t bytes_read, uint16_t block_cnt, bo (void)hexact_parser_parse(dump, bytes_read); } + if (is_valid_proac_card(dump, bytes_read)) { + (void)proac_parser_parse(dump, bytes_read); + } + return PM3_SUCCESS; } diff --git a/client/src/parsers/parseproac.c b/client/src/parsers/parseproac.c new file mode 100644 index 000000000..779bee579 --- /dev/null +++ b/client/src/parsers/parseproac.c @@ -0,0 +1,159 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// PROAC PACS parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parseproac.h" + +#include // isprint +#include + +#include "commonutil.h" // ARRAYLEN +#include "crc.h" // CRC8Mad +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow, str_append, str_trim +#include "mifare/mad.h" +#include "mifare/mifaredefault.h" // MFBLOCK_SIZE +#include "mifare/mifare4.h" // mfFirstBlockOfSector + +#define PROAC_SCHED_SECTOR_A 1 +#define PROAC_SCHED_SECTOR_B 2 +#define PROAC_SCHED_HEADER 4 + +#define PROAC_LOT_SECTOR 14 +#define PROAC_LOT_BLOCK 1 + +#define PROAC_BLOB_SECTOR_A 12 +#define PROAC_BLOB_SECTOR_B 13 + +#define PROAC_TAIL_SECTOR 15 + +// three data blocks per sector, both schedule sectors, less the header +#define PROAC_SCHED_MAX ((2 * 3 * MFBLOCK_SIZE) - PROAC_SCHED_HEADER) + +static const uint8_t *proac_block(const uint8_t *dump, size_t dumplen, uint8_t sector, uint8_t block) { + size_t off = (mfFirstBlockOfSector(sector) + block) * MFBLOCK_SIZE; + if (off + MFBLOCK_SIZE > dumplen) { + return NULL; + } + return dump + off; +} + +bool is_valid_proac_card(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < sizeof(mad1_sector_t)) { + return false; + } + return (mad_find_aid((const mad1_sector_t *)dump, PROAC_MAD_AID) > -1); +} + +static size_t proac_schedule(const uint8_t *dump, size_t dumplen, char *out, size_t outlen) { + size_t n = 0; + const uint8_t sectors[2] = { PROAC_SCHED_SECTOR_A, PROAC_SCHED_SECTOR_B }; + + for (uint8_t s = 0; s < ARRAYLEN(sectors); s++) { + + for (uint8_t b = 0; b < 3; b++) { + + const uint8_t *p = proac_block(dump, dumplen, sectors[s], b); + if (p == NULL) { + return n; + } + + uint8_t start = (s == 0 && b == 0) ? PROAC_SCHED_HEADER : 0; + for (uint8_t k = start; k < MFBLOCK_SIZE; k++) { + + if (p[k] == 0x00) { + return n; + } + + if (n + 1 >= outlen) { + return n; + } + out[n++] = isprint(p[k]) ? (char)p[k] : '.'; + } + } + } + return n; +} + +int proac_parser_parse(const uint8_t *dump, size_t dumplen) { + + if (is_valid_proac_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + const mad1_sector_t *s0 = (const mad1_sector_t *)dump; + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "--- " _CYAN_("PROAC") " detected --------------------"); + PrintAndLogEx(INFO, "MAD AID............ " _YELLOW_("0x%04X") " on %d of %d sectors", + PROAC_MAD_AID, + mad_count_aid(s0, PROAC_MAD_AID), + MAD1_NUM_AIDS + ); + + // The one card this was written against carries a MAD whose CRC was never + // filled in, so a reader in this system cannot be validating it. Say so + // rather than silently trusting the directory. + uint8_t crc = CRC8Mad((uint8_t *)&s0->mad.info, sizeof(mad1_t) - 1); + if (crc != s0->mad.crc) { + PrintAndLogEx(INFO, "MAD CRC............ %02X, computed %02X ( " _RED_("invalid") " )", s0->mad.crc, crc); + } + + const uint8_t *hdr = proac_block(dump, dumplen, PROAC_SCHED_SECTOR_A, 0); + if (hdr) { + PrintAndLogEx(INFO, "Schedule header.... %s", sprint_hex_inrow(hdr, PROAC_SCHED_HEADER)); + } + + char sched[PROAC_SCHED_MAX + 1] = {0}; + if (proac_schedule(dump, dumplen, sched, sizeof(sched)) > 0) { + PrintAndLogEx(INFO, "Schedule........... " _YELLOW_("%s"), sched); + PrintAndLogEx(INFO, " written in clear, in a sector this card's own key can rewrite"); + } + + const uint8_t *lot = proac_block(dump, dumplen, PROAC_LOT_SECTOR, PROAC_LOT_BLOCK); + if (lot && isprint(lot[0])) { + char text[MFBLOCK_SIZE + 1] = {0}; + str_append(text, sizeof(text), "%.*s", MFBLOCK_SIZE, (const char *)lot); + str_trim(text); + PrintAndLogEx(INFO, "Lot................ " _YELLOW_("%s"), text); + } + + PrintAndLogEx(INFO, "Payload............ sector %u and %u, 96 bytes, " _YELLOW_("not decoded"), + PROAC_BLOB_SECTOR_A, + PROAC_BLOB_SECTOR_B + ); + + const uint8_t sectors[2] = { PROAC_BLOB_SECTOR_A, PROAC_BLOB_SECTOR_B }; + + for (uint8_t s = 0; s < ARRAYLEN(sectors); s++) { + for (uint8_t b = 0; b < 3; b++) { + const uint8_t *p = proac_block(dump, dumplen, sectors[s], b); + if (p) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", sectors[s], b, sprint_hex_inrow(p, MFBLOCK_SIZE)); + } + } + } + + PrintAndLogEx(INFO, "Tail............... sector %u, " _YELLOW_("not decoded"), PROAC_TAIL_SECTOR); + for (uint8_t b = 0; b < 3; b++) { + const uint8_t *p = proac_block(dump, dumplen, PROAC_TAIL_SECTOR, b); + if (p) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", PROAC_TAIL_SECTOR, b, sprint_hex_inrow(p, MFBLOCK_SIZE)); + } + } + return PM3_SUCCESS; +} diff --git a/client/src/parsers/parseproac.h b/client/src/parsers/parseproac.h new file mode 100644 index 000000000..3bfe1132f --- /dev/null +++ b/client/src/parsers/parseproac.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// PROAC parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#ifndef PARSEPROAC_H__ +#define PARSEPROAC_H__ + +#include "common.h" + +// MAD application id, function cluster 0x49, the VIGIK cluster +#define PROAC_MAD_AID 0x4982 + +// True when the MAD claims a sector for the PROAC application +bool is_valid_proac_card(const uint8_t *dump, size_t dumplen); + +// Print what is known about the layout of a PROAC card +int proac_parser_parse(const uint8_t *dump, size_t dumplen); + +#endif diff --git a/client/src/parsers/parsevigik.c b/client/src/parsers/parsevigik.c index cf7cd4e89..e7dc5d79b 100644 --- a/client/src/parsers/parsevigik.c +++ b/client/src/parsers/parsevigik.c @@ -19,6 +19,7 @@ #include "parsevigik.h" #include +#include // isdigit #include #include "commonutil.h" @@ -396,16 +397,15 @@ int vigik_annotate(mfc_vigik_t *d) { } -// Key layouts of the VIGIK family building access systems. Every one of these -// ships the same keys on every card, so matching the whole set identifies the -// system outright - no guessing from the payload. Sourced from the schemas in -// armsrc/Standalone/hf_colin.c. Hexact is not here; it has its own parser. -typedef struct { +typedef struct vigik_schema_s { const char *name; uint64_t key_a[16]; uint64_t key_b[16]; + int (*decode)(const uint8_t *dump, size_t dumplen); } vigik_schema_t; +static int vigik_print_urmet_captiv(const uint8_t *dump, size_t dumplen); + static const vigik_schema_t vigik_schemas[] = { { "Noralsy", @@ -420,7 +420,8 @@ static const vigik_schema_t vigik_schemas[] = { 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL - } + }, + NULL }, { "Urmet Captiv", @@ -435,7 +436,8 @@ static const vigik_schema_t vigik_schemas[] = { 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL - } + }, + vigik_print_urmet_captiv }, { "VIGIK service badge", @@ -450,7 +452,8 @@ static const vigik_schema_t vigik_schemas[] = { VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY - } + }, + NULL }, }; @@ -468,7 +471,7 @@ static uint64_t vigik_sector_key(const uint8_t *dump, size_t dumplen, uint8_t se return key; } -const char *vigik_detect_schema(const uint8_t *dump, size_t dumplen) { +static const vigik_schema_t *vigik_detect_schema_entry(const uint8_t *dump, size_t dumplen) { if (dump == NULL) { return NULL; @@ -481,24 +484,98 @@ const char *vigik_detect_schema(const uint8_t *dump, size_t dumplen) { for (uint8_t s = 0; s < 16 && match; s++) { - if (schema->key_a[s] != VIGIK_KEY_ANY && - schema->key_a[s] != vigik_sector_key(dump, dumplen, s, false)) { + if ((schema->key_a[s] != VIGIK_KEY_ANY) && (schema->key_a[s] != vigik_sector_key(dump, dumplen, s, false))) { match = false; } - if (schema->key_b[s] != VIGIK_KEY_ANY && - schema->key_b[s] != vigik_sector_key(dump, dumplen, s, true)) { + if ((schema->key_b[s] != VIGIK_KEY_ANY) && (schema->key_b[s] != vigik_sector_key(dump, dumplen, s, true))) { match = false; } } if (match) { - return schema->name; + return schema; } } return NULL; } +const char *vigik_detect_schema(const uint8_t *dump, size_t dumplen) { + const vigik_schema_t *schema = vigik_detect_schema_entry(dump, dumplen); + return (schema != NULL) ? schema->name : NULL; +} + +// --------------------------------------------------------------- Urmet Captiv +#define URMET_DIGITS_OFF 5 +#define URMET_DIGITS_LEN 11 + +static bool urmet_all_digits(const uint8_t *p, size_t len) { + for (size_t i = 0; i < len; i++) { + if (isdigit(p[i]) == 0) { + return false; + } + } + return true; +} + +static int vigik_print_urmet_captiv(const uint8_t *dump, size_t dumplen) { + + if (dumplen < MFBLOCK_SIZE * 3) { + return PM3_EINVARG; + } + + const uint8_t *b1 = dump + MFBLOCK_SIZE; + const uint8_t *b2 = dump + (2 * MFBLOCK_SIZE); + + PrintAndLogEx(INFO, "Header............. %s", sprint_hex_inrow(b1, URMET_DIGITS_OFF)); + + if (urmet_all_digits(b1 + URMET_DIGITS_OFF, URMET_DIGITS_LEN)) { + char num[URMET_DIGITS_LEN + 1] = {0}; + memcpy(num, b1 + URMET_DIGITS_OFF, URMET_DIGITS_LEN); + PrintAndLogEx(INFO, "Number............. " _YELLOW_("%s") "%s" + , num + , (strspn(num, "0") == URMET_DIGITS_LEN) ? " ( all zero, unset )" : "" + ); + } else { + PrintAndLogEx(INFO, "Number............. %s ( not ASCII digits )", sprint_hex_inrow(b1 + URMET_DIGITS_OFF, URMET_DIGITS_LEN)); + } + + PrintAndLogEx(INFO, "Marker............. %s", sprint_hex_inrow(b2, 2)); + PrintAndLogEx(INFO, "Data............... layout " _YELLOW_("not decoded")); + + int shown = 0; + for (uint8_t s = 1; s < 16; s++) { + + for (uint8_t b = 0; b < 3; b++) { + + size_t off = (mfFirstBlockOfSector(s) + b) * MFBLOCK_SIZE; + if (off + MFBLOCK_SIZE > dumplen) { + continue; + } + + const uint8_t *p = dump + off; + bool empty = true; + for (uint8_t k = 0; k < MFBLOCK_SIZE; k++) { + if (p[k]) { + empty = false; + break; + } + } + if (empty == false) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", s, b, sprint_hex_inrow(p, MFBLOCK_SIZE)); + shown++; + } + } + } + + if (shown == 0) { + PrintAndLogEx(INFO, " every sector past 0 is empty, card not personalised"); + } + + PrintAndLogEx(INFO, "Keys............... one key r/w every block"); + return PM3_SUCCESS; +} + bool is_valid_vigik_card(const uint8_t *dump, size_t dumplen) { if (dump == NULL || dumplen < sizeof(mad1_sector_t)) { return false; @@ -525,10 +602,11 @@ int vigik_parser_parse(const uint8_t *dump, size_t dumplen) { PrintAndLogEx(SUCCESS, "System............. " _YELLOW_("%s"), schema); } - // Only the deployments that publish a MAD can be taken apart any further. - // The others keep the same structure somewhere else on the card, and where - // that is has not been worked out, so say so instead of printing nonsense. if (DetectHID(s0, VIGIK_MAD_AID) < 0) { + const vigik_schema_t *entry = vigik_detect_schema_entry(dump, dumplen); + if (entry != NULL && entry->decode != NULL) { + return entry->decode(dump, dumplen); + } PrintAndLogEx(INFO, "No MAD on this card, structure " _YELLOW_("not decoded")); return PM3_SUCCESS; } @@ -540,8 +618,6 @@ int vigik_parser_parse(const uint8_t *dump, size_t dumplen) { return res; } - // The VIGIK structure is the data blocks of sector 0 followed by the data - // blocks of every sector the MAD hands to the VIGIK application. union { uint8_t *bytes; mfc_vigik_t *vigik; @@ -558,8 +634,11 @@ int vigik_parser_parse(const uint8_t *dump, size_t dumplen) { dlen += MFBLOCK_SIZE * 3; for (size_t i = 0; i < mad_list.len; i++) { + if (VIGIK_MAD_AID == mad_list.entries[i].aid || VIGIK_MAD_AID_ALT == mad_list.entries[i].aid) { + uint32_t offset = mad_list.entries[i].sector * MFBLOCK_SIZE * 4; + if (offset + (MFBLOCK_SIZE * 3) > dumplen) { continue; } From 6f4643a637c31395f14cd8e072da999605a8afe9 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:13:35 +0200 Subject: [PATCH 2/8] hf mf: add the MAD helpers parseproac needs, and register AID 0x4982 b82f60362 landed parseproac.c without them, so a clean checkout does not build. - mad.c / mad.h: mad_find_aid() and mad_count_aid(); DetectHID() delegates to the first, it was already generic - mad.json: 0x4982 PROAC, sorted in after NORALSY's 0x4980 Co-Authored-By: Claude Opus 5 (1M context) --- client/resources/mad.json | 7 +++++++ client/src/mifare/mad.c | 22 ++++++++++++++++++++-- client/src/mifare/mad.h | 8 ++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/client/resources/mad.json b/client/resources/mad.json index 6b8465f55..840c2ad8f 100644 --- a/client/resources/mad.json +++ b/client/resources/mad.json @@ -6224,6 +6224,13 @@ "service_provider": "NORALSY", "system_integrator": "NORALSY" }, + { + "application": "(access control and security) VIGIK", + "company": "PROAC", + "mad": "0x4982", + "service_provider": "PROAC", + "system_integrator": "PROAC" + }, { "application": "Card Administration, cardholder administration, access control & security, company services, miscellaneous applications", "company": "Ministry of Defense", diff --git a/client/src/mifare/mad.c b/client/src/mifare/mad.c index ff3980687..64b54f5eb 100644 --- a/client/src/mifare/mad.c +++ b/client/src/mifare/mad.c @@ -499,19 +499,37 @@ bool HasMADKey(const mad1_sector_t *s0) { return (memcmp(s0->trailer.key_a, g_mifare_mad_key, sizeof(g_mifare_mad_key)) == 0); } -int DetectHID(const mad1_sector_t *s0, uint16_t manufacture) { +int mad_find_aid(const mad1_sector_t *s0, uint16_t aid) { if (s0 == NULL) { return -1; } for (int i = 0; i < MAD1_NUM_AIDS; i++) { - if (madGetAID(s0->mad.aid[i], false) == manufacture) { + if (madGetAID(s0->mad.aid[i], false) == aid) { return i + 1; } } return -1; } +int mad_count_aid(const mad1_sector_t *s0, uint16_t aid) { + if (s0 == NULL) { + return 0; + } + + int n = 0; + for (int i = 0; i < MAD1_NUM_AIDS; i++) { + if (madGetAID(s0->mad.aid[i], false) == aid) { + n++; + } + } + return n; +} + +int DetectHID(const mad1_sector_t *s0, uint16_t manufacture) { + return mad_find_aid(s0, manufacture); +} + int convert_mad_to_arr(const mad1_sector_t *s0, const mad2_sector_t *s16, size_t dump_len, uint8_t *out, size_t omax, size_t *olen, bool override) { if (s0 == NULL || out == NULL || olen == NULL || dump_len < sizeof(mad1_sector_t) || omax == 0) { diff --git a/client/src/mifare/mad.h b/client/src/mifare/mad.h index 8d018aacb..e2714b6fc 100644 --- a/client/src/mifare/mad.h +++ b/client/src/mifare/mad.h @@ -114,6 +114,14 @@ int MADDFDecodeAndPrint(uint32_t short_aid, bool verbose); int MADCardHolderInfoDecode(const uint8_t *data, size_t datalen, bool verbose); void MADPrintHeader(void); bool HasMADKey(const mad1_sector_t *s0); +// First sector carrying this AID, 1..15, or -1. The MAD is how an application +// says which sectors are its own, so every dump parser that keys off an AID +// wants this. +int mad_find_aid(const mad1_sector_t *s0, uint16_t aid); + +// Count of sectors carrying this AID +int mad_count_aid(const mad1_sector_t *s0, uint16_t aid); + int DetectHID(const mad1_sector_t *s0, uint16_t manufacture); int convert_mad_to_arr(const mad1_sector_t *s0, const mad2_sector_t *s16, size_t dump_len, From 9caa6d61173d4b334d46634fbccd52098848615e Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:16:51 +0200 Subject: [PATCH 3/8] minor change we device now reports back EMULATOR memory size to the client. Had to bump capability version number to 11. --- CHANGELOG.md | 1 + armsrc/BigBuf.c | 15 +++++++++++++-- armsrc/BigBuf.h | 2 ++ armsrc/appmain.c | 2 ++ client/src/cmdhf15.c | 1 - client/src/comms.c | 4 ++++ client/src/comms.h | 1 + include/pm3_cmd.h | 11 ++++++++++- 8 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7ea9403..e1d0e51bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `hw status` - now reports EMULATOR memory (@iceman1001) - Changed `hf mf view` - Urmet Captiv cards now decode their number field and data regions (@iceman1001) - Changed `hf mf view --selftest` - now also checks the Hexact payload decoder, both ways (@iceman1001) - Changed `hf mf view` - Hexact sector 9,11 payload is now decoded and cross checked against sector 0,15 and UID (@iceman1001) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index cfd495c5d..bbdd0e93b 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -113,11 +113,18 @@ uint32_t BigBuf_get_hi(void) { return s_bigbuf_hi; } -/* +// how much emulator memory a card image may use. Reported to the client in +// capabilities_t so it sizes eload / esave from what the device actually has, +// instead of hardcoding a copy of CARD_MEMORY_SIZE uint32_t BigBuf_get_EM_size(void) { return CARD_MEMORY_SIZE; } -*/ + +// has the emulator memory been handed out yet? It is allocated lazily on the +// first BigBuf_get_EM_addr(), so "not allocated" means no card image is loaded +bool BigBuf_is_EM_allocated(void) { + return (s_emulator_memory != NULL); +} // clear ALL of BigBuf void BigBuf_Clear(void) { @@ -192,6 +199,10 @@ void BigBuf_print_status(void) { DbpString(_CYAN_("Memory")); Dbprintf(" BigBuf_size............. %d", s_bigbuf_size); Dbprintf(" Available memory........ %d", s_bigbuf_hi); + Dbprintf(" Emulator memory......... %d ( %s )" + , CARD_MEMORY_SIZE + , (s_emulator_memory != NULL) ? "in use" : "not allocated" + ); DbpString(_CYAN_("Tracing")); Dbprintf(" tracing ................ %d", s_tracing); Dbprintf(" traceLen ............... %d", s_trace_len); diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index d690b4cd5..63bf4bb77 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -42,6 +42,8 @@ uint8_t *BigBuf_get_addr(void); uint32_t BigBuf_get_size(void); uint8_t *BigBuf_get_EM_addr(void); +uint32_t BigBuf_get_EM_size(void); +bool BigBuf_is_EM_allocated(void); uint16_t BigBuf_max_traceLen(void); uint32_t BigBuf_get_hi(void); diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 43250a368..2f5d5476d 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -712,6 +712,8 @@ static void SendCapabilities(void) { capabilities.via_fpc = g_reply_via_fpc; capabilities.via_usb = g_reply_via_usb; capabilities.bigbuf_size = BigBuf_get_size(); + capabilities.em_size = BigBuf_get_EM_size(); + capabilities.em_allocated = BigBuf_is_EM_allocated(); capabilities.baudrate = 0; // no real baudrate for USB-CDC #ifdef WITH_FPC_USART if (g_reply_via_fpc) diff --git a/client/src/cmdhf15.c b/client/src/cmdhf15.c index fa69da62e..ff05ec5d3 100644 --- a/client/src/cmdhf15.c +++ b/client/src/cmdhf15.c @@ -49,7 +49,6 @@ #define Logic0 Iso15693Logic0 #define Logic1 Iso15693Logic1 #define FrameEOF Iso15693FrameEOF -#define CARD_MEMORY_SIZE 4096 #ifndef ISO15_ERROR_HANDLING_RESPONSE #define ISO15_ERROR_HANDLING_RESPONSE { \ diff --git a/client/src/comms.c b/client/src/comms.c index 7c54e17bf..193c4346d 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -952,6 +952,7 @@ int TestProxmark(pm3_device_t *dev) { g_conn.send_via_fpc_usart = g_pm3_capabilities.via_fpc; g_conn.uart_speed = g_pm3_capabilities.baudrate; g_conn.max_cmd_data_size = MIN(g_pm3_capabilities.max_cmd_data_size, (uint16_t)PM3_CMD_DATA_SIZE); + g_conn.em_size = g_pm3_capabilities.em_size; bool is_tcp_conn = (g_conn.send_via_ip == PM3_TCPv4 || g_conn.send_via_ip == PM3_TCPv6); bool is_bt_conn = (memcmp(g_conn.serial_port_name, "bt:", 3) == 0); @@ -965,6 +966,9 @@ int TestProxmark(pm3_device_t *dev) { ); PrintAndLogEx(SUCCESS, "Max frame size: " _GREEN_("%u") " bytes", g_conn.max_cmd_data_size); + PrintAndLogEx(SUCCESS, "Emulator memory: " _GREEN_("%u") " bytes%s", + g_conn.em_size, + (g_pm3_capabilities.em_allocated) ? " ( in use )" : ""); if (g_conn.send_via_fpc_usart) { PrintAndLogEx(SUCCESS, "PM3 UART serial baudrate: " _GREEN_("%u") "\n", g_conn.uart_speed); } else { diff --git a/client/src/comms.h b/client/src/comms.h index 4eba1b0c5..08fddfb20 100644 --- a/client/src/comms.h +++ b/client/src/comms.h @@ -78,6 +78,7 @@ typedef struct { bool listen_for_incoming; char serial_port_name[FILE_PATH_SIZE]; uint16_t max_cmd_data_size; + uint16_t em_size; // device side CARD_MEMORY_SIZE } communication_arg_t; extern communication_arg_t g_conn; diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 9fafe84f3..8874eb1db 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -276,11 +276,20 @@ typedef struct { // the client accepts a shorter struct from older firmware // PM5 can use this to inform which size they support oo. uint16_t max_cmd_data_size; // device side PM3_CMD_DATA_SIZE + + // Appended in version 11. + // How much emulator memory a card image may use, device side + // CARD_MEMORY_SIZE. The client sizes eload / esave from this instead of + // carrying its own copy of the constant, so a PM5 reporting a larger + // emulator memory needs no client change. + uint16_t em_size; + bool em_allocated : 1; // is a card image resident right now } PACKED capabilities_t; // Bump this whenever a command payload changes shape, so a mismatched client and // firmware refuse to talk instead of misparsing each other. // 10: CMD_HF_MIFARE_EML_MEMSET gained a flags byte -#define CAPABILITIES_VERSION 10 +// 11: capabilities_t gained em_size / em_allocated +#define CAPABILITIES_VERSION 11 // what a pre-v9 device would have used, it could not tell us #define CAPABILITIES_LEGACY_CMD_DATA_SIZE 512 extern capabilities_t g_pm3_capabilities; From aae70014e07a74751ad6c4a45324b7e2d02aadaa Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:21:47 +0200 Subject: [PATCH 4/8] - VIGIK service badge fields now show which ones the RSA signature actually covers --- CHANGELOG.md | 1 + client/src/parsers/parsevigik.c | 53 +++++++++++++-------------------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d0e51bd..bf8bb7a75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `hf mf view` - now show which VIGIK fields are RSA signed (@iceman1001) - Changed `hw status` - now reports EMULATOR memory (@iceman1001) - Changed `hf mf view` - Urmet Captiv cards now decode their number field and data regions (@iceman1001) - Changed `hf mf view --selftest` - now also checks the Hexact payload decoder, both ways (@iceman1001) diff --git a/client/src/parsers/parsevigik.c b/client/src/parsers/parsevigik.c index e7dc5d79b..f435956e4 100644 --- a/client/src/parsers/parsevigik.c +++ b/client/src/parsers/parsevigik.c @@ -32,6 +32,7 @@ #include "mbedtls/bignum.h" #define VIGIK_SIG_LEN 128 // RSA 1024, the signature on the card +#define VIGIK_SIGNED "( " _GREEN_("signed") " )" #define VIGIK_MSG_SLOTS 64 // message byte slots in an ISO 9796-1 block #define VIGIK_MSG_PAD 8 // leading zero bytes of the signed message @@ -62,7 +63,6 @@ const char *vigik_get_service(uint16_t service_code) { // ISO 9796-1 shadow permutation. Each message byte is stored next to its shadow, -// which is what gives the scheme its redundancy. static const uint8_t vigik_iso9796_pi[16] = { 0x0E, 0x03, 0x05, 0x08, 0x09, 0x04, 0x02, 0x0F, 0x00, 0x0D, 0x0B, 0x06, 0x07, 0x0A, 0x0C, 0x01 @@ -75,10 +75,6 @@ static uint8_t vigik_shadow(uint8_t b) { // Recover the message an ISO 9796-1 signature carries, checking the redundancy on // the way. `sig` is the signature as it sits on the card, `modulus` the public key // to try. On success msg holds msglen bytes. -// -// Rabin, so v = 2 and squaring the signature gives the block back. Which of the -// four candidates is the block is decided by the format itself: an ISO 9796-1 -// block always ends in the nibble 6. static int vigik_iso9796_recover(const uint8_t *sig, const char *modulus, uint8_t *msg, size_t *msglen) { uint8_t n[VIGIK_SIG_LEN] = {0}; @@ -158,9 +154,6 @@ static int vigik_iso9796_recover(const uint8_t *sig, const char *modulus, uint8_ return PM3_ESOFT; } - // Every byte pair has to be a message byte next to its shadow. Exactly three - // are allowed not to be: the leading pair carries the bit that keeps the block - // below n, one marks how long the message is, and the last holds the forced 6. size_t broken[4] = {0}; size_t brokencnt = 0; for (size_t i = 0; i < VIGIK_SIG_LEN; i += 2) { @@ -186,9 +179,6 @@ static int vigik_iso9796_recover(const uint8_t *sig, const char *modulus, uint8_ stream[i / 2] = f[i + 1]; } - // The message is repeated to fill the block, the first slot belonging to the - // truncated tail of the run before it. Where a second copy fits, it has to - // agree with the first. if (((1 + (2 * z)) <= VIGIK_MSG_SLOTS) && (memcmp(stream + 1, stream + 1 + z, z) != 0)) { return PM3_ESOFT; } @@ -198,26 +188,21 @@ static int vigik_iso9796_recover(const uint8_t *sig, const char *modulus, uint8_ return PM3_SUCCESS; } -// The five byte dates are year since 1900, then month, day, hour, minute. -// Print the raw bytes with the date spelled out beside them, and leave the -// decoded form off when the bytes cannot be a date at all. -static void vigik_print_date(const char *label, const uint8_t *v) { +static void vigik_print_date(const char *label, const uint8_t *v, const char *suffix) { bool sane = ((v[1] >= 1) && (v[1] <= 12) && (v[2] >= 1) && (v[2] <= 31) && (v[3] <= 23) && (v[4] <= 59)); if (sane) { - PrintAndLogEx(INFO, "%s %s ( " _YELLOW_("%04u-%02u-%02u %02u:%02u") " )", + PrintAndLogEx(INFO, "%s %s ( " _YELLOW_("%04u-%02u-%02u %02u:%02u") " )%s", label, sprint_hex_inrow(v, 5), - 1900 + v[0], v[1], v[2], v[3], v[4]); + 1900 + v[0], v[1], v[2], v[3], v[4], suffix); } else { - PrintAndLogEx(INFO, "%s %s", label, sprint_hex_inrow(v, 5)); + PrintAndLogEx(INFO, "%s %s" "%s", label, sprint_hex_inrow(v, 5), suffix); } } -// What a VIGIK signature covers: the UID the card has to keep, the service it -// belongs to, and the window it is good for. static size_t vigik_expected_msg(const mfc_vigik_t *d, uint8_t *out) { size_t len = 0; @@ -277,16 +262,16 @@ int vigik_verify(mfc_vigik_t *d) { PrintAndLogEx(INFO, " Service code..... 0x%04X", (uint16_t)d->service_code); PrintAndLogEx(INFO, " Key version...... %u", d->key_version); PrintAndLogEx(INFO, " Services counter. %u", d->services_counter); - vigik_print_date(" Access date......", d->slot_access_date); + vigik_print_date(" Access date......", d->slot_access_date, ""); PrintAndLogEx(INFO, " DST duration..... %u", d->slot_dst_duration); - // The service code on the card says which service issued it, so it should - // name the key that just verified. Where it does not, the table pairs a - // description with the wrong modulus - say so rather than quietly - // reporting the wrong issuer. + // The service code on the card says which service issued it, so it should name the key if (vigik_rsa_pk[i].code != (uint16_t)d->service_code) { PrintAndLogEx(WARNING, "Card says service 0x%04X but the key that verified is listed as 0x%04X ( %s )", - (uint16_t)d->service_code, vigik_rsa_pk[i].code, vigik_rsa_pk[i].desc); + (uint16_t)d->service_code, + vigik_rsa_pk[i].code, + vigik_rsa_pk[i].desc + ); } PrintAndLogEx(SUCCESS, "Signature verification: " _GREEN_("successful")); @@ -377,17 +362,21 @@ int vigik_annotate(mfc_vigik_t *d) { PrintAndLogEx(INFO, "MAD................. %s", sprint_hex_inrow(d->mad, sizeof(d->mad))); PrintAndLogEx(INFO, "Counters............ %u", d->counters); PrintAndLogEx(INFO, "rtf................. %s", sprint_hex_inrow(d->rtf, sizeof(d->rtf))); - PrintAndLogEx(INFO, "Service code........ 0x%08x / %u - " _YELLOW_("%s"), d->service_code, d->service_code, vigik_get_service(d->service_code)); + PrintAndLogEx(INFO, "Service code........ 0x%08x / %u - " _YELLOW_("%s") " " VIGIK_SIGNED, d->service_code, d->service_code, vigik_get_service(d->service_code)); PrintAndLogEx(INFO, "Info flag........... %u -", d->info_flag); // , sprint_bin(d->info_flag, 1)); - PrintAndLogEx(INFO, "Key version......... %u", d->key_version); + PrintAndLogEx(INFO, "Key version......... %u " VIGIK_SIGNED, d->key_version); PrintAndLogEx(INFO, "PTR Counter......... %u", d->ptr_counter); PrintAndLogEx(INFO, "Counter num......... %u", d->counter_num); - vigik_print_date("Slot access date....", d->slot_access_date); - PrintAndLogEx(INFO, "Slot dst duration... %u", d->slot_dst_duration); + vigik_print_date("Slot access date....", d->slot_access_date, " " VIGIK_SIGNED); + PrintAndLogEx(INFO, "Slot dst duration... %u " VIGIK_SIGNED, d->slot_dst_duration); PrintAndLogEx(INFO, "Other Slots......... %s", sprint_hex_inrow(d->other_slots, sizeof(d->other_slots))); - PrintAndLogEx(INFO, "Services counter.... %u", d->services_counter); - vigik_print_date("Loading date........", d->loading_date); + PrintAndLogEx(INFO, "Services counter.... %u " VIGIK_SIGNED, d->services_counter); + vigik_print_date("Loading date........", d->loading_date, ""); PrintAndLogEx(INFO, "Reserved null....... %u", d->reserved_null); + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Unmarked fields are outside the signature, %d of the %d service record", + 35, 48); + PrintAndLogEx(INFO, "bytes, and every one of them is writable with a published key"); PrintAndLogEx(INFO, "----------------------------------------------------------------"); PrintAndLogEx(INFO, ""); vigik_verify(d); From 465deeda399ea9574b2de9bf343d3f23532c1923 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:24:31 +0200 Subject: [PATCH 5/8] text --- client/src/cmdsmartcard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdsmartcard.c b/client/src/cmdsmartcard.c index 97cdc4f23..385a44151 100644 --- a/client/src/cmdsmartcard.c +++ b/client/src/cmdsmartcard.c @@ -622,7 +622,7 @@ static int CmdSmartUpgrade(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "smart upgrade", "Upgrade RDV4 sim module firmware", - "smart upgrade -f sim020.bin" + "smart upgrade -f sim024.bin" ); void *argtable[] = { From 8f5fd048d3a576a135a2707c700bab6d7da7dcc0 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:27:06 +0200 Subject: [PATCH 6/8] improve emulator downloads with out of bounds checks --- armsrc/appmain.c | 23 +++++++++++++++++++++++ client/src/cmdhficlass.c | 12 ++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 2f5d5476d..219bee189 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -3299,10 +3299,33 @@ static void PacketReceived(PacketCommandNG *packet) { if (packet->length < sizeof(download_req_t)) { break; } + + if (mem == NULL) { + reply_download_done(CMD_DOWNLOAD_EML_BIGBUF, 0, 0); + LED_B_OFF(); + break; + } + const download_req_t *dreq = (const download_req_t *)packet->data.asBytes; uint32_t startidx = dreq->start_index; uint32_t numofbytes = dreq->bytes; + // We report the emulator memory size in capabilities_t, so honour it + // here as well. Without this a client asking for more than the + // emulator holds reads on past it into the rest of BigBuf + uint32_t em_size = BigBuf_get_EM_size(); + if (startidx >= em_size) { + Dbprintf("Emulator memory download starts past the end, %u >= %u", startidx, em_size); + reply_download_done(CMD_DOWNLOAD_EML_BIGBUF, 0, 0); + LED_B_OFF(); + break; + } + + if (startidx + numofbytes > em_size) { + Dbprintf("Emulator memory is %u bytes, truncating download of %u to %u", em_size, numofbytes, em_size - startidx); + numofbytes = em_size - startidx; + } + // arg0 = startindex // arg1 = length bytes to transfer // arg2 = RFU diff --git a/client/src/cmdhficlass.c b/client/src/cmdhficlass.c index 306d5bd93..0d3a64263 100644 --- a/client/src/cmdhficlass.c +++ b/client/src/cmdhficlass.c @@ -2017,9 +2017,9 @@ static int CmdHFiClassESave(const char *Cmd) { CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); uint16_t bytes = arg_get_int_def(ctx, 2, 256); - if (bytes > 4096) { - PrintAndLogEx(WARNING, "Emulator memory is max 4096bytes. Truncating %u to 4096", bytes); - bytes = 4096; + if (bytes > g_conn.em_size) { + PrintAndLogEx(WARNING, "Emulator memory is max %u bytes. Truncating %u to %u", g_conn.em_size, bytes, g_conn.em_size); + bytes = g_conn.em_size; } CLIParserFree(ctx); @@ -2077,9 +2077,9 @@ static int CmdHFiClassEView(const char *Cmd) { CLIParserFree(ctx); - if (bytes > 4096) { - PrintAndLogEx(WARNING, "Emulator memory is max 4096bytes. Truncating %u to 4096", bytes); - bytes = 4096; + if (bytes > g_conn.em_size) { + PrintAndLogEx(WARNING, "Emulator memory is max %u bytes. Truncating %u to %u", g_conn.em_size, bytes, g_conn.em_size); + bytes = g_conn.em_size; } if (bytes % 8 != 0) { From 83937835487dae22fe8a27e64bcdd0fa084ace3f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 12:40:51 +0200 Subject: [PATCH 7/8] one more entry in the mad --- client/resources/mad.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/resources/mad.json b/client/resources/mad.json index 840c2ad8f..7be261320 100644 --- a/client/resources/mad.json +++ b/client/resources/mad.json @@ -10011,6 +10011,13 @@ "service_provider": "Unisys", "system_integrator": "Unisys" }, + { + "application": "MICROHARD", + "company": "MICROHARD SRL", + "mad": "0x6938", + "service_provider": "MICROHARD SRL", + "system_integrator": "MICROHARD SRL" + }, { "application": "Access control, time attendance, bus ticket, hotel room card, VIP card f. club", "company": "Giga-TMS Inc", From 5b909402b51f87f798cdd262c6ffe2dd0b0c6f0b Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 14 Sep 2026 13:06:14 +0200 Subject: [PATCH 8/8] BigBuf: stop narrowing sizes, and size the 14a modulation buffer to fit Three things that all cost memory or correctness once BigBuf is larger than 64KB, which it already is on PM5. BigBuf_max_traceLen() returned a uint16_t while s_bigbuf_hi is a uint32_t. At 33272 on AT91 that is harmless; on AT32, where BigBuf runs to several hundred kbyte, it truncates and LF sampling asks for a fraction of the buffer that is there. Widened, along with the four callers that assigned it straight back into a uint16_t. BigBuf_malloc() and BigBuf_calloc() took a uint16_t chunksize, so a request of exactly 65536 arrived as 0 and anything above wrapped. It returned NULL for the 65536 case by accident, not by design. Both take a uint32_t now and the guard tests the upper bound explicitly, so an oversized request fails like any other allocation that does not fit. The 14a tag simulation allocated its dynamic modulation buffer as a flat 512, or 4096 for ST25TA. prepare_tag_modulation() memcpy's the encoded answer out of the ToSend buffer and tosend_stuffbit() hard caps that at TOSEND_BUFFER_SIZE, so 1788 of ST25TA's 4096 could never be reached -- it was the largest single allocation of any tag simulation for nothing. Meanwhile 512 is 68 bytes short of what a full 64 byte response encodes to, at 9 bytes per byte plus 4, so those answers were refused at modulation time. Both are now derived from the response size and capped at what ToSend can hold: 580 for the default tag types, 2308 for ST25TA. That also fixes the reason the 4096 never helped -- all six call sites passed the 512 macro as max_buffer_size rather than the size actually allocated, so ST25TA allocated 4096 and then bounds checked against 512. Worst case for a simulation that also holds emulator memory drops from 12247 bytes of BigBuf to 10459. Built for client, RDV4 and PM3GENERIC. Not yet run on hardware. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 3 +++ armsrc/BigBuf.c | 18 ++++++++++++++---- armsrc/BigBuf.h | 6 +++--- armsrc/appmain.c | 5 +++-- armsrc/iso14443a.c | 30 ++++++++++++++++++++---------- armsrc/lfops.c | 2 +- armsrc/lfsampling.c | 4 ++-- armsrc/pcf7931.c | 2 +- armsrc/spiffs.c | 14 +++++++------- 9 files changed, 54 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf8bb7a75..3a564283d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `hf 14a sim` - a dynamic response now gets a modulation buffer sized to fit it, instead of one 68 bytes too small (or 1788 too large for ST25TA) (@iceman1001) +- Fixed `BigBuf_malloc` - takes a uint32_t, so a request of 64KB or more fails instead of wrapping to zero (@iceman1001) +- Fixed `BigBuf_max_traceLen` - returns a uint32_t, so LF sampling on a device with more than 64KB of BigBuf is not handed a truncated buffer (@iceman1001) - Changed `hf mf view` - now show which VIGIK fields are RSA signed (@iceman1001) - Changed `hw status` - now reports EMULATOR memory (@iceman1001) - Changed `hf mf view` - Urmet Captiv cards now decode their number field and data regions (@iceman1001) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index bbdd0e93b..a68bf7a07 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -151,10 +151,16 @@ void BigBuf_Clear_keep_EM(void) { // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory // at the beginning of BigBuf is always for traces/samples -uint8_t *BigBuf_malloc(uint16_t chunksize) { +// +// chunksize is a uint32_t on purpose. It used to be a uint16_t, which silently +// wrapped anything from 64 kbyte up -- a request for exactly 65536 came through +// as 0 -- and on a PM5, where BigBuf is several hundred kbyte, that is a size a +// caller can reasonably ask for. Oversized requests now fail the check below and +// return NULL like any other allocation that does not fit. +uint8_t *BigBuf_malloc(uint32_t chunksize) { chunksize = (chunksize + BIGBUF_ALIGN_BYTES - 1) & BIGBUF_ALIGN_MASK; // round up to next multiple of 4 - if (s_bigbuf_hi - s_trace_len < chunksize || chunksize == 0) { + if (chunksize == 0 || chunksize > s_bigbuf_hi || s_bigbuf_hi - s_trace_len < chunksize) { // no memory left or chunksize too large return NULL; } @@ -165,7 +171,7 @@ uint8_t *BigBuf_malloc(uint16_t chunksize) { // allocate a chunk of memory from BigBuf, and returns a pointer to it. // sets the memory to zero -uint8_t *BigBuf_calloc(uint16_t chunksize) { +uint8_t *BigBuf_calloc(uint32_t chunksize) { uint8_t *mem = BigBuf_malloc(chunksize); if (mem != NULL) { memset(mem, 0x00, ((chunksize + BIGBUF_ALIGN_BYTES - 1) & BIGBUF_ALIGN_MASK)); // round up to next multiple of 4 @@ -229,7 +235,11 @@ void BigBuf_print_status(void) { } // return the maximum trace length (i.e. the unallocated size of BigBuf) -uint16_t BigBuf_max_traceLen(void) { +// Room left for traces and samples, ie everything below the lowest allocation. +// s_bigbuf_hi is a uint32_t and on a PM5 BigBuf is several hundred kbyte, so this +// must not be narrowed -- truncating it to 16 bits would silently hand LF +// sampling a fraction of the buffer it actually has +uint32_t BigBuf_max_traceLen(void) { return s_bigbuf_hi & BIGBUF_ALIGN_MASK; } diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index 63bf4bb77..8a9110b15 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -44,7 +44,7 @@ uint32_t BigBuf_get_size(void); uint8_t *BigBuf_get_EM_addr(void); uint32_t BigBuf_get_EM_size(void); bool BigBuf_is_EM_allocated(void); -uint16_t BigBuf_max_traceLen(void); +uint32_t BigBuf_max_traceLen(void); uint32_t BigBuf_get_hi(void); void BigBuf_initialize(void); @@ -52,8 +52,8 @@ void BigBuf_Clear(void); void BigBuf_Clear_ext(bool verbose); void BigBuf_Clear_keep_EM(void); void BigBuf_Clear_EM(void); -uint8_t *BigBuf_malloc(uint16_t); -uint8_t *BigBuf_calloc(uint16_t); +uint8_t *BigBuf_malloc(uint32_t); +uint8_t *BigBuf_calloc(uint32_t); void BigBuf_free(void); void BigBuf_free_keep_EM(void); void BigBuf_print_status(void); diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 219bee189..18279feb2 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -3441,8 +3441,9 @@ static void PacketReceived(PacketCommandNG *packet) { if (g_dbglevel >= DBG_DEBUG) Dbprintf("Filename received for spiffs dump : %s", filename); // a file can be bigger than BigBuf, so it is streamed out one frame at a - // time. Reading it into BigBuf first wrapped the uint16_t BigBuf_calloc() - // takes at 64KB and then read the whole file into the short buffer + // time. Reading it into BigBuf first used to wrap at 64KB, back when + // BigBuf_calloc() took a uint16_t, and read the whole file into the + // short buffer that produced const uint16_t dl_chunk = reply_ng_max_data_size() - sizeof(download_chunk_t); uint8_t *chunkbuf = BigBuf_calloc(dl_chunk); if (chunkbuf == NULL) { diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 0c9899182..071b5b9fe 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -1795,18 +1795,28 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u // pool and BigBuf_malloc() won't hand out memory the trace still occupies. clear_trace(); - // Allocate 512 bytes for the dynamic modulation, created when the reader queries for it + // Buffers for the dynamic response, created when the reader queries for it. // Such a response is less time critical, so we can prepare them on the fly #define DYNAMIC_RESPONSE_BUFFER_SIZE 64 -#define DYNAMIC_MODULATION_BUFFER_SIZE 512 uint16_t dynamic_response_buffer_size = DYNAMIC_RESPONSE_BUFFER_SIZE; - uint16_t dynamic_modulation_buffer_size = DYNAMIC_MODULATION_BUFFER_SIZE; if (tagType == 10) { dynamic_response_buffer_size = ST25TA_EML_NDEF_MAX + 4; - dynamic_modulation_buffer_size = 4096; } + // prepare_tag_modulation() memcpy's the encoded answer out of the ToSend + // buffer, and tosend_stuffbit() hard caps that at TOSEND_BUFFER_SIZE, so the + // modulation can never be larger than that however big the response is. + // Encoding costs 9 bytes per response byte -- 8 data bits plus a parity bit -- + // and 4 more for start, stop and the correction bit. + // + // ST25TA used to ask for a flat 4096 here, 1788 bytes more than ToSend can + // ever hand it, which made it the largest single allocation of any tag + // simulation. The 512 the other tag types got was 68 bytes short of what a + // full 64 byte dynamic response needs, so those were refused at modulation + // time with an out-of-bound message + uint16_t dynamic_modulation_buffer_size = MIN((9 * dynamic_response_buffer_size) + 4, TOSEND_BUFFER_SIZE); + uint8_t *dynamic_response_buffer = BigBuf_calloc(dynamic_response_buffer_size); if (dynamic_response_buffer == NULL) { BigBuf_free_keep_EM(); @@ -2222,7 +2232,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u num_to_bytes(nonce, 4, dynamic_response_info.response); dynamic_response_info.response_n = 4; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_AUTH; } else if (receivedCmd[0] == ISO14443A_CMD_RATS && len == 4) { // Received a RATS request @@ -2260,7 +2270,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u // prepare to send dynamic_response_info.response_n = 1 + 8 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_AUTH; @@ -2311,7 +2321,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u dynamic_response_info.response_n = 1 + 8 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_NONE; @@ -2342,7 +2352,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u // prepare to send dynamic_response_info.response_n = 1 + 16 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_AUTH; @@ -2396,7 +2406,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u dynamic_response_info.response_n = 1 + 16 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_NONE; @@ -2426,7 +2436,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u dynamic_response_info.response[1] = pack_live[1]; AddCrc14A(dynamic_response_info.response, 2); dynamic_response_info.response_n = 4; // 2 PACK + 2 CRC - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; } else { if (g_dbglevel >= DBG_DEBUG) Dbprintf("Password did not match, NACK_IV."); diff --git a/armsrc/lfops.c b/armsrc/lfops.c index 559793272..21e3ad89d 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -536,7 +536,7 @@ void ReadTItag(bool ledcontrol) { #define FREQHI 134200 signed char *dest = (signed char *)BigBuf_get_addr(); - uint16_t n = BigBuf_max_traceLen(); + uint32_t n = BigBuf_max_traceLen(); // 128 bit shift register [shift3:shift2:shift1:shift0] uint32_t shift3 = 0, shift2 = 0, shift1 = 0, shift0 = 0; diff --git a/armsrc/lfsampling.c b/armsrc/lfsampling.c index ed6f32658..b4ae75411 100644 --- a/armsrc/lfsampling.c +++ b/armsrc/lfsampling.c @@ -598,7 +598,7 @@ void doT55x7Acquisition(size_t sample_size, bool ledcontrol) { #define T55xx_READ_TOL 5 uint8_t *dest = BigBuf_get_addr(); - uint16_t bufsize = BigBuf_max_traceLen(); + uint32_t bufsize = BigBuf_max_traceLen(); if (bufsize > sample_size) bufsize = sample_size; @@ -685,7 +685,7 @@ void doT55x7Acquisition(size_t sample_size, bool ledcontrol) { #endif void doCotagAcquisition(void) { - uint16_t bufsize = BigBuf_max_traceLen(); + uint32_t bufsize = BigBuf_max_traceLen(); uint8_t *dest = BigBuf_calloc(bufsize); dest[0] = 0; diff --git a/armsrc/pcf7931.c b/armsrc/pcf7931.c index 1a69eb0b6..a39fa7065 100644 --- a/armsrc/pcf7931.c +++ b/armsrc/pcf7931.c @@ -60,7 +60,7 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) { uint8_t bits[256] = {0x00}; uint8_t blocks[8][16]; uint8_t *dest = BigBuf_get_addr(); - uint16_t g_GraphTraceLen = BigBuf_max_traceLen(); + uint32_t g_GraphTraceLen = BigBuf_max_traceLen(); // limit g_GraphTraceLen to a little more than 2 data frames. // To make sure a complete dataframe is in the dataset. // 1 Frame is 16 Byte -> 128byte. at a T0 of 64 -> 8129 Samples per frame. diff --git a/armsrc/spiffs.c b/armsrc/spiffs.c index 20413d528..e7a6cbebf 100644 --- a/armsrc/spiffs.c +++ b/armsrc/spiffs.c @@ -448,9 +448,9 @@ static void copy_in_spiffs(const char *src, const char *dst) { spiffs_close_cached(); - // no BigBuf_calloc(filesize) here: a file can be bigger than BigBuf, and bigger - // than the uint16_t BigBuf_calloc() takes. Copy it in SPIFFS_WRITE_CHUNK_SIZE - // chunks instead, which is also what rdv40_spiffs_write() writes in + // no BigBuf_calloc(filesize) here: a file can be bigger than BigBuf. Copy it + // in SPIFFS_WRITE_CHUNK_SIZE chunks instead, which is also what + // rdv40_spiffs_write() writes in uint8_t *mem = BigBuf_calloc(SPIFFS_WRITE_CHUNK_SIZE); if (mem == NULL) { Dbprintf("error, cannot allocate copy buffer"); @@ -854,10 +854,10 @@ static int stream_from_spiffs(const char *filename, uint32_t offset, uint32_t si // Stream `size` bytes from `offset` of a file, one `chunklen` chunk at a time. // // The download path used to read the whole file into BigBuf first. That caps a -// download at what BigBuf holds and, since BigBuf_calloc() takes a uint16_t, the -// request wrapped for files >= 64KB: a short buffer, then a full size read run -// straight past the end of it. Here the file is opened once and only one chunk -// is ever buffered, so file size no longer enters into it. +// download at what BigBuf holds, and back when BigBuf_calloc() took a uint16_t +// the request wrapped for files >= 64KB: a short buffer, then a full size read +// run straight past the end of it. Here the file is opened once and only one +// chunk is ever buffered, so file size no longer enters into it. // // Returns the number of bytes streamed, or a negative PM3_E* on failure. //