From 983459ab4428a9f44c000aef78dbb2830d6f61b6 Mon Sep 17 00:00:00 2001 From: trichimtrich Date: Wed, 5 Aug 2026 15:26:49 +0800 Subject: [PATCH 1/5] fix `hf iclass unhash` omitting valid pre-images hash0() reduces each six-bit z chunk modulo a position-dependent value: _zn = (zn % (63 - n)) + n n = 0..3 _zn4 = (zn4 % (64 - n)) + n n = 0..3, chunks 4..7 The modulus is below 0x40, so the reduction is lossy and a recovered residue r has a second pre-image at r + modulus whenever that still fits in six bits. invert_hash0() forked on a hardcoded table mapping {00,01,02,03} to {3f,3e,3d,3c}, i.e. alt = 63 - r, gated on `hydra_head <= n % 4 || hydra_head >= 63 - (n % 4)`. Neither matches the actual arithmetic: the alternative is r + modulus, not 63 - r, and which chunks can fork follows the modulus rather than n % 4. The table happens to be right for a few positions and wrong for the rest, so some valid pre-images were never generated. Replaced with the modulus arithmetic taken directly from hash0(). Also drops ~15 lines. Impact: the pre-image is the DES ciphertext for the master key crack. If the true one is missing, hashcat searches the entire keyspace and finds nothing, with no way to distinguish that from "the key isn't there". Measured over randomly generated master/CSN pairs, where the true ciphertext is computable and can be checked against the output: before the true DES ciphertext was absent for 5 of 30 keys (17%) after present for 149 of 150 keys The one remaining failure is unrelated and predates this change: invert_hash0() returns no pre-images at all for roughly 0.5% of keys (measured 2 of 400), where neither p nor ~p is found in pi[] and the x search yields nothing. Left alone here. Example. Master key 1111111111111111 and CSN 2222222222222222 diversify to B94318BC673F0A12, and DES_enc(CSN, master) = 950973182317F80B: hf iclass unhash -k B94318BC673F0A12 before after 090973182314280B 090973182314280B 4F0973182314280B 090973182317F80B 950973182314280B 4F0973182314280B DB0973182314280B 4F0973182317F80B 950973182314280B 950973182317F80B <- the true one DB0973182314280B DB0973182317F80B Every printed candidate is still validated through hash0() before being reported, so this only adds pre-images that verifiably hash back to the key. --- CHANGELOG.md | 1 + client/src/loclass/ikeys.c | 56 ++++++++++++++------------------------ 2 files changed, 21 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2f28c41e..c8c421993 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] +- Fixed `hf iclass unhash` omitting valid hash0 pre-images, which could make the subsequent hashcat DES crack unable to find the master key (@trichimtrich) - Added `hf felica seacauth1` command - Added `lf trovan` commands to support Trovan Animal ID (@iceman1001) - Added `hf mf gdmgetblk/gdmgethidblk/gdmsethidblk/gdmsetuid/gdmwipe/gdmsetsig` (@0x6r1an0y) diff --git a/client/src/loclass/ikeys.c b/client/src/loclass/ikeys.c index b50699e9e..592c5dd9c 100644 --- a/client/src/loclass/ikeys.c +++ b/client/src/loclass/ikeys.c @@ -545,8 +545,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 +564,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 +588,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); } } } From 9dd2c6f3e5095c811c247e8f62c702f4bf49a156 Mon Sep 17 00:00:00 2001 From: trichimtrich Date: Wed, 5 Aug 2026 16:05:50 +0800 Subject: [PATCH 2/5] fix `hf iclass unhash` failing on keys with repeated z values Second, independent cause of missing pre-images. Where the previous commit addressed the modulo forks, this one addresses check(). ck() rewrites a repeated value as the *index* it matched: ck(i, j, z) = ck(i, j-1, z[i] <- j) if z[i] == z[j] so a stored value in 0..3 is ambiguous: it may be such a marker, or a genuine small value. reverse_ck() committed to one reading, and walked j in the same descending order as the forward pass, so it could not undo a chained substitution: half 01 03 24 03 ck() 01 03 24 00 03 matched z[1] -> 1, then 1 matched z[0] -> 0 reverse_ck() 01 03 24 01 restores z[0]=01 at j=0, never revisits j=1 8.9% of random four-value halves failed the ck() round-trip. Most of the time that still landed on a different but valid pre-image, since hash0 is many-to-one, which is why the visible symptom was intermittent: usually a missing pre-image, occasionally none at all. Fix. 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 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 a half can be inverted index by index: z[0] is known, then z[1], and so on, undoing each step in reverse and branching wherever both readings are possible. The halves are independent, so the result is their cross product. reverse_ck()/reverse_check() are replaced by invert_ck_index(), invert_ck_half() and reverse_check_all(), which return every candidate rather than one. invert_hash0() now iterates them. Candidates are still validated through hash0() before being reported, so nothing invalid can be emitted. Verified by round-tripping random pre-images through hash0() and invert_hash0(): before after recovered 82.55% 100.000% keys with no output 1108 0 pre-images per key max 32, avg 4.2 max 128, avg 6.0 over 500,000 keys. Every emitted pre-image was additionally checked to hash back to the input key; none failed. The output now matches an independent exhaustive sweep of the pre-image space on every key tested. --- CHANGELOG.md | 1 + client/src/loclass/ikeys.c | 135 ++++++++++++++++++++++++++++--------- 2 files changed, 103 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8c421993..dd4e64fbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac ## [unreleased][unreleased] - 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 `hf felica seacauth1` command - Added `lf trovan` commands to support Trovan Animal ID (@iceman1001) - Added `hf mf gdmgetblk/gdmgethidblk/gdmsethidblk/gdmsetuid/gdmwipe/gdmsetsig` (@0x6r1an0y) diff --git a/client/src/loclass/ikeys.c b/client/src/loclass/ikeys.c index 592c5dd9c..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 @@ -651,6 +718,8 @@ void invert_hash0(uint8_t k[8]) { } // Free allocated memory free(hydra_heads); + + } // for each zP candidate } } From b11b85e51a1b2482c34deb5608ec9b5d77475533 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 20 Aug 2026 13:59:59 +0200 Subject: [PATCH 3/5] CARDHOPPER: missing header --- armsrc/Standalone/hf_cardhopper.c | 1 + 1 file changed, 1 insertion(+) diff --git a/armsrc/Standalone/hf_cardhopper.c b/armsrc/Standalone/hf_cardhopper.c index cc600a865..1220fa32d 100644 --- a/armsrc/Standalone/hf_cardhopper.c +++ b/armsrc/Standalone/hf_cardhopper.c @@ -30,6 +30,7 @@ #include "usart.h" #include "cmd.h" #include "usb_cdc_apis.h" +#include "usb_read_ng.h" #ifdef CARDHOPPER_USB #define cardhopper_write usb_write From 08f8dc8692eaf143867af9351374ca4176ee290e Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 20 Aug 2026 14:28:08 +0200 Subject: [PATCH 4/5] CARDHOPPER: document how to set CARDHOPPER_USB --- armsrc/Standalone/hf_cardhopper.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/armsrc/Standalone/hf_cardhopper.c b/armsrc/Standalone/hf_cardhopper.c index 1220fa32d..5b2ba3cae 100644 --- a/armsrc/Standalone/hf_cardhopper.c +++ b/armsrc/Standalone/hf_cardhopper.c @@ -32,6 +32,9 @@ #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 #define cardhopper_read usb_read_ng From c9581bde4d35dfa59725b77891ae055e6c56b8f4 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 20 Aug 2026 15:45:57 +0200 Subject: [PATCH 5/5] rdv4_init_extflash: use same values as init_rdv4.lua --- client/cmdscripts/rdv4_init_extflash.cmd | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/client/cmdscripts/rdv4_init_extflash.cmd b/client/cmdscripts/rdv4_init_extflash.cmd index 20143b0ef..27e73fcd1 100755 --- a/client/cmdscripts/rdv4_init_extflash.cmd +++ b/client/cmdscripts/rdv4_init_extflash.cmd @@ -5,4 +5,7 @@ mem load -f t55xx_default_pwds --t55xx mem load -f iclass_default_keys --iclass mem load -f mfulc_default_keys --ulc mem load -f mfulaes_default_keys --aes -lf t55xx deviceconfig -z -p +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