1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2002 by Theodore Ts'o
9 #include <linux/unicode.h>
10 #include <linux/compiler.h>
11 #include <linux/bitops.h>
14 #define DELTA 0x9E3779B9
16 static void TEA_transform(__u32 buf[4], __u32 const in[])
19 __u32 b0 = buf[0], b1 = buf[1];
20 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
25 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
26 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
33 /* F, G and H are basic MD4 functions: selection, majority, parity */
34 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
35 #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
36 #define H(x, y, z) ((x) ^ (y) ^ (z))
39 * The generic round function. The application is so specific that
40 * we don't bother protecting all the arguments with parens, as is generally
41 * good macro practice, in favor of extra legibility.
42 * Rotation is separate from addition to prevent recomputation
44 #define ROUND(f, a, b, c, d, x, s) \
45 (a += f(b, c, d) + x, a = rol32(a, s))
47 #define K2 013240474631UL
48 #define K3 015666365641UL
51 * Basic cut-down MD4 transform. Returns only 32 bits of result.
53 static __u32 half_md4_transform(__u32 buf[4], __u32 const in[8])
55 __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
58 ROUND(F, a, b, c, d, in[0] + K1, 3);
59 ROUND(F, d, a, b, c, in[1] + K1, 7);
60 ROUND(F, c, d, a, b, in[2] + K1, 11);
61 ROUND(F, b, c, d, a, in[3] + K1, 19);
62 ROUND(F, a, b, c, d, in[4] + K1, 3);
63 ROUND(F, d, a, b, c, in[5] + K1, 7);
64 ROUND(F, c, d, a, b, in[6] + K1, 11);
65 ROUND(F, b, c, d, a, in[7] + K1, 19);
68 ROUND(G, a, b, c, d, in[1] + K2, 3);
69 ROUND(G, d, a, b, c, in[3] + K2, 5);
70 ROUND(G, c, d, a, b, in[5] + K2, 9);
71 ROUND(G, b, c, d, a, in[7] + K2, 13);
72 ROUND(G, a, b, c, d, in[0] + K2, 3);
73 ROUND(G, d, a, b, c, in[2] + K2, 5);
74 ROUND(G, c, d, a, b, in[4] + K2, 9);
75 ROUND(G, b, c, d, a, in[6] + K2, 13);
78 ROUND(H, a, b, c, d, in[3] + K3, 3);
79 ROUND(H, d, a, b, c, in[7] + K3, 9);
80 ROUND(H, c, d, a, b, in[2] + K3, 11);
81 ROUND(H, b, c, d, a, in[6] + K3, 15);
82 ROUND(H, a, b, c, d, in[1] + K3, 3);
83 ROUND(H, d, a, b, c, in[5] + K3, 9);
84 ROUND(H, c, d, a, b, in[0] + K3, 11);
85 ROUND(H, b, c, d, a, in[4] + K3, 15);
92 return buf[1]; /* "most hashed" word */
102 /* The old legacy hash */
103 static __u32 dx_hack_hash_unsigned(const char *name, int len)
105 __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
106 const unsigned char *ucp = (const unsigned char *) name;
109 hash = hash1 + (hash0 ^ (((int) *ucp++) * 7152373));
111 if (hash & 0x80000000)
119 static __u32 dx_hack_hash_signed(const char *name, int len)
121 __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
122 const signed char *scp = (const signed char *) name;
125 hash = hash1 + (hash0 ^ (((int) *scp++) * 7152373));
127 if (hash & 0x80000000)
135 static void str2hashbuf_signed(const char *msg, int len, __u32 *buf, int num)
139 const signed char *scp = (const signed char *) msg;
141 pad = (__u32)len | ((__u32)len << 8);
147 for (i = 0; i < len; i++) {
148 val = ((int) scp[i]) + (val << 8);
161 static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)
165 const unsigned char *ucp = (const unsigned char *) msg;
167 pad = (__u32)len | ((__u32)len << 8);
173 for (i = 0; i < len; i++) {
174 val = ((int) ucp[i]) + (val << 8);
188 * Returns the hash of a filename. If len is 0 and name is NULL, then
189 * this function can be used to test whether or not a hash version is
192 * The seed is an 4 longword (32 bits) "secret" which can be used to
193 * uniquify a hash. If the seed is all zero's, then some default seed
196 * A particular hash version specifies whether or not the seed is
197 * represented, and whether or not the returned hash is 32 bits or 64
198 * bits. 32 bit hashes will return 0 for the minor hash.
200 static int __ext4fs_dirhash(const struct inode *dir, const char *name, int len,
201 struct dx_hash_info *hinfo)
204 __u32 minor_hash = 0;
208 void (*str2hashbuf)(const char *, int, __u32 *, int) =
211 /* Initialize the default seed for the hash checksum functions */
217 /* Check to see if the seed is all zero's */
219 for (i = 0; i < 4; i++) {
220 if (hinfo->seed[i]) {
221 memcpy(buf, hinfo->seed, sizeof(buf));
227 switch (hinfo->hash_version) {
228 case DX_HASH_LEGACY_UNSIGNED:
229 hash = dx_hack_hash_unsigned(name, len);
232 hash = dx_hack_hash_signed(name, len);
234 case DX_HASH_HALF_MD4_UNSIGNED:
235 str2hashbuf = str2hashbuf_unsigned;
237 case DX_HASH_HALF_MD4:
240 (*str2hashbuf)(p, len, in, 8);
241 half_md4_transform(buf, in);
248 case DX_HASH_TEA_UNSIGNED:
249 str2hashbuf = str2hashbuf_unsigned;
254 (*str2hashbuf)(p, len, in, 4);
255 TEA_transform(buf, in);
262 case DX_HASH_SIPHASH:
264 struct qstr qname = QSTR_INIT(name, len);
267 if (fscrypt_has_encryption_key(dir)) {
268 combined_hash = fscrypt_fname_siphash(dir, &qname);
270 ext4_warning_inode(dir, "Siphash requires key");
274 hash = (__u32)(combined_hash >> 32);
275 minor_hash = (__u32)combined_hash;
280 hinfo->minor_hash = 0;
281 ext4_warning(dir->i_sb,
282 "invalid/unsupported hash tree version %u",
283 hinfo->hash_version);
287 if (hash == (EXT4_HTREE_EOF_32BIT << 1))
288 hash = (EXT4_HTREE_EOF_32BIT - 1) << 1;
290 hinfo->minor_hash = minor_hash;
294 int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
295 struct dx_hash_info *hinfo)
297 #if IS_ENABLED(CONFIG_UNICODE)
298 const struct unicode_map *um = dir->i_sb->s_encoding;
301 struct qstr qstr = {.name = name, .len = len };
303 if (len && IS_CASEFOLDED(dir) && um &&
304 (!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir))) {
305 buff = kzalloc(sizeof(char) * PATH_MAX, GFP_KERNEL);
309 dlen = utf8_casefold(um, &qstr, buff, PATH_MAX);
315 r = __ext4fs_dirhash(dir, buff, dlen, hinfo);
322 return __ext4fs_dirhash(dir, name, len, hinfo);