hash.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if ((i % 4) == 0)
  130. val = pad;
  131. val = ((int) scp[i]) + (val << 8);
  132. if ((i % 4) == 3) {
  133. *buf++ = val;
  134. val = pad;
  135. num--;
  136. }
  137. }
  138. if (--num >= 0)
  139. *buf++ = val;
  140. while (--num >= 0)
  141. *buf++ = pad;
  142. }
  143. static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)
  144. {
  145. __u32 pad, val;
  146. int i;
  147. const unsigned char *ucp = (const unsigned char *) msg;
  148. pad = (__u32)len | ((__u32)len << 8);
  149. pad |= pad << 16;
  150. val = pad;
  151. if (len > num*4)
  152. len = num * 4;
  153. for (i = 0; i < len; i++) {
  154. if ((i % 4) == 0)
  155. val = pad;
  156. val = ((int) ucp[i]) + (val << 8);
  157. if ((i % 4) == 3) {
  158. *buf++ = val;
  159. val = pad;
  160. num--;
  161. }
  162. }
  163. if (--num >= 0)
  164. *buf++ = val;
  165. while (--num >= 0)
  166. *buf++ = pad;
  167. }
  168. /*
  169. * Returns the hash of a filename. If len is 0 and name is NULL, then
  170. * this function can be used to test whether or not a hash version is
  171. * supported.
  172. *
  173. * The seed is an 4 longword (32 bits) "secret" which can be used to
  174. * uniquify a hash. If the seed is all zero's, then some default seed
  175. * may be used.
  176. *
  177. * A particular hash version specifies whether or not the seed is
  178. * represented, and whether or not the returned hash is 32 bits or 64
  179. * bits. 32 bit hashes will return 0 for the minor hash.
  180. */
  181. int ext4fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
  182. {
  183. __u32 hash;
  184. __u32 minor_hash = 0;
  185. const char *p;
  186. int i;
  187. __u32 in[8], buf[4];
  188. void (*str2hashbuf)(const char *, int, __u32 *, int) =
  189. str2hashbuf_signed;
  190. /* Initialize the default seed for the hash checksum functions */
  191. buf[0] = 0x67452301;
  192. buf[1] = 0xefcdab89;
  193. buf[2] = 0x98badcfe;
  194. buf[3] = 0x10325476;
  195. /* Check to see if the seed is all zero's */
  196. if (hinfo->seed) {
  197. for (i = 0; i < 4; i++) {
  198. if (hinfo->seed[i]) {
  199. memcpy(buf, hinfo->seed, sizeof(buf));
  200. break;
  201. }
  202. }
  203. }
  204. switch (hinfo->hash_version) {
  205. case DX_HASH_LEGACY_UNSIGNED:
  206. hash = dx_hack_hash_unsigned(name, len);
  207. break;
  208. case DX_HASH_LEGACY:
  209. hash = dx_hack_hash_signed(name, len);
  210. break;
  211. case DX_HASH_HALF_MD4_UNSIGNED:
  212. str2hashbuf = str2hashbuf_unsigned;
  213. case DX_HASH_HALF_MD4:
  214. p = name;
  215. while (len > 0) {
  216. (*str2hashbuf)(p, len, in, 8);
  217. half_md4_transform(buf, in);
  218. len -= 32;
  219. p += 32;
  220. }
  221. minor_hash = buf[2];
  222. hash = buf[1];
  223. break;
  224. case DX_HASH_TEA_UNSIGNED:
  225. str2hashbuf = str2hashbuf_unsigned;
  226. case DX_HASH_TEA:
  227. p = name;
  228. while (len > 0) {
  229. (*str2hashbuf)(p, len, in, 4);
  230. TEA_transform(buf, in);
  231. len -= 16;
  232. p += 16;
  233. }
  234. hash = buf[0];
  235. minor_hash = buf[1];
  236. break;
  237. default:
  238. hinfo->hash = 0;
  239. return -1;
  240. }
  241. hash = hash & ~1;
  242. if (hash == (EXT4_HTREE_EOF_32BIT << 1))
  243. hash = (EXT4_HTREE_EOF_32BIT - 1) << 1;
  244. hinfo->hash = hash;
  245. hinfo->minor_hash = minor_hash;
  246. return 0;
  247. }