dirhash.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * dirhash.c -- Calculate the hash of a directory entry
  4. *
  5. * Copyright (c) 2001 Daniel Phillips
  6. *
  7. * Copyright (c) 2002 Theodore Ts'o.
  8. *
  9. * %Begin-Header%
  10. * This file may be redistributed under the terms of the GNU Public
  11. * License.
  12. * %End-Header%
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "ext2_fs.h"
  17. #include "ext2fs.h"
  18. /*
  19. * Keyed 32-bit hash function using TEA in a Davis-Meyer function
  20. * H0 = Key
  21. * Hi = E Mi(Hi-1) + Hi-1
  22. *
  23. * (see Applied Cryptography, 2nd edition, p448).
  24. *
  25. * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
  26. *
  27. * This code is made available under the terms of the GPL
  28. */
  29. #define DELTA 0x9E3779B9
  30. static void TEA_transform(__u32 buf[4], __u32 const in[])
  31. {
  32. __u32 sum = 0;
  33. __u32 b0 = buf[0], b1 = buf[1];
  34. __u32 a = in[0], b = in[1], c = in[2], d = in[3];
  35. int n = 16;
  36. do {
  37. sum += DELTA;
  38. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  39. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  40. } while(--n);
  41. buf[0] += b0;
  42. buf[1] += b1;
  43. }
  44. /* F, G and H are basic MD4 functions: selection, majority, parity */
  45. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  46. #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
  47. #define H(x, y, z) ((x) ^ (y) ^ (z))
  48. /*
  49. * The generic round function. The application is so specific that
  50. * we don't bother protecting all the arguments with parens, as is generally
  51. * good macro practice, in favor of extra legibility.
  52. * Rotation is separate from addition to prevent recomputation
  53. */
  54. #define ROUND(f, a, b, c, d, x, s) \
  55. (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
  56. #define K1 0
  57. #define K2 013240474631UL
  58. #define K3 015666365641UL
  59. /*
  60. * Basic cut-down MD4 transform. Returns only 32 bits of result.
  61. */
  62. static void halfMD4Transform (__u32 buf[4], __u32 const in[])
  63. {
  64. __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  65. /* Round 1 */
  66. ROUND(F, a, b, c, d, in[0] + K1, 3);
  67. ROUND(F, d, a, b, c, in[1] + K1, 7);
  68. ROUND(F, c, d, a, b, in[2] + K1, 11);
  69. ROUND(F, b, c, d, a, in[3] + K1, 19);
  70. ROUND(F, a, b, c, d, in[4] + K1, 3);
  71. ROUND(F, d, a, b, c, in[5] + K1, 7);
  72. ROUND(F, c, d, a, b, in[6] + K1, 11);
  73. ROUND(F, b, c, d, a, in[7] + K1, 19);
  74. /* Round 2 */
  75. ROUND(G, a, b, c, d, in[1] + K2, 3);
  76. ROUND(G, d, a, b, c, in[3] + K2, 5);
  77. ROUND(G, c, d, a, b, in[5] + K2, 9);
  78. ROUND(G, b, c, d, a, in[7] + K2, 13);
  79. ROUND(G, a, b, c, d, in[0] + K2, 3);
  80. ROUND(G, d, a, b, c, in[2] + K2, 5);
  81. ROUND(G, c, d, a, b, in[4] + K2, 9);
  82. ROUND(G, b, c, d, a, in[6] + K2, 13);
  83. /* Round 3 */
  84. ROUND(H, a, b, c, d, in[3] + K3, 3);
  85. ROUND(H, d, a, b, c, in[7] + K3, 9);
  86. ROUND(H, c, d, a, b, in[2] + K3, 11);
  87. ROUND(H, b, c, d, a, in[6] + K3, 15);
  88. ROUND(H, a, b, c, d, in[1] + K3, 3);
  89. ROUND(H, d, a, b, c, in[5] + K3, 9);
  90. ROUND(H, c, d, a, b, in[0] + K3, 11);
  91. ROUND(H, b, c, d, a, in[4] + K3, 15);
  92. buf[0] += a;
  93. buf[1] += b;
  94. buf[2] += c;
  95. buf[3] += d;
  96. }
  97. #undef ROUND
  98. #undef F
  99. #undef G
  100. #undef H
  101. #undef K1
  102. #undef K2
  103. #undef K3
  104. /* The old legacy hash */
  105. static ext2_dirhash_t dx_hack_hash (const char *name, int len)
  106. {
  107. __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
  108. while (len--) {
  109. __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
  110. if (hash & 0x80000000) hash -= 0x7fffffff;
  111. hash1 = hash0;
  112. hash0 = hash;
  113. }
  114. return (hash0 << 1);
  115. }
  116. static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
  117. {
  118. __u32 pad, val;
  119. int i;
  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. if ((i % 4) == 0)
  127. val = pad;
  128. val = msg[i] + (val << 8);
  129. if ((i % 4) == 3) {
  130. *buf++ = val;
  131. val = pad;
  132. num--;
  133. }
  134. }
  135. if (--num >= 0)
  136. *buf++ = val;
  137. while (--num >= 0)
  138. *buf++ = pad;
  139. }
  140. /*
  141. * Returns the hash of a filename. If len is 0 and name is NULL, then
  142. * this function can be used to test whether or not a hash version is
  143. * supported.
  144. *
  145. * The seed is an 4 longword (32 bits) "secret" which can be used to
  146. * uniquify a hash. If the seed is all zero's, then some default seed
  147. * may be used.
  148. *
  149. * A particular hash version specifies whether or not the seed is
  150. * represented, and whether or not the returned hash is 32 bits or 64
  151. * bits. 32 bit hashes will return 0 for the minor hash.
  152. */
  153. errcode_t ext2fs_dirhash(int version, const char *name, int len,
  154. const __u32 *seed,
  155. ext2_dirhash_t *ret_hash,
  156. ext2_dirhash_t *ret_minor_hash)
  157. {
  158. __u32 hash;
  159. __u32 minor_hash = 0;
  160. const char *p;
  161. int i;
  162. __u32 in[8], buf[4];
  163. /* Initialize the default seed for the hash checksum functions */
  164. buf[0] = 0x67452301;
  165. buf[1] = 0xefcdab89;
  166. buf[2] = 0x98badcfe;
  167. buf[3] = 0x10325476;
  168. /* Check to see if the seed is all zero's */
  169. if (seed) {
  170. for (i=0; i < 4; i++) {
  171. if (seed[i])
  172. break;
  173. }
  174. if (i < 4)
  175. memcpy(buf, seed, sizeof(buf));
  176. }
  177. switch (version) {
  178. case EXT2_HASH_LEGACY:
  179. hash = dx_hack_hash(name, len);
  180. break;
  181. case EXT2_HASH_HALF_MD4:
  182. p = name;
  183. while (len > 0) {
  184. str2hashbuf(p, len, in, 8);
  185. halfMD4Transform(buf, in);
  186. len -= 32;
  187. p += 32;
  188. }
  189. minor_hash = buf[2];
  190. hash = buf[1];
  191. break;
  192. case EXT2_HASH_TEA:
  193. p = name;
  194. while (len > 0) {
  195. str2hashbuf(p, len, in, 4);
  196. TEA_transform(buf, in);
  197. len -= 16;
  198. p += 16;
  199. }
  200. hash = buf[0];
  201. minor_hash = buf[1];
  202. break;
  203. default:
  204. *ret_hash = 0;
  205. return EXT2_ET_DIRHASH_UNSUPP;
  206. }
  207. *ret_hash = hash & ~1;
  208. if (ret_minor_hash)
  209. *ret_minor_hash = minor_hash;
  210. return 0;
  211. }