From f7df318a1c2f88ca50e19b35eb190f9a3754084d Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Wed, 4 Nov 2026 11:05:43 +0200 Subject: [PATCH] native implementation of ext4 directory hashing Signed-off-by: Avi Deitcher --- filesystem/ext4/dirhash.go | 260 ++++++++------- filesystem/ext4/dirhash_test.go | 538 +++++++++++++++--------------- filesystem/ext4/ext4-hash-spec.md | 269 +++++++++++++++ filesystem/ext4/md/md4.go | 46 +++ filesystem/ext4/md/md4_test.go | 65 ++++ filesystem/ext4/md4/md4.go | 73 ---- filesystem/ext4/md4/md4_test.go | 151 --------- 7 files changed, 785 insertions(+), 617 deletions(-) create mode 100644 filesystem/ext4/ext4-hash-spec.md create mode 100644 filesystem/ext4/md/md4.go create mode 100644 filesystem/ext4/md/md4_test.go delete mode 100644 filesystem/ext4/md4/md4.go delete mode 100644 filesystem/ext4/md4/md4_test.go diff --git a/filesystem/ext4/dirhash.go b/filesystem/ext4/dirhash.go index 8717c341..ef4d2525 100644 --- a/filesystem/ext4/dirhash.go +++ b/filesystem/ext4/dirhash.go @@ -1,16 +1,12 @@ package ext4 import ( - "github.com/diskfs/go-diskfs/filesystem/ext4/md4" -) + "encoding/binary" + "fmt" + "math/bits" + "syscall" -const ( - teaDelta uint32 = 0x9E3779B9 - k1 uint32 = 0 - k2 uint32 = 0o13240474631 - k3 uint32 = 0o15666365641 - ext4HtreeEOF32 uint32 = ((1 << (32 - 1)) - 1) - ext4HtreeEOF64 uint64 = ((1 << (64 - 1)) - 1) + "github.com/diskfs/go-diskfs/filesystem/ext4/md" ) type hashVersion uint8 @@ -25,133 +21,155 @@ const ( HashVersionSIP = 6 ) -func TEATransform(buf [4]uint32, in []uint32) [4]uint32 { - var sum uint32 - var b0, b1 = buf[0], buf[1] - var a, b, c, d = in[0], in[1], in[2], in[3] - var n = 16 - - for ; n > 0; n-- { - sum += teaDelta - b0 += ((b1 << 4) + a) ^ (b1 + sum) ^ ((b1 >> 5) + b) - b1 += ((b0 << 4) + c) ^ (b0 + sum) ^ ((b0 >> 5) + d) - } - - buf[0] += b0 - buf[1] += b1 - return buf +// dirHashInfo holds an effective hash version (with any filesystem unsigned +// offset already applied), numeric seed words, and the two output hashes. +// A non-nil sipHashKey means the directory encryption key is available and its +// separate directory hash key has been initialized. An all-zero key is valid. +type dirHashInfo struct { + version hashVersion + seed *[4]uint32 + sipHashKey *[16]byte + hash uint32 + minorHash uint32 } -// the old legacy hash -// -//nolint:unparam,revive // we do not used signed, but we probably should, so leaving until we are sure -func dxHackHash(name string, signed bool) uint32 { - var hash uint32 - var hash0, hash1 uint32 = 0x12a3fe2d, 0x37abe8f9 - b := []byte(name) - - for i := len(b); i > 0; i-- { - // get the specific character - c := int(b[i-1]) - // the value of the individual character depends on if it is signed or not - hash = hash1 + (hash0 ^ uint32(c*7152373)) - - if hash&0x80000000 != 0 { - hash -= 0x7fffffff +// ext4fsDirhash hashes exactly len(name) opaque bytes, including embedded zeros. +// Callers needing case folding must prepare the name using the filesystem's +// Unicode map before calling. On an unsupported version both outputs are zero; +// on a missing SipHash key both outputs retain their incoming values. +func ext4fsDirhash(name []byte, info *dirHashInfo) error { + var major, minor uint32 + unsigned := info.version >= HashVersionLegacyUnsigned && info.version <= HashVersionTEAUnsigned + switch info.version { + case HashVersionLegacy, HashVersionLegacyUnsigned: + current, previous := uint32(0x12a3fe2d), uint32(0x37abe8f9) + for _, b := range name { + next := previous + (current ^ (hashByte(b, unsigned) * 7152373)) + if next&0x80000000 != 0 { + next -= 0x7fffffff + } + previous, current = current, next } - hash1 = hash0 - hash0 = hash + major = current << 1 + case HashVersionHalfMD4, HashVersionHalfMD4Unsigned, HashVersionTEA, HashVersionTEAUnsigned: + state := [4]uint32{0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476} + if info.seed != nil && *info.seed != [4]uint32{} { + state = *info.seed + } + halfMD4 := info.version == HashVersionHalfMD4 || info.version == HashVersionHalfMD4Unsigned + for len(name) > 0 { + if halfMD4 { + var input [8]uint32 + hashWords(name, input[:], unsigned) + md.HalfMD4Transform(&state, input) + name = name[min(len(name), 32):] + } else { + var input [4]uint32 + hashWords(name, input[:], unsigned) + TEATransform(&state, input) + name = name[min(len(name), 16):] + } + } + if halfMD4 { + major, minor = state[1], state[2] + } else { + major, minor = state[0], state[1] + } + case HashVersionSIP: + if info.sipHashKey == nil { + return fmt.Errorf("ext4 directory SipHash requires a directory hash key: %w", syscall.EINVAL) + } + hash := sipHash24(name, *info.sipHashKey) + major, minor = uint32(hash>>32), uint32(hash) + default: + info.hash, info.minorHash = 0, 0 + return fmt.Errorf("unsupported ext4 directory hash version %d: %w", info.version, syscall.EINVAL) + } + major &^= 1 + if major == 0xfffffffe { + major = 0xfffffffc } - return hash0 << 1 + info.hash, info.minorHash = major, minor + return nil } -//nolint:unparam,revive // we do not used signed, but we probably should, so leaving until we are sure -func str2hashbuf(msg string, num int, signed bool) []uint32 { - var buf [8]uint32 - var pad, val uint32 - b := []byte(msg) - size := len(b) +func hashByte(b byte, unsigned bool) uint32 { + if unsigned { + return uint32(b) + } + return uint32(int32(int8(b))) +} - pad = uint32(size) | (uint32(size) << 8) +// hashWords converts one block. name includes all remaining bytes so padding +// uses the remaining length before truncation to the output's capacity. +func hashWords(name []byte, output []uint32, unsigned bool) { + pad := uint32(len(name)) + pad |= pad << 8 pad |= pad << 16 - - val = pad - if size > num*4 { - size = num * 4 - } - var j int - for i := 0; i < size; i++ { - c := int(b[i]) - val = uint32(c) + (val << 8) - if (i % 4) == 3 { - buf[j] = val - val = pad - num-- - j++ + name = name[:min(len(name), len(output)*4)] + for i := range output { + word := pad + n := min(len(name), 4) + if n == 4 { + word = 0 } + for _, b := range name[:n] { + word = (word << 8) + hashByte(b, unsigned) + } + output[i] = word + name = name[n:] } - num-- - if num >= 0 { - buf[j] = val - j++ - } - for num--; num >= 0; num-- { - buf[j] = pad - j++ +} + +// TEATransform mixes four input words into state using sixteen TEA iterations +// and feed-forward addition. State words 2 and 3 are left unchanged. +func TEATransform(state *[4]uint32, input [4]uint32) { + x, y := state[0], state[1] + var sum uint32 + for i := 0; i < 16; i++ { + sum += 0x9e3779b9 + x += ((y << 4) + input[0]) ^ (y + sum) ^ ((y >> 5) + input[1]) + y += ((x << 4) + input[2]) ^ (x + sum) ^ ((x >> 5) + input[3]) } - return buf[:] + state[0] += x + state[1] += y } -func ext4fsDirhash(name string, version hashVersion, seed []uint32) (hash, minorHash uint32) { - /* Initialize the default seed for the hash checksum functions */ - var buf = [4]uint32{0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476} +func sipHashRound(v *[4]uint64) { + v[0] += v[1] + v[1] = bits.RotateLeft64(v[1], 13) ^ v[0] + v[0] = bits.RotateLeft64(v[0], 32) + v[2] += v[3] + v[3] = bits.RotateLeft64(v[3], 16) ^ v[2] + v[0] += v[3] + v[3] = bits.RotateLeft64(v[3], 21) ^ v[0] + v[2] += v[1] + v[1] = bits.RotateLeft64(v[1], 17) ^ v[2] + v[2] = bits.RotateLeft64(v[2], 32) +} - // Check to see if the seed is all zero, and if so, use the default - for i, val := range seed { - if val != 0 { - buf[i] = val - } +func sipHash24(name []byte, key [16]byte) uint64 { + k0, k1 := binary.LittleEndian.Uint64(key[:8]), binary.LittleEndian.Uint64(key[8:]) + v := [4]uint64{0x736f6d6570736575 ^ k0, 0x646f72616e646f6d ^ k1, + 0x6c7967656e657261 ^ k0, 0x7465646279746573 ^ k1} + compress := func(word uint64) { + v[3] ^= word + sipHashRound(&v) + sipHashRound(&v) + v[0] ^= word } - - switch version { - case HashVersionLegacyUnsigned: - hash = dxHackHash(name, false) - case HashVersionLegacy: - hash = dxHackHash(name, true) - case HashVersionHalfMD4Unsigned: - for i := 0; i < len(name); i += 32 { - in := str2hashbuf(name[i:], 8, false) - buf[1] = md4.HalfMD4Transform(buf, in) - } - minorHash = buf[2] - hash = buf[1] - case HashVersionHalfMD4: - for i := 0; i < len(name); i += 32 { - in := str2hashbuf(name[i:], 8, true) - buf[1] = md4.HalfMD4Transform(buf, in) - } - minorHash = buf[2] - hash = buf[1] - case HashVersionTEAUnsigned: - for i := 0; i < len(name); i += 16 { - in := str2hashbuf(name[i:], 4, false) - buf = TEATransform(buf, in) - } - hash = buf[0] - minorHash = buf[1] - case HashVersionTEA: - for i := 0; i < len(name); i += 16 { - in := str2hashbuf(name[i:], 4, true) - buf = TEATransform(buf, in) - } - hash = buf[0] - minorHash = buf[1] - default: - return 0, 0 + last := uint64(len(name)&0xff) << 56 + for len(name) >= 8 { + compress(binary.LittleEndian.Uint64(name[:8])) + name = name[8:] + } + for i, b := range name { + last |= uint64(b) << (8 * i) } - hash &= ^uint32(1) - if hash == (ext4HtreeEOF32 << 1) { - hash = (ext4HtreeEOF32 - 1) << 1 + compress(last) + v[2] ^= 0xff + for i := 0; i < 4; i++ { + sipHashRound(&v) } - return hash, minorHash + return v[0] ^ v[1] ^ v[2] ^ v[3] } diff --git a/filesystem/ext4/dirhash_test.go b/filesystem/ext4/dirhash_test.go index d6b855cb..99fb6b58 100644 --- a/filesystem/ext4/dirhash_test.go +++ b/filesystem/ext4/dirhash_test.go @@ -1,317 +1,311 @@ package ext4 import ( + "bytes" + "errors" "fmt" - "strings" + "syscall" "testing" ) -// TestTEATransform tests the TEA (Tiny Encryption Algorithm) transform function -// with known inputs and verifies deterministic output. -func TestTEATransform(t *testing.T) { - tests := []struct { - name string - buf [4]uint32 - in []uint32 - }{ - {"zero buf zero in", [4]uint32{0, 0, 0, 0}, []uint32{0, 0, 0, 0}}, - {"nonzero buf zero in", [4]uint32{1, 2, 3, 4}, []uint32{0, 0, 0, 0}}, - {"zero buf nonzero in", [4]uint32{0, 0, 0, 0}, []uint32{1, 2, 3, 4}}, - {"all ones", [4]uint32{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}, []uint32{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}}, - {"mixed values", [4]uint32{0x12345678, 0x9abcdef0, 0x0fedcba9, 0x87654321}, []uint32{0xdeadbeef, 0xcafebabe, 0xfeedface, 0x0d15ea5e}}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := TEATransform(tt.buf, tt.in) - // TEA is deterministic — calling with the same input must return the same output - result2 := TEATransform(tt.buf, tt.in) - if result != result2 { - t.Errorf("TEATransform is not deterministic: first %v, second %v", result, result2) - } - // Verify it actually changed from the initial buf (except for zero case which still shifts) - // TEATransform adds to buf[0] and buf[1], so the result should differ from the original - // unless in some degenerate case. We just verify it doesn't panic and is deterministic. - }) - } -} - -// TestStr2hashbuf tests the str2hashbuf helper function that converts strings to uint32 slices -func TestStr2hashbuf(t *testing.T) { +func TestExt4fsDirhashConformance(t *testing.T) { + seed := &[4]uint32{0x11111111, 0x22222222, 0x33333333, 0x44444444} tests := []struct { - name string - msg string - num int - signed bool + version hashVersion + name string + seed *[4]uint32 + major, minor uint32 }{ - {"empty string 8 words", "", 8, false}, - {"short string 8 words", "hello", 8, false}, - {"exact 32 bytes", "abcdefghijklmnopqrstuvwxyz012345", 8, false}, - {"longer than num*4", "this is a very long string that exceeds the limit", 4, false}, - {"single char", "a", 8, false}, - {"signed mode", "hello", 8, true}, - {"num 1", "hello", 1, false}, + {0, "abc", nil, 0x75afd992, 0}, + {3, "abc", nil, 0x75afd992, 0}, + {1, "abc", nil, 0xd196a868, 0xc420eb28}, + {4, "abc", nil, 0xd196a868, 0xc420eb28}, + {2, "abc", nil, 0xb1435ec4, 0x3f7eaa0e}, + {5, "abc", nil, 0xb1435ec4, 0x3f7eaa0e}, + {0, "", nil, 0x2547fc5a, 0}, + {3, "", nil, 0x2547fc5a, 0}, + {1, "", nil, 0xefcdab88, 0x98badcfe}, + {4, "", nil, 0xefcdab88, 0x98badcfe}, + {2, "", nil, 0x67452300, 0xefcdab89}, + {5, "", nil, 0x67452300, 0xefcdab89}, + {1, "1234567890123456789012345678901", nil, 0xc4db1f78, 0xea23921b}, + {1, "12345678901234567890123456789012", nil, 0xfa6cc63e, 0x2f77bd1c}, + {1, "123456789012345678901234567890123", nil, 0xdc0c2dec, 0x5ca23365}, + {4, "1234567890123456789012345678901", nil, 0xc4db1f78, 0xea23921b}, + {4, "12345678901234567890123456789012", nil, 0xfa6cc63e, 0x2f77bd1c}, + {4, "123456789012345678901234567890123", nil, 0xdc0c2dec, 0x5ca23365}, + {2, "123456789abcdef", nil, 0xa562903a, 0x6174a00f}, + {2, "1234567890abcdef", nil, 0x8449f258, 0x49a16d46}, + {2, "123456789abcdefgh", nil, 0xf32ec10c, 0x58ceae61}, + {1, "same-name", seed, 0x8aebf604, 0x66ce48fe}, + {1, "same-name", &[4]uint32{0, 1, 0, 0xffffffff}, 0xb7b44c20, 0x3cb6a3d3}, + {2, "same-name", &[4]uint32{0, 1, 0, 0xffffffff}, 0xdcbbc302, 0x76d1a477}, + {1, "\x80\x81\x82\x83\x84", nil, 0x8bab0498, 0xc326632d}, + {4, "\x80\x81\x82\x83\x84", nil, 0xbc48596e, 0xde0fad41}, + {2, "\x80\x81\x82\x83\x84", nil, 0x21e3a154, 0x90112c3d}, + {5, "\x80\x81\x82\x83\x84", nil, 0x9b648616, 0x011dd507}, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := str2hashbuf(tt.msg, tt.num, tt.signed) - if result == nil { - t.Fatalf("str2hashbuf returned nil") + for _, tc := range tests { + t.Run(fmt.Sprintf("%d/%x", tc.version, tc.name), func(t *testing.T) { + info := dirHashInfo{version: tc.version, seed: tc.seed} + if err := ext4fsDirhash([]byte(tc.name), &info); err != nil { + t.Fatal(err) } - // The returned slice is from a fixed [8]uint32 array - if len(result) != 8 { - t.Errorf("expected length 8, got %d", len(result)) + if info.hash != tc.major || info.minorHash != tc.minor { + t.Errorf("got (%08x, %08x), want (%08x, %08x)", info.hash, info.minorHash, tc.major, tc.minor) } }) } } -// TestDxHackHash tests the legacy directory hash function -func TestDxHackHash(t *testing.T) { +func TestHashWords(t *testing.T) { tests := []struct { - name string - input string - signed bool + name string + unsigned bool + want [4]uint32 }{ - {"empty string unsigned", "", false}, - {"single char unsigned", "a", false}, - {"short name unsigned", "hello", false}, - {"typical filename unsigned", "testfile.txt", false}, - {"max length name unsigned", strings.Repeat("a", 255), false}, - {"empty string signed", "", true}, - {"short name signed", "hello", true}, + {"", false, [4]uint32{}}, + {"a", false, [4]uint32{0x01010161, 0x01010101, 0x01010101, 0x01010101}}, + {"ab", false, [4]uint32{0x02026162, 0x02020202, 0x02020202, 0x02020202}}, + {"abc", false, [4]uint32{0x03616263, 0x03030303, 0x03030303, 0x03030303}}, + {"abcd", false, [4]uint32{0x61626364, 0x04040404, 0x04040404, 0x04040404}}, + {"\x80\x81\x82\x83\x84", false, [4]uint32{0x7f808183, 0x05050484, 0x05050505, 0x05050505}}, + {"\x80\x81\x82\x83\x84", true, [4]uint32{0x80818283, 0x05050584, 0x05050505, 0x05050505}}, + {"\xff", false, [4]uint32{0x010100ff, 0x01010101, 0x01010101, 0x01010101}}, + {"\x00\xff", false, [4]uint32{0x0201ffff, 0x02020202, 0x02020202, 0x02020202}}, + {"\x00\x00\xff", false, [4]uint32{0x02ffffff, 0x03030303, 0x03030303, 0x03030303}}, + {"\x80\x00\x00\x00", false, [4]uint32{0x80000000, 0x04040404, 0x04040404, 0x04040404}}, + {"\x00\x80\x00\x00", false, [4]uint32{0xff800000, 0x04040404, 0x04040404, 0x04040404}}, + {"\x00\x00\x80\x00", false, [4]uint32{0xffff8000, 0x04040404, 0x04040404, 0x04040404}}, + {"\x00\x00\x00\x80", false, [4]uint32{0xffffff80, 0x04040404, 0x04040404, 0x04040404}}, + {"abcdefghijklmnopq", false, [4]uint32{0x61626364, 0x65666768, 0x696a6b6c, 0x6d6e6f70}}, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - hash := dxHackHash(tt.input, tt.signed) - // Verify determinism - hash2 := dxHackHash(tt.input, tt.signed) - if hash != hash2 { - t.Errorf("dxHackHash is not deterministic: first %d, second %d", hash, hash2) - } - // Verify the result is left-shifted by 1 (i.e. lowest bit is 0) - if hash&1 != 0 { - t.Errorf("dxHackHash result should have lowest bit 0 (shifted left by 1), got 0x%08x", hash) + for _, tc := range tests { + t.Run(fmt.Sprintf("%x/unsigned=%t", tc.name, tc.unsigned), func(t *testing.T) { + got := [4]uint32{0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef} + hashWords([]byte(tc.name), got[:], tc.unsigned) + if got != tc.want { + t.Errorf("got %08x, want %08x", got, tc.want) } }) } - - // Different inputs should (generally) produce different hashes - t.Run("different inputs differ", func(t *testing.T) { - h1 := dxHackHash("file1.txt", false) - h2 := dxHackHash("file2.txt", false) - h3 := dxHackHash("completely_different_name", false) - // It's theoretically possible for collisions, but these specific strings should differ - if h1 == h2 && h2 == h3 { - t.Errorf("all three different inputs produced the same hash: 0x%08x", h1) - } - }) + // Spare output words expose padding for a length above 255 without clamping + // the remaining length to a byte or to a transform block's capacity. + var words [80]uint32 + hashWords(make([]byte, 256), words[:], true) + if words[64] != 0x01010100 || words[79] != 0x01010100 { + t.Fatalf("256-byte padding = %#x, %#x; want 0x01010100", words[64], words[79]) + } } -// TestExt4fsDirhash tests the main directory hash entry point with all supported hash versions -func TestExt4fsDirhash(t *testing.T) { - noSeed := []uint32{0, 0, 0, 0} - customSeed := []uint32{0x12345678, 0x9abcdef0, 0x0fedcba9, 0x87654321} - - hashVersions := []struct { - version hashVersion - name string - }{ - {HashVersionLegacy, "Legacy"}, - {HashVersionHalfMD4, "HalfMD4"}, - {HashVersionTEA, "TEA"}, - {HashVersionLegacyUnsigned, "LegacyUnsigned"}, - {HashVersionHalfMD4Unsigned, "HalfMD4Unsigned"}, - {HashVersionTEAUnsigned, "TEAUnsigned"}, +func TestTEATransform(t *testing.T) { + tests := []struct{ state, input, want [4]uint32 }{ + {[4]uint32{}, [4]uint32{}, [4]uint32{0xa889f798, 0x182d8083, 0, 0}}, + {[4]uint32{0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}, + [4]uint32{0x03616263, 0x03030303, 0x03030303, 0x03030303}, + [4]uint32{0xb1435ec5, 0x3f7eaa0e, 0x98badcfe, 0x10325476}}, + {[4]uint32{0xffffffff, 0, 0x12345678, 0x87654321}, + [4]uint32{0xffffffff, 0x12345678, 0x87654321, 0}, + [4]uint32{0xcf2c03bb, 0x2e8aeeda, 0x12345678, 0x87654321}}, } - - filenames := []string{ - "", - "a", - "hello.txt", - "my_document.pdf", - "very_long_filename_that_is_used_to_test_how_the_hash_function_handles_longer_inputs.data", - strings.Repeat("x", 255), // max ext4 filename length + for _, tc := range tests { + state := tc.state + TEATransform(&state, tc.input) + if state != tc.want { + t.Errorf("state %08x, input %08x: got %08x, want %08x", tc.state, tc.input, state, tc.want) + } } +} - for _, hv := range hashVersions { - t.Run(hv.name, func(t *testing.T) { - for _, name := range filenames { - displayName := name - if len(displayName) > 30 { - displayName = displayName[:30] + "..." +func TestDirhashSeedsAndAdjustment(t *testing.T) { + for version := hashVersion(0); version <= 5; version++ { + for _, name := range [][]byte{nil, []byte("same-name"), bytes.Repeat([]byte{0x80, 0, 0xff}, 23)} { + plain := dirHashInfo{version: version} + zero := dirHashInfo{version: version, seed: &[4]uint32{}} + if err := ext4fsDirhash(name, &plain); err != nil { + t.Fatal(err) + } + if err := ext4fsDirhash(name, &zero); err != nil { + t.Fatal(err) + } + if plain.hash != zero.hash || plain.minorHash != zero.minorHash { + t.Errorf("version %d: zero seed differs from absent seed", version) + } + seed := [4]uint32{0, 1, 0, 0xffffffff} + custom := dirHashInfo{version: version, seed: &seed} + if err := ext4fsDirhash(name, &custom); err != nil { + t.Fatal(err) + } + if seed != [4]uint32{0, 1, 0, 0xffffffff} { + t.Fatal("seed mutated") + } + if version == 0 || version == 3 { + if custom.hash != plain.hash || custom.minorHash != plain.minorHash { + t.Error("legacy depends on seed") } - t.Run(fmt.Sprintf("name=%q", displayName), func(t *testing.T) { - // Test with no seed - hash, minor := ext4fsDirhash(name, hv.version, noSeed) - - // Verify determinism - hash2, minor2 := ext4fsDirhash(name, hv.version, noSeed) - if hash != hash2 || minor != minor2 { - t.Errorf("ext4fsDirhash not deterministic: (%d,%d) vs (%d,%d)", hash, hash2, minor, minor2) - } - - // The hash should have the lowest bit cleared (hash &= ^uint32(1)) - if hash&1 != 0 { - t.Errorf("hash should have lowest bit cleared, got 0x%08x", hash) - } - - // Test with custom seed - hashS, minorS := ext4fsDirhash(name, hv.version, customSeed) - hashS2, minorS2 := ext4fsDirhash(name, hv.version, customSeed) - if hashS != hashS2 || minorS != minorS2 { - t.Errorf("ext4fsDirhash not deterministic with seed: (%d,%d) vs (%d,%d)", hashS, hashS2, minorS, minorS2) - } - - // For non-legacy versions, different seeds should generally produce different hashes - // (Legacy ignores seed since it uses dxHackHash) - if hv.version != HashVersionLegacy && hv.version != HashVersionLegacyUnsigned && name != "" { - if hash == hashS && minor == minorS { - // Collisions are possible but unlikely for non-trivial inputs - t.Logf("WARNING: same hash with different seeds for %q: hash=0x%08x minor=0x%08x", displayName, hash, minor) - } - } - }) } - }) + if version == 2 || version == 5 { + other := dirHashInfo{version: version, seed: &[4]uint32{0, 1, 2, 3}} + if err := ext4fsDirhash(name, &other); err != nil { + t.Fatal(err) + } + if custom.hash != other.hash || custom.minorHash != other.minorHash { + t.Error("TEA depends on seed words 2 and 3") + } + } + } } -} - -// TestExt4fsDirhashUnknownVersion tests that an unknown hash version returns zero -func TestExt4fsDirhashUnknownVersion(t *testing.T) { - hash, minor := ext4fsDirhash("test", hashVersion(99), []uint32{0, 0, 0, 0}) - if hash != 0 || minor != 0 { - t.Errorf("expected (0,0) for unknown hash version, got (%d,%d)", hash, minor) + for _, raw := range []uint32{0, 1, 2, 0x80000001, 0xfffffffd, 0xfffffffe, 0xffffffff} { + want := raw &^ 1 + if want == 0xfffffffe { + want = 0xfffffffc + } + for _, version := range []hashVersion{1, 2, 4, 5} { + seed := [4]uint32{raw, raw, 0xffffffff, 0} + info := dirHashInfo{version: version, seed: &seed} + if err := ext4fsDirhash(nil, &info); err != nil { + t.Fatal(err) + } + minor := uint32(0xffffffff) + if version == 2 || version == 5 { + minor = raw + } + if info.hash != want || info.minorHash != minor { + t.Errorf("version %d raw %#x: got (%#x, %#x), want (%#x, %#x)", version, raw, info.hash, info.minorHash, want, minor) + } + } } } -// TestExt4fsDirhashSIPVersion tests that SIP hash version (unimplemented) returns zero -func TestExt4fsDirhashSIPVersion(t *testing.T) { - hash, minor := ext4fsDirhash("test", HashVersionSIP, []uint32{0, 0, 0, 0}) - if hash != 0 || minor != 0 { - t.Errorf("expected (0,0) for SIP hash version (unimplemented), got (%d,%d)", hash, minor) +func TestDirhashErrors(t *testing.T) { + for _, version := range []hashVersion{7, 255} { + info := dirHashInfo{version: version, hash: 123, minorHash: 456} + if err := ext4fsDirhash(nil, &info); !errors.Is(err, syscall.EINVAL) { + t.Fatalf("version %d: error %v", version, err) + } + if info.hash != 0 || info.minorHash != 0 { + t.Error("unsupported version did not clear outputs") + } + } + for _, name := range [][]byte{nil, []byte("abc")} { + info := dirHashInfo{version: HashVersionSIP, hash: 123, minorHash: 456} + if err := ext4fsDirhash(name, &info); !errors.Is(err, syscall.EINVAL) { + t.Fatalf("missing key: error %v", err) + } + if info.hash != 123 || info.minorHash != 456 { + t.Error("missing key modified outputs") + } } } -// TestExt4fsDirhashDifferentNamesProduceDifferentHashes verifies that typical different filenames -// produce different hash values for each hash version (collision avoidance check) -func TestExt4fsDirhashDifferentNamesProduceDifferentHashes(t *testing.T) { - noSeed := []uint32{0, 0, 0, 0} - names := []string{ - "file1.txt", - "file2.txt", - "README.md", - "main.go", - "config.json", - "data.bin", - "image.png", - "test_file", - ".hidden", - "Makefile", +func TestDirhashBlockBoundaries(t *testing.T) { + // Independently calculated from the spec using the repeating bytes below. + tests := []struct { + length int + want [6][2]uint32 + }{ + {1, [6][2]uint32{{0x949fce4c, 0}, {0x8ab2d53a, 0x85fa26f2}, {0xa6122120, 0x637dfbd4}, {0x949fce4c, 0}, {0x8ab2d53a, 0x85fa26f2}, {0xa6122120, 0x637dfbd4}}}, + {2, [6][2]uint32{{0x1e1f5db6, 0}, {0x5103ab52, 0x899387f7}, {0xa4ac7f02, 0x567c49aa}, {0x1e1f5db6, 0}, {0x5103ab52, 0x899387f7}, {0xa4ac7f02, 0x567c49aa}}}, + {3, [6][2]uint32{{0x21622502, 0}, {0xacd6be64, 0x4bfb34b5}, {0x6d1420d8, 0xb00d5b13}, {0x07dd7704, 0}, {0x799c1aa0, 0x7c1ea45a}, {0x3bc23a08, 0xf2b2e4f2}}}, + {4, [6][2]uint32{{0xfc66fccc, 0}, {0xa6d20e80, 0x20aeceff}, {0xc832b2fe, 0x58072e3d}, {0xfcd630c8, 0}, {0x17498c48, 0x987b75a7}, {0xc50a41de, 0x7048d523}}}, + {15, [6][2]uint32{{0x005150c6, 0}, {0x778339c8, 0x89e5671f}, {0x0c02437a, 0x310d9e3f}, {0x254b3944, 0}, {0x4d12b838, 0xc6781b32}, {0xf7c1b13c, 0xcba26694}}}, + {16, [6][2]uint32{{0x8a0f4e48, 0}, {0x04b3b2f0, 0xaca227eb}, {0x0ca505ec, 0xdbb492bc}, {0xb8619fb4, 0}, {0xdf09a868, 0x4026d991}, {0x6ee2ddae, 0x6ca612cb}}}, + {17, [6][2]uint32{{0xe6993224, 0}, {0xd5ab1870, 0x57343c16}, {0x791f2cf8, 0x2ea51591}, {0xf97469e6, 0}, {0xc35ce184, 0xc8b93ea5}, {0x9e78ce4e, 0xe8b9d5d5}}}, + {31, [6][2]uint32{{0x3713a346, 0}, {0xec70c2b6, 0x6125052e}, {0x5eb472f8, 0x499f612f}, {0x6a6d690a, 0}, {0xf5e48eb0, 0x05306e8e}, {0x34ef6dd6, 0x7125becd}}}, + {32, [6][2]uint32{{0x4c194ad4, 0}, {0x95a86b20, 0xe6fcf333}, {0x748b9602, 0xccc39c51}, {0xfa47d9d2, 0}, {0xb1f8c71e, 0x86557017}, {0x5c6efaa4, 0xfe6f8daf}}}, + {33, [6][2]uint32{{0x15d7e51a, 0}, {0xa7c20a9a, 0x68449ffb}, {0x7de0ba1a, 0x2c3c5b6e}, {0x01d295de, 0}, {0xb26ba11c, 0xd3d47e1d}, {0x1c5dd3d4, 0xa3b13cfb}}}, + {63, [6][2]uint32{{0x3d3de7f4, 0}, {0x5fa04676, 0x2ba356d9}, {0x72073bd4, 0x3be928ec}, {0x88149f48, 0}, {0xce52caa4, 0xc9bd3d59}, {0xbf58314e, 0xbb3430c2}}}, + {64, [6][2]uint32{{0x9860bb4e, 0}, {0x3a611734, 0x56da916f}, {0xae9a732c, 0xefcb683b}, {0xba27acc0, 0}, {0xafb72ff0, 0x2038e23a}, {0x959a56bc, 0x26e57cb8}}}, + {65, [6][2]uint32{{0x4915d39a, 0}, {0x7d855388, 0xe0058d1e}, {0xbdd8b4c2, 0x09c15a61}, {0x5fef3574, 0}, {0xd51c3678, 0x340dc813}, {0x40e39a6a, 0x02ebfbb6}}}, } + for _, tc := range tests { + for version := hashVersion(0); version <= 5; version++ { + t.Run(fmt.Sprintf("%d/length=%d", version, tc.length), func(t *testing.T) { + name := bytes.Repeat([]byte{0, 0x7f, 0x80, 0xff, 0x81}, 13)[:tc.length] + original := bytes.Clone(name) + info := dirHashInfo{version: version} + for repeat := 0; repeat < 2; repeat++ { + if err := ext4fsDirhash(name, &info); err != nil { + t.Fatal(err) + } + if got := [2]uint32{info.hash, info.minorHash}; got != tc.want[version] { + t.Fatalf("call %d: got %08x, want %08x", repeat, got, tc.want[version]) + } + } + if !bytes.Equal(name, original) { + t.Fatal("input mutated") + } + // ASCII (including embedded NUL) is identical in both byte modes. + for i := range name { + name[i] &= 0x7f + } + signed := dirHashInfo{version: version % 3} + unsigned := dirHashInfo{version: version%3 + 3} + if err := ext4fsDirhash(name, &signed); err != nil { + t.Fatal(err) + } + if err := ext4fsDirhash(name, &unsigned); err != nil { + t.Fatal(err) + } + if signed.hash != unsigned.hash || signed.minorHash != unsigned.minorHash { + t.Fatal("ASCII signed and unsigned results differ") + } + }) + } + } +} - versions := []struct { - version hashVersion - name string +func TestSipHash24(t *testing.T) { + // Independent numeric vectors from the specified 64-bit round operations. + // Input bytes and key bytes are their indices modulo 256. + tests := []struct { + length int + want uint64 }{ - {HashVersionHalfMD4, "HalfMD4"}, - {HashVersionTEA, "TEA"}, - {HashVersionHalfMD4Unsigned, "HalfMD4Unsigned"}, - {HashVersionTEAUnsigned, "TEAUnsigned"}, - {HashVersionLegacy, "Legacy"}, - {HashVersionLegacyUnsigned, "LegacyUnsigned"}, + {0, 0x726fdb47dd0e0e31}, {1, 0x74f839c593dc67fd}, + {2, 0x0d6c8009d9a94f5a}, {3, 0x85676696d7fb7e2d}, + {4, 0xcf2794e0277187b7}, {5, 0x18765564cd99a68d}, + {6, 0xcbc9466e58fee3ce}, {7, 0xab0200f58b01d137}, + {8, 0x93f5f5799a932462}, {9, 0x9e0082df0ba9e4b0}, + {10, 0x7a5dbbc594ddb9f3}, {11, 0xf4b32f46226bada7}, + {12, 0x751e8fbc860ee5fb}, {13, 0x14ea5627c0843d90}, + {14, 0xf723ca908e7af2ee}, {15, 0xa129ca6149be45e5}, + {16, 0x3f2acc7f57c29bdb}, {17, 0x699ae9f52cbe4794}, + {31, 0x32d892fad841c342}, {32, 0x7127512f72f27cce}, + {33, 0xa7f32346f95978e3}, {63, 0x958a324ceb064572}, + {64, 0xacd2c40b8502cad8}, {65, 0x36b6bedb91a25924}, + {255, 0xa9c169fec74db21a}, {256, 0x999d0526d2a7bfd7}, + {257, 0x8a817b8d55b29748}, } - - for _, hv := range versions { - t.Run(hv.name, func(t *testing.T) { - hashes := make(map[uint32]string) - collisions := 0 - for _, name := range names { - hash, _ := ext4fsDirhash(name, hv.version, noSeed) - if existingName, exists := hashes[hash]; exists { - collisions++ - t.Logf("collision: %q and %q both hash to 0x%08x", existingName, name, hash) + var key [16]byte + for i := range key { + key[i] = byte(i) + } + for _, tc := range tests { + t.Run(fmt.Sprint(tc.length), func(t *testing.T) { + for offset := 0; offset < 8; offset++ { + name := make([]byte, offset+tc.length)[offset:] + for i := range name { + name[i] = byte(i) + } + if got := sipHash24(name, key); got != tc.want { + t.Fatalf("offset %d: got %016x, want %016x", offset, got, tc.want) + } + info := dirHashInfo{version: HashVersionSIP, sipHashKey: &key, seed: &[4]uint32{1, 2, 3, 4}} + if err := ext4fsDirhash(name, &info); err != nil { + t.Fatal(err) + } + if uint64(info.hash) != (tc.want>>32)&^1 || uint64(info.minorHash) != tc.want&0xffffffff { + t.Fatalf("incorrect major/minor selection: (%08x, %08x)", info.hash, info.minorHash) } - hashes[hash] = name - } - // We expect very few if any collisions among 10 distinct filenames - if collisions > 2 { - t.Errorf("too many collisions (%d out of %d names) for hash version %s", collisions, len(names), hv.name) } }) } -} - -// TestExt4fsDirhashMinorHash verifies that HalfMD4 and TEA variants produce non-trivial minor hashes -func TestExt4fsDirhashMinorHash(t *testing.T) { - noSeed := []uint32{0, 0, 0, 0} - names := []string{"file1.txt", "file2.txt", "README.md", "main.go", "config.json"} - - // HalfMD4 variants should produce minor hashes from buf[2] - t.Run("HalfMD4", func(t *testing.T) { - allZero := true - for _, name := range names { - _, minor := ext4fsDirhash(name, HashVersionHalfMD4, noSeed) - if minor != 0 { - allZero = false - break - } - } - if allZero { - t.Errorf("all minor hashes are zero for HalfMD4 — expected some non-zero values") - } - }) - - // TEA variants should produce minor hashes from buf[1] - t.Run("TEA", func(t *testing.T) { - allZero := true - for _, name := range names { - _, minor := ext4fsDirhash(name, HashVersionTEA, noSeed) - if minor != 0 { - allZero = false - break - } - } - if allZero { - t.Errorf("all minor hashes are zero for TEA — expected some non-zero values") - } - }) - - // Legacy versions do NOT produce minor hashes (always 0) - t.Run("Legacy", func(t *testing.T) { - for _, name := range names { - _, minor := ext4fsDirhash(name, HashVersionLegacy, noSeed) - if minor != 0 { - t.Errorf("expected minor hash 0 for Legacy version, got %d for %q", minor, name) - } - } - }) -} - -// TestExt4fsDirhashLongNameChunking tests that long names are properly chunked in the hash computation. -// HalfMD4 processes in 32-byte chunks, TEA in 16-byte chunks. -func TestExt4fsDirhashLongNameChunking(t *testing.T) { - noSeed := []uint32{0, 0, 0, 0} - - // Names that differ only after the first chunk boundary should produce different hashes - t.Run("HalfMD4 differ after 32 bytes", func(t *testing.T) { - prefix := strings.Repeat("a", 32) - name1 := prefix + "XXXX" - name2 := prefix + "YYYY" - h1, _ := ext4fsDirhash(name1, HashVersionHalfMD4, noSeed) - h2, _ := ext4fsDirhash(name2, HashVersionHalfMD4, noSeed) - if h1 == h2 { - t.Errorf("expected different hashes for names differing after 32 bytes, both got 0x%08x", h1) - } - }) - - t.Run("TEA differ after 16 bytes", func(t *testing.T) { - prefix := strings.Repeat("b", 16) - name1 := prefix + "XXXX" - name2 := prefix + "YYYY" - h1, _ := ext4fsDirhash(name1, HashVersionTEA, noSeed) - h2, _ := ext4fsDirhash(name2, HashVersionTEA, noSeed) - if h1 == h2 { - t.Errorf("expected different hashes for names differing after 16 bytes, both got 0x%08x", h1) - } - }) + info := dirHashInfo{version: HashVersionSIP, sipHashKey: &[16]byte{}} + if err := ext4fsDirhash(nil, &info); err != nil { + t.Fatal(err) + } + if info.hash != 0x1e924b9c || info.minorHash != 0x737700d7 { + t.Fatalf("zero key empty hash: (%08x, %08x)", info.hash, info.minorHash) + } } diff --git a/filesystem/ext4/ext4-hash-spec.md b/filesystem/ext4/ext4-hash-spec.md new file mode 100644 index 00000000..23369496 --- /dev/null +++ b/filesystem/ext4/ext4-hash-spec.md @@ -0,0 +1,269 @@ +# ext4 directory filename hashing specification + +## Source and scope + +This specification describes the Linux kernel source at revision **05441d697f7f9a927264f4058cbbc6a03dfcac9d**, resolved from torvalds/linux master during this review on 4 November 2026. All algorithm requirements below refer to that revision, rather than to a moving branch or a general-purpose hash bearing the same name. + +The primary source is [fs/ext4/hash.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/hash.c), reviewed in full. Supporting sources are linked where their behavior is described. This document contains prose, mathematical definitions, and data tables, but no implementation code. It is based on the referenced Linux sources, without consulting another implementation. + +The subject is the computation of a directory filename's major and minor hashes, including all supported versions, byte conversion, compression transforms, seed selection, output adjustment, errors, and the optional case-folding wrapper. Directory index traversal, block splitting, filesystem checksums, encryption of filenames, and provisioning encryption keys are separate operations. A caller using encrypted directories must supply an already established directory hash key; the four-word filesystem hash seed is not that key. + +## Inputs, arithmetic, and output contract + +The core operation takes an ordered sequence of filename bytes, an explicit nonnegative byte length, a hash version, and an optional seed consisting of exactly four 32-bit words. SipHash additionally requires the parent directory's available encryption key and initialized directory hash key. The outer wrapper also depends on the directory's case-fold and encryption flags, the filesystem's Unicode map, and whether Unicode support is enabled. + +The result consists of two separate unsigned 32-bit numbers: the **major hash** and the **minor hash**. Their order and selection are specified for each algorithm. They are not a generic serialized digest. Successful kernel calls return status zero. + +Lengths count bytes, not Unicode characters. The core consumes exactly the stated number of bytes, with no trailing terminator added and no special interpretation of zero bytes. Filename validity and directory entry length limits are responsibilities of the surrounding filesystem. Negative lengths and insufficient input storage are outside this specification's valid input domain. + +Unless a section explicitly says 64-bit, arithmetic on hash words is unsigned modulo 2³². Discard carries beyond bit 31 after addition, subtraction, multiplication, or left shifts. Bit numbers start at zero at the least significant bit. Right shifts insert zeros. Left rotations wrap the high bits back into the low positions within the stated word width. Bitwise complements are limited to that width. Numeric constants beginning with 0x are hexadecimal; other numbers in this document are decimal. + +A signed-byte version interprets each byte below 128 as its ordinary value and each byte at least 128 as its value minus 256. Thus 0x80 means −128 and 0xff means −1. When such a value enters word arithmetic it is represented modulo 2³², preserving sign extension. Unsigned-byte versions interpret every byte as a value from 0 through 255. Version selection must determine this explicitly, independently of the implementation language's character type or host architecture. + +## Hash versions and filesystem selection + +The version identifiers are defined in [fs/ext4/ext4.h](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/ext4.h#L2659). + +| Identifier | Algorithm | Byte interpretation | Raw major | Raw minor | +| --- | --- | --- | --- | --- | +| 0 | Legacy | Signed | Final legacy accumulator shifted left one bit | Zero | +| 1 | Half MD4 | Signed | State word 1 | State word 2 | +| 2 | TEA | Signed | State word 0 | State word 1 | +| 3 | Legacy | Unsigned | Final legacy accumulator shifted left one bit | Zero | +| 4 | Half MD4 | Unsigned | State word 1 | State word 2 | +| 5 | TEA | Unsigned | State word 0 | State word 1 | +| 6 | SipHash-2-4 | Raw bytes | High 32 bits of the 64-bit result | Low 32 bits of the 64-bit result | + +Every successful version then applies the common major-hash adjustment described below. Minor hashes are never adjusted. + +The identifiers accepted by the computational routine differ from the identifiers accepted in an indexed directory's root. In [fs/ext4/namei.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/namei.c#L779), root identifiers 0, 1, 2, and 6 are accepted. For 0 through 2, a filesystem-wide unsigned-hash offset of either zero or three is added before hashing. Identifier 6 is accepted only for directories whose entries store hashes, which are encrypted, case-folded directories. Such directories must use identifier 6. + +[fs/ext4/super.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/super.c#L5296) converts each of the four on-disk seed words from little-endian into its numeric 32-bit value. It selects offset three when the filesystem's unsigned-hash flag, 0x0002, is set. Otherwise the signed-hash flag, 0x0001, selects offset zero. If neither flag is set, the kernel uses its native character signedness to choose the offset and records the corresponding flag on a writable filesystem. The unsigned flag takes precedence if both flags are set. These rules concern resolving filesystem metadata; a caller that already has an effective version from the table must not add the offset again. + +The mount initialization accepts default version values 0 through 5 and rejects 6 and larger values. This does not change the narrower root validation above. A newly indexed encrypted, case-folded directory selects SipHash independently of the ordinary default. + +## Seed selection + +Half MD4 and TEA initialize a four-word state, indexed from zero, to the following numeric values in order: + +| State word | Default value | +| --- | --- | +| 0 | 0x67452301 | +| 1 | 0xefcdab89 | +| 2 | 0x98badcfe | +| 3 | 0x10325476 | + +An absent seed and an all-zero four-word seed both select this default. If **any** supplied seed word is nonzero, replace **all four** default words with the supplied seed, retaining any zero words it contains. Do this once for the entire filename. Do not reinitialize between blocks. + +Legacy ignores the seed. SipHash ignores this four-word seed and instead uses the separate 128-bit directory hash key. TEA's transform uses only state words 0 and 1; words 2 and 3 cannot affect its result. + +## Converting bytes into transform input words + +Half MD4 and TEA use the same conversion procedure, with a capacity of eight words / 32 bytes for Half MD4 and four words / 16 bytes for TEA. This conversion is not MD4 message padding. + +For each block, let L be the number of filename bytes remaining **before** limiting consumption to the block capacity. Construct a 32-bit padding word in two stages: bitwise-OR L with L shifted left eight bits, then bitwise-OR that result with itself shifted left sixteen bits. All shifts and results use the 32-bit rules above. For L from 0 through 255, this repeats L in each of the four bytes. Do not first truncate L to eight bits for a larger input. + +Consume the smaller of L and the block capacity. Process each complete group of four bytes into one word, starting at zero and, for each byte in order, shifting the accumulator left eight bits and adding the interpreted byte value. For unsigned bytes this is ordinary big-endian word assembly. For signed bytes the additions of negative values can borrow across byte positions; simply assembling unsigned bytes and reinterpreting the final word as signed would be incorrect. + +If capacity remains after all complete groups, start the next word at the padding word. Incorporate each of the remaining zero to three bytes by the same shift-left-eight-and-add operation. Emit this word even when there are zero remaining bytes. Fill every subsequent unused word with the unmodified padding word. If complete groups already filled the capacity, emit no additional word. + +Consequently: + +- A completely full block has no padding word and no extra block. +- A partial block whose consumed byte count is divisible by four has a padding-only word immediately after its complete words, followed by more padding words as necessary. +- With one to three trailing bytes, those bytes occupy the low positions of the next word after repeated shifting of the padding word. +- Padding depends on the current remaining length, not the number of bytes consumed, the original total length on every block, or the final short block's length on every block. +- The core does not perform this conversion at all for an empty filename. + +Examples of conversion data, with words written as hexadecimal numbers: + +| Input and remaining length | Byte interpretation | Beginning of resulting words | +| --- | --- | --- | +| ASCII abc, L = 3 | Either | 0x03616263, then padding words 0x03030303 | +| ASCII abcd, L = 4 | Either | 0x61626364, then padding words 0x04040404 | +| Bytes 80 81 82 83 84, L = 5 | Unsigned | 0x80818283, 0x05050584, then 0x05050505 | +| Bytes 80 81 82 83 84, L = 5 | Signed | 0x7f808183, 0x05050484, then 0x05050505 | + +For a 33-byte Half MD4 input, conversion uses L = 33 for the first 32 bytes and L = 1 for the second block. For a 17-byte TEA input it uses L = 17 and then L = 1. Conversion produces numeric words and is independent of host byte order or alignment. + +## Legacy hash + +Initialize the current accumulator to 0x12a3fe2d and the previous accumulator to 0x37abe8f9. + +For each byte, multiply its signed or unsigned interpreted value by 7,152,373 and represent the product as a 32-bit word. XOR that product with the current accumulator, then add the previous accumulator to obtain a tentative word. If its high bit is set, subtract 0x7fffffff once. This test applies to the tentative word before subtraction; it is not an instruction to repeatedly clear a sign bit. + +Replace the previous accumulator with the old current accumulator and replace the current accumulator with the adjusted tentative word. After the final byte, shift the current accumulator left one bit to obtain the raw major hash. The minor hash is zero. Apply the common output adjustment. + +For an empty input no recurrence is performed, giving major 0x2547fc5a and minor zero. + +## Half MD4 + +### Block processing and state + +Initialize the four state words using the seed rules. Process the filename in successive blocks of at most 32 bytes. Convert each block into eight words using the conversion rules and the current remaining byte count. Compress it into the existing state. Continue with the next 32 bytes until no bytes remain. A final full block does not cause another padding block. + +The compression transform takes eight input words, indexed 0 through 7, and a four-word state. At the start of each transform copy the state into four working registers A, B, C, D, preserving the incoming state for feed-forward. + +Define these functions independently at every bit position: + +- **Selection F(x, y, z):** select the bit of y where x is one and the bit of z where x is zero. +- **Majority G(x, y, z):** produce one if at least two of the three bits are one. +- **Parity H(x, y, z):** XOR the three bits. + +There are three rounds of eight operations each. Each operation adds the current target register, the round's function applied to three ordered registers, the scheduled input word, and the round constant. Rotate that sum left by the scheduled rotation and replace the target with the result. Only that register changes in the operation. Later operations use all earlier updates. + +The register roles for operations 1 through 8 of **each** round are: + +| Operation | Target | Ordered function arguments | +| --- | --- | --- | +| 1 | A | B, C, D | +| 2 | D | A, B, C | +| 3 | C | D, A, B | +| 4 | B | C, D, A | +| 5 | A | B, C, D | +| 6 | D | A, B, C | +| 7 | C | D, A, B | +| 8 | B | C, D, A | + +The complete round schedules are: + +| Round | Function | Constant | Input word indices, in operation order | Left rotations, in operation order | +| --- | --- | --- | --- | --- | +| 1 | F | 0x00000000 | 0, 1, 2, 3, 4, 5, 6, 7 | 3, 7, 11, 19, 3, 7, 11, 19 | +| 2 | G | 0x5a827999 | 1, 3, 5, 7, 0, 2, 4, 6 | 3, 5, 9, 13, 3, 5, 9, 13 | +| 3 | H | 0x6ed9eba1 | 3, 7, 2, 6, 1, 5, 0, 4 | 3, 9, 11, 15, 3, 9, 11, 15 | + +Do not reset the registers between rounds. After all 24 operations, add A, B, C, D respectively to the four preserved incoming state words, modulo 2³². These four sums are the outgoing state for the next block. The compression routine's individual return value is outgoing state word 1, but all four outgoing state words must be retained. + +### Final result + +After all blocks, take state word 1 as the raw major hash and state word 2 as the minor hash, then apply the common output adjustment. An empty filename causes no transform; its output is derived directly from the selected initial state. With the default seed it yields major 0xefcdab88 and minor 0x98badcfe. + +This is a reduced MD4-derived construction with eight-word blocks and 24 operations. There is no 0x80 suffix, appended bit length, standard MD4 finalization, or full MD4 digest truncation. + +## TEA + +Initialize the same four-word state under the same seed rules. Process successive blocks of at most 16 bytes, converting each to four words using the current remaining length. A full final block produces no additional block. + +For each compression, preserve incoming state words 0 and 1. Copy them to working words X and Y. Name the four converted input words K0, K1, K2, K3 in their original order. Initialize a 32-bit running sum to zero for this block. + +Perform exactly sixteen iterations, in this order within each iteration: + +1. Add 0x9e3779b9 to the running sum. +2. Form three terms: Y shifted left four bits plus K0; Y plus the updated running sum; and Y shifted right five bits plus K1. XOR the three terms and add the result to X. +3. Using the **updated X**, form three terms: X shifted left four bits plus K2; X plus the same running sum; and X shifted right five bits plus K3. XOR these terms and add the result to Y. + +All terms and updates are 32-bit modular quantities. After the sixteenth iteration, add the final X to preserved incoming state word 0 and the final Y to preserved incoming state word 1. Those sums replace state words 0 and 1. State words 2 and 3 remain unchanged. Reset the running sum to zero at the start of the next block, but retain the updated state. + +After the last block, state word 0 is the raw major hash and state word 1 is the minor hash. Apply the common output adjustment. Empty input performs no transform; with the default seed its major is 0x67452300 and its minor is 0xefcdab89. + +The transform uses sixteen iterations and feed-forward addition to the incoming state. The converted filename block supplies the four mixing words; the filesystem seed supplies the initial state. + +## SipHash + +### Key and ext4 integration + +The kernel's [fscrypt filename hashing helper](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/crypto/fname.c#L457) applies SipHash-2-4 to the given bytes with the directory's initialized secret 128-bit hash key. There is no signed-byte variant and no use of the four-word seed or the conversion procedure above. + +[fs/crypto/keysetup.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/crypto/keysetup.c#L285) obtains this key from fscrypt's key derivation, using its directory-hash context and the directory's nonce. It interprets the resulting sixteen bytes as two little-endian 64-bit words, K0 and K1. The hash operation assumes that key setup has already completed; it does not derive a key from the filesystem seed. Encryption policy and the fscrypt key derivation protocol are outside this filename-hash operation. + +### Exact SipHash-2-4 operation + +The supporting algorithm is defined by [include/linux/siphash.h](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/include/linux/siphash.h) and [lib/crypto/siphash.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/lib/crypto/siphash.c). In this section only, use 64-bit modular addition, shifts, rotations, and bitwise operations. + +Initialize four working words V0 through V3 by XORing these constants with the indicated key word: + +| Word | Constant | Key word | +| --- | --- | --- | +| V0 | 0x736f6d6570736575 | K0 | +| V1 | 0x646f72616e646f6d | K1 | +| V2 | 0x6c7967656e657261 | K0 | +| V3 | 0x7465646279746573 | K1 | + +One SipHash round consists of the following sequential updates. Each update uses the latest values: + +1. Add V1 to V0. Rotate V1 left 13 bits, then XOR it with V0. Rotate V0 left 32 bits. +2. Add V3 to V2. Rotate V3 left 16 bits, then XOR it with V2. +3. Add V3 to V0. Rotate V3 left 21 bits, then XOR it with V0. +4. Add V1 to V2. Rotate V1 left 17 bits, then XOR it with V2. Rotate V2 left 32 bits. + +Consume every complete eight-byte group as a little-endian 64-bit numeric word M. For each group, XOR M into V3, perform two rounds, then XOR M into V0. + +Construct a final word with the low eight bits of the total input byte length in its most significant byte. Place the remaining zero to seven input bytes into its lowest byte positions in little-endian order; all other positions are zero. This final word is always processed, even for empty input or an exact multiple of eight bytes. + +XOR the final word into V3, perform two rounds, and XOR the final word into V0. Next XOR 0xff into V2, perform four rounds, and XOR V0, V1, V2, V3 together to produce the 64-bit result. + +ext4 selects the high 32 bits as the raw major hash and the low 32 bits as the minor hash, then applies the common major-hash adjustment. Do not exchange those halves based on host byte order. + +## Common major-hash adjustment + +For every successful version, clear bit zero of the raw major hash. If the resulting value is 0xfffffffe, replace it with 0xfffffffc. Leave all other bits and the minor hash unchanged. + +The reserved value follows from the 32-bit htree end-of-directory constant 0x7fffffff, defined in [fs/ext4/ext4.h](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/ext4.h#L2685): shifting it left once produces 0xfffffffe. Thus raw values 0xfffffffe and 0xffffffff both become 0xfffffffc. Raw 0xfffffffd also becomes 0xfffffffc by ordinary bit clearing. Neither major nor minor is restricted to a signed 31-bit value. + +The resulting major is always even and never 0xfffffffe. The low bit is available to directory index collision bookkeeping: [namei.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/namei.c#L1987) can set it on a split boundary when a hash continues from the preceding block. That bookkeeping does not change the filename hash returned by this operation. + +## Optional Unicode case-folding wrapper + +When Unicode support is compiled in, the public operation first considers case folding. Fold only when the input length is nonzero, the parent directory has its case-fold flag set, and either the directory is unencrypted or its encryption key is available. In all other circumstances, pass the original bytes and length to the core hash operation. + +For folding, allocate a buffer of PATH_MAX bytes; the Linux value is 4096. Failure to allocate returns an out-of-memory error without hashing. Use the filesystem's configured Unicode map, rather than the host runtime's current Unicode version or locale. + +The [Unicode normalization definitions](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/include/linux/unicode.h#L39) and [UTF-8 case-fold routine](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/unicode/utf8-core.c#L100) specify NFDICF: canonical decomposition, removal of default-ignorable code points, and full Unicode case folding using the common and full mappings. This can change byte length and can produce an empty sequence. It is more than ASCII lowercasing or one-character-to-one-character conversion. The filesystem's versioned Unicode tables are an external input to this wrapper; a standalone core byte-hash implementation need not implement those tables. + +On successful folding, hash the folded UTF-8 bytes using their resulting byte length, excluding their terminator. Every block's padding length is based on those resulting bytes. On a negative folding result, including malformed UTF-8 or insufficient buffer capacity, discard the folding attempt and hash the **entire original sequence** with its original length. Do not hash a partially folded prefix or turn this fallback into a hash error. A folded length of zero is a successful fold and uses the core's empty-input behavior. + +Without Unicode support the wrapper always hashes the original sequence. Broader name validation, including any strict encoding policy enforced elsewhere, is separate from this wrapper's fallback behavior. + +Encrypted, case-folded callers can prepare a folded name and precompute its SipHash before directory traversal; [namei.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/namei.c#L1360) then reuses the prepared result. A directory lacking the encryption key may instead use already stored hash values through surrounding filesystem operations. That does not authorize computing a fresh SipHash without the key. + +## Errors and empty-input behavior + +An unsupported effective version returns an invalid-argument error, sets both output hashes to zero, and skips the common adjustment. The kernel also emits a warning identifying the unsupported version. + +SipHash without an available directory encryption key returns an invalid-argument error and emits a warning. This path does **not** assign either output field, so their incoming contents remain unchanged. It does not select another algorithm or produce a zero-key SipHash. Buffer allocation failure in the folding wrapper likewise occurs before output assignments. On any error, callers must use the status rather than treating the output fields as a computed hash. + +For versions 0 through 5, zero-length input is accepted, requires no input-byte access, and follows the empty-input results described above. It can therefore be used to probe support. A zero-length input does not bypass SipHash's key requirement; with a key it produces SipHash of the empty sequence. The comment describing a null-name, zero-length support probe must be understood with this version-specific condition. + +## Conformance data and review cases + +The following data comes from the pinned upstream [fs/ext4/hash-test.c](https://github.com/torvalds/linux/blob/05441d697f7f9a927264f4058cbbc6a03dfcac9d/fs/ext4/hash-test.c). Results already include the common major adjustment. Unless specified otherwise, use the default seed and disable case folding. ASCII inputs contain exactly the displayed characters, with no terminating byte. Hex-byte inputs are literal bytes, including bytes that are invalid UTF-8. + +| Version | Input | Seed | Major | Minor | +| --- | --- | --- | --- | --- | +| 0 | ASCII abc | Default | 0x75afd992 | 0x00000000 | +| 3 | ASCII abc | Default | 0x75afd992 | 0x00000000 | +| 1 | ASCII abc | Default | 0xd196a868 | 0xc420eb28 | +| 4 | ASCII abc | Default | 0xd196a868 | 0xc420eb28 | +| 2 | ASCII abc | Default | 0xb1435ec4 | 0x3f7eaa0e | +| 5 | ASCII abc | Default | 0xb1435ec4 | 0x3f7eaa0e | +| 1 | Empty sequence | Default | 0xefcdab88 | 0x98badcfe | +| 1 | ASCII 1234567890123456789012345678901 | Default | 0xc4db1f78 | 0xea23921b | +| 1 | ASCII 12345678901234567890123456789012 | Default | 0xfa6cc63e | 0x2f77bd1c | +| 1 | ASCII 123456789012345678901234567890123 | Default | 0xdc0c2dec | 0x5ca23365 | +| 4 | ASCII 1234567890123456789012345678901 | Default | 0xc4db1f78 | 0xea23921b | +| 4 | ASCII 12345678901234567890123456789012 | Default | 0xfa6cc63e | 0x2f77bd1c | +| 4 | ASCII 123456789012345678901234567890123 | Default | 0xdc0c2dec | 0x5ca23365 | +| 2 | ASCII 123456789abcdef | Default | 0xa562903a | 0x6174a00f | +| 2 | ASCII 1234567890abcdef | Default | 0x8449f258 | 0x49a16d46 | +| 2 | ASCII 123456789abcdefgh | Default | 0xf32ec10c | 0x58ceae61 | +| 1 | ASCII same-name | 0x11111111, 0x22222222, 0x33333333, 0x44444444 | 0x8aebf604 | 0x66ce48fe | +| 1 | Hex bytes 80 81 82 83 84 | Default | 0x8bab0498 | 0xc326632d | +| 4 | Hex bytes 80 81 82 83 84 | Default | 0xbc48596e | 0xde0fad41 | +| 2 | Hex bytes 80 81 82 83 84 | Default | 0x21e3a154 | 0x90112c3d | +| 5 | Hex bytes 80 81 82 83 84 | Default | 0x9b648616 | 0x011dd507 | + +The upstream tests also establish that different nonzero seeds can change Half MD4 results, all three algorithm families distinguish signed and unsigned high-byte input, unsupported versions clear both output fields, SipHash fails without a key, case-folded ASCII Alpha and aLPHa produce identical hash pairs, and invalid UTF-8 bytes c3 28 take the opaque-byte fallback. + +Additional consequences of the algorithm definitions should be covered when validating a new implementation: + +- Absent and all-zero seeds are equivalent; a partly zero, nonzero seed replaces the entire state. Legacy is seed-independent, and changing only seed words 2 and 3 cannot change TEA. +- Empty Half MD4 and TEA results come directly from the selected initial state. No block is compressed. +- Inputs at lengths 1, 2, 3, 4, 15, 16, 17, 31, 32, 33, 63, 64, and 65 exercise partial words, exact block boundaries, and multiple blocks. Include high bytes in each of the four positions of a complete word and in every partial-word position. +- Signed and unsigned counterparts are identical when every input byte is below 128. Include zero bytes within explicitly sized input to confirm length-based processing. +- Repeated calls start with fresh state; block chaining occurs only within one filename. A digest cannot be obtained by independently hashing blocks and combining the results. +- Common output adjustment affects only the major word, including when an empty-input seed deliberately selects a raw major near 0xffffffff. +- Unicode tests need canonical-equivalent names, full case-fold expansions, default-ignorable characters, invalid sequences, and a fold that becomes empty, all under the same configured Unicode map. +- SipHash tests must use the separate 128-bit key, exercise zero and nonzero byte lengths around multiples of eight, and check high-half versus low-half output selection before major adjustment. Buffer alignment and CPU byte order must not change results. + +These review cases are derived requirements and boundary checks, not additional upstream expected-value vectors. diff --git a/filesystem/ext4/md/md4.go b/filesystem/ext4/md/md4.go new file mode 100644 index 00000000..bbefadd5 --- /dev/null +++ b/filesystem/ext4/md/md4.go @@ -0,0 +1,46 @@ +// Package md implements ext4's reduced MD4 directory-hash transform. +package md + +import "math/bits" + +func f(x, y, z uint32) uint32 { return (x & y) | (^x & z) } +func g(x, y, z uint32) uint32 { return (x & y) | (x & z) | (y & z) } +func h(x, y, z uint32) uint32 { return x ^ y ^ z } + +// HalfMD4Transform compresses eight numeric input words into state in place and +// returns the updated state word 1. It performs 24 operations with feed-forward; +// it does not implement standard MD4 padding or finalization. +func HalfMD4Transform(state *[4]uint32, input [8]uint32) uint32 { + a, b, c, d := state[0], state[1], state[2], state[3] + rounds := [...]struct { + function func(uint32, uint32, uint32) uint32 + constant uint32 + indices [8]int + rotations [4]int + }{ + {f, 0, [8]int{0, 1, 2, 3, 4, 5, 6, 7}, [4]int{3, 7, 11, 19}}, + {g, 0x5a827999, [8]int{1, 3, 5, 7, 0, 2, 4, 6}, [4]int{3, 5, 9, 13}}, + {h, 0x6ed9eba1, [8]int{3, 7, 2, 6, 1, 5, 0, 4}, [4]int{3, 9, 11, 15}}, + } + for _, round := range rounds { + for i, index := range round.indices { + word := input[index] + round.constant + rotation := round.rotations[i%4] + switch i % 4 { + case 0: + a = bits.RotateLeft32(a+round.function(b, c, d)+word, rotation) + case 1: + d = bits.RotateLeft32(d+round.function(a, b, c)+word, rotation) + case 2: + c = bits.RotateLeft32(c+round.function(d, a, b)+word, rotation) + case 3: + b = bits.RotateLeft32(b+round.function(c, d, a)+word, rotation) + } + } + } + state[0] += a + state[1] += b + state[2] += c + state[3] += d + return state[1] +} diff --git a/filesystem/ext4/md/md4_test.go b/filesystem/ext4/md/md4_test.go new file mode 100644 index 00000000..270a58ad --- /dev/null +++ b/filesystem/ext4/md/md4_test.go @@ -0,0 +1,65 @@ +package md + +import "testing" + +func TestBooleanFunctions(t *testing.T) { + // Exhaust the truth tables at every bit position, including the high bit. + for bit := 0; bit < 32; bit++ { + for combination := 0; combination < 8; combination++ { + x, y, z := uint32(combination>>2), uint32((combination>>1)&1), uint32(combination&1) + selection := z + if x == 1 { + selection = y + } + var majority uint32 + if x+y+z >= 2 { + majority = 1 + } + for _, tc := range []struct { + name string + fn func(uint32, uint32, uint32) uint32 + want uint32 + }{ + {"selection", f, selection}, + {"majority", g, majority}, + {"parity", h, (x + y + z) % 2}, + } { + if got := tc.fn(x<> (32 - s)) -} - -// basic MD4 functions -func f(x, y, z uint32) uint32 { - return z ^ (x & (y ^ z)) -} - -func g(x, y, z uint32) uint32 { - return (x & y) + ((x ^ y) & z) -} - -func h(x, y, z uint32) uint32 { - return x ^ y ^ z -} - -// MD4 constants -const ( - k1 uint32 = 0 - k2 uint32 = 0x5A827999 - k3 uint32 = 0x6ED9EBA1 -) - -// round applies the round function as a macro -func round(f func(uint32, uint32, uint32) uint32, a, b, c, d, x uint32, s uint) uint32 { - return rotateLeft(a+f(b, c, d)+x, s) -} - -// halfMD4Transform basic cut-down MD4 transform. Returns only 32 bits of result. -func HalfMD4Transform(buf [4]uint32, in []uint32) uint32 { - var a, b, c, d = buf[0], buf[1], buf[2], buf[3] - - /* Round 1 */ - a = round(f, a, b, c, d, in[0]+k1, 3) - d = round(f, d, a, b, c, in[1]+k1, 7) - c = round(f, c, d, a, b, in[2]+k1, 11) - b = round(f, b, c, d, a, in[3]+k1, 19) - a = round(f, a, b, c, d, in[4]+k1, 3) - d = round(f, d, a, b, c, in[5]+k1, 7) - c = round(f, c, d, a, b, in[6]+k1, 11) - b = round(f, b, c, d, a, in[7]+k1, 19) - - /* Round 2 */ - a = round(g, a, b, c, d, in[1]+k2, 3) - d = round(g, d, a, b, c, in[3]+k2, 5) - c = round(g, c, d, a, b, in[5]+k2, 9) - b = round(g, b, c, d, a, in[7]+k2, 13) - a = round(g, a, b, c, d, in[0]+k2, 3) - d = round(g, d, a, b, c, in[2]+k2, 5) - c = round(g, c, d, a, b, in[4]+k2, 9) - b = round(g, b, c, d, a, in[6]+k2, 13) - - /* Round 3 */ - a = round(h, a, b, c, d, in[3]+k3, 3) - d = round(h, d, a, b, c, in[7]+k3, 9) - c = round(h, c, d, a, b, in[2]+k3, 11) - b = round(h, b, c, d, a, in[6]+k3, 15) - a = round(h, a, b, c, d, in[1]+k3, 3) - d = round(h, d, a, b, c, in[5]+k3, 9) - c = round(h, c, d, a, b, in[0]+k3, 11) - b = round(h, b, c, d, a, in[4]+k3, 15) - - buf[0] += a - buf[1] += b - buf[2] += c - buf[3] += d - - return buf[1] -} diff --git a/filesystem/ext4/md4/md4_test.go b/filesystem/ext4/md4/md4_test.go deleted file mode 100644 index 23c2a1e4..00000000 --- a/filesystem/ext4/md4/md4_test.go +++ /dev/null @@ -1,151 +0,0 @@ -package md4 - -import ( - "testing" -) - -// Test rotateLeft function -func TestRotateLeft(t *testing.T) { - tests := []struct { - x uint32 - s uint - expect uint32 - }{ - {x: 0x12345678, s: 0, expect: 0x12345678}, - {x: 0x12345678, s: 4, expect: 0x23456781}, - {x: 0x12345678, s: 16, expect: 0x56781234}, - {x: 0x12345678, s: 32, expect: 0x12345678}, - } - - for _, tt := range tests { - result := rotateLeft(tt.x, tt.s) - if result != tt.expect { - t.Errorf("rotateLeft(%#x, %d) = %#x; want %#x", tt.x, tt.s, result, tt.expect) - } - } -} - -// Test f function -func TestF(t *testing.T) { - tests := []struct { - x, y, z uint32 - expect uint32 - }{ - {x: 0xFFFFFFFF, y: 0xAAAAAAAA, z: 0x55555555, expect: 0xAAAAAAAA}, - {x: 0x0, y: 0xAAAAAAAA, z: 0x55555555, expect: 0x55555555}, - {x: 0x12345678, y: 0x9ABCDEF0, z: 0x0FEDCBA9, expect: 0x1ffddff1}, - } - - for _, tt := range tests { - result := f(tt.x, tt.y, tt.z) - if result != tt.expect { - t.Errorf("f(%#x, %#x, %#x) = %#x; want %#x", tt.x, tt.y, tt.z, result, tt.expect) - } - } -} - -// Test g function -func TestG(t *testing.T) { - tests := []struct { - x, y, z uint32 - expect uint32 - }{ - {x: 0xFFFFFFFF, y: 0xAAAAAAAA, z: 0x55555555, expect: 0xffffffff}, - {x: 0x0, y: 0xAAAAAAAA, z: 0x55555555, expect: 0x0}, - {x: 0x12345678, y: 0x9ABCDEF0, z: 0x0FEDCBA9, expect: 0x1abcdef8}, - } - - for _, tt := range tests { - result := g(tt.x, tt.y, tt.z) - if result != tt.expect { - t.Errorf("g(%#x, %#x, %#x) = %#x; want %#x", tt.x, tt.y, tt.z, result, tt.expect) - } - } -} - -// Test h function -func TestH(t *testing.T) { - tests := []struct { - x, y, z uint32 - expect uint32 - }{ - {x: 0xFFFFFFFF, y: 0xAAAAAAAA, z: 0x55555555, expect: 0x0}, - {x: 0x0, y: 0xAAAAAAAA, z: 0x55555555, expect: 0xFFFFFFFF}, - {x: 0x12345678, y: 0x9ABCDEF0, z: 0x0FEDCBA9, expect: 0x87654321}, - } - - for _, tt := range tests { - result := h(tt.x, tt.y, tt.z) - if result != tt.expect { - t.Errorf("h(%#x, %#x, %#x) = %#x; want %#x", tt.x, tt.y, tt.z, result, tt.expect) - } - } -} - -// Test round function -func TestRound(t *testing.T) { - tests := []struct { - name string - f func(x, y, z uint32) uint32 - a, b, c, d uint32 - x uint32 - s uint - expect uint32 - }{ - {"f", f, 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0x12345678, 3, 0x91a2b3b8}, - {"g", g, 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0x12345678, 5, 0x468acee2}, - {"h", h, 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0x12345678, 7, 0x5f4e3d70}, - } - - for _, tt := range tests { - a, b, c, d := tt.a, tt.b, tt.c, tt.d - result := round(tt.f, a, b, c, d, tt.x, tt.s) - if result != tt.expect { - t.Errorf("round(%s, %d) = %#x; want %#x", tt.name, tt.s, result, tt.expect) - } - } -} - -func TestHalfMD4Transform(t *testing.T) { - var buf = [4]uint32{0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476} - tests := []struct { - name string - in [8]uint32 - expect uint32 - }{ - { - name: "Test Case 1", - in: [8]uint32{0, 1, 2, 3, 4, 5, 6, 7}, - expect: 0xF254F422, - }, - { - name: "Test Case 2", - in: [8]uint32{0x12345678, 0x9ABCDEF0, 0x0FEDCBA9, 0x87654321, 0x11223344, 0xAABBCCDD, 0x55667788, 0x99AABBCC}, - expect: 0xA4405E22, - }, - { - name: "Test Case 3", - in: [8]uint32{0x00000000, 0xFFFFFFFF, 0xAAAAAAAA, 0x55555555, 0x33333333, 0x66666666, 0x99999999, 0xCCCCCCCC}, - expect: 0x35B92DEF, - }, - { - name: "Test Case 4 (Empty Input)", - in: [8]uint32{0, 0, 0, 0, 0, 0, 0, 0}, - expect: 0x5B0AA4BE, - }, - { - name: "Test Case 5 (Random Input)", - in: [8]uint32{0x89ABCDEF, 0x01234567, 0xFEDCBA98, 0x76543210, 0xA1B2C3D4, 0x0BADC0DE, 0xDEADBEEF, 0xCAFEBABE}, - expect: 0x2748FDB6, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := HalfMD4Transform(buf, tt.in[:]) - if result != tt.expect { - t.Errorf("halfMD4Transform(%#v, %#v) = %#x; want %#x", buf, tt.in, result, tt.expect) - } - }) - } -}