hash.c 6.2 KB

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