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.
This commit is contained in:
trichimtrich committed 2026-08-05 15:28:35 +08:00
1 parent da509461b7
commit 983459ab44
2 files changed
+21 -36

No files matched your search

+1
View File
@@ -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)
+20 -36
View File
@@ -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);
}
}
}