hash.c 7.4 KB

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