hash.c 6.1 KB

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