diff --git a/CHANGELOG.md b/CHANGELOG.md index f552afc85..5b86a6320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac ## [unreleased][unreleased] - Fixed Proxmark5 (PM5/AT32) failing to re-enter the bootloader on the software-reset flash path: the ERTC had no clock source selected, so the battery-domain (BPR) write-protection register write stalled the APB at the full clock and left USB dead until a physical replug; the ERTC is now given a clock before the BPR write (@nemanjan00) +- Fixed `hw fpga config` on Proxmark5 (PM5/AT32) needing a prior reader command (e.g. `hf 14a read`) to work: it now powers on the FPGA (24MHz clock) itself at the start of the config, before driving the JTAG upload (@nemanjan00) +- Fixed `hf mfdes chk` to correctly check all provided keys instead of only checking a small portion (@corollary-de). +- Fixed `hf iclass unhash` omitting valid hash0 pre-images, which could make the subsequent hashcat DES crack unable to find the master key (@trichimtrich) +- Fixed `hf iclass unhash` returning no pre-images at all for some keys, caused by `check()` not being invertible in a single reading (@trichimtrich) +- Added `--rgb` option to `hf tune` / `lf tune` (Proxmark5/PM5): mirrors the antenna tuning level on the antenna RGB LED (blue=low, green=mid, red=high, tracking the on-screen bar), for locating tags/implants by feel without watching the screen. Colour is computed client-side; the device gets a new `CMD_PM5_RGB_SET` handled by a dedicated AT32 RGB HAL module (`common_arm/rgb`), so other platforms are unaffected (@nemanjan00) +- Fixed `hw version` / `hw status` showing a bogus FPGA image (`fpga_pm3_hf.ncd image 2s30vq100`) on Proxmark5 (PM5/AT32): PM5's Gowin FPGA bitstream is loaded externally and not compiled in, so the built-in Xilinx version info is meaningless there; the `[ FPGA ]` section (and the client FPGA "chip mismatch" check) are now suppressed for PM5 (@nemanjan00) +- Fixed `hw status` [Model] section reporting "PM3 GENERIC" firmware on Proxmark5 (PM5/AT32); it now reports `PM5` (@nemanjan00) +- Added Proxmark5 (PM5/AT32) reporting in `hw version` / `hw status`: correct MCU (AT32F437), flash size and `PM5` target, instead of the AT91-decoded "Unknown / 32 KB / PM3 GENERIC". The device now also reports its on-chip flash size, appended to the `CMD_VERSION` reply in a backward-compatible way (@nemanjan00) - Fixed `hf mf dump` preserving readable sector trailer Key B data instead of overwriting it with values from the supplied key file (@oSPANNERo) - Changed `magic_cards_notes.md` - documented the USCUID-UL helper scripts (`hf_mfu_uscuid` / `hf_mf_uscuid_prog`) and how to set the tag signature, replacing the outdated "No implemented commands" note (@c-barron) - Fixed `hf_mf_uscuid_prog.lua` - corrected the script name shown in its usage text (@c-barron) diff --git a/Makefile.platform.sample b/Makefile.platform.sample index b8850e76d..cf51ab424 100644 --- a/Makefile.platform.sample +++ b/Makefile.platform.sample @@ -18,6 +18,10 @@ PLATFORM=PM3RDV4 # uncomment the line below #PLATFORM=PM3ULTIMATE +# For Proxmark5 (PM5, Artery AT32F435/437): +# uncomment the line below +#PLATFORM=PM5 + # If you want more than one PLATFORM_EXTRAS option, separate them by spaces: #PLATFORM_EXTRAS=BTADDON #PLATFORM_EXTRAS=FLASH diff --git a/armsrc/CMakeLists.txt b/armsrc/CMakeLists.txt index 2c2574b20..19d170d86 100644 --- a/armsrc/CMakeLists.txt +++ b/armsrc/CMakeLists.txt @@ -37,6 +37,7 @@ set(INC_DIRS ../include ../common_arm ../common_arm/rssi + ../common_arm/rgb ../common_arm/ticks ../common_arm/usb ../common_arm/flash_data @@ -57,6 +58,7 @@ if (PM5) set(SRC_GPIO ../common_arm/gpio/gpio_hw_at32.c) set(SRC_WDT ../common_arm/wdt/wdt_hw_at32.c) set(SRC_RSSI ../common_arm/rssi/rssi_hw_at32.c) + set(SRC_RGB ../common_arm/rgb/rgb_hw_at32.c) set(SRC_SYS ../common_arm/sys/sys_hw_at32.c) set(SRC_FLASH_DATA ../common_arm/flash_data/flashmem_hw_at32.c) set(SRC_USB_CDC ../common_arm/usb/usb_cdc_at32.c) @@ -67,6 +69,7 @@ else () set(SRC_GPIO ../common_arm/gpio/gpio_hw_at91.c) set(SRC_WDT ../common_arm/wdt/wdt_hw_at91.c) set(SRC_RSSI ../common_arm/rssi/rssi_hw_at91.c) + set(SRC_RGB "") set(SRC_SYS ../common_arm/sys/sys_hw_at91.c) set(SRC_FLASH_DATA ../common_arm/flash_data/flashmem_hw_at91.c) set(SRC_USB_CDC ../common_arm/usb/usb_cdc_at91.c) @@ -268,6 +271,7 @@ set(THUMBSRC string.c BigBuf.c ${SRC_RSSI} + ${SRC_RGB} ${SRC_TICKS} ${SRC_GPIO} ${SRC_WDT} diff --git a/armsrc/Makefile b/armsrc/Makefile index 9be51e4f2..4be839947 100644 --- a/armsrc/Makefile +++ b/armsrc/Makefile @@ -45,6 +45,7 @@ ifeq ($(PLATFORM),PM5) SRC_GPIO = ../common_arm/gpio/gpio_hw_at32.c SRC_WDT = ../common_arm/wdt/wdt_hw_at32.c SRC_RSSI = ../common_arm/rssi/rssi_hw_at32.c + SRC_RGB = ../common_arm/rgb/rgb_hw_at32.c SRC_SYS = ../common_arm/sys/sys_hw_at32.c SRC_FPGA = ../common_arm/fpga/fpga_hw_at32.c ../common_arm/fpga/fpga_gw_jtag.c SRC_USB_CDC = ../common_arm/usb/usb_cdc_at32.c @@ -60,6 +61,7 @@ else SRC_GPIO = ../common_arm/gpio/gpio_hw_at91.c SRC_WDT = ../common_arm/wdt/wdt_hw_at91.c SRC_RSSI = ../common_arm/rssi/rssi_hw_at91.c + SRC_RGB = SRC_SYS = ../common_arm/sys/sys_hw_at91.c SRC_FPGA = ../common_arm/fpga/fpga_hw_at91.c SRC_USB_CDC = ../common_arm/usb/usb_cdc_at91.c @@ -177,6 +179,7 @@ THUMBSRC = start.c \ $(SRC_STANDALONE) \ $(SRC_ZX) \ $(SRC_RSSI) \ + $(SRC_RGB) \ $(SRC_TICKS) \ $(SRC_GPIO) \ $(SRC_WDT) \ diff --git a/armsrc/Standalone/hf_cardhopper.c b/armsrc/Standalone/hf_cardhopper.c index cc600a865..5b2ba3cae 100644 --- a/armsrc/Standalone/hf_cardhopper.c +++ b/armsrc/Standalone/hf_cardhopper.c @@ -30,6 +30,10 @@ #include "usart.h" #include "cmd.h" #include "usb_cdc_apis.h" +#include "usb_read_ng.h" + +// To enable CARDHOPPER_USB, add the following line in your Makefile.platform +// STANDALONE_PLATFORM_DEFS+=-DCARDHOPPER_USB #ifdef CARDHOPPER_USB #define cardhopper_write usb_write diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 9c2ab9f78..56ad5970f 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -29,6 +29,7 @@ #include "fpga_loader.h" #include "fpga_apis.h" #include "rssi_apis.h" +#include "rgb_apis.h" #include "string.h" #include "printf.h" #include "legicrf.h" @@ -362,6 +363,11 @@ static void SendVersion(void) { strncat(VersionString, " Compiler... GCC "__VERSION__"\n", sizeof(VersionString) - strlen(VersionString) - 1); #endif +#ifndef PM5 + // PM5's FPGA (Gowin) bitstream is loaded at runtime via `hw fpga config` and is + // not compiled into the firmware, so there is no meaningful built-in FPGA + // version to report here. g_fpga_version_information[] describes the Xilinx + // bitstream that PM5 does not run, so omit the section entirely on PM5. strncat(VersionString, "\n [ "_YELLOW_("FPGA")" ] \n ", sizeof(VersionString) - strlen(VersionString) - 1); for (int i = 0; i < g_fpga_bitstream_num; i++) { @@ -370,6 +376,7 @@ static void SendVersion(void) { strncat(VersionString, "\n ", sizeof(VersionString) - strlen(VersionString) - 1); } } +#endif #ifdef WITH_COMPRESSION // Send Chip ID and used flash memory uint32_t text_and_rodata_section_size = (uint32_t)__data_src_start__ - (uint32_t)_flash_start; @@ -396,7 +403,21 @@ static void SendVersion(void) { payload.versionstr_len = strlen(VersionString) + 1; memcpy(payload.versionstr, VersionString, payload.versionstr_len); - reply_ng(CMD_VERSION, PM3_SUCCESS, (uint8_t *)&payload, 12 + payload.versionstr_len); + uint32_t reply_len = 12 + payload.versionstr_len; + + // Append the total on-chip flash size (bytes) AFTER the version string. This is + // backward compatible: older clients stop at versionstr and ignore the trailing + // bytes, and this stays valid when talking to older firmware that omits it. It + // lets the client report memory usage on MCUs whose size can't be derived from + // the chip id (e.g. AT32). Keep the header layout unchanged (do not break the + // CMD_VERSION protocol). + if (reply_len + sizeof(uint32_t) <= sizeof(payload)) { + uint32_t flash_size = GetChipFlashSize(); + memcpy(payload.versionstr + payload.versionstr_len, &flash_size, sizeof(flash_size)); + reply_len += sizeof(flash_size); + } + + reply_ng(CMD_VERSION, PM3_SUCCESS, (uint8_t *)&payload, reply_len); } #ifdef CHIP_AT91SAM7S // Only AT91SAM7S chip series need calibration. @@ -3603,6 +3624,18 @@ static void PacketReceived(PacketCommandNG *packet) { reply_ng(CMD_PM5_QC_TEST, QCTestPM5(&failed_item) ? PM3_SUCCESS : PM3_EFAILED, &failed_item, 1); break; } + case CMD_PM5_RGB_SET: { + // Set the antenna RGB LED colour (used by `hf/lf tune --rgb`). + struct p { + uint8_t r; + uint8_t g; + uint8_t b; + } PACKED; + struct p *payload = (struct p *)packet->data.asBytes; + RgbLedSet(payload->r, payload->g, payload->b); + reply_ng(CMD_PM5_RGB_SET, PM3_SUCCESS, NULL, 0); + break; + } #endif default: { Dbprintf("%s: 0x%04x", "unknown command:", packet->cmd); diff --git a/client/cmdscripts/rdv4_init_extflash.cmd b/client/cmdscripts/rdv4_init_extflash.cmd index 243f66096..27e73fcd1 100755 --- a/client/cmdscripts/rdv4_init_extflash.cmd +++ b/client/cmdscripts/rdv4_init_extflash.cmd @@ -4,4 +4,8 @@ mem load -f mfc_default_keys --mfc mem load -f t55xx_default_pwds --t55xx mem load -f iclass_default_keys --iclass mem load -f mfulc_default_keys --ulc -lf t55xx deviceconfig -z -p +mem load -f mfulaes_default_keys --aes +lf t55xx deviceconfig --r0 -a 29 -b 17 -c 15 -d 47 -e 15 -p +lf t55xx deviceconfig --r1 -a 29 -b 17 -c 18 -d 50 -e 15 -p +lf t55xx deviceconfig --r2 -a 29 -b 17 -c 18 -d 40 -e 15 -p +lf t55xx deviceconfig --r3 -a 29 -b 17 -c 15 -d 31 -e 15 -f 47 -g 63 -p diff --git a/client/luascripts/init_rdv4.lua b/client/luascripts/init_rdv4.lua index a5d68829c..9fb92f45e 100644 --- a/client/luascripts/init_rdv4.lua +++ b/client/luascripts/init_rdv4.lua @@ -79,6 +79,7 @@ function main(args) core.console('mem load -f t55xx_default_pwds --t55xx') core.console('mem load -f iclass_default_keys --iclass') core.console('mem load -f mfulc_default_keys --ulc') + core.console('mem load -f mfulaes_default_keys --aes') print(dash) -- T55x7 Device configuration diff --git a/client/luascripts/lf_ht2_paxton.lau b/client/luascripts/lf_ht2_paxton.lua similarity index 100% rename from client/luascripts/lf_ht2_paxton.lau rename to client/luascripts/lf_ht2_paxton.lua diff --git a/client/src/cmdhf.c b/client/src/cmdhf.c index 3bd5fed91..b5c22369f 100644 --- a/client/src/cmdhf.c +++ b/client/src/cmdhf.c @@ -21,6 +21,7 @@ #include "cmdparser.h" // command_t #include "cliparser.h" // parse #include "comms.h" // clearCommandBuffer +#include "util.h" // set_rgb #include "lfdemod.h" // computeSignalProperties #include "cmdhf14a.h" // ISO14443-A #include "cmdhf14b.h" // ISO14443-B @@ -298,6 +299,22 @@ int CmdHFSearch(const char *Cmd) { return res; } +// Mirror an antenna tuning level on the PM5 antenna RGB LED. `volt` is scaled +// relative to the running peak (`v_max`), so the colour tracks the on-screen bar: +// blue = low, green = mid, red = high. +static void tune_rgb_update(uint32_t volt, uint32_t v_max) { + uint32_t t = (v_max > 0) ? (volt * 510 / v_max) : 0; + if (t > 510) { + t = 510; + } + if (t < 255) { // blue -> green + set_rgb(0, (uint8_t)t, (uint8_t)(255 - t)); + } else { // green -> red + t -= 255; + set_rgb((uint8_t)t, (uint8_t)(255 - t), 0); + } +} + int CmdHFTune(const char *Cmd) { CLIParserContext *ctx; @@ -315,6 +332,7 @@ int CmdHFTune(const char *Cmd) { arg_lit0(NULL, "mix", "mixed style"), arg_lit0(NULL, "value", "values style"), arg_lit0("v", "verbose", "verbose output"), + arg_lit0(NULL, "rgb", "(PM5) mirror the tuning level on the antenna RGB LED"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -323,8 +341,14 @@ int CmdHFTune(const char *Cmd) { bool is_mix = arg_get_lit(ctx, 3); bool is_value = arg_get_lit(ctx, 4); bool verbose = arg_get_lit(ctx, 5); + bool use_rgb = arg_get_lit(ctx, 6); CLIParserFree(ctx); + if (use_rgb && (IfPm5() == false)) { + PrintAndLogEx(WARNING, "`--rgb` is only supported on Proxmark5; ignoring"); + use_rgb = false; + } + if ((is_bar + is_mix + is_value) > 1) { PrintAndLogEx(ERR, "Select only one output style"); return PM3_EINVARG; @@ -397,6 +421,12 @@ int CmdHFTune(const char *Cmd) { v_count++; } print_progress(volt, v_max, style); + if (use_rgb) { + tune_rgb_update(volt, v_max); + } + } + if (use_rgb) { + set_rgb(0, 0, 0); // turn the LED off on exit } mode[0] = 3; diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 874b2a0b7..64fa8e85c 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1258,7 +1258,7 @@ static void DesFill2bPattern( memcpy(&k3kkeyList[*k3kkeyListLen][2], &k3kkeyList[*k3kkeyListLen][0], 2); memcpy(&k3kkeyList[*k3kkeyListLen][4], &k3kkeyList[*k3kkeyListLen][0], 4); memcpy(&k3kkeyList[*k3kkeyListLen][8], &k3kkeyList[*k3kkeyListLen][0], 8); - memcpy(&k3kkeyList[*k3kkeyListLen][16], &k3kkeyList[*k3kkeyListLen][0], 4); + memcpy(&k3kkeyList[*k3kkeyListLen][16], &k3kkeyList[*k3kkeyListLen][0], 8); (*k3kkeyListLen)++; } @@ -1639,56 +1639,6 @@ static int CmdHF14aDesChk(const char *Cmd) { k3kkeyListLen = 0x100; } - // 2-byte pattern search mode - if (pattern2b) { - DesFill2bPattern(deskeyList, &deskeyListLen, aeskeyList, &aeskeyListLen, k3kkeyList, &k3kkeyListLen, &startPattern); - } - - // dictionary mode - size_t endFilePosition = 0; - if (dict_filenamelen) { - - res = loadFileDICTIONARYEx((char *)dict_filename, deskeyList, sizeof(deskeyList), NULL, 8, &deskeyListLen, 0, &endFilePosition, true); - if (res == PM3_SUCCESS && endFilePosition) { - PrintAndLogEx(SUCCESS, "First part of des dictionary successfully loaded."); - } - - endFilePosition = 0; - res = loadFileDICTIONARYEx((char *)dict_filename, aeskeyList, sizeof(aeskeyList), NULL, 16, &aeskeyListLen, 0, &endFilePosition, true); - if (res == PM3_SUCCESS && endFilePosition) { - PrintAndLogEx(SUCCESS, "First part of aes dictionary successfully loaded."); - } - - endFilePosition = 0; - res = loadFileDICTIONARYEx((char *)dict_filename, k3kkeyList, sizeof(k3kkeyList), NULL, 24, &k3kkeyListLen, 0, &endFilePosition, true); - if (res == PM3_SUCCESS && endFilePosition) { - PrintAndLogEx(SUCCESS, "First part of k3kdes dictionary successfully loaded."); - } - - endFilePosition = 0; - } - - if (aeskeyListLen == 0 && deskeyListLen == 0 && k3kkeyListLen == 0) { - PrintAndLogEx(ERR, "No keys provided. Nothing to check."); - return PM3_EINVARG; - } - - if (aeskeyListLen != 0) { - PrintAndLogEx(INFO, "Loaded " _YELLOW_("%"PRIu32) " aes keys", aeskeyListLen); - } - - if (deskeyListLen != 0) { - PrintAndLogEx(INFO, "Loaded " _YELLOW_("%"PRIu32) " des keys", deskeyListLen); - } - - if (k3kkeyListLen != 0) { - PrintAndLogEx(INFO, "Loaded " _YELLOW_("%"PRIu32) " k3kdes keys", k3kkeyListLen); - } - - if (verbose == false) { - PrintAndLogEx(INFO, "Search keys:"); - } - bool result = false; uint8_t app_ids[78] = {0}; size_t app_ids_len = 0; @@ -1724,53 +1674,131 @@ static int CmdHF14aDesChk(const char *Cmd) { app_ids_len = 3; } + { + uint32_t deskeyCountTotal = 0; + uint32_t aeskeyCountTotal = 0; + uint32_t k3kkeyCountTotal = 0; + + if (pattern1b) { + deskeyCountTotal = deskeyListLen; + aeskeyCountTotal = aeskeyListLen; + k3kkeyCountTotal = k3kkeyListLen; + } else if (pattern2b) { + deskeyCountTotal = 0x10000 - startPattern; + aeskeyCountTotal = 0x10000 - startPattern; + k3kkeyCountTotal = 0x10000 - startPattern; + } else if (dict_filenamelen) { + size_t readStart = 0; + size_t readEnd = 1; + uint32_t n = 0; + + while (readEnd != 0) { + loadFileDICTIONARYEx((char *)dict_filename, deskeyList, sizeof(deskeyList), NULL, 8, &n, readStart, &readEnd, false); + readStart = readEnd; + deskeyCountTotal += n; + } + + readStart = 0; + readEnd = 1; + while (readEnd != 0) { + loadFileDICTIONARYEx((char *)dict_filename, aeskeyList, sizeof(aeskeyList), NULL, 16, &n, readStart, &readEnd, false); + readStart = readEnd; + aeskeyCountTotal += n; + } + + readStart = 0; + readEnd = 1; + while (readEnd != 0) { + loadFileDICTIONARYEx((char *)dict_filename, k3kkeyList, sizeof(k3kkeyList), NULL, 24, &n, readStart, &readEnd, false); + readStart = readEnd; + k3kkeyCountTotal += n; + } + } + + if (deskeyCountTotal > 0) + PrintAndLogEx(INFO, "Loaded " _YELLOW_("%"PRIu32) " des keys", deskeyCountTotal); + if (aeskeyCountTotal > 0) + PrintAndLogEx(INFO, "Loaded " _YELLOW_("%"PRIu32) " aes keys", aeskeyCountTotal); + if (k3kkeyCountTotal > 0) + PrintAndLogEx(INFO, "Loaded " _YELLOW_("%"PRIu32) " k3kdes keys", k3kkeyCountTotal); + + if (deskeyCountTotal + aeskeyCountTotal + k3kkeyCountTotal == 0) { + PrintAndLogEx(ERR, "No keys provided. Nothing to check."); + return PM3_EINVARG; + } + } + for (uint32_t x = 0; x < app_ids_len / 3; x++) { uint32_t curaid = (app_ids[x * 3] & 0xFF) + ((app_ids[(x * 3) + 1] & 0xFF) << 8) + ((app_ids[(x * 3) + 2] & 0xFF) << 16); PrintAndLogEx(ERR, "Checking aid 0x%06X...", curaid); - res = AuthCheckDesfire(&dctx, secureChannel, &app_ids[x * 3], deskeyList, deskeyListLen, aeskeyList, aeskeyListLen, k3kkeyList, k3kkeyListLen, cmdKDFAlgo, kdfInputLen, kdfInput, foundKeys, &result, (verbose == false)); - if (res == PM3_EOPABORTED) { - break; - } + bool loadedAllKeys = false; + size_t desReadStart = 0; + size_t desReadEnd = 1; + size_t aesReadStart = 0; + size_t aesReadEnd = 1; + size_t k3kReadStart = 0; + size_t k3kReadEnd = 1; + uint32_t pattern2bOffset = startPattern; - if (pattern2b && startPattern < 0x10000) { - if (verbose == false) { - PrintAndLogEx(NORMAL, "p" NOLF); - } + while (!loadedAllKeys) { + bool foundKeyThisRound = false; - aeskeyListLen = 0; - deskeyListLen = 0; - k3kkeyListLen = 0; - DesFill2bPattern(deskeyList, &deskeyListLen, aeskeyList, &aeskeyListLen, k3kkeyList, &k3kkeyListLen, &startPattern); - continue; - } + if (pattern1b) { + loadedAllKeys = true; + } else if (pattern2b) { + if (pattern2bOffset < 0x10000) { + aeskeyListLen = 0; + deskeyListLen = 0; + k3kkeyListLen = 0; + DesFill2bPattern(deskeyList, &deskeyListLen, aeskeyList, &aeskeyListLen, k3kkeyList, &k3kkeyListLen, &pattern2bOffset); + } else { + loadedAllKeys = true; + } + } else if (dict_filenamelen) { + deskeyListLen = 0; + if (desReadEnd != 0) { + res = loadFileDICTIONARYEx((char *)dict_filename, deskeyList, sizeof(deskeyList), NULL, 8, &deskeyListLen, desReadStart, &desReadEnd, false); + if (res != PM3_SUCCESS) + desReadStart = desReadEnd; + } else { + // Every 16 byte or 24 byte key also gets read as a valid des key, so when desReadEnd == 0 there are absolutely no more keys of any kind left in the dictionary + loadedAllKeys = true; + } - if (dict_filenamelen) { - if (verbose == false) { - PrintAndLogEx(NORMAL, "d" NOLF); - } + aeskeyListLen = 0; + if (aesReadEnd != 0) { + res = loadFileDICTIONARYEx((char *)dict_filename, aeskeyList, sizeof(aeskeyList), NULL, 16, &aeskeyListLen, aesReadStart, &aesReadEnd, false); + if (res != PM3_SUCCESS) + aesReadStart = aesReadEnd; + } - uint32_t keycnt = 0; - res = loadFileDICTIONARYEx((char *)dict_filename, deskeyList, sizeof(deskeyList), NULL, 16, &keycnt, endFilePosition, &endFilePosition, false); - if (res == PM3_SUCCESS && endFilePosition) { - deskeyListLen = keycnt; - } + k3kkeyListLen = 0; + if (k3kReadEnd != 0) { + res = loadFileDICTIONARYEx((char *)dict_filename, k3kkeyList, sizeof(k3kkeyList), NULL, 24, &k3kkeyListLen, k3kReadStart, &k3kReadEnd, false); + if (res != PM3_SUCCESS) + k3kReadStart = k3kReadEnd; + } + } - keycnt = 0; - res = loadFileDICTIONARYEx((char *)dict_filename, aeskeyList, sizeof(aeskeyList), NULL, 16, &keycnt, endFilePosition, &endFilePosition, false); - if (res == PM3_SUCCESS && endFilePosition) { - aeskeyListLen = keycnt; - } + res = AuthCheckDesfire(&dctx, secureChannel, &app_ids[x * 3], deskeyList, deskeyListLen, aeskeyList, aeskeyListLen, k3kkeyList, k3kkeyListLen, cmdKDFAlgo, kdfInputLen, kdfInput, foundKeys, &foundKeyThisRound, verbose); + if (res == PM3_EOPABORTED) { + break; + } - keycnt = 0; - res = loadFileDICTIONARYEx((char *)dict_filename, k3kkeyList, sizeof(k3kkeyList), NULL, 16, &keycnt, endFilePosition, &endFilePosition, false); - if (res == PM3_SUCCESS && endFilePosition) { - k3kkeyListLen = keycnt; - } + result = result || foundKeyThisRound; - continue; - } + if (foundKeyThisRound == true && verbose == false) { + if (pattern1b || pattern2b) + PrintAndLogEx(NORMAL, "p" NOLF); + else if (dict_filenamelen) + PrintAndLogEx(NORMAL, "d" NOLF); + } + } + + if (!loadedAllKeys) + break; } if (verbose == false) { PrintAndLogEx(NORMAL, ""); diff --git a/client/src/cmdhfmfsen.c b/client/src/cmdhfmfsen.c index 08796a617..9b6d67294 100644 --- a/client/src/cmdhfmfsen.c +++ b/client/src/cmdhfmfsen.c @@ -943,44 +943,26 @@ static int fm11_generate_1nt_candidates(uint32_t uid, uint32_t nt, uint32_t nt_e return PM3_SUCCESS; } -static uint16_t fm11_i_lfsr16[1 << 16] = {0}; -static uint16_t fm11_s_lfsr16[1 << 16] = {0}; -static uint16_t fm11_prev8_lfsr16[1 << 16] = {0}; -static uint16_t fm11_prev14_lfsr16[1 << 16] = {0}; +static uint16_t fm11_prev8_lfsr16[1 << 16]; +static uint16_t fm11_prev14_lfsr16[1 << 16]; static bool fm11_lfsr16_ready = false; -static uint16_t fm11_prev_lfsr16(uint16_t nonce); - static void fm11_init_lfsr16_table(void) { if (fm11_lfsr16_ready) { return; } + uint16_t buffer[16] = { 0 }; uint16_t x = 1; - for (uint16_t i = 1; i; ++i) { - fm11_i_lfsr16[(x & 0xff) << 8 | x >> 8] = i; - fm11_s_lfsr16[i] = (x & 0xff) << 8 | x >> 8; + for (uint32_t i = 0; i < 65536 + 16; ++i) { + fm11_prev8_lfsr16[buffer[(i + 8) % 16]] = buffer[i % 16]; + fm11_prev14_lfsr16[buffer[(i + 14) % 16]] = buffer[i % 16]; + x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15; - } - for (uint32_t nonce = 0; nonce <= UINT16_MAX; nonce++) { - uint16_t p = nonce; - for (uint8_t i = 0; i < 8; i++) { - p = fm11_prev_lfsr16(p); - } - fm11_prev8_lfsr16[nonce] = p; - for (uint8_t i = 8; i < 14; i++) { - p = fm11_prev_lfsr16(p); - } - fm11_prev14_lfsr16[nonce] = p; + buffer[i % 16] = (x & 0xFF) << 8 | x >> 8; } fm11_lfsr16_ready = true; } -static uint16_t fm11_prev_lfsr16(uint16_t nonce) { - uint16_t i = fm11_i_lfsr16[nonce]; - i = (i == 1) ? 0xffff : i - 1; - return fm11_s_lfsr16[i]; -} - static uint16_t fm11_compute_seednt16_nt32(uint32_t nt32, uint64_t key) { static const uint8_t a[] = {0, 8, 9, 4, 6, 11, 1, 15, 12, 5, 2, 13, 10, 14, 3, 7}; static const uint8_t b[] = {0, 13, 1, 14, 4, 10, 15, 7, 5, 3, 8, 6, 9, 2, 12, 11}; @@ -2295,13 +2277,6 @@ static int fm11_check_default_keys(uint32_t uid, return PM3_EMALLOC; } - bool was_found[FM11RF08S_SECTORS][2] = {{false}}; - for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { - for (uint8_t kt = 0; kt < 2; kt++) { - was_found[sec][kt] = found_key[sec][kt]; - } - } - for (uint8_t sec = 0; sec < FM11RF08S_NORMAL_SECTORS; sec++) { for (uint8_t kt = 0; kt < 2; kt++) { if (found_key[sec][kt]) { @@ -2378,8 +2353,9 @@ normal_out: fm11_sen_keycheck_progress("def", real_sec32, kt, sec32_done, sec32_total, matches.count); progress_shown = true; if (res == PM3_SUCCESS) { - keys_found[sec32][kt] = out_key & 0xFFFFFFFFFFFFULL; + keys_found[sec32][kt] = out_key; found_key[sec32][kt] = true; + fm11_print_key_hit_row(nonce_count, sec32, kt, keys_found[sec32][kt], found_key); break; } if (res == PM3_ETIMEOUT || res == PM3_EOPABORTED) { @@ -2394,18 +2370,14 @@ normal_out: for (uint8_t sec = 0; sec < FM11RF08S_NORMAL_SECTORS; sec++) { for (uint8_t kt = 0; kt < 2; kt++) { + if (found_key[sec][kt]) { + continue; + } if (e_sector[sec].foundKey[kt]) { - keys_found[sec][kt] = e_sector[sec].Key[kt] & 0xFFFFFFFFFFFFULL; + keys_found[sec][kt] = e_sector[sec].Key[kt]; found_key[sec][kt] = true; - if (was_found[sec][kt] == false) { - fm11_print_key_hit_row(nonce_count, sec, kt, keys_found[sec][kt], found_key); - } + fm11_print_key_hit_row(nonce_count, sec, kt, keys_found[sec][kt], found_key); } - } - } - for (uint8_t kt = 0; kt < 2; kt++) { - if (found_key[sec32][kt] && was_found[sec32][kt] == false) { - fm11_print_key_hit_row(nonce_count, sec32, kt, keys_found[sec32][kt], found_key); } } free(e_sector); @@ -2467,11 +2439,11 @@ static uint32_t fm11_propagate_key_reuse_online(uint32_t nonce_count, uint64_t k int res = mf_check_keys(mfFirstBlockOfSector(real_sec32), kt, false, 1, key_block, &out_key); fm11_sen_keycheck_progress("use", real_sec32, kt, (pass_total * 2) + kt + 1, (pass_total * 2) + 2, pass_total + 2); progress_shown = true; - if (res == PM3_SUCCESS && ((out_key & 0xFFFFFFFFFFFFULL) == key)) { + if (res == PM3_SUCCESS && out_key == key) { keys_found[sec32][kt] = key; found_key[sec32][kt] = true; newly_found++; - fm11_print_key_hit_row(nonce_count, sec32, kt, key, found_key); + fm11_print_key_hit_row(nonce_count, sec32, kt, keys_found[sec32][kt], found_key); } } if (progress_shown) { @@ -2480,11 +2452,14 @@ static uint32_t fm11_propagate_key_reuse_online(uint32_t nonce_count, uint64_t k for (uint8_t sec = 0; sec < FM11RF08S_NORMAL_SECTORS; sec++) { for (uint8_t kt = 0; kt < 2; kt++) { - if (e_sector[sec].foundKey[kt] && found_key[sec][kt] == false) { + if (found_key[sec][kt]) { + continue; + } + if (e_sector[sec].foundKey[kt] && e_sector[sec].Key[kt] == key) { keys_found[sec][kt] = key; found_key[sec][kt] = true; newly_found++; - fm11_print_key_hit_row(nonce_count, sec, kt, key, found_key); + fm11_print_key_hit_row(nonce_count, sec, kt, keys_found[sec][kt], found_key); } } } @@ -2626,6 +2601,9 @@ static int fm11_verify_candidates(uint8_t real_sec, uint8_t key_type, const fm11 uint32_t last_progress = 0; fm11_sen_candidate_progress(real_sec, key_type, 0, list->count); while (idx < list->count) { + if (kbd_enter_pressed()) { + return PM3_EOPABORTED; + } uint8_t key_block[KEYBLOCK_SIZE] = {0}; uint8_t chunk = MIN(KEYS_IN_BLOCK, list->count - idx); for (uint8_t i = 0; i < chunk; i++) { @@ -3071,8 +3049,6 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } } } - { - } uint32_t total_candidates = 0; for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index b0d3f50f0..d2671d105 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -53,7 +53,20 @@ static int CmdHelp(const char *Cmd); -static void lookup_chipid_short(uint32_t iChipID, uint32_t mem_used) { +static void lookup_chipid_short(uint32_t iChipID, uint32_t mem_used, uint32_t flash_size) { + // AT32 (PM5): the chip id is an ARM DBGMCU IDCODE, not an Atmel CIDR, so the + // AT91 decode below does not apply (it would print "Unknown" and a bogus flash + // size). Report the MCU and use the real flash size the device reported. + if (IfPm5()) { + PrintAndLogEx(NORMAL, " MCU....... " _YELLOW_("%s"), "AT32F437"); + uint32_t mem_kb = flash_size / 1024; + PrintAndLogEx(NORMAL, " Memory.... " _YELLOW_("%u") " KB ( " _YELLOW_("%2.0f%%") " used )" + , mem_kb + , mem_kb == 0 ? 0.0f : (float)mem_used / (mem_kb * 1024) * 100 + ); + return; + } + const char *asBuff; switch (iChipID) { case 0x270B0A40: @@ -157,11 +170,24 @@ static void lookup_chipid_short(uint32_t iChipID, uint32_t mem_used) { ); } -static void lookupChipID(uint32_t iChipID, uint32_t mem_used) { +static void lookupChipID(uint32_t iChipID, uint32_t mem_used, uint32_t flash_size) { const char *asBuff; uint32_t mem_avail = 0; PrintAndLogEx(NORMAL, "\n [ " _YELLOW_("Hardware") " ]"); + // AT32 (PM5): the chip id is an ARM DBGMCU IDCODE, not an Atmel CIDR, so the + // verbose AT91 decode below does not apply. Print a short AT32 summary instead. + if (IfPm5()) { + PrintAndLogEx(NORMAL, " --= uC: AT32F437"); + uint32_t mem_kb = flash_size / 1024; + PrintAndLogEx(NORMAL, " --= Nonvolatile Program Memory Size: %u KB, Used: %u bytes (%2.0f%%)" + , mem_kb + , mem_used + , mem_kb == 0 ? 0.0f : (float)mem_used / (mem_kb * 1024) * 100 + ); + return; + } + switch (iChipID) { case 0x270B0A40: asBuff = "AT91SAM7S512 Rev A"; @@ -2029,9 +2055,18 @@ void pm3_version_short(void) { struct p *payload = (struct p *)&resp.data.asBytes; - lookup_chipid_short(payload->id, payload->section_size); + // Flash size (bytes) is appended after the version string by newer + // firmware; 0 if the device didn't send it (older firmware). + uint32_t flash_size = 0; + if (resp.length >= 12 + payload->versionstr_len + sizeof(uint32_t)) { + memcpy(&flash_size, payload->versionstr + payload->versionstr_len, sizeof(flash_size)); + } - if (IfPm3Rdv4Fw()) { + lookup_chipid_short(payload->id, payload->section_size, flash_size); + + if (IfPm5()) { + PrintAndLogEx(NORMAL, " Target.... %s", _YELLOW_("PM5")); + } else if (IfPm3Rdv4Fw()) { // validate signature data rdv40_validation_t mem; @@ -2168,7 +2203,10 @@ void pm3_version(bool verbose, bool oneliner) { SendCommandNG(CMD_VERSION, NULL, 0); if (WaitForResponseTimeout(CMD_VERSION, &resp, 1000)) { - if (IfPm3Rdv4Fw()) { + if (IfPm5()) { + PrintAndLogEx(NORMAL, " Firmware.................. " _GREEN_("PM5")); + PrintAndLogEx(NORMAL, " External flash............ %s", IfPm3Flash() ? _GREEN_("present") : _YELLOW_("absent")); + } else if (IfPm3Rdv4Fw()) { // validate signature data rdv40_validation_t mem; @@ -2230,11 +2268,20 @@ void pm3_version(bool verbose, bool oneliner) { } } PrintAndLogEx(NORMAL, payload->versionstr); - if (strstr(payload->versionstr, FPGA_TYPE) == NULL) { + // PM5 doesn't report a built-in FPGA version (Gowin bitstream is loaded + // externally), so skip the Xilinx FPGA_TYPE match check for it. + if (!IfPm5() && strstr(payload->versionstr, FPGA_TYPE) == NULL) { PrintAndLogEx(NORMAL, " FPGA firmware... %s", _RED_("chip mismatch")); } - lookupChipID(payload->id, payload->section_size); + // Flash size (bytes) is appended after the version string by newer + // firmware; 0 if the device didn't send it (older firmware). + uint32_t flash_size = 0; + if (resp.length >= 12 + payload->versionstr_len + sizeof(uint32_t)) { + memcpy(&flash_size, payload->versionstr + payload->versionstr_len, sizeof(flash_size)); + } + + lookupChipID(payload->id, payload->section_size, flash_size); // Get unique id of mainchip clearCommandBuffer(); diff --git a/client/src/cmdlf.c b/client/src/cmdlf.c index 9d6b59cad..988ccd473 100644 --- a/client/src/cmdlf.c +++ b/client/src/cmdlf.c @@ -28,6 +28,7 @@ #include "cmdparser.h" // command_t #include "comms.h" +#include "util.h" // set_rgb #include "commonutil.h" // ARRAYLEN #include "lfdemod.h" // device/client demods of LF signals #include "ui.h" // for show graph controls @@ -107,6 +108,22 @@ int lfsim_wait_check(uint32_t cmd) { return PM3_SUCCESS; } +// Mirror an antenna tuning level on the PM5 antenna RGB LED. `volt` is scaled +// relative to the running peak (`v_max`), so the colour tracks the on-screen bar: +// blue = low, green = mid, red = high. +static void tune_rgb_update(uint32_t volt, uint32_t v_max) { + uint32_t t = (v_max > 0) ? (volt * 510 / v_max) : 0; + if (t > 510) { + t = 510; + } + if (t < 255) { // blue -> green + set_rgb(0, (uint8_t)t, (uint8_t)(255 - t)); + } else { // green -> red + t -= 255; + set_rgb((uint8_t)t, (uint8_t)(255 - t), 0); + } +} + static int CmdLFTune(const char *Cmd) { CLIParserContext *ctx; @@ -128,6 +145,7 @@ static int CmdLFTune(const char *Cmd) { arg_lit0(NULL, "mix", "mixed style"), arg_lit0(NULL, "value", "values style"), arg_lit0("v", "verbose", "verbose output"), + arg_lit0(NULL, "rgb", "(PM5) mirror the tuning level on the antenna RGB LED"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -139,8 +157,14 @@ static int CmdLFTune(const char *Cmd) { bool is_mix = arg_get_lit(ctx, 5); bool is_value = arg_get_lit(ctx, 6); bool verbose = arg_get_lit(ctx, 7); + bool use_rgb = arg_get_lit(ctx, 8); CLIParserFree(ctx); + if (use_rgb && (IfPm5() == false)) { + PrintAndLogEx(WARNING, "`--rgb` is only supported on Proxmark5; ignoring"); + use_rgb = false; + } + if (divisor < 19) { PrintAndLogEx(ERR, "divisor must be between 19 and 255"); return PM3_EINVARG; @@ -232,6 +256,12 @@ static int CmdLFTune(const char *Cmd) { v_count++; } print_progress(volt, v_max, style); + if (use_rgb) { + tune_rgb_update(volt, v_max); + } + } + if (use_rgb) { + set_rgb(0, 0, 0); // turn the LED off on exit } params[0] = 3; diff --git a/client/src/cmdparser.c b/client/src/cmdparser.c index 2341d44ae..b4e57775a 100644 --- a/client/src/cmdparser.c +++ b/client/src/cmdparser.c @@ -30,7 +30,7 @@ # include "pthread_spin_lock_shim.h" // spinlock shim for OSX .. #endif -#ifdef __ANDROID__ +#if defined(__ANDROID__) && __ANDROID_API__ < 24 //Spinlock patch for building with Android NDK typedef pthread_mutex_t pthread_spinlock_t; diff --git a/client/src/loclass/ikeys.c b/client/src/loclass/ikeys.c index b50699e9e..f195a8cd0 100644 --- a/client/src/loclass/ikeys.c +++ b/client/src/loclass/ikeys.c @@ -201,50 +201,110 @@ static uint64_t check(uint64_t z) { } // Reverse ck (scramble-1) -static uint64_t reverse_ck(int i, int j, uint64_t z) { - if (i == 1 && j == -1) { - return z; - } else if (j == -1) { - return reverse_ck(i - 1, i - 2, z); - } +// +// ck() rewrites a repeated value as the *index* it matched, so a stored value in +// 0..3 is ambiguous: it may be such a marker, or a genuine value that happens to +// be small. The inverse therefore has to enumerate, not pick one reading. +// +// ck() reduces to: for i = 3, 2, 1 (in that order) +// +// v = z[i]; for j = i-1 down to 0: if (v == z[j]) v = j; out[i] = v; +// +// where every z[j] on the right is still the *original* value, because z[j] is +// only ever rewritten once j becomes the outer index, which happens later. So +// out[i] depends solely on the original z[i] and z[0..i-1], and the halves can +// be inverted index by index, resolving z[0], then z[1], and so on. - uint64_t newz = 0; - if (getSixBitByte(z, i) == j) { // Reverse the swap logic based on condition in scramble^{-1} - // Perform reverse swap - for (int c = 0; c < 4; c++) { - uint8_t val = getSixBitByte(z, c); - if (c == i) { - pushbackSixBitByte(&newz, getSixBitByte(z, j), c); - } else { - pushbackSixBitByte(&newz, val, c); +#define CK_MAX_PREIMAGES 64 + +// All v with: v0 = v; for j = i-1 down to 0: if (v0 == zs[j]) v0 = j; giving t. +// Undoes the steps in reverse order, branching wherever both readings are possible. +static int invert_ck_index(uint8_t t, const uint8_t *zs, int i, uint8_t *out, int max) { + uint8_t cur[CK_MAX_PREIMAGES], nxt[CK_MAX_PREIMAGES]; + int n = 0, m; + cur[n++] = t; + + for (int j = 0; j < i; j++) { + m = 0; + for (int a = 0; a < n; a++) { + uint8_t va = cur[a]; + if (va == j) { // the substitution happened + uint8_t v = zs[j]; + bool dup = false; + for (int d = 0; d < m; d++) if (nxt[d] == v) dup = true; + if (!dup && m < CK_MAX_PREIMAGES) nxt[m++] = v; + } + if (va != zs[j]) { // no substitution at this step + bool dup = false; + for (int d = 0; d < m; d++) if (nxt[d] == va) dup = true; + if (!dup && m < CK_MAX_PREIMAGES) nxt[m++] = va; } } - return reverse_ck(i, j - 1, newz); - } else { - // Continue recursion - return reverse_ck(i, j - 1, z); + memcpy(cur, nxt, m); + n = m; + if (n == 0) break; } + + int count = 0; + for (int a = 0; a < n && count < max; a++) + if (cur[a] < 0x40) out[count++] = cur[a]; + return count; } -static uint64_t reverse_check(uint64_t z) { +// Every four-value half that ck(3, 2, .) maps to o[]. Returns how many were written. +static int invert_ck_half(const uint8_t o[4], uint8_t (*out)[4], int max) { + uint8_t c1[CK_MAX_PREIMAGES], c2[CK_MAX_PREIMAGES], c3[CK_MAX_PREIMAGES]; + uint8_t zs[4]; + int count = 0; - //retrieve ck1 and ck2 from the hash + zs[0] = o[0]; // ck() never rewrites z[0] + int n1 = invert_ck_index(o[1], zs, 1, c1, CK_MAX_PREIMAGES); - // Step 1: Extract ck2 shifted part from result - // Assuming ck2 shifted part is from bits 0-23 of the result - uint64_t shifted_ck2_part = z & 0x0000000000FFFFFF; // Mask the lower 24 bits + for (int a = 0; a < n1 && count < max; a++) { + zs[1] = c1[a]; + int n2 = invert_ck_index(o[2], zs, 2, c2, CK_MAX_PREIMAGES); - // Step 2: Reconstruct ck2 - uint64_t ck2 = shifted_ck2_part << 24; // Shift back to get original ck2 value - // Step 3: Recover ck1 - uint64_t ck1 = z & ~(ck2 >> 24); // Clear the bits where ck2 affected the result - // Now ck1 and ck2 have their original values before (after ck function took place) + for (int b = 0; b < n2 && count < max; b++) { + zs[2] = c2[b]; + int n3 = invert_ck_index(o[3], zs, 3, c3, CK_MAX_PREIMAGES); - ck1 = reverse_ck(3, 2, ck1); - ck2 = reverse_ck(3, 2, ck2); + for (int d = 0; d < n3 && count < max; d++) { + out[count][0] = zs[0]; + out[count][1] = zs[1]; + out[count][2] = zs[2]; + out[count][3] = c3[d]; + count++; + } + } + } + return count; +} - return ck1 | ck2 >> 24; //This is now zP +// Every zP that check() maps to z. The two halves are independent, so the result +// is their cross product. Returns how many were written to out[]. +static int reverse_check_all(uint64_t z, uint64_t *out, int max) { + uint8_t h1[4], h2[4]; + for (int n = 0; n < 4; n++) { + h1[n] = getSixBitByte(z, n); + h2[n] = getSixBitByte(z, n + 4); + } + uint8_t p1[CK_MAX_PREIMAGES][4], p2[CK_MAX_PREIMAGES][4]; + int n1 = invert_ck_half(h1, p1, CK_MAX_PREIMAGES); + int n2 = invert_ck_half(h2, p2, CK_MAX_PREIMAGES); + + int count = 0; + for (int a = 0; a < n1 && count < max; a++) { + for (int b = 0; b < n2 && count < max; b++) { + uint64_t zP = 0; + for (int n = 0; n < 4; n++) { + pushbackSixBitByte(&zP, p1[a][n], n); + pushbackSixBitByte(&zP, p2[b][n], n + 4); + } + out[count++] = zP; + } + } + return count; } static void printState(const char *desc, uint64_t c) { @@ -527,7 +587,14 @@ void invert_hash0(uint8_t k[8]) { uint64_t zCaret_fixed = zCaret_fixed1 | zCaret_fixed2; if (g_debugMode > 0) printState("0|0|z^", zCaret_fixed); - uint64_t zP = reverse_check(zCaret_fixed); + // check() is not injective either -- see reverse_check_all() -- so this + // yields every zP that maps to zCaret_fixed, and each is followed through. + uint64_t zP_list[CK_MAX_PREIMAGES]; + int zP_count = reverse_check_all(zCaret_fixed, zP_list, CK_MAX_PREIMAGES); + + for (int zp_i = 0; zp_i < zP_count; zp_i++) { + + uint64_t zP = zP_list[zp_i]; if (g_debugMode > 0) printState("0|0|z'", zP); // reverse the modulo transformation in the hash0 function for the six-bit chunks @@ -545,8 +612,15 @@ void invert_hash0(uint8_t k[8]) { pushbackSixBitByte(&c, zn4, n + 4); } - // The Hydra: depending on their positions, values 0x00, 0x01, 0x02, 0x03, 0x3c, 0x3d, 0x3e, 0x3f can lead to additional pre-images. - // When these values are present we need to generate additional pre-images if they have the same modulo as other values + // The Hydra: hash0() reduces every six-bit z chunk modulo a value that + // depends on the chunk position: + // + // _zn = (zn % (63 - n)) + n for n = 0..3 + // _zn4 = (zn4 % (64 - n)) + n for n = 0..3 (chunks 4..7) + // + // Since the modulus is smaller than 0x40 the reduction is lossy, so a + // recovered residue r has a second pre-image at r + modulus whenever + // that value still fits in six bits. Each such chunk forks the search. // Initialize an array of pointers to uint64_t (start with one value, initialized to 0) uint64_t *hydra_heads = (uint64_t *)calloc(sizeof(uint64_t), 1); // Start with one uint64_t @@ -557,12 +631,17 @@ void invert_hash0(uint8_t k[8]) { hydra_heads[0] = 0; // Initialize first value to 0 int heads_count = 1; // Track number of forks - // Iterate 4 times as per the original loop for (int n = 0; n < 8; n++) { uint8_t hydra_head = getSixBitByte(c, n); - if (hydra_head <= (n % 4) || hydra_head >= 63 - (n % 4)) { + // the modulus hash0() applied to this chunk, mirroring the loop there + uint8_t modulus = (n < 4) ? (63 - n) : (64 - (n - 4)); + + // the only other value that reduces to the same residue + uint8_t alt_head = hydra_head + modulus; + + if (alt_head <= 0x3F) { // Create new forks by duplicating existing uint64_t values int new_head = heads_count * 2; @@ -576,46 +655,18 @@ void invert_hash0(uint8_t k[8]) { } hydra_heads = ptmp; - // Duplicate all current values and add the value to both original and new ones + // keep the residue in the existing branch, the alternative in the copy for (int i = 0; i < heads_count; i++) { - - // Duplicate current value hydra_heads[heads_count + i] = hydra_heads[i]; - uint8_t small_hydra_head = 0; - uint8_t big_hydra_head = 0; - uint8_t hydra_lil_spawns[4] = {0x00, 0x01, 0x02, 0x03}; - uint8_t hydra_big_spawns[4] = {0x3f, 0x3e, 0x3d, 0x3c}; - - if (hydra_head <= n % 4) { // check if is in the lower range - - // replace with big spawn in one hydra and keep small in another - small_hydra_head = hydra_head; - for (int fh = 0; fh < 4; fh++) { - if (hydra_lil_spawns[fh] == hydra_head) { - big_hydra_head = hydra_big_spawns[fh]; - } - } - - } else if (hydra_head >= 63 - (n % 4)) { // or the higher range - - // replace with small in one hydra and keep big in another - big_hydra_head = hydra_head; - for (int fh = 0; fh < 4; fh++) { - if (hydra_big_spawns[fh] == hydra_head) { - small_hydra_head = hydra_lil_spawns[fh]; - } - } - } - // Add to both original and duplicate values - pushbackSixBitByte(&hydra_heads[i], big_hydra_head, n); - pushbackSixBitByte(&hydra_heads[heads_count + i], small_hydra_head, n); + pushbackSixBitByte(&hydra_heads[i], hydra_head, n); + pushbackSixBitByte(&hydra_heads[heads_count + i], alt_head, n); } // Update the count of total values heads_count = new_head; } else { // no hydra head spawns for (int i = 0; i < heads_count; i++) { - pushbackSixBitByte(&hydra_heads[i], hydra_head, n);; + pushbackSixBitByte(&hydra_heads[i], hydra_head, n); } } } @@ -667,6 +718,8 @@ void invert_hash0(uint8_t k[8]) { } // Free allocated memory free(hydra_heads); + + } // for each zP candidate } } diff --git a/client/src/util.c b/client/src/util.c index 2e7627892..f6a556fd3 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -33,6 +33,8 @@ #include // Mingw #include "ui.h" // PrintAndLog +#include "comms.h" // SendCommandNG / WaitForResponseTimeout (set_rgb) +#include "pm3_cmd.h" // CMD_PM5_RGB_SET #define UTIL_BUFFER_SIZE_SPRINT 8196 // global client debug variable @@ -2048,3 +2050,15 @@ int str_copy_without_whitespace(const char *src, char *dst, size_t dst_size, siz *dst_len = out; return PM3_SUCCESS; } + +void set_rgb(uint8_t r, uint8_t g, uint8_t b) { + struct { + uint8_t r; + uint8_t g; + uint8_t b; + } PACKED payload = { r, g, b }; + clearCommandBuffer(); + SendCommandNG(CMD_PM5_RGB_SET, (uint8_t *)&payload, sizeof(payload)); + PacketResponseNG resp; + WaitForResponseTimeout(CMD_PM5_RGB_SET, &resp, 200); +} diff --git a/client/src/util.h b/client/src/util.h index 727c95545..047d24269 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -231,4 +231,10 @@ void str_unescape_newlines_inplace(char *s); */ int str_copy_without_whitespace(const char *src, char *dst, size_t dst_size, size_t *dst_len); +/** + * @brief Set the antenna RGB LED colour (Proxmark5). No-op reply on devices + * without an RGB LED. Sends CMD_PM5_RGB_SET and waits briefly for the ack. + */ +void set_rgb(uint8_t r, uint8_t g, uint8_t b); + #endif diff --git a/common_arm/Makefile.common b/common_arm/Makefile.common index 56ef0753a..90fe79235 100644 --- a/common_arm/Makefile.common +++ b/common_arm/Makefile.common @@ -43,12 +43,12 @@ OBJDIR = obj INCLUDE = -I../include -I../common_arm -I../common_fpga -I../common -I. INCLUDE += -I../common_arm/flash_code -I../common_arm/flash_data -I../common_arm/fpga -I../common_arm/gpio -INCLUDE += -I../common_arm/rssi -I../common_arm/sys -I../common_arm/ticks -I../common_arm/usb -I../common_arm/wdt +INCLUDE += -I../common_arm/rssi -I../common_arm/rgb -I../common_arm/sys -I../common_arm/ticks -I../common_arm/usb -I../common_arm/wdt # Also search prerequisites in the common directory (for usb.c), the fpga directory (for fpga.bit), and the lz4 directory VPATH = . ../common_arm ../common ../common/crapto1 ../common/mbedtls ../common/lz4 ../fpga ../armsrc/Standalone ../common/hitag2 VPATH += ../common_arm/flash_code ../common_arm/flash_data ../common_arm/fpga ../common_arm/gpio -VPATH += ../common_arm/rssi ../common_arm/sys ../common_arm/ticks ../common_arm/usb ../common_arm/wdt +VPATH += ../common_arm/rssi ../common_arm/rgb ../common_arm/sys ../common_arm/ticks ../common_arm/usb ../common_arm/wdt INCLUDES = ../include/proxmark3_arm.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/pm3_cmd.h diff --git a/common_arm/Makefile.hal b/common_arm/Makefile.hal index 619a5e701..8bacdc31e 100644 --- a/common_arm/Makefile.hal +++ b/common_arm/Makefile.hal @@ -196,10 +196,16 @@ ifneq (,$(findstring FLASH,$(PLATFORM_EXTRAS_TMP))) PLATFORM_EXTRAS_TMP := $(strip $(filter-out FLASH,$(PLATFORM_EXTRAS_TMP))) endif ifneq (,$(findstring BTADDON,$(PLATFORM_EXTRAS_TMP))) + ifeq ($(PLATFORM),PM5) + $(error Incompatible PLATFORM_EXTRAS token BTADDON) + endif PLATFORM_DEFS += -DWITH_FPC_USART_HOST PLATFORM_EXTRAS_TMP := $(strip $(filter-out BTADDON,$(PLATFORM_EXTRAS_TMP))) endif ifneq (,$(findstring FPC_USART_DEV,$(PLATFORM_EXTRAS_TMP))) + ifeq ($(PLATFORM),PM5) + $(error Incompatible PLATFORM_EXTRAS token FPC_USART_DEV) + endif PLATFORM_DEFS += -DWITH_FPC_USART_DEV PLATFORM_EXTRAS_TMP := $(strip $(filter-out FPC_USART_DEV,$(PLATFORM_EXTRAS_TMP))) endif diff --git a/common_arm/fpga/fpga_hw_at32.c b/common_arm/fpga/fpga_hw_at32.c index 92b2b2baf..7b323a4af 100644 --- a/common_arm/fpga/fpga_hw_at32.c +++ b/common_arm/fpga/fpga_hw_at32.c @@ -374,6 +374,11 @@ int FpgaStartConfig(bool configSram, uint32_t fileLength) { // TODO DXL: Check the file length is valid in this platform? // if not, return the PM3_EOVFLOW + // Make sure the FPGA is clocked/powered before we drive its JTAG. Previously + // a reader command (e.g. `hf 14a read`) had to be run first to bring the FPGA + // up, otherwise `hw fpga config` had nothing to talk to. Do it here instead. + FpgaSetup24MHzClk(); + // Init jtag hardware link. gpio_fpga_download_setup(); diff --git a/common_arm/rgb/rgb_apis.h b/common_arm/rgb/rgb_apis.h new file mode 100644 index 000000000..8789a8557 --- /dev/null +++ b/common_arm/rgb/rgb_apis.h @@ -0,0 +1,15 @@ +#ifndef RGB_APIS_H_ +#define RGB_APIS_H_ + +#include "common.h" + +//----------------------------------------------------------------------------- +// Antenna RGB LED control. +// +// Only implemented on platforms that actually have an RGB LED (currently PM5, +// driven by an I2C RGB controller). Other platforms don't build this module, so +// callers must guard usage accordingly (e.g. the CMD_PM5_RGB_SET handler). +//----------------------------------------------------------------------------- +void RgbLedSet(uint8_t r, uint8_t g, uint8_t b); + +#endif // RGB_APIS_H_ diff --git a/common_arm/rgb/rgb_hw_at32.c b/common_arm/rgb/rgb_hw_at32.c new file mode 100644 index 000000000..e851d6b72 --- /dev/null +++ b/common_arm/rgb/rgb_hw_at32.c @@ -0,0 +1,22 @@ +#include "rgb_apis.h" +#include "i2c.h" // RGB LED is driven over I2C +#include "ticks_apis.h" // I2C timing needs ticks running + +// PM5 antenna RGB LED, driven by an I2C RGB controller (7-bit address 0x48). +// Register map (from the QC/bring-up code): +// 0x02 = index (which LED) +// 0x01 = count (number of LEDs on the string; must be set or nothing lights) +// 0x03 = data (3 bytes RGB888 per LED) +#define RGB_I2C_ADDR (0x48 << 1) // 8-bit address form expected by the I2C_* API +#define RGB_REG_COUNT 0x01 +#define RGB_REG_INDEX 0x02 +#define RGB_REG_DATA 0x03 + +void RgbLedSet(uint8_t r, uint8_t g, uint8_t b) { + uint8_t rgb[3] = { r, g, b }; + StartTicks(); + I2C_init(true); + I2C_WriteByte(0, RGB_REG_INDEX, RGB_I2C_ADDR); // address LED index 0 + I2C_WriteByte(1, RGB_REG_COUNT, RGB_I2C_ADDR); // 1 LED on the string + I2C_BufferWrite(rgb, sizeof(rgb), RGB_REG_DATA, RGB_I2C_ADDR); +} diff --git a/common_arm/ticks/ticks_hw_at91.c b/common_arm/ticks/ticks_hw_at91.c index b1c186aa3..987bc95fd 100644 --- a/common_arm/ticks/ticks_hw_at91.c +++ b/common_arm/ticks/ticks_hw_at91.c @@ -272,10 +272,12 @@ uint16_t RAMFUNC GetLoEdgeCaptureCount(void) { } lo_edge_t RAMFUNC GetLoEdgeCaptureStatus(void) { - if (AT91C_BASE_TC1->TC_SR & INPUT_CAPTURE_EVT_RISING_EDGE) { + // Read Once Only: Reading TC_SR will simultaneously clear status bits such as LDRAS/LDRBS. + uint32_t sr = AT91C_BASE_TC1->TC_SR; + if (sr & INPUT_CAPTURE_EVT_RISING_EDGE) { return LO_EDGE_RISING; } - if (AT91C_BASE_TC1->TC_SR & INPUT_CAPTURE_EVT_FALLING_EDGE) { + if (sr & INPUT_CAPTURE_EVT_FALLING_EDGE) { return LO_EDGE_FALLING; } return LO_EDGE_NO; diff --git a/doc/md/Development/Proxmark5.md b/doc/md/Development/Proxmark5.md index 9374ac982..156fa1c0f 100644 --- a/doc/md/Development/Proxmark5.md +++ b/doc/md/Development/Proxmark5.md @@ -1,39 +1,31 @@ -1. Some hardware abstractions are still incomplete; all 'TODO DXL' items need to be addressed. -2. Certain timeout mechanisms (`timeout--`) are not cross-platform compatible. If `timeout--` is not used with additional clock conditions, please replace it with a timeout mechanism tied to a specific clock source, as the main clock frequency varies across different platforms. -3. The ADC waveform is inverted, which differs from the RDV4. -4. All future modifications to ARM should consider cross-platform compatibility. -5. Please avoid modifying `/armsrc/fpgaloader.c` and files within the `common_arm` directory before this PR is merged into the `master` branch. -6. The client-side logic for identifying the chip capacity of the AT32F435 is not yet complete, which may result in an incorrect estimation of the CODE FLASH capacity. -7. To maintain compatibility with the PM5 power button, the bootrom behavior has been changed: instead of entering flash mode immediately upon power-up when the button is pressed, it will now enter flash mode only after the button is held for 3 seconds. - > After pressing and holding the button, connect to the USB and wait for 3 seconds. After 3 seconds, LED_B and LED_D will light up. Release the button. At this time, the device has entered FLASH mode and can perform firmware FLASH. -8. The Proxmark5 does not require J-Link or similar tools for unbricking. Simply connect it via USB, power it on, and hold the button for 6 seconds to enter ISP mode. You can then use the AT32 ISP tool to flash the complete firmware. -9. The FPGA code for the Proxmark5 has not yet been pushed to the repository and is currently being organized. A new PR will be created for merging once the cleanup is complete. -10. Chinese comments will remain in the code temporarily. You may translate them into English, but **please ensure the 'TODO DXL' tags are preserved**, otherwise we might lose track of pending tasks. -11. The armsrc can only build by cmake, the client can only build by makefile. it is need to be addressed. -12. The 'hw fpga config' command can be used to configure the FPGA firmware. -13. The 'hw fpga pwrpwm' command can be used to adjust the antenna's drive voltage. -14. The `hw ant_pm5 -m <8bit data>` command can be used to modify the frequency and Q value. Note: High Q is only allowed at 125kHz/134kHz to prevent excessive resonant voltage from damaging the device. - > 8bit map: 125 134 250 375 500 HFLED LFLED Q (lsb) -15. We will release documentation for the RGB controller and antenna controller as soon as possible. -16. ~The bootrom depends on version_pm3.c, but currently there is no logic in the bootrom's CMakeLists.txt to generate this source file, so the bootrom project cannot be compiled yet.~ -17. **Currently, only the client and firmware of this PR can support PM5. Please do not use the client and firmware from the master branch to flash PM5 for the time being.** -18. If you flashed the wrong firmware to PM5 using the xxx link probe, or if the PM5 firmware fails to run for unknown reasons, you can try the following methods to fix it: - > 1. Download AT32 ISP tool [GO](https://www.arterychip.com/file/download/1764) & Extract - > 2. Install the driver for ISP USB(The driver is in the `Artery_DFU_DriverInstall` dir) - > 3. Connect the USB to the USB port on the same side as the button (instead of the CEP port). - > 4. Press and hold the button for 6-8 seconds. The device will reboot to ISP mode, usb will startup. - > 5. To open the `Artery ISP Programmer` to select `HEX` file to flash, the bootrom & fullimage is required. - > 6. When flash done, disconnect usb and reconnect for restart device to exit ISP mode. - > 7. Open the proxmark client to try to connect to verify your device is re-working. -19. **[IMPORTANT]**: After the PM5 starts up, it needs to immediately pull PB0 high to enable power supply. If PB0 is released during the enable process, it means that the device needs to be shut down. This causes a problem. The CMD_HARDWARE_RESET command calls the `void ResetChip(void)` function, which is implemented using NVIC Reset in the factory firmware. According to the AT32 manual, NVIC RESET takes up to 10-25ms to start. During this period, PB0 has already been released for a long time, so the RESET operation becomes a shutdown. Therefore, the flash process will fail. - > The latest commit resolves this issue, and users can fix it by forcibly entering FLASH MODE to update BOOTROM+FULLIMAGE according to the instructions in item 7. -20. To make debugging easier, I disabled standalone mod. Now, when the device is powered on, pressing and holding the button for a few seconds will start displaying a running light, and then releasing the button will power off the device. This is suitable for users who have installed BWM. -21. **[IMPORTANT]**: It is worth noting that Proxmark5's SRAM is much larger than RDV4's (8 times larger), so Bigbuf will also be much larger. If some variables are related to the size/index of Bigbuf, be sure to use uint32_t instead of uint16_t, otherwise it will definitely be incompatible with Proxmark5 and there is a risk of overflow. +These notes are mostly intended for developers. +Read first the [Proxmark5 Instructions](../Installation_Instructions/Proxmark5-Instructions.md). + +- Some hardware abstractions are still incomplete; all 'TODO DXL' items need to be addressed. +- Certain timeout mechanisms (`timeout--`) are not cross-platform compatible. If `timeout--` is not used with additional clock conditions, please replace it with a timeout mechanism tied to a specific clock source, as the main clock frequency varies across different platforms. +- The ADC waveform is inverted, which differs from the RDV4. +- All future modifications to ARM must ensure cross-platform compatibility. +- The client-side logic for identifying the chip capacity of the AT32F435 is not yet complete, which may result in an incorrect estimation of the CODE FLASH capacity. +- The FPGA code for the Proxmark5 has not yet been pushed to the repository and is currently being organized. A new PR will be created for merging once the cleanup is complete. +- Chinese comments will remain in the code temporarily. You may translate them into English, but **please ensure the 'TODO DXL' tags are preserved**, otherwise we might lose track of pending tasks. +- We will release documentation for the RGB controller and antenna controller as soon as possible. +- **[IMPORTANT]**: After the PM5 starts up, it needs to immediately pull PB0 high to enable power supply. If PB0 is released during the enable process, it means that the device needs to be shut down. This causes a problem. The CMD_HARDWARE_RESET command calls the `void ResetChip(void)` function, which is implemented using NVIC Reset in the factory firmware. According to the AT32 manual, NVIC RESET takes up to 10-25ms to start. During this period, PB0 has already been released for a long time, so the RESET operation becomes a shutdown. Therefore, the flash process will fail. The current code takes care of it. +- **[IMPORTANT]**: It is worth noting that Proxmark5's SRAM is much larger than RDV4's (8 times larger), so Bigbuf will also be much larger. If some variables are related to the size/index of Bigbuf, be sure to use uint32_t instead of uint16_t, otherwise it will definitely be incompatible with Proxmark5 and there is a risk of overflow. > For example, this [BUG](https://github.com/RfidResearchGroup/proxmark3/pull/3449/commits/8f550a8c0f6ccd1b345eedb72c12af11bae17ebe) caused all data blocks in the `hf mfu dump` to be empty because an index overflow caused access to the wrong memory address. BUG found: [mifarecmd.c#L660](https://github.com/RfidResearchGroup/proxmark3/blob/master/armsrc/mifarecmd.c#L660) -22. The speed of I2C has been modified, and hardware abstraction of I2C is not yet complete. This is also a TODO. +- The speed of I2C has been modified, and hardware abstraction of I2C is not yet complete. This is also a TODO. > Ideally, I2C should also perform HAL in common_arm. -23. The communication driver between BWM and PM5_ARM is currently on the TODO list, therefore, operating PM5 via BWM is not supported at this time. (Communication between ARM and BWM is protocol-based, unlike RDV4 which is transparent.) +- The communication driver between BWM and PM5_ARM is currently on the TODO list, therefore, operating PM5 via BWM is not supported at this time. (Communication between ARM and BWM is protocol-based, unlike RDV4 which is transparent.) > Having a protocol allows for better flow control, preventing packet loss caused by UART being too 'fast' and BLE being too 'slow'. -24. The HAL work related to HITAG has been completed. For details, see: [HitagS & HitagU](https://github.com/RfidResearchGroup/proxmark3/pull/3449#issuecomment-5303520489) +- The HAL work related to HITAG has been completed. For details, see: [HitagS & HitagU](https://github.com/RfidResearchGroup/proxmark3/pull/3449#issuecomment-5303520489) +- The code of the BWM Battery Wireless Module is [hosted here](https://github.com/RfidResearchGroup/Proxmark5_BWM_esp32) + +The ARM code is primarily compiled with the Makefiles, but it also supports Cmake. +``` +mkdir build && cd build +cmake -DPLATFORM:STRING=PM5 .. +cmake --build . +cd .. +client/proxmark3 --port /dev/ttyACM0 --flash --unlock-bootloader --image build/obj/bootrom.elf --image build/obj/fullimage.elf +``` diff --git a/doc/md/Installation_Instructions/Proxmark5-Instructions.md b/doc/md/Installation_Instructions/Proxmark5-Instructions.md new file mode 100644 index 000000000..66eab1781 --- /dev/null +++ b/doc/md/Installation_Instructions/Proxmark5-Instructions.md @@ -0,0 +1,88 @@ +# Proxmark5 specificities + +## Compiling instructions + +The Proxmark5 platform must be specified when compiling the firmware. +You have several options: +* Update your `Makefile.platform` with `PLATFORM=PM5` then `make -j` +* Specify it on-the-fly with `make -j PLATFORM=PM5` +* If you have both Proxmark3 and Proxmark5, you can maintain a separate file like `Makefile.pm5.platform` and specify it when compiling, with `make -j PLATFORM_FILE=Makefile.pm5.platform`. +* ⚠️ Make sure to not have any conflicting `PLATFORM_EXTRAS` such as `BTADDON` or `FPC_USART_DEV` which are specific to the RDV4. + +💡 The same client can handle both Proxmark3 and Proxmark5, no need to compile separate clients if you own both hardwares. + +## Flashing instructions + +⚠️ Disconnect the BWM (Battery Wireless Module), it's not supported yet, and creates issues flashing. +⚠️ As usual, make sure [ModemManager won't interfere](ModemManager-Must-Be-Discarded.md). +⚠️ Make sure no other Proxmark (3 or 5) is plugged into your PC. + + +Use the yellow USB-C on the same side as the button. +* Plug in your Proxmark5 while holding the button for about 4 seconds until you see 2 LEDs illuminated (B and D). Don't wait too long. If the LEDs are now off again, you entered DFU mode. Unplug and try again. +* Run `./pm3-flash-bootrom` +* Unplug. +* Plug in your Proxmark5 while holding the button for about 4 seconds until you see 2 LEDs illuminated (B and D). +* Run `./pm3-flash-fullimage` + +Later, you don't need to reflash the bootrom and you can just run `./pm3-flash-fullimage` + +If you see "🚨 The elf file is not applicable to the currently connected device.", you probably forgot to add the `PLATFORM=PM5` when compiling. + +The FPGA code for the Proxmark5 has not yet been pushed to the repository. To flash the FGPA with the latest image: +``` +wget https://github.com/user-attachments/files/31105593/PM5_FPGA_fix_loedge_bug.zip +unzip PM5_FPGA_fix_loedge_bug.zip +./pm3 +hw fpga config -f PM5_FPGA_fix_loedge_bug.bin +hf 14a read --drop +``` + +## Recovery flashing via DFU + +If the device seems unresponsive and unable to enter boot mode when the button is pressed when plugged, you can reflash the bootrom over DFU. The Proxmark5 does not require J-Link or similar tools for unbricking. + +`sudo apt install dfu-util` + +Enter DFU mode: Plug in your Proxmark5 while holding the button for about 8 seconds until you see 2 LEDs (B and D) going on then off. + +Backup current flash content, if needed: +`sudo dfu-util -d 2e3c:df11 -a 0 -s 0x08000000:1048576 -U pm5-full-flash-backup.bin` +Flash bootrom: +`sudo dfu-util -d 2e3c:df11 -a 0 -s 0x08000000 -D recovery/bootrom.bin` +Flash fullimage: +`sudo dfu-util -d 2e3c:df11 -a 0 -s 0x08004000:leave -D recovery/fullimage.bin` + +You can also flash bootrom and fullimage in one go: +`sudo dfu-util -d 2e3c:df11 -a 0 -s 0x08000000:leave -D recovery/recovery.bin` + +On Windows, you can try the following: +- Download and extract the [AT32 ISP Programmer](https://www.arterychip.com/file/download/1764) +- Install the USB driver present in `Artery_DFU_DriverInstall/` +- Enter DFU mode as explained above +- Open the `Artery ISP Programmer` , select `HEX` file to flash, and use the bootrom & fullimage is required. +- When flash done, disconnect usb and reconnect for restart device to exit ISP mode. +- Open the proxmark client to try to connect to verify your device is re-working. + +## Specific commands + +Do not use them unless you fully understand what you're doing. + +* `hw fpga config`: to upload the FPGA firmware +* `hw fpga pwrpwm`: to adjust the antenna's drive voltage TODO: acceptable ranges? +* `hw ant_pm5 -m --set <8bit data>` command can be used to modify the frequency and Q value. Note: High Q is only allowed at 125kHz/134kHz to prevent excessive resonant voltage from damaging the device. + > 8bit map: 125 134 250 375 500 HFLED LFLED Q (lsb) +* `hw qc_pm5`: factory quality check command, will activate the RBG, the buzzer and the antenna LEDs. +* `hw factorydata`: read/write the factory data (originality signature etc) + +## Standalone Modes + +Standalone modes are disabled for now, to ease debugging. + +## BWM aka Battery Wireless Module + +⚠️ the BWM is not yet fully supported, it's better not to plug it for now. + +* [Installation instructions](https://github.com/RfidResearchGroup/Proxmark5_BWM_esp32/blob/master/INSTALL.md) +* Pressing the button will wake the device +* When the device is powered on, pressing and holding the button for a few seconds will start displaying a running light, and then releasing the button will power off the device diff --git a/doc/md/Use_of_Proxmark/4_Advanced-compilation-parameters.md b/doc/md/Use_of_Proxmark/4_Advanced-compilation-parameters.md index c871e6d46..84e8cbf3c 100644 --- a/doc/md/Use_of_Proxmark/4_Advanced-compilation-parameters.md +++ b/doc/md/Use_of_Proxmark/4_Advanced-compilation-parameters.md @@ -171,7 +171,7 @@ If you own a Proxmark3 Easy with only 256KB, you can use a few definitions to he First thing is of course to use the `PLATFORM=PM3GENERIC`. Adding `PLATFORM_SIZE=256` will provoke an error during compilation of the recovery image if your image is too big, so you can detect the problem before trying to flash the Proxmark3, e.g. ``` -[=] GEN proxmark3_recovery.bin +[=] GEN recovery.bin !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ERROR: Firmware image too large for your platform! 262768 > 262144 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/doc/path_notes.md b/doc/path_notes.md index 2dbe03661..ae1c9c9d4 100644 --- a/doc/path_notes.md +++ b/doc/path_notes.md @@ -63,7 +63,7 @@ The recovery / firmware files will be copied to /usr/local/share/proxmark3/firmware ``` -* Proxmark3 firmware: `bootrom.elf`, `fullimage.elf`, `proxmark3_recovery.bin` (used for JTAG) +* Proxmark3 firmware: `bootrom.elf`, `fullimage.elf`, `recovery.bin` (used for JTAG) * SIM firmware: `sim014.bin`, `sim014.sha512.txt` diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index eb3d7d249..6eef5820f 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -637,6 +637,8 @@ typedef struct { #define CMD_EEPROM_FACTORY_INFO_WRITE 0x0173 // PM5, QC test for the hardware #define CMD_PM5_QC_TEST 0x0177 +// PM5, set the antenna RGB LED colour (payload: r,g,b). Used by `hf/lf tune --rgb`. +#define CMD_PM5_RGB_SET 0x0178 // For low-frequency tags #define CMD_LF_TI_READ 0x0202 diff --git a/recovery/Makefile b/recovery/Makefile index e18e66c06..22425bb1e 100644 --- a/recovery/Makefile +++ b/recovery/Makefile @@ -1,6 +1,6 @@ include ../common_arm/Makefile.common -INSTALLFW = proxmark3_recovery.bin +INSTALLFW = recovery.bin ifneq (,$(FWTAG)) INSTALLFWTAG = $(notdir $(INSTALLFW:%.bin=%-$(FWTAG).bin)) else @@ -24,13 +24,19 @@ all: $(BINS) bootrom.bin: ../bootrom/obj/bootrom.elf $(info [=] GEN $@) - $(Q)$(CROSS_OBJCOPY) --gap-fill=0xff --pad-to 0x00102000 -O binary $^ $@ +ifeq ($(PLATFORM),PM5) + $(info [=] GENA $@) + $(Q)$(CROSS_OBJCOPY) --gap-fill=0xff --pad-to 0x08004000 -O binary $^ $@ +else + $(info [=] GENB $@) + $(Q)$(CROSS_OBJCOPY) --gap-fill=0xff --pad-to 0x00104000 -O binary $^ $@ +endif fullimage.bin: ../armsrc/obj/fullimage.elf $(info [=] GEN $@) $(Q)$(CROSS_OBJCOPY) --gap-fill=0xff -O binary $^ $@ -proxmark3_recovery.bin: bootrom.bin fullimage.bin +recovery.bin: bootrom.bin fullimage.bin $(info [=] GEN $@) $(Q)cat bootrom.bin fullimage.bin > $@ diff --git a/tools/jtag_openocd/openocd_configuration.sample b/tools/jtag_openocd/openocd_configuration.sample index 2468bab76..2fc6a82e3 100644 --- a/tools/jtag_openocd/openocd_configuration.sample +++ b/tools/jtag_openocd/openocd_configuration.sample @@ -1,6 +1,6 @@ CONFIG_GEN=general.cfg CONFIG_BOARD=board-at91sam7s.cfg -IMAGE=../../recovery/proxmark3_recovery.bin +IMAGE=../../recovery/recovery.bin DUMP="dump_$(date +'%Y%m%d-%H%M%S').bin" # One can use openocd-provided interfaces or local interface files diff --git a/tools/mfc/card_only/staticnested_2x1nt_rf08s.c b/tools/mfc/card_only/staticnested_2x1nt_rf08s.c index 0a83c10fd..9d206162e 100644 --- a/tools/mfc/card_only/staticnested_2x1nt_rf08s.c +++ b/tools/mfc/card_only/staticnested_2x1nt_rf08s.c @@ -18,15 +18,18 @@ #include #include -uint16_t i_lfsr16[1 << 16] = {0}; -uint16_t s_lfsr16[1 << 16] = {0}; +uint16_t prev8_lfsr16[1 << 16]; +uint16_t prev14_lfsr16[1 << 16]; static void init_lfsr16_table(void) { + uint16_t buffer[16] = { 0 }; uint16_t x = 1; - for (uint16_t i = 1; i; ++i) { - i_lfsr16[(x & 0xff) << 8 | x >> 8] = i; - s_lfsr16[i] = (x & 0xff) << 8 | x >> 8; + for (uint32_t i = 0; i < 65536 + 16; ++i) { + prev8_lfsr16[buffer[(i + 8) % 16]] = buffer[i % 16]; + prev14_lfsr16[buffer[(i + 14) % 16]] = buffer[i % 16]; + x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15; + buffer[i % 16] = (x & 0xFF) << 8 | x >> 8; } } @@ -40,28 +43,25 @@ static void init_lfsr16_table(void) { // return s_lfsr16[i]; // } -static uint16_t prev_lfsr16(uint16_t nonce) { - uint16_t i = i_lfsr16[nonce]; - if (i == 1) { - i = 0xffff; - } else { - i--; - } - return s_lfsr16[i]; -} +// static uint16_t prev_lfsr16(uint16_t nonce) { +// uint16_t i = i_lfsr16[nonce]; +// if (i == 1) { +// i = 0xffff; +// } else { +// i--; +// } +// return s_lfsr16[i]; +// } static uint16_t compute_seednt16_nt32(uint32_t nt32, uint64_t key) { uint8_t a[] = {0, 8, 9, 4, 6, 11, 1, 15, 12, 5, 2, 13, 10, 14, 3, 7}; uint8_t b[] = {0, 13, 1, 14, 4, 10, 15, 7, 5, 3, 8, 6, 9, 2, 12, 11}; - uint16_t nt = nt32 >> 16; - uint8_t prev = 14; - for (uint8_t i = 0; i < prev; i++) { - nt = prev_lfsr16(nt); - } - uint8_t prevoff = 8; - bool odd = 1; + uint16_t nt = prev14_lfsr16[nt32 >> 16]; + + bool odd = 1; for (uint8_t i = 0; i < 6 * 8; i += 8) { + if (odd) { nt ^= (a[(key >> i) & 0xF]); nt ^= (b[(key >> i >> 4) & 0xF]) << 4; @@ -69,11 +69,9 @@ static uint16_t compute_seednt16_nt32(uint32_t nt32, uint64_t key) { nt ^= (b[(key >> i) & 0xF]); nt ^= (a[(key >> i >> 4) & 0xF]) << 4; } + odd ^= 1; - prev += prevoff; - for (uint8_t j = 0; j < prevoff; j++) { - nt = prev_lfsr16(nt); - } + nt = prev8_lfsr16[nt]; } return nt; } diff --git a/tools/mfc/card_only/staticnested_2x1nt_rf08s_1key.c b/tools/mfc/card_only/staticnested_2x1nt_rf08s_1key.c index 87b2f330d..496d22e7a 100644 --- a/tools/mfc/card_only/staticnested_2x1nt_rf08s_1key.c +++ b/tools/mfc/card_only/staticnested_2x1nt_rf08s_1key.c @@ -20,15 +20,18 @@ static uint32_t hex_to_uint32(const char *hex_str) { return (uint32_t)strtoul(hex_str, NULL, 16); } -uint16_t i_lfsr16[1 << 16] = {0}; -uint16_t s_lfsr16[1 << 16] = {0}; +uint16_t prev8_lfsr16[1 << 16]; +uint16_t prev14_lfsr16[1 << 16]; static void init_lfsr16_table(void) { + uint16_t buffer[16] = { 0 }; uint16_t x = 1; - for (uint16_t i = 1; i; ++i) { - i_lfsr16[(x & 0xff) << 8 | x >> 8] = i; - s_lfsr16[i] = (x & 0xff) << 8 | x >> 8; + for (uint32_t i = 0; i < 65536 + 16; ++i) { + prev8_lfsr16[buffer[(i + 8) % 16]] = buffer[i % 16]; + prev14_lfsr16[buffer[(i + 14) % 16]] = buffer[i % 16]; + x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15; + buffer[i % 16] = (x & 0xFF) << 8 | x >> 8; } } @@ -42,29 +45,23 @@ static void init_lfsr16_table(void) { // return s_lfsr16[i]; // } -static uint16_t prev_lfsr16(uint16_t nonce) { - uint16_t i = i_lfsr16[nonce]; - if (i == 1) { - i = 0xffff; - } else { - i--; - } - return s_lfsr16[i]; -} +// static uint16_t prev_lfsr16(uint16_t nonce) { +// uint16_t i = i_lfsr16[nonce]; +// if (i == 1) { +// i = 0xffff; +// } else { +// i--; +// } +// return s_lfsr16[i]; +// } static uint16_t compute_seednt16_nt32(uint32_t nt32, uint64_t key) { uint8_t a[] = {0, 8, 9, 4, 6, 11, 1, 15, 12, 5, 2, 13, 10, 14, 3, 7}; uint8_t b[] = {0, 13, 1, 14, 4, 10, 15, 7, 5, 3, 8, 6, 9, 2, 12, 11}; - uint16_t nt = nt32 >> 16; - uint8_t prev = 14; - for (uint8_t i = 0; i < prev; i++) { - nt = prev_lfsr16(nt); - } + uint16_t nt = prev14_lfsr16[nt32 >> 16]; - uint8_t prevoff = 8; bool odd = 1; - for (uint8_t i = 0; i < 6 * 8; i += 8) { if (odd) { @@ -76,11 +73,7 @@ static uint16_t compute_seednt16_nt32(uint32_t nt32, uint64_t key) { } odd ^= 1; - prev += prevoff; - - for (uint8_t j = 0; j < prevoff; j++) { - nt = prev_lfsr16(nt); - } + nt = prev8_lfsr16[nt]; } return nt; } diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 170abb922..16bd3719b 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -311,7 +311,7 @@ while true; do fi if $TESTALL || $TESTRECOVERY; then echo -e "\n${C_BLUE}Testing recovery:${C_NC}" - if ! CheckFileExist "recovery image exists" "./recovery/proxmark3_recovery.bin"; then break; fi + if ! CheckFileExist "recovery image exists" "./recovery/recovery.bin"; then break; fi fi if $TESTALL || $TESTFPGACOMPRESS; then