^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/ext4/hash.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2002 by Theodore Ts'o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/unicode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define DELTA 0x9E3779B9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static void TEA_transform(__u32 buf[4], __u32 const in[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) __u32 sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) __u32 b0 = buf[0], b1 = buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) __u32 a = in[0], b = in[1], c = in[2], d = in[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int n = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) sum += DELTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) } while (--n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) buf[0] += b0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) buf[1] += b1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* F, G and H are basic MD4 functions: selection, majority, parity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define H(x, y, z) ((x) ^ (y) ^ (z))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * The generic round function. The application is so specific that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * we don't bother protecting all the arguments with parens, as is generally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * good macro practice, in favor of extra legibility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Rotation is separate from addition to prevent recomputation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define ROUND(f, a, b, c, d, x, s) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) (a += f(b, c, d) + x, a = rol32(a, s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define K1 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define K2 013240474631UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define K3 015666365641UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Basic cut-down MD4 transform. Returns only 32 bits of result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static __u32 half_md4_transform(__u32 buf[4], __u32 const in[8])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Round 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ROUND(F, a, b, c, d, in[0] + K1, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ROUND(F, d, a, b, c, in[1] + K1, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ROUND(F, c, d, a, b, in[2] + K1, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ROUND(F, b, c, d, a, in[3] + K1, 19);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ROUND(F, a, b, c, d, in[4] + K1, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ROUND(F, d, a, b, c, in[5] + K1, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ROUND(F, c, d, a, b, in[6] + K1, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ROUND(F, b, c, d, a, in[7] + K1, 19);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* Round 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ROUND(G, a, b, c, d, in[1] + K2, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ROUND(G, d, a, b, c, in[3] + K2, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ROUND(G, c, d, a, b, in[5] + K2, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ROUND(G, b, c, d, a, in[7] + K2, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ROUND(G, a, b, c, d, in[0] + K2, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ROUND(G, d, a, b, c, in[2] + K2, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ROUND(G, c, d, a, b, in[4] + K2, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ROUND(G, b, c, d, a, in[6] + K2, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Round 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ROUND(H, a, b, c, d, in[3] + K3, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ROUND(H, d, a, b, c, in[7] + K3, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ROUND(H, c, d, a, b, in[2] + K3, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ROUND(H, b, c, d, a, in[6] + K3, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) ROUND(H, a, b, c, d, in[1] + K3, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ROUND(H, d, a, b, c, in[5] + K3, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ROUND(H, c, d, a, b, in[0] + K3, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ROUND(H, b, c, d, a, in[4] + K3, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) buf[0] += a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) buf[1] += b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) buf[2] += c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) buf[3] += d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return buf[1]; /* "most hashed" word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #undef ROUND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #undef K1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #undef K2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #undef K3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #undef F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #undef G
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #undef H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* The old legacy hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static __u32 dx_hack_hash_unsigned(const char *name, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) const unsigned char *ucp = (const unsigned char *) name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) hash = hash1 + (hash0 ^ (((int) *ucp++) * 7152373));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (hash & 0x80000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) hash -= 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) hash1 = hash0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) hash0 = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return hash0 << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static __u32 dx_hack_hash_signed(const char *name, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const signed char *scp = (const signed char *) name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) hash = hash1 + (hash0 ^ (((int) *scp++) * 7152373));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (hash & 0x80000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) hash -= 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) hash1 = hash0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) hash0 = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return hash0 << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void str2hashbuf_signed(const char *msg, int len, __u32 *buf, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) __u32 pad, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) const signed char *scp = (const signed char *) msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pad = (__u32)len | ((__u32)len << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) pad |= pad << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (len > num*4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) len = num * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) for (i = 0; i < len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) val = ((int) scp[i]) + (val << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if ((i % 4) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *buf++ = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) num--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (--num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *buf++ = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) while (--num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *buf++ = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) __u32 pad, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) const unsigned char *ucp = (const unsigned char *) msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pad = (__u32)len | ((__u32)len << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pad |= pad << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (len > num*4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) len = num * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) for (i = 0; i < len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) val = ((int) ucp[i]) + (val << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if ((i % 4) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *buf++ = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) num--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (--num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) *buf++ = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) while (--num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *buf++ = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Returns the hash of a filename. If len is 0 and name is NULL, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * this function can be used to test whether or not a hash version is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * The seed is an 4 longword (32 bits) "secret" which can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * uniquify a hash. If the seed is all zero's, then some default seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * may be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * A particular hash version specifies whether or not the seed is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * represented, and whether or not the returned hash is 32 bits or 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * bits. 32 bit hashes will return 0 for the minor hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int __ext4fs_dirhash(const struct inode *dir, const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct dx_hash_info *hinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) __u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) __u32 minor_hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) __u32 in[8], buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) void (*str2hashbuf)(const char *, int, __u32 *, int) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) str2hashbuf_signed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* Initialize the default seed for the hash checksum functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) buf[0] = 0x67452301;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) buf[1] = 0xefcdab89;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) buf[2] = 0x98badcfe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) buf[3] = 0x10325476;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* Check to see if the seed is all zero's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (hinfo->seed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (hinfo->seed[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) memcpy(buf, hinfo->seed, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) switch (hinfo->hash_version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) case DX_HASH_LEGACY_UNSIGNED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) hash = dx_hack_hash_unsigned(name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case DX_HASH_LEGACY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) hash = dx_hack_hash_signed(name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) case DX_HASH_HALF_MD4_UNSIGNED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) str2hashbuf = str2hashbuf_unsigned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) case DX_HASH_HALF_MD4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) p = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) (*str2hashbuf)(p, len, in, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) half_md4_transform(buf, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) len -= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) p += 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) minor_hash = buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) hash = buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) case DX_HASH_TEA_UNSIGNED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) str2hashbuf = str2hashbuf_unsigned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) case DX_HASH_TEA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) p = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) (*str2hashbuf)(p, len, in, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) TEA_transform(buf, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) len -= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) p += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) hash = buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) minor_hash = buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case DX_HASH_SIPHASH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct qstr qname = QSTR_INIT(name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __u64 combined_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (fscrypt_has_encryption_key(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) combined_hash = fscrypt_fname_siphash(dir, &qname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ext4_warning_inode(dir, "Siphash requires key");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) hash = (__u32)(combined_hash >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) minor_hash = (__u32)combined_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) hinfo->hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) hash = hash & ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (hash == (EXT4_HTREE_EOF_32BIT << 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) hash = (EXT4_HTREE_EOF_32BIT - 1) << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) hinfo->hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) hinfo->minor_hash = minor_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct dx_hash_info *hinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) const struct unicode_map *um = dir->i_sb->s_encoding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int r, dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) unsigned char *buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct qstr qstr = {.name = name, .len = len };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (len && IS_CASEFOLDED(dir) && um &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) (!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) buff = kzalloc(sizeof(char) * PATH_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dlen = utf8_casefold(um, &qstr, buff, PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (dlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) kfree(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto opaque_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) r = __ext4fs_dirhash(dir, buff, dlen, hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) kfree(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) opaque_seq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return __ext4fs_dirhash(dir, name, len, hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }